From c1f4f1ec94f53164e2c9b034cf2d8fa0bcf35ca8 Mon Sep 17 00:00:00 2001 From: Roman Date: Fri, 21 Jun 2024 20:37:32 +0200 Subject: [PATCH] test: provide additional to deserialize parent aggregation Signed-off-by: Roman --- CHANGELOG.md | 1 + .../core/AggregateResponseTest.java | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/core/AggregateResponseTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 453685d9e4..e331fa12d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This section is for maintaining a changelog for all breaking changes for the cli ### Fixed - Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254)) +- Deserialize aggregation containing a parent aggregation ([#706](https://github.com/opensearch-project/opensearch-java/pull/706)) ### Security diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/core/AggregateResponseTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/core/AggregateResponseTest.java new file mode 100644 index 0000000000..2b10a7e732 --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/core/AggregateResponseTest.java @@ -0,0 +1,55 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.client.opensearch.core; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import jakarta.json.stream.JsonParser; +import java.io.StringReader; +import java.util.Map; +import org.junit.Test; +import org.opensearch.client.opensearch._types.aggregations.Aggregate; +import org.opensearch.client.opensearch._types.aggregations.ParentAggregate; +import org.opensearch.client.opensearch.model.ModelTestCase; + +public class AggregateResponseTest extends ModelTestCase { + + @Test + public void shouldCreateParentAggregate() { + // given + final Aggregate aggregate = Aggregate.of((b) -> b.parent((p) -> p.docCount(789L))); + + // when + final ParentAggregate parentAggregate = aggregate.parent(); + + // then + assertEquals(parentAggregate._aggregateKind(), Aggregate.Kind.Parent); + assertEquals(parentAggregate.docCount(), 789L); + } + + @Test + public void shouldDeserializeParentAggregate() throws JsonProcessingException { + // given + final String parentAggreationJson = "{\"took\": 3,\"timed_out\": false," + + "\"_shards\": {\"total\": 1,\"successful\": 1,\"skipped\": 0,\"failed\": 0}," + + "\"hits\": {\"total\": {\"value\": 0,\"relation\": \"eq\"},\"hits\": []}," + + "\"aggregations\": {\"parent#foo\": {\"doc_count\": 123456}}}"; + final JsonParser parser = mapper.jsonProvider().createParser(new StringReader(parentAggreationJson)); + + // when + final SearchResponse response = SearchResponse._DESERIALIZER.deserialize(parser, mapper); + + // then + final Map aggregations = response.aggregations(); + assertFalse(aggregations.isEmpty()); + assertTrue(aggregations.containsKey("foo")); + assertEquals(aggregations.get("foo").parent()._aggregateKind(), Aggregate.Kind.Parent); + assertEquals(aggregations.get("foo").parent().docCount(), 123456L); + } +}