|
|
@@ -0,0 +1,279 @@
|
|
|
+/*
|
|
|
+package cz.senslog.connector.fetch.azure;
|
|
|
+
|
|
|
+
|
|
|
+import cz.senslog.connector.config.model.HostConfig;
|
|
|
+import cz.senslog.connector.fetch.azure.auth.AzureAuthenticationService;
|
|
|
+import cz.senslog.connector.http.HttpClient;
|
|
|
+import cz.senslog.connector.http.HttpCode;
|
|
|
+import cz.senslog.connector.http.HttpRequest;
|
|
|
+import cz.senslog.connector.http.HttpResponse;
|
|
|
+import cz.senslog.connector.model.azure.AzureModel;
|
|
|
+import cz.senslog.connector.model.azure.SensorData;
|
|
|
+import cz.senslog.connector.model.azure.SensorInfo;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.stubbing.Answer;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.time.ZonedDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static cz.senslog.connector.http.HttpCode.SERVER_ERROR;
|
|
|
+import static cz.senslog.connector.json.BasicJson.objectToJson;
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+class AzureFetcherTest {
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void fetch_GetSensorsAndData_OneSensorOneData() throws Exception {
|
|
|
+
|
|
|
+ HttpClient httpClient = mock(HttpClient.class);
|
|
|
+ when(httpClient.send(any(HttpRequest.class))).thenAnswer((Answer<HttpResponse>) invocationOnMock -> {
|
|
|
+ HttpRequest request = invocationOnMock.getArgument(0, HttpRequest.class);
|
|
|
+ String path = request.getUrl().getPath();
|
|
|
+ if (path.equals("/sensorInfo")) {
|
|
|
+ SensorInfo sensorInfo = new SensorInfo();
|
|
|
+ sensorInfo.setEui("1234");
|
|
|
+ List<SensorInfo> bodyList = new ArrayList<>();
|
|
|
+ bodyList.add(sensorInfo);
|
|
|
+ return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
|
+ .body(objectToJson(bodyList)).build();
|
|
|
+ } else if (path.equals("/sensorData")) {
|
|
|
+ List<SensorData> sensorDataList = new ArrayList<>();
|
|
|
+ SensorData data = new SensorData();
|
|
|
+ data.setTime(ZonedDateTime.of(LocalDateTime.MAX, ZoneOffset.UTC));
|
|
|
+ data.setTemperature(25.5F);
|
|
|
+ sensorDataList.add(data);
|
|
|
+ SensorInfo sensorInfo = new SensorInfo();
|
|
|
+ sensorInfo.setData(sensorDataList);
|
|
|
+ sensorInfo.setEui("1234");
|
|
|
+ return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
|
+ .body(objectToJson(sensorInfo)).build();
|
|
|
+ }
|
|
|
+ return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
+ when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
+
|
|
|
+ AzureConfig config = mock(AzureConfig.class);
|
|
|
+ when(config.getStartDate()).thenReturn(LocalDateTime.MIN);
|
|
|
+ when(config.getSensorInfoHost()).thenReturn(new HostConfig("http://test.com", "sensorInfo"));
|
|
|
+ when(config.getSensorDataHost()).thenReturn(new HostConfig("http://test.com", "sensorData"));
|
|
|
+
|
|
|
+ AzureFetcher fetcher = new AzureFetcher(config, authService, httpClient);
|
|
|
+
|
|
|
+ fetcher.init();
|
|
|
+
|
|
|
+ AzureModel model = fetcher.fetch();
|
|
|
+
|
|
|
+ assertNotNull(model.getSensors());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
+ assertEquals(LocalDateTime.MAX, model.getTo());
|
|
|
+ assertEquals(1, model.getSensors().size());
|
|
|
+
|
|
|
+ SensorInfo sensorInfo = model.getSensors().get(0);
|
|
|
+ assertEquals("1234", sensorInfo.getEui());
|
|
|
+ assertEquals(1, sensorInfo.getData().size());
|
|
|
+ assertEquals(25.5F, sensorInfo.getData().get(0).getTemperature());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void fetch_EmptySensors_EmptySensors() {
|
|
|
+
|
|
|
+ HttpClient httpClient = mock(HttpClient.class);
|
|
|
+ when(httpClient.send(any(HttpRequest.class))).thenAnswer((Answer<HttpResponse>) invocationOnMock -> {
|
|
|
+ HttpRequest request = invocationOnMock.getArgument(0, HttpRequest.class);
|
|
|
+ String path = request.getUrl().getPath();
|
|
|
+ if (path.equals("/sensorInfo")) {
|
|
|
+ return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
|
+ .body(objectToJson(new ArrayList<>())).build();
|
|
|
+ }
|
|
|
+ return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
+ when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
+
|
|
|
+ AzureConfig config = mock(AzureConfig.class);
|
|
|
+ when(config.getStartDate()).thenReturn(LocalDateTime.MIN);
|
|
|
+ when(config.getSensorInfoHost()).thenReturn(new HostConfig("http://test.com", "sensorInfo"));
|
|
|
+ when(config.getSensorDataHost()).thenReturn(new HostConfig("http://test.com", "sensorData"));
|
|
|
+
|
|
|
+ AzureFetcher fetcher = new AzureFetcher(config, authService, httpClient);
|
|
|
+
|
|
|
+ assertThrows(Exception.class, fetcher::init);
|
|
|
+
|
|
|
+ AzureModel model = fetcher.fetch();
|
|
|
+
|
|
|
+ assertNotNull(model.getSensors());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
+ assertEquals(0, model.getSensors().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void fetch_ErrorSensorsInfoRequest_EmptySensors() {
|
|
|
+
|
|
|
+ HttpClient httpClient = mock(HttpClient.class);
|
|
|
+ when(httpClient.send(any(HttpRequest.class))).thenAnswer((Answer<HttpResponse>) invocationOnMock -> {
|
|
|
+ HttpRequest request = invocationOnMock.getArgument(0, HttpRequest.class);
|
|
|
+ String path = request.getUrl().getPath();
|
|
|
+ if (path.equals("/sensorInfo")) {
|
|
|
+ HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
+ }
|
|
|
+ return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
+ when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
+
|
|
|
+ AzureConfig config = mock(AzureConfig.class);
|
|
|
+ when(config.getStartDate()).thenReturn(LocalDateTime.MIN);
|
|
|
+ when(config.getSensorInfoHost()).thenReturn(new HostConfig("http://test.com", "sensorInfo"));
|
|
|
+ when(config.getSensorDataHost()).thenReturn(new HostConfig("http://test.com", "sensorData"));
|
|
|
+
|
|
|
+ AzureFetcher fetcher = new AzureFetcher(config, authService, httpClient);
|
|
|
+
|
|
|
+ assertThrows(Exception.class, fetcher::init);
|
|
|
+
|
|
|
+ AzureModel model = fetcher.fetch();
|
|
|
+
|
|
|
+ assertNotNull(model.getSensors());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
+ assertEquals(0, model.getSensors().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void fetch_ErrorSensorDataRequest_OneSensorEmptyData() throws Exception {
|
|
|
+
|
|
|
+ HttpClient httpClient = mock(HttpClient.class);
|
|
|
+ when(httpClient.send(any(HttpRequest.class))).thenAnswer((Answer<HttpResponse>) invocationOnMock -> {
|
|
|
+ HttpRequest request = invocationOnMock.getArgument(0, HttpRequest.class);
|
|
|
+ String path = request.getUrl().getPath();
|
|
|
+ if (path.equals("/sensorInfo")) {
|
|
|
+ SensorInfo sensorInfo = new SensorInfo();
|
|
|
+ sensorInfo.setEui("1234");
|
|
|
+ List<SensorInfo> bodyList = new ArrayList<>();
|
|
|
+ bodyList.add(sensorInfo);
|
|
|
+ return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
|
+ .body(objectToJson(bodyList)).build();
|
|
|
+ } else if (path.equals("/sensorData")) {
|
|
|
+ return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
+ }
|
|
|
+ return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
+ when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
+
|
|
|
+ AzureConfig config = mock(AzureConfig.class);
|
|
|
+ when(config.getStartDate()).thenReturn(LocalDateTime.MIN);
|
|
|
+ when(config.getSensorInfoHost()).thenReturn(new HostConfig("http://test.com", "sensorInfo"));
|
|
|
+ when(config.getSensorDataHost()).thenReturn(new HostConfig("http://test.com", "sensorData"));
|
|
|
+
|
|
|
+ AzureFetcher fetcher = new AzureFetcher(config, authService, httpClient);
|
|
|
+
|
|
|
+ fetcher.init();
|
|
|
+
|
|
|
+ AzureModel model = fetcher.fetch();
|
|
|
+
|
|
|
+ assertNotNull(model.getSensors());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
+ assertEquals(1, model.getSensors().size());
|
|
|
+
|
|
|
+ SensorInfo sensorInfo = model.getSensors().get(0);
|
|
|
+ assertEquals("1234", sensorInfo.getEui());
|
|
|
+ assertEquals(0, sensorInfo.getData().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void fetch_InvalidJsonSensor_EmptySensors() {
|
|
|
+
|
|
|
+ HttpClient httpClient = mock(HttpClient.class);
|
|
|
+ when(httpClient.send(any(HttpRequest.class))).thenAnswer((Answer<HttpResponse>) invocationOnMock -> {
|
|
|
+ HttpRequest request = invocationOnMock.getArgument(0, HttpRequest.class);
|
|
|
+ String path = request.getUrl().getPath();
|
|
|
+ if (path.equals("/sensorInfo")) {
|
|
|
+ return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
|
+ .body(objectToJson("invalid_json")).build();
|
|
|
+ }
|
|
|
+ return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
+ });
|
|
|
+
|
|
|
+ AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
+ when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
+
|
|
|
+ AzureConfig config = mock(AzureConfig.class);
|
|
|
+ when(config.getStartDate()).thenReturn(LocalDateTime.MIN);
|
|
|
+ when(config.getSensorInfoHost()).thenReturn(new HostConfig("http://test.com", "sensorInfo"));
|
|
|
+ when(config.getSensorDataHost()).thenReturn(new HostConfig("http://test.com", "sensorData"));
|
|
|
+
|
|
|
+ AzureFetcher fetcher = new AzureFetcher(config, authService, httpClient);
|
|
|
+
|
|
|
+ assertThrows(Exception.class, fetcher::init);
|
|
|
+
|
|
|
+ AzureModel model = fetcher.fetch();
|
|
|
+
|
|
|
+ assertNotNull(model.getSensors());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
+ assertEquals(0, model.getSensors().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void fetch_InvalidJsonData_OneSensorEmptyData() throws Exception {
|
|
|
+
|
|
|
+ HttpClient httpClient = mock(HttpClient.class);
|
|
|
+ when(httpClient.send(any(HttpRequest.class))).thenAnswer((Answer<HttpResponse>) invocationOnMock -> {
|
|
|
+ HttpRequest request = invocationOnMock.getArgument(0, HttpRequest.class);
|
|
|
+ String path = request.getUrl().getPath();
|
|
|
+ if (path.equals("/sensorInfo")) {
|
|
|
+ SensorInfo sensorInfo = new SensorInfo();
|
|
|
+ sensorInfo.setEui("1234");
|
|
|
+ List<SensorInfo> bodyList = new ArrayList<>();
|
|
|
+ bodyList.add(sensorInfo);
|
|
|
+ return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
|
+ .body(objectToJson(bodyList)).build();
|
|
|
+ } else if (path.equals("/sensorData")) {
|
|
|
+ return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
|
+ .body(objectToJson("invalid_json")).build();
|
|
|
+ }
|
|
|
+ return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
+ });
|
|
|
+
|
|
|
+ AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
+ when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
+
|
|
|
+ AzureConfig config = mock(AzureConfig.class);
|
|
|
+ when(config.getStartDate()).thenReturn(LocalDateTime.MIN);
|
|
|
+ when(config.getSensorInfoHost()).thenReturn(new HostConfig("http://test.com", "sensorInfo"));
|
|
|
+ when(config.getSensorDataHost()).thenReturn(new HostConfig("http://test.com", "sensorData"));
|
|
|
+
|
|
|
+ AzureFetcher fetcher = new AzureFetcher(config, authService, httpClient);
|
|
|
+
|
|
|
+ fetcher.init();
|
|
|
+
|
|
|
+ AzureModel model = fetcher.fetch();
|
|
|
+
|
|
|
+ assertNotNull(model.getSensors());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
+ assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
+ assertEquals(1, model.getSensors().size());
|
|
|
+
|
|
|
+ assertEquals(0, model.getSensors().get(0).getData().size());
|
|
|
+ }
|
|
|
+}
|
|
|
+ */
|