|
|
@ -16,6 +16,7 @@ |
|
|
package org.thingsboard.rule.engine.api.util; |
|
|
package org.thingsboard.rule.engine.api.util; |
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
|
|
import org.hamcrest.CoreMatchers; |
|
|
import org.junit.Assert; |
|
|
import org.junit.Assert; |
|
|
import org.junit.Test; |
|
|
import org.junit.Test; |
|
|
import org.junit.runner.RunWith; |
|
|
import org.junit.runner.RunWith; |
|
|
@ -25,9 +26,15 @@ import org.thingsboard.server.common.msg.TbMsg; |
|
|
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|
|
import org.thingsboard.server.common.msg.TbMsgMetaData; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.is; |
|
|
|
|
|
import static org.hamcrest.MatcherAssert.assertThat; |
|
|
|
|
|
|
|
|
@RunWith(MockitoJUnitRunner.class) |
|
|
@RunWith(MockitoJUnitRunner.class) |
|
|
public class TbNodeUtilsTest { |
|
|
public class TbNodeUtilsTest { |
|
|
|
|
|
|
|
|
|
|
|
private static final String DATA_VARIABLE_TEMPLATE = "$[%s]"; |
|
|
|
|
|
private static final String METADATA_VARIABLE_TEMPLATE = "${%s}"; |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void testSimpleReplacement() { |
|
|
public void testSimpleReplacement() { |
|
|
String pattern = "ABC ${metadata_key} $[data_key]"; |
|
|
String pattern = "ABC ${metadata_key} $[data_key]"; |
|
|
@ -112,4 +119,27 @@ public class TbNodeUtilsTest { |
|
|
Assert.assertEquals("ABC metadata_value $[key1.key2[0].key3]", result); |
|
|
Assert.assertEquals("ABC metadata_value $[key1.key2[0].key3]", result); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
public void givenKey_whenFormatDataVarTemplate_thenReturnTheSameStringAsFormat() { |
|
|
|
|
|
assertThat(TbNodeUtils.formatDataVarTemplate("key"), is("$[key]")); |
|
|
|
|
|
assertThat(TbNodeUtils.formatDataVarTemplate("key"), is(String.format(DATA_VARIABLE_TEMPLATE, "key"))); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TbNodeUtils.formatDataVarTemplate(""), is("$[]")); |
|
|
|
|
|
assertThat(TbNodeUtils.formatDataVarTemplate(""), is(String.format(DATA_VARIABLE_TEMPLATE, ""))); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TbNodeUtils.formatDataVarTemplate(null), is("$[null]")); |
|
|
|
|
|
assertThat(TbNodeUtils.formatDataVarTemplate(null), is(String.format(DATA_VARIABLE_TEMPLATE, (String) null))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
public void givenKey_whenFormatMetadataVarTemplate_thenReturnTheSameStringAsFormat() { |
|
|
|
|
|
assertThat(TbNodeUtils.formatMetadataVarTemplate("key"), is("${key}")); |
|
|
|
|
|
assertThat(TbNodeUtils.formatMetadataVarTemplate("key"), is(String.format(METADATA_VARIABLE_TEMPLATE, "key"))); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TbNodeUtils.formatMetadataVarTemplate(""), is("${}")); |
|
|
|
|
|
assertThat(TbNodeUtils.formatMetadataVarTemplate(""), is(String.format(METADATA_VARIABLE_TEMPLATE, ""))); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(TbNodeUtils.formatMetadataVarTemplate(null), is("${null}")); |
|
|
|
|
|
assertThat(TbNodeUtils.formatMetadataVarTemplate(null), is(String.format(METADATA_VARIABLE_TEMPLATE, (String) null))); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|