From 6dfaf331f8d62781a87a43101610056160386ce6 Mon Sep 17 00:00:00 2001 From: Marcin Hutek Date: Tue, 12 Dec 2023 21:12:29 +0100 Subject: [PATCH] descriptions for each class created --- src/main/java/model/CityNames.java | 6 ++- src/main/java/model/ForecastPreparation.java | 4 ++ src/main/java/model/HttpClient.java | 14 ++--- src/main/java/model/JsonToMap.java | 5 ++ src/main/java/model/TemporaryFile.java | 4 ++ src/main/java/model/weatherForecast.java | 6 +++ src/main/java/org/example/Main.java | 3 ++ src/test/java/HttpClientTest.java | 55 +++++++------------- 8 files changed, 54 insertions(+), 43 deletions(-) diff --git a/src/main/java/model/CityNames.java b/src/main/java/model/CityNames.java index 6b3c18a..d93dfe1 100644 --- a/src/main/java/model/CityNames.java +++ b/src/main/java/model/CityNames.java @@ -8,7 +8,11 @@ import java.io.IOException; import java.util.List; import java.util.stream.Collectors; -// WYKORZYSTANIE STRUMIENIA +/* + Klasa odczytujaca oraz wypisujaca dostepne miasta z pliku .json. + Wykorzystanie strumienia. +*/ + public class CityNames { public static void cityNamesRead(String filePath) { ObjectMapper objectMapper = new ObjectMapper(); diff --git a/src/main/java/model/ForecastPreparation.java b/src/main/java/model/ForecastPreparation.java index 8657175..9c56d76 100644 --- a/src/main/java/model/ForecastPreparation.java +++ b/src/main/java/model/ForecastPreparation.java @@ -5,6 +5,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; +/* + Klasa odczytujaca z odpowiedzi API wyamagane informacje o prognozie pogody. +*/ + public class ForecastPreparation { diff --git a/src/main/java/model/HttpClient.java b/src/main/java/model/HttpClient.java index b03f4c4..9d30341 100644 --- a/src/main/java/model/HttpClient.java +++ b/src/main/java/model/HttpClient.java @@ -10,28 +10,28 @@ import org.apache.http.util.EntityUtils; import java.io.IOException; +/* + Klasa tworzaca klienta Http i wykonujaca zapytanie GET +*/ + public class HttpClient { private CloseableHttpClient httpClient; - // Konstruktor domyślny, używa domyślnego klienta HTTP public HttpClient() { this.httpClient = HttpClients.createDefault(); } - // Konstruktor umożliwiający ustawienie własnego klienta HTTP (do użycia w testach) public HttpClient(CloseableHttpClient httpClient) { this.httpClient = httpClient; } - // Metoda umożliwiająca ustawienie własnego klienta HTTP (do użycia w testach) - public void setHttpClient(CloseableHttpClient httpClient) { - this.httpClient = httpClient; - } +// public void setHttpClient(CloseableHttpClient httpClient) { +// this.httpClient = httpClient; +// } public String executeGetRequest(String urlString) { -// CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet request = new HttpGet(urlString); try (CloseableHttpResponse response = httpClient.execute(request)) { diff --git a/src/main/java/model/JsonToMap.java b/src/main/java/model/JsonToMap.java index 99089d6..4b3d7fc 100644 --- a/src/main/java/model/JsonToMap.java +++ b/src/main/java/model/JsonToMap.java @@ -6,6 +6,11 @@ import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; + +/* + Klasa laczaca dostepne nazwy miast i ich koordynaty w mape. +*/ + public class JsonToMap { public static Map> createCityMap(String jsonFile) { diff --git a/src/main/java/model/TemporaryFile.java b/src/main/java/model/TemporaryFile.java index adfdf40..828539b 100644 --- a/src/main/java/model/TemporaryFile.java +++ b/src/main/java/model/TemporaryFile.java @@ -4,6 +4,10 @@ import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; +/* + Klasa tworzaca pusty plik temporary. +*/ + public class TemporaryFile { public static final String FILE_NAME = "temporary_file.txt"; diff --git a/src/main/java/model/weatherForecast.java b/src/main/java/model/weatherForecast.java index 67ebb97..a0c03eb 100644 --- a/src/main/java/model/weatherForecast.java +++ b/src/main/java/model/weatherForecast.java @@ -14,6 +14,12 @@ import java.io.BufferedReader; import java.io.FileOutputStream; 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 static final List allEntries = new ArrayList<>(); private final String cityName; diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 16d1e3e..446a57b 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -10,6 +10,9 @@ import java.time.format.DateTimeFormatter; import java.util.Objects; import java.util.Scanner; +/* + Klasa Main +*/ public class Main { public static void main(String[] args) throws IOException { diff --git a/src/test/java/HttpClientTest.java b/src/test/java/HttpClientTest.java index dfb7060..7be55bd 100644 --- a/src/test/java/HttpClientTest.java +++ b/src/test/java/HttpClientTest.java @@ -3,15 +3,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.*; 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.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; 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.mockito.ArgumentMatchers; import org.mockito.Mockito; @@ -20,19 +15,20 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; +/* + Zgodnie z wymogami projektu - trzy testy mockujace przygotowane dla klasy HttpClient. +*/ public class HttpClientTest { @Test void testExecuteGetRequest() throws Exception { -// 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"; + // Przykladowy response String expectedResponse = "{\"coord\":{\"lon\":44.34,\"lat\":10.99},\"weather\":" + "[{\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\"" + ",\"icon\":\"04n\"}],\"base\":\"stations\",\"main\":{\"temp\":299.46,\"" + @@ -43,64 +39,53 @@ public class HttpClientTest { "\"sunrise\":1701918532,\"sunset\":1701959952},\"timezone\":10800,\"id\"" + ":54746,\"name\":\"Lughaye\",\"cod\":200}\n"; - // Tworzymy mock dla CloseableHttpClient + // Przygotowanie mockow CloseableHttpClient mockHttpClient = Mockito.mock(CloseableHttpClient.class); - - // Tworzymy mock dla HttpEntity 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); when(mockResponse.getEntity()).thenReturn(mockEntity); - - // Konfigurujemy mockEntity do zwrócenia StringEntity when(mockEntity.getContent()).thenReturn(new ByteArrayInputStream(expectedResponse.getBytes(StandardCharsets.UTF_8))); when(mockEntity.getContentLength()).thenReturn((long) expectedResponse.length()); // Przygotowujemy odpowiedź do zwrócenia przez mock 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); - // Wywołujemy metodę, którą chcemy przetestować + // Act & Assert String result = httpClient.executeGetRequest(urlString); - - // Sprawdzamy, czy wynik metody jest zgodny z oczekiwaniami - Assertions.assertEquals(expectedResponse, result); + assertEquals(expectedResponse, result); } @Test 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"; - // Arrange + + // Przygotowanie mockow CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class); CloseableHttpResponse mockHttpResponse = mock(CloseableHttpResponse.class); 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); - // Act + // Act & Assert String result = httpClient.executeGetRequest(urlString); - // Assert - assertEquals("", result); // Oczekiwany rezultat - pusty string + assertEquals("", result); } @Test void testExecuteGetRequest_ClientProtocolException() throws IOException { - // Arrange + + // Przygotowanie mockow CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class); - CloseableHttpResponse mockHttpResponse = mock(CloseableHttpResponse.class); when(mockHttpClient.execute(ArgumentMatchers.any(HttpGet.class))).thenThrow(new ClientProtocolException("Mocked ClientProtocolException")); + // Instancja testowanej klasy z przekazaniem zmockowanego klienta HttpClient httpClient = new HttpClient(mockHttpClient); // Act & Assert