Search in sources :

Example 6 with AbstractMessage

use of com.google.protobuf.AbstractMessage in project thingsboard by thingsboard.

the class BaseEdgeTest method testAlarms.

@Test
public void testAlarms() throws Exception {
    // 1
    edgeImitator.expectMessageAmount(1);
    Device device = findDeviceByName("Edge Device 1");
    Alarm alarm = new Alarm();
    alarm.setOriginator(device.getId());
    alarm.setStatus(AlarmStatus.ACTIVE_UNACK);
    alarm.setType("alarm");
    alarm.setSeverity(AlarmSeverity.CRITICAL);
    Alarm savedAlarm = doPost("/api/alarm", alarm, Alarm.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof AlarmUpdateMsg);
    AlarmUpdateMsg alarmUpdateMsg = (AlarmUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, alarmUpdateMsg.getMsgType());
    Assert.assertEquals(alarmUpdateMsg.getType(), savedAlarm.getType());
    Assert.assertEquals(alarmUpdateMsg.getName(), savedAlarm.getName());
    Assert.assertEquals(alarmUpdateMsg.getOriginatorName(), device.getName());
    Assert.assertEquals(alarmUpdateMsg.getStatus(), savedAlarm.getStatus().name());
    Assert.assertEquals(alarmUpdateMsg.getSeverity(), savedAlarm.getSeverity().name());
    // 2
    edgeImitator.expectMessageAmount(1);
    doPost("/api/alarm/" + savedAlarm.getUuidId() + "/ack");
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof AlarmUpdateMsg);
    alarmUpdateMsg = (AlarmUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ALARM_ACK_RPC_MESSAGE, alarmUpdateMsg.getMsgType());
    Assert.assertEquals(alarmUpdateMsg.getType(), savedAlarm.getType());
    Assert.assertEquals(alarmUpdateMsg.getName(), savedAlarm.getName());
    Assert.assertEquals(alarmUpdateMsg.getOriginatorName(), device.getName());
    Assert.assertEquals(alarmUpdateMsg.getStatus(), AlarmStatus.ACTIVE_ACK.name());
    // 3
    edgeImitator.expectMessageAmount(1);
    doPost("/api/alarm/" + savedAlarm.getUuidId() + "/clear");
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof AlarmUpdateMsg);
    alarmUpdateMsg = (AlarmUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ALARM_CLEAR_RPC_MESSAGE, alarmUpdateMsg.getMsgType());
    Assert.assertEquals(alarmUpdateMsg.getType(), savedAlarm.getType());
    Assert.assertEquals(alarmUpdateMsg.getName(), savedAlarm.getName());
    Assert.assertEquals(alarmUpdateMsg.getOriginatorName(), device.getName());
    Assert.assertEquals(alarmUpdateMsg.getStatus(), AlarmStatus.CLEARED_ACK.name());
    // 4
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/alarm/" + savedAlarm.getUuidId()).andExpect(status().isOk());
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof AlarmUpdateMsg);
    alarmUpdateMsg = (AlarmUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, alarmUpdateMsg.getMsgType());
    Assert.assertEquals(alarmUpdateMsg.getType(), savedAlarm.getType());
    Assert.assertEquals(alarmUpdateMsg.getName(), savedAlarm.getName());
    Assert.assertEquals(alarmUpdateMsg.getOriginatorName(), device.getName());
    Assert.assertEquals(alarmUpdateMsg.getStatus(), AlarmStatus.CLEARED_ACK.name());
}
Also used : AlarmUpdateMsg(org.thingsboard.server.gen.edge.v1.AlarmUpdateMsg) AbstractMessage(com.google.protobuf.AbstractMessage) Device(org.thingsboard.server.common.data.Device) Alarm(org.thingsboard.server.common.data.alarm.Alarm) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Example 7 with AbstractMessage

use of com.google.protobuf.AbstractMessage in project thingsboard by thingsboard.

the class BaseEdgeTest method testPostAttributesMsg.

private void testPostAttributesMsg(Device device) throws JsonProcessingException, InterruptedException {
    String postAttributesData = "{\"scope\":\"SERVER_SCOPE\",\"kv\":{\"key2\":\"value2\"}}";
    JsonNode postAttributesEntityData = mapper.readTree(postAttributesData);
    EdgeEvent edgeEvent = constructEdgeEvent(tenantId, edge.getId(), EdgeEventActionType.POST_ATTRIBUTES, device.getId().getId(), EdgeEventType.DEVICE, postAttributesEntityData);
    edgeImitator.expectMessageAmount(1);
    edgeEventService.save(edgeEvent);
    clusterService.onEdgeEventUpdate(tenantId, edge.getId());
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof EntityDataProto);
    EntityDataProto latestEntityDataMsg = (EntityDataProto) latestMessage;
    Assert.assertEquals(device.getUuidId().getMostSignificantBits(), latestEntityDataMsg.getEntityIdMSB());
    Assert.assertEquals(device.getUuidId().getLeastSignificantBits(), latestEntityDataMsg.getEntityIdLSB());
    Assert.assertEquals(device.getId().getEntityType().name(), latestEntityDataMsg.getEntityType());
    Assert.assertEquals("SERVER_SCOPE", latestEntityDataMsg.getPostAttributeScope());
    Assert.assertTrue(latestEntityDataMsg.hasPostAttributesMsg());
    TransportProtos.PostAttributeMsg postAttributesMsg = latestEntityDataMsg.getPostAttributesMsg();
    Assert.assertEquals(1, postAttributesMsg.getKvCount());
    TransportProtos.KeyValueProto keyValueProto = postAttributesMsg.getKv(0);
    Assert.assertEquals("key2", keyValueProto.getKey());
    Assert.assertEquals("value2", keyValueProto.getStringV());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) EntityDataProto(org.thingsboard.server.gen.edge.v1.EntityDataProto) JsonNode(com.fasterxml.jackson.databind.JsonNode) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent)

Example 8 with AbstractMessage

use of com.google.protobuf.AbstractMessage in project thingsboard by thingsboard.

the class BaseEdgeTest method testSendRuleChainMetadataRequestToCloud.

@Test
public void testSendRuleChainMetadataRequestToCloud() throws Exception {
    RuleChainId edgeRootRuleChainId = edge.getRootRuleChainId();
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    RuleChainMetadataRequestMsg.Builder ruleChainMetadataRequestMsgBuilder = RuleChainMetadataRequestMsg.newBuilder();
    ruleChainMetadataRequestMsgBuilder.setRuleChainIdMSB(edgeRootRuleChainId.getId().getMostSignificantBits());
    ruleChainMetadataRequestMsgBuilder.setRuleChainIdLSB(edgeRootRuleChainId.getId().getLeastSignificantBits());
    testAutoGeneratedCodeByProtobuf(ruleChainMetadataRequestMsgBuilder);
    uplinkMsgBuilder.addRuleChainMetadataRequestMsg(ruleChainMetadataRequestMsgBuilder.build());
    testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder);
    edgeImitator.expectResponsesAmount(1);
    edgeImitator.expectMessageAmount(1);
    edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
    Assert.assertTrue(edgeImitator.waitForResponses());
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof RuleChainMetadataUpdateMsg);
    RuleChainMetadataUpdateMsg ruleChainMetadataUpdateMsg = (RuleChainMetadataUpdateMsg) latestMessage;
    Assert.assertEquals(ruleChainMetadataUpdateMsg.getRuleChainIdMSB(), edgeRootRuleChainId.getId().getMostSignificantBits());
    Assert.assertEquals(ruleChainMetadataUpdateMsg.getRuleChainIdLSB(), edgeRootRuleChainId.getId().getLeastSignificantBits());
    testAutoGeneratedCodeByProtobuf(ruleChainMetadataUpdateMsg);
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) RuleChainMetadataUpdateMsg(org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg) UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) RuleChainMetadataRequestMsg(org.thingsboard.server.gen.edge.v1.RuleChainMetadataRequestMsg) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Example 9 with AbstractMessage

use of com.google.protobuf.AbstractMessage in project thingsboard by thingsboard.

the class BaseEdgeTest method testDeviceProfiles.

@Test
public void testDeviceProfiles() throws Exception {
    // 1
    DeviceProfile deviceProfile = this.createDeviceProfile("ONE_MORE_DEVICE_PROFILE", null);
    extendDeviceProfileData(deviceProfile);
    edgeImitator.expectMessageAmount(1);
    deviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof DeviceProfileUpdateMsg);
    DeviceProfileUpdateMsg deviceProfileUpdateMsg = (DeviceProfileUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, deviceProfileUpdateMsg.getMsgType());
    Assert.assertEquals(deviceProfileUpdateMsg.getIdMSB(), deviceProfile.getUuidId().getMostSignificantBits());
    Assert.assertEquals(deviceProfileUpdateMsg.getIdLSB(), deviceProfile.getUuidId().getLeastSignificantBits());
    // 2
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/deviceProfile/" + deviceProfile.getUuidId()).andExpect(status().isOk());
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof DeviceProfileUpdateMsg);
    deviceProfileUpdateMsg = (DeviceProfileUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, deviceProfileUpdateMsg.getMsgType());
    Assert.assertEquals(deviceProfileUpdateMsg.getIdMSB(), deviceProfile.getUuidId().getMostSignificantBits());
    Assert.assertEquals(deviceProfileUpdateMsg.getIdLSB(), deviceProfile.getUuidId().getLeastSignificantBits());
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceProfileUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceProfileUpdateMsg) AbstractMessage(com.google.protobuf.AbstractMessage) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Example 10 with AbstractMessage

use of com.google.protobuf.AbstractMessage in project thingsboard by thingsboard.

the class BaseEdgeTest method sendAttributesRequestAndVerify.

private void sendAttributesRequestAndVerify(Device device, String scope, String attributesDataStr, String expectedKey, String expectedValue) throws Exception {
    JsonNode attributesData = mapper.readTree(attributesDataStr);
    doPost("/api/plugins/telemetry/DEVICE/" + device.getUuidId() + "/attributes/" + scope, attributesData);
    // Wait before device attributes saved to database before requesting them from edge
    Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> {
        String urlTemplate = "/api/plugins/telemetry/DEVICE/" + device.getId() + "/keys/attributes/" + scope;
        List<String> actualKeys = doGetAsyncTyped(urlTemplate, new TypeReference<>() {
        });
        return actualKeys != null && !actualKeys.isEmpty() && actualKeys.contains(expectedKey);
    });
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    AttributesRequestMsg.Builder attributesRequestMsgBuilder = AttributesRequestMsg.newBuilder();
    attributesRequestMsgBuilder.setEntityIdMSB(device.getUuidId().getMostSignificantBits());
    attributesRequestMsgBuilder.setEntityIdLSB(device.getUuidId().getLeastSignificantBits());
    attributesRequestMsgBuilder.setEntityType(EntityType.DEVICE.name());
    attributesRequestMsgBuilder.setScope(scope);
    testAutoGeneratedCodeByProtobuf(attributesRequestMsgBuilder);
    uplinkMsgBuilder.addAttributesRequestMsg(attributesRequestMsgBuilder.build());
    testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder);
    edgeImitator.expectResponsesAmount(1);
    edgeImitator.expectMessageAmount(1);
    edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
    Assert.assertTrue(edgeImitator.waitForResponses());
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof EntityDataProto);
    EntityDataProto latestEntityDataMsg = (EntityDataProto) latestMessage;
    Assert.assertEquals(device.getUuidId().getMostSignificantBits(), latestEntityDataMsg.getEntityIdMSB());
    Assert.assertEquals(device.getUuidId().getLeastSignificantBits(), latestEntityDataMsg.getEntityIdLSB());
    Assert.assertEquals(device.getId().getEntityType().name(), latestEntityDataMsg.getEntityType());
    Assert.assertEquals(scope, latestEntityDataMsg.getPostAttributeScope());
    Assert.assertTrue(latestEntityDataMsg.hasAttributesUpdatedMsg());
    TransportProtos.PostAttributeMsg attributesUpdatedMsg = latestEntityDataMsg.getAttributesUpdatedMsg();
    boolean found = false;
    for (TransportProtos.KeyValueProto keyValueProto : attributesUpdatedMsg.getKvList()) {
        if (keyValueProto.getKey().equals(expectedKey)) {
            Assert.assertEquals(expectedKey, keyValueProto.getKey());
            Assert.assertEquals(expectedValue, keyValueProto.getStringV());
            found = true;
        }
    }
    Assert.assertTrue("Expected key and value must be found", found);
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) EntityDataProto(org.thingsboard.server.gen.edge.v1.EntityDataProto) UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) JsonNode(com.fasterxml.jackson.databind.JsonNode) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) AttributesRequestMsg(org.thingsboard.server.gen.edge.v1.AttributesRequestMsg)

Aggregations

AbstractMessage (com.google.protobuf.AbstractMessage)27 Test (org.junit.Test)17 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)17 Device (org.thingsboard.server.common.data.Device)11 UplinkMsg (org.thingsboard.server.gen.edge.v1.UplinkMsg)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 EdgeEvent (org.thingsboard.server.common.data.edge.EdgeEvent)5 EntityDataProto (org.thingsboard.server.gen.edge.v1.EntityDataProto)5 UUID (java.util.UUID)4 DeviceUpdateMsg (org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg)4 Asset (org.thingsboard.server.common.data.asset.Asset)3 DeviceCredentialsRequestMsg (org.thingsboard.server.gen.edge.v1.DeviceCredentialsRequestMsg)3 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)2 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)2 EntityViewUpdateMsg (org.thingsboard.server.gen.edge.v1.EntityViewUpdateMsg)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)1