Fix serde for FragmentSearchQuerySpec and add some tests.

This commit is contained in:
Gian Merlino 2015-11-30 17:34:35 -08:00
parent bad96537a9
commit cd2cff24ff
2 changed files with 71 additions and 9 deletions

View File

@ -38,9 +38,8 @@ public class FragmentSearchQuerySpec implements SearchQuerySpec
private final String[] target;
@JsonCreator
public FragmentSearchQuerySpec(
@JsonProperty("values") List<String> values
List<String> values
)
{
this(values, false);
@ -54,7 +53,7 @@ public class FragmentSearchQuerySpec implements SearchQuerySpec
{
this.values = values;
this.caseSensitive = caseSensitive;
Set<String> set = new TreeSet();
Set<String> set = new TreeSet<>();
if (values != null) {
for (String value : values) {
set.add(value);

View File

@ -17,7 +17,12 @@
package io.druid.query.extraction;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.druid.jackson.DefaultObjectMapper;
import io.druid.query.dimension.ExtractionDimensionSpec;
import io.druid.query.search.search.FragmentSearchQuerySpec;
import io.druid.query.search.search.SearchQuerySpec;
import org.junit.Assert;
@ -46,11 +51,11 @@ public class SearchQuerySpecDimExtractionFnTest
public void testExtraction()
{
SearchQuerySpec spec = new FragmentSearchQuerySpec(
Arrays.asList("to", "yo")
Arrays.asList("tO", "yO")
);
ExtractionFn extractionFn = new SearchQuerySpecDimExtractionFn(spec);
List<String> expected = Arrays.asList("Kyoto", "Tokyo", "Toyokawa", "Yorktown");
Set<String> extracted = Sets.newHashSet();
List<String> expected = ImmutableList.of("Kyoto", "Tokyo", "Toyokawa", "Yorktown");
List<String> extracted = Lists.newArrayList();
for (String str : testStrings) {
String res = extractionFn.apply(str);
@ -59,10 +64,68 @@ public class SearchQuerySpecDimExtractionFnTest
}
}
Assert.assertEquals(4, extracted.size());
for (String str : extracted) {
Assert.assertTrue(expected.contains(str));
Assert.assertEquals(expected, extracted);
}
@Test
public void testCaseSensitiveExtraction()
{
SearchQuerySpec spec = new FragmentSearchQuerySpec(
Arrays.asList("to", "yo"),
true
);
ExtractionFn extractionFn = new SearchQuerySpecDimExtractionFn(spec);
List<String> expected = ImmutableList.of("Kyoto");
List<String> extracted = Lists.newArrayList();
for (String str : testStrings) {
String res = extractionFn.apply(str);
if (res != null) {
extracted.add(res);
}
}
Assert.assertEquals(expected, extracted);
}
@Test
public void testCaseSensitiveExtraction2()
{
SearchQuerySpec spec = new FragmentSearchQuerySpec(
Arrays.asList("To", "yo"),
true
);
ExtractionFn extractionFn = new SearchQuerySpecDimExtractionFn(spec);
List<String> expected = ImmutableList.of("Tokyo", "Toyokawa");
List<String> extracted = Lists.newArrayList();
for (String str : testStrings) {
String res = extractionFn.apply(str);
if (res != null) {
extracted.add(res);
}
}
Assert.assertEquals(expected, extracted);
}
@Test
public void testSerde() throws Exception
{
ObjectMapper objectMapper = new DefaultObjectMapper();
SearchQuerySpec spec = new FragmentSearchQuerySpec(
Arrays.asList("to", "yo"),
true
);
ExtractionFn extractionFn = new SearchQuerySpecDimExtractionFn(spec);
ExtractionFn extractionFn2 = objectMapper.readValue(
objectMapper.writeValueAsBytes(extractionFn),
ExtractionFn.class
);
FragmentSearchQuerySpec spec2 = (FragmentSearchQuerySpec) ((SearchQuerySpecDimExtractionFn) extractionFn2).getSearchQuerySpec();
Assert.assertEquals(extractionFn, extractionFn2);
Assert.assertEquals(true, spec2.isCaseSensitive());
Assert.assertEquals(ImmutableList.of("to", "yo"), spec2.getValues());
}
}