Search in sources :

Example 21 with AbstractMessage

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

the class BaseEdgeTest method testCustomerAndNewUser.

@Test
public void testCustomerAndNewUser() throws Exception {
    // 1
    edgeImitator.expectMessageAmount(1);
    Customer customer = new Customer();
    customer.setTitle("Edge Customer 1");
    Customer savedCustomer = doPost("/api/customer", customer, Customer.class);
    doPost("/api/customer/" + savedCustomer.getUuidId() + "/edge/" + edge.getUuidId(), Edge.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof CustomerUpdateMsg);
    CustomerUpdateMsg customerUpdateMsg = (CustomerUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, customerUpdateMsg.getMsgType());
    Assert.assertEquals(customerUpdateMsg.getIdMSB(), savedCustomer.getUuidId().getMostSignificantBits());
    Assert.assertEquals(customerUpdateMsg.getIdLSB(), savedCustomer.getUuidId().getLeastSignificantBits());
    Assert.assertEquals(customerUpdateMsg.getTitle(), savedCustomer.getTitle());
    testAutoGeneratedCodeByProtobuf(customerUpdateMsg);
    // 2
    edgeImitator.expectMessageAmount(1);
    User customerUser = new User();
    customerUser.setAuthority(Authority.CUSTOMER_USER);
    customerUser.setTenantId(savedTenant.getId());
    customerUser.setCustomerId(savedCustomer.getId());
    customerUser.setEmail("customerUser@thingsboard.org");
    customerUser.setFirstName("John");
    customerUser.setLastName("Edwards");
    User savedUser = doPost("/api/user", customerUser, User.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof UserUpdateMsg);
    UserUpdateMsg userUpdateMsg = (UserUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, userUpdateMsg.getMsgType());
    Assert.assertEquals(userUpdateMsg.getIdMSB(), savedUser.getUuidId().getMostSignificantBits());
    Assert.assertEquals(userUpdateMsg.getIdLSB(), savedUser.getUuidId().getLeastSignificantBits());
    Assert.assertEquals(userUpdateMsg.getEmail(), savedUser.getEmail());
    testAutoGeneratedCodeByProtobuf(userUpdateMsg);
    // 3
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/customer/edge/" + edge.getUuidId(), Edge.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof CustomerUpdateMsg);
    customerUpdateMsg = (CustomerUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, customerUpdateMsg.getMsgType());
    Assert.assertEquals(customerUpdateMsg.getIdMSB(), savedCustomer.getUuidId().getMostSignificantBits());
    Assert.assertEquals(customerUpdateMsg.getIdLSB(), savedCustomer.getUuidId().getLeastSignificantBits());
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/customer/" + savedCustomer.getUuidId()).andExpect(status().isOk());
    Assert.assertFalse(edgeImitator.waitForMessages(1));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) User(org.thingsboard.server.common.data.User) Customer(org.thingsboard.server.common.data.Customer) UserUpdateMsg(org.thingsboard.server.gen.edge.v1.UserUpdateMsg) CustomerUpdateMsg(org.thingsboard.server.gen.edge.v1.CustomerUpdateMsg) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Example 22 with AbstractMessage

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

the class BaseEdgeTest method testRpcCall.

@Test
public void testRpcCall() throws Exception {
    Device device = findDeviceByName("Edge Device 1");
    ObjectNode body = mapper.createObjectNode();
    body.put("requestId", new Random().nextInt());
    body.put("requestUUID", Uuids.timeBased().toString());
    body.put("oneway", false);
    body.put("expirationTime", System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10));
    body.put("method", "test_method");
    body.put("params", "{\"param1\":\"value1\"}");
    EdgeEvent edgeEvent = constructEdgeEvent(tenantId, edge.getId(), EdgeEventActionType.RPC_CALL, device.getId().getId(), EdgeEventType.DEVICE, body);
    edgeImitator.expectMessageAmount(1);
    edgeEventService.save(edgeEvent);
    clusterService.onEdgeEventUpdate(tenantId, edge.getId());
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof DeviceRpcCallMsg);
    DeviceRpcCallMsg latestDeviceRpcCallMsg = (DeviceRpcCallMsg) latestMessage;
    Assert.assertEquals("test_method", latestDeviceRpcCallMsg.getRequestMsg().getMethod());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Random(java.util.Random) Device(org.thingsboard.server.common.data.Device) DeviceRpcCallMsg(org.thingsboard.server.gen.edge.v1.DeviceRpcCallMsg) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Example 23 with AbstractMessage

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

the class BaseEdgeTest method verifyEntityViewUpdateMsg.

private void verifyEntityViewUpdateMsg(EntityView entityView, Device device) {
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof EntityViewUpdateMsg);
    EntityViewUpdateMsg entityViewUpdateMsg = (EntityViewUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, entityViewUpdateMsg.getMsgType());
    Assert.assertEquals(entityViewUpdateMsg.getType(), entityView.getType());
    Assert.assertEquals(entityViewUpdateMsg.getName(), entityView.getName());
    Assert.assertEquals(entityViewUpdateMsg.getIdMSB(), entityView.getUuidId().getMostSignificantBits());
    Assert.assertEquals(entityViewUpdateMsg.getIdLSB(), entityView.getUuidId().getLeastSignificantBits());
    Assert.assertEquals(entityViewUpdateMsg.getEntityIdMSB(), device.getUuidId().getMostSignificantBits());
    Assert.assertEquals(entityViewUpdateMsg.getEntityIdLSB(), device.getUuidId().getLeastSignificantBits());
    Assert.assertEquals(entityViewUpdateMsg.getEntityType().name(), device.getId().getEntityType().name());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) EntityViewUpdateMsg(org.thingsboard.server.gen.edge.v1.EntityViewUpdateMsg)

Example 24 with AbstractMessage

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

the class BaseEdgeTest method testEntityView.

@Test
public void testEntityView() throws Exception {
    // 1
    edgeImitator.expectMessageAmount(1);
    Device device = findDeviceByName("Edge Device 1");
    EntityView entityView = new EntityView();
    entityView.setName("Edge EntityView 1");
    entityView.setType("test");
    entityView.setEntityId(device.getId());
    EntityView savedEntityView = doPost("/api/entityView", entityView, EntityView.class);
    doPost("/api/edge/" + edge.getUuidId() + "/entityView/" + savedEntityView.getUuidId(), EntityView.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    verifyEntityViewUpdateMsg(savedEntityView, device);
    // 2
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    EntityViewsRequestMsg.Builder entityViewsRequestBuilder = EntityViewsRequestMsg.newBuilder();
    entityViewsRequestBuilder.setEntityIdMSB(device.getUuidId().getMostSignificantBits());
    entityViewsRequestBuilder.setEntityIdLSB(device.getUuidId().getLeastSignificantBits());
    entityViewsRequestBuilder.setEntityType(device.getId().getEntityType().name());
    testAutoGeneratedCodeByProtobuf(entityViewsRequestBuilder);
    uplinkMsgBuilder.addEntityViewsRequestMsg(entityViewsRequestBuilder.build());
    testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder);
    edgeImitator.expectResponsesAmount(1);
    edgeImitator.expectMessageAmount(1);
    edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
    Assert.assertTrue(edgeImitator.waitForResponses());
    Assert.assertTrue(edgeImitator.waitForMessages());
    verifyEntityViewUpdateMsg(savedEntityView, device);
    // 3
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/edge/" + edge.getUuidId() + "/entityView/" + savedEntityView.getUuidId(), EntityView.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof EntityViewUpdateMsg);
    EntityViewUpdateMsg entityViewUpdateMsg = (EntityViewUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, entityViewUpdateMsg.getMsgType());
    Assert.assertEquals(entityViewUpdateMsg.getIdMSB(), savedEntityView.getUuidId().getMostSignificantBits());
    Assert.assertEquals(entityViewUpdateMsg.getIdLSB(), savedEntityView.getUuidId().getLeastSignificantBits());
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/entityView/" + savedEntityView.getUuidId()).andExpect(status().isOk());
    Assert.assertFalse(edgeImitator.waitForMessages(1));
}
Also used : EntityViewsRequestMsg(org.thingsboard.server.gen.edge.v1.EntityViewsRequestMsg) AbstractMessage(com.google.protobuf.AbstractMessage) EntityView(org.thingsboard.server.common.data.EntityView) Device(org.thingsboard.server.common.data.Device) UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) EntityViewUpdateMsg(org.thingsboard.server.gen.edge.v1.EntityViewUpdateMsg) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Example 25 with AbstractMessage

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

the class BaseEdgeTest method testSendDeviceToCloud.

@Test
public void testSendDeviceToCloud() throws Exception {
    UUID uuid = Uuids.timeBased();
    UplinkMsg.Builder uplinkMsgBuilder = UplinkMsg.newBuilder();
    DeviceUpdateMsg.Builder deviceUpdateMsgBuilder = DeviceUpdateMsg.newBuilder();
    deviceUpdateMsgBuilder.setIdMSB(uuid.getMostSignificantBits());
    deviceUpdateMsgBuilder.setIdLSB(uuid.getLeastSignificantBits());
    deviceUpdateMsgBuilder.setName("Edge Device 2");
    deviceUpdateMsgBuilder.setType("test");
    deviceUpdateMsgBuilder.setMsgType(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE);
    testAutoGeneratedCodeByProtobuf(deviceUpdateMsgBuilder);
    uplinkMsgBuilder.addDeviceUpdateMsg(deviceUpdateMsgBuilder.build());
    edgeImitator.expectResponsesAmount(1);
    edgeImitator.expectMessageAmount(1);
    testAutoGeneratedCodeByProtobuf(uplinkMsgBuilder);
    edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
    Assert.assertTrue(edgeImitator.waitForResponses());
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof DeviceCredentialsRequestMsg);
    DeviceCredentialsRequestMsg latestDeviceCredentialsRequestMsg = (DeviceCredentialsRequestMsg) latestMessage;
    Assert.assertEquals(uuid.getMostSignificantBits(), latestDeviceCredentialsRequestMsg.getDeviceIdMSB());
    Assert.assertEquals(uuid.getLeastSignificantBits(), latestDeviceCredentialsRequestMsg.getDeviceIdLSB());
    UUID newDeviceId = new UUID(latestDeviceCredentialsRequestMsg.getDeviceIdMSB(), latestDeviceCredentialsRequestMsg.getDeviceIdLSB());
    Device device = doGet("/api/device/" + newDeviceId, Device.class);
    Assert.assertNotNull(device);
    Assert.assertEquals("Edge Device 2", device.getName());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) DeviceCredentialsRequestMsg(org.thingsboard.server.gen.edge.v1.DeviceCredentialsRequestMsg) UplinkMsg(org.thingsboard.server.gen.edge.v1.UplinkMsg) Device(org.thingsboard.server.common.data.Device) DeviceUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg) 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