input Json file mapping

This commit is contained in:
Marcin Hutek 2023-12-08 17:33:48 +01:00
parent d8ac338daf
commit c09edc1103
4 changed files with 239 additions and 4 deletions

48
pom.xml
View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>PRA2024</artifactId>
<artifactId>testy</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
@ -13,5 +13,49 @@
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}
}

View File

@ -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<String, Map<String, Double>> createCityMap(String jsonFile) {
Map<String, Map<String, Double>> 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<String, Double> cordsMap = new HashMap<>();
cordsMap.put("latitude", latitude);
cordsMap.put("longitude", longitude);
cityMap.put(cityName, cordsMap);
}
} catch (IOException e) {
e.printStackTrace();
}
return cityMap;
}
}

View File

@ -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());
}
}