From c09edc1103dd0e0eb6bb70de2cf1c5a9403e5868 Mon Sep 17 00:00:00 2001 From: Marcin Hutek Date: Fri, 8 Dec 2023 17:33:48 +0100 Subject: [PATCH] input Json file mapping --- pom.xml | 48 +++++++++++- src/main/java/model/HttpClient.java | 49 +++++++++++- src/main/java/model/JsonToMap.java | 34 +++++++++ src/test/java/HttpClientTest.java | 112 +++++++++++++++++++++++++++- 4 files changed, 239 insertions(+), 4 deletions(-) create mode 100644 src/main/java/model/JsonToMap.java diff --git a/pom.xml b/pom.xml index 7a9e652..afd25a1 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.example - PRA2024 + testy 1.0-SNAPSHOT @@ -13,5 +13,49 @@ 18 UTF-8 - + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + 2.15.1 + + + org.apache.httpcomponents + httpclient + 4.5.13 + + + com.fasterxml.jackson.core + jackson-databind + 2.9.8 + + + com.itextpdf + itextpdf + 5.5.13 + + + org.mockito + mockito-core + 4.8.0 + test + + + junit + junit + 4.12 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + commons-io + commons-io + 2.11.0 + + \ No newline at end of file diff --git a/src/main/java/model/HttpClient.java b/src/main/java/model/HttpClient.java index 3a1c446..b03f4c4 100644 --- a/src/main/java/model/HttpClient.java +++ b/src/main/java/model/HttpClient.java @@ -1,2 +1,49 @@ -package model;public class HttpClient { +package model; + +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 java.io.IOException; + +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 String executeGetRequest(String urlString) { + +// CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpGet request = new HttpGet(urlString); + + try (CloseableHttpResponse response = httpClient.execute(request)) { + HttpEntity entity = response.getEntity(); + if (entity != null) { + return EntityUtils.toString(entity); + } + return ""; + } catch (ClientProtocolException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } } diff --git a/src/main/java/model/JsonToMap.java b/src/main/java/model/JsonToMap.java new file mode 100644 index 0000000..99089d6 --- /dev/null +++ b/src/main/java/model/JsonToMap.java @@ -0,0 +1,34 @@ +package model; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +public class JsonToMap { + public static Map> createCityMap(String jsonFile) { + + Map> cityMap = new HashMap<>(); + + try { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonArray = objectMapper.readTree(new File(jsonFile)); + + for (JsonNode jsonNode : jsonArray) { + String cityName = jsonNode.get("name").asText(); + double latitude = jsonNode.get("latitude").asDouble(); + double longitude = jsonNode.get("longitude").asDouble(); + + Map cordsMap = new HashMap<>(); + cordsMap.put("latitude", latitude); + cordsMap.put("longitude", longitude); + + cityMap.put(cityName, cordsMap); + } + } catch (IOException e) { + e.printStackTrace(); + } + return cityMap; + } +} diff --git a/src/test/java/HttpClientTest.java b/src/test/java/HttpClientTest.java index 2c470d5..dfb7060 100644 --- a/src/test/java/HttpClientTest.java +++ b/src/test/java/HttpClientTest.java @@ -1,2 +1,112 @@ -package PACKAGE_NAME;public class HttpClientTest { +import static org.junit.Assert.assertEquals; +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; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + + +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; + String urlString = "www.example.com"; + + 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,\"" + + "feels_like\":299.46,\"temp_min\":299.46,\"temp_max\":299.46,\"pressure\"" + + ":1014,\"humidity\":66,\"sea_level\":1014,\"grnd_level\":1014},\"" + + "visibility\":10000,\"wind\":{\"speed\":4.89,\"deg\":78,\"gust\":5.26}," + + "\"clouds\":{\"all\":88},\"dt\":1701977972,\"sys\":{\"country\":\"SO\"," + + "\"sunrise\":1701918532,\"sunset\":1701959952},\"timezone\":10800,\"id\"" + + ":54746,\"name\":\"Lughaye\",\"cod\":200}\n"; + + // Tworzymy mock dla CloseableHttpClient + 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 + HttpClient httpClient = new HttpClient(mockHttpClient); + + // Wywołujemy metodę, którą chcemy przetestować + String result = httpClient.executeGetRequest(urlString); + + // Sprawdzamy, czy wynik metody jest zgodny z oczekiwaniami + Assertions.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; + + String urlString = "www.example.com"; + // Arrange + 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 + + HttpClient httpClient = new HttpClient(mockHttpClient); + + // Act + String result = httpClient.executeGetRequest(urlString); + + // Assert + assertEquals("", result); // Oczekiwany rezultat - pusty string + } + @Test + void testExecuteGetRequest_ClientProtocolException() throws IOException { + // Arrange + CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class); + CloseableHttpResponse mockHttpResponse = mock(CloseableHttpResponse.class); + when(mockHttpClient.execute(ArgumentMatchers.any(HttpGet.class))).thenThrow(new ClientProtocolException("Mocked ClientProtocolException")); + + HttpClient httpClient = new HttpClient(mockHttpClient); + + // Act & Assert + RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> + httpClient.executeGetRequest("http://example.com") + ); + assertEquals("org.apache.http.client.ClientProtocolException: Mocked ClientProtocolException", runtimeException.getMessage()); + } }