descriptions for each class created
This commit is contained in:
parent
81a1a67b6b
commit
6dfaf331f8
@ -8,7 +8,11 @@ import java.io.IOException;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
// WYKORZYSTANIE STRUMIENIA
|
/*
|
||||||
|
Klasa odczytujaca oraz wypisujaca dostepne miasta z pliku .json.
|
||||||
|
Wykorzystanie strumienia.
|
||||||
|
*/
|
||||||
|
|
||||||
public class CityNames {
|
public class CityNames {
|
||||||
public static void cityNamesRead(String filePath) {
|
public static void cityNamesRead(String filePath) {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
@ -5,6 +5,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Klasa odczytujaca z odpowiedzi API wyamagane informacje o prognozie pogody.
|
||||||
|
*/
|
||||||
|
|
||||||
public class ForecastPreparation {
|
public class ForecastPreparation {
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,28 +10,28 @@ import org.apache.http.util.EntityUtils;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Klasa tworzaca klienta Http i wykonujaca zapytanie GET
|
||||||
|
*/
|
||||||
|
|
||||||
public class HttpClient {
|
public class HttpClient {
|
||||||
|
|
||||||
private CloseableHttpClient httpClient;
|
private CloseableHttpClient httpClient;
|
||||||
|
|
||||||
// Konstruktor domyślny, używa domyślnego klienta HTTP
|
|
||||||
public HttpClient() {
|
public HttpClient() {
|
||||||
this.httpClient = HttpClients.createDefault();
|
this.httpClient = HttpClients.createDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Konstruktor umożliwiający ustawienie własnego klienta HTTP (do użycia w testach)
|
|
||||||
public HttpClient(CloseableHttpClient httpClient) {
|
public HttpClient(CloseableHttpClient httpClient) {
|
||||||
this.httpClient = httpClient;
|
this.httpClient = httpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Metoda umożliwiająca ustawienie własnego klienta HTTP (do użycia w testach)
|
// public void setHttpClient(CloseableHttpClient httpClient) {
|
||||||
public void setHttpClient(CloseableHttpClient httpClient) {
|
// this.httpClient = httpClient;
|
||||||
this.httpClient = httpClient;
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
public String executeGetRequest(String urlString) {
|
public String executeGetRequest(String urlString) {
|
||||||
|
|
||||||
// CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
||||||
HttpGet request = new HttpGet(urlString);
|
HttpGet request = new HttpGet(urlString);
|
||||||
|
|
||||||
try (CloseableHttpResponse response = httpClient.execute(request)) {
|
try (CloseableHttpResponse response = httpClient.execute(request)) {
|
||||||
|
@ -6,6 +6,11 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Klasa laczaca dostepne nazwy miast i ich koordynaty w mape.
|
||||||
|
*/
|
||||||
|
|
||||||
public class JsonToMap {
|
public class JsonToMap {
|
||||||
public static Map<String, Map<String, Double>> createCityMap(String jsonFile) {
|
public static Map<String, Map<String, Double>> createCityMap(String jsonFile) {
|
||||||
|
|
||||||
|
@ -4,6 +4,10 @@ import java.io.BufferedWriter;
|
|||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Klasa tworzaca pusty plik temporary.
|
||||||
|
*/
|
||||||
|
|
||||||
public class TemporaryFile {
|
public class TemporaryFile {
|
||||||
public static final String FILE_NAME = "temporary_file.txt";
|
public static final String FILE_NAME = "temporary_file.txt";
|
||||||
|
|
||||||
|
@ -14,6 +14,12 @@ import java.io.BufferedReader;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Klasa weatherForecast zawierajaca metody pozwalajace na zapis do poszczegolnych formatow.
|
||||||
|
Obiekty tworzone poprzez wywolanie klasy zbierane sa do listy ulatwiajacej generacje plikow json i xml.
|
||||||
|
Do zapisu do pdf wykorzystywany jest plik temporary zbierajacy wywolane prognozy w formacie txt.
|
||||||
|
*/
|
||||||
|
|
||||||
public class weatherForecast {
|
public class weatherForecast {
|
||||||
public static final List<weatherForecast> allEntries = new ArrayList<>();
|
public static final List<weatherForecast> allEntries = new ArrayList<>();
|
||||||
private final String cityName;
|
private final String cityName;
|
||||||
|
@ -10,6 +10,9 @@ import java.time.format.DateTimeFormatter;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Klasa Main
|
||||||
|
*/
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
|
@ -3,15 +3,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import model.HttpClient;
|
import model.HttpClient;
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.apache.http.HttpEntity;
|
|
||||||
import org.apache.http.client.ClientProtocolException;
|
import org.apache.http.client.ClientProtocolException;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.ArgumentMatchers;
|
import org.mockito.ArgumentMatchers;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
@ -20,19 +15,20 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Zgodnie z wymogami projektu - trzy testy mockujace przygotowane dla klasy HttpClient.
|
||||||
|
*/
|
||||||
|
|
||||||
public class HttpClientTest {
|
public class HttpClientTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testExecuteGetRequest() throws Exception {
|
void testExecuteGetRequest() throws Exception {
|
||||||
// String API_key = "4c991761f17e10358c944a3c64a3e24c";
|
|
||||||
// double longitude = 10.99;
|
// Dowolny adres URL
|
||||||
// double latitude = 44.34;
|
|
||||||
// String urlString = "https://api.openweathermap.org/data/2.5/weather?lat=" + latitude +
|
|
||||||
// "&lon=" + longitude + "&appid=" + API_key;
|
|
||||||
String urlString = "www.example.com";
|
String urlString = "www.example.com";
|
||||||
|
|
||||||
|
// Przykladowy response
|
||||||
String expectedResponse = "{\"coord\":{\"lon\":44.34,\"lat\":10.99},\"weather\":" +
|
String expectedResponse = "{\"coord\":{\"lon\":44.34,\"lat\":10.99},\"weather\":" +
|
||||||
"[{\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\"" +
|
"[{\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\"" +
|
||||||
",\"icon\":\"04n\"}],\"base\":\"stations\",\"main\":{\"temp\":299.46,\"" +
|
",\"icon\":\"04n\"}],\"base\":\"stations\",\"main\":{\"temp\":299.46,\"" +
|
||||||
@ -43,64 +39,53 @@ public class HttpClientTest {
|
|||||||
"\"sunrise\":1701918532,\"sunset\":1701959952},\"timezone\":10800,\"id\"" +
|
"\"sunrise\":1701918532,\"sunset\":1701959952},\"timezone\":10800,\"id\"" +
|
||||||
":54746,\"name\":\"Lughaye\",\"cod\":200}\n";
|
":54746,\"name\":\"Lughaye\",\"cod\":200}\n";
|
||||||
|
|
||||||
// Tworzymy mock dla CloseableHttpClient
|
// Przygotowanie mockow
|
||||||
CloseableHttpClient mockHttpClient = Mockito.mock(CloseableHttpClient.class);
|
CloseableHttpClient mockHttpClient = Mockito.mock(CloseableHttpClient.class);
|
||||||
|
|
||||||
// Tworzymy mock dla HttpEntity
|
|
||||||
org.apache.http.HttpEntity mockEntity = Mockito.mock(org.apache.http.HttpEntity.class);
|
org.apache.http.HttpEntity mockEntity = Mockito.mock(org.apache.http.HttpEntity.class);
|
||||||
|
|
||||||
// Przygotowujemy odpowiedź do zwrócenia przez mocka
|
|
||||||
CloseableHttpResponse mockResponse = Mockito.mock(CloseableHttpResponse.class);
|
CloseableHttpResponse mockResponse = Mockito.mock(CloseableHttpResponse.class);
|
||||||
when(mockResponse.getEntity()).thenReturn(mockEntity);
|
when(mockResponse.getEntity()).thenReturn(mockEntity);
|
||||||
|
|
||||||
// Konfigurujemy mockEntity do zwrócenia StringEntity
|
|
||||||
when(mockEntity.getContent()).thenReturn(new ByteArrayInputStream(expectedResponse.getBytes(StandardCharsets.UTF_8)));
|
when(mockEntity.getContent()).thenReturn(new ByteArrayInputStream(expectedResponse.getBytes(StandardCharsets.UTF_8)));
|
||||||
when(mockEntity.getContentLength()).thenReturn((long) expectedResponse.length());
|
when(mockEntity.getContentLength()).thenReturn((long) expectedResponse.length());
|
||||||
|
|
||||||
// Przygotowujemy odpowiedź do zwrócenia przez mock
|
// Przygotowujemy odpowiedź do zwrócenia przez mock
|
||||||
when(mockHttpClient.execute(any(HttpGet.class))).thenReturn(mockResponse);
|
when(mockHttpClient.execute(any(HttpGet.class))).thenReturn(mockResponse);
|
||||||
|
|
||||||
// Tworzymy instancję testowanej klasy, przekazując mockHttpClient
|
// Instancja testowanej klasy z przekazaniem zmockowanego klienta
|
||||||
HttpClient httpClient = new HttpClient(mockHttpClient);
|
HttpClient httpClient = new HttpClient(mockHttpClient);
|
||||||
|
|
||||||
// Wywołujemy metodę, którą chcemy przetestować
|
// Act & Assert
|
||||||
String result = httpClient.executeGetRequest(urlString);
|
String result = httpClient.executeGetRequest(urlString);
|
||||||
|
assertEquals(expectedResponse, result);
|
||||||
// Sprawdzamy, czy wynik metody jest zgodny z oczekiwaniami
|
|
||||||
Assertions.assertEquals(expectedResponse, result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testExecuteGetRequest_EmptyResponse() throws IOException {
|
void testExecuteGetRequest_EmptyResponse() throws IOException {
|
||||||
// String API_key = "4c991761f17e10358c944a3c64a3e24c";
|
|
||||||
// double longitude = 10.99;
|
|
||||||
// double latitude = 44.34;
|
|
||||||
//
|
|
||||||
// String urlString = "https://api.openweathermap.org/data/2.5/weather?lat=" + latitude +
|
|
||||||
// "&lon=" + longitude + "&appid=" + API_key;
|
|
||||||
|
|
||||||
|
// Dowolny adres url
|
||||||
String urlString = "www.example.com";
|
String urlString = "www.example.com";
|
||||||
// Arrange
|
|
||||||
|
// Przygotowanie mockow
|
||||||
CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class);
|
CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class);
|
||||||
CloseableHttpResponse mockHttpResponse = mock(CloseableHttpResponse.class);
|
CloseableHttpResponse mockHttpResponse = mock(CloseableHttpResponse.class);
|
||||||
when(mockHttpClient.execute(ArgumentMatchers.any(HttpGet.class))).thenReturn(mockHttpResponse);
|
when(mockHttpClient.execute(ArgumentMatchers.any(HttpGet.class))).thenReturn(mockHttpResponse);
|
||||||
when(mockHttpResponse.getEntity()).thenReturn(null); // Brak treści w odpowiedzi
|
when(mockHttpResponse.getEntity()).thenReturn(null);
|
||||||
|
|
||||||
|
// Instancja testowanej klasy z przekazaniem zmockowanego klienta
|
||||||
HttpClient httpClient = new HttpClient(mockHttpClient);
|
HttpClient httpClient = new HttpClient(mockHttpClient);
|
||||||
|
|
||||||
// Act
|
// Act & Assert
|
||||||
String result = httpClient.executeGetRequest(urlString);
|
String result = httpClient.executeGetRequest(urlString);
|
||||||
|
|
||||||
// Assert
|
assertEquals("", result);
|
||||||
assertEquals("", result); // Oczekiwany rezultat - pusty string
|
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
void testExecuteGetRequest_ClientProtocolException() throws IOException {
|
void testExecuteGetRequest_ClientProtocolException() throws IOException {
|
||||||
// Arrange
|
|
||||||
|
// Przygotowanie mockow
|
||||||
CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class);
|
CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class);
|
||||||
CloseableHttpResponse mockHttpResponse = mock(CloseableHttpResponse.class);
|
|
||||||
when(mockHttpClient.execute(ArgumentMatchers.any(HttpGet.class))).thenThrow(new ClientProtocolException("Mocked ClientProtocolException"));
|
when(mockHttpClient.execute(ArgumentMatchers.any(HttpGet.class))).thenThrow(new ClientProtocolException("Mocked ClientProtocolException"));
|
||||||
|
|
||||||
|
// Instancja testowanej klasy z przekazaniem zmockowanego klienta
|
||||||
HttpClient httpClient = new HttpClient(mockHttpClient);
|
HttpClient httpClient = new HttpClient(mockHttpClient);
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
|
Loading…
Reference in New Issue
Block a user