|
6 | 6 |
|
7 | 7 | package org.elasticsearch.xpack.core.dataframe.transforms.pivot;
|
8 | 8 |
|
| 9 | +import org.elasticsearch.common.ParsingException; |
9 | 10 | import org.elasticsearch.common.bytes.BytesReference;
|
10 | 11 | import org.elasticsearch.common.io.stream.Writeable.Reader;
|
11 | 12 | import org.elasticsearch.common.xcontent.ToXContent;
|
|
27 | 28 |
|
28 | 29 | public class GroupConfigTests extends AbstractSerializingTestCase<GroupConfig> {
|
29 | 30 |
|
| 31 | + // array of illegal characters, see {@link AggregatorFactories#VALID_AGG_NAME} |
| 32 | + private static final char[] ILLEGAL_FIELD_NAME_CHARACTERS = {'[', ']', '>'}; |
| 33 | + |
30 | 34 | public static GroupConfig randomGroupConfig() {
|
31 | 35 | Map<String, Object> source = new LinkedHashMap<>();
|
32 | 36 | Map<String, SingleGroupSource> groups = new LinkedHashMap<>();
|
@@ -88,6 +92,34 @@ public void testEmptyGroupBy() throws IOException {
|
88 | 92 | }
|
89 | 93 | }
|
90 | 94 |
|
| 95 | + public void testInvalidGroupByNames() throws IOException { |
| 96 | + |
| 97 | + String invalidName = randomAlphaOfLengthBetween(0, 5) |
| 98 | + + ILLEGAL_FIELD_NAME_CHARACTERS[randomIntBetween(0, ILLEGAL_FIELD_NAME_CHARACTERS.length - 1)] |
| 99 | + + randomAlphaOfLengthBetween(0, 5); |
| 100 | + |
| 101 | + XContentBuilder source = JsonXContent.contentBuilder() |
| 102 | + .startObject() |
| 103 | + .startObject(invalidName) |
| 104 | + .startObject("terms") |
| 105 | + .field("field", "user") |
| 106 | + .endObject() |
| 107 | + .endObject() |
| 108 | + .endObject(); |
| 109 | + |
| 110 | + // lenient, passes but reports invalid |
| 111 | + try (XContentParser parser = createParser(source)) { |
| 112 | + GroupConfig groupConfig = GroupConfig.fromXContent(parser, true); |
| 113 | + assertFalse(groupConfig.isValid()); |
| 114 | + } |
| 115 | + |
| 116 | + // strict throws |
| 117 | + try (XContentParser parser = createParser(source)) { |
| 118 | + Exception e = expectThrows(ParsingException.class, () -> GroupConfig.fromXContent(parser, false)); |
| 119 | + assertTrue(e.getMessage().startsWith("Invalid group name")); |
| 120 | + } |
| 121 | + } |
| 122 | + |
91 | 123 | private static Map<String, Object> getSource(SingleGroupSource groupSource) {
|
92 | 124 | try (XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()) {
|
93 | 125 | XContentBuilder content = groupSource.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
|
|
0 commit comments