9 changed files with 797 additions and 39 deletions
@ -0,0 +1,178 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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 lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.rule.engine.api.RuleNode; |
|||
import org.thingsboard.rule.engine.api.TbContext; |
|||
import org.thingsboard.rule.engine.api.TbNode; |
|||
import org.thingsboard.rule.engine.api.TbNodeConfiguration; |
|||
import org.thingsboard.rule.engine.api.TbNodeException; |
|||
import org.thingsboard.rule.engine.api.util.TbNodeUtils; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.msg.TbMsgType; |
|||
import org.thingsboard.server.common.data.plugin.ComponentType; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.gen.transport.TransportProtos; |
|||
import org.thingsboard.server.queue.TbQueueCallback; |
|||
import org.thingsboard.server.queue.TbQueueMsgMetadata; |
|||
|
|||
import java.util.Map; |
|||
|
|||
@Slf4j |
|||
@RuleNode( |
|||
type = ComponentType.ACTION, |
|||
name = "device state", |
|||
nodeDescription = "Triggers device connectivity events", |
|||
nodeDetails = "If incoming message originator is a device," + |
|||
" registers configured event for that device in the Device State Service," + |
|||
" which sends appropriate message to the Rule Engine. " + |
|||
"Incoming message is forwarded using the <code>Success</code> chain," + |
|||
" unless an unexpected error occurs during message processing" + |
|||
" then incoming message is forwarded using the <code>Failure</code> chain." + |
|||
"<br>" + |
|||
"Supported device connectivity events are:" + |
|||
"<ul>" + |
|||
"<li>Connect event</li>" + |
|||
"<li>Disconnect event</li>" + |
|||
"<li>Activity event</li>" + |
|||
"<li>Inactivity event</li>" + |
|||
"</ul>" + |
|||
"This node is particularly useful when device isn't using transports to receive data," + |
|||
" such as when fetching data from external API or computing new data within the rule chain.", |
|||
configClazz = TbDeviceStateNodeConfiguration.class, |
|||
uiResources = {"static/rulenode/rulenode-core-config.js"}, |
|||
configDirective = "tbActionNodeDeviceStateConfig" |
|||
) |
|||
public class TbDeviceStateNode implements TbNode { |
|||
|
|||
private static final TbQueueCallback EMPTY_CALLBACK = new TbQueueCallback() { |
|||
|
|||
@Override |
|||
public void onSuccess(TbQueueMsgMetadata metadata) { |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Throwable t) { |
|||
} |
|||
|
|||
}; |
|||
|
|||
private final Map<TbMsgType, ConnectivityEvent> SUPPORTED_EVENTS = Map.of( |
|||
TbMsgType.CONNECT_EVENT, this::sendDeviceConnectMsg, |
|||
TbMsgType.ACTIVITY_EVENT, this::sendDeviceActivityMsg, |
|||
TbMsgType.DISCONNECT_EVENT, this::sendDeviceDisconnectMsg, |
|||
TbMsgType.INACTIVITY_EVENT, this::sendDeviceInactivityMsg |
|||
); |
|||
|
|||
private interface ConnectivityEvent { |
|||
|
|||
void sendEvent(TbContext ctx, TbMsg msg); |
|||
|
|||
} |
|||
|
|||
private TbMsgType event; |
|||
|
|||
@Override |
|||
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { |
|||
TbMsgType event = TbNodeUtils.convert(configuration, TbDeviceStateNodeConfiguration.class).getEvent(); |
|||
if (event == null) { |
|||
throw new TbNodeException("Event cannot be null!", true); |
|||
} |
|||
if (!SUPPORTED_EVENTS.containsKey(event)) { |
|||
throw new TbNodeException("Unsupported event: " + event, true); |
|||
} |
|||
this.event = event; |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void onMsg(TbContext ctx, TbMsg msg) { |
|||
var originator = msg.getOriginator(); |
|||
if (!ctx.isLocalEntity(originator)) { |
|||
log.warn("[{}][device-state-node] Received message from non-local entity [{}]!", ctx.getSelfId(), originator); |
|||
return; |
|||
} |
|||
if (!EntityType.DEVICE.equals(originator.getEntityType())) { |
|||
ctx.tellSuccess(msg); |
|||
return; |
|||
} |
|||
SUPPORTED_EVENTS.get(event).sendEvent(ctx, msg); |
|||
ctx.tellSuccess(msg); |
|||
} |
|||
|
|||
private void sendDeviceConnectMsg(TbContext ctx, TbMsg msg) { |
|||
var tenantUuid = ctx.getTenantId().getId(); |
|||
var deviceUuid = msg.getOriginator().getId(); |
|||
var deviceConnectMsg = TransportProtos.DeviceConnectProto.newBuilder() |
|||
.setTenantIdMSB(tenantUuid.getMostSignificantBits()) |
|||
.setTenantIdLSB(tenantUuid.getLeastSignificantBits()) |
|||
.setDeviceIdMSB(deviceUuid.getMostSignificantBits()) |
|||
.setDeviceIdLSB(deviceUuid.getLeastSignificantBits()) |
|||
.build(); |
|||
var toCoreMsg = TransportProtos.ToCoreMsg.newBuilder() |
|||
.setDeviceConnectMsg(deviceConnectMsg) |
|||
.build(); |
|||
ctx.getClusterService().pushMsgToCore(ctx.getTenantId(), msg.getOriginator(), toCoreMsg, EMPTY_CALLBACK); |
|||
} |
|||
|
|||
private void sendDeviceActivityMsg(TbContext ctx, TbMsg msg) { |
|||
var tenantUuid = ctx.getTenantId().getId(); |
|||
var deviceUuid = msg.getOriginator().getId(); |
|||
var deviceActivityMsg = TransportProtos.DeviceActivityProto.newBuilder() |
|||
.setTenantIdMSB(tenantUuid.getMostSignificantBits()) |
|||
.setTenantIdLSB(tenantUuid.getLeastSignificantBits()) |
|||
.setDeviceIdMSB(deviceUuid.getMostSignificantBits()) |
|||
.setDeviceIdLSB(deviceUuid.getLeastSignificantBits()) |
|||
.setLastActivityTime(System.currentTimeMillis()) |
|||
.build(); |
|||
var toCoreMsg = TransportProtos.ToCoreMsg.newBuilder() |
|||
.setDeviceActivityMsg(deviceActivityMsg) |
|||
.build(); |
|||
ctx.getClusterService().pushMsgToCore(ctx.getTenantId(), msg.getOriginator(), toCoreMsg, EMPTY_CALLBACK); |
|||
} |
|||
|
|||
private void sendDeviceDisconnectMsg(TbContext ctx, TbMsg msg) { |
|||
var tenantUuid = ctx.getTenantId().getId(); |
|||
var deviceUuid = msg.getOriginator().getId(); |
|||
var deviceDisconnectMsg = TransportProtos.DeviceDisconnectProto.newBuilder() |
|||
.setTenantIdMSB(tenantUuid.getMostSignificantBits()) |
|||
.setTenantIdLSB(tenantUuid.getLeastSignificantBits()) |
|||
.setDeviceIdMSB(deviceUuid.getMostSignificantBits()) |
|||
.setDeviceIdLSB(deviceUuid.getLeastSignificantBits()) |
|||
.build(); |
|||
var toCoreMsg = TransportProtos.ToCoreMsg.newBuilder() |
|||
.setDeviceDisconnectMsg(deviceDisconnectMsg) |
|||
.build(); |
|||
ctx.getClusterService().pushMsgToCore(ctx.getTenantId(), msg.getOriginator(), toCoreMsg, EMPTY_CALLBACK); |
|||
} |
|||
|
|||
private void sendDeviceInactivityMsg(TbContext ctx, TbMsg msg) { |
|||
var tenantUuid = ctx.getTenantId().getId(); |
|||
var deviceUuid = msg.getOriginator().getId(); |
|||
var deviceInactivityMsg = TransportProtos.DeviceInactivityProto.newBuilder() |
|||
.setTenantIdMSB(tenantUuid.getMostSignificantBits()) |
|||
.setTenantIdLSB(tenantUuid.getLeastSignificantBits()) |
|||
.setDeviceIdMSB(deviceUuid.getMostSignificantBits()) |
|||
.setDeviceIdLSB(deviceUuid.getLeastSignificantBits()) |
|||
.build(); |
|||
var toCoreMsg = TransportProtos.ToCoreMsg.newBuilder() |
|||
.setDeviceInactivityMsg(deviceInactivityMsg) |
|||
.build(); |
|||
ctx.getClusterService().pushMsgToCore(ctx.getTenantId(), msg.getOriginator(), toCoreMsg, EMPTY_CALLBACK); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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 lombok.Data; |
|||
import org.thingsboard.rule.engine.api.NodeConfiguration; |
|||
import org.thingsboard.server.common.data.msg.TbMsgType; |
|||
|
|||
@Data |
|||
public class TbDeviceStateNodeConfiguration implements NodeConfiguration<TbDeviceStateNodeConfiguration> { |
|||
|
|||
private TbMsgType event; |
|||
|
|||
@Override |
|||
public TbDeviceStateNodeConfiguration defaultConfiguration() { |
|||
var config = new TbDeviceStateNodeConfiguration(); |
|||
config.setEvent(TbMsgType.ACTIVITY_EVENT); |
|||
return config; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,313 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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 org.junit.jupiter.api.BeforeAll; |
|||
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.Mock; |
|||
import org.mockito.junit.jupiter.MockitoExtension; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
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.cluster.TbClusterService; |
|||
import org.thingsboard.server.common.data.Device; |
|||
import org.thingsboard.server.common.data.id.AssetId; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.msg.TbMsgType; |
|||
import org.thingsboard.server.common.msg.TbMsg; |
|||
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|||
import org.thingsboard.server.gen.transport.TransportProtos; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.ArgumentMatchers.eq; |
|||
import static org.mockito.BDDMockito.given; |
|||
import static org.mockito.BDDMockito.then; |
|||
import static org.mockito.Mockito.only; |
|||
import static org.mockito.Mockito.times; |
|||
|
|||
@ExtendWith(MockitoExtension.class) |
|||
public class TbDeviceStateNodeTest { |
|||
|
|||
private static TenantId DUMMY_TENANT_ID; |
|||
private static TbMsg DUMMY_MSG; |
|||
private static DeviceId DUMMY_MSG_ORIGINATOR; |
|||
@Mock |
|||
private TbContext ctxMock; |
|||
@Mock |
|||
private TbClusterService tbClusterServiceMock; |
|||
private TbDeviceStateNode node; |
|||
private TbDeviceStateNodeConfiguration config; |
|||
|
|||
@BeforeAll |
|||
public static void init() { |
|||
DUMMY_TENANT_ID = TenantId.fromUUID(UUID.randomUUID()); |
|||
|
|||
var device = new Device(); |
|||
device.setTenantId(DUMMY_TENANT_ID); |
|||
device.setId(new DeviceId(UUID.randomUUID())); |
|||
device.setName("My humidity sensor"); |
|||
device.setType("Humidity sensor"); |
|||
device.setDeviceProfileId(new DeviceProfileId(UUID.randomUUID())); |
|||
var metaData = new TbMsgMetaData(); |
|||
metaData.putValue("deviceName", device.getName()); |
|||
metaData.putValue("deviceType", device.getType()); |
|||
metaData.putValue("ts", String.valueOf(System.currentTimeMillis())); |
|||
var data = JacksonUtil.newObjectNode(); |
|||
data.put("humidity", 58.3); |
|||
DUMMY_MSG = TbMsg.newMsg( |
|||
TbMsgType.POST_TELEMETRY_REQUEST, device.getId(), metaData, JacksonUtil.toString(data) |
|||
); |
|||
DUMMY_MSG_ORIGINATOR = device.getId(); |
|||
} |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
node = new TbDeviceStateNode(); |
|||
config = new TbDeviceStateNodeConfiguration(); |
|||
} |
|||
|
|||
@Test |
|||
public void givenDefaultConfiguration_whenInvoked_thenCorrectValuesAreSet() { |
|||
// GIVEN-WHEN
|
|||
config = config.defaultConfiguration(); |
|||
|
|||
// THEN
|
|||
assertThat(config.getEvent()).isEqualTo(TbMsgType.ACTIVITY_EVENT); |
|||
} |
|||
|
|||
@Test |
|||
public void givenNullEventInConfig_whenInit_thenThrowsUnrecoverableTbNodeException() { |
|||
// GIVEN
|
|||
config.setEvent(null); |
|||
var nodeConfig = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); |
|||
|
|||
// WHEN-THEN
|
|||
assertThatThrownBy(() -> node.init(ctxMock, nodeConfig)) |
|||
.isInstanceOf(TbNodeException.class) |
|||
.hasMessage("Event cannot be null!") |
|||
.matches(e -> ((TbNodeException) e).isUnrecoverable()); |
|||
} |
|||
|
|||
@Test |
|||
public void givenUnsupportedEventInConfig_whenInit_thenThrowsUnrecoverableTbNodeException() { |
|||
// GIVEN
|
|||
var unsupportedEvent = TbMsgType.TO_SERVER_RPC_REQUEST; |
|||
config.setEvent(unsupportedEvent); |
|||
var nodeConfig = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); |
|||
|
|||
// WHEN-THEN
|
|||
assertThatThrownBy(() -> node.init(ctxMock, nodeConfig)) |
|||
.isInstanceOf(TbNodeException.class) |
|||
.hasMessage("Unsupported event: " + unsupportedEvent) |
|||
.matches(e -> ((TbNodeException) e).isUnrecoverable()); |
|||
} |
|||
|
|||
@Test |
|||
public void givenNonDeviceOriginator_whenOnMsg_thenTellsSuccessAndNoActivityActionsTriggered() { |
|||
// GIVEN
|
|||
var msg = TbMsg.newMsg(TbMsgType.ENTITY_CREATED, new AssetId(UUID.randomUUID()), new TbMsgMetaData(), "{}"); |
|||
|
|||
// WHEN
|
|||
node.onMsg(ctxMock, msg); |
|||
|
|||
// THEN
|
|||
then(ctxMock).should(only()).tellSuccess(msg); |
|||
} |
|||
|
|||
@Test |
|||
public void givenNonLocalOriginator_whenOnMsg_thenTellsSuccessAndNoActivityActionsTriggered() { |
|||
// GIVEN
|
|||
given(ctxMock.isLocalEntity(DUMMY_MSG.getOriginator())).willReturn(false); |
|||
|
|||
// WHEN
|
|||
node.onMsg(ctxMock, DUMMY_MSG); |
|||
|
|||
// THEN
|
|||
then(ctxMock).should(times(1)).isLocalEntity(DUMMY_MSG_ORIGINATOR); |
|||
then(ctxMock).should(times(1)).tellSuccess(DUMMY_MSG); |
|||
then(ctxMock).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
@Test |
|||
public void givenConnectEventInConfig_whenOnMsg_thenOnDeviceConnectCalledAndTellsSuccess() throws TbNodeException { |
|||
// GIVEN
|
|||
config.setEvent(TbMsgType.CONNECT_EVENT); |
|||
var nodeConfig = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); |
|||
node.init(ctxMock, nodeConfig); |
|||
given(ctxMock.getTenantId()).willReturn(DUMMY_TENANT_ID); |
|||
given(ctxMock.getClusterService()).willReturn(tbClusterServiceMock); |
|||
given(ctxMock.isLocalEntity(DUMMY_MSG.getOriginator())).willReturn(true); |
|||
|
|||
// WHEN
|
|||
node.onMsg(ctxMock, DUMMY_MSG); |
|||
|
|||
// THEN
|
|||
var protoCaptor = ArgumentCaptor.forClass(TransportProtos.ToCoreMsg.class); |
|||
then(tbClusterServiceMock).should(times(1)) |
|||
.pushMsgToCore(eq(DUMMY_TENANT_ID), eq(DUMMY_MSG_ORIGINATOR), protoCaptor.capture(), any()); |
|||
|
|||
TransportProtos.DeviceConnectProto expectedDeviceConnectMsg = TransportProtos.DeviceConnectProto.newBuilder() |
|||
.setTenantIdMSB(DUMMY_TENANT_ID.getId().getMostSignificantBits()) |
|||
.setTenantIdLSB(DUMMY_TENANT_ID.getId().getLeastSignificantBits()) |
|||
.setDeviceIdMSB(DUMMY_MSG_ORIGINATOR.getId().getMostSignificantBits()) |
|||
.setDeviceIdLSB(DUMMY_MSG_ORIGINATOR.getId().getLeastSignificantBits()) |
|||
.build(); |
|||
TransportProtos.ToCoreMsg expectedToCoreMsg = TransportProtos.ToCoreMsg.newBuilder() |
|||
.setDeviceConnectMsg(expectedDeviceConnectMsg) |
|||
.build(); |
|||
assertThat(expectedToCoreMsg).isEqualTo(protoCaptor.getValue()); |
|||
|
|||
then(ctxMock).should(times(1)).tellSuccess(DUMMY_MSG); |
|||
then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); |
|||
then(ctxMock).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
@Test |
|||
public void givenActivityEventInConfig_whenOnMsg_thenOnDeviceActivityCalledWithCorrectTimeAndTellsSuccess() |
|||
throws TbNodeException { |
|||
// GIVEN
|
|||
config.setEvent(TbMsgType.ACTIVITY_EVENT); |
|||
var nodeConfig = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); |
|||
node.init(ctxMock, nodeConfig); |
|||
given(ctxMock.getTenantId()).willReturn(DUMMY_TENANT_ID); |
|||
given(ctxMock.getClusterService()).willReturn(tbClusterServiceMock); |
|||
given(ctxMock.isLocalEntity(DUMMY_MSG.getOriginator())).willReturn(true); |
|||
|
|||
|
|||
// WHEN
|
|||
long timeBeforeCall = System.currentTimeMillis(); |
|||
node.onMsg(ctxMock, DUMMY_MSG); |
|||
long timeAfterCall = System.currentTimeMillis(); |
|||
|
|||
// THEN
|
|||
var protoCaptor = ArgumentCaptor.forClass(TransportProtos.ToCoreMsg.class); |
|||
then(tbClusterServiceMock).should(times(1)) |
|||
.pushMsgToCore(eq(DUMMY_TENANT_ID), eq(DUMMY_MSG_ORIGINATOR), protoCaptor.capture(), any()); |
|||
|
|||
TransportProtos.ToCoreMsg actualToCoreMsg = protoCaptor.getValue(); |
|||
long actualLastActivityTime = actualToCoreMsg.getDeviceActivityMsg().getLastActivityTime(); |
|||
|
|||
assertThat(actualLastActivityTime).isGreaterThanOrEqualTo(timeBeforeCall); |
|||
assertThat(actualLastActivityTime).isLessThanOrEqualTo(timeAfterCall); |
|||
|
|||
TransportProtos.DeviceActivityProto updatedActivityMsg = actualToCoreMsg.getDeviceActivityMsg().toBuilder() |
|||
.setLastActivityTime(123L) |
|||
.build(); |
|||
TransportProtos.ToCoreMsg updatedToCoreMsg = actualToCoreMsg.toBuilder() |
|||
.setDeviceActivityMsg(updatedActivityMsg) |
|||
.build(); |
|||
|
|||
TransportProtos.DeviceActivityProto expectedDeviceActivityMsg = TransportProtos.DeviceActivityProto.newBuilder() |
|||
.setTenantIdMSB(DUMMY_TENANT_ID.getId().getMostSignificantBits()) |
|||
.setTenantIdLSB(DUMMY_TENANT_ID.getId().getLeastSignificantBits()) |
|||
.setDeviceIdMSB(DUMMY_MSG_ORIGINATOR.getId().getMostSignificantBits()) |
|||
.setDeviceIdLSB(DUMMY_MSG_ORIGINATOR.getId().getLeastSignificantBits()) |
|||
.setLastActivityTime(123L) |
|||
.build(); |
|||
TransportProtos.ToCoreMsg expectedToCoreMsg = TransportProtos.ToCoreMsg.newBuilder() |
|||
.setDeviceActivityMsg(expectedDeviceActivityMsg) |
|||
.build(); |
|||
|
|||
assertThat(updatedToCoreMsg).isEqualTo(expectedToCoreMsg); |
|||
|
|||
then(ctxMock).should(times(1)).tellSuccess(DUMMY_MSG); |
|||
then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); |
|||
then(ctxMock).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
@Test |
|||
public void givenInactivityEventInConfig_whenOnMsg_thenOnDeviceInactivityCalledAndTellsSuccess() |
|||
throws TbNodeException { |
|||
// GIVEN
|
|||
config.setEvent(TbMsgType.INACTIVITY_EVENT); |
|||
var nodeConfig = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); |
|||
node.init(ctxMock, nodeConfig); |
|||
given(ctxMock.getTenantId()).willReturn(DUMMY_TENANT_ID); |
|||
given(ctxMock.getClusterService()).willReturn(tbClusterServiceMock); |
|||
given(ctxMock.isLocalEntity(DUMMY_MSG.getOriginator())).willReturn(true); |
|||
|
|||
// WHEN
|
|||
node.onMsg(ctxMock, DUMMY_MSG); |
|||
|
|||
// THEN
|
|||
var protoCaptor = ArgumentCaptor.forClass(TransportProtos.ToCoreMsg.class); |
|||
then(tbClusterServiceMock).should(times(1)) |
|||
.pushMsgToCore(eq(DUMMY_TENANT_ID), eq(DUMMY_MSG_ORIGINATOR), protoCaptor.capture(), any()); |
|||
|
|||
TransportProtos.DeviceInactivityProto expectedDeviceInactivityMsg = |
|||
TransportProtos.DeviceInactivityProto.newBuilder() |
|||
.setTenantIdMSB(DUMMY_TENANT_ID.getId().getMostSignificantBits()) |
|||
.setTenantIdLSB(DUMMY_TENANT_ID.getId().getLeastSignificantBits()) |
|||
.setDeviceIdMSB(DUMMY_MSG_ORIGINATOR.getId().getMostSignificantBits()) |
|||
.setDeviceIdLSB(DUMMY_MSG_ORIGINATOR.getId().getLeastSignificantBits()) |
|||
.build(); |
|||
TransportProtos.ToCoreMsg expectedToCoreMsg = TransportProtos.ToCoreMsg.newBuilder() |
|||
.setDeviceInactivityMsg(expectedDeviceInactivityMsg) |
|||
.build(); |
|||
assertThat(expectedToCoreMsg).isEqualTo(protoCaptor.getValue()); |
|||
|
|||
then(ctxMock).should(times(1)).tellSuccess(DUMMY_MSG); |
|||
then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); |
|||
then(ctxMock).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
@Test |
|||
public void givenDisconnectEventInConfig_whenOnMsg_thenOnDeviceDisconnectCalledAndTellsSuccess() |
|||
throws TbNodeException { |
|||
// GIVEN
|
|||
config.setEvent(TbMsgType.DISCONNECT_EVENT); |
|||
var nodeConfig = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); |
|||
node.init(ctxMock, nodeConfig); |
|||
given(ctxMock.getTenantId()).willReturn(DUMMY_TENANT_ID); |
|||
given(ctxMock.getClusterService()).willReturn(tbClusterServiceMock); |
|||
given(ctxMock.isLocalEntity(DUMMY_MSG.getOriginator())).willReturn(true); |
|||
|
|||
// WHEN
|
|||
node.onMsg(ctxMock, DUMMY_MSG); |
|||
|
|||
// THEN
|
|||
var protoCaptor = ArgumentCaptor.forClass(TransportProtos.ToCoreMsg.class); |
|||
then(tbClusterServiceMock).should(times(1)) |
|||
.pushMsgToCore(eq(DUMMY_TENANT_ID), eq(DUMMY_MSG_ORIGINATOR), protoCaptor.capture(), any()); |
|||
|
|||
TransportProtos.DeviceDisconnectProto expectedDeviceDisconnectMsg = |
|||
TransportProtos.DeviceDisconnectProto.newBuilder() |
|||
.setTenantIdMSB(DUMMY_TENANT_ID.getId().getMostSignificantBits()) |
|||
.setTenantIdLSB(DUMMY_TENANT_ID.getId().getLeastSignificantBits()) |
|||
.setDeviceIdMSB(DUMMY_MSG_ORIGINATOR.getId().getMostSignificantBits()) |
|||
.setDeviceIdLSB(DUMMY_MSG_ORIGINATOR.getId().getLeastSignificantBits()) |
|||
.build(); |
|||
TransportProtos.ToCoreMsg expectedToCoreMsg = TransportProtos.ToCoreMsg.newBuilder() |
|||
.setDeviceDisconnectMsg(expectedDeviceDisconnectMsg) |
|||
.build(); |
|||
assertThat(expectedToCoreMsg).isEqualTo(protoCaptor.getValue()); |
|||
|
|||
then(ctxMock).should(times(1)).tellSuccess(DUMMY_MSG); |
|||
then(tbClusterServiceMock).shouldHaveNoMoreInteractions(); |
|||
then(ctxMock).shouldHaveNoMoreInteractions(); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue