removed quote and double quote when all values have the same way. Also, fix the delete quote, when the value has a quote. Also, added a test to check this method.

This commit is contained in:
van-vanich 2021-11-05 17:51:05 +02:00
parent c197f27d8e
commit 4bdc93f33e
2 changed files with 109 additions and 2 deletions

View File

@ -578,8 +578,27 @@ public class EntityKeyMapping {
return String.format("((%s is not null and %s)", field, stringOperationQuery);
}
private List<String> getListValuesWithoutQuote(String value) {
return List.of(value.replaceAll("'", "").replaceAll("\"", "").trim().split("\\s*,\\s*"));
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) {

View File

@ -0,0 +1,88 @@
package org.thingsboard.server.dao.sql.query;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class )
@SpringBootTest(classes = EntityKeyMapping.class)
public class EntityKeyMappingTest {
@Autowired
private EntityKeyMapping entityKeyMapping;
private static final List<String> result = List.of("device1", "device2", "device3");
@Test
public void testSplitToList() {
String value = "device1, device2, device3";
Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result);
}
@Test
public void testReplaceSingleQuote() {
String value = "'device1', 'device2', 'device3'";
Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result);
}
@Test
public void testReplaceDoubleQuote() {
String value = "\"device1\", \"device2\", \"device3\"";
Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result);
}
@Test
public void testSplitWithoutSpace() {
String value = "\"device1\" , \"device2\" , \"device3\"";
Assert.assertEquals(entityKeyMapping.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);
}
@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);
}
@Test
public void testNotDeleteQuoteWhenDifferentStyle() {
String value = "\"device1\", 'device2', \"device3\"";
List<String> result = List.of("\"device1\"", "'device2'", "\"device3\"");
Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result);
value = "'device1', \"device2\", \"device3\"";
result = List.of("'device1'", "\"device2\"", "\"device3\"");
Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result);
value = "device1, 'device2', \"device3\"";
result = List.of("device1", "'device2'", "\"device3\"");
Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result);
value = "'device1', device2, \"device3\"";
result = List.of("'device1'", "device2", "\"device3\"");
Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result);
value = "device1, \"device2\", \"device3\"";
result = List.of("device1", "\"device2\"", "\"device3\"");
Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result);
value = "\"device1\", device2, \"device3\"";
result = List.of("\"device1\"", "device2", "\"device3\"");
Assert.assertEquals(entityKeyMapping.getListValuesWithoutQuote(value), result);
}
}