Commit 4be76280 authored by Zhibin Mai's avatar Zhibin Mai
Browse files

Support multi-kinds separated by comma

parent 81f4e514
Pipeline #105311 passed with stages
in 8 minutes and 5 seconds
......@@ -16,7 +16,7 @@ package org.opengroup.osdu.core.common.util;
import org.opengroup.osdu.core.common.SwaggerDoc;
import java.util.LinkedList;
import java.util.Arrays;
import java.util.List;
public class KindParser {
......@@ -25,9 +25,10 @@ public class KindParser {
throw new IllegalArgumentException(SwaggerDoc.KIND_VALIDATION_CAN_NOT_BE_NULL_OR_EMPTY);
}
List<String> kinds = new LinkedList<>();
List<String> kinds;
if (kind instanceof String) {
kinds.add((String) kind);
// To remove extra spaces before and/or after the delimiter
kinds = Arrays.asList(((String)kind).trim().split("\\s*,\\s*"));
} else if (kind instanceof List<?>) {
kinds = (List<String>) kind;
// The above case is subtle. It won't throw exception even if kind is a list of integers or mixed types.
......
......@@ -70,6 +70,21 @@ public class MultiKindValidatorTest {
assertTrue(this.sut.isValid(kinds, this.context));
}
@Test
public void validMultiKindsSeparatedByComma() {
assertTrue(this.sut.isValid("A1:S1:E1:1.0.0,A2:S2:E2:2.0.0,A3:S3:E3:2.0.0", this.context));
}
@Test
public void validMultiKindsSeparatedByCommaAndWhitespace() {
assertTrue(this.sut.isValid(" A1:S1:E1:1.0.0 , A2:S2:E2:2.0.0, A3:S3:E3:2.0.0 ", this.context));
}
@Test
public void invalidMultiKindsSeparatedByComma() {
assertFalse(this.sut.isValid("A1:S1:E1:1.0.0, ,A2:S2:E2:2.0.0,A3:S3:E3:2.0.0", this.context));
}
@Test
public void simpleKindWithInvalidFormat() {
assertFalse(this.sut.isValid("*:*:*:", this.context));
......
......@@ -75,4 +75,26 @@ public class KindParserTest {
assertNotNull(query.kind);
KindParser.parse(query.kind);
}
@Test
public void validTypeOfMultiKindsSeparatedWithComma() {
Gson gson = new Gson();
String jsonText = "{ \"kind\": \"a:s:e:1.0.0,a:s:e:2.0.0\" }";
FakeQuery query = gson.fromJson(jsonText, FakeQuery.class);
List<String> kinds = KindParser.parse(query.kind);
assertEquals(2, kinds.size());
assertEquals("a:s:e:1.0.0", kinds.get(0));
assertEquals("a:s:e:2.0.0", kinds.get(1));
}
@Test
public void validTypeOfMultiKindsSeparatedWithCommaAndWhitespace() {
Gson gson = new Gson();
String jsonText = "{ \"kind\": \" a:s:e:1.0.0 , a:s:e:2.0.0 \" }";
FakeQuery query = gson.fromJson(jsonText, FakeQuery.class);
List<String> kinds = KindParser.parse(query.kind);
assertEquals(2, kinds.size());
assertEquals("a:s:e:1.0.0", kinds.get(0));
assertEquals("a:s:e:2.0.0", kinds.get(1));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment