Skip to content

Commit

Permalink
test: provide additional to deserialize parent aggregation
Browse files Browse the repository at this point in the history
Signed-off-by: Roman <[email protected]>
  • Loading branch information
romansmirnov committed Jun 21, 2024
1 parent 2a32357 commit c1f4f1e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> response = SearchResponse._DESERIALIZER.deserialize(parser, mapper);

// then
final Map<String, Aggregate> 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);
}
}

0 comments on commit c1f4f1e

Please sign in to comment.