Search in sources :

Example 51 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class RuleChainMsgConstructorTest method testConstructRuleChainMetadataUpdatedMsg_V_3_3_3.

@Test
public void testConstructRuleChainMetadataUpdatedMsg_V_3_3_3() throws JsonProcessingException {
    RuleChainId ruleChainId = new RuleChainId(UUID.randomUUID());
    RuleChainMsgConstructor constructor = new RuleChainMsgConstructor();
    RuleChainMetaData ruleChainMetaData = createRuleChainMetaData(ruleChainId, 3, createRuleNodes(ruleChainId), createConnections());
    RuleChainMetadataUpdateMsg ruleChainMetadataUpdateMsg = constructor.constructRuleChainMetadataUpdatedMsg(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, ruleChainMetaData, EdgeVersion.V_3_3_3);
    Assert.assertEquals("First rule node index incorrect!", 3, ruleChainMetadataUpdateMsg.getFirstNodeIndex());
    Assert.assertEquals("Nodes count incorrect!", 12, ruleChainMetadataUpdateMsg.getNodesCount());
    Assert.assertEquals("Connections count incorrect!", 13, ruleChainMetadataUpdateMsg.getConnectionsCount());
    Assert.assertEquals("Rule chain connections count incorrect!", 0, ruleChainMetadataUpdateMsg.getRuleChainConnectionsCount());
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(3, 6, "Success"), ruleChainMetadataUpdateMsg.getConnections(0));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(3, 10, "Success"), ruleChainMetadataUpdateMsg.getConnections(1));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(3, 0, "Success"), ruleChainMetadataUpdateMsg.getConnections(2));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(4, 11, "Success"), ruleChainMetadataUpdateMsg.getConnections(3));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(5, 11, "Success"), ruleChainMetadataUpdateMsg.getConnections(4));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(6, 11, "Attributes Updated"), ruleChainMetadataUpdateMsg.getConnections(5));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(6, 7, "RPC Request from Device"), ruleChainMetadataUpdateMsg.getConnections(6));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(6, 4, "Post telemetry"), ruleChainMetadataUpdateMsg.getConnections(7));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(6, 5, "Post attributes"), ruleChainMetadataUpdateMsg.getConnections(8));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(6, 8, "Other"), ruleChainMetadataUpdateMsg.getConnections(9));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(6, 9, "RPC Request to Device"), ruleChainMetadataUpdateMsg.getConnections(10));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(7, 11, "Success"), ruleChainMetadataUpdateMsg.getConnections(11));
    compareNodeConnectionInfoAndProto(createNodeConnectionInfo(10, 9, "RPC"), ruleChainMetadataUpdateMsg.getConnections(12));
}
Also used : RuleChainMetadataUpdateMsg(org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Test(org.junit.Test)

Example 52 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class TbTransformMsgNodeTest method exceptionHandledCorrectly.

@Test
public void exceptionHandledCorrectly() throws TbNodeException, ScriptException {
    initWithScript();
    TbMsgMetaData metaData = new TbMsgMetaData();
    metaData.putValue("temp", "7");
    String rawJson = "{\"passed\": 5";
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("USER", null, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    when(scriptEngine.executeUpdateAsync(msg)).thenReturn(Futures.immediateFailedFuture(new IllegalStateException("error")));
    node.onMsg(ctx, msg);
    verifyError(msg, "error", IllegalStateException.class);
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Example 53 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class MqttClientTest method clientSideRpc.

@Test
public void clientSideRpc() throws Exception {
    restClient.login("tenant@thingsboard.org", "tenant");
    Device device = createDevice("mqtt_");
    DeviceCredentials deviceCredentials = restClient.getDeviceCredentialsByDeviceId(device.getId()).get();
    MqttMessageListener listener = new MqttMessageListener();
    MqttClient mqttClient = getMqttClient(deviceCredentials, listener);
    mqttClient.on("v1/devices/me/rpc/request/+", listener, MqttQoS.AT_LEAST_ONCE).get();
    // Get the default rule chain id to make it root again after test finished
    RuleChainId defaultRuleChainId = getDefaultRuleChainId();
    // Create a new root rule chain
    RuleChainId ruleChainId = createRootRuleChainForRpcResponse();
    TimeUnit.SECONDS.sleep(3);
    // Send the request to the server
    JsonObject clientRequest = new JsonObject();
    clientRequest.addProperty("method", "getResponse");
    clientRequest.addProperty("params", true);
    Integer requestId = 42;
    mqttClient.publish("v1/devices/me/rpc/request/" + requestId, Unpooled.wrappedBuffer(clientRequest.toString().getBytes())).get();
    // Check the response from the server
    TimeUnit.SECONDS.sleep(1);
    MqttEvent responseFromServer = listener.getEvents().poll(1, TimeUnit.SECONDS);
    Integer responseId = Integer.valueOf(Objects.requireNonNull(responseFromServer).getTopic().substring("v1/devices/me/rpc/response/".length()));
    Assert.assertEquals(requestId, responseId);
    Assert.assertEquals("requestReceived", mapper.readTree(responseFromServer.getMessage()).get("response").asText());
    // Make the default rule chain a root again
    ResponseEntity<RuleChain> rootRuleChainResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/ruleChain/{ruleChainId}/root", null, RuleChain.class, defaultRuleChainId);
    Assert.assertTrue(rootRuleChainResponse.getStatusCode().is2xxSuccessful());
    // Delete the created rule chain
    restClient.getRestTemplate().delete(HTTPS_URL + "/api/ruleChain/{ruleChainId}", ruleChainId);
    restClient.getRestTemplate().delete(HTTPS_URL + "/api/device/" + device.getId());
}
Also used : MqttClient(org.thingsboard.mqtt.MqttClient) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Device(org.thingsboard.server.common.data.Device) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) JsonObject(com.google.gson.JsonObject) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) AbstractContainerTest(org.thingsboard.server.msa.AbstractContainerTest)

Example 54 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class TbTransformMsgNodeTest method metadataCanBeUpdated.

@Test
public void metadataCanBeUpdated() throws TbNodeException, ScriptException {
    initWithScript();
    TbMsgMetaData metaData = new TbMsgMetaData();
    metaData.putValue("temp", "7");
    String rawJson = "{\"passed\": 5}";
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("USER", null, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    TbMsg transformedMsg = TbMsg.newMsg("USER", null, metaData, TbMsgDataType.JSON, "{new}", ruleChainId, ruleNodeId);
    when(scriptEngine.executeUpdateAsync(msg)).thenReturn(Futures.immediateFuture(Collections.singletonList(transformedMsg)));
    node.onMsg(ctx, msg);
    verify(ctx).getDbCallbackExecutor();
    ArgumentCaptor<TbMsg> captor = ArgumentCaptor.forClass(TbMsg.class);
    verify(ctx).tellSuccess(captor.capture());
    TbMsg actualMsg = captor.getValue();
    assertEquals(transformedMsg, actualMsg);
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Example 55 with RuleChainId

use of org.thingsboard.server.common.data.id.RuleChainId in project thingsboard by thingsboard.

the class TbAlarmNodeTest method testCreateAlarmWithDynamicSeverityFromMetadata.

@Test
public void testCreateAlarmWithDynamicSeverityFromMetadata() throws Exception {
    TbCreateAlarmNodeConfiguration config = new TbCreateAlarmNodeConfiguration();
    config.setPropagate(true);
    config.setSeverity("${alarmSeverity}");
    config.setAlarmType("SomeType");
    config.setAlarmDetailsBuildJs("DETAILS");
    config.setDynamicSeverity(true);
    ObjectMapper mapper = new ObjectMapper();
    TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
    when(ctx.createJsScriptEngine("DETAILS")).thenReturn(detailsJs);
    when(ctx.getTenantId()).thenReturn(tenantId);
    when(ctx.getAlarmService()).thenReturn(alarmService);
    when(ctx.getDbCallbackExecutor()).thenReturn(dbExecutor);
    node = new TbCreateAlarmNode();
    node.init(ctx, nodeConfiguration);
    metaData.putValue("alarmSeverity", "WARNING");
    TbMsg msg = TbMsg.newMsg("USER", originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    long ts = msg.getTs();
    when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null));
    when(alarmService.findLatestByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(Futures.immediateFuture(null));
    doAnswer((Answer<Alarm>) invocationOnMock -> (Alarm) (invocationOnMock.getArguments())[0]).when(alarmService).createOrUpdateAlarm(any(Alarm.class));
    node.onMsg(ctx, msg);
    verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture());
    successCaptor.getValue().run();
    verify(ctx).tellNext(any(), eq("Created"));
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals("ALARM", typeCaptor.getValue());
    assertEquals(originator, originatorCaptor.getValue());
    assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(IS_NEW_ALARM));
    assertNotSame(metaData, metadataCaptor.getValue());
    Alarm actualAlarm = new ObjectMapper().readValue(dataCaptor.getValue().getBytes(), Alarm.class);
    Alarm expectedAlarm = Alarm.builder().startTs(ts).endTs(ts).tenantId(tenantId).originator(originator).status(ACTIVE_UNACK).severity(WARNING).propagate(true).type("SomeType").details(null).build();
    assertEquals(expectedAlarm, actualAlarm);
}
Also used : CRITICAL(org.thingsboard.server.common.data.alarm.AlarmSeverity.CRITICAL) RuleEngineAlarmService(org.thingsboard.rule.engine.api.RuleEngineAlarmService) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) Assert.assertNotSame(org.junit.Assert.assertNotSame) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) ACTIVE_UNACK(org.thingsboard.server.common.data.alarm.AlarmStatus.ACTIVE_UNACK) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Mockito.doAnswer(org.mockito.Mockito.doAnswer) JsonNode(com.fasterxml.jackson.databind.JsonNode) ScriptException(javax.script.ScriptException) DeviceId(org.thingsboard.server.common.data.id.DeviceId) IS_NEW_ALARM(org.thingsboard.server.common.data.DataConstants.IS_NEW_ALARM) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TbNodeConfiguration(org.thingsboard.rule.engine.api.TbNodeConfiguration) Mockito.atMost(org.mockito.Mockito.atMost) WARNING(org.thingsboard.server.common.data.alarm.AlarmSeverity.WARNING) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.eq(org.mockito.Mockito.eq) NotImplementedException(org.apache.commons.lang3.NotImplementedException) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) Mock(org.mockito.Mock) IS_CLEARED_ALARM(org.thingsboard.server.common.data.DataConstants.IS_CLEARED_ALARM) RunWith(org.junit.runner.RunWith) Callable(java.util.concurrent.Callable) TbContext(org.thingsboard.rule.engine.api.TbContext) Captor(org.mockito.Captor) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) ListeningExecutor(org.thingsboard.common.util.ListeningExecutor) EntityId(org.thingsboard.server.common.data.id.EntityId) ScriptEngine(org.thingsboard.rule.engine.api.ScriptEngine) CLEARED_UNACK(org.thingsboard.server.common.data.alarm.AlarmStatus.CLEARED_UNACK) Mockito.anyLong(org.mockito.Mockito.anyLong) Mockito.same(org.mockito.Mockito.same) IS_EXISTING_ALARM(org.thingsboard.server.common.data.DataConstants.IS_EXISTING_ALARM) Before(org.junit.Before) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) CLEARED_ACK(org.thingsboard.server.common.data.alarm.AlarmStatus.CLEARED_ACK) Consumer(java.util.function.Consumer) Futures(com.google.common.util.concurrent.Futures) AlarmId(org.thingsboard.server.common.data.id.AlarmId) TbMsgDataType(org.thingsboard.server.common.msg.TbMsgDataType) Assert.assertEquals(org.junit.Assert.assertEquals) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbNodeConfiguration(org.thingsboard.rule.engine.api.TbNodeConfiguration) EntityId(org.thingsboard.server.common.data.id.EntityId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TbMsg(org.thingsboard.server.common.msg.TbMsg) Test(org.junit.Test)

Aggregations

RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)60 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)22 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)20 Test (org.junit.Test)17 TbMsg (org.thingsboard.server.common.msg.TbMsg)16 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)12 EntityId (org.thingsboard.server.common.data.id.EntityId)12 ApiOperation (io.swagger.annotations.ApiOperation)11 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 TenantId (org.thingsboard.server.common.data.id.TenantId)11 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)11 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)10 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)8 Futures (com.google.common.util.concurrent.Futures)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 IOException (java.io.IOException)7 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6