|
19 | 19 |
|
20 | 20 | package org.elasticsearch.client.dataframe.transforms.pivot; |
21 | 21 |
|
| 22 | +import org.elasticsearch.common.bytes.BytesArray; |
| 23 | +import org.elasticsearch.common.xcontent.DeprecationHandler; |
| 24 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
22 | 25 | import org.elasticsearch.common.xcontent.XContentParser; |
| 26 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
23 | 27 | import org.elasticsearch.test.AbstractXContentTestCase; |
24 | 28 |
|
25 | 29 | import java.io.IOException; |
|
28 | 32 | import java.util.Map; |
29 | 33 | import java.util.Set; |
30 | 34 |
|
| 35 | +import static org.hamcrest.Matchers.instanceOf; |
| 36 | + |
31 | 37 | public class GroupConfigTests extends AbstractXContentTestCase<GroupConfig> { |
32 | 38 |
|
33 | 39 | public static GroupConfig randomGroupConfig() { |
34 | 40 | Map<String, SingleGroupSource> groups = new LinkedHashMap<>(); |
35 | 41 |
|
36 | 42 | // ensure that the unlikely does not happen: 2 group_by's share the same name |
37 | 43 | Set<String> names = new HashSet<>(); |
38 | | - for (int i = 0; i < randomIntBetween(1, 1); ++i) { |
| 44 | + for (int i = 0; i < randomIntBetween(1, 4); ++i) { |
39 | 45 | String targetFieldName = randomAlphaOfLengthBetween(1, 20); |
40 | 46 | if (names.add(targetFieldName)) { |
41 | 47 | SingleGroupSource groupBy; |
@@ -72,4 +78,33 @@ protected GroupConfig doParseInstance(XContentParser parser) throws IOException |
72 | 78 | protected boolean supportsUnknownFields() { |
73 | 79 | return false; |
74 | 80 | } |
| 81 | + |
| 82 | + public void testLenientParsing() throws IOException { |
| 83 | + BytesArray json = new BytesArray( |
| 84 | + "{ " + |
| 85 | + "\"unknown-field\":\"foo\", " + |
| 86 | + "\"destination-field\": {" + |
| 87 | + "\"terms\": {" + |
| 88 | + "\"field\": \"term-field\"" + |
| 89 | + "}" + |
| 90 | + "}," + |
| 91 | + "\"unknown-field-2\":\"bar\"," + |
| 92 | + "\"destination-field2\": {" + |
| 93 | + "\"terms\": {" + |
| 94 | + "\"field\": \"term-field2\"" + |
| 95 | + "}" + |
| 96 | + "}," + |
| 97 | + "\"array-field\" : [1.0, 2.0]" + |
| 98 | + "}"); |
| 99 | + XContentParser parser = JsonXContent.jsonXContent |
| 100 | + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json.streamInput()); |
| 101 | + |
| 102 | + GroupConfig gc = GroupConfig.fromXContent(parser); |
| 103 | + |
| 104 | + assertEquals(gc.getGroups().size(), 2); |
| 105 | + assertTrue(gc.getGroups().containsKey("destination-field")); |
| 106 | + SingleGroupSource groupSource = gc.getGroups().get("destination-field"); |
| 107 | + assertThat(groupSource, instanceOf(TermsGroupSource.class)); |
| 108 | + assertEquals(groupSource.getField(), "term-field"); |
| 109 | + } |
75 | 110 | } |
0 commit comments