Commit d1bede770d891ee37b999e5471b43c93e13b10fc

Authored by Volodymyr Babak
1 parent e05e681b

Edge test fixes. Replaced do/while loop with awailability framework approach

... ... @@ -290,6 +290,11 @@
290 290 <scope>test</scope>
291 291 </dependency>
292 292 <dependency>
  293 + <groupId>org.awaitility</groupId>
  294 + <artifactId>awaitility</artifactId>
  295 + <scope>test</scope>
  296 + </dependency>
  297 + <dependency>
293 298 <groupId>org.mockito</groupId>
294 299 <artifactId>mockito-core</artifactId>
295 300 <scope>test</scope>
... ...
... ... @@ -27,6 +27,8 @@ import com.google.protobuf.InvalidProtocolBufferException;
27 27 import com.google.protobuf.MessageLite;
28 28 import lombok.extern.slf4j.Slf4j;
29 29 import org.apache.commons.lang3.RandomStringUtils;
  30 +import org.awaitility.Awaitility;
  31 +import org.hamcrest.Matchers;
30 32 import org.junit.After;
31 33 import org.junit.Assert;
32 34 import org.junit.Before;
... ... @@ -650,7 +652,7 @@ abstract public class BaseEdgeTest extends AbstractControllerTest {
650 652 Assert.assertEquals(relationUpdateMsg.getFromIdMSB(), relation.getFrom().getId().getMostSignificantBits());
651 653 Assert.assertEquals(relationUpdateMsg.getToIdLSB(), relation.getTo().getId().getLeastSignificantBits());
652 654 Assert.assertEquals(relationUpdateMsg.getToEntityType(), relation.getTo().getEntityType().name());
653   - Assert.assertEquals(relationUpdateMsg.getTypeGroup(), relation.getTypeGroup().name());
  655 + Assert.assertEquals(relationUpdateMsg.getTypeGroup().getValue(), relation.getTypeGroup().name());
654 656
655 657 // 2
656 658 edgeImitator.expectMessageAmount(1);
... ... @@ -674,7 +676,7 @@ abstract public class BaseEdgeTest extends AbstractControllerTest {
674 676 Assert.assertEquals(relationUpdateMsg.getFromIdMSB(), relation.getFrom().getId().getMostSignificantBits());
675 677 Assert.assertEquals(relationUpdateMsg.getToIdLSB(), relation.getTo().getId().getLeastSignificantBits());
676 678 Assert.assertEquals(relationUpdateMsg.getToEntityType(), relation.getTo().getEntityType().name());
677   - Assert.assertEquals(relationUpdateMsg.getTypeGroup(), relation.getTypeGroup().name());
  679 + Assert.assertEquals(relationUpdateMsg.getTypeGroup().getValue(), relation.getTypeGroup().name());
678 680
679 681 log.info("Relations tested successfully");
680 682 }
... ... @@ -902,8 +904,8 @@ abstract public class BaseEdgeTest extends AbstractControllerTest {
902 904 Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, widgetTypeUpdateMsg.getMsgType());
903 905 Assert.assertEquals(widgetTypeUpdateMsg.getIdMSB(), savedWidgetType.getUuidId().getMostSignificantBits());
904 906 Assert.assertEquals(widgetTypeUpdateMsg.getIdLSB(), savedWidgetType.getUuidId().getLeastSignificantBits());
905   - Assert.assertEquals(widgetTypeUpdateMsg.getAlias(), savedWidgetType.getAlias());
906   - Assert.assertEquals(widgetTypeUpdateMsg.getName(), savedWidgetType.getName());
  907 + Assert.assertEquals(widgetTypeUpdateMsg.getAlias().getValue(), savedWidgetType.getAlias());
  908 + Assert.assertEquals(widgetTypeUpdateMsg.getName().getValue(), savedWidgetType.getName());
907 909 Assert.assertEquals(JacksonUtil.toJsonNode(widgetTypeUpdateMsg.getDescriptorJson().getValue()), savedWidgetType.getDescriptor());
908 910
909 911 // 3
... ... @@ -1204,7 +1206,7 @@ abstract public class BaseEdgeTest extends AbstractControllerTest {
1204 1206 Assert.assertTrue(latestMessage instanceof DeviceUpdateMsg);
1205 1207 DeviceUpdateMsg latestDeviceUpdateMsg = (DeviceUpdateMsg) latestMessage;
1206 1208 Assert.assertNotEquals(deviceOnCloudName, latestDeviceUpdateMsg.getName());
1207   - Assert.assertEquals(deviceOnCloudName, latestDeviceUpdateMsg.getConflictName());
  1209 + Assert.assertEquals(deviceOnCloudName, latestDeviceUpdateMsg.getConflictName().getValue());
1208 1210
1209 1211 UUID newDeviceId = new UUID(latestDeviceUpdateMsg.getIdMSB(), latestDeviceUpdateMsg.getIdLSB());
1210 1212
... ... @@ -1271,7 +1273,7 @@ abstract public class BaseEdgeTest extends AbstractControllerTest {
1271 1273 EntityId toEntityId = EntityIdFactory.getByTypeAndUuid(relationUpdateMsg.getToEntityType(), toUUID);
1272 1274 Assert.assertEquals(relation.getTo(), toEntityId);
1273 1275
1274   - Assert.assertEquals(relation.getTypeGroup().name(), relationUpdateMsg.getTypeGroup());
  1276 + Assert.assertEquals(relation.getTypeGroup().name(), relationUpdateMsg.getTypeGroup().getValue());
1275 1277 }
1276 1278
1277 1279 private void sendAlarm() throws Exception {
... ... @@ -1350,15 +1352,11 @@ abstract public class BaseEdgeTest extends AbstractControllerTest {
1350 1352 edgeImitator.sendUplinkMsg(uplinkMsgBuilder2.build());
1351 1353 Assert.assertTrue(edgeImitator.waitForResponses());
1352 1354
1353   - int attempt = 0;
1354   - Map<String, List<Map<String, String>>> timeseries;
1355   - do {
1356   - timeseries = doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getUuidId() + "/values/timeseries?keys=" + timeseriesKey,
1357   - new TypeReference<>() {});
1358   - // Wait before device attributes saved to database before requesting them from controller
1359   - Thread.sleep(100);
1360   - attempt++;
1361   - } while (!timeseries.containsKey(timeseriesKey) || attempt < 10);
  1355 + Awaitility.await()
  1356 + .atMost(2, TimeUnit.SECONDS)
  1357 + .until(() -> isTimeseriesAlreadyAvailable(device, timeseriesKey));
  1358 +
  1359 + Map<String, List<Map<String, String>>> timeseries = loadDeviceTimeseries(device, timeseriesKey);
1362 1360 Assert.assertTrue(timeseries.containsKey(timeseriesKey));
1363 1361 Assert.assertEquals(1, timeseries.get(timeseriesKey).size());
1364 1362 Assert.assertEquals(timeseriesValue, timeseries.get(timeseriesKey).get(0).get("value"));
... ... @@ -1370,6 +1368,15 @@ abstract public class BaseEdgeTest extends AbstractControllerTest {
1370 1368
1371 1369 }
1372 1370
  1371 + private boolean isTimeseriesAlreadyAvailable(Device device, String timeseriesKey) throws Exception {
  1372 + return loadDeviceTimeseries(device, timeseriesKey).containsKey(timeseriesKey);
  1373 + }
  1374 +
  1375 + private Map<String, List<Map<String, String>>> loadDeviceTimeseries(Device device, String timeseriesKey) throws Exception {
  1376 + return doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getUuidId() + "/values/timeseries?keys=" + timeseriesKey,
  1377 + new TypeReference<>() {});
  1378 + }
  1379 +
1373 1380 private void sendRelation() throws Exception {
1374 1381 List<Device> edgeDevices = doGetTypedWithPageLink("/api/edge/" + edge.getId().getId().toString() + "/devices?",
1375 1382 new TypeReference<PageData<Device>>() {}, new PageLink(100)).getData();
... ... @@ -1449,7 +1456,7 @@ abstract public class BaseEdgeTest extends AbstractControllerTest {
1449 1456 edgeImitator.expectMessageAmount(1);
1450 1457 edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
1451 1458 Assert.assertTrue(edgeImitator.waitForResponses());
1452   - Assert.assertTrue(edgeImitator.waitForMessages());;
  1459 + Assert.assertTrue(edgeImitator.waitForMessages());
1453 1460
1454 1461 AbstractMessage latestMessage = edgeImitator.getLatestMessage();
1455 1462 Assert.assertTrue(latestMessage instanceof RuleChainMetadataUpdateMsg);
... ...
... ... @@ -49,6 +49,7 @@
49 49 <json-path.version>2.2.0</json-path.version>
50 50 <junit.version>4.12</junit.version>
51 51 <jupiter.version>5.7.1</jupiter.version>
  52 + <awaitility.version>4.1.0</awaitility.version>
52 53 <hamcrest.version>2.2</hamcrest.version>
53 54 <slf4j.version>1.7.7</slf4j.version>
54 55 <logback.version>1.2.3</logback.version>
... ... @@ -1438,6 +1439,12 @@
1438 1439 <scope>test</scope>
1439 1440 </dependency>
1440 1441 <dependency>
  1442 + <groupId>org.awaitility</groupId>
  1443 + <artifactId>awaitility</artifactId>
  1444 + <version>${awaitility.version}</version>
  1445 + <scope>test</scope>
  1446 + </dependency>
  1447 + <dependency>
1441 1448 <groupId>org.hamcrest</groupId>
1442 1449 <artifactId>hamcrest</artifactId>
1443 1450 <version>${hamcrest.version}</version>
... ...