|
@@ -1,35 +1,35 @@
|
|
|
-/*
|
|
|
|
|
|
|
+
|
|
|
package cz.senslog.connector.fetch.azure;
|
|
package cz.senslog.connector.fetch.azure;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+import cz.senslog.connector.fetch.api.ExecutableFetcher;
|
|
|
|
|
+import cz.senslog.connector.fetch.azure.auth.AuthenticationService;
|
|
|
|
|
+import cz.senslog.connector.model.config.DefaultConfig;
|
|
|
import cz.senslog.connector.model.config.HostConfig;
|
|
import cz.senslog.connector.model.config.HostConfig;
|
|
|
-import cz.senslog.connector.fetch.azure.auth.AzureAuthenticationService;
|
|
|
|
|
import cz.senslog.common.http.HttpClient;
|
|
import cz.senslog.common.http.HttpClient;
|
|
|
import cz.senslog.common.http.HttpCode;
|
|
import cz.senslog.common.http.HttpCode;
|
|
|
import cz.senslog.common.http.HttpRequest;
|
|
import cz.senslog.common.http.HttpRequest;
|
|
|
import cz.senslog.common.http.HttpResponse;
|
|
import cz.senslog.common.http.HttpResponse;
|
|
|
import cz.senslog.connector.model.azure.AzureModel;
|
|
import cz.senslog.connector.model.azure.AzureModel;
|
|
|
-import cz.senslog.connector.model.azure.SensorData;
|
|
|
|
|
import cz.senslog.connector.model.azure.SensorInfo;
|
|
import cz.senslog.connector.model.azure.SensorInfo;
|
|
|
|
|
+import cz.senslog.connector.model.config.PropertyConfig;
|
|
|
|
|
+import cz.senslog.connector.model.fieldclimate.FieldClimateModel;
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.mockito.stubbing.Answer;
|
|
import org.mockito.stubbing.Answer;
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
-import java.time.ZoneOffset;
|
|
|
|
|
-import java.time.ZonedDateTime;
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
|
|
+import static cz.senslog.common.http.HttpClient.newHttpClient;
|
|
|
|
|
+import static cz.senslog.common.http.HttpClient.newHttpSSLClient;
|
|
|
import static cz.senslog.common.http.HttpCode.SERVER_ERROR;
|
|
import static cz.senslog.common.http.HttpCode.SERVER_ERROR;
|
|
|
import static cz.senslog.common.json.BasicJson.objectToJson;
|
|
import static cz.senslog.common.json.BasicJson.objectToJson;
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
-import static org.mockito.Mockito.mock;
|
|
|
|
|
-import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
|
|
|
class AzureFetcherTest {
|
|
class AzureFetcherTest {
|
|
|
|
|
|
|
|
-
|
|
|
|
|
@Test
|
|
@Test
|
|
|
void fetch_GetSensorsAndData_OneSensorOneData() throws Exception {
|
|
void fetch_GetSensorsAndData_OneSensorOneData() throws Exception {
|
|
|
|
|
|
|
@@ -40,27 +40,30 @@ class AzureFetcherTest {
|
|
|
if (path.equals("/sensorInfo")) {
|
|
if (path.equals("/sensorInfo")) {
|
|
|
SensorInfo sensorInfo = new SensorInfo();
|
|
SensorInfo sensorInfo = new SensorInfo();
|
|
|
sensorInfo.setEui("1234");
|
|
sensorInfo.setEui("1234");
|
|
|
|
|
+ sensorInfo.setStatus(1);
|
|
|
List<SensorInfo> bodyList = new ArrayList<>();
|
|
List<SensorInfo> bodyList = new ArrayList<>();
|
|
|
bodyList.add(sensorInfo);
|
|
bodyList.add(sensorInfo);
|
|
|
return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
|
.body(objectToJson(bodyList)).build();
|
|
.body(objectToJson(bodyList)).build();
|
|
|
} else if (path.equals("/sensorData")) {
|
|
} 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");
|
|
|
|
|
|
|
+ Map<String, Object> dataObj = new HashMap<>();
|
|
|
|
|
+ dataObj.put("temperature", 25.5F);
|
|
|
|
|
+ dataObj.put("time", "1970-01-01T00:00:00+00:00");
|
|
|
|
|
+ List<Object> data = new ArrayList<>();
|
|
|
|
|
+ data.add(dataObj);
|
|
|
|
|
+ Map<String, Object> sensorDataJson = new HashMap<>();
|
|
|
|
|
+ sensorDataJson.put("eui", "1234");
|
|
|
|
|
+ sensorDataJson.put("name", "name");
|
|
|
|
|
+ sensorDataJson.put("data", data);
|
|
|
|
|
+
|
|
|
return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
|
- .body(objectToJson(sensorInfo)).build();
|
|
|
|
|
|
|
+ .body(objectToJson(sensorDataJson)).build();
|
|
|
}
|
|
}
|
|
|
return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
|
|
|
|
+ AuthenticationService authService = mock(AuthenticationService.class);
|
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
|
|
|
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
@@ -72,11 +75,9 @@ class AzureFetcherTest {
|
|
|
|
|
|
|
|
fetcher.init();
|
|
fetcher.init();
|
|
|
|
|
|
|
|
- AzureModel model = fetcher.fetch();
|
|
|
|
|
|
|
+ AzureModel model = fetcher.fetch(Optional.empty());
|
|
|
|
|
|
|
|
assertNotNull(model.getSensors());
|
|
assertNotNull(model.getSensors());
|
|
|
- assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
|
|
- assertEquals(LocalDateTime.MAX, model.getTo());
|
|
|
|
|
assertEquals(1, model.getSensors().size());
|
|
assertEquals(1, model.getSensors().size());
|
|
|
|
|
|
|
|
SensorInfo sensorInfo = model.getSensors().get(0);
|
|
SensorInfo sensorInfo = model.getSensors().get(0);
|
|
@@ -100,7 +101,7 @@ class AzureFetcherTest {
|
|
|
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
|
|
|
|
+ AuthenticationService authService = mock(AuthenticationService.class);
|
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
|
|
|
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
@@ -112,11 +113,9 @@ class AzureFetcherTest {
|
|
|
|
|
|
|
|
assertThrows(Exception.class, fetcher::init);
|
|
assertThrows(Exception.class, fetcher::init);
|
|
|
|
|
|
|
|
- AzureModel model = fetcher.fetch();
|
|
|
|
|
|
|
+ AzureModel model = fetcher.fetch(Optional.empty());
|
|
|
|
|
|
|
|
assertNotNull(model.getSensors());
|
|
assertNotNull(model.getSensors());
|
|
|
- assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
|
|
- assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
|
|
assertEquals(0, model.getSensors().size());
|
|
assertEquals(0, model.getSensors().size());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -134,7 +133,7 @@ class AzureFetcherTest {
|
|
|
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
|
|
|
|
+ AuthenticationService authService = mock(AuthenticationService.class);
|
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
|
|
|
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
@@ -146,11 +145,9 @@ class AzureFetcherTest {
|
|
|
|
|
|
|
|
assertThrows(Exception.class, fetcher::init);
|
|
assertThrows(Exception.class, fetcher::init);
|
|
|
|
|
|
|
|
- AzureModel model = fetcher.fetch();
|
|
|
|
|
|
|
+ AzureModel model = fetcher.fetch(Optional.empty());
|
|
|
|
|
|
|
|
assertNotNull(model.getSensors());
|
|
assertNotNull(model.getSensors());
|
|
|
- assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
|
|
- assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
|
|
assertEquals(0, model.getSensors().size());
|
|
assertEquals(0, model.getSensors().size());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -164,6 +161,7 @@ class AzureFetcherTest {
|
|
|
if (path.equals("/sensorInfo")) {
|
|
if (path.equals("/sensorInfo")) {
|
|
|
SensorInfo sensorInfo = new SensorInfo();
|
|
SensorInfo sensorInfo = new SensorInfo();
|
|
|
sensorInfo.setEui("1234");
|
|
sensorInfo.setEui("1234");
|
|
|
|
|
+ sensorInfo.setStatus(1);
|
|
|
List<SensorInfo> bodyList = new ArrayList<>();
|
|
List<SensorInfo> bodyList = new ArrayList<>();
|
|
|
bodyList.add(sensorInfo);
|
|
bodyList.add(sensorInfo);
|
|
|
return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
@@ -175,7 +173,7 @@ class AzureFetcherTest {
|
|
|
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
|
|
|
|
+ AuthenticationService authService = mock(AuthenticationService.class);
|
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
|
|
|
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
@@ -187,11 +185,9 @@ class AzureFetcherTest {
|
|
|
|
|
|
|
|
fetcher.init();
|
|
fetcher.init();
|
|
|
|
|
|
|
|
- AzureModel model = fetcher.fetch();
|
|
|
|
|
|
|
+ AzureModel model = fetcher.fetch(Optional.empty());
|
|
|
|
|
|
|
|
assertNotNull(model.getSensors());
|
|
assertNotNull(model.getSensors());
|
|
|
- assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
|
|
- assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
|
|
assertEquals(1, model.getSensors().size());
|
|
assertEquals(1, model.getSensors().size());
|
|
|
|
|
|
|
|
SensorInfo sensorInfo = model.getSensors().get(0);
|
|
SensorInfo sensorInfo = model.getSensors().get(0);
|
|
@@ -213,7 +209,7 @@ class AzureFetcherTest {
|
|
|
return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
|
|
|
|
+ AuthenticationService authService = mock(AuthenticationService.class);
|
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
|
|
|
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
@@ -225,11 +221,9 @@ class AzureFetcherTest {
|
|
|
|
|
|
|
|
assertThrows(Exception.class, fetcher::init);
|
|
assertThrows(Exception.class, fetcher::init);
|
|
|
|
|
|
|
|
- AzureModel model = fetcher.fetch();
|
|
|
|
|
|
|
+ AzureModel model = fetcher.fetch(Optional.empty());
|
|
|
|
|
|
|
|
assertNotNull(model.getSensors());
|
|
assertNotNull(model.getSensors());
|
|
|
- assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
|
|
- assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
|
|
assertEquals(0, model.getSensors().size());
|
|
assertEquals(0, model.getSensors().size());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -243,6 +237,7 @@ class AzureFetcherTest {
|
|
|
if (path.equals("/sensorInfo")) {
|
|
if (path.equals("/sensorInfo")) {
|
|
|
SensorInfo sensorInfo = new SensorInfo();
|
|
SensorInfo sensorInfo = new SensorInfo();
|
|
|
sensorInfo.setEui("1234");
|
|
sensorInfo.setEui("1234");
|
|
|
|
|
+ sensorInfo.setStatus(1);
|
|
|
List<SensorInfo> bodyList = new ArrayList<>();
|
|
List<SensorInfo> bodyList = new ArrayList<>();
|
|
|
bodyList.add(sensorInfo);
|
|
bodyList.add(sensorInfo);
|
|
|
return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
return HttpResponse.newBuilder().status(HttpCode.OK)
|
|
@@ -254,7 +249,7 @@ class AzureFetcherTest {
|
|
|
return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
return HttpResponse.newBuilder().status(SERVER_ERROR).build();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- AzureAuthenticationService authService = mock(AzureAuthenticationService.class);
|
|
|
|
|
|
|
+ AuthenticationService authService = mock(AuthenticationService.class);
|
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
when(authService.getAccessToken()).thenReturn("#12345");
|
|
|
|
|
|
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
AzureConfig config = mock(AzureConfig.class);
|
|
@@ -266,14 +261,57 @@ class AzureFetcherTest {
|
|
|
|
|
|
|
|
fetcher.init();
|
|
fetcher.init();
|
|
|
|
|
|
|
|
- AzureModel model = fetcher.fetch();
|
|
|
|
|
|
|
+ AzureModel model = fetcher.fetch(Optional.empty());
|
|
|
|
|
|
|
|
assertNotNull(model.getSensors());
|
|
assertNotNull(model.getSensors());
|
|
|
- assertEquals(LocalDateTime.MIN, model.getFrom());
|
|
|
|
|
- assertEquals(LocalDateTime.MIN, model.getTo());
|
|
|
|
|
assertEquals(1, model.getSensors().size());
|
|
assertEquals(1, model.getSensors().size());
|
|
|
|
|
|
|
|
assertEquals(0, model.getSensors().get(0).getData().size());
|
|
assertEquals(0, model.getSensors().get(0).getData().size());
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
- */
|
|
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void fetch() throws Exception {
|
|
|
|
|
+
|
|
|
|
|
+ DefaultConfig defConfig = new DefaultConfig("", null);
|
|
|
|
|
+
|
|
|
|
|
+ defConfig.setProperty("startDate", LocalDateTime.of(2020, 1, 1, 0, 0));
|
|
|
|
|
+ defConfig.setProperty("limitPerSensor", 1);
|
|
|
|
|
+ Map<String, String> infoHost = new HashMap<>();
|
|
|
|
|
+ infoHost.put("domain", "https://iotlorawan.azurewebsites.net"); infoHost.put("path", "api/sensors");
|
|
|
|
|
+ defConfig.setProperty("sensorInfoHost", infoHost);
|
|
|
|
|
+ Map<String, String> dataHost = new HashMap<>();
|
|
|
|
|
+ dataHost.put("domain", "https://iotlorawan.azurewebsites.net"); dataHost.put("path", "api/sensordata");
|
|
|
|
|
+ defConfig.setProperty("sensorDataHost", dataHost);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> authConfig = new HashMap<>();
|
|
|
|
|
+ authConfig.put("username", "netluky@ima.cz");
|
|
|
|
|
+ authConfig.put("password", "SensLogIMA1");
|
|
|
|
|
+ authConfig.put("refreshPeriodIfFail", 1000);
|
|
|
|
|
+ Map<String, String> host = new HashMap<>();
|
|
|
|
|
+ host.put("domain", "https://iotlorawan.azurewebsites.net"); host.put("path", "api/accounts/login");
|
|
|
|
|
+ authConfig.put("host", host);
|
|
|
|
|
+
|
|
|
|
|
+ defConfig.setProperty("authentication", authConfig);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> sessionProxy = new HashMap<>();
|
|
|
|
|
+ sessionProxy.put("lastObservationHost", new HashMap<String, String>() {{
|
|
|
|
|
+ put("domain", "http://51.15.45.95:8080/senslog1");
|
|
|
|
|
+ put("path", "SensorService");
|
|
|
|
|
+ }});
|
|
|
|
|
+ sessionProxy.put("user", "vilcini");
|
|
|
|
|
+ sessionProxy.put("group", "vilcini");
|
|
|
|
|
+ defConfig.setProperty("sessionProxy", sessionProxy);
|
|
|
|
|
+
|
|
|
|
|
+ AzureConfig config = new AzureConfig(defConfig);
|
|
|
|
|
+ AuthenticationService authService = AuthenticationService.newAuthService(config.getAuthentication(), newHttpSSLClient());
|
|
|
|
|
+ AzureFetcher fetcher = new AzureFetcher(config, authService, newHttpClient());
|
|
|
|
|
+ AzureProxySession proxySession = new AzureProxySession(fetcher, config.getSessionProxy(), newHttpClient());
|
|
|
|
|
+
|
|
|
|
|
+ ExecutableFetcher<AzureModel> executableFetcher = ExecutableFetcher.createWithProxySession(proxySession);
|
|
|
|
|
+ executableFetcher.getRawFetcher().init();
|
|
|
|
|
+
|
|
|
|
|
+ executableFetcher.execute();
|
|
|
|
|
+ executableFetcher.execute();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+}
|