Search in sources :

Example 26 with AbstractMessage

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

the class BaseEdgeTest method testDevices.

@Test
public void testDevices() throws Exception {
    // 1
    Device savedDevice = saveDeviceOnCloudAndVerifyDeliveryToEdge();
    // 2
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/edge/" + edge.getUuidId() + "/device/" + savedDevice.getUuidId(), Device.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof DeviceUpdateMsg);
    DeviceUpdateMsg deviceUpdateMsg = (DeviceUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, deviceUpdateMsg.getMsgType());
    Assert.assertEquals(deviceUpdateMsg.getIdMSB(), savedDevice.getUuidId().getMostSignificantBits());
    Assert.assertEquals(deviceUpdateMsg.getIdLSB(), savedDevice.getUuidId().getLeastSignificantBits());
    // 3
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/device/" + savedDevice.getUuidId()).andExpect(status().isOk());
    // we should not get any message because device is not assigned to edge any more
    Assert.assertFalse(edgeImitator.waitForMessages(1));
    // 4
    edgeImitator.expectMessageAmount(1);
    savedDevice = saveDevice("Edge Device 3", "Default");
    doPost("/api/edge/" + edge.getUuidId() + "/device/" + savedDevice.getUuidId(), Device.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof DeviceUpdateMsg);
    deviceUpdateMsg = (DeviceUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, deviceUpdateMsg.getMsgType());
    Assert.assertEquals(deviceUpdateMsg.getIdMSB(), savedDevice.getUuidId().getMostSignificantBits());
    Assert.assertEquals(deviceUpdateMsg.getIdLSB(), savedDevice.getUuidId().getLeastSignificantBits());
    Assert.assertEquals(deviceUpdateMsg.getName(), savedDevice.getName());
    Assert.assertEquals(deviceUpdateMsg.getType(), savedDevice.getType());
    // 5
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/device/" + savedDevice.getUuidId()).andExpect(status().isOk());
    // in this case we should get messages because device was assigned to edge
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof DeviceUpdateMsg);
    deviceUpdateMsg = (DeviceUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, deviceUpdateMsg.getMsgType());
    Assert.assertEquals(deviceUpdateMsg.getIdMSB(), savedDevice.getUuidId().getMostSignificantBits());
    Assert.assertEquals(deviceUpdateMsg.getIdLSB(), savedDevice.getUuidId().getLeastSignificantBits());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Device(org.thingsboard.server.common.data.Device) DeviceUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Example 27 with AbstractMessage

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

the class BaseEdgeTest method testSendRelationRequestToCloud.

@Test
public void testSendRelationRequestToCloud() throws Exception {
    Device device = findDeviceByName("Edge Device 1");
    Asset asset = findAssetByName("Edge Asset 1");
    EntityRelation relation = new EntityRelation();
    relation.setType("test");
    relation.setFrom(device.getId());
    relation.setTo(asset.getId());
    relation.setTypeGroup(RelationTypeGroup.COMMON);
    edgeImitator.expectMessageAmount(1);
    doPost("/api/relation", relation);
    Assert.assertTrue(edgeImitator.waitForMessages());
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    RelationRequestMsg.Builder relationRequestMsgBuilder = RelationRequestMsg.newBuilder();
    relationRequestMsgBuilder.setEntityIdMSB(device.getId().getId().getMostSignificantBits());
    relationRequestMsgBuilder.setEntityIdLSB(device.getId().getId().getLeastSignificantBits());
    relationRequestMsgBuilder.setEntityType(device.getId().getEntityType().name());
    testAutoGeneratedCodeByProtobuf(relationRequestMsgBuilder);
    uplinkMsgBuilder.addRelationRequestMsg(relationRequestMsgBuilder.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 RelationUpdateMsg);
    RelationUpdateMsg relationUpdateMsg = (RelationUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, relationUpdateMsg.getMsgType());
    Assert.assertEquals(relation.getType(), relationUpdateMsg.getType());
    UUID fromUUID = new UUID(relationUpdateMsg.getFromIdMSB(), relationUpdateMsg.getFromIdLSB());
    EntityId fromEntityId = EntityIdFactory.getByTypeAndUuid(relationUpdateMsg.getFromEntityType(), fromUUID);
    Assert.assertEquals(relation.getFrom(), fromEntityId);
    UUID toUUID = new UUID(relationUpdateMsg.getToIdMSB(), relationUpdateMsg.getToIdLSB());
    EntityId toEntityId = EntityIdFactory.getByTypeAndUuid(relationUpdateMsg.getToEntityType(), toUUID);
    Assert.assertEquals(relation.getTo(), toEntityId);
    Assert.assertEquals(relation.getTypeGroup().name(), relationUpdateMsg.getTypeGroup());
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RelationRequestMsg(org.thingsboard.server.gen.edge.v1.RelationRequestMsg) AbstractMessage(com.google.protobuf.AbstractMessage) Device(org.thingsboard.server.common.data.Device) UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) Asset(org.thingsboard.server.common.data.asset.Asset) RelationUpdateMsg(org.thingsboard.server.gen.edge.v1.RelationUpdateMsg) UUID(java.util.UUID) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

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