/** Test serialization and deserialization of the test AggregatorFactory. */ public void testSerialization() throws IOException { AF testAgg = createTestAggregatorFactory(); try (BytesStreamOutput output = new BytesStreamOutput()) { output.writeNamedWriteable(testAgg); try (StreamInput in = new NamedWriteableAwareStreamInput( output.bytes().streamInput(), namedWriteableRegistry)) { PipelineAggregationBuilder deserializedQuery = in.readNamedWriteable(PipelineAggregationBuilder.class); assertEquals(deserializedQuery, testAgg); assertEquals(deserializedQuery.hashCode(), testAgg.hashCode()); assertNotSame(deserializedQuery, testAgg); } } }
/** * 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()); }
public void testEqualsAndHashcode() throws IOException { AF firstAgg = createTestAggregatorFactory(); assertFalse("aggregation is equal to null", firstAgg.equals(null)); assertFalse("aggregation is equal to incompatible type", firstAgg.equals("")); assertTrue("aggregation is not equal to self", firstAgg.equals(firstAgg)); assertThat( "same aggregation's hashcode returns different values if called multiple times", firstAgg.hashCode(), equalTo(firstAgg.hashCode())); AF secondQuery = copyAggregation(firstAgg); assertTrue("aggregation is not equal to self", secondQuery.equals(secondQuery)); assertTrue("aggregation is not equal to its copy", firstAgg.equals(secondQuery)); assertTrue("equals is not symmetric", secondQuery.equals(firstAgg)); assertThat( "aggregation copy's hashcode is different from original hashcode", secondQuery.hashCode(), equalTo(firstAgg.hashCode())); AF thirdQuery = copyAggregation(secondQuery); assertTrue("aggregation is not equal to self", thirdQuery.equals(thirdQuery)); assertTrue("aggregation is not equal to its copy", secondQuery.equals(thirdQuery)); assertThat( "aggregation copy's hashcode is different from original hashcode", secondQuery.hashCode(), equalTo(thirdQuery.hashCode())); assertTrue("equals is not transitive", firstAgg.equals(thirdQuery)); assertThat( "aggregation copy's hashcode is different from original hashcode", firstAgg.hashCode(), equalTo(thirdQuery.hashCode())); assertTrue("equals is not symmetric", thirdQuery.equals(secondQuery)); assertTrue("equals is not symmetric", thirdQuery.equals(firstAgg)); }