public void testMissingType() throws Exception { String source = JsonXContent.contentBuilder() .startObject() .startObject("by_date") .startObject("date_histogram") .field("field", "timestamp") .field("interval", "month") .endObject() .startObject("aggs") .startObject("tag_count") // the aggregation type is missing // .startObject("cardinality") .field("field", "tag") // .endObject() .endObject() .endObject() .endObject() .endObject() .string(); try { XContentParser parser = XContentFactory.xContent(source).createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); fail(); } catch (ParsingException e) { // All Good } }
public void testSameAggregationName() throws Exception { final String name = randomAsciiOfLengthBetween(1, 10); String source = JsonXContent.contentBuilder() .startObject() .startObject(name) .startObject("terms") .field("field", "a") .endObject() .endObject() .startObject(name) .startObject("terms") .field("field", "b") .endObject() .endObject() .endObject() .string(); try { XContentParser parser = XContentFactory.xContent(source).createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); fail(); } catch (IllegalArgumentException e) { assertThat( e.toString(), containsString("Two sibling aggregations cannot have the same name: [" + name + "]")); } }
public void testTwoTypes() throws Exception { String source = JsonXContent.contentBuilder() .startObject() .startObject("in_stock") .startObject("filter") .startObject("range") .startObject("stock") .field("gt", 0) .endObject() .endObject() .endObject() .startObject("terms") .field("field", "stock") .endObject() .endObject() .endObject() .string(); try { XContentParser parser = XContentFactory.xContent(source).createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); fail(); } catch (ParsingException e) { assertThat( e.toString(), containsString( "Found two aggregation type definitions in [in_stock]: [filter] and [terms]")); } }
public void testInvalidAggregationName() throws Exception { Matcher matcher = Pattern.compile("[^\\[\\]>]+").matcher(""); String name; Random rand = random(); int len = randomIntBetween(1, 5); char[] word = new char[len]; while (true) { for (int i = 0; i < word.length; i++) { word[i] = (char) rand.nextInt(127); } name = String.valueOf(word); if (!matcher.reset(name).matches()) { break; } } String source = JsonXContent.contentBuilder() .startObject() .startObject(name) .startObject("filter") .startObject("range") .startObject("stock") .field("gt", 0) .endObject() .endObject() .endObject() .endObject() .endObject() .string(); try { XContentParser parser = XContentFactory.xContent(source).createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); fail(); } catch (ParsingException e) { assertThat(e.toString(), containsString("Invalid aggregation name [" + name + "]")); } }
public void testTwoAggs() throws Exception { String source = JsonXContent.contentBuilder() .startObject() .startObject("by_date") .startObject("date_histogram") .field("field", "timestamp") .field("interval", "month") .endObject() .startObject("aggs") .startObject("tag_count") .startObject("cardinality") .field("field", "tag") .endObject() .endObject() .endObject() .startObject("aggs") // 2nd "aggs": illegal .startObject("tag_count2") .startObject("cardinality") .field("field", "tag") .endObject() .endObject() .endObject() .endObject() .endObject() .string(); try { XContentParser parser = XContentFactory.xContent(source).createParser(source); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); aggParsers.parseAggregators(parseContext); fail(); } catch (ParsingException e) { assertThat( e.toString(), containsString("Found two sub aggregation definitions under [by_date]")); } }
/** * Generic test that creates new AggregatorFactory from the test AggregatorFactory and checks both * for equality and asserts equality on the two queries. */ public void testFromXContent() throws IOException { AF testAgg = createTestAggregatorFactory(); AggregatorFactories.Builder factoriesBuilder = AggregatorFactories.builder().skipResolveOrder().addPipelineAggregator(testAgg); logger.info("Content string: {}", factoriesBuilder); XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); if (randomBoolean()) { builder.prettyPrint(); } factoriesBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); XContentBuilder shuffled = shuffleXContent(builder); XContentParser parser = XContentFactory.xContent(shuffled.bytes()).createParser(shuffled.bytes()); QueryParseContext parseContext = new QueryParseContext(queriesRegistry, parser, parseFieldMatcher); String contentString = factoriesBuilder.toString(); logger.info("Content string: {}", contentString); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken()); assertEquals(testAgg.getName(), parser.currentName()); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken()); assertEquals(testAgg.type(), parser.currentName()); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); PipelineAggregationBuilder newAgg = aggParsers .pipelineParser(testAgg.getWriteableName(), ParseFieldMatcher.STRICT) .parse(testAgg.getName(), parseContext); assertSame(XContentParser.Token.END_OBJECT, parser.currentToken()); assertSame(XContentParser.Token.END_OBJECT, parser.nextToken()); assertSame(XContentParser.Token.END_OBJECT, parser.nextToken()); assertNull(parser.nextToken()); assertNotNull(newAgg); assertNotSame(newAgg, testAgg); assertEquals(testAgg, newAgg); assertEquals(testAgg.hashCode(), newAgg.hashCode()); }