|
|
@ -44,12 +44,12 @@ import static org.mockito.Mockito.spy; |
|
|
import static org.mockito.Mockito.times; |
|
|
import static org.mockito.Mockito.times; |
|
|
import static org.mockito.Mockito.verify; |
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
|
public class TbCopyFromMdToMsgNodeTest { |
|
|
public class TbCopyKeysNodeTest { |
|
|
final ObjectMapper mapper = new ObjectMapper(); |
|
|
final ObjectMapper mapper = new ObjectMapper(); |
|
|
|
|
|
|
|
|
DeviceId deviceId; |
|
|
DeviceId deviceId; |
|
|
TbCopyFromMdToMsgNode node; |
|
|
TbCopyKeysNode node; |
|
|
TbCopyFromMdToMsgNodeConfiguration config; |
|
|
TbCopyKeysNodeConfiguration config; |
|
|
TbNodeConfiguration nodeConfiguration; |
|
|
TbNodeConfiguration nodeConfiguration; |
|
|
TbContext ctx; |
|
|
TbContext ctx; |
|
|
TbMsgCallback callback; |
|
|
TbMsgCallback callback; |
|
|
@ -59,10 +59,11 @@ public class TbCopyFromMdToMsgNodeTest { |
|
|
deviceId = new DeviceId(UUID.randomUUID()); |
|
|
deviceId = new DeviceId(UUID.randomUUID()); |
|
|
callback = mock(TbMsgCallback.class); |
|
|
callback = mock(TbMsgCallback.class); |
|
|
ctx = mock(TbContext.class); |
|
|
ctx = mock(TbContext.class); |
|
|
config = new TbCopyFromMdToMsgNodeConfiguration().defaultConfiguration(); |
|
|
config = new TbCopyKeysNodeConfiguration().defaultConfiguration(); |
|
|
config.setMetadataMsgKeys(List.of("TestKey_1", "TestKey_2", "TestKey_3")); |
|
|
config.setKeys(List.of("TestKey_1", "TestKey_2", "TestKey_3", "(\\w*)Data(\\w*)")); |
|
|
|
|
|
config.setFromMetadata(true); |
|
|
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config)); |
|
|
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config)); |
|
|
node = spy(new TbCopyFromMdToMsgNode()); |
|
|
node = spy(new TbCopyKeysNode()); |
|
|
node.init(ctx, nodeConfiguration); |
|
|
node.init(ctx, nodeConfiguration); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -78,12 +79,13 @@ public class TbCopyFromMdToMsgNodeTest { |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void givenDefaultConfig_whenVerify_thenOK() { |
|
|
void givenDefaultConfig_whenVerify_thenOK() { |
|
|
TbCopyFromMdToMsgNodeConfiguration defaultConfig = new TbCopyFromMdToMsgNodeConfiguration().defaultConfiguration(); |
|
|
TbCopyKeysNodeConfiguration defaultConfig = new TbCopyKeysNodeConfiguration().defaultConfiguration(); |
|
|
assertThat(defaultConfig.getMetadataMsgKeys()).isEqualTo(Collections.emptyList()); |
|
|
assertThat(defaultConfig.getKeys()).isEqualTo(Collections.emptyList()); |
|
|
|
|
|
assertThat(defaultConfig.isFromMetadata()).isEqualTo(false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void givenMsg_whenOnMsg_thenVerifyOutput() throws Exception { |
|
|
void givenMsgFromMetadata_whenOnMsg_thenVerifyOutput() throws Exception { |
|
|
String data = "{}"; |
|
|
String data = "{}"; |
|
|
node.onMsg(ctx, getTbMsg(deviceId, data)); |
|
|
node.onMsg(ctx, getTbMsg(deviceId, data)); |
|
|
|
|
|
|
|
|
@ -96,11 +98,33 @@ public class TbCopyFromMdToMsgNodeTest { |
|
|
|
|
|
|
|
|
JsonNode dataNode = JacksonUtil.toJsonNode(newMsg.getData()); |
|
|
JsonNode dataNode = JacksonUtil.toJsonNode(newMsg.getData()); |
|
|
assertThat(dataNode.has("TestKey_1")).isEqualTo(true); |
|
|
assertThat(dataNode.has("TestKey_1")).isEqualTo(true); |
|
|
|
|
|
assertThat(dataNode.has("voltageDataValue")).isEqualTo(true); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void givenMsgFromMsg_whenOnMsg_thenVerifyOutput() throws Exception { |
|
|
|
|
|
config.setFromMetadata(false); |
|
|
|
|
|
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config)); |
|
|
|
|
|
node.init(ctx, nodeConfiguration); |
|
|
|
|
|
|
|
|
|
|
|
String data = "{\"DigitData\":22.5,\"TempDataValue\":10.5}"; |
|
|
|
|
|
node.onMsg(ctx, getTbMsg(deviceId, data)); |
|
|
|
|
|
|
|
|
|
|
|
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); |
|
|
|
|
|
verify(ctx, times(1)).tellSuccess(newMsgCaptor.capture()); |
|
|
|
|
|
verify(ctx, never()).tellFailure(any(), any()); |
|
|
|
|
|
|
|
|
|
|
|
TbMsg newMsg = newMsgCaptor.getValue(); |
|
|
|
|
|
assertThat(newMsg).isNotNull(); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> metaDataMap = newMsg.getMetaData().getData(); |
|
|
|
|
|
assertThat(metaDataMap.containsKey("DigitData")).isEqualTo(true); |
|
|
|
|
|
assertThat(metaDataMap.containsKey("TempDataValue")).isEqualTo(true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void givenEmptyKeys_whenOnMsg_thenVerifyOutput() throws Exception { |
|
|
void givenEmptyKeys_whenOnMsg_thenVerifyOutput() throws Exception { |
|
|
TbCopyFromMdToMsgNodeConfiguration defaultConfig = new TbCopyFromMdToMsgNodeConfiguration().defaultConfiguration(); |
|
|
TbCopyKeysNodeConfiguration defaultConfig = new TbCopyKeysNodeConfiguration().defaultConfiguration(); |
|
|
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(defaultConfig)); |
|
|
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(defaultConfig)); |
|
|
node.init(ctx, nodeConfiguration); |
|
|
node.init(ctx, nodeConfiguration); |
|
|
|
|
|
|
|
|
@ -134,6 +158,7 @@ public class TbCopyFromMdToMsgNodeTest { |
|
|
final Map<String, String> mdMap = Map.of( |
|
|
final Map<String, String> mdMap = Map.of( |
|
|
"TestKey_1", "Test", |
|
|
"TestKey_1", "Test", |
|
|
"country", "US", |
|
|
"country", "US", |
|
|
|
|
|
"voltageDataValue", "220", |
|
|
"city", "NY" |
|
|
"city", "NY" |
|
|
); |
|
|
); |
|
|
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback); |
|
|
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback); |