From b43a90d1dfab6f3bacfc8b7a2e0e3e21d637e740 Mon Sep 17 00:00:00 2001 From: Dmytro Skarzhynets Date: Wed, 10 Apr 2024 15:24:58 +0300 Subject: [PATCH] Separate create alarm node tests from clear alarm node tests, refactor existing and add more create alarm node tests --- .../rule/engine/action/TbCreateAlarmNode.java | 6 +- .../rule/engine/action/TbAlarmNodeTest.java | 642 --------- .../engine/action/TbClearAlarmNodeTest.java | 218 +++ .../engine/action/TbCreateAlarmNodeTest.java | 1223 +++++++++++++++++ 4 files changed, 1446 insertions(+), 643 deletions(-) delete mode 100644 rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbAlarmNodeTest.java create mode 100644 rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbClearAlarmNodeTest.java create mode 100644 rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbCreateAlarmNodeTest.java diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbCreateAlarmNode.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbCreateAlarmNode.java index 9377fca636..7e535596cd 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbCreateAlarmNode.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/action/TbCreateAlarmNode.java @@ -176,7 +176,7 @@ public class TbCreateAlarmNode extends TbAbstractAlarmNode successCaptor; - @Captor - private ArgumentCaptor> failureCaptor; - - private final RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased()); - private final RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased()); - - private ListeningExecutor dbExecutor; - - private final EntityId originator = new DeviceId(Uuids.timeBased()); - private final EntityId alarmOriginator = new AlarmId(Uuids.timeBased()); - private final TenantId tenantId = TenantId.fromUUID(Uuids.timeBased()); - private final TbMsgMetaData metaData = new TbMsgMetaData(); - private final String rawJson = "{\"name\": \"Vit\", \"passed\": 5}"; - - @Before - public void before() { - dbExecutor = new TestDbCallbackExecutor(); - } - - @Test - public void newAlarmCanBeCreated() { - initWithCreateAlarmScript(); - metaData.putValue("key", "value"); - TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId); - long ts = msg.getTs(); - - when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); - when(alarmService.findLatestActiveByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(null); - Alarm expectedAlarm = Alarm.builder() - .startTs(ts) - .endTs(ts) - .tenantId(tenantId) - .originator(originator) - .severity(AlarmSeverity.CRITICAL) - .propagate(true) - .type("SomeType") - .details(null) - .build(); - when(alarmService.createAlarm(any(AlarmCreateOrUpdateActiveRequest.class))).thenReturn( - AlarmApiCallResult.builder() - .created(true) - .alarm(new AlarmInfo(expectedAlarm)) - .build()); - - node.onMsg(ctx, msg); - - verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); - successCaptor.getValue().run(); - verify(ctx).tellNext(any(), eq("Created")); - - ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); - ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); - ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); - ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); - ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); - verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); - - assertEquals(TbMsgType.ALARM, typeCaptor.getValue()); - assertEquals(originator, originatorCaptor.getValue()); - assertEquals("value", metadataCaptor.getValue().getValue("key")); - assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(DataConstants.IS_NEW_ALARM)); - assertNotSame(metaData, metadataCaptor.getValue()); - - Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); - assertEquals(expectedAlarm, actualAlarm); - } - - @Test - public void buildDetailsThrowsException() { - initWithCreateAlarmScript(); - metaData.putValue("key", "value"); - TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId); - - when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFailedFuture(new NotImplementedException("message"))); - when(alarmService.findLatestActiveByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(null); - - node.onMsg(ctx, msg); - - verifyError(msg, "message", NotImplementedException.class); - - verify(ctx).createScriptEngine(ScriptLanguage.JS, "DETAILS"); - verify(ctx).getAlarmService(); - verify(ctx, times(2)).getDbCallbackExecutor(); - verify(ctx).logJsEvalRequest(); - verify(ctx).getTenantId(); - verify(alarmService).findLatestActiveByOriginatorAndType(tenantId, originator, "SomeType"); - - verifyNoMoreInteractions(ctx, alarmService); - } - - @Test - public void ifAlarmClearedCreateNew() { - initWithCreateAlarmScript(); - metaData.putValue("key", "value"); - TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId); - long ts = msg.getTs(); - Alarm clearedAlarm = Alarm.builder().cleared(true).acknowledged(true).build(); - - when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); - when(alarmService.findLatestActiveByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(clearedAlarm); - - Alarm expectedAlarm = Alarm.builder() - .startTs(ts) - .endTs(ts) - .tenantId(tenantId) - .originator(originator) - .severity(AlarmSeverity.CRITICAL) - .propagate(true) - .type("SomeType") - .details(null) - .build(); - when(alarmService.createAlarm(any(AlarmCreateOrUpdateActiveRequest.class))).thenReturn( - AlarmApiCallResult.builder() - .successful(true) - .created(true) - .alarm(new AlarmInfo(expectedAlarm)) - .build()); - - node.onMsg(ctx, msg); - - verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); - successCaptor.getValue().run(); - verify(ctx).tellNext(any(), eq("Created")); - - ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); - ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); - ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); - ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); - ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); - verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); - - assertEquals(TbMsgType.ALARM, typeCaptor.getValue()); - assertEquals(originator, originatorCaptor.getValue()); - assertEquals("value", metadataCaptor.getValue().getValue("key")); - assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(DataConstants.IS_NEW_ALARM)); - assertNotSame(metaData, metadataCaptor.getValue()); - - - Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); - assertEquals(expectedAlarm, actualAlarm); - } - - @Test - public void alarmCanBeUpdated() { - initWithCreateAlarmScript(); - metaData.putValue("key", "value"); - TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId); - - long oldEndDate = System.currentTimeMillis(); - Alarm activeAlarm = Alarm.builder().type("SomeType").tenantId(tenantId).originator(originator).severity(AlarmSeverity.WARNING).endTs(oldEndDate).build(); - - when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); - when(alarmService.findLatestActiveByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(activeAlarm); - - Alarm expectedAlarm = Alarm.builder() - .tenantId(tenantId) - .originator(originator) - .severity(AlarmSeverity.CRITICAL) - .propagate(true) - .type("SomeType") - .details(null) - .endTs(activeAlarm.getEndTs()) - .build(); - when(alarmService.updateAlarm(any(AlarmUpdateRequest.class))).thenReturn( - AlarmApiCallResult.builder() - .successful(true) - .modified(true) - .old(new Alarm(activeAlarm)) - .alarm(new AlarmInfo(expectedAlarm)) - .build()); - node.onMsg(ctx, msg); - - verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); - successCaptor.getValue().run(); - verify(ctx).tellNext(any(), eq("Updated")); - - ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); - ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); - ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); - ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); - ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); - verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); - - assertEquals(TbMsgType.ALARM, typeCaptor.getValue()); - assertEquals(originator, originatorCaptor.getValue()); - assertEquals("value", metadataCaptor.getValue().getValue("key")); - assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(DataConstants.IS_EXISTING_ALARM)); - assertNotSame(metaData, metadataCaptor.getValue()); - - Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); - assertTrue(activeAlarm.getEndTs() >= oldEndDate); - assertEquals(expectedAlarm, actualAlarm); - } - - @Test - public void alarmCanBeCleared() { - initWithClearAlarmScript(); - metaData.putValue("key", "value"); - TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId); - - long oldEndDate = System.currentTimeMillis(); - Alarm activeAlarm = Alarm.builder().type("SomeType").tenantId(tenantId).originator(originator).severity(AlarmSeverity.WARNING).endTs(oldEndDate).build(); - - Alarm expectedAlarm = Alarm.builder() - .tenantId(tenantId) - .originator(originator) - .cleared(true) - .severity(AlarmSeverity.WARNING) - .propagate(false) - .type("SomeType") - .details(null) - .endTs(oldEndDate) - .build(); - - when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); - when(alarmService.findLatestActiveByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(activeAlarm); - when(alarmService.clearAlarm(eq(activeAlarm.getTenantId()), eq(activeAlarm.getId()), anyLong(), nullable(JsonNode.class))) - .thenReturn(AlarmApiCallResult.builder() - .successful(true) - .cleared(true) - .alarm(new AlarmInfo(expectedAlarm)) - .build()); - - node.onMsg(ctx, msg); - - verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); - successCaptor.getValue().run(); - verify(ctx).tellNext(any(), eq("Cleared")); - - ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); - ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); - ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); - ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); - ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); - verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); - - assertEquals(TbMsgType.ALARM, typeCaptor.getValue()); - assertEquals(originator, originatorCaptor.getValue()); - assertEquals("value", metadataCaptor.getValue().getValue("key")); - assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(DataConstants.IS_CLEARED_ALARM)); - assertNotSame(metaData, metadataCaptor.getValue()); - - Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); - assertEquals(expectedAlarm, actualAlarm); - } - - @Test - public void alarmCanBeClearedWithAlarmOriginator() throws ScriptException, IOException { - initWithClearAlarmScript(); - metaData.putValue("key", "value"); - TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, alarmOriginator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId); - - long oldEndDate = System.currentTimeMillis(); - AlarmId id = new AlarmId(alarmOriginator.getId()); - Alarm activeAlarm = Alarm.builder().type("SomeType").tenantId(tenantId).originator(originator).severity(AlarmSeverity.WARNING).endTs(oldEndDate).build(); - activeAlarm.setId(id); - - Alarm expectedAlarm = Alarm.builder() - .tenantId(tenantId) - .originator(originator) - .cleared(true) - .severity(AlarmSeverity.WARNING) - .propagate(false) - .type("SomeType") - .details(null) - .endTs(oldEndDate) - .build(); - expectedAlarm.setId(id); - - when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); - when(alarmService.findAlarmById(tenantId, id)).thenReturn(activeAlarm); - when(alarmService.clearAlarm(eq(activeAlarm.getTenantId()), eq(activeAlarm.getId()), anyLong(), nullable(JsonNode.class))) - .thenReturn(AlarmApiCallResult.builder() - .successful(true) - .cleared(true) - .alarm(new AlarmInfo(expectedAlarm)) - .build()); - - node.onMsg(ctx, msg); - - verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); - successCaptor.getValue().run(); - verify(ctx).tellNext(any(), eq("Cleared")); - - ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); - ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); - ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); - ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); - ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); - verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); - - assertEquals(TbMsgType.ALARM, typeCaptor.getValue()); - assertEquals(alarmOriginator, originatorCaptor.getValue()); - assertEquals("value", metadataCaptor.getValue().getValue("key")); - assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(DataConstants.IS_CLEARED_ALARM)); - assertNotSame(metaData, metadataCaptor.getValue()); - - Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); - assertEquals(expectedAlarm, actualAlarm); - } - - @Test - public void testCreateAlarmWithDynamicSeverityFromMessageBody() throws Exception { - TbCreateAlarmNodeConfiguration config = new TbCreateAlarmNodeConfiguration(); - config.setPropagate(true); - config.setSeverity("$[alarmSeverity]"); - config.setAlarmType("SomeType"); - config.setScriptLang(ScriptLanguage.JS); - config.setAlarmDetailsBuildJs("DETAILS"); - config.setDynamicSeverity(true); - TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); - - when(ctx.createScriptEngine(ScriptLanguage.JS, "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); - - String rawJson = "{\"alarmSeverity\": \"WARNING\", \"passed\": 5}"; - metaData.putValue("key", "value"); - TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId); - long ts = msg.getTs(); - Alarm expectedAlarm = Alarm.builder() - .startTs(ts) - .endTs(ts) - .tenantId(tenantId) - .originator(originator) - .severity(AlarmSeverity.WARNING) - .propagate(true) - .type("SomeType") - .details(null) - .build(); - - when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); - when(alarmService.findLatestActiveByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(null); - when(alarmService.createAlarm(any(AlarmCreateOrUpdateActiveRequest.class))).thenReturn( - AlarmApiCallResult.builder() - .successful(true) - .created(true) - .alarm(new AlarmInfo(expectedAlarm)) - .build()); - - node.onMsg(ctx, msg); - - verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); - successCaptor.getValue().run(); - verify(ctx).tellNext(any(), eq("Created")); - - ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); - ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); - ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); - ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); - ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); - verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); - - assertEquals(TbMsgType.ALARM, typeCaptor.getValue()); - assertEquals(originator, originatorCaptor.getValue()); - assertEquals("value", metadataCaptor.getValue().getValue("key")); - assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(DataConstants.IS_NEW_ALARM)); - assertNotSame(metaData, metadataCaptor.getValue()); - - Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); - assertEquals(expectedAlarm, actualAlarm); - } - - @Test - public void testCreateAlarmWithDynamicSeverityFromMetadata() throws Exception { - TbCreateAlarmNodeConfiguration config = new TbCreateAlarmNodeConfiguration(); - config.setPropagate(true); - config.setScriptLang(ScriptLanguage.JS); - config.setSeverity("${alarmSeverity}"); - config.setAlarmType("SomeType"); - config.setAlarmDetailsBuildJs("DETAILS"); - config.setDynamicSeverity(true); - TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); - - when(ctx.createScriptEngine(ScriptLanguage.JS, "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(TbMsgType.POST_TELEMETRY_REQUEST, originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId); - long ts = msg.getTs(); - Alarm expectedAlarm = Alarm.builder() - .startTs(ts) - .endTs(ts) - .tenantId(tenantId) - .originator(originator) - .severity(AlarmSeverity.WARNING) - .propagate(true) - .type("SomeType") - .details(null) - .build(); - - when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); - when(alarmService.findLatestActiveByOriginatorAndType(tenantId, originator, "SomeType")).thenReturn(null); - when(alarmService.createAlarm(any(AlarmCreateOrUpdateActiveRequest.class))).thenReturn( - AlarmApiCallResult.builder() - .successful(true) - .created(true) - .alarm(new AlarmInfo(expectedAlarm)) - .build()); - - node.onMsg(ctx, msg); - - verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); - successCaptor.getValue().run(); - verify(ctx).tellNext(any(), eq("Created")); - - ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); - ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); - ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); - ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); - ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); - verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); - - assertEquals(TbMsgType.ALARM, typeCaptor.getValue()); - assertEquals(originator, originatorCaptor.getValue()); - assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(DataConstants.IS_NEW_ALARM)); - assertNotSame(metaData, metadataCaptor.getValue()); - - Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); - assertEquals(expectedAlarm, actualAlarm); - } - - @Test - public void testCreateAlarmsWithPropagationToTenantWithDynamicTypes() throws Exception { - for (int i = 0; i < 10; i++) { - var config = new TbCreateAlarmNodeConfiguration(); - config.setPropagateToTenant(true); - config.setSeverity(AlarmSeverity.CRITICAL.name()); - config.setAlarmType("SomeType" + i); - config.setScriptLang(ScriptLanguage.JS); - config.setAlarmDetailsBuildJs("DETAILS"); - config.setDynamicSeverity(true); - TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); - - when(ctx.createScriptEngine(ScriptLanguage.JS, "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("key", "value"); - TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId); - long ts = msg.getTs(); - Alarm expectedAlarm = Alarm.builder() - .startTs(ts) - .endTs(ts) - .tenantId(tenantId) - .originator(originator) - .severity(AlarmSeverity.CRITICAL) - .propagateToTenant(true) - .type("SomeType" + i) - .details(null) - .build(); - - when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); - when(alarmService.findLatestActiveByOriginatorAndType(tenantId, originator, "SomeType" + i)).thenReturn(null); - when(alarmService.createAlarm(any(AlarmCreateOrUpdateActiveRequest.class))).thenReturn( - AlarmApiCallResult.builder() - .successful(true) - .created(true) - .alarm(new AlarmInfo(expectedAlarm)) - .build()); - node.onMsg(ctx, msg); - - verify(ctx, atMost(10)).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); - successCaptor.getValue().run(); - verify(ctx, atMost(10)).tellNext(any(), eq("Created")); - - ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); - ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); - ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); - ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); - ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); - verify(ctx, atMost(10)).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); - - assertEquals(TbMsgType.ALARM, typeCaptor.getValue()); - assertEquals(originator, originatorCaptor.getValue()); - assertEquals("value", metadataCaptor.getValue().getValue("key")); - assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(DataConstants.IS_NEW_ALARM)); - assertNotSame(metaData, metadataCaptor.getValue()); - - Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); - assertEquals(expectedAlarm, actualAlarm); - } - } - - private void initWithCreateAlarmScript() { - try { - TbCreateAlarmNodeConfiguration config = new TbCreateAlarmNodeConfiguration(); - config.setPropagate(true); - config.setSeverity(AlarmSeverity.CRITICAL.name()); - config.setAlarmType("SomeType"); - config.setScriptLang(ScriptLanguage.JS); - config.setAlarmDetailsBuildJs("DETAILS"); - TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); - - when(ctx.createScriptEngine(ScriptLanguage.JS, "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); - } catch (TbNodeException ex) { - throw new IllegalStateException(ex); - } - } - - private void initWithClearAlarmScript() { - try { - TbClearAlarmNodeConfiguration config = new TbClearAlarmNodeConfiguration(); - config.setAlarmType("SomeType"); - config.setScriptLang(ScriptLanguage.JS); - config.setAlarmDetailsBuildJs("DETAILS"); - TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); - - when(ctx.createScriptEngine(ScriptLanguage.JS, "DETAILS")).thenReturn(detailsJs); - - when(ctx.getTenantId()).thenReturn(tenantId); - when(ctx.getAlarmService()).thenReturn(alarmService); - when(ctx.getDbCallbackExecutor()).thenReturn(dbExecutor); - - node = new TbClearAlarmNode(); - node.init(ctx, nodeConfiguration); - } catch (TbNodeException ex) { - throw new IllegalStateException(ex); - } - } - - private void verifyError(TbMsg msg, String message, Class expectedClass) { - ArgumentCaptor captor = ArgumentCaptor.forClass(Throwable.class); - verify(ctx).tellFailure(same(msg), captor.capture()); - - Throwable value = captor.getValue(); - assertEquals(expectedClass, value.getClass()); - assertEquals(message, value.getMessage()); - } - -} diff --git a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbClearAlarmNodeTest.java b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbClearAlarmNodeTest.java new file mode 100644 index 0000000000..afab0a0a99 --- /dev/null +++ b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbClearAlarmNodeTest.java @@ -0,0 +1,218 @@ +/** + * Copyright © 2016-2024 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.rule.engine.action; + +import com.datastax.oss.driver.api.core.uuid.Uuids; +import com.fasterxml.jackson.databind.JsonNode; +import com.google.common.util.concurrent.Futures; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.thingsboard.common.util.JacksonUtil; +import org.thingsboard.common.util.ListeningExecutor; +import org.thingsboard.rule.engine.TestDbCallbackExecutor; +import org.thingsboard.rule.engine.api.RuleEngineAlarmService; +import org.thingsboard.rule.engine.api.ScriptEngine; +import org.thingsboard.rule.engine.api.TbContext; +import org.thingsboard.rule.engine.api.TbNodeConfiguration; +import org.thingsboard.rule.engine.api.TbNodeException; +import org.thingsboard.server.common.data.DataConstants; +import org.thingsboard.server.common.data.alarm.Alarm; +import org.thingsboard.server.common.data.alarm.AlarmApiCallResult; +import org.thingsboard.server.common.data.alarm.AlarmInfo; +import org.thingsboard.server.common.data.alarm.AlarmSeverity; +import org.thingsboard.server.common.data.id.AlarmId; +import org.thingsboard.server.common.data.id.DeviceId; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.msg.TbMsgType; +import org.thingsboard.server.common.data.script.ScriptLanguage; +import org.thingsboard.server.common.msg.TbMsg; +import org.thingsboard.server.common.msg.TbMsgMetaData; + +import java.util.function.Consumer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class TbClearAlarmNodeTest { + + @Mock + TbContext ctxMock; + @Mock + RuleEngineAlarmService alarmServiceMock; + @Mock + ScriptEngine alarmDetailsScriptMock; + + @Captor + ArgumentCaptor successCaptor; + @Captor + ArgumentCaptor> failureCaptor; + + TbClearAlarmNode node; + + final TenantId tenantId = TenantId.fromUUID(Uuids.timeBased()); + final EntityId msgOriginator = new DeviceId(Uuids.timeBased()); + final EntityId alarmOriginator = new AlarmId(Uuids.timeBased()); + TbMsgMetaData metadata; + + ListeningExecutor dbExecutor; + + @BeforeEach + void before() { + dbExecutor = new TestDbCallbackExecutor(); + metadata = new TbMsgMetaData(); + } + + @Test + void alarmCanBeCleared() { + initWithClearAlarmScript(); + metadata.putValue("key", "value"); + TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, msgOriginator, metadata, "{\"temperature\": 50}"); + + long oldEndDate = System.currentTimeMillis(); + Alarm activeAlarm = Alarm.builder().type("SomeType").tenantId(tenantId).originator(msgOriginator).severity(AlarmSeverity.WARNING).endTs(oldEndDate).build(); + + Alarm expectedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(true) + .severity(AlarmSeverity.WARNING) + .propagate(false) + .type("SomeType") + .details(null) + .endTs(oldEndDate) + .build(); + + when(alarmDetailsScriptMock.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); + when(alarmServiceMock.findLatestActiveByOriginatorAndType(tenantId, msgOriginator, "SomeType")).thenReturn(activeAlarm); + when(alarmServiceMock.clearAlarm(eq(activeAlarm.getTenantId()), eq(activeAlarm.getId()), anyLong(), nullable(JsonNode.class))) + .thenReturn(AlarmApiCallResult.builder() + .successful(true) + .cleared(true) + .alarm(new AlarmInfo(expectedAlarm)) + .build()); + + node.onMsg(ctxMock, msg); + + verify(ctxMock).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); + successCaptor.getValue().run(); + verify(ctxMock).tellNext(any(), eq("Cleared")); + + ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); + ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); + ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); + ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); + ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); + verify(ctxMock).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); + + assertThat(TbMsgType.ALARM).isEqualTo(typeCaptor.getValue()); + assertThat(msgOriginator).isEqualTo(originatorCaptor.getValue()); + assertThat("value").isEqualTo(metadataCaptor.getValue().getValue("key")); + assertThat(Boolean.TRUE.toString()).isEqualTo(metadataCaptor.getValue().getValue(DataConstants.IS_CLEARED_ALARM)); + assertThat(metadata).isNotSameAs(metadataCaptor.getValue()); + + Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); + assertThat(actualAlarm).isEqualTo(expectedAlarm); + } + + @Test + void alarmCanBeClearedWithAlarmOriginator() { + initWithClearAlarmScript(); + metadata.putValue("key", "value"); + TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, alarmOriginator, metadata, "{\"temperature\": 50}"); + + long oldEndDate = System.currentTimeMillis(); + AlarmId id = new AlarmId(alarmOriginator.getId()); + Alarm activeAlarm = Alarm.builder().type("SomeType").tenantId(tenantId).originator(msgOriginator).severity(AlarmSeverity.WARNING).endTs(oldEndDate).build(); + activeAlarm.setId(id); + + Alarm expectedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(true) + .severity(AlarmSeverity.WARNING) + .propagate(false) + .type("SomeType") + .details(null) + .endTs(oldEndDate) + .build(); + expectedAlarm.setId(id); + + when(alarmDetailsScriptMock.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null)); + when(alarmServiceMock.findAlarmById(tenantId, id)).thenReturn(activeAlarm); + when(alarmServiceMock.clearAlarm(eq(activeAlarm.getTenantId()), eq(activeAlarm.getId()), anyLong(), nullable(JsonNode.class))) + .thenReturn(AlarmApiCallResult.builder() + .successful(true) + .cleared(true) + .alarm(new AlarmInfo(expectedAlarm)) + .build()); + + node.onMsg(ctxMock, msg); + + verify(ctxMock).enqueue(any(), successCaptor.capture(), failureCaptor.capture()); + successCaptor.getValue().run(); + verify(ctxMock).tellNext(any(), eq("Cleared")); + + ArgumentCaptor msgCaptor = ArgumentCaptor.forClass(TbMsg.class); + ArgumentCaptor typeCaptor = ArgumentCaptor.forClass(TbMsgType.class); + ArgumentCaptor originatorCaptor = ArgumentCaptor.forClass(EntityId.class); + ArgumentCaptor metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); + ArgumentCaptor dataCaptor = ArgumentCaptor.forClass(String.class); + verify(ctxMock).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); + + assertThat(TbMsgType.ALARM).isEqualTo(typeCaptor.getValue()); + assertThat(alarmOriginator).isEqualTo(originatorCaptor.getValue()); + assertThat("value").isEqualTo(metadataCaptor.getValue().getValue("key")); + assertThat(Boolean.TRUE.toString()).isEqualTo(metadataCaptor.getValue().getValue(DataConstants.IS_CLEARED_ALARM)); + assertThat(metadata).isNotSameAs(metadataCaptor.getValue()); + + Alarm actualAlarm = JacksonUtil.fromBytes(dataCaptor.getValue().getBytes(), Alarm.class); + assertThat(actualAlarm).isEqualTo(expectedAlarm); + } + + private void initWithClearAlarmScript() { + try { + TbClearAlarmNodeConfiguration config = new TbClearAlarmNodeConfiguration(); + config.setAlarmType("SomeType"); + config.setScriptLang(ScriptLanguage.JS); + config.setAlarmDetailsBuildJs("DETAILS"); + TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); + + when(ctxMock.createScriptEngine(ScriptLanguage.JS, "DETAILS")).thenReturn(alarmDetailsScriptMock); + + when(ctxMock.getTenantId()).thenReturn(tenantId); + when(ctxMock.getAlarmService()).thenReturn(alarmServiceMock); + when(ctxMock.getDbCallbackExecutor()).thenReturn(dbExecutor); + + node = new TbClearAlarmNode(); + node.init(ctxMock, nodeConfiguration); + } catch (TbNodeException ex) { + throw new IllegalStateException(ex); + } + } + +} diff --git a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbCreateAlarmNodeTest.java b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbCreateAlarmNodeTest.java new file mode 100644 index 0000000000..c0e27231c4 --- /dev/null +++ b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbCreateAlarmNodeTest.java @@ -0,0 +1,1223 @@ +/** + * Copyright © 2016-2024 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.rule.engine.action; + +import com.datastax.oss.driver.api.core.uuid.Uuids; +import com.fasterxml.jackson.databind.JsonNode; +import com.google.common.util.concurrent.Futures; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; +import org.thingsboard.common.util.JacksonUtil; +import org.thingsboard.common.util.ListeningExecutor; +import org.thingsboard.rule.engine.TestDbCallbackExecutor; +import org.thingsboard.rule.engine.api.RuleEngineAlarmService; +import org.thingsboard.rule.engine.api.ScriptEngine; +import org.thingsboard.rule.engine.api.TbContext; +import org.thingsboard.rule.engine.api.TbNodeConfiguration; +import org.thingsboard.server.common.data.DataConstants; +import org.thingsboard.server.common.data.alarm.Alarm; +import org.thingsboard.server.common.data.alarm.AlarmApiCallResult; +import org.thingsboard.server.common.data.alarm.AlarmCreateOrUpdateActiveRequest; +import org.thingsboard.server.common.data.alarm.AlarmInfo; +import org.thingsboard.server.common.data.alarm.AlarmPropagationInfo; +import org.thingsboard.server.common.data.alarm.AlarmSeverity; +import org.thingsboard.server.common.data.alarm.AlarmUpdateRequest; +import org.thingsboard.server.common.data.id.AlarmId; +import org.thingsboard.server.common.data.id.AssetId; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.DeviceId; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.id.RuleNodeId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.msg.TbMsgType; +import org.thingsboard.server.common.data.msg.TbNodeConnectionType; +import org.thingsboard.server.common.data.script.ScriptLanguage; +import org.thingsboard.server.common.msg.TbMsg; +import org.thingsboard.server.common.msg.TbMsgMetaData; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.never; + +@ExtendWith(MockitoExtension.class) +class TbCreateAlarmNodeTest { + + @Mock + TbContext ctxMock; + @Mock + RuleEngineAlarmService alarmServiceMock; + @Mock + ScriptEngine alarmDetailsScriptMock; + @Mock + TbMsg alarmActionMsgMock; + + @Captor + ArgumentCaptor successCaptor; + + @Spy + TbCreateAlarmNode nodeSpy; + TbCreateAlarmNodeConfiguration config; + + final TenantId tenantId = TenantId.fromUUID(Uuids.timeBased()); + final EntityId msgOriginator = new DeviceId(Uuids.timeBased()); + TbMsgMetaData metadata; + + ListeningExecutor dbExecutor; + + @BeforeEach + void before() { + dbExecutor = new TestDbCallbackExecutor(); + metadata = new TbMsgMetaData(); + config = new TbCreateAlarmNodeConfiguration(); + } + + @Test + @DisplayName("When defaultConfiguration() is called, then correct values are set.") + void whenDefaultConfiguration_thenShouldSetCorrectValues() { + // GIVEN-WHEN + config = config.defaultConfiguration(); + + // THEN + assertThat(config.getAlarmType()).isEqualTo("General Alarm"); + assertThat(config.getScriptLang()).isEqualTo(ScriptLanguage.TBEL); + assertThat(config.getAlarmDetailsBuildJs()).isEqualTo(""" + \ + var details = {}; + if (metadata.prevAlarmDetails) { + details = JSON.parse(metadata.prevAlarmDetails); + //remove prevAlarmDetails from metadata + delete metadata.prevAlarmDetails; + //now metadata is the same as it comes IN this rule node + } + + + return details;"""); + assertThat(config.getAlarmDetailsBuildTbel()).isEqualTo(""" + \ + var details = {}; + if (metadata.prevAlarmDetails != null) { + details = JSON.parse(metadata.prevAlarmDetails); + //remove prevAlarmDetails from metadata + metadata.remove('prevAlarmDetails'); + //now metadata is the same as it comes IN this rule node + } + + + return details;"""); + assertThat(config.getSeverity()).isEqualTo(AlarmSeverity.CRITICAL.name()); + assertThat(config.isPropagate()).isFalse(); + assertThat(config.isPropagateToOwner()).isFalse(); + assertThat(config.isPropagateToTenant()).isFalse(); + assertThat(config.isUseMessageAlarmData()).isFalse(); + assertThat(config.isOverwriteAlarmDetails()).isFalse(); + assertThat(config.isDynamicSeverity()).isFalse(); + assertThat(config.getRelationTypes()).isEmpty(); + } + + @Test + @DisplayName("When node is taking alarm info from default node config and alarm does not exist, then should create new alarm using info from default config.") + void whenAlarmDataIsTakenFromDefaultNodeConfigAndAlarmDoesNotExist_thenNewAlarmIsCreated() throws Exception { + // GIVEN + + // node configuration + config = config.defaultConfiguration(); + + // other values + String alarmType = config.getAlarmType(); + AlarmSeverity alarmSeverity = AlarmSeverity.valueOf(config.getSeverity()); + JsonNode alarmDetails = JacksonUtil.newObjectNode(); + + long metadataTs = 1711631716127L; + metadata.putValue("ts", Long.toString(metadataTs)); + metadata.putValue("location", "Company office"); + + var ruleNodeSelfId = new RuleNodeId(Uuids.timeBased()); + + var incomingMsg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, msgOriginator, metadata, "{\"temperature\": 50}"); + + Alarm existingAlarm = null; + + // expected values + var expectedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(alarmSeverity) + .propagate(false) + .propagateToOwner(false) + .propagateToTenant(false) + .propagateRelationTypes(Collections.emptyList()) + .type(alarmType) + .startTs(metadataTs) + .endTs(metadataTs) + .details(alarmDetails) + .build(); + var expectedCreatedAlarmInfo = new AlarmInfo(expectedAlarm); + expectedCreatedAlarmInfo.setId(new AlarmId(Uuids.timeBased())); + + var expectedCreateAlarmRequest = AlarmCreateOrUpdateActiveRequest.builder() + .tenantId(tenantId) + .customerId(null) + .type(alarmType) + .originator(msgOriginator) + .severity(alarmSeverity) + .startTs(metadataTs) + .endTs(metadataTs) + .details(alarmDetails) + .propagation(AlarmPropagationInfo.builder() + .propagate(false) + .propagateToOwner(false) + .propagateToTenant(false) + .propagateRelationTypes(Collections.emptyList()).build()) + .userId(null) + .edgeAlarmId(null) + .build(); + + // mocks + given(ctxMock.getTenantId()).willReturn(tenantId); + given(ctxMock.getAlarmService()).willReturn(alarmServiceMock); + given(ctxMock.getDbCallbackExecutor()).willReturn(dbExecutor); + given(ctxMock.getSelfId()).willReturn(ruleNodeSelfId); + given(alarmServiceMock.findLatestActiveByOriginatorAndType(tenantId, msgOriginator, alarmType)).willReturn(existingAlarm); + given(alarmDetailsScriptMock.executeJsonAsync(incomingMsg)).willReturn(Futures.immediateFuture(alarmDetails)); + var apiCallResult = AlarmApiCallResult.builder() + .successful(true) + .created(true) + .modified(false) + .cleared(false) + .deleted(false) + .alarm(expectedCreatedAlarmInfo) + .old(null) + .propagatedEntitiesList(Collections.emptyList()) + .build(); + given(alarmServiceMock.createAlarm(expectedCreateAlarmRequest)).willReturn(apiCallResult); + given(ctxMock.alarmActionMsg(expectedCreatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_CREATED)).willReturn(alarmActionMsgMock); + given(ctxMock.transformMsg(any(TbMsg.class), any(TbMsgType.class), any(EntityId.class), any(TbMsgMetaData.class), anyString())) + .willAnswer(answer -> TbMsg.transformMsg( + answer.getArgument(0, TbMsg.class), + answer.getArgument(1, TbMsgType.class), + answer.getArgument(2, EntityId.class), + answer.getArgument(3, TbMsgMetaData.class), + answer.getArgument(4, String.class)) + ); + given(ctxMock.createScriptEngine(ScriptLanguage.TBEL, TbAbstractAlarmNodeConfiguration.ALARM_DETAILS_BUILD_TBEL_TEMPLATE)).willReturn(alarmDetailsScriptMock); + + // node initialization + nodeSpy.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config))); + + // WHEN + nodeSpy.onMsg(ctxMock, incomingMsg); + + // THEN + + // verify alarm details script evaluation + then(ctxMock).should().logJsEvalRequest(); + then(alarmDetailsScriptMock).should().executeJsonAsync(incomingMsg); + then(ctxMock).should().logJsEvalResponse(); + + // verify we called createAlarm() with correct AlarmCreateOrUpdateActiveRequest + then(alarmServiceMock).should().createAlarm(expectedCreateAlarmRequest); + then(alarmServiceMock).should(never()).updateAlarm(any()); + + // verify that we created a correct alarm action message and enqueued it + then(ctxMock).should().alarmActionMsg(expectedCreatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_CREATED); + then(ctxMock).should().enqueue(eq(alarmActionMsgMock), successCaptor.capture(), any()); + + // run success captor to emulate successful sending and to trigger further processing on the success path + successCaptor.getValue().run(); + + // capture and verify an outgoing message + var outgoingMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(ctxMock).should().tellNext(outgoingMsgCaptor.capture(), eq("Created")); + var actualOutgoingMsg = outgoingMsgCaptor.getValue(); + assertThat(actualOutgoingMsg.getType()).isEqualTo(TbMsgType.ALARM.name()); + assertThat(actualOutgoingMsg.getOriginator()).isEqualTo(msgOriginator); + assertThat(actualOutgoingMsg.getData()).isEqualTo(JacksonUtil.valueToTree(expectedCreatedAlarmInfo).toString()); + + Map actualOutgoingMsgMetadataContent = actualOutgoingMsg.getMetaData().getData(); + assertThat(actualOutgoingMsgMetadataContent).containsAllEntriesOf(metadata.getData()); + assertThat(actualOutgoingMsgMetadataContent).containsEntry(DataConstants.IS_NEW_ALARM, Boolean.TRUE.toString()); + assertThat(actualOutgoingMsgMetadataContent).size().isEqualTo(metadata.getData().size() + 1); + + // verify wrong processing paths were not taken + then(ctxMock).should(never()).tellNext(any(), eq(TbNodeConnectionType.FALSE)); + then(ctxMock).should(never()).tellNext(any(), eq("Updated")); + then(ctxMock).should(never()).tellNext(any(), eq("Cleared")); + then(ctxMock).should(never()).tellSuccess(any()); + then(ctxMock).should(never()).tellFailure(any(), any()); + } + + @Test + @DisplayName("When node is taking alarm info from node config and cleared alarm exists, then should create new alarm using info from config.") + void whenAlarmDataIsTakenFromNodeConfigAndClearedAlarmExists_thenNewAlarmIsCreated() throws Exception { + // GIVEN + + // node configuration + config.setAlarmType("$[alarmType]"); + config.setScriptLang(ScriptLanguage.JS); + config.setAlarmDetailsBuildJs(""" + return { + alarmDetails: "Some alarm details" + }; + """); + config.setAlarmDetailsBuildTbel(""" + return { + alarmDetails: "Some alarm details" + }; + """); + config.setDynamicSeverity(true); + config.setSeverity("${alarmSeverity}"); + config.setPropagate(true); + config.setPropagateToOwner(true); + config.setPropagateToTenant(true); + config.setRelationTypes(List.of("RELATION_TYPE_1", "RELATION_TYPE_2", "RELATION_TYPE_3")); + config.setUseMessageAlarmData(false); + config.setOverwriteAlarmDetails(false); + + // other values + String alarmType = "High Temperature"; + AlarmSeverity alarmSeverity = AlarmSeverity.MAJOR; + JsonNode alarmDetails = JacksonUtil.newObjectNode().put("alarmDetails", "Some alarm details"); + + long metadataTs = 1711631716127L; + metadata.putValue("ts", Long.toString(metadataTs)); + metadata.putValue("alarmSeverity", alarmSeverity.name()); + metadata.putValue("location", "Company office"); + + var ruleNodeSelfId = new RuleNodeId(Uuids.timeBased()); + + var incomingMsg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, msgOriginator, metadata, "{\"temperature\": 50, \"alarmType\": \"" + alarmType + "\"}"); + + var existingClearedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(true) + .acknowledged(false) + .severity(AlarmSeverity.WARNING) + .propagate(false) + .propagateToOwner(false) + .propagateToTenant(false) + .propagateRelationTypes(Collections.emptyList()) + .type(alarmType) + .startTs(100L) + .endTs(200L) + .details(JacksonUtil.newObjectNode().put("oldAlarmDetailsProperty", "oldAlarmDetailsPropertyValue")) + .build(); + existingClearedAlarm.setId(new AlarmId(Uuids.timeBased())); + + // expected values + var expectedCreatedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(alarmSeverity) + .propagate(true) + .propagateToOwner(true) + .propagateToTenant(true) + .propagateRelationTypes(config.getRelationTypes()) + .type(alarmType) + .startTs(metadataTs) + .endTs(metadataTs) + .details(alarmDetails) + .build(); + var expectedCreatedAlarmInfo = new AlarmInfo(expectedCreatedAlarm); + expectedCreatedAlarmInfo.setId(new AlarmId(Uuids.timeBased())); + + var expectedCreateAlarmRequest = AlarmCreateOrUpdateActiveRequest.builder() + .tenantId(tenantId) + .customerId(null) + .type(alarmType) + .originator(msgOriginator) + .severity(alarmSeverity) + .startTs(metadataTs) + .endTs(metadataTs) + .details(alarmDetails) + .propagation(AlarmPropagationInfo.builder() + .propagate(true) + .propagateToOwner(true) + .propagateToTenant(true) + .propagateRelationTypes(config.getRelationTypes()).build()) + .userId(null) + .edgeAlarmId(null) + .build(); + + // mocks + given(ctxMock.getTenantId()).willReturn(tenantId); + given(ctxMock.getAlarmService()).willReturn(alarmServiceMock); + given(ctxMock.getDbCallbackExecutor()).willReturn(dbExecutor); + given(ctxMock.getSelfId()).willReturn(ruleNodeSelfId); + given(alarmServiceMock.findLatestActiveByOriginatorAndType(tenantId, msgOriginator, alarmType)).willReturn(existingClearedAlarm); + given(alarmDetailsScriptMock.executeJsonAsync(incomingMsg)).willReturn(Futures.immediateFuture(alarmDetails)); + var apiCallResult = AlarmApiCallResult.builder() + .successful(true) + .created(true) + .modified(false) + .cleared(false) + .deleted(false) + .alarm(expectedCreatedAlarmInfo) + .old(null) + .propagatedEntitiesList(List.of(TenantId.fromUUID(Uuids.timeBased()), new CustomerId(Uuids.timeBased()), new AssetId(Uuids.timeBased()))) + .build(); + given(alarmServiceMock.createAlarm(expectedCreateAlarmRequest)).willReturn(apiCallResult); + given(ctxMock.alarmActionMsg(expectedCreatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_CREATED)).willReturn(alarmActionMsgMock); + given(ctxMock.transformMsg(any(TbMsg.class), any(TbMsgType.class), any(EntityId.class), any(TbMsgMetaData.class), anyString())) + .willAnswer(answer -> TbMsg.transformMsg( + answer.getArgument(0, TbMsg.class), + answer.getArgument(1, TbMsgType.class), + answer.getArgument(2, EntityId.class), + answer.getArgument(3, TbMsgMetaData.class), + answer.getArgument(4, String.class)) + ); + given(ctxMock.createScriptEngine(ScriptLanguage.JS, config.getAlarmDetailsBuildJs())).willReturn(alarmDetailsScriptMock); + + // node initialization + nodeSpy.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config))); + + // WHEN + nodeSpy.onMsg(ctxMock, incomingMsg); + + // THEN + + // verify alarm details script evaluation + then(ctxMock).should().logJsEvalRequest(); + then(alarmDetailsScriptMock).should().executeJsonAsync(incomingMsg); + then(ctxMock).should().logJsEvalResponse(); + + // verify we called createAlarm() with correct AlarmCreateOrUpdateActiveRequest + then(alarmServiceMock).should().createAlarm(expectedCreateAlarmRequest); + then(alarmServiceMock).should(never()).updateAlarm(any()); + + // verify that we created a correct alarm action message and enqueued it + then(ctxMock).should().alarmActionMsg(expectedCreatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_CREATED); + then(ctxMock).should().enqueue(eq(alarmActionMsgMock), successCaptor.capture(), any()); + + // run success captor to emulate successful sending and to trigger further processing on the success path + successCaptor.getValue().run(); + + // capture and verify an outgoing message + var outgoingMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(ctxMock).should().tellNext(outgoingMsgCaptor.capture(), eq("Created")); + var actualOutgoingMsg = outgoingMsgCaptor.getValue(); + assertThat(actualOutgoingMsg.getType()).isEqualTo(TbMsgType.ALARM.name()); + assertThat(actualOutgoingMsg.getOriginator()).isEqualTo(msgOriginator); + assertThat(actualOutgoingMsg.getData()).isEqualTo(JacksonUtil.valueToTree(expectedCreatedAlarmInfo).toString()); + + Map actualOutgoingMsgMetadataContent = actualOutgoingMsg.getMetaData().getData(); + assertThat(actualOutgoingMsgMetadataContent).containsAllEntriesOf(metadata.getData()); + assertThat(actualOutgoingMsgMetadataContent).containsEntry(DataConstants.IS_NEW_ALARM, Boolean.TRUE.toString()); + assertThat(actualOutgoingMsgMetadataContent).size().isEqualTo(metadata.getData().size() + 1); + + // verify wrong processing paths were not taken + then(ctxMock).should(never()).tellNext(any(), eq(TbNodeConnectionType.FALSE)); + then(ctxMock).should(never()).tellNext(any(), eq("Updated")); + then(ctxMock).should(never()).tellNext(any(), eq("Cleared")); + then(ctxMock).should(never()).tellSuccess(any()); + then(ctxMock).should(never()).tellFailure(any(), any()); + } + + @Test + @DisplayName("When node is taking alarm info from node config and active alarm exists, then should update existing alarm using info from config.") + void whenAlarmDataIsTakenFromNodeConfigAndActiveAlarmExists_thenExistingAlarmIsUpdated() throws Exception { + // GIVEN + + // values that changed between existing alarm and updated alarm + AlarmSeverity oldAlarmSeverity = AlarmSeverity.WARNING; + AlarmSeverity newAlarmSeverity = AlarmSeverity.MAJOR; + + boolean oldPropagate = true; + boolean newPropagate = false; + + boolean oldPropagateToOwner = false; + boolean newPropagateToOwner = true; + + boolean oldPropagateToTenant = false; + boolean newPropagateToTenant = true; + + List oldPropagateRelationTypes = List.of("RELATION_TYPE_1", "RELATION_TYPE_2", "RELATION_TYPE_3"); + List newPropagateRelationTypes = Collections.emptyList(); + + JsonNode oldAlarmDetails = JacksonUtil.newObjectNode().put("oldAlarmDetailsKey", "oldAlarmDetailsValue"); + JsonNode newAlarmDetails = JacksonUtil.newObjectNode().put("newAlarmDetails", "Some alarm details TBEL").set("oldAlarmDetails", oldAlarmDetails); + + long oldEndTs = 200L; + long newEndTs = 300L; + + // node configuration + config.setAlarmType("${alarmType}"); + config.setScriptLang(ScriptLanguage.TBEL); + config.setAlarmDetailsBuildJs(""" + return { + oldAlarmDetails: metadata.prevAlarmDetails, + newAlarmDetails: "Some alarm details JS" + }; + """); + config.setAlarmDetailsBuildTbel(""" + return { + oldAlarmDetails: metadata.prevAlarmDetails, + newAlarmDetails: "Some alarm details TBEL" + }; + """); + config.setDynamicSeverity(true); + config.setSeverity("$[alarmSeverity]"); + config.setPropagate(newPropagate); + config.setPropagateToOwner(newPropagateToOwner); + config.setPropagateToTenant(newPropagateToTenant); + config.setRelationTypes(newPropagateRelationTypes); + config.setUseMessageAlarmData(false); + config.setOverwriteAlarmDetails(false); + + // other values + String alarmType = "High Temperature"; + + long metadataTs = 1711631716127L; + metadata.putValue("ts", Long.toString(metadataTs)); + metadata.putValue("alarmType", alarmType); + metadata.putValue("location", "Company office"); + + var ruleNodeSelfId = new RuleNodeId(Uuids.timeBased()); + + var incomingMsg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, msgOriginator, metadata, "{\"temperature\": 50, \"alarmSeverity\": \"" + newAlarmSeverity.name() + "\"}"); + + var existingAlarmId = new AlarmId(Uuids.timeBased()); + var existingActiveAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(oldAlarmSeverity) + .propagate(oldPropagate) + .propagateToOwner(oldPropagateToOwner) + .propagateToTenant(oldPropagateToTenant) + .propagateRelationTypes(oldPropagateRelationTypes) + .type(alarmType) + .startTs(100L) + .endTs(oldEndTs) + .details(oldAlarmDetails) + .build(); + existingActiveAlarm.setId(existingAlarmId); + + // expected values + var expectedUpdatedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(newAlarmSeverity) + .propagate(newPropagate) + .propagateToOwner(newPropagateToOwner) + .propagateToTenant(newPropagateToTenant) + .propagateRelationTypes(newPropagateRelationTypes) + .type(alarmType) + .startTs(100L) + .endTs(newEndTs) + .details(newAlarmDetails) + .build(); + expectedUpdatedAlarm.setId(existingAlarmId); + var expectedUpdatedAlarmInfo = new AlarmInfo(expectedUpdatedAlarm); + + var expectedUpdateAlarmRequest = AlarmUpdateRequest.builder() + .tenantId(tenantId) + .alarmId(existingAlarmId) + .severity(newAlarmSeverity) + .startTs(100L) + .endTs(newEndTs) + .details(newAlarmDetails) + .propagation(AlarmPropagationInfo.builder() + .propagate(newPropagate) + .propagateToOwner(newPropagateToOwner) + .propagateToTenant(newPropagateToTenant) + .propagateRelationTypes(newPropagateRelationTypes).build()) + .userId(null) + .build(); + + // mocks + given(ctxMock.getTenantId()).willReturn(tenantId); + given(ctxMock.getAlarmService()).willReturn(alarmServiceMock); + given(ctxMock.getDbCallbackExecutor()).willReturn(dbExecutor); + given(ctxMock.getSelfId()).willReturn(ruleNodeSelfId); + given(alarmServiceMock.findLatestActiveByOriginatorAndType(tenantId, msgOriginator, alarmType)).willReturn(existingActiveAlarm); + given(alarmDetailsScriptMock.executeJsonAsync(any())).willReturn(Futures.immediateFuture(newAlarmDetails)); + doReturn(newEndTs).when(nodeSpy).currentTimeMillis(); + var apiCallResult = AlarmApiCallResult.builder() + .successful(true) + .created(false) + .modified(true) + .cleared(false) + .deleted(false) + .alarm(expectedUpdatedAlarmInfo) + .old(new Alarm(existingActiveAlarm)) + .propagatedEntitiesList(List.of(tenantId)) + .build(); + given(alarmServiceMock.updateAlarm(expectedUpdateAlarmRequest)).willReturn(apiCallResult); + given(ctxMock.alarmActionMsg(expectedUpdatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_UPDATED)).willReturn(alarmActionMsgMock); + given(ctxMock.transformMsg(any(TbMsg.class), any(TbMsgType.class), any(EntityId.class), any(TbMsgMetaData.class), anyString())) + .willAnswer(answer -> TbMsg.transformMsg( + answer.getArgument(0, TbMsg.class), + answer.getArgument(1, TbMsgType.class), + answer.getArgument(2, EntityId.class), + answer.getArgument(3, TbMsgMetaData.class), + answer.getArgument(4, String.class)) + ); + given(ctxMock.createScriptEngine(ScriptLanguage.TBEL, config.getAlarmDetailsBuildTbel())).willReturn(alarmDetailsScriptMock); + + // node initialization + nodeSpy.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config))); + + // WHEN + nodeSpy.onMsg(ctxMock, incomingMsg); + + // THEN + + // verify alarm details script evaluation + then(ctxMock).should().logJsEvalRequest(); + var dummyMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(alarmDetailsScriptMock).should().executeJsonAsync(dummyMsgCaptor.capture()); + TbMsg actualDummyMsg = dummyMsgCaptor.getValue(); + assertThat(actualDummyMsg.getType()).isEqualTo(incomingMsg.getType()); + assertThat(actualDummyMsg.getData()).isEqualTo(incomingMsg.getData()); + assertThat(actualDummyMsg.getMetaData().getData()).containsEntry("prevAlarmDetails", JacksonUtil.toString(oldAlarmDetails)); + then(ctxMock).should().logJsEvalResponse(); + + // verify we called updateAlarm() with correct AlarmUpdateRequest + then(alarmServiceMock).should().updateAlarm(expectedUpdateAlarmRequest); + then(alarmServiceMock).should(never()).createAlarm(any()); + + // verify that we created a correct alarm action message and enqueued it + then(ctxMock).should().alarmActionMsg(expectedUpdatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_UPDATED); + then(ctxMock).should().enqueue(eq(alarmActionMsgMock), successCaptor.capture(), any()); + + // run success captor to emulate successful queueing of an alarm action message and to trigger further processing on the success path + successCaptor.getValue().run(); + + // capture and verify an outgoing message + var outgoingMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(ctxMock).should().tellNext(outgoingMsgCaptor.capture(), eq("Updated")); + var actualOutgoingMsg = outgoingMsgCaptor.getValue(); + assertThat(actualOutgoingMsg.getType()).isEqualTo(TbMsgType.ALARM.name()); + assertThat(actualOutgoingMsg.getOriginator()).isEqualTo(msgOriginator); + assertThat(actualOutgoingMsg.getData()).isEqualTo(JacksonUtil.valueToTree(expectedUpdatedAlarmInfo).toString()); + + Map actualOutgoingMsgMetadataContent = actualOutgoingMsg.getMetaData().getData(); + assertThat(actualOutgoingMsgMetadataContent).containsAllEntriesOf(metadata.getData()); + assertThat(actualOutgoingMsgMetadataContent).containsEntry(DataConstants.IS_EXISTING_ALARM, Boolean.TRUE.toString()); + assertThat(actualOutgoingMsgMetadataContent).size().isEqualTo(metadata.getData().size() + 1); + + // verify wrong processing paths were not taken + then(ctxMock).should(never()).tellNext(any(), eq(TbNodeConnectionType.FALSE)); + then(ctxMock).should(never()).tellNext(any(), eq("Created")); + then(ctxMock).should(never()).tellNext(any(), eq("Cleared")); + then(ctxMock).should(never()).tellSuccess(any()); + then(ctxMock).should(never()).tellFailure(any(), any()); + } + + @Test + @DisplayName("When node is taking alarm data from incoming message and cleared alarm exists, then should create new alarm using info from incoming message.") + void whenAlarmDataIsTakenFromMsgAndClearedAlarmExists_thenNewAlarmIsCreated() throws Exception { + // GIVEN + + // node configuration + config = config.defaultConfiguration(); + config.setUseMessageAlarmData(true); + config.setOverwriteAlarmDetails(false); + + // other values + String alarmType = "High Temperature"; + AlarmSeverity alarmSeverity = AlarmSeverity.MAJOR; + JsonNode alarmDetails = JacksonUtil.newObjectNode().put("alarmDetails", "Some alarm details"); + + // alarm that is inside an incoming message + var alarmFromIncomingMessage = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(alarmSeverity) + .propagate(true) + .propagateToOwner(true) + .propagateToTenant(true) + .propagateRelationTypes(Collections.emptyList()) + .type(alarmType) + .startTs(100L) + .endTs(300L) + .details(alarmDetails) + .build(); + + long metadataTs = 1711631716127L; + metadata.putValue("ts", Long.toString(metadataTs)); + metadata.putValue("location", "Company office"); + + var ruleNodeSelfId = new RuleNodeId(Uuids.timeBased()); + + var incomingMsg = TbMsg.newMsg(TbMsgType.ALARM, msgOriginator, metadata, JacksonUtil.toString(alarmFromIncomingMessage)); + + var existingClearedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(true) + .acknowledged(false) + .severity(AlarmSeverity.WARNING) + .propagate(false) + .propagateToOwner(true) + .propagateToTenant(true) + .propagateRelationTypes(Collections.emptyList()) + .type(alarmType) + .startTs(100L) + .endTs(200L) + .details(JacksonUtil.newObjectNode()) + .build(); + existingClearedAlarm.setId(new AlarmId(Uuids.timeBased())); + + // expected values + var expectedCreatedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(alarmSeverity) + .propagate(true) + .propagateToOwner(true) + .propagateToTenant(true) + .propagateRelationTypes(Collections.emptyList()) + .type(alarmType) + .startTs(100L) + .endTs(300L) + .details(alarmDetails) + .build(); + var expectedCreatedAlarmInfo = new AlarmInfo(expectedCreatedAlarm); + expectedCreatedAlarmInfo.setId(new AlarmId(Uuids.timeBased())); + + var expectedCreateAlarmRequest = AlarmCreateOrUpdateActiveRequest.builder() + .tenantId(tenantId) + .customerId(null) + .type(alarmType) + .originator(msgOriginator) + .severity(alarmSeverity) + .startTs(100L) + .endTs(300L) + .details(alarmDetails) + .propagation(AlarmPropagationInfo.builder() + .propagate(true) + .propagateToOwner(true) + .propagateToTenant(true) + .propagateRelationTypes(Collections.emptyList()).build()) + .userId(null) + .edgeAlarmId(null) + .build(); + + // mocks + given(ctxMock.getTenantId()).willReturn(tenantId); + given(ctxMock.getAlarmService()).willReturn(alarmServiceMock); + given(ctxMock.getDbCallbackExecutor()).willReturn(dbExecutor); + given(ctxMock.getSelfId()).willReturn(ruleNodeSelfId); + given(alarmServiceMock.findLatestActiveByOriginatorAndType(tenantId, msgOriginator, alarmType)).willReturn(existingClearedAlarm); + var apiCallResult = AlarmApiCallResult.builder() + .successful(true) + .created(true) + .modified(false) + .cleared(false) + .deleted(false) + .alarm(expectedCreatedAlarmInfo) + .old(null) + .propagatedEntitiesList(List.of(TenantId.fromUUID(Uuids.timeBased()), new CustomerId(Uuids.timeBased()), new AssetId(Uuids.timeBased()))) + .build(); + given(alarmServiceMock.createAlarm(expectedCreateAlarmRequest)).willReturn(apiCallResult); + given(ctxMock.alarmActionMsg(expectedCreatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_CREATED)).willReturn(alarmActionMsgMock); + given(ctxMock.transformMsg(any(TbMsg.class), any(TbMsgType.class), any(EntityId.class), any(TbMsgMetaData.class), anyString())) + .willAnswer(answer -> TbMsg.transformMsg( + answer.getArgument(0, TbMsg.class), + answer.getArgument(1, TbMsgType.class), + answer.getArgument(2, EntityId.class), + answer.getArgument(3, TbMsgMetaData.class), + answer.getArgument(4, String.class)) + ); + given(ctxMock.createScriptEngine(ScriptLanguage.TBEL, config.getAlarmDetailsBuildTbel())).willReturn(alarmDetailsScriptMock); + + // node initialization + nodeSpy.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config))); + + // WHEN + nodeSpy.onMsg(ctxMock, incomingMsg); + + // THEN + + // verify alarm details script was not evaluated + then(ctxMock).should(never()).logJsEvalRequest(); + then(alarmDetailsScriptMock).should(never()).executeJsonAsync(any()); + then(ctxMock).should(never()).logJsEvalResponse(); + + // verify we called createAlarm() with correct AlarmCreateOrUpdateActiveRequest + then(alarmServiceMock).should().createAlarm(expectedCreateAlarmRequest); + then(alarmServiceMock).should(never()).updateAlarm(any()); + + // verify that we created a correct alarm action message and enqueued it + then(ctxMock).should().alarmActionMsg(expectedCreatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_CREATED); + then(ctxMock).should().enqueue(eq(alarmActionMsgMock), successCaptor.capture(), any()); + + // run success captor to emulate successful sending and to trigger further processing on the success path + successCaptor.getValue().run(); + + // capture and verify an outgoing message + var outgoingMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(ctxMock).should().tellNext(outgoingMsgCaptor.capture(), eq("Created")); + var actualOutgoingMsg = outgoingMsgCaptor.getValue(); + assertThat(actualOutgoingMsg.getType()).isEqualTo(TbMsgType.ALARM.name()); + assertThat(actualOutgoingMsg.getOriginator()).isEqualTo(msgOriginator); + assertThat(actualOutgoingMsg.getData()).isEqualTo(JacksonUtil.valueToTree(expectedCreatedAlarmInfo).toString()); + + Map actualOutgoingMsgMetadataContent = actualOutgoingMsg.getMetaData().getData(); + assertThat(actualOutgoingMsgMetadataContent).containsAllEntriesOf(metadata.getData()); + assertThat(actualOutgoingMsgMetadataContent).containsEntry(DataConstants.IS_NEW_ALARM, Boolean.TRUE.toString()); + assertThat(actualOutgoingMsgMetadataContent).size().isEqualTo(metadata.getData().size() + 1); + + // verify wrong processing paths were not taken + then(ctxMock).should(never()).tellNext(any(), eq(TbNodeConnectionType.FALSE)); + then(ctxMock).should(never()).tellNext(any(), eq("Updated")); + then(ctxMock).should(never()).tellNext(any(), eq("Cleared")); + then(ctxMock).should(never()).tellSuccess(any()); + then(ctxMock).should(never()).tellFailure(any(), any()); + } + + @Test + @DisplayName("When node is taking alarm data from incoming message and active alarm exists, then should update existing alarm using info from incoming message.") + void whenAlarmDataIsTakenFromMsgAndActiveAlarmExists_thenExistingAlarmIsUpdated() throws Exception { + // GIVEN + + // values that changed between existing alarm and updated alarm + AlarmSeverity oldAlarmSeverity = AlarmSeverity.WARNING; + AlarmSeverity newAlarmSeverity = AlarmSeverity.MAJOR; + + boolean oldPropagate = true; + boolean newPropagate = false; + + boolean oldPropagateToOwner = false; + boolean newPropagateToOwner = true; + + boolean oldPropagateToTenant = false; + boolean newPropagateToTenant = true; + + List oldPropagateRelationTypes = List.of("RELATION_TYPE_1", "RELATION_TYPE_2", "RELATION_TYPE_3"); + List newPropagateRelationTypes = Collections.emptyList(); + + JsonNode oldAlarmDetails = JacksonUtil.newObjectNode().put("oldAlarmDetailsKey", "oldAlarmDetailsValue"); + JsonNode newAlarmDetails = JacksonUtil.newObjectNode().put("newAlarmDetails", "Some alarm details TBEL").set("oldAlarmDetails", oldAlarmDetails); + + long oldEndTs = 200L; + long newEndTs = 300L; + + // node configuration + config = config.defaultConfiguration(); + config.setUseMessageAlarmData(true); + config.setOverwriteAlarmDetails(true); + + // other values + String alarmType = "High Temperature"; + + long metadataTs = 1711631716127L; + metadata.putValue("ts", Long.toString(metadataTs)); + metadata.putValue("location", "Company office"); + + var ruleNodeSelfId = new RuleNodeId(Uuids.timeBased()); + + // alarm that is inside an incoming message + var alarmFromIncomingMessage = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(newAlarmSeverity) + .propagate(newPropagate) + .propagateToOwner(newPropagateToOwner) + .propagateToTenant(newPropagateToTenant) + .propagateRelationTypes(newPropagateRelationTypes) + .type(alarmType) + .startTs(100L) + .endTs(newEndTs) + .details(newAlarmDetails) + .build(); + + var incomingMsg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, msgOriginator, metadata, JacksonUtil.toString(alarmFromIncomingMessage)); + + var existingAlarmId = new AlarmId(Uuids.timeBased()); + var existingActiveAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(oldAlarmSeverity) + .propagate(oldPropagate) + .propagateToOwner(oldPropagateToOwner) + .propagateToTenant(oldPropagateToTenant) + .propagateRelationTypes(oldPropagateRelationTypes) + .type(alarmType) + .startTs(100L) + .endTs(oldEndTs) + .details(oldAlarmDetails) + .build(); + existingActiveAlarm.setId(existingAlarmId); + + // expected values + var expectedUpdatedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(newAlarmSeverity) + .propagate(newPropagate) + .propagateToOwner(newPropagateToOwner) + .propagateToTenant(newPropagateToTenant) + .propagateRelationTypes(newPropagateRelationTypes) + .type(alarmType) + .startTs(100L) + .endTs(newEndTs) + .details(newAlarmDetails) + .build(); + expectedUpdatedAlarm.setId(existingAlarmId); + var expectedUpdatedAlarmInfo = new AlarmInfo(expectedUpdatedAlarm); + + var expectedUpdateAlarmRequest = AlarmUpdateRequest.builder() + .tenantId(tenantId) + .alarmId(existingAlarmId) + .severity(newAlarmSeverity) + .startTs(100L) + .endTs(newEndTs) + .details(newAlarmDetails) + .propagation(AlarmPropagationInfo.builder() + .propagate(newPropagate) + .propagateToOwner(newPropagateToOwner) + .propagateToTenant(newPropagateToTenant) + .propagateRelationTypes(newPropagateRelationTypes).build()) + .userId(null) + .build(); + + // mocks + given(ctxMock.getTenantId()).willReturn(tenantId); + given(ctxMock.getAlarmService()).willReturn(alarmServiceMock); + given(ctxMock.getDbCallbackExecutor()).willReturn(dbExecutor); + given(ctxMock.getSelfId()).willReturn(ruleNodeSelfId); + given(alarmServiceMock.findLatestActiveByOriginatorAndType(tenantId, msgOriginator, alarmType)).willReturn(existingActiveAlarm); + given(alarmDetailsScriptMock.executeJsonAsync(any())).willReturn(Futures.immediateFuture(newAlarmDetails)); + doReturn(newEndTs).when(nodeSpy).currentTimeMillis(); + var apiCallResult = AlarmApiCallResult.builder() + .successful(true) + .created(false) + .modified(true) + .cleared(false) + .deleted(false) + .alarm(expectedUpdatedAlarmInfo) + .old(new Alarm(existingActiveAlarm)) + .propagatedEntitiesList(List.of(tenantId)) + .build(); + given(alarmServiceMock.updateAlarm(expectedUpdateAlarmRequest)).willReturn(apiCallResult); + given(ctxMock.alarmActionMsg(expectedUpdatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_UPDATED)).willReturn(alarmActionMsgMock); + given(ctxMock.transformMsg(any(TbMsg.class), any(TbMsgType.class), any(EntityId.class), any(TbMsgMetaData.class), anyString())) + .willAnswer(answer -> TbMsg.transformMsg( + answer.getArgument(0, TbMsg.class), + answer.getArgument(1, TbMsgType.class), + answer.getArgument(2, EntityId.class), + answer.getArgument(3, TbMsgMetaData.class), + answer.getArgument(4, String.class)) + ); + given(ctxMock.createScriptEngine(ScriptLanguage.TBEL, config.getAlarmDetailsBuildTbel())).willReturn(alarmDetailsScriptMock); + + // node initialization + nodeSpy.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config))); + + // WHEN + nodeSpy.onMsg(ctxMock, incomingMsg); + + // THEN + + // verify alarm details script evaluation + then(ctxMock).should().logJsEvalRequest(); + var dummyMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(alarmDetailsScriptMock).should().executeJsonAsync(dummyMsgCaptor.capture()); + TbMsg actualDummyMsg = dummyMsgCaptor.getValue(); + assertThat(actualDummyMsg.getType()).isEqualTo(incomingMsg.getType()); + assertThat(actualDummyMsg.getData()).isEqualTo(incomingMsg.getData()); + assertThat(actualDummyMsg.getMetaData().getData()).containsEntry("prevAlarmDetails", JacksonUtil.toString(oldAlarmDetails)); + then(ctxMock).should().logJsEvalResponse(); + + // verify we called updateAlarm() with correct AlarmUpdateRequest + then(alarmServiceMock).should().updateAlarm(expectedUpdateAlarmRequest); + then(alarmServiceMock).should(never()).createAlarm(any()); + + // verify that we created a correct alarm action message and enqueued it + then(ctxMock).should().alarmActionMsg(expectedUpdatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_UPDATED); + then(ctxMock).should().enqueue(eq(alarmActionMsgMock), successCaptor.capture(), any()); + + // run success captor to emulate successful queueing of an alarm action message and to trigger further processing on the success path + successCaptor.getValue().run(); + + // capture and verify an outgoing message + var outgoingMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(ctxMock).should().tellNext(outgoingMsgCaptor.capture(), eq("Updated")); + var actualOutgoingMsg = outgoingMsgCaptor.getValue(); + assertThat(actualOutgoingMsg.getType()).isEqualTo(TbMsgType.ALARM.name()); + assertThat(actualOutgoingMsg.getOriginator()).isEqualTo(msgOriginator); + assertThat(actualOutgoingMsg.getData()).isEqualTo(JacksonUtil.valueToTree(expectedUpdatedAlarmInfo).toString()); + + Map actualOutgoingMsgMetadataContent = actualOutgoingMsg.getMetaData().getData(); + assertThat(actualOutgoingMsgMetadataContent).containsAllEntriesOf(metadata.getData()); + assertThat(actualOutgoingMsgMetadataContent).containsEntry(DataConstants.IS_EXISTING_ALARM, Boolean.TRUE.toString()); + assertThat(actualOutgoingMsgMetadataContent).size().isEqualTo(metadata.getData().size() + 1); + + // verify wrong processing paths were not taken + then(ctxMock).should(never()).tellNext(any(), eq(TbNodeConnectionType.FALSE)); + then(ctxMock).should(never()).tellNext(any(), eq("Created")); + then(ctxMock).should(never()).tellNext(any(), eq("Cleared")); + then(ctxMock).should(never()).tellSuccess(any()); + then(ctxMock).should(never()).tellFailure(any(), any()); + } + + @Test + @DisplayName("When only severity was updated (other fields the same), then should consider this as an alarm update and take update processing path.") + void whenOnlySeverityWasUpdated_thenShouldTakeAlarmUpdatedPath() throws Exception { + // GIVEN + + // values that changed between existing alarm and updated alarm + AlarmSeverity oldAlarmSeverity = AlarmSeverity.WARNING; + AlarmSeverity newAlarmSeverity = AlarmSeverity.MAJOR; + + boolean propagate = true; + boolean propagateToOwner = false; + boolean propagateToTenant = false; + List propagateRelationTypes = List.of("RELATION_TYPE_1", "RELATION_TYPE_2", "RELATION_TYPE_3"); + JsonNode alarmDetails = JacksonUtil.newObjectNode().put("oldAlarmDetailsKey", "oldAlarmDetailsValue"); + long endTs = 200L; + + // node configuration + config = config.defaultConfiguration(); + config.setUseMessageAlarmData(true); + config.setOverwriteAlarmDetails(true); + + // other values + String alarmType = "High Temperature"; + + long metadataTs = 1711631716127L; + metadata.putValue("ts", Long.toString(metadataTs)); + metadata.putValue("location", "Company office"); + + var ruleNodeSelfId = new RuleNodeId(Uuids.timeBased()); + + // alarm that is inside an incoming message + var alarmFromIncomingMessage = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(newAlarmSeverity) + .propagate(propagate) + .propagateToOwner(propagateToOwner) + .propagateToTenant(propagateToTenant) + .propagateRelationTypes(propagateRelationTypes) + .type(alarmType) + .startTs(100L) + .endTs(endTs) + .details(alarmDetails) + .build(); + + var incomingMsg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, msgOriginator, metadata, JacksonUtil.toString(alarmFromIncomingMessage)); + + var existingAlarmId = new AlarmId(Uuids.timeBased()); + var existingActiveAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(oldAlarmSeverity) + .propagate(propagate) + .propagateToOwner(propagateToOwner) + .propagateToTenant(propagateToTenant) + .propagateRelationTypes(propagateRelationTypes) + .type(alarmType) + .startTs(100L) + .endTs(endTs) + .details(alarmDetails) + .build(); + existingActiveAlarm.setId(existingAlarmId); + + // expected values + var expectedUpdatedAlarm = Alarm.builder() + .tenantId(tenantId) + .originator(msgOriginator) + .cleared(false) + .acknowledged(false) + .severity(newAlarmSeverity) + .propagate(propagate) + .propagateToOwner(propagateToOwner) + .propagateToTenant(propagateToTenant) + .propagateRelationTypes(propagateRelationTypes) + .type(alarmType) + .startTs(100L) + .endTs(endTs) + .details(alarmDetails) + .build(); + expectedUpdatedAlarm.setId(existingAlarmId); + var expectedUpdatedAlarmInfo = new AlarmInfo(expectedUpdatedAlarm); + + var expectedUpdateAlarmRequest = AlarmUpdateRequest.builder() + .tenantId(tenantId) + .alarmId(existingAlarmId) + .severity(newAlarmSeverity) + .startTs(100L) + .endTs(endTs) + .details(alarmDetails) + .propagation(AlarmPropagationInfo.builder() + .propagate(propagate) + .propagateToOwner(propagateToOwner) + .propagateToTenant(propagateToTenant) + .propagateRelationTypes(propagateRelationTypes).build()) + .userId(null) + .build(); + + // mocks + given(ctxMock.getTenantId()).willReturn(tenantId); + given(ctxMock.getAlarmService()).willReturn(alarmServiceMock); + given(ctxMock.getDbCallbackExecutor()).willReturn(dbExecutor); + given(ctxMock.getSelfId()).willReturn(ruleNodeSelfId); + given(alarmServiceMock.findLatestActiveByOriginatorAndType(tenantId, msgOriginator, alarmType)).willReturn(existingActiveAlarm); + given(alarmDetailsScriptMock.executeJsonAsync(any())).willReturn(Futures.immediateFuture(alarmDetails)); + doReturn(endTs).when(nodeSpy).currentTimeMillis(); + var apiCallResult = AlarmApiCallResult.builder() + .successful(true) + .created(false) + .modified(true) + .cleared(false) + .deleted(false) + .alarm(expectedUpdatedAlarmInfo) + .old(new Alarm(existingActiveAlarm)) + .propagatedEntitiesList(List.of(tenantId)) + .build(); + given(alarmServiceMock.updateAlarm(expectedUpdateAlarmRequest)).willReturn(apiCallResult); + given(ctxMock.alarmActionMsg(expectedUpdatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_UPDATED)).willReturn(alarmActionMsgMock); + given(ctxMock.transformMsg(any(TbMsg.class), any(TbMsgType.class), any(EntityId.class), any(TbMsgMetaData.class), anyString())) + .willAnswer(answer -> TbMsg.transformMsg( + answer.getArgument(0, TbMsg.class), + answer.getArgument(1, TbMsgType.class), + answer.getArgument(2, EntityId.class), + answer.getArgument(3, TbMsgMetaData.class), + answer.getArgument(4, String.class)) + ); + given(ctxMock.createScriptEngine(ScriptLanguage.TBEL, config.getAlarmDetailsBuildTbel())).willReturn(alarmDetailsScriptMock); + + // node initialization + nodeSpy.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config))); + + // WHEN + nodeSpy.onMsg(ctxMock, incomingMsg); + + // THEN + + // verify alarm details script evaluation + then(ctxMock).should().logJsEvalRequest(); + var dummyMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(alarmDetailsScriptMock).should().executeJsonAsync(dummyMsgCaptor.capture()); + TbMsg actualDummyMsg = dummyMsgCaptor.getValue(); + assertThat(actualDummyMsg.getType()).isEqualTo(incomingMsg.getType()); + assertThat(actualDummyMsg.getData()).isEqualTo(incomingMsg.getData()); + assertThat(actualDummyMsg.getMetaData().getData()).containsEntry("prevAlarmDetails", JacksonUtil.toString(alarmDetails)); + then(ctxMock).should().logJsEvalResponse(); + + // verify we called updateAlarm() with correct AlarmUpdateRequest + then(alarmServiceMock).should().updateAlarm(expectedUpdateAlarmRequest); + then(alarmServiceMock).should(never()).createAlarm(any()); + + // verify that we created a correct alarm action message and enqueued it + then(ctxMock).should().alarmActionMsg(expectedUpdatedAlarmInfo, ruleNodeSelfId, TbMsgType.ENTITY_UPDATED); + then(ctxMock).should().enqueue(eq(alarmActionMsgMock), successCaptor.capture(), any()); + + // run success captor to emulate successful queueing of an alarm action message and to trigger further processing on the success path + successCaptor.getValue().run(); + + // capture and verify an outgoing message + var outgoingMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); + then(ctxMock).should().tellNext(outgoingMsgCaptor.capture(), eq("Updated")); + var actualOutgoingMsg = outgoingMsgCaptor.getValue(); + assertThat(actualOutgoingMsg.getType()).isEqualTo(TbMsgType.ALARM.name()); + assertThat(actualOutgoingMsg.getOriginator()).isEqualTo(msgOriginator); + assertThat(actualOutgoingMsg.getData()).isEqualTo(JacksonUtil.valueToTree(expectedUpdatedAlarmInfo).toString()); + + Map actualOutgoingMsgMetadataContent = actualOutgoingMsg.getMetaData().getData(); + assertThat(actualOutgoingMsgMetadataContent).containsAllEntriesOf(metadata.getData()); + assertThat(actualOutgoingMsgMetadataContent).containsEntry(DataConstants.IS_EXISTING_ALARM, Boolean.TRUE.toString()); + assertThat(actualOutgoingMsgMetadataContent).size().isEqualTo(metadata.getData().size() + 1); + + // verify wrong processing paths were not taken + then(ctxMock).should(never()).tellNext(any(), eq(TbNodeConnectionType.FALSE)); + then(ctxMock).should(never()).tellNext(any(), eq("Created")); + then(ctxMock).should(never()).tellNext(any(), eq("Cleared")); + then(ctxMock).should(never()).tellSuccess(any()); + then(ctxMock).should(never()).tellFailure(any(), any()); + } + + @Test + @DisplayName("When the alarm details script throws an exception, " + + "node should tell failure with that exception, and it should neither create nor update any alarms, nor should it send any other messages.") + void whenAlarmDetailsScriptThrowsException_thenShouldTellFailureAndNoOtherActions() throws Exception { + // GIVEN + config = config.defaultConfiguration(); + + var incomingMsg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, msgOriginator, metadata, "{\"temperature\": 50}"); + + given(ctxMock.getTenantId()).willReturn(tenantId); + given(ctxMock.getAlarmService()).willReturn(alarmServiceMock); + given(ctxMock.getDbCallbackExecutor()).willReturn(dbExecutor); + given(ctxMock.createScriptEngine(ScriptLanguage.TBEL, config.getAlarmDetailsBuildTbel())).willReturn(alarmDetailsScriptMock); + + var expectedException = new ExecutionException("Failed to execute script.", new RuntimeException("Something went wrong!")); + given(alarmDetailsScriptMock.executeJsonAsync(incomingMsg)).willReturn(Futures.immediateFailedFuture(expectedException)); + + nodeSpy.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config))); + + // WHEN + nodeSpy.onMsg(ctxMock, incomingMsg); + + // THEN + var exceptionCaptor = ArgumentCaptor.forClass(Throwable.class); + then(ctxMock).should().tellFailure(eq(incomingMsg), exceptionCaptor.capture()); + Throwable actualException = exceptionCaptor.getValue(); + assertThat(actualException).isEqualTo(expectedException); + + then(alarmServiceMock).should(never()).createAlarm(any()); + then(alarmServiceMock).should(never()).updateAlarm(any()); + + then(ctxMock).should(never()).tellNext(any(), eq(TbNodeConnectionType.FALSE)); + then(ctxMock).should(never()).tellNext(any(), eq("Created")); + then(ctxMock).should(never()).tellNext(any(), eq("Updated")); + then(ctxMock).should(never()).tellNext(any(), eq("Cleared")); + then(ctxMock).should(never()).tellSuccess(any()); + } + +}