Browse Source

handling predicate value as coma separated string

pull/10175/head
dashevchenko 2 years ago
parent
commit
1749a35f04
  1. 25
      common/data/src/main/java/org/thingsboard/server/common/data/StringUtils.java
  2. 27
      dao/src/main/java/org/thingsboard/server/dao/sql/query/EntityKeyMapping.java
  3. 26
      dao/src/test/java/org/thingsboard/server/dao/sql/query/EntityKeyMappingTest.java
  4. 8
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/AlarmRuleState.java
  5. 14
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/profile/AlarmRuleStateTest.java

25
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<String> getListValuesWithoutQuote(String value) {
List<String> splitValues = List.of(value.trim().split("\\s*,\\s*"));
List<String> 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;
}
}

27
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<String> getListValuesWithoutQuote(String value) {
List<String> splitValues = List.of(value.trim().split("\\s*,\\s*"));
List<String> 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 = "";

26
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<String> 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<String> 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<String> 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);
}
}

8
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());
}

14
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<Arguments> 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),

Loading…
Cancel
Save