|
|
|
@ -20,15 +20,11 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.junit.jupiter.api.AfterEach; |
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
import org.mockito.ArgumentCaptor; |
|
|
|
import org.mockito.Captor; |
|
|
|
import org.mockito.junit.MockitoJUnitRunner; |
|
|
|
import org.thingsboard.rule.engine.api.RuleEngineTelemetryService; |
|
|
|
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.id.DeviceId; |
|
|
|
import org.thingsboard.server.common.msg.TbMsg; |
|
|
|
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|
|
|
@ -42,8 +38,10 @@ import java.util.function.Consumer; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
import static org.mockito.ArgumentMatchers.anyBoolean; |
|
|
|
import static org.mockito.ArgumentMatchers.anyList; |
|
|
|
import static org.mockito.ArgumentMatchers.anyString; |
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
import static org.mockito.BDDMockito.willAnswer; |
|
|
|
import static org.mockito.BDDMockito.willReturn; |
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
@ -51,14 +49,18 @@ import static org.mockito.Mockito.never; |
|
|
|
import static org.mockito.Mockito.spy; |
|
|
|
import static org.mockito.Mockito.times; |
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
import static org.thingsboard.server.common.data.DataConstants.NOTIFY_DEVICE_METADATA_KEY; |
|
|
|
import static org.thingsboard.server.common.data.DataConstants.SCOPE; |
|
|
|
import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE; |
|
|
|
import static org.thingsboard.server.common.data.DataConstants.SHARED_SCOPE; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
public class TbMsgDeleteAttributesTest { |
|
|
|
public class TbMsgDeleteAttributesNodeTest { |
|
|
|
final ObjectMapper mapper = new ObjectMapper(); |
|
|
|
|
|
|
|
DeviceId deviceId; |
|
|
|
TbMsgDeleteAttributes node; |
|
|
|
TbMsgDeleteAttributesConfiguration config; |
|
|
|
TbMsgDeleteAttributesNode node; |
|
|
|
TbMsgDeleteAttributesNodeConfiguration config; |
|
|
|
TbNodeConfiguration nodeConfiguration; |
|
|
|
TbContext ctx; |
|
|
|
TbMsgCallback callback; |
|
|
|
@ -70,20 +72,20 @@ public class TbMsgDeleteAttributesTest { |
|
|
|
deviceId = new DeviceId(UUID.randomUUID()); |
|
|
|
callback = mock(TbMsgCallback.class); |
|
|
|
ctx = mock(TbContext.class); |
|
|
|
config = new TbMsgDeleteAttributesConfiguration().defaultConfiguration(); |
|
|
|
config = new TbMsgDeleteAttributesNodeConfiguration().defaultConfiguration(); |
|
|
|
config.setKeys(List.of("${TestAttribute_1}", "$[TestAttribute_2]", "$[TestAttribute_3]", "TestAttribute_4")); |
|
|
|
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config)); |
|
|
|
node = spy(new TbMsgDeleteAttributes()); |
|
|
|
node = spy(new TbMsgDeleteAttributesNode()); |
|
|
|
node.init(ctx, nodeConfiguration); |
|
|
|
telemetryService = mock(RuleEngineTelemetryService.class); |
|
|
|
|
|
|
|
willReturn(telemetryService).given(ctx).getTelemetryService(); |
|
|
|
willAnswer(invocation -> { |
|
|
|
TelemetryNodeCallback callBack = invocation.getArgument(4); |
|
|
|
TelemetryNodeCallback callBack = invocation.getArgument(5); |
|
|
|
callBack.onSuccess(null); |
|
|
|
return null; |
|
|
|
}).given(telemetryService).deleteAndNotify( |
|
|
|
any(), any(), anyString(), anyList(), any()); |
|
|
|
any(), any(), anyString(), anyList(), anyBoolean(), any()); |
|
|
|
} |
|
|
|
|
|
|
|
@AfterEach |
|
|
|
@ -93,30 +95,51 @@ public class TbMsgDeleteAttributesTest { |
|
|
|
|
|
|
|
@Test |
|
|
|
void givenDefaultConfig_whenVerify_thenOK() { |
|
|
|
TbMsgDeleteAttributesConfiguration defaultConfig = new TbMsgDeleteAttributesConfiguration().defaultConfiguration(); |
|
|
|
assertThat(defaultConfig.getScope()).isEqualTo(DataConstants.SERVER_SCOPE); |
|
|
|
TbMsgDeleteAttributesNodeConfiguration defaultConfig = new TbMsgDeleteAttributesNodeConfiguration().defaultConfiguration(); |
|
|
|
assertThat(defaultConfig.getScope()).isEqualTo(SERVER_SCOPE); |
|
|
|
assertThat(defaultConfig.getKeys()).isEqualTo(Collections.emptyList()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void givenMsg_whenOnMsg_thenVerifyOutput_NoSendAttributesDeletedNotification() throws Exception { |
|
|
|
onMsg_thenVerifyOutput(false); |
|
|
|
void givenMsg_whenOnMsg_thenVerifyOutput_NoSendAttributesDeletedNotification_NoNotifyDevice() throws Exception { |
|
|
|
onMsg_thenVerifyOutput(false, false, false); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void givenMsg_whenOnMsg_thenVerifyOutput_SendAttributesDeletedNotification() throws Exception { |
|
|
|
void givenMsg_whenOnMsg_thenVerifyOutput_SendAttributesDeletedNotification_NoNotifyDevice() throws Exception { |
|
|
|
config.setSendAttributesDeletedNotification(true); |
|
|
|
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config)); |
|
|
|
node.init(ctx, nodeConfiguration); |
|
|
|
onMsg_thenVerifyOutput(true); |
|
|
|
onMsg_thenVerifyOutput(true, false, false); |
|
|
|
} |
|
|
|
|
|
|
|
void onMsg_thenVerifyOutput(boolean sendAttributesDeletedNotification) throws Exception { |
|
|
|
@Test |
|
|
|
void givenMsg_whenOnMsg_thenVerifyOutput_SendAttributesDeletedNotification_NotifyDevice() throws Exception { |
|
|
|
config.setSendAttributesDeletedNotification(true); |
|
|
|
config.setNotifyDevice(true); |
|
|
|
config.setScope(SHARED_SCOPE); |
|
|
|
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config)); |
|
|
|
node.init(ctx, nodeConfiguration); |
|
|
|
onMsg_thenVerifyOutput(true, true, false); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void givenMsg_whenOnMsg_thenVerifyOutput_NoSendAttributesDeletedNotification_NotifyDeviceMetadata() throws Exception { |
|
|
|
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config)); |
|
|
|
node.init(ctx, nodeConfiguration); |
|
|
|
onMsg_thenVerifyOutput(false, false, true); |
|
|
|
} |
|
|
|
|
|
|
|
void onMsg_thenVerifyOutput(boolean sendAttributesDeletedNotification, boolean notifyDevice, boolean notifyDeviceMetadata) throws Exception { |
|
|
|
final Map<String, String> mdMap = Map.of( |
|
|
|
"TestAttribute_1", "temperature", |
|
|
|
"city", "NY" |
|
|
|
); |
|
|
|
final TbMsgMetaData metaData = new TbMsgMetaData(mdMap); |
|
|
|
TbMsgMetaData metaData = new TbMsgMetaData(mdMap); |
|
|
|
if (notifyDeviceMetadata) { |
|
|
|
metaData.putValue(NOTIFY_DEVICE_METADATA_KEY, "true"); |
|
|
|
metaData.putValue(SCOPE, SHARED_SCOPE); |
|
|
|
} |
|
|
|
final String data = "{\"TestAttribute_2\": \"humidity\", \"TestAttribute_3\": \"voltage\"}"; |
|
|
|
|
|
|
|
TbMsg msg = TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", deviceId, metaData, data, callback); |
|
|
|
@ -133,6 +156,6 @@ public class TbMsgDeleteAttributesTest { |
|
|
|
} |
|
|
|
verify(ctx, times(1)).tellSuccess(newMsgCaptor.capture()); |
|
|
|
verify(ctx, never()).tellFailure(any(), any()); |
|
|
|
verify(telemetryService, times(1)).deleteAndNotify(any(), any(), anyString(), anyList(), any()); |
|
|
|
verify(telemetryService, times(1)).deleteAndNotify(any(), any(), anyString(), anyList(), eq(notifyDevice || notifyDeviceMetadata), any()); |
|
|
|
} |
|
|
|
} |