From 1749a35f048dfefe6b17e4160927faf24feff599 Mon Sep 17 00:00:00 2001 From: dashevchenko Date: Tue, 13 Feb 2024 17:14:36 +0200 Subject: [PATCH] handling predicate value as coma separated string --- .../server/common/data/StringUtils.java | 25 +++++++++++++++++ .../dao/sql/query/EntityKeyMapping.java | 27 +++---------------- .../dao/sql/query/EntityKeyMappingTest.java | 26 +++++++++--------- .../rule/engine/profile/AlarmRuleState.java | 8 +++--- .../engine/profile/AlarmRuleStateTest.java | 14 +++------- 5 files changed, 51 insertions(+), 49 deletions(-) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java b/common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java index 3bd8cbcd20..5e7566ea5f 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java @@ -19,7 +19,9 @@ import com.google.common.base.Splitter; import org.apache.commons.lang3.RandomStringUtils; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.Base64; +import java.util.List; import java.util.function.Function; import static org.apache.commons.lang3.StringUtils.repeat; @@ -245,4 +247,27 @@ public class StringUtils { return string.substring(0, maxLength) + truncationMarkerFunc.apply(truncatedSymbols); } + public static List getListValuesWithoutQuote(String value) { + List splitValues = List.of(value.trim().split("\\s*,\\s*")); + List result = new ArrayList<>(); + char lastWayInputValue = '#'; + for (String str : splitValues) { + char startWith = str.charAt(0); + char endWith = str.charAt(str.length() - 1); + + // if first value is not quote, so we return values after split + if (startWith != '\'' && startWith != '"') return splitValues; + + // if value is not in quote, so we return values after split + if (startWith != endWith) return splitValues; + + // if different way values, so don't replace quote and return values after split + if (lastWayInputValue != '#' && startWith != lastWayInputValue) return splitValues; + + result.add(str.substring(1, str.length() - 1)); + lastWayInputValue = startWith; + } + return result; + } + } diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityKeyMapping.java b/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityKeyMapping.java index f5874cbd08..595f8ae3e8 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityKeyMapping.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityKeyMapping.java @@ -47,6 +47,8 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.thingsboard.server.common.data.StringUtils.getListValuesWithoutQuote; + @Data public class EntityKeyMapping { @@ -582,30 +584,7 @@ public class EntityKeyMapping { return String.format("((%s is not null and %s)", field, stringOperationQuery); } - protected List getListValuesWithoutQuote(String value) { - List splitValues = List.of(value.trim().split("\\s*,\\s*")); - List result = new ArrayList<>(); - char lastWayInputValue = '#'; - for (String str : splitValues) { - char startWith = str.charAt(0); - char endWith = str.charAt(str.length() - 1); - - // if first value is not quote, so we return values after split - if (startWith != '\'' && startWith != '"') return splitValues; - - // if value is not in quote, so we return values after split - if (startWith != endWith) return splitValues; - - // if different way values, so don't replace quote and return values after split - if (lastWayInputValue != '#' && startWith != lastWayInputValue) return splitValues; - - result.add(str.substring(1, str.length() - 1)); - lastWayInputValue = startWith; - } - return result; - } - - private String buildNumericPredicateQuery(QueryContext ctx, String field, NumericFilterPredicate numericFilterPredicate) { + private String buildNumericPredicateQuery(QueryContext ctx, String field, NumericFilterPredicate numericFilterPredicate) { String paramName = getNextParameterName(field); ctx.addDoubleParameter(paramName, numericFilterPredicate.getValue().getValue()); String numericOperationQuery = ""; diff --git a/dao/src/test/java/org/thingsboard/server/dao/sql/query/EntityKeyMappingTest.java b/dao/src/test/java/org/thingsboard/server/dao/sql/query/EntityKeyMappingTest.java index b9baffef1c..7d6d23f248 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/sql/query/EntityKeyMappingTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/sql/query/EntityKeyMappingTest.java @@ -24,6 +24,8 @@ import org.springframework.test.context.junit4.SpringRunner; import java.util.List; +import static org.thingsboard.server.common.data.StringUtils.getListValuesWithoutQuote; + @RunWith(SpringRunner.class ) @SpringBootTest(classes = EntityKeyMapping.class) public class EntityKeyMappingTest { @@ -36,39 +38,39 @@ public class EntityKeyMappingTest { @Test public void testSplitToList() { String value = "device1, device2, device3"; - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); } @Test public void testReplaceSingleQuote() { String value = "'device1', 'device2', 'device3'"; - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); } @Test public void testReplaceDoubleQuote() { String value = "\"device1\", \"device2\", \"device3\""; - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); } @Test public void testSplitWithoutSpace() { String value = "\"device1\" , \"device2\" , \"device3\""; - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); } @Test public void testSaveSpacesBetweenString() { String value = "device 1 , device 2 , device 3"; List result = List.of("device 1", "device 2", "device 3"); - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); } @Test public void testSaveQuoteInString() { String value = "device ''1 , device \"\"2 , device \"'3"; List result = List.of("device ''1", "device \"\"2", "device \"'3"); - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); } @Test @@ -76,28 +78,28 @@ public class EntityKeyMappingTest { String value = "\"device1\", 'device2', \"device3\""; List result = List.of("\"device1\"", "'device2'", "\"device3\""); - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); value = "'device1', \"device2\", \"device3\""; result = List.of("'device1'", "\"device2\"", "\"device3\""); - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); value = "device1, 'device2', \"device3\""; result = List.of("device1", "'device2'", "\"device3\""); - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); value = "'device1', device2, \"device3\""; result = List.of("'device1'", "device2", "\"device3\""); - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); value = "device1, \"device2\", \"device3\""; result = List.of("device1", "\"device2\"", "\"device3\""); - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); value = "\"device1\", device2, \"device3\""; result = List.of("\"device1\"", "device2", "\"device3\""); - Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result); + Assert.assertEquals(getListValuesWithoutQuote(value), result); } } \ No newline at end of file diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/AlarmRuleState.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/AlarmRuleState.java index da63c72750..028afa25ce 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/AlarmRuleState.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/AlarmRuleState.java @@ -46,11 +46,13 @@ import org.thingsboard.server.common.adaptor.JsonConverter; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.function.Function; +import static org.thingsboard.server.common.data.StringUtils.equalsAny; +import static org.thingsboard.server.common.data.StringUtils.getListValuesWithoutQuote; + @Data @Slf4j class AlarmRuleState { @@ -469,9 +471,9 @@ class AlarmRuleState { case NOT_CONTAINS: return !val.contains(predicateValue); case IN: - return predicateValue.contains(val); + return equalsAny(val, getListValuesWithoutQuote(predicateValue).toArray(new String[0])); case NOT_IN: - return !predicateValue.contains(val); + return !equalsAny(val, getListValuesWithoutQuote(predicateValue).toArray(new String[0])); default: throw new RuntimeException("Operation not supported: " + predicate.getOperation()); } diff --git a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/profile/AlarmRuleStateTest.java b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/profile/AlarmRuleStateTest.java index a46517b682..e4b2856dc3 100644 --- a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/profile/AlarmRuleStateTest.java +++ b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/profile/AlarmRuleStateTest.java @@ -15,14 +15,10 @@ */ package org.thingsboard.rule.engine.profile; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.thingsboard.rule.engine.math.TbRuleNodeMathFunctionType; import org.thingsboard.server.common.data.device.profile.AlarmCondition; import org.thingsboard.server.common.data.device.profile.AlarmConditionFilter; import org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey; @@ -39,16 +35,14 @@ import java.util.List; import java.util.Set; import java.util.stream.Stream; -import static org.junit.Assert.assertEquals; - public class AlarmRuleStateTest { private static Stream testEvalCondition() { return Stream.of( - Arguments.of(StringFilterPredicate.StringOperation.IN, "test value", "test", AlarmEvalResult.TRUE), - Arguments.of(StringFilterPredicate.StringOperation.IN, "test value", "teeeeest", AlarmEvalResult.FALSE), - Arguments.of(StringFilterPredicate.StringOperation.NOT_IN, "test value", "test", AlarmEvalResult.FALSE), - Arguments.of(StringFilterPredicate.StringOperation.NOT_IN, "test value", "teeeeest", AlarmEvalResult.TRUE), + Arguments.of(StringFilterPredicate.StringOperation.IN, "test,value", "test", AlarmEvalResult.TRUE), + Arguments.of(StringFilterPredicate.StringOperation.IN, "test,value", "teeeeest", AlarmEvalResult.FALSE), + Arguments.of(StringFilterPredicate.StringOperation.NOT_IN, "test,value", "test", AlarmEvalResult.FALSE), + Arguments.of(StringFilterPredicate.StringOperation.NOT_IN, "test,value", "teeeeest", AlarmEvalResult.TRUE), Arguments.of(StringFilterPredicate.StringOperation.CONTAINS, "test value", "test value", AlarmEvalResult.TRUE), Arguments.of(StringFilterPredicate.StringOperation.CONTAINS, "test value", "test", AlarmEvalResult.FALSE), Arguments.of(StringFilterPredicate.StringOperation.NOT_CONTAINS, "test value", "test", AlarmEvalResult.TRUE),