From 56341be2055a50746551300915bc2eb870dea284 Mon Sep 17 00:00:00 2001 From: Vacha Shah Date: Wed, 4 Oct 2023 20:36:29 +0000 Subject: [PATCH] Enabling spotless for rest of the code including tests Signed-off-by: Vacha Shah --- java-client/build.gradle.kts | 2 +- .../documentation/ApiConventionsTest.java | 17 +- .../client/documentation/ConnectingTest.java | 12 +- .../documentation/FailingTransport.java | 25 +- .../client/documentation/MigrateHlrcTest.java | 13 +- .../opensearch/experiments/ClientTests.java | 115 ++- .../opensearch/experiments/ParsingTests.java | 236 +++--- .../opensearch/experiments/api/Bar.java | 75 +- .../experiments/api/FooOptRequest.java | 418 +++++----- .../experiments/api/FooRequest.java | 468 ++++++----- .../experiments/api/FooResponse.java | 2 +- .../experiments/api/query2/BoolQuery.java | 31 +- .../experiments/api/query2/Query.java | 14 +- .../experiments/api/query2/QueryTest.java | 3 +- .../experiments/api/query2/TermsQuery.java | 2 +- .../experiments/api/query2/Union2.java | 205 ++--- .../opensearch/experiments/base/Client.java | 28 +- .../experiments/base/PrimitiveUtils.java | 24 +- .../opensearch/experiments/base/Variants.java | 191 +++-- .../experiments/containers/SomeUnion.java | 14 +- .../experiments/containers/SomeUnionTest.java | 9 +- .../containers/SomeUnionVariant.java | 3 +- .../experiments/containers/UVariantA.java | 5 +- .../experiments/containers/UVariantB.java | 7 +- .../experiments/generics/GenericClass.java | 8 +- .../inheritance/InheritanceTest.java | 1 - .../inheritance/base/BaseClass.java | 9 +- .../inheritance/child/ChildClass.java | 14 +- .../inheritance/final_/FinalClass.java | 18 +- .../AbstractAggregationRequestIT.java | 457 +++++------ .../integTest/AbstractCatClientIT.java | 92 ++- .../integTest/AbstractClusterClientIT.java | 132 ++-- .../opensearch/integTest/AbstractCrudIT.java | 249 ++---- .../integTest/AbstractHighlightIT.java | 81 +- .../integTest/AbstractIndicesClientIT.java | 51 +- .../opensearch/integTest/AbstractKnnIT.java | 77 +- .../AbstractMultiSearchRequestIT.java | 708 +++++++++-------- .../opensearch/integTest/AbstractNodesIT.java | 1 - .../integTest/AbstractPingAndInfoIT.java | 7 +- .../integTest/AbstractRequestIT.java | 738 +++++++----------- .../AbstractSearchTemplateRequestIT.java | 363 +++++---- .../OpenSearchJavaClientTestCase.java | 52 +- .../integTest/OpenSearchTransportSupport.java | 5 +- .../integTest/aws/AwsSdk2BulkRequestIT.java | 37 +- .../integTest/aws/AwsSdk2SearchIT.java | 37 +- .../integTest/aws/AwsSdk2SecurityIT.java | 14 +- .../aws/AwsSdk2TransportTestCase.java | 142 ++-- .../httpclient5/AggregationRequestIT.java | 3 +- .../integTest/httpclient5/CatIT.java | 5 +- .../httpclient5/ClusterClientIT.java | 1 - .../integTest/httpclient5/CrudIT.java | 3 +- .../integTest/httpclient5/HighlightIT.java | 3 +- .../HttpClient5TransportSupport.java | 21 +- .../httpclient5/IndicesClientIT.java | 3 +- .../integTest/httpclient5/KnnIT.java | 3 +- .../httpclient5/MultiSearchRequestIT.java | 3 +- .../integTest/httpclient5/NodesIT.java | 3 +- .../integTest/httpclient5/PingAndInfoIT.java | 4 +- .../integTest/httpclient5/RequestIT.java | 3 +- .../httpclient5/SearchTemplateRequestIT.java | 3 +- .../restclient/AggregationRequestIT.java | 3 +- .../integTest/restclient/CatIT.java | 3 +- .../integTest/restclient/ClusterClientIT.java | 1 - .../integTest/restclient/CrudIT.java | 1 - .../integTest/restclient/HighlightIT.java | 3 +- .../integTest/restclient/IndicesClientIT.java | 1 - .../restclient/MultiSearchRequestIT.java | 11 +- .../integTest/restclient/NodesIT.java | 1 - .../integTest/restclient/PingAndInfoIT.java | 1 - .../integTest/restclient/RequestIT.java | 73 +- .../restclient/SearchTemplateRequestIT.java | 3 +- .../opensearch/model/BehaviorsTest.java | 58 +- .../opensearch/model/BuiltinTypesTest.java | 85 +- .../opensearch/model/ClassStructureTest.java | 105 +-- .../client/opensearch/model/EndpointTest.java | 4 +- .../client/opensearch/model/EnumTest.java | 3 +- .../opensearch/model/ModelTestCase.java | 13 +- .../opensearch/model/RequestEncodingTest.java | 17 +- .../opensearch/model/SerializationTest.java | 44 +- .../opensearch/model/TypedKeysTest.java | 70 +- .../client/opensearch/model/UnionTests.java | 17 +- .../client/opensearch/model/VariantsTest.java | 154 ++-- 82 files changed, 2714 insertions(+), 3227 deletions(-) diff --git a/java-client/build.gradle.kts b/java-client/build.gradle.kts index c54dfd3bc9..cb0967413b 100644 --- a/java-client/build.gradle.kts +++ b/java-client/build.gradle.kts @@ -272,7 +272,7 @@ tasks.withType { spotless { java { - target("**/json/**/*.java", "**/transport/**/*.java", "**/util/**/*.java", "**/main/**/opensearch/**/*.java") + target("**/*.java") // Use the default importOrder configuration importOrder() diff --git a/java-client/src/test/java/org/opensearch/client/documentation/ApiConventionsTest.java b/java-client/src/test/java/org/opensearch/client/documentation/ApiConventionsTest.java index 867e7d45ef..8d4b2ce9d6 100644 --- a/java-client/src/test/java/org/opensearch/client/documentation/ApiConventionsTest.java +++ b/java-client/src/test/java/org/opensearch/client/documentation/ApiConventionsTest.java @@ -32,6 +32,14 @@ package org.opensearch.client.documentation; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.LogManager; +import java.util.logging.Logger; +import org.junit.Assert; +import org.junit.Test; import org.opensearch.client.opensearch.OpenSearchAsyncClient; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch._types.NodeStatistics; @@ -46,15 +54,6 @@ import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.TransportException; import org.opensearch.client.util.ApiTypeHelper; -import org.junit.Assert; -import org.junit.Test; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.logging.LogManager; -import java.util.logging.Logger; public class ApiConventionsTest extends Assert { diff --git a/java-client/src/test/java/org/opensearch/client/documentation/ConnectingTest.java b/java-client/src/test/java/org/opensearch/client/documentation/ConnectingTest.java index 4e28b487aa..53c2f65825 100644 --- a/java-client/src/test/java/org/opensearch/client/documentation/ConnectingTest.java +++ b/java-client/src/test/java/org/opensearch/client/documentation/ConnectingTest.java @@ -32,20 +32,20 @@ package org.opensearch.client.documentation; +import org.apache.hc.core5.http.HttpHost; +import org.junit.Ignore; +import org.junit.Test; +import org.opensearch.client.RestClient; +import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch.core.SearchResponse; import org.opensearch.client.opensearch.core.search.Hit; -import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.rest_client.RestClientTransport; -import org.opensearch.client.RestClient; -import org.apache.hc.core5.http.HttpHost; -import org.junit.Ignore; -import org.junit.Test; public class ConnectingTest { - private static class Product{} + private static class Product {} @Ignore // we don't have a running ES @Test diff --git a/java-client/src/test/java/org/opensearch/client/documentation/FailingTransport.java b/java-client/src/test/java/org/opensearch/client/documentation/FailingTransport.java index 87d11d4795..07e9215c6f 100644 --- a/java-client/src/test/java/org/opensearch/client/documentation/FailingTransport.java +++ b/java-client/src/test/java/org/opensearch/client/documentation/FailingTransport.java @@ -32,14 +32,6 @@ package org.opensearch.client.documentation; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; -import org.opensearch.client.transport.OpenSearchTransport; -import org.opensearch.client.transport.Endpoint; -import org.opensearch.client.transport.TransportException; -import org.opensearch.client.transport.TransportOptions; - -import javax.annotation.Nullable; import java.io.IOException; import java.util.Collection; import java.util.Collections; @@ -47,6 +39,13 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.function.Function; +import javax.annotation.Nullable; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.jsonb.JsonbJsonpMapper; +import org.opensearch.client.transport.Endpoint; +import org.opensearch.client.transport.OpenSearchTransport; +import org.opensearch.client.transport.TransportException; +import org.opensearch.client.transport.TransportOptions; /** * A transport implementation that always fails. Used for simple doc sections where we just want to check compilation. @@ -87,8 +86,11 @@ public ResponseT performRequest( } @Override - public CompletableFuture performRequestAsync(RequestT request, Endpoint endpoint, @Nullable TransportOptions options) { + public CompletableFuture performRequestAsync( + RequestT request, + Endpoint endpoint, + @Nullable TransportOptions options + ) { CompletableFuture future = new CompletableFuture<>(); future.completeExceptionally(new TransportException("Not implemented")); return future; @@ -105,6 +107,5 @@ public TransportOptions options() { } @Override - public void close() throws IOException { - } + public void close() throws IOException {} } diff --git a/java-client/src/test/java/org/opensearch/client/documentation/MigrateHlrcTest.java b/java-client/src/test/java/org/opensearch/client/documentation/MigrateHlrcTest.java index d8c57e4e76..bf814aca4d 100644 --- a/java-client/src/test/java/org/opensearch/client/documentation/MigrateHlrcTest.java +++ b/java-client/src/test/java/org/opensearch/client/documentation/MigrateHlrcTest.java @@ -32,21 +32,20 @@ package org.opensearch.client.documentation; -import org.opensearch.client.opensearch.OpenSearchClient; +import org.apache.hc.core5.http.HttpHost; +import org.junit.Test; +import org.opensearch.client.RestClient; +import org.opensearch.client.RestClientBuilder; import org.opensearch.client.json.jackson.JacksonJsonpMapper; +import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.transport.OpenSearchTransport; import org.opensearch.client.transport.rest_client.RestClientTransport; -import org.opensearch.client.RestClient; -import org.opensearch.client.RestClientBuilder; -import org.apache.hc.core5.http.HttpHost; -import org.junit.Test; public class MigrateHlrcTest { // Fake HLRC -- we don't want to import it for just one example public static class RestHighLevelClient { - public RestHighLevelClient(RestClientBuilder builder) { - } + public RestHighLevelClient(RestClientBuilder builder) {} public RestClient getLowLevelClient() { return null; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ClientTests.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ClientTests.java index 7ac43fc81e..3bcf0aee12 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ClientTests.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ClientTests.java @@ -32,6 +32,9 @@ package org.opensearch.client.opensearch.experiments; +import java.io.IOException; +import java.util.Collections; +import org.junit.Test; import org.opensearch.client.opensearch.experiments.api.Bar; import org.opensearch.client.opensearch.experiments.api.FooRequest; import org.opensearch.client.opensearch.experiments.api.FooResponse; @@ -39,83 +42,55 @@ import org.opensearch.client.opensearch.experiments.api.query2.TermsQuery; import org.opensearch.client.opensearch.experiments.base.Client; import org.opensearch.client.transport.Endpoint; -import org.junit.Test; - -import java.io.IOException; -import java.util.Collections; public class ClientTests { - @Test - public void testClient() throws Exception { + @Test + public void testClient() throws Exception { - Client client = new Client(){ - @Override - protected ResponseT performRequest( - RequestT request, Endpoint endpoint - ) throws IOException { - // don't care for now, we're testing compilation - return null; - } - }; + Client client = new Client() { + @Override + protected ResponseT performRequest( + RequestT request, + Endpoint endpoint + ) throws IOException { + // don't care for now, we're testing compilation + return null; + } + }; - client.foo(fb -> fb - .name("z") - .value(1) - .routing("fooo") - .query(q -> q - .bool(b-> b - .add_must(m -> m.terms((TermsQuery) null)) - ) - //.terms(tq -> tq.term("")) - .meta(Collections.emptyMap()) - ) - ); + client.foo( + fb -> fb.name("z") + .value(1) + .routing("fooo") + .query( + q -> q.bool(b -> b.add_must(m -> m.terms((TermsQuery) null))) + // .terms(tq -> tq.term("")) + .meta(Collections.emptyMap()) + ) + ); - // Builders everywhere - FooResponse r1 = client.foo( - FooRequest.builder() - .name("z") - .value(1) - .bar(Bar.builder() - .name("Raise the bar") - .build() - ) - .build() - ); + // Builders everywhere + FooResponse r1 = client.foo(FooRequest.builder().name("z").value(1).bar(Bar.builder().name("Raise the bar").build()).build()); - // Illustrates creating an object outside of the client call - TermsQuery filter = TermsQuery.builder() - .field("foo") - .term("bar") - .build(); + // Illustrates creating an object outside of the client call + TermsQuery filter = TermsQuery.builder().field("foo").term("bar").build(); - Query filter2 = new Query.Builder() - .terms(t -> t - .field("foo") - .term("bar") - ).build(); + Query filter2 = new Query.Builder().terms(t -> t.field("foo").term("bar")).build(); - // Fluid lambda-based builders - FooResponse r2 = client.foo(f -> f - .name("z") - .value(1) - .bar(bar -> bar - .name("Raise the bar") - ) - .query(q -> q - .bool(b -> b - .add_must(q1 -> q1.terms(filter)) - .add_must(q1 -> q1 - .terms(t -> t.field("a").term("b")) - ) - .add_must(q1 -> q1 - .terms(t -> t.field("a").term("b")) - ) - .minimumShouldMatch(2) - ) - ) - ); - } + // Fluid lambda-based builders + FooResponse r2 = client.foo( + f -> f.name("z") + .value(1) + .bar(bar -> bar.name("Raise the bar")) + .query( + q -> q.bool( + b -> b.add_must(q1 -> q1.terms(filter)) + .add_must(q1 -> q1.terms(t -> t.field("a").term("b"))) + .add_must(q1 -> q1.terms(t -> t.field("a").term("b"))) + .minimumShouldMatch(2) + ) + ) + ); + } } - diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java index 026fe79bf1..31ad1932be 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java @@ -54,81 +54,76 @@ public class ParsingTests extends ModelTestCase { - @Test - public void testFoo() { - - FooRequest foo = FooRequest.builder() - .name("z") - .value(1) - .indices("a", "b", "c") - .bar(b -> b - .name("Raise the bar") + @Test + public void testFoo() { + + FooRequest foo = FooRequest.builder().name("z").value(1).indices("a", "b", "c").bar(b -> b.name("Raise the bar")).build(); + + String str = toJson(foo); + assertEquals("{\"name\":\"z\",\"value\":1,\"indices\":[\"a\",\"b\",\"c\"],\"bar\":{\"name\":\"Raise the bar\"}}", str); + + FooRequest foo2 = fromJson(str, FooRequest.parser()); + assertEquals(foo.name(), foo2.name()); + assertEquals(foo.value(), foo2.value()); + assertNull(foo2.size()); + assertEquals(foo.indices(), foo2.indices()); + assertEquals("Raise the bar", foo.bar().name()); + } + + @Test + public void testIndexSettingsTranslogOptionsParsing() { + + var indexSettings = IndexSettings.of( + _1 -> _1.translog( + Translog.of(tr -> tr.syncInterval(Time.of(t -> t.time("10s"))).durability("async").flushThresholdSize("256mb")) ) - .build(); - - String str = toJson(foo); - assertEquals("{\"name\":\"z\",\"value\":1,\"indices\":[\"a\",\"b\",\"c\"],\"bar\":{\"name\":\"Raise the bar\"}}", str); - - FooRequest foo2 = fromJson(str, FooRequest.parser()); - assertEquals(foo.name(), foo2.name()); - assertEquals(foo.value(), foo2.value()); - assertNull(foo2.size()); - assertEquals(foo.indices(), foo2.indices()); - assertEquals("Raise the bar", foo.bar().name()); - } - - @Test - public void testIndexSettingsTranslogOptionsParsing() { - - var indexSettings = IndexSettings.of(_1 -> _1.translog(Translog.of(tr -> tr - .syncInterval(Time.of(t -> t.time("10s"))) - .durability("async") - .flushThresholdSize("256mb")))); - - var str = toJson(indexSettings); - assertEquals("{\"translog\":{\"durability\":\"async\",\"flush_threshold_size\":\"256mb\"," + - "\"sync_interval\":\"10s\"}}", str); - - IndexSettings deserialized = fromJson(str, IndexSettings._DESERIALIZER); - assertEquals(indexSettings.translog().syncInterval().time(), deserialized.translog().syncInterval().time()); - assertEquals(indexSettings.translog().durability(), deserialized.translog().durability()); - assertEquals(indexSettings.translog().flushThresholdSize(), deserialized.translog().flushThresholdSize()); - - var deprecatedForm = "{\"translog\":{\"sync_interval\":\"10s\"},\"translog.durability\":\"async\",\"translog" + - ".flush_threshold_size\":\"256mb\"}"; - IndexSettings deprecatedDeserialized = fromJson(deprecatedForm, IndexSettings._DESERIALIZER); - assertEquals(indexSettings.translog().syncInterval().time(), deprecatedDeserialized.translog().syncInterval().time()); - assertEquals(indexSettings.translog().durability(), deprecatedDeserialized.translog().durability()); - assertEquals(indexSettings.translog().flushThresholdSize(), deprecatedDeserialized.translog().flushThresholdSize()); - } - - @Test - public void testIndexSettingsMappingParsing() { - - var mapping = IndexSettingsMapping.of(b -> b - .totalFields(d -> d.limit(1L)) - .depth(d -> d.limit(2L)) - .nestedFields(d -> d.limit(3L)) - .nestedObjects(d -> d.limit(4L)) - .fieldNameLength(d -> d.limit(5L))); - - var str = toJson(mapping); - assertEquals("{\"total_fields\":{\"limit\":1},\"depth\":{\"limit\":2},\"nested_fields\":{\"limit\":3}," + - "\"nested_objects\":{\"limit\":4},\"field_name_length\":{\"limit\":5}}", str); - - var deserialized = fromJson(str, IndexSettingsMapping._DESERIALIZER); - assertEquals(mapping.totalFields().limit(), deserialized.totalFields().limit()); - assertEquals(mapping.depth().limit(), deserialized.depth().limit()); - assertEquals(mapping.nestedFields().limit(), deserialized.nestedFields().limit()); - assertEquals(mapping.nestedObjects().limit(), deserialized.nestedObjects().limit()); - assertEquals(mapping.fieldNameLength().limit(), deserialized.fieldNameLength().limit()); - } + ); + + var str = toJson(indexSettings); + assertEquals("{\"translog\":{\"durability\":\"async\",\"flush_threshold_size\":\"256mb\"," + "\"sync_interval\":\"10s\"}}", str); + + IndexSettings deserialized = fromJson(str, IndexSettings._DESERIALIZER); + assertEquals(indexSettings.translog().syncInterval().time(), deserialized.translog().syncInterval().time()); + assertEquals(indexSettings.translog().durability(), deserialized.translog().durability()); + assertEquals(indexSettings.translog().flushThresholdSize(), deserialized.translog().flushThresholdSize()); + + var deprecatedForm = "{\"translog\":{\"sync_interval\":\"10s\"},\"translog.durability\":\"async\",\"translog" + + ".flush_threshold_size\":\"256mb\"}"; + IndexSettings deprecatedDeserialized = fromJson(deprecatedForm, IndexSettings._DESERIALIZER); + assertEquals(indexSettings.translog().syncInterval().time(), deprecatedDeserialized.translog().syncInterval().time()); + assertEquals(indexSettings.translog().durability(), deprecatedDeserialized.translog().durability()); + assertEquals(indexSettings.translog().flushThresholdSize(), deprecatedDeserialized.translog().flushThresholdSize()); + } + + @Test + public void testIndexSettingsMappingParsing() { + + var mapping = IndexSettingsMapping.of( + b -> b.totalFields(d -> d.limit(1L)) + .depth(d -> d.limit(2L)) + .nestedFields(d -> d.limit(3L)) + .nestedObjects(d -> d.limit(4L)) + .fieldNameLength(d -> d.limit(5L)) + ); + + var str = toJson(mapping); + assertEquals( + "{\"total_fields\":{\"limit\":1},\"depth\":{\"limit\":2},\"nested_fields\":{\"limit\":3}," + + "\"nested_objects\":{\"limit\":4},\"field_name_length\":{\"limit\":5}}", + str + ); + + var deserialized = fromJson(str, IndexSettingsMapping._DESERIALIZER); + assertEquals(mapping.totalFields().limit(), deserialized.totalFields().limit()); + assertEquals(mapping.depth().limit(), deserialized.depth().limit()); + assertEquals(mapping.nestedFields().limit(), deserialized.nestedFields().limit()); + assertEquals(mapping.nestedObjects().limit(), deserialized.nestedObjects().limit()); + assertEquals(mapping.fieldNameLength().limit(), deserialized.fieldNameLength().limit()); + } @Test public void testSmartcn_Analyzer() { - final Analyzer analyzer = new Analyzer.Builder() - .smartcn() - .build(); + final Analyzer analyzer = new Analyzer.Builder().smartcn().build(); assertTrue(analyzer.isSmartcn()); @@ -151,23 +146,17 @@ public void testSmartcn_Tokenizer() { @Test public void testSmartcn_StopFilter() { - final TokenFilterDefinition analyzer = new TokenFilterDefinition.Builder() - .smartcn_stop(b -> b) - .build(); + final TokenFilterDefinition analyzer = new TokenFilterDefinition.Builder().smartcn_stop(b -> b).build(); String str = toJson(analyzer); assertEquals("{\"type\":\"smartcn_stop\"}", str); TokenFilterDefinition analyzer2 = fromJson(str, TokenFilterDefinition._DESERIALIZER); } + @Test public void testCjk_Analyzer() { - final Analyzer analyzer = new Analyzer.Builder() - .cjk(b -> b - .stopwords(List.of("a", "b", "c")) - .stopwordsPath("path") - ) - .build(); + final Analyzer analyzer = new Analyzer.Builder().cjk(b -> b.stopwords(List.of("a", "b", "c")).stopwordsPath("path")).build(); assertTrue(analyzer.isCjk()); @@ -185,52 +174,65 @@ public void testFieldMappingResponse() { final String indexName = "indexName"; final String field1Name = "field1"; final String field1Analyzer = "my_analyzer"; - final FieldMapping field1 = FieldMapping.of(fmb -> fmb - .fullName(field1Name) - .mapping(field1Name, pb -> pb - .text(tpb -> tpb - .store(true) - .termVector(TermVectorOption.WithPositionsOffsets) - .analyzer(field1Analyzer) - .positionIncrementGap(10)) - ) + final FieldMapping field1 = FieldMapping.of( + fmb -> fmb.fullName(field1Name) + .mapping( + field1Name, + pb -> pb.text( + tpb -> tpb.store(true) + .termVector(TermVectorOption.WithPositionsOffsets) + .analyzer(field1Analyzer) + .positionIncrementGap(10) + ) + ) ); final String field3Name = "field3"; // Build aFieldMappingResponse with several fields - final GetFieldMappingResponse response = GetFieldMappingResponse.of(b -> b - .putResult(indexName, TypeFieldMappings.of(tfmb -> tfmb - .putMappings(field1Name, field1) - .putMappings("field2", fmb -> fmb - .fullName("field2") - .mapping("field2", pb -> pb - .text(tpb -> tpb - .store(true) - .termVector(TermVectorOption.WithPositionsOffsets) - .analyzer("another_analyzer") - .positionIncrementGap(10)) + final GetFieldMappingResponse response = GetFieldMappingResponse.of( + b -> b.putResult( + indexName, + TypeFieldMappings.of( + tfmb -> tfmb.putMappings(field1Name, field1) + .putMappings( + "field2", + fmb -> fmb.fullName("field2") + .mapping( + "field2", + pb -> pb.text( + tpb -> tpb.store(true) + .termVector(TermVectorOption.WithPositionsOffsets) + .analyzer("another_analyzer") + .positionIncrementGap(10) + ) + ) ) - ) - .putMappings(field3Name, fmb -> fmb - .fullName(field3Name) - .mapping(field3Name, pb -> pb - .text(tpb -> tpb - .store(true) - .termVector(TermVectorOption.WithPositionsOffsets) - .analyzer("this_analyzer") - .positionIncrementGap(10)) + .putMappings( + field3Name, + fmb -> fmb.fullName(field3Name) + .mapping( + field3Name, + pb -> pb.text( + tpb -> tpb.store(true) + .termVector(TermVectorOption.WithPositionsOffsets) + .analyzer("this_analyzer") + .positionIncrementGap(10) + ) + ) ) - ) ) ) ); String str = toJson(response); - assertEquals("{\"indexName\":{\"mappings\":{\"field1\":{\"full_name\":\"field1\",\"mapping\"" - +":{\"field1\":{\"type\":\"text\",\"store\":true,\"analyzer\":\"my_analyzer\"," + - "\"position_increment_gap\":10,\"term_vector\":\"with_positions_offsets\"}}}," - +"\"field3\":{\"full_name\":\"field3\",\"mapping\":{\"field3\":{\"type\":\"text\",\"store\":true," + - "\"analyzer\":\"this_analyzer\",\"position_increment_gap\":10,\"term_vector\":\"with_positions_offsets\"}}}," - +"\"field2\":{\"full_name\":\"field2\",\"mapping\":{\"field2\":{\"type\":\"text\",\"store\":true," - +"\"analyzer\":\"another_analyzer\",\"position_increment_gap\":10,\"term_vector\":\"with_positions_offsets\"}}}}}}", str); + assertEquals( + "{\"indexName\":{\"mappings\":{\"field1\":{\"full_name\":\"field1\",\"mapping\"" + + ":{\"field1\":{\"type\":\"text\",\"store\":true,\"analyzer\":\"my_analyzer\"," + + "\"position_increment_gap\":10,\"term_vector\":\"with_positions_offsets\"}}}," + + "\"field3\":{\"full_name\":\"field3\",\"mapping\":{\"field3\":{\"type\":\"text\",\"store\":true," + + "\"analyzer\":\"this_analyzer\",\"position_increment_gap\":10,\"term_vector\":\"with_positions_offsets\"}}}," + + "\"field2\":{\"full_name\":\"field2\",\"mapping\":{\"field2\":{\"type\":\"text\",\"store\":true," + + "\"analyzer\":\"another_analyzer\",\"position_increment_gap\":10,\"term_vector\":\"with_positions_offsets\"}}}}}}", + str + ); final GetFieldMappingResponse response2 = fromJson(str, GetFieldMappingResponse._DESERIALIZER); final TypeFieldMappings typeFieldMappings = response2.get(indexName); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/Bar.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/Bar.java index c8e3347fa5..8388634e92 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/Bar.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/Bar.java @@ -32,64 +32,63 @@ package org.opensearch.client.opensearch.experiments.api; +import jakarta.json.stream.JsonGenerator; +import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; - -import javax.annotation.Nullable; public class Bar implements JsonpSerializable { - private final String name; + private final String name; - @Nullable - public String name() { - return this.name; - } + @Nullable + public String name() { + return this.name; + } - public Bar(Bar.Builder builder) { - this.name = builder.name; - } + public Bar(Bar.Builder builder) { + this.name = builder.name; + } - @Override - public void serialize(JsonGenerator builder, JsonpMapper mapper) { - builder.writeStartObject(); - if (this.name != null) builder.write("name", this.name); - builder.writeEnd(); - } + @Override + public void serialize(JsonGenerator builder, JsonpMapper mapper) { + builder.writeStartObject(); + if (this.name != null) builder.write("name", this.name); + builder.writeEnd(); + } - public static Builder builder() { - return new Builder(); - } + public static Builder builder() { + return new Builder(); + } - public static class Builder implements ObjectBuilder { - private String name; + public static class Builder implements ObjectBuilder { + private String name; - public Bar.Builder name(@Nullable String name) { - this.name = name; - return this; - } + public Bar.Builder name(@Nullable String name) { + this.name = name; + return this; + } - public Bar build() { - return new Bar(this); + public Bar build() { + return new Bar(this); + } } - } - private static final JsonpDeserializer PARSER; + private static final JsonpDeserializer PARSER; - public static JsonpDeserializer parser() { - return PARSER; - } + public static JsonpDeserializer parser() { + return PARSER; + } - static { - ObjectDeserializer op = new ObjectDeserializer<>(Builder::new); + static { + ObjectDeserializer op = new ObjectDeserializer<>(Builder::new); - op.add(Builder::name, JsonpDeserializer.stringDeserializer(), "name"); + op.add(Builder::name, JsonpDeserializer.stringDeserializer(), "name"); - PARSER = new ObjectBuilderDeserializer<>(op); - } + PARSER = new ObjectBuilderDeserializer<>(op); + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooOptRequest.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooOptRequest.java index 191c8940b3..b1c15a1396 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooOptRequest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooOptRequest.java @@ -32,256 +32,254 @@ package org.opensearch.client.opensearch.experiments.api; -import org.opensearch.client.opensearch._types.ErrorResponse; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.JsonpSerializable; -import org.opensearch.client.transport.Endpoint; -import org.opensearch.client.transport.endpoints.SimpleEndpoint; -import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; +import static java.util.Objects.requireNonNull; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import jakarta.json.stream.JsonGenerator; import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.OptionalInt; - -import static java.util.Objects.requireNonNull; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.JsonpSerializable; +import org.opensearch.client.opensearch._types.ErrorResponse; +import org.opensearch.client.transport.Endpoint; +import org.opensearch.client.transport.endpoints.SimpleEndpoint; +import org.opensearch.client.util.ObjectBuilder; // Implementing ToXContent is optional, only if there's a request body public class FooOptRequest implements JsonpSerializable { - //=========================================== - // Fields and getters - //=========================================== - - //------------------------------------------- - // A required String - - private final String name; - - @Nonnull - public final String name() { - return this.name; - } - - //------------------------------------------- - // An optional String - - private final String routing; - - @Nonnull - public final Optional routing() { - return Optional.ofNullable(this.routing); - } + // =========================================== + // Fields and getters + // =========================================== - //------------------------------------------- - // A required primitive type + // ------------------------------------------- + // A required String - private final int value; + private final String name; - public int value() { - return this.value; - } - - //------------------------------------------- - // An optional primitive type - private final OptionalInt size; - - /** - * Description for field {@code size} - */ - @Nonnull - public final OptionalInt size() { - return this.size; - } - - //------------------------------------------- - // An optional array - - private final List indices; - - @Nonnull - public final Optional> indices() { - return Optional.ofNullable(this.indices); - } - - //------------------------------------------- - // An optional sub-object - - private final Bar bar; + @Nonnull + public final String name() { + return this.name; + } - @Nonnull - public Optional bar() { - return Optional.ofNullable(this.bar); - } + // ------------------------------------------- + // An optional String - public FooOptRequest(Builder builder) { - this.name = builder.name; - this.routing = builder.routing; - this.value = builder.value; - this.size = builder.size == null ? OptionalInt.empty() : OptionalInt.of(builder.size); - this.indices = builder.indices; - this.bar = builder.bar; - } + private final String routing; - //=========================================== - // Serialization - //=========================================== + @Nonnull + public final Optional routing() { + return Optional.ofNullable(this.routing); + } + // ------------------------------------------- + // A required primitive type - @Override - public void serialize(JsonGenerator builder, JsonpMapper mapper) { - builder.writeStartObject(); + private final int value; - // Classic approach is to use the deserialization field's preferred name: - // - // if (buckets.isPresent()) builder.field(INT_FIELD.getPreferredName(), buckets.getAsInt()); - // - // In a generated code context we can use the string constant, which also avoids declaring ParseField if we - // only serialize (e.g. requests) + public int value() { + return this.value; + } - // required field - builder.write("name", this.name); + // ------------------------------------------- + // An optional primitive type + private final OptionalInt size; - // optional field - if (this.routing != null) builder.write("routing", this.routing); + /** + * Description for field {@code size} + */ + @Nonnull + public final OptionalInt size() { + return this.size; + } - // required field - builder.write("value", value); + // ------------------------------------------- + // An optional array - // optional field - if (this.size.isPresent()) builder.write("size", this.size.getAsInt()); + private final List indices; - // We could just call this and let the builder will handle it by itself with: - // - // if (this.indices != null) builder.field("indices", this.indices); - // - // But the logic is a bit involved (like checking circular dependencies) so we use a more direct implementation: - if (this.indices != null) { - builder.writeKey("indices").writeStartArray(); - for (String str : this.indices) builder.write(str); - builder.writeEnd(); + @Nonnull + public final Optional> indices() { + return Optional.ofNullable(this.indices); } - if (this.bar != null) { - builder.writeKey("bar"); - this.bar.serialize(builder, mapper); - } + // ------------------------------------------- + // An optional sub-object - builder.writeEnd(); - } - - //=========================================== - // Builder - //=========================================== - - public static Builder builder() { - return new Builder(); - } - - // Constructor with required fields - public static Builder builder(String name, int size) { - Builder result = new Builder(); - result.name(name); - result.size(size); - return result; - } - - public static class Builder implements ObjectBuilder { - private String name; - private String routing; - private int value; - private boolean value$isSet = false; - private Integer size; - private List indices; - private Bar bar; - - public final Builder name(@Nonnull String v) { - this.name = requireNonNull(v); - return this; - } + private final Bar bar; - public final Builder routing(@Nullable String v) { - this.routing = v; - return this; + @Nonnull + public Optional bar() { + return Optional.ofNullable(this.bar); } - public final Builder value(int v) { - this.value = v; - this.value$isSet = true; - return this; + public FooOptRequest(Builder builder) { + this.name = builder.name; + this.routing = builder.routing; + this.value = builder.value; + this.size = builder.size == null ? OptionalInt.empty() : OptionalInt.of(builder.size); + this.indices = builder.indices; + this.bar = builder.bar; } - public final Builder size(@Nullable Integer v) { - this.size = v; - return this; + // =========================================== + // Serialization + // =========================================== + + @Override + public void serialize(JsonGenerator builder, JsonpMapper mapper) { + builder.writeStartObject(); + + // Classic approach is to use the deserialization field's preferred name: + // + // if (buckets.isPresent()) builder.field(INT_FIELD.getPreferredName(), buckets.getAsInt()); + // + // In a generated code context we can use the string constant, which also avoids declaring ParseField if we + // only serialize (e.g. requests) + + // required field + builder.write("name", this.name); + + // optional field + if (this.routing != null) builder.write("routing", this.routing); + + // required field + builder.write("value", value); + + // optional field + if (this.size.isPresent()) builder.write("size", this.size.getAsInt()); + + // We could just call this and let the builder will handle it by itself with: + // + // if (this.indices != null) builder.field("indices", this.indices); + // + // But the logic is a bit involved (like checking circular dependencies) so we use a more direct implementation: + if (this.indices != null) { + builder.writeKey("indices").writeStartArray(); + for (String str : this.indices) + builder.write(str); + builder.writeEnd(); + } + + if (this.bar != null) { + builder.writeKey("bar"); + this.bar.serialize(builder, mapper); + } + + builder.writeEnd(); } - public final Builder indices(@Nullable List v) { - indices = v; - return this; - } + // =========================================== + // Builder + // =========================================== - // Convenience variant with varargs - public final Builder indices(String... v) { - indices = Arrays.asList(v); - return this; + public static Builder builder() { + return new Builder(); } - public Builder bar(@Nullable Bar v) { - bar = v; - return this; + // Constructor with required fields + public static Builder builder(String name, int size) { + Builder result = new Builder(); + result.name(name); + result.size(size); + return result; } - public Builder bar(@Nullable Bar.Builder v) { - bar = v.build(); - return this; + public static class Builder implements ObjectBuilder { + private String name; + private String routing; + private int value; + private boolean value$isSet = false; + private Integer size; + private List indices; + private Bar bar; + + public final Builder name(@Nonnull String v) { + this.name = requireNonNull(v); + return this; + } + + public final Builder routing(@Nullable String v) { + this.routing = v; + return this; + } + + public final Builder value(int v) { + this.value = v; + this.value$isSet = true; + return this; + } + + public final Builder size(@Nullable Integer v) { + this.size = v; + return this; + } + + public final Builder indices(@Nullable List v) { + indices = v; + return this; + } + + // Convenience variant with varargs + public final Builder indices(String... v) { + indices = Arrays.asList(v); + return this; + } + + public Builder bar(@Nullable Bar v) { + bar = v; + return this; + } + + public Builder bar(@Nullable Bar.Builder v) { + bar = v.build(); + return this; + } + + public FooOptRequest build() { + requireNonNull(this.name, "Name was not set"); + requireTrue(this.value$isSet, "Value was not set"); + return new FooOptRequest(this); + } } - public FooOptRequest build() { - requireNonNull(this.name, "Name was not set"); - requireTrue(this.value$isSet, "Value was not set"); - return new FooOptRequest(this); - } - } - - //=========================================== - // Deserialization - //=========================================== - -// // Only expose the most general interface to hide implementation details that may change over time. -// public static final ContextParser PARSER; -// -// static { -// ObjectParser op = -// new ObjectParser<>(FooOptRequest.class.getName(), FooOptRequest.Builder::new); -// -// op.declareString(Builder::name, new ParseField("name")); -// op.declareString(Builder::routing, new ParseField("routing")); -// -// op.declareInt(Builder::value, new ParseField("value")); -// op.declareInt(Builder::size, new ParseField("size")); -// -// op.declareStringArray(Builder::indices, new ParseField("indices")); -// op.declareObject(Builder::bar, Bar.PARSER, new ParseField("bar")); -// -// PARSER = new ObjectBuilderParser<>(op); -// } - - public static final Endpoint ENDPOINT = - new SimpleEndpoint( - r -> "POST", - r -> "/foo", - SimpleEndpoint.emptyMap(), - SimpleEndpoint.emptyMap(), - true, - FooResponse.PARSER + // =========================================== + // Deserialization + // =========================================== + + // // Only expose the most general interface to hide implementation details that may change over time. + // public static final ContextParser PARSER; + // + // static { + // ObjectParser op = + // new ObjectParser<>(FooOptRequest.class.getName(), FooOptRequest.Builder::new); + // + // op.declareString(Builder::name, new ParseField("name")); + // op.declareString(Builder::routing, new ParseField("routing")); + // + // op.declareInt(Builder::value, new ParseField("value")); + // op.declareInt(Builder::size, new ParseField("size")); + // + // op.declareStringArray(Builder::indices, new ParseField("indices")); + // op.declareObject(Builder::bar, Bar.PARSER, new ParseField("bar")); + // + // PARSER = new ObjectBuilderParser<>(op); + // } + + public static final Endpoint ENDPOINT = new SimpleEndpoint( + r -> "POST", + r -> "/foo", + SimpleEndpoint.emptyMap(), + SimpleEndpoint.emptyMap(), + true, + FooResponse.PARSER ) { - }; + }; - public static void requireTrue(boolean v, String message) { - if (!v) throw new IllegalStateException(message); - } + public static void requireTrue(boolean v, String message) { + if (!v) throw new IllegalStateException(message); + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooRequest.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooRequest.java index 6a82a795d3..a23c291c62 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooRequest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooRequest.java @@ -32,295 +32,293 @@ package org.opensearch.client.opensearch.experiments.api; -import org.opensearch.client.opensearch._types.ErrorResponse; -import org.opensearch.client.opensearch.experiments.api.query2.Query; +import static java.util.Objects.requireNonNull; + +import jakarta.json.stream.JsonGenerator; +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.opensearch._types.ErrorResponse; +import org.opensearch.client.opensearch.experiments.api.query2.Query; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.endpoints.SimpleEndpoint; import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Arrays; -import java.util.List; -import java.util.function.Function; - -import static java.util.Objects.requireNonNull; // Implementing ToXContent is optional, only if there's a request body public class FooRequest implements JsonpSerializable { - //=========================================== - // Fields and getters - //=========================================== - - //------------------------------------------- - // A required String - - private final String name; - - @Nonnull - public final String name() { - return this.name; - } - - //------------------------------------------- - // An optional String + // =========================================== + // Fields and getters + // =========================================== - private final String routing; + // ------------------------------------------- + // A required String - @Nullable - public final String routing() { - return this.routing; - } + private final String name; - //------------------------------------------- - // A required primitive type - - private final int value; - - public int value() { - return this.value; - } + @Nonnull + public final String name() { + return this.name; + } - //------------------------------------------- - // An optional primitive type, represented as a nullable boxed value + // ------------------------------------------- + // An optional String - private final Integer size; + private final String routing; - /** - * Description for field {@code size} - */ - @Nullable - public final Integer size() { - return this.size; - } + @Nullable + public final String routing() { + return this.routing; + } - //------------------------------------------- - // An optional array + // ------------------------------------------- + // A required primitive type - private final List indices; + private final int value; - /** - * A string[] property - */ - @Nullable - public final List indices() { - return this.indices; - } + public int value() { + return this.value; + } - //------------------------------------------- - // An optional sub-object + // ------------------------------------------- + // An optional primitive type, represented as a nullable boxed value - private final Bar bar; + private final Integer size; - @Nullable - public Bar bar() { - return this.bar; - } + /** + * Description for field {@code size} + */ + @Nullable + public final Integer size() { + return this.size; + } - //------------------------------------------- - // An optional query + // ------------------------------------------- + // An optional array - private final Query query; + private final List indices; - @Nullable - public Query query() { - return this.query; - } + /** + * A string[] property + */ + @Nullable + public final List indices() { + return this.indices; + } - //------------------------------------------- + // ------------------------------------------- + // An optional sub-object - public FooRequest(Builder builder) { - this.name = builder.name; - this.routing = builder.routing; - this.value = builder.value; - this.size = builder.size; - this.indices = builder.indices; - this.bar = builder.bar; - this.query = builder.query; - } + private final Bar bar; + @Nullable + public Bar bar() { + return this.bar; + } - //=========================================== - // Builder - //=========================================== + // ------------------------------------------- + // An optional query - public static class Builder implements ObjectBuilder { - protected String name; - protected String routing; - protected int value; - protected boolean value$isSet = false; - protected Integer size; - protected List indices; - protected Bar bar; - protected Query query; + private final Query query; - public final Builder name(@Nonnull String v) { - this.name = requireNonNull(v); - return this; + @Nullable + public Query query() { + return this.query; } - public final Builder routing(String v) { - this.routing = v; - return this; - } + // ------------------------------------------- - public final Builder value(int v) { - this.value = v; - this.value$isSet = true; - return this; + public FooRequest(Builder builder) { + this.name = builder.name; + this.routing = builder.routing; + this.value = builder.value; + this.size = builder.size; + this.indices = builder.indices; + this.bar = builder.bar; + this.query = builder.query; } - public final Builder size(@Nullable Integer v) { - this.size = v; - return this; + // =========================================== + // Builder + // =========================================== + + public static class Builder implements ObjectBuilder { + protected String name; + protected String routing; + protected int value; + protected boolean value$isSet = false; + protected Integer size; + protected List indices; + protected Bar bar; + protected Query query; + + public final Builder name(@Nonnull String v) { + this.name = requireNonNull(v); + return this; + } + + public final Builder routing(String v) { + this.routing = v; + return this; + } + + public final Builder value(int v) { + this.value = v; + this.value$isSet = true; + return this; + } + + public final Builder size(@Nullable Integer v) { + this.size = v; + return this; + } + + public final Builder indices(@Nullable List v) { + indices = v; + return this; + } + + // Convenience variant with varargs + public final Builder indices(String... v) { + indices = Arrays.asList(v); + return this; + } + + public Builder bar(@Nullable Bar v) { + bar = v; + return this; + } + + public Builder bar(Function> b) { + return bar(b.apply(new Bar.Builder()).build()); + } + + public Builder query(Query v) { + this.query = v; + return this; + } + + public Builder query(Function> b) { + return query(b.apply(new Query.Builder()).build()); + } + + public FooRequest build() { + requireNonNull(this.name, "'name' was not set"); + requireTrue(this.value$isSet, "'value' was not set"); + return new FooRequest(this); + } } - public final Builder indices(@Nullable List v) { - indices = v; - return this; + public static Builder builder() { + return new Builder(); } - // Convenience variant with varargs - public final Builder indices(String... v) { - indices = Arrays.asList(v); - return this; + // Constructor with required fields + public static Builder builder(String name, int size) { + Builder result = new Builder(); + result.name(name); + result.size(size); + return result; } - public Builder bar(@Nullable Bar v) { - bar = v; - return this; + // =========================================== + // Serialization + // =========================================== + + @Override + public void serialize(JsonGenerator builder, JsonpMapper mapper) { + builder.writeStartObject(); + + // Classic approach is to use the deserialization field's preferred name: + // + // if (buckets.isPresent()) builder.field(INT_FIELD.getPreferredName(), buckets.getAsInt()); + // + // In a generated code context we can use the string constant, which also avoids declaring ParseField if we + // only serialize (e.g. requests) + + // required field + builder.write("name", this.name); + + // optional field + if (this.routing != null) builder.write("routing", this.routing); + + // required field + builder.write("value", value); + + // optional field + if (this.size != null) builder.write("size", this.size.intValue()); + + // We could just call this and let the builder will handle it by itself with: + // + // if (this.indices != null) builder.field("indices", this.indices); + // + // But the logic is a bit involved (like checking circular dependencies) so we use a more direct implementation: + if (this.indices != null) { + builder.writeKey("indices").writeStartArray(); + for (String str : this.indices) + builder.write(str); + builder.writeEnd(); + } + + if (this.bar != null) { + builder.writeKey("bar"); + this.bar.serialize(builder, mapper); + } + + if (this.query != null) { + builder.writeKey("query"); + this.query.serialize(builder, mapper); + } + + builder.writeEnd(); } - public Builder bar(Function> b) { - return bar(b.apply(new Bar.Builder()).build()); - } + // =========================================== + // Deserialization + // (formally not needed for requests) + // =========================================== - public Builder query(Query v) { - this.query = v; - return this; - } + private static final JsonpDeserializer PARSER; - public Builder query(Function> b) { - return query(b.apply(new Query.Builder()).build()); + public static JsonpDeserializer parser() { + return PARSER; } - public FooRequest build() { - requireNonNull(this.name, "'name' was not set"); - requireTrue(this.value$isSet, "'value' was not set"); - return new FooRequest(this); - } - } - - public static Builder builder() { - return new Builder(); - } - - // Constructor with required fields - public static Builder builder(String name, int size) { - Builder result = new Builder(); - result.name(name); - result.size(size); - return result; - } - - //=========================================== - // Serialization - //=========================================== - - @Override - public void serialize(JsonGenerator builder, JsonpMapper mapper) { - builder.writeStartObject(); - - // Classic approach is to use the deserialization field's preferred name: - // - // if (buckets.isPresent()) builder.field(INT_FIELD.getPreferredName(), buckets.getAsInt()); - // - // In a generated code context we can use the string constant, which also avoids declaring ParseField if we - // only serialize (e.g. requests) - - // required field - builder.write("name", this.name); - - // optional field - if (this.routing != null) builder.write("routing", this.routing); - - // required field - builder.write("value", value); - - // optional field - if (this.size != null) builder.write("size", this.size.intValue()); - - // We could just call this and let the builder will handle it by itself with: - // - // if (this.indices != null) builder.field("indices", this.indices); - // - // But the logic is a bit involved (like checking circular dependencies) so we use a more direct implementation: - if (this.indices != null) { - builder.writeKey("indices").writeStartArray(); - for (String str : this.indices) builder.write(str); - builder.writeEnd(); - } + static { + ObjectDeserializer op = new ObjectDeserializer<>(Builder::new); - if (this.bar != null) { - builder.writeKey("bar"); - this.bar.serialize(builder, mapper); - } + op.add(Builder::name, JsonpDeserializer.stringDeserializer(), "name"); + op.add(Builder::routing, JsonpDeserializer.stringDeserializer(), "routing"); + op.add(Builder::value, "value"); // primitive type + op.add(Builder::size, "size"); + op.add(Builder::indices, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "indices"); + op.add(Builder::bar, Bar.parser(), "bar"); + op.add(Builder::query, Query.parser(), "query"); - if (this.query != null) { - builder.writeKey("query"); - this.query.serialize(builder, mapper); + PARSER = new ObjectBuilderDeserializer<>(op); } - builder.writeEnd(); - } - - //=========================================== - // Deserialization - // (formally not needed for requests) - //=========================================== - - private static final JsonpDeserializer PARSER; - - public static JsonpDeserializer parser() { - return PARSER; - } - - static { - ObjectDeserializer op = new ObjectDeserializer<>(Builder::new); - - op.add(Builder::name, JsonpDeserializer.stringDeserializer(), "name"); - op.add(Builder::routing, JsonpDeserializer.stringDeserializer(), "routing"); - op.add(Builder::value, "value"); // primitive type - op.add(Builder::size, "size"); - op.add(Builder::indices, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "indices"); - op.add(Builder::bar, Bar.parser(), "bar"); - op.add(Builder::query, Query.parser(), "query"); - - PARSER = new ObjectBuilderDeserializer<>(op); - } - - //=========================================== - // Endpoint - //=========================================== - - public static final Endpoint ENDPOINT = - new SimpleEndpoint<>( - r -> "POST", - r -> "/foo", - SimpleEndpoint.emptyMap(), - SimpleEndpoint.emptyMap(), - true, - FooResponse.PARSER + // =========================================== + // Endpoint + // =========================================== + + public static final Endpoint ENDPOINT = new SimpleEndpoint<>( + r -> "POST", + r -> "/foo", + SimpleEndpoint.emptyMap(), + SimpleEndpoint.emptyMap(), + true, + FooResponse.PARSER ); - public static void requireTrue(boolean v, String message) { - if (!v) throw new IllegalStateException(message); - } + public static void requireTrue(boolean v, String message) { + if (!v) throw new IllegalStateException(message); + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooResponse.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooResponse.java index 8b99bec059..fdc2f6aa0c 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooResponse.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/FooResponse.java @@ -36,5 +36,5 @@ public class FooResponse { - public static JsonpDeserializer PARSER = null; + public static JsonpDeserializer PARSER = null; } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/BoolQuery.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/BoolQuery.java index 2f2644d454..7897648ccf 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/BoolQuery.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/BoolQuery.java @@ -32,18 +32,17 @@ package org.opensearch.client.opensearch.experiments.api.query2; +import jakarta.json.stream.JsonGenerator; +import java.util.ArrayList; +import java.util.Collection; +import java.util.function.Function; +import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; - -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.function.Function; public class BoolQuery implements Query.Variant, JsonpSerializable { @@ -81,24 +80,23 @@ public void serialize(JsonGenerator builder, JsonpMapper mapper) { if (should != null) { builder.writeKey("should"); builder.writeStartArray(); - for (Query v: should) v.serialize(builder, mapper); + for (Query v : should) + v.serialize(builder, mapper); builder.writeEnd(); } if (should != null) { builder.writeKey("must"); builder.writeStartArray(); - for (Query v: must) v.serialize(builder, mapper); + for (Query v : must) + v.serialize(builder, mapper); builder.writeEnd(); } if (minimumShouldMatch != null) { builder.writeKey("minimum_should_match"); // Unions have to be expanded inline as serialization depends on the value type. - this.minimumShouldMatch.serialize(builder, mapper, - (v, b, p) -> builder.write(v), - (v, b, p) -> builder.write(v) - ); + this.minimumShouldMatch.serialize(builder, mapper, (v, b, p) -> builder.write(v), (v, b, p) -> builder.write(v)); } builder.writeEnd(); @@ -175,10 +173,11 @@ public static JsonpDeserializer parser() { op.add(Builder::must, JsonpDeserializer.arrayDeserializer(Query.parser()), "must"); op.add(Builder::should, JsonpDeserializer.arrayDeserializer(Query.parser()), "should"); - op.add(Builder::minimumShouldMatch, new Union2.JsonpParser<>( - JsonpDeserializer.integerDeserializer(), - JsonpDeserializer.stringDeserializer() - ), "minimum_should_match"); + op.add( + Builder::minimumShouldMatch, + new Union2.JsonpParser<>(JsonpDeserializer.integerDeserializer(), JsonpDeserializer.stringDeserializer()), + "minimum_should_match" + ); PARSER = new ObjectBuilderDeserializer<>(op); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/Query.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/Query.java index 762febb710..617d6668a0 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/Query.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/Query.java @@ -32,21 +32,20 @@ package org.opensearch.client.opensearch.experiments.api.query2; -import org.opensearch.client.opensearch.experiments.UnionVariant; +import jakarta.json.JsonValue; +import jakarta.json.stream.JsonGenerator; +import java.util.Map; +import java.util.function.Function; import org.opensearch.client.json.BuildFunctionDeserializer; import org.opensearch.client.json.JsonEnum; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.opensearch.experiments.UnionVariant; import org.opensearch.client.util.ObjectBuilder; import org.opensearch.client.util.TaggedUnion; import org.opensearch.client.util.TaggedUnionUtils; -import jakarta.json.JsonValue; -import jakarta.json.stream.JsonGenerator; - -import java.util.Map; -import java.util.function.Function; public class Query implements TaggedUnion, JsonpSerializable { @@ -127,7 +126,7 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) { } if (meta != null) { generator.writeStartObject("meta"); - for (Map.Entry e: meta.entrySet()) { + for (Map.Entry e : meta.entrySet()) { generator.write(e.getKey(), e.getValue()); } generator.writeEnd(); @@ -214,5 +213,4 @@ public static JsonpDeserializer parser() { return PARSER; } - } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/QueryTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/QueryTest.java index f7e51e4a70..6405ad3d44 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/QueryTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/QueryTest.java @@ -32,11 +32,10 @@ package org.opensearch.client.opensearch.experiments.api.query2; +import java.util.Collection; import org.junit.Assert; import org.junit.Test; -import java.util.Collection; - public class QueryTest extends Assert { @Test public void testQuery() { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/TermsQuery.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/TermsQuery.java index f953a040cb..fd1cbd724c 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/TermsQuery.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/TermsQuery.java @@ -32,13 +32,13 @@ package org.opensearch.client.opensearch.experiments.api.query2; +import jakarta.json.stream.JsonGenerator; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; public class TermsQuery implements JsonpSerializable, Query.Variant { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/Union2.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/Union2.java index 0f7443b73f..bc9cab27c2 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/Union2.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/api/query2/Union2.java @@ -32,14 +32,14 @@ package org.opensearch.client.opensearch.experiments.api.query2; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.JsonpDeserializerBase; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.util.TriConsumer; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParser.Event; import jakarta.json.stream.JsonParsingException; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpDeserializerBase; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.util.TriConsumer; /** * A tagged union (also known as variant, sum type or coproduct) with two members. @@ -56,112 +56,113 @@ public class Union2 { - /** - * The variant tag. - */ - public enum Tag{A, B}; - - private final Tag tag; - private final Object value; - - private Union2(Tag tag, Object value) { - this.tag = tag; - this.value = value; - } - - /** - * Creates a variant {@code A}. - */ - public static Union2 ofA(A value) { - return new Union2<>(Tag.A, value); - } - - /** - * Creates a variant {@code B}. - */ - public static Union2 ofB(B value) { - return new Union2<>(Tag.B, value); - } - - /** - * Get the variant's tag of this union. - * - * @return the variant's tag - */ - public Tag tag() { - return tag; - } - - /** - * Get the variant {@code A} of this union. - * - * @return the variant's value - * @throws IllegalStateException if the union is not holding a variant {@code A} - */ - public A a() { - return getVariant(Tag.A); - } - - /** - * Get the variant {@code B} of this union. - * - * @return the variant's value - * @throws IllegalStateException if the union is not holding a variant {@code B} - */ - public B b() { - return getVariant(Tag.B); - } - - private T getVariant(Tag tag) { - if (this.tag != tag) throw new IllegalStateException("Union holds variant " + tag); - - @SuppressWarnings("unchecked") - T result = (T)value; - - return result; - } - - //----------------------------------------------------------------------------------------------- - // Serialization / deserialization - - public void serialize( - JsonGenerator builder, JsonpMapper params, - TriConsumer a, - TriConsumer b - ) { - switch (this.tag) { - case A: - a.accept(this.a(), builder, params); - case B: - b.accept(this.b(), builder, params); + /** + * The variant tag. + */ + public enum Tag { + A, + B + }; + + private final Tag tag; + private final Object value; + + private Union2(Tag tag, Object value) { + this.tag = tag; + this.value = value; + } + + /** + * Creates a variant {@code A}. + */ + public static Union2 ofA(A value) { + return new Union2<>(Tag.A, value); + } + + /** + * Creates a variant {@code B}. + */ + public static Union2 ofB(B value) { + return new Union2<>(Tag.B, value); + } + + /** + * Get the variant's tag of this union. + * + * @return the variant's tag + */ + public Tag tag() { + return tag; + } + + /** + * Get the variant {@code A} of this union. + * + * @return the variant's value + * @throws IllegalStateException if the union is not holding a variant {@code A} + */ + public A a() { + return getVariant(Tag.A); + } + + /** + * Get the variant {@code B} of this union. + * + * @return the variant's value + * @throws IllegalStateException if the union is not holding a variant {@code B} + */ + public B b() { + return getVariant(Tag.B); } - } - public static class JsonpParser extends JsonpDeserializerBase> { + private T getVariant(Tag tag) { + if (this.tag != tag) throw new IllegalStateException("Union holds variant " + tag); - private final JsonpDeserializer parserA; - private final JsonpDeserializer parserB; + @SuppressWarnings("unchecked") + T result = (T) value; - public JsonpParser( - JsonpDeserializer parserA, - JsonpDeserializer parserB + return result; + } + + // ----------------------------------------------------------------------------------------------- + // Serialization / deserialization + + public void serialize( + JsonGenerator builder, + JsonpMapper params, + TriConsumer a, + TriConsumer b ) { - super(allAcceptedEvents(parserA, parserB)); - this.parserA = parserA; - this.parserB = parserB; + switch (this.tag) { + case A: + a.accept(this.a(), builder, params); + case B: + b.accept(this.b(), builder, params); + } } - @Override - public Union2 deserialize(JsonParser parser, JsonpMapper mapper, Event event) { - if (parserA.accepts(event)) { - return Union2.ofA(parserA.deserialize(parser, mapper, event)); + public static class JsonpParser extends JsonpDeserializerBase> { + + private final JsonpDeserializer parserA; + private final JsonpDeserializer parserB; + + public JsonpParser(JsonpDeserializer parserA, JsonpDeserializer parserB) { + super(allAcceptedEvents(parserA, parserB)); + this.parserA = parserA; + this.parserB = parserB; + } + + @Override + public Union2 deserialize(JsonParser parser, JsonpMapper mapper, Event event) { + if (parserA.accepts(event)) { + return Union2.ofA(parserA.deserialize(parser, mapper, event)); - } else if (parserB.accepts(event)) { - return Union2.ofB(parserB.deserialize(parser, mapper, event)); + } else if (parserB.accepts(event)) { + return Union2.ofB(parserB.deserialize(parser, mapper, event)); - } else { - throw new JsonParsingException("Unexpected event '" + event + "'", parser.getLocation()); - } + } else { + throw new JsonParsingException("Unexpected event '" + event + "'", parser.getLocation()); + } + } } - } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/Client.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/Client.java index 164f31cb3d..f2d53f3d5c 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/Client.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/Client.java @@ -32,30 +32,28 @@ package org.opensearch.client.opensearch.experiments.base; +import java.io.IOException; +import java.util.function.Function; import org.opensearch.client.opensearch.experiments.api.FooRequest; import org.opensearch.client.opensearch.experiments.api.FooResponse; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.Transport; import org.opensearch.client.util.ObjectBuilder; -import java.io.IOException; -import java.util.function.Function; - public class Client { - private Transport transport; + private Transport transport; - public FooResponse foo(FooRequest request) throws IOException { - return performRequest(request, FooRequest.ENDPOINT); - } + public FooResponse foo(FooRequest request) throws IOException { + return performRequest(request, FooRequest.ENDPOINT); + } - public FooResponse foo(Function> b) throws IOException { - return foo(b.apply(new FooRequest.Builder()).build()); - } + public FooResponse foo(Function> b) throws IOException { + return foo(b.apply(new FooRequest.Builder()).build()); + } - protected ResponseT performRequest( - RequestT request, Endpoint endpoint - ) throws IOException { - return transport.performRequest(request, endpoint, null); - } + protected ResponseT performRequest(RequestT request, Endpoint endpoint) + throws IOException { + return transport.performRequest(request, endpoint, null); + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/PrimitiveUtils.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/PrimitiveUtils.java index 6031ef74bf..4278259cae 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/PrimitiveUtils.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/PrimitiveUtils.java @@ -36,19 +36,19 @@ * Support functions for primitive fields in generated classes */ public class PrimitiveUtils { - public static int checkedValue(int value, boolean isSet) { - mustBeSet(isSet); - return value; - } + public static int checkedValue(int value, boolean isSet) { + mustBeSet(isSet); + return value; + } - public static long checkedValue(long value, boolean isSet) { - mustBeSet(isSet); - return value; - } + public static long checkedValue(long value, boolean isSet) { + mustBeSet(isSet); + return value; + } - private static void mustBeSet(boolean isSet) { - if (!isSet) { - throw new IllegalStateException("Value is not set"); + private static void mustBeSet(boolean isSet) { + if (!isSet) { + throw new IllegalStateException("Value is not set"); + } } - } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/Variants.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/Variants.java index d0a1119556..425b6c83cc 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/Variants.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/base/Variants.java @@ -32,137 +32,134 @@ package org.opensearch.client.opensearch.experiments.base; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.JsonpSerializable; -import org.opensearch.client.json.JsonpUtils; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; import jakarta.json.stream.JsonParsingException; - import java.util.EnumSet; import java.util.HashMap; import java.util.Map; import java.util.function.Function; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.JsonpSerializable; +import org.opensearch.client.json.JsonpUtils; /** * Describes the variants of a transport class, providing their name and parsers. It is representation-agnostic. */ public class Variants { - private Function, Builder> builderFunc; - - private Map, String> variantNames; - private Map> parsers; - - /** - * Builds a new {@code Variants} from a builder-creation function. This approach allows the object to be built lazily, - * which is needed for recursive variant data structures (e.g. a query can be a boolean query that itself contains - * other queries). - * - * This is required because cyclic dependencies between static class initialization code can lead to unexpected - * behaviour (a subclass static init may be called while static init of the parent class is not finished). - */ - public Variants(Function, Builder> b) { - this.builderFunc = b; - } - - public Variants(Builder builder) { - this.variantNames = builder.variantNames; - this.parsers = builder.parsers; - } - - public String variantName(Class clazz) { - build(); - return variantNames.get(clazz); - } - - public JsonpDeserializer variantParser(String name) { - build(); - return parsers.get(name); - } - - private void build() { - if (builderFunc != null) { - Builder builder = builderFunc.apply(new Builder<>()); - this.variantNames = builder.variantNames; - this.parsers = builder.parsers; - this.builderFunc = null; + private Function, Builder> builderFunc; + + private Map, String> variantNames; + private Map> parsers; + + /** + * Builds a new {@code Variants} from a builder-creation function. This approach allows the object to be built lazily, + * which is needed for recursive variant data structures (e.g. a query can be a boolean query that itself contains + * other queries). + * + * This is required because cyclic dependencies between static class initialization code can lead to unexpected + * behaviour (a subclass static init may be called while static init of the parent class is not finished). + */ + public Variants(Function, Builder> b) { + this.builderFunc = b; } - } - //------------------------------------------------------------------------------------------------------------------- + public Variants(Builder builder) { + this.variantNames = builder.variantNames; + this.parsers = builder.parsers; + } - public static Builder builder() { - return new Builder<>(); - } + public String variantName(Class clazz) { + build(); + return variantNames.get(clazz); + } - public static class Builder { - private final Map, String> variantNames = new HashMap<>(); - private final Map> parsers = new HashMap<>(); + public JsonpDeserializer variantParser(String name) { + build(); + return parsers.get(name); + } - public Builder add(String name, Class clazz, JsonpDeserializer parser) { - variantNames.put(clazz, name); - parsers.put(name, parser); - return this; + private void build() { + if (builderFunc != null) { + Builder builder = builderFunc.apply(new Builder<>()); + this.variantNames = builder.variantNames; + this.parsers = builder.parsers; + this.builderFunc = null; + } } - public Variants build() { - return new Variants<>(this); + // ------------------------------------------------------------------------------------------------------------------- + + public static Builder builder() { + return new Builder<>(); } - } - //------------------------------------------------------------------------------------------------------------------- + public static class Builder { + private final Map, String> variantNames = new HashMap<>(); + private final Map> parsers = new HashMap<>(); + + public Builder add(String name, Class clazz, JsonpDeserializer parser) { + variantNames.put(clazz, name); + parsers.put(name, parser); + return this; + } + + public Variants build() { + return new Variants<>(this); + } + } - /** - * Variant serializer/deserializer using externally tagged JSON. - *

+ * See Enumerations in Serde.rs that clearly explains the + * various strategies to represent polymorphic values in JSON. */ - public static JsonpDeserializer pparser(Variants variants) { - return JsonpDeserializer.of(EnumSet.of(JsonParser.Event.START_OBJECT), (parser, params) -> { - JsonpUtils.expectNextEvent(parser, JsonParser.Event.START_OBJECT); - JsonpUtils.expectNextEvent(parser, JsonParser.Event.KEY_NAME); + public static class ExternallyTagged { - String variant = parser.getString(); - JsonpDeserializer variantParser = variants.variantParser(variant); + /** + * Creates a parser for externally tagged variants + */ + public static JsonpDeserializer pparser(Variants variants) { + return JsonpDeserializer.of(EnumSet.of(JsonParser.Event.START_OBJECT), (parser, params) -> { + JsonpUtils.expectNextEvent(parser, JsonParser.Event.START_OBJECT); + JsonpUtils.expectNextEvent(parser, JsonParser.Event.KEY_NAME); - if (variantParser == null) { - throw new JsonParsingException("Unknown variant '" + variant + "'", parser.getLocation()); - } + String variant = parser.getString(); + JsonpDeserializer variantParser = variants.variantParser(variant); - T result = variantParser.deserialize(parser, params); + if (variantParser == null) { + throw new JsonParsingException("Unknown variant '" + variant + "'", parser.getLocation()); + } - JsonpUtils.expectNextEvent(parser, JsonParser.Event.END_OBJECT); + T result = variantParser.deserialize(parser, params); - return result; - }); - } + JsonpUtils.expectNextEvent(parser, JsonParser.Event.END_OBJECT); - /** - * Serializes an externally tagged variant object - */ + return result; + }); + } + + /** + * Serializes an externally tagged variant object + */ - public static void serialize( - Variants variants, T v, JsonGenerator builder, JsonpMapper mapper - ) { - @SuppressWarnings("unchecked") - String variant = variants.variantName((Class)v.getClass()); + public static void serialize(Variants variants, T v, JsonGenerator builder, JsonpMapper mapper) { + @SuppressWarnings("unchecked") + String variant = variants.variantName((Class) v.getClass()); - if (variant == null) { - throw new IllegalArgumentException("No variant name found for " + v.getClass().getName()); - } + if (variant == null) { + throw new IllegalArgumentException("No variant name found for " + v.getClass().getName()); + } - builder.writeStartObject(); - builder.writeKey(variant); - v.serialize(builder, mapper); - builder.writeEnd(); + builder.writeStartObject(); + builder.writeKey(variant); + v.serialize(builder, mapper); + builder.writeEnd(); + } } - } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnion.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnion.java index 4a2aa4315b..02eb8d405c 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnion.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnion.java @@ -46,6 +46,8 @@ interface VariantB { } */ +import jakarta.json.stream.JsonGenerator; +import java.util.function.Function; import org.opensearch.client.json.JsonEnum; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; @@ -56,9 +58,6 @@ interface VariantB { import org.opensearch.client.util.ObjectBuilder; import org.opensearch.client.util.TaggedUnion; import org.opensearch.client.util.TaggedUnionUtils; -import jakarta.json.stream.JsonGenerator; - -import java.util.function.Function; @JsonpDeserializable public class SomeUnion implements TaggedUnion, JsonpSerializable { @@ -91,7 +90,7 @@ public SomeUnion(Builder builder) { public void serialize(JsonGenerator generator, JsonpMapper mapper) { // Delegate to the variant object if (_value instanceof JsonpSerializable) { - ((JsonpSerializable)_value).serialize(generator, mapper); + ((JsonpSerializable) _value).serialize(generator, mapper); } } @@ -153,8 +152,11 @@ protected SomeUnion build() { } } - public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy(SomeUnion.Builder::new, - SomeUnion::setupSomeUnionDeserializer, SomeUnion.Builder::build); + public static final JsonpDeserializer _DESERIALIZER = ObjectBuilderDeserializer.lazy( + SomeUnion.Builder::new, + SomeUnion::setupSomeUnionDeserializer, + SomeUnion.Builder::build + ); protected static void setupSomeUnionDeserializer(ObjectDeserializer op) { op.add(SomeUnion.Builder::variantA, UVariantA._DESERIALIZER, "variant_a"); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionTest.java index 3342bbb65b..9349566aa3 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionTest.java @@ -38,10 +38,7 @@ public class SomeUnionTest extends ModelTestCase { - SomeUnion su = new SomeUnion.Builder() - .variantA(a_ -> a_ - .name("a-name") - ).build(); + SomeUnion su = new SomeUnion.Builder().variantA(a_ -> a_.name("a-name")).build(); String json = "{\"type\":\"variant_a\",\"name\":\"a-name\"}"; @@ -72,9 +69,7 @@ public void testSerialization() { public void testMissingVariantDeserialization() { String json = "{}"; - JsonParsingException e = assertThrows(JsonParsingException.class, () -> { - fromJson(json, SomeUnion._DESERIALIZER); - }); + JsonParsingException e = assertThrows(JsonParsingException.class, () -> { fromJson(json, SomeUnion._DESERIALIZER); }); assertEquals("Property 'type' not found", e.getMessage()); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionVariant.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionVariant.java index b0461522a6..57f3cc5f6e 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionVariant.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/SomeUnionVariant.java @@ -34,5 +34,4 @@ import org.opensearch.client.opensearch.experiments.UnionVariant; -public interface SomeUnionVariant extends UnionVariant { -} +public interface SomeUnionVariant extends UnionVariant {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/UVariantA.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/UVariantA.java index cd82385af9..a23ed20942 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/UVariantA.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/UVariantA.java @@ -32,6 +32,8 @@ package org.opensearch.client.opensearch.experiments.containers; +import jakarta.json.stream.JsonGenerator; +import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; @@ -39,9 +41,6 @@ import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; - -import javax.annotation.Nullable; @JsonpDeserializable public class UVariantA implements SomeUnionVariant, JsonpSerializable { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/UVariantB.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/UVariantB.java index d2c292e736..25bde79072 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/UVariantB.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/containers/UVariantB.java @@ -32,6 +32,8 @@ package org.opensearch.client.opensearch.experiments.containers; +import jakarta.json.stream.JsonGenerator; +import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; @@ -39,9 +41,6 @@ import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; - -import javax.annotation.Nullable; @JsonpDeserializable public class UVariantB implements SomeUnionVariant, JsonpSerializable { @@ -54,7 +53,7 @@ public String _variantType() { } @Nullable - public Integer number () { + public Integer number() { return this.number; } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/generics/GenericClass.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/generics/GenericClass.java index 15b83578fd..31228322b4 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/generics/GenericClass.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/generics/GenericClass.java @@ -32,7 +32,8 @@ package org.opensearch.client.opensearch.experiments.generics; -import org.opensearch.client.opensearch._types.ErrorResponse; +import jakarta.json.stream.JsonGenerator; +import java.util.function.Supplier; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; @@ -40,12 +41,10 @@ import org.opensearch.client.json.JsonpUtils; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.opensearch._types.ErrorResponse; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.endpoints.SimpleEndpoint; import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; - -import java.util.function.Supplier; public class GenericClass implements JsonpSerializable { @@ -112,7 +111,6 @@ private static void setupParser(ObjectDeserializer> op.add(Builder::genParam, deserializer, "genParam"); } - public static Endpoint, ErrorResponse> endpoint( JsonpDeserializer genParamDeserializer ) { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/InheritanceTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/InheritanceTest.java index 6f1f677844..f80d68e9c2 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/InheritanceTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/InheritanceTest.java @@ -75,7 +75,6 @@ public void testDeserialization() { assertEquals("childValue", fc.childField()); assertEquals("finalValue", fc.finalField()); - json = "{\"baseField\":\"baseValue\",\"childField\":\"childValue\"}"; ChildClass cc = fromJson(json, ChildClass.JSONP_PARSER); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/base/BaseClass.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/base/BaseClass.java index d4a2e8f673..a783c12edc 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/base/BaseClass.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/base/BaseClass.java @@ -32,13 +32,12 @@ package org.opensearch.client.opensearch.experiments.inheritance.base; +import jakarta.json.stream.JsonGenerator; +import java.util.Objects; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.ObjectDeserializer; -import jakarta.json.stream.JsonGenerator; - -import java.util.Objects; /** * An abstract transport class @@ -66,7 +65,7 @@ protected void toJsonpInternal(JsonGenerator generator, JsonpMapper mapper) { generator.write("baseField", this.baseField); } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- // This is class protected as it's only of interest to subclasses. Yet public members are visible // from the outside on public subclasses. @@ -82,7 +81,7 @@ public T baseField(String value) { protected abstract T self(); } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- protected static > void setupBaseClassParser(ObjectDeserializer op) { op.add(AbstractBuilder::baseField, JsonpDeserializer.stringDeserializer(), "baseField"); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/child/ChildClass.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/child/ChildClass.java index 5bb86da7af..6d6016698d 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/child/ChildClass.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/child/ChildClass.java @@ -32,16 +32,15 @@ package org.opensearch.client.opensearch.experiments.inheritance.child; -import org.opensearch.client.opensearch.experiments.inheritance.base.BaseClass; +import jakarta.json.stream.JsonGenerator; +import java.util.Objects; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.JsonpSerializable; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.opensearch.experiments.inheritance.base.BaseClass; import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; - -import java.util.Objects; /** * A concrete class that both inherits a parent class and has child classes @@ -65,7 +64,7 @@ protected void toJsonpInternal(JsonGenerator generator, JsonpMapper params) { generator.write("childField", this.childField); } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- protected abstract static class AbstractBuilder> extends BaseClass.AbstractBuilder { @@ -92,7 +91,7 @@ public ChildClass build() { } } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- protected static > void setupChildClassParser(ObjectDeserializer op) { BaseClass.setupBaseClassParser(op); @@ -100,7 +99,8 @@ protected static > void setupChildClassParser(Objec } public static final JsonpDeserializer JSONP_PARSER = ObjectBuilderDeserializer.createForObject( - Builder::new, ChildClass::setupChildClassParser + Builder::new, + ChildClass::setupChildClassParser ); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/final_/FinalClass.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/final_/FinalClass.java index 63acc8485d..257c7790f0 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/final_/FinalClass.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/inheritance/final_/FinalClass.java @@ -32,15 +32,14 @@ package org.opensearch.client.opensearch.experiments.inheritance.final_; -import org.opensearch.client.opensearch.experiments.inheritance.child.ChildClass; +import jakarta.json.stream.JsonGenerator; +import java.util.Objects; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.opensearch.experiments.inheritance.child.ChildClass; import org.opensearch.client.util.ObjectBuilder; -import jakarta.json.stream.JsonGenerator; - -import java.util.Objects; /** * A concrete class that inherits a parent class but has no child classes @@ -64,11 +63,9 @@ protected void toJsonpInternal(JsonGenerator generator, JsonpMapper mapper) { generator.write("finalField", this.finalField); } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- - public static class Builder - extends ChildClass.AbstractBuilder - implements ObjectBuilder { + public static class Builder extends ChildClass.AbstractBuilder implements ObjectBuilder { private String finalField; @@ -88,7 +85,7 @@ public FinalClass build() { } } - //--------------------------------------------------------------------------------------------- + // --------------------------------------------------------------------------------------------- private static void setupFinalClassParser(ObjectDeserializer op) { ChildClass.setupChildClassParser(op); @@ -105,7 +102,8 @@ private static void setupFinalClassParser(ObjectDeserializer op) { // (even if users should rarely have to interact directly with the parser). public static final JsonpDeserializer JSONP_PARSER = ObjectBuilderDeserializer.createForObject( - Builder::new, FinalClass::setupFinalClassParser + Builder::new, + FinalClass::setupFinalClassParser ); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractAggregationRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractAggregationRequestIT.java index 402792f90f..18c5589f19 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractAggregationRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractAggregationRequestIT.java @@ -8,6 +8,11 @@ package org.opensearch.client.opensearch.integTest; +import java.io.IOException; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.List; import org.junit.Test; import org.opensearch.Version; import org.opensearch.client.opensearch._types.Refresh; @@ -23,255 +28,211 @@ import org.opensearch.client.opensearch.core.InfoResponse; import org.opensearch.client.opensearch.core.SearchResponse; -import java.io.IOException; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.List; - public abstract class AbstractAggregationRequestIT extends OpenSearchJavaClientTestCase { - @Test - public void testValueRangeAggregation() throws Exception { - var index = "test-value-range-aggregation"; - createDateRangeDocuments(index); - var searchResponse = sendAggregateRequest(index, "cost_ranges", getCostValueRangeAggregation()); - var costRangesAggregations = searchResponse.aggregations().get("cost_ranges"); - var buckets = costRangesAggregations._get() - ._toAggregate() - .range() - .buckets() - .array(); - - assertEquals(3, buckets.size()); - assertEquals(2, buckets.get(0).docCount()); - assertEquals(2, buckets.get(1).docCount()); - assertEquals(2, buckets.get(2).docCount()); - } - - @Test - public void testDateRangeAggregation() throws Exception { - var index = "test-date-range-aggregation"; - createDateRangeDocuments(index); - var searchResponse = sendAggregateRequest(index, "expiry_ranges", getExpiryDateRangeAggregation()); - var expiryRangesAggregations = searchResponse.aggregations().get("expiry_ranges"); - var buckets = expiryRangesAggregations._get() - ._toAggregate() - .dateRange() - .buckets() - .array(); - - assertEquals(3, buckets.size()); - assertEquals(2, buckets.get(0).docCount()); - assertEquals(2, buckets.get(1).docCount()); - assertEquals(2, buckets.get(2).docCount()); - } - - @Test - public void testMultiTermsAggregation() throws Exception { - checkIfOpenSearchSupportsMultiTermsAggregation(); - - var index = "test-multiterms-aggregation-no-size"; - createMultiTermsDocuments(index); - var searchResponse = sendAggregateRequest(index, "multiterms", getMultiTermsAggregation(null)); - var multitermsAggregations = searchResponse.aggregations().get("multiterms"); - var buckets = multitermsAggregations._get() - ._toAggregate() - .multiTerms() - .buckets() - .array(); - - assertEquals(3, buckets.size()); - assertEquals(1, buckets.get(0).docCount()); - } - - @Test - public void testMultiTermsAggregationWithSizeFewerThenBucketsSize() throws Exception { - checkIfOpenSearchSupportsMultiTermsAggregation(); - - var index = "test-multiterms-aggregation-fewer-size"; - createMultiTermsDocuments(index); - var searchResponse = sendAggregateRequest(index, "multiterms", getMultiTermsAggregation(1)); - var multitermsAggregations = searchResponse.aggregations().get("multiterms"); - var buckets = multitermsAggregations._get() - ._toAggregate() - .multiTerms() - .buckets() - .array(); - - assertEquals(1, buckets.size()); - assertEquals(1, buckets.get(0).docCount()); - } - - @Test - public void testMultiTermsAggregationWithSizeBiggerThenBucketsSize() throws Exception { - checkIfOpenSearchSupportsMultiTermsAggregation(); - - var index = "test-multiterms-aggregation-bigger-size"; - createMultiTermsDocuments(index); - var searchResponse = sendAggregateRequest(index, "multiterms", getMultiTermsAggregation(50)); - var multitermsAggregations = searchResponse.aggregations().get("multiterms"); - var buckets = multitermsAggregations._get() - ._toAggregate() - .multiTerms() - .buckets() - .array(); - - assertEquals(3, buckets.size()); - assertEquals(1, buckets.get(0).docCount()); - } - - private void checkIfOpenSearchSupportsMultiTermsAggregation() throws Exception { - InfoResponse info = javaClient().info(); - String version = info.version().number(); - if (version.contains("SNAPSHOT")) { - version = version.split("-")[0]; - } - assumeTrue("multi_terms is supported in OpenSearch 2.1.0 and later", - Version.fromString(version).onOrAfter(Version.fromString("2.1.0"))); - } - - private Aggregation getExpiryDateRangeAggregation() { - DateRangeAggregation expiryDateRangeAggregation = new DateRangeAggregation.Builder() - .field("expDate") - .ranges(getDateAggregationRanges()) - .build(); - return new Aggregation.Builder().dateRange(expiryDateRangeAggregation).build(); - } - - private Aggregation getCostValueRangeAggregation() { - RangeAggregation costValueRangeAggregation = new RangeAggregation.Builder() - .field("cost") - .ranges(getValueAggregationRanges()) - .build(); - return new Aggregation.Builder().range(costValueRangeAggregation).build(); - } - - private Aggregation getMultiTermsAggregation(Integer size) { - MultiTermsAggregation.Builder multiTermsAggregationBuilder = new MultiTermsAggregation.Builder() - .terms(List.of( - MultiTermLookup.of(multiTermLookup -> multiTermLookup.field("cost")), - MultiTermLookup.of(multiTermLookup -> multiTermLookup.field("expDate")) - )); - - if(size != null) { - multiTermsAggregationBuilder = multiTermsAggregationBuilder.size(size); - } - - MultiTermsAggregation multiTermsAggregation = multiTermsAggregationBuilder.build(); - return new Aggregation.Builder().multiTerms(multiTermsAggregation).build(); - } - - private SearchResponse sendAggregateRequest(String index, String key, Aggregation value) throws IOException { - return javaClient().search( - request -> request.index(index) - .size(0) - .aggregations(key, value), - Void.class); - } - - private List getDateAggregationRanges() { - return List.of( - new DateRangeExpression.Builder() - .from(builder -> builder.value((double) getDatePlusDays(1).toEpochMilli())) - .to(FieldDateMath.of( - builder -> builder.value((double) getDatePlusDays(3).toEpochMilli() - 1000)) - ) - .key("from-1-to-2-days") - .build(), - new DateRangeExpression.Builder() - .from(builder -> builder.value((double) getDatePlusDays(3).toEpochMilli())) - .to(FieldDateMath.of( - builder -> builder.value((double) getDatePlusDays(5).toEpochMilli() - 1000)) - ) - .key("from-3-to-4-days") - .build(), - new DateRangeExpression.Builder() - .from(builder -> builder.value((double) getDatePlusDays(5).toEpochMilli())) - .to(FieldDateMath.of( - builder -> builder.value((double) getDatePlusDays(7).toEpochMilli() - 1000)) - ) - .key("from-5-to-6-days") - .build() - ); - } - - private List getValueAggregationRanges() { - return List.of( - new AggregationRange.Builder().to("10").build(), - new AggregationRange.Builder().from("10").to("30").build(), - new AggregationRange.Builder().from("30").build() - ); - } - - private void createDateRangeDocuments(String index) throws IOException { - createIndex(index); - javaClient().create(_1 -> _1.index(index).id("1").document(createProduct("egg", 2, 1)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("2").document(createProduct("meat", 15, 2)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("3").document(createProduct("ham", 30, 3)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("4").document(createProduct("cheese", 25, 4)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("5").document(createProduct("pasta", 8, 5)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("6").document(createProduct("oil", 50, 6)).refresh(Refresh.True)); - } - - private void createMultiTermsDocuments(String index) throws IOException { - createIndex(index); - javaClient().create(_1 -> _1.index(index).id("1").document(createProduct("appleA", 2, 1)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("2").document(createProduct("appleB", 2, 2)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("3").document(createProduct("appleC", 2, 3)).refresh(Refresh.True)); - } - - private void createIndex(String index) throws IOException { - Property nameValueProp = new Property.Builder() - .text(v -> v) - .build(); - Property costValueProp = new Property.Builder() - .integer(v -> v) - .build(); - Property expDateKeywordProp = new Property.Builder() - .date(v -> v) - .build(); - javaClient().indices().create(c -> c.index(index) - .mappings(m -> m.properties("name", nameValueProp) - .properties("cost", costValueProp) - .properties("expDate", expDateKeywordProp) - ) - ); - } - - private ProductDetails createProduct(String name, int cost, int plusDays) { - return new ProductDetails(name, cost, getDatePlusDays(plusDays).toEpochMilli()); - } - - private Instant getDatePlusDays(int plusDays) { - return LocalDateTime.of(2023, 2, 20, 0, 0, 0, 0).plusDays(plusDays).toInstant(ZoneOffset.UTC); - } - - public static class ProductDetails { - private String name; - private int cost; - private Long expDate; - - public ProductDetails() { - } - - public ProductDetails(String name, int cost, Long expDate) { - this.name = name; - this.cost = cost; - this.expDate = expDate; - } - - public String getName() { - return name; - } - - public int getCost() { - return cost; - } - - public Long getExpDate() { - return expDate; - } - } + @Test + public void testValueRangeAggregation() throws Exception { + var index = "test-value-range-aggregation"; + createDateRangeDocuments(index); + var searchResponse = sendAggregateRequest(index, "cost_ranges", getCostValueRangeAggregation()); + var costRangesAggregations = searchResponse.aggregations().get("cost_ranges"); + var buckets = costRangesAggregations._get()._toAggregate().range().buckets().array(); + + assertEquals(3, buckets.size()); + assertEquals(2, buckets.get(0).docCount()); + assertEquals(2, buckets.get(1).docCount()); + assertEquals(2, buckets.get(2).docCount()); + } + + @Test + public void testDateRangeAggregation() throws Exception { + var index = "test-date-range-aggregation"; + createDateRangeDocuments(index); + var searchResponse = sendAggregateRequest(index, "expiry_ranges", getExpiryDateRangeAggregation()); + var expiryRangesAggregations = searchResponse.aggregations().get("expiry_ranges"); + var buckets = expiryRangesAggregations._get()._toAggregate().dateRange().buckets().array(); + + assertEquals(3, buckets.size()); + assertEquals(2, buckets.get(0).docCount()); + assertEquals(2, buckets.get(1).docCount()); + assertEquals(2, buckets.get(2).docCount()); + } + + @Test + public void testMultiTermsAggregation() throws Exception { + checkIfOpenSearchSupportsMultiTermsAggregation(); + + var index = "test-multiterms-aggregation-no-size"; + createMultiTermsDocuments(index); + var searchResponse = sendAggregateRequest(index, "multiterms", getMultiTermsAggregation(null)); + var multitermsAggregations = searchResponse.aggregations().get("multiterms"); + var buckets = multitermsAggregations._get()._toAggregate().multiTerms().buckets().array(); + + assertEquals(3, buckets.size()); + assertEquals(1, buckets.get(0).docCount()); + } + + @Test + public void testMultiTermsAggregationWithSizeFewerThenBucketsSize() throws Exception { + checkIfOpenSearchSupportsMultiTermsAggregation(); + + var index = "test-multiterms-aggregation-fewer-size"; + createMultiTermsDocuments(index); + var searchResponse = sendAggregateRequest(index, "multiterms", getMultiTermsAggregation(1)); + var multitermsAggregations = searchResponse.aggregations().get("multiterms"); + var buckets = multitermsAggregations._get()._toAggregate().multiTerms().buckets().array(); + + assertEquals(1, buckets.size()); + assertEquals(1, buckets.get(0).docCount()); + } + + @Test + public void testMultiTermsAggregationWithSizeBiggerThenBucketsSize() throws Exception { + checkIfOpenSearchSupportsMultiTermsAggregation(); + + var index = "test-multiterms-aggregation-bigger-size"; + createMultiTermsDocuments(index); + var searchResponse = sendAggregateRequest(index, "multiterms", getMultiTermsAggregation(50)); + var multitermsAggregations = searchResponse.aggregations().get("multiterms"); + var buckets = multitermsAggregations._get()._toAggregate().multiTerms().buckets().array(); + + assertEquals(3, buckets.size()); + assertEquals(1, buckets.get(0).docCount()); + } + + private void checkIfOpenSearchSupportsMultiTermsAggregation() throws Exception { + InfoResponse info = javaClient().info(); + String version = info.version().number(); + if (version.contains("SNAPSHOT")) { + version = version.split("-")[0]; + } + assumeTrue( + "multi_terms is supported in OpenSearch 2.1.0 and later", + Version.fromString(version).onOrAfter(Version.fromString("2.1.0")) + ); + } + + private Aggregation getExpiryDateRangeAggregation() { + DateRangeAggregation expiryDateRangeAggregation = new DateRangeAggregation.Builder().field("expDate") + .ranges(getDateAggregationRanges()) + .build(); + return new Aggregation.Builder().dateRange(expiryDateRangeAggregation).build(); + } + + private Aggregation getCostValueRangeAggregation() { + RangeAggregation costValueRangeAggregation = new RangeAggregation.Builder().field("cost") + .ranges(getValueAggregationRanges()) + .build(); + return new Aggregation.Builder().range(costValueRangeAggregation).build(); + } + + private Aggregation getMultiTermsAggregation(Integer size) { + MultiTermsAggregation.Builder multiTermsAggregationBuilder = new MultiTermsAggregation.Builder().terms( + List.of( + MultiTermLookup.of(multiTermLookup -> multiTermLookup.field("cost")), + MultiTermLookup.of(multiTermLookup -> multiTermLookup.field("expDate")) + ) + ); + + if (size != null) { + multiTermsAggregationBuilder = multiTermsAggregationBuilder.size(size); + } + + MultiTermsAggregation multiTermsAggregation = multiTermsAggregationBuilder.build(); + return new Aggregation.Builder().multiTerms(multiTermsAggregation).build(); + } + + private SearchResponse sendAggregateRequest(String index, String key, Aggregation value) throws IOException { + return javaClient().search(request -> request.index(index).size(0).aggregations(key, value), Void.class); + } + + private List getDateAggregationRanges() { + return List.of( + new DateRangeExpression.Builder().from(builder -> builder.value((double) getDatePlusDays(1).toEpochMilli())) + .to(FieldDateMath.of(builder -> builder.value((double) getDatePlusDays(3).toEpochMilli() - 1000))) + .key("from-1-to-2-days") + .build(), + new DateRangeExpression.Builder().from(builder -> builder.value((double) getDatePlusDays(3).toEpochMilli())) + .to(FieldDateMath.of(builder -> builder.value((double) getDatePlusDays(5).toEpochMilli() - 1000))) + .key("from-3-to-4-days") + .build(), + new DateRangeExpression.Builder().from(builder -> builder.value((double) getDatePlusDays(5).toEpochMilli())) + .to(FieldDateMath.of(builder -> builder.value((double) getDatePlusDays(7).toEpochMilli() - 1000))) + .key("from-5-to-6-days") + .build() + ); + } + + private List getValueAggregationRanges() { + return List.of( + new AggregationRange.Builder().to("10").build(), + new AggregationRange.Builder().from("10").to("30").build(), + new AggregationRange.Builder().from("30").build() + ); + } + + private void createDateRangeDocuments(String index) throws IOException { + createIndex(index); + javaClient().create(_1 -> _1.index(index).id("1").document(createProduct("egg", 2, 1)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("2").document(createProduct("meat", 15, 2)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("3").document(createProduct("ham", 30, 3)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("4").document(createProduct("cheese", 25, 4)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("5").document(createProduct("pasta", 8, 5)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("6").document(createProduct("oil", 50, 6)).refresh(Refresh.True)); + } + + private void createMultiTermsDocuments(String index) throws IOException { + createIndex(index); + javaClient().create(_1 -> _1.index(index).id("1").document(createProduct("appleA", 2, 1)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("2").document(createProduct("appleB", 2, 2)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("3").document(createProduct("appleC", 2, 3)).refresh(Refresh.True)); + } + + private void createIndex(String index) throws IOException { + Property nameValueProp = new Property.Builder().text(v -> v).build(); + Property costValueProp = new Property.Builder().integer(v -> v).build(); + Property expDateKeywordProp = new Property.Builder().date(v -> v).build(); + javaClient().indices() + .create( + c -> c.index(index) + .mappings( + m -> m.properties("name", nameValueProp).properties("cost", costValueProp).properties("expDate", expDateKeywordProp) + ) + ); + } + + private ProductDetails createProduct(String name, int cost, int plusDays) { + return new ProductDetails(name, cost, getDatePlusDays(plusDays).toEpochMilli()); + } + + private Instant getDatePlusDays(int plusDays) { + return LocalDateTime.of(2023, 2, 20, 0, 0, 0, 0).plusDays(plusDays).toInstant(ZoneOffset.UTC); + } + + public static class ProductDetails { + private String name; + private int cost; + private Long expDate; + + public ProductDetails() {} + + public ProductDetails(String name, int cost, Long expDate) { + this.name = name; + this.cost = cost; + this.expDate = expDate; + } + + public String getName() { + return name; + } + + public int getCost() { + return cost; + } + + public Long getExpDate() { + return expDate; + } + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCatClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCatClientIT.java index 1a1afc77bc..230f067ce8 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCatClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCatClientIT.java @@ -12,7 +12,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; - import org.junit.Test; import org.opensearch.Version; import org.opensearch.client.opensearch._types.Bytes; @@ -42,8 +41,12 @@ public abstract class AbstractCatClientIT extends OpenSearchJavaClientTestCase { public void testCatNodes() throws Exception { - NodesResponse nodesResponse = javaClient().cat().nodes(r -> r.bytes(Bytes.Bytes).fullId(true) - .headers("id,ip,name,version,pid,heap.percent,ram.percent,cpu,load_1m,load_5m,load_15m,node.role")); + NodesResponse nodesResponse = javaClient().cat() + .nodes( + r -> r.bytes(Bytes.Bytes) + .fullId(true) + .headers("id,ip,name,version,pid,heap.percent,ram.percent,cpu,load_1m,load_5m,load_15m,node.role") + ); assertNotNull(nodesResponse.valueBody()); assertTrue(nodesResponse.valueBody().size() > 0); @@ -67,8 +70,13 @@ public void testCatNodes() throws Exception { public void testCatIndices() throws Exception { createIndex("my-index-for-cat-test"); - IndicesResponse indicesResponse = javaClient().cat().indices(r -> r.headers("index,health,status,uuid,pri,rep,docs.count," + - "docs.deleted,store.size,pri.store.size,creation.date,creation.date.string").bytes(Bytes.Bytes)); + IndicesResponse indicesResponse = javaClient().cat() + .indices( + r -> r.headers( + "index,health,status,uuid,pri,rep,docs.count," + + "docs.deleted,store.size,pri.store.size,creation.date,creation.date.string" + ).bytes(Bytes.Bytes) + ); assertNotNull(indicesResponse.valueBody()); assertTrue(indicesResponse.valueBody().size() > 0); @@ -91,8 +99,13 @@ public void testCatIndices() throws Exception { public void testCatRecovery() throws Exception { createIndex("test-cat-recovery-index"); - RecoveryResponse recoveryResponse = javaClient().cat().recovery(r -> r.headers("index,shard,type,stage,source_host,source_node," + - "target_host,target_node,repository,snapshot,files,files_recovered,files_percent,files_total").bytes(Bytes.Bytes)); + RecoveryResponse recoveryResponse = javaClient().cat() + .recovery( + r -> r.headers( + "index,shard,type,stage,source_host,source_node," + + "target_host,target_node,repository,snapshot,files,files_recovered,files_percent,files_total" + ).bytes(Bytes.Bytes) + ); assertNotNull("recoveryResponse.valueBody() is null", recoveryResponse.valueBody()); assertTrue("recoveryResponse.valueBody().size() == 0", recoveryResponse.valueBody().size() > 0); @@ -121,14 +134,17 @@ public void testCatAliases() throws Exception { createIndex(indexName); assertTrue(javaClient().indices().putAlias(r -> r.index(indexName).name(alias).isWriteIndex(true)).acknowledged()); - AliasesResponse aliasesResponse = javaClient().cat().aliases(r -> - r.headers("alias,index,is_write_index,routing.index,routing.search").sort("is_write_index")); + AliasesResponse aliasesResponse = javaClient().cat() + .aliases(r -> r.headers("alias,index,is_write_index,routing.index,routing.search").sort("is_write_index")); assertNotNull(aliasesResponse.valueBody()); assertTrue(aliasesResponse.valueBody().size() > 0); - AliasesRecord aliasRecord = aliasesResponse.valueBody().stream().filter(aliasesRecord -> aliasesRecord.alias().equals(alias)) - .findAny().get(); + AliasesRecord aliasRecord = aliasesResponse.valueBody() + .stream() + .filter(aliasesRecord -> aliasesRecord.alias().equals(alias)) + .findAny() + .get(); assertTrue(aliasRecord.index().equals(indexName)); assertTrue(aliasRecord.isWriteIndex().equals(Boolean.TRUE.toString())); @@ -143,8 +159,8 @@ public void testCatShards() throws Exception { createIndex("test-cat-shards-index-2"); createIndex("a-test-cat-shards-index"); - ShardsResponse shardsResponse = javaClient().cat().shards(r -> r.headers("index,shard,prirep,state,store").sort("index") - .bytes(Bytes.Bytes)); + ShardsResponse shardsResponse = javaClient().cat() + .shards(r -> r.headers("index,shard,prirep,state,store").sort("index").bytes(Bytes.Bytes)); assertNotNull(shardsResponse.valueBody()); assertTrue(shardsResponse.valueBody().size() > 0); @@ -165,13 +181,16 @@ public void testCatShards() throws Exception { public void testCatAllocation() throws Exception { createIndex("test-cat-allocations-index"); - AllocationResponse allocationResponse = javaClient().cat().allocation(r -> - r.headers("ip,node,shards,disk.indices,disk.used,disk.avail,disk.total,disk.percent").sort("ip")); + AllocationResponse allocationResponse = javaClient().cat() + .allocation(r -> r.headers("ip,node,shards,disk.indices,disk.used,disk.avail,disk.total,disk.percent").sort("ip")); assertNotNull(allocationResponse.valueBody()); assertTrue(allocationResponse.valueBody().size() > 0); - AllocationRecord allocationRecord = allocationResponse.valueBody().stream().filter(allocation -> - !allocation.node().equals("UNASSIGNED")).findAny().get(); + AllocationRecord allocationRecord = allocationResponse.valueBody() + .stream() + .filter(allocation -> !allocation.node().equals("UNASSIGNED")) + .findAny() + .get(); assertNotNull(allocationRecord.ip()); assertNotNull(allocationRecord.node()); assertNotNull(allocationRecord.shards()); @@ -193,11 +212,12 @@ public void testCatPlugins() throws Exception { public void testCatSegments() throws Exception { createIndex("cat-segments-test-index"); - final IndexResponse index = javaClient().index(b -> b - .index("test-cat-segments-index") + final IndexResponse index = javaClient().index( + b -> b.index("test-cat-segments-index") .id("id") .refresh(Refresh.True) - .document(Map.of("test-cat-segments-key", "test-cat-segments-value"))); + .document(Map.of("test-cat-segments-key", "test-cat-segments-value")) + ); assertTrue(index.result() == Result.Created); @@ -215,39 +235,39 @@ public void testCatSegments() throws Exception { assertNull(segmentsRecord.ip()); assertNull(segmentsRecord.prirep()); } - + @Test public void testCatPitSegments() throws Exception { InfoResponse info = javaClient().info(); String version = info.version().number(); if (version.contains("SNAPSHOT")) { - version = version.split("-")[0]; + version = version.split("-")[0]; } - assumeTrue("The PIT is supported in OpenSearch 2.4.0 and later", - Version.fromString(version).onOrAfter(Version.fromString("2.4.0"))); + assumeTrue( + "The PIT is supported in OpenSearch 2.4.0 and later", + Version.fromString(version).onOrAfter(Version.fromString("2.4.0")) + ); createIndex("cat-pit-segments-test-index"); - final IndexResponse index = javaClient().index(b -> b - .index("test-cat-pit-segments-index") + final IndexResponse index = javaClient().index( + b -> b.index("test-cat-pit-segments-index") .id("id") .refresh(Refresh.True) - .document(Map.of("test-cat-pit-segments-key", "test-cat-pit-segments-value"))); + .document(Map.of("test-cat-pit-segments-key", "test-cat-pit-segments-value")) + ); assertTrue(index.result() == Result.Created); createPit("cat-pit-segments-test-index"); - SegmentsResponse PitSegmentsResponse = javaClient().cat() - .pitSegments(r -> r.headers("index,shard,id,segment,size")); + SegmentsResponse PitSegmentsResponse = javaClient().cat().pitSegments(r -> r.headers("index,shard,id,segment,size")); assertNotNull("PitSegmentsResponse.segments() is null", PitSegmentsResponse.valueBody()); if (Version.fromString(version).onOrAfter(Version.fromString("2.10.0"))) { - assertTrue("PitSegmentsResponse.segments().size() == 0", - PitSegmentsResponse.valueBody().isEmpty()); + assertTrue("PitSegmentsResponse.segments().size() == 0", PitSegmentsResponse.valueBody().isEmpty()); } else { - assertTrue("PitSegmentsResponse.segments().size() == 0", - PitSegmentsResponse.valueBody().size() > 0); + assertTrue("PitSegmentsResponse.segments().size() == 0", PitSegmentsResponse.valueBody().size() > 0); } } @@ -258,9 +278,9 @@ private void createIndex(String indexName) throws Exception { } private void createPit(String indexName) throws Exception { - CreatePitResponse createPitResponse = javaClient() - .createPit(r -> r.targetIndexes(Collections.singletonList(indexName)) - .keepAlive(new Time.Builder().time("100m").build())); + CreatePitResponse createPitResponse = javaClient().createPit( + r -> r.targetIndexes(Collections.singletonList(indexName)).keepAlive(new Time.Builder().time("100m").build()) + ); assertNotNull(createPitResponse); assertNotNull(createPitResponse.pitId()); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java index e695f7cf87..02c24a94d6 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java @@ -8,6 +8,14 @@ package org.opensearch.client.opensearch.integTest; +import static java.util.Collections.emptyMap; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import org.opensearch.client.ResponseException; import org.opensearch.client.json.JsonData; import org.opensearch.client.opensearch.OpenSearchClient; @@ -26,15 +34,6 @@ import org.opensearch.common.settings.Settings; import org.opensearch.indices.recovery.RecoverySettings; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import static java.util.Collections.emptyMap; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertTrue; - public abstract class AbstractClusterClientIT extends OpenSearchJavaClientTestCase { public void testClusterPutSettings() throws IOException { OpenSearchClient openSearchClient = javaClient(); @@ -53,29 +52,34 @@ public void testClusterPutSettings() throws IOException { transientSettingsMap.put(transientSettingKey, JsonData.of(transientSettingValue)); persistentSettingsMap.put(persistentSettingKey, JsonData.of(persistentSettingValue)); - PutClusterSettingsRequest request = new PutClusterSettingsRequest.Builder() - .persistent(persistentSettingsMap) - .transient_(transientSettingsMap) - .build(); + PutClusterSettingsRequest request = new PutClusterSettingsRequest.Builder().persistent(persistentSettingsMap) + .transient_(transientSettingsMap) + .build(); PutClusterSettingsResponse response = openSearchClient.cluster().putSettings(request); assertTrue(response.acknowledged()); assertThat(response.transient_().get(transientSettingKeySplit[0]), notNullValue()); assertThat(response.transient_().get(persistentSettingKeySplit[0]), nullValue()); assertEquals( - response.transient_().get(transientSettingKeySplit[0]).toJson().asJsonObject() - .getJsonObject("recovery") - .getString("max_bytes_per_sec"), - transientSettingValue + response.transient_() + .get(transientSettingKeySplit[0]) + .toJson() + .asJsonObject() + .getJsonObject("recovery") + .getString("max_bytes_per_sec"), + transientSettingValue ); assertThat(response.persistent().get(transientSettingKeySplit[0]), nullValue()); assertThat(response.persistent().get(persistentSettingKeySplit[0]), notNullValue()); assertEquals( - response.persistent().get(persistentSettingKeySplit[0]).toJson().asJsonObject() - .getJsonObject("routing") - .getJsonObject("allocation") - .getString("enable"), - persistentSettingValue + response.persistent() + .get(persistentSettingKeySplit[0]) + .toJson() + .asJsonObject() + .getJsonObject("routing") + .getJsonObject("allocation") + .getString("enable"), + persistentSettingValue ); } @@ -87,9 +91,7 @@ public void testClusterUpdateSettingNonExistent() throws IOException { Map transientSettingsMap = new HashMap<>(); transientSettingsMap.put(setting, JsonData.of(value)); - PutClusterSettingsRequest request = new PutClusterSettingsRequest.Builder() - .transient_(transientSettingsMap) - .build(); + PutClusterSettingsRequest request = new PutClusterSettingsRequest.Builder().transient_(transientSettingsMap).build(); try { openSearchClient.cluster().putSettings(request); fail(); @@ -115,21 +117,23 @@ public void testClusterGetSettings() throws IOException { transientSettingsMap.put(transientSettingKey, JsonData.of(transientSettingValue)); persistentSettingsMap.put(persistentSettingKey, JsonData.of(persistentSettingValue)); - PutClusterSettingsRequest request = new PutClusterSettingsRequest.Builder() - .persistent(persistentSettingsMap) - .transient_(transientSettingsMap) - .build(); + PutClusterSettingsRequest request = new PutClusterSettingsRequest.Builder().persistent(persistentSettingsMap) + .transient_(transientSettingsMap) + .build(); openSearchClient.cluster().putSettings(request); GetClusterSettingsResponse getSettingsResponse = openSearchClient.cluster() - .getSettings(new GetClusterSettingsRequest.Builder().build()); + .getSettings(new GetClusterSettingsRequest.Builder().build()); assertTrue(getSettingsResponse.persistent().containsKey("cluster")); assertEquals( - getSettingsResponse.persistent().get("cluster").toJson().asJsonObject() - .getJsonObject("routing") - .getJsonObject("allocation") - .getString("enable"), - persistentSettingValue + getSettingsResponse.persistent() + .get("cluster") + .toJson() + .asJsonObject() + .getJsonObject("routing") + .getJsonObject("allocation") + .getString("enable"), + persistentSettingValue ); assertEquals(1, getSettingsResponse.transient_().size()); assertEquals(0, getSettingsResponse.defaults().size()); @@ -150,31 +154,33 @@ public void testClusterGetSettingsWithDefault() throws IOException { transientSettingsMap.put(transientSettingKey, JsonData.of(transientSettingValue)); persistentSettingsMap.put(persistentSettingKey, JsonData.of(persistentSettingValue)); - PutClusterSettingsRequest request = new PutClusterSettingsRequest.Builder() - .persistent(persistentSettingsMap) - .transient_(transientSettingsMap) - .build(); + PutClusterSettingsRequest request = new PutClusterSettingsRequest.Builder().persistent(persistentSettingsMap) + .transient_(transientSettingsMap) + .build(); openSearchClient.cluster().putSettings(request); - GetClusterSettingsResponse getSettingsResponse = openSearchClient.cluster().getSettings(new GetClusterSettingsRequest.Builder() - .includeDefaults(true).build()); + GetClusterSettingsResponse getSettingsResponse = openSearchClient.cluster() + .getSettings(new GetClusterSettingsRequest.Builder().includeDefaults(true).build()); assertTrue(getSettingsResponse.persistent().containsKey("cluster")); assertEquals( - getSettingsResponse.persistent().get("cluster").toJson().asJsonObject() - .getJsonObject("routing") - .getJsonObject("allocation") - .getString("enable"), - persistentSettingValue + getSettingsResponse.persistent() + .get("cluster") + .toJson() + .asJsonObject() + .getJsonObject("routing") + .getJsonObject("allocation") + .getString("enable"), + persistentSettingValue ); assertEquals(1, getSettingsResponse.transient_().size()); - assertTrue(getSettingsResponse.defaults().size()>0); + assertTrue(getSettingsResponse.defaults().size() > 0); } public void testClusterHealthYellowClusterLevel() throws IOException { OpenSearchClient openSearchClient = javaClient(); createIndex("index", Settings.EMPTY); createIndex("index2", Settings.EMPTY); - HealthRequest request = new HealthRequest.Builder().timeout(t->t.time("5s")).build(); + HealthRequest request = new HealthRequest.Builder().timeout(t -> t.time("5s")).build(); HealthResponse response = openSearchClient.cluster().health(request); assertEquals(response.indices().size(), 0); } @@ -191,11 +197,10 @@ public void testClusterHealthYellowIndicesLevel() throws IOException { if (randomBoolean()) { createIndex(ignoredIndex, Settings.EMPTY); } - HealthRequest request = new HealthRequest.Builder() - .index(firstIndex, secondIndex) - .timeout(t->t.time("5s")) - .level(Level.Indices) - .build(); + HealthRequest request = new HealthRequest.Builder().index(firstIndex, secondIndex) + .timeout(t -> t.time("5s")) + .level(Level.Indices) + .build(); HealthResponse response = openSearchClient.cluster().health(request); assertYellowShards(response); assertEquals(response.indices().size(), 2); @@ -212,7 +217,7 @@ private static void assertYellowShards(HealthResponse response) { assertEquals(response.numberOfDataNodes(), 1); assertEquals(response.numberOfNodes(), 1); assertEquals(response.activeShards(), 2); - assertEquals(response.delayedUnassignedShards(),0); + assertEquals(response.delayedUnassignedShards(), 0); assertEquals(response.initializingShards(), 0); assertEquals(response.unassignedShards(), 2); } @@ -221,11 +226,7 @@ public void testClusterHealthYellowSpecificIndex() throws IOException { OpenSearchClient openSearchClient = javaClient(); createIndex("index", Settings.EMPTY); createIndex("index2", Settings.EMPTY); - HealthRequest request = new HealthRequest.Builder() - .index("index") - .timeout(t->t.time("5s")) - .level(Level.Shards) - .build(); + HealthRequest request = new HealthRequest.Builder().index("index").timeout(t -> t.time("5s")).level(Level.Shards).build(); HealthResponse response = openSearchClient.cluster().health(request); assertNotNull(response); @@ -245,7 +246,7 @@ public void testClusterHealthYellowSpecificIndex() throws IOException { private static void assertYellowIndex(String indexName, IndexHealthStats indexHealth, boolean emptyShards) { assertNotNull(indexHealth); - assertEquals(indexHealth.activePrimaryShards(),1); + assertEquals(indexHealth.activePrimaryShards(), 1); assertEquals(indexHealth.activeShards(), 1); assertEquals(indexHealth.numberOfReplicas(), 1); assertEquals(indexHealth.initializingShards(), 0); @@ -285,11 +286,10 @@ private static void assertNoIndices(HealthResponse response) { public void testClusterHealthNotFoundIndex() throws IOException { OpenSearchClient openSearchClient = javaClient(); createIndex("index", Settings.EMPTY); - HealthRequest request = new HealthRequest.Builder() - .index("notexisted-index") - .timeout(t->t.time("5s")) - .level(Level.Indices) - .build(); + HealthRequest request = new HealthRequest.Builder().index("notexisted-index") + .timeout(t -> t.time("5s")) + .level(Level.Indices) + .build(); try { HealthResponse response = openSearchClient.cluster().health(request); assertNotNull(response); @@ -300,4 +300,4 @@ public void testClusterHealthNotFoundIndex() throws IOException { assertNotNull(e); } } -} \ No newline at end of file +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java index dbf45cd4e6..16a9d5ff57 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java @@ -8,6 +8,10 @@ package org.opensearch.client.opensearch.integTest; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormat; @@ -34,11 +38,6 @@ import org.opensearch.client.opensearch.indices.DeleteIndexResponse; import org.opensearch.client.transport.endpoints.BooleanResponse; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - public abstract class AbstractCrudIT extends OpenSearchJavaClientTestCase { public void testDelete() throws IOException { @@ -59,11 +58,7 @@ public void testDelete() throws IOException { String index = "my-index1"; javaClient().indices().create(b -> b.index(index)); - String id = javaClient().index(b -> b - .index(index) - .id(docId) - .document(Collections.singletonMap("foo", "bar"))) - .id(); + String id = javaClient().index(b -> b.index(index).id(docId).document(Collections.singletonMap("foo", "bar"))).id(); assertEquals(id, docId); DeleteResponse deleteResponse = javaClient().delete(d -> d.id(docId).index(index)); @@ -84,19 +79,13 @@ public void testExists() throws IOException { assertFalse(javaClient().indices().exists(b -> b.index("index")).value()); String index = "ingest-test"; // Create an index - CreateIndexResponse createIndexResponse = javaClient().indices().create(b -> b - .index(index) - ); + CreateIndexResponse createIndexResponse = javaClient().indices().create(b -> b.index(index)); assertEquals(index, createIndexResponse.index()); // Check that it actually exists. Example of a boolean response assertTrue(javaClient().indices().exists(b -> b.index(index)).value()); - javaClient().index(b -> b - .index(index) - .id("id") - .document(Collections.singletonMap("foo", "bar")) - .refresh(Refresh.True)); + javaClient().index(b -> b.index(index).id("id").document(Collections.singletonMap("foo", "bar")).refresh(Refresh.True)); assertTrue(javaClient().exists(b -> b.index(index).id("id")).value()); assertFalse(javaClient().exists(b -> b.index(index).id("random_id")).value()); @@ -109,31 +98,18 @@ public void testSourceExists() throws IOException { appData.setMsg("foo"); { - ExistsSourceRequest request = new ExistsSourceRequest.Builder() - .index("index") - .id("id") - .build(); + ExistsSourceRequest request = new ExistsSourceRequest.Builder().index("index").id("id").build(); BooleanResponse response = javaClient().existsSource(request); assertFalse(response.value()); } - javaClient().index(b -> b - .index("index") - .id("id") - .refresh(Refresh.True) - .document(appData)); + javaClient().index(b -> b.index("index").id("id").refresh(Refresh.True).document(appData)); { - ExistsSourceRequest request = new ExistsSourceRequest.Builder() - .index("index") - .id("id") - .build(); + ExistsSourceRequest request = new ExistsSourceRequest.Builder().index("index").id("id").build(); BooleanResponse response = javaClient().existsSource(request); assertTrue(response.value()); } { - ExistsSourceRequest request = new ExistsSourceRequest.Builder() - .index("index") - .id("does_not_exist") - .build(); + ExistsSourceRequest request = new ExistsSourceRequest.Builder().index("index").id("does_not_exist").build(); BooleanResponse response = javaClient().existsSource(request); assertFalse(response.value()); } @@ -143,12 +119,11 @@ public void testGet() throws IOException { { OpenSearchException exception = expectThrows( - OpenSearchException.class, - () -> javaClient().get(new GetRequest.Builder().index("index").id("id").build(), String.class) + OpenSearchException.class, + () -> javaClient().get(new GetRequest.Builder().index("index").id("id").build(), String.class) ); assertEquals(404, exception.status()); - assertEquals("Request failed: [index_not_found_exception] no such index [index]", - exception.getMessage()); + assertEquals("Request failed: [index_not_found_exception] no such index [index]", exception.getMessage()); } AppData appData = new AppData(); @@ -158,12 +133,7 @@ public void testGet() throws IOException { javaClient().index(b -> b.index("index").id("id").document(appData).refresh(Refresh.True)); { - GetResponse getResponse = javaClient().get(b -> b - .index("index") - .id("id") - .version(1L) - , AppData.class - ); + GetResponse getResponse = javaClient().get(b -> b.index("index").id("id").version(1L), AppData.class); assertEquals("index", getResponse.index()); assertEquals("id", getResponse.id()); assertTrue(getResponse.found()); @@ -172,11 +142,7 @@ public void testGet() throws IOException { assertEquals(appData.getMsg(), getResponse.source().getMsg()); } { - GetResponse getResponse = javaClient().get(b -> b - .index("index") - .id("does_not_exist") - , AppData.class - ); + GetResponse getResponse = javaClient().get(b -> b.index("index").id("does_not_exist"), AppData.class); assertEquals("index", getResponse.index()); assertEquals("does_not_exist", getResponse.id()); assertFalse(getResponse.found()); @@ -190,17 +156,9 @@ public void testGetWithTypes() throws IOException { appData.setIntValue(1337); appData.setMsg("foo"); - javaClient().index(b -> b - .index("index") - .id("id") - .document(appData) - .refresh(Refresh.True)); + javaClient().index(b -> b.index("index").id("id").document(appData).refresh(Refresh.True)); - GetResponse getResponse = javaClient().get(b -> b - .index("index") - .id("id") - , AppData.class - ); + GetResponse getResponse = javaClient().get(b -> b.index("index").id("id"), AppData.class); assertEquals("index", getResponse.index()); assertEquals("id", getResponse.id()); @@ -219,27 +177,17 @@ public void testMultiGet() throws IOException { List ids = new ArrayList<>(); ids.add("id1"); ids.add("id2"); - MgetResponse response = javaClient().mget(b -> b - .index("index") - .ids(ids) - , AppData.class - ); + MgetResponse response = javaClient().mget(b -> b.index("index").ids(ids), AppData.class); assertEquals(2, response.docs().size()); assertTrue(response.docs().get(0).isFailure()); assertEquals("id1", response.docs().get(0).failure().id()); assertEquals("index", response.docs().get(0).failure().index()); - assertEquals( - "no such index [index]", - response.docs().get(0).failure().error().reason() - ); + assertEquals("no such index [index]", response.docs().get(0).failure().error().reason()); assertTrue(response.docs().get(1).isFailure()); assertEquals("id2", response.docs().get(1).failure().id()); assertEquals("index", response.docs().get(1).failure().index()); - assertEquals( - "no such index [index]", - response.docs().get(1).failure().error().reason() - ); + assertEquals("no such index [index]", response.docs().get(1).failure().error().reason()); } } @@ -253,11 +201,10 @@ public void testUpdate() throws Exception { appData.setMsg("bar"); { - UpdateRequest updateRequest = new UpdateRequest.Builder() - .index("index") - .id("does_not_exist") - .doc(appData) - .build(); + UpdateRequest updateRequest = new UpdateRequest.Builder().index("index") + .id("does_not_exist") + .doc(appData) + .build(); try { javaClient().update(updateRequest, AppData.class); } catch (OpenSearchException e) { @@ -269,22 +216,17 @@ public void testUpdate() throws Exception { } } { - IndexRequest indexRequest = new IndexRequest.Builder() - .index("index") - .id("id") - .document(appData) - .build(); + IndexRequest indexRequest = new IndexRequest.Builder().index("index").id("id").document(appData).build(); IndexResponse indexResponse = javaClient().index(indexRequest); assertEquals(Result.Created, indexResponse.result()); long lastUpdateSeqNo; long lastUpdatePrimaryTerm; { - UpdateRequest updateRequest = new UpdateRequest.Builder() - .index("index") - .id("id") - .doc(updatedAppData) - .build(); + UpdateRequest updateRequest = new UpdateRequest.Builder().index("index") + .id("id") + .doc(updatedAppData) + .build(); UpdateResponse updateResponse = javaClient().update(updateRequest, AppData.class); assertEquals(Result.Updated, updateResponse.result()); assertEquals(indexResponse.version() + 1, updateResponse.version()); @@ -307,18 +249,13 @@ public void testUpdateWithTypes() throws IOException { appData.setIntValue(3333); appData.setMsg("bar"); - IndexRequest indexRequest = new IndexRequest.Builder() - .index("index") - .id("id") - .document(appData) - .build(); + IndexRequest indexRequest = new IndexRequest.Builder().index("index").id("id").document(appData).build(); IndexResponse indexResponse = javaClient().index(indexRequest); - UpdateRequest updateRequest = new UpdateRequest.Builder() - .index("index") - .id("id") - .doc(updatedAppData) - .build(); + UpdateRequest updateRequest = new UpdateRequest.Builder().index("index") + .id("id") + .doc(updatedAppData) + .build(); UpdateResponse updateResponse = javaClient().update(updateRequest, AppData.class); assertEquals(Result.Updated, updateResponse.result()); @@ -338,14 +275,15 @@ public void testBulk() throws IOException { String id = String.valueOf(i); boolean erroneous = randomBoolean(); errors[i] = erroneous; - BulkOperation.Kind opType = randomFrom(BulkOperation.Kind.Delete, BulkOperation.Kind.Index, - BulkOperation.Kind.Create, BulkOperation.Kind.Update); + BulkOperation.Kind opType = randomFrom( + BulkOperation.Kind.Delete, + BulkOperation.Kind.Index, + BulkOperation.Kind.Create, + BulkOperation.Kind.Update + ); if (opType.equals(BulkOperation.Kind.Delete)) { if (!erroneous) { - assertEquals( - Result.Created, - javaClient().index(b -> b.index("index").id(id).document(appData)).result() - ); + assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result()); } BulkOperation op = new BulkOperation.Builder().delete(d -> d.index("index").id(id)).build(); opsList.add(op); @@ -353,39 +291,26 @@ public void testBulk() throws IOException { appData.setIntValue(i); appData.setMsg("id"); if (opType.equals(BulkOperation.Kind.Index)) { - BulkOperation op = new BulkOperation.Builder().index(b -> b - .index("index") - .id(id) - .document(appData) - .ifSeqNo(erroneous ? 12L : null) - .ifPrimaryTerm(erroneous ? 12L : null)).build(); + BulkOperation op = new BulkOperation.Builder().index( + b -> b.index("index").id(id).document(appData).ifSeqNo(erroneous ? 12L : null).ifPrimaryTerm(erroneous ? 12L : null) + ).build(); opsList.add(op); } else if (opType.equals(BulkOperation.Kind.Create)) { - BulkOperation op = new BulkOperation.Builder().create(o -> o - .index("index") - .id(id) - .document(appData) - ).build(); + BulkOperation op = new BulkOperation.Builder().create(o -> o.index("index").id(id).document(appData)).build(); opsList.add(op); if (erroneous) { - assertEquals(Result.Created, javaClient().index(b -> b. - index("index").id(id).document(appData)).result()); + assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result()); } } else if (opType.equals(BulkOperation.Kind.Update)) { - BulkOperation op = new BulkOperation.Builder().update(o -> o - .index("index") - .id(id) - .document(Collections.singletonMap("key", "value")) + BulkOperation op = new BulkOperation.Builder().update( + o -> o.index("index").id(id).document(Collections.singletonMap("key", "value")) ).build(); opsList.add(op); if (!erroneous) { - assertEquals( - Result.Created, - javaClient().index(b -> b.index("index").id(id).document(appData)).result() - ); + assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result()); } } } @@ -406,20 +331,22 @@ public void testBulkUpdateScript() throws IOException { appData.setIntValue(1337); appData.setMsg("foo"); - assertEquals( - Result.Created, - javaClient().index(b -> b.index("index").id(id).document(appData)).result() - ); + assertEquals(Result.Created, javaClient().index(b -> b.index("index").id(id).document(appData)).result()); - final BulkOperation op = new BulkOperation.Builder().update(o -> o - .index("index") + final BulkOperation op = new BulkOperation.Builder().update( + o -> o.index("index") .id(id) - .script(Script.of(s -> s.inline(new InlineScript.Builder() - .lang("painless") - .source("ctx._source.intValue += params.inc") - .params("inc", JsonData.of(1)) - .build()))) - ).build(); + .script( + Script.of( + s -> s.inline( + new InlineScript.Builder().lang("painless") + .source("ctx._source.intValue += params.inc") + .params("inc", JsonData.of(1)) + .build() + ) + ) + ) + ).build(); BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build(); BulkResponse bulkResponse = javaClient().bulk(bulkRequest); @@ -439,16 +366,21 @@ public void testBulkUpdateScriptUpsert() throws IOException { appData.setIntValue(1337); appData.setMsg("foo"); - final BulkOperation op = new BulkOperation.Builder().update(o -> o - .index("index") + final BulkOperation op = new BulkOperation.Builder().update( + o -> o.index("index") .id(id) .upsert(appData) - .script(Script.of(s -> s.inline(new InlineScript.Builder() - .lang("painless") - .source("ctx._source.intValue += params.inc") - .params("inc", JsonData.of(1)) - .build()))) - ).build(); + .script( + Script.of( + s -> s.inline( + new InlineScript.Builder().lang("painless") + .source("ctx._source.intValue += params.inc") + .params("inc", JsonData.of(1)) + .build() + ) + ) + ) + ).build(); BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build(); BulkResponse bulkResponse = javaClient().bulk(bulkRequest); @@ -468,12 +400,8 @@ public void testBulkUpdateUpsert() throws IOException { appData.setIntValue(1337); appData.setMsg("foo"); - final BulkOperation op = new BulkOperation.Builder().update(o -> o - .index("index") - .id(id) - .document(new AppData()) - .upsert(appData) - ).build(); + final BulkOperation op = new BulkOperation.Builder().update(o -> o.index("index").id(id).document(new AppData()).upsert(appData)) + .build(); BulkRequest bulkRequest = new BulkRequest.Builder().operations(op).build(); BulkResponse bulkResponse = javaClient().bulk(bulkRequest); @@ -509,7 +437,7 @@ private void validateBulkResponses(int nbItems, boolean[] errors, BulkResponse b public void testUrlEncode() throws IOException { String indexPattern = ""; String expectedIndex = "logstash-" - + DateTimeFormat.forPattern("YYYY.MM.dd").print(new DateTime(DateTimeZone.UTC).monthOfYear().roundFloorCopy()); + + DateTimeFormat.forPattern("YYYY.MM.dd").print(new DateTime(DateTimeZone.UTC).monthOfYear().roundFloorCopy()); AppData appData = new AppData(); appData.setIntValue(1337); appData.setMsg("foo"); @@ -519,8 +447,7 @@ public void testUrlEncode() throws IOException { assertEquals("id#1", response.id()); } { - GetResponse getResponse = javaClient().get(b -> b - .index(indexPattern).id("id#1"), AppData.class); + GetResponse getResponse = javaClient().get(b -> b.index(indexPattern).id("id#1"), AppData.class); assertTrue(getResponse.found()); assertEquals(expectedIndex, getResponse.index()); assertEquals("id#1", getResponse.id()); @@ -533,8 +460,7 @@ public void testUrlEncode() throws IOException { assertEquals(docId, indexResponse.id()); } { - GetResponse getResponse = javaClient().get(b -> b - .index("index").id(docId), AppData.class); + GetResponse getResponse = javaClient().get(b -> b.index("index").id(docId), AppData.class); assertTrue(getResponse.found()); assertEquals("index", getResponse.index()); assertEquals(docId, getResponse.id()); @@ -555,8 +481,7 @@ public void testParamsEncode() throws IOException { assertEquals(id, response.id()); } { - GetResponse getResponse = javaClient().get(b -> b - .index("index").id("id").routing(routing), AppData.class); + GetResponse getResponse = javaClient().get(b -> b.index("index").id("id").routing(routing), AppData.class); assertTrue(getResponse.found()); assertEquals("index", getResponse.index()); assertEquals("id", getResponse.id()); @@ -571,14 +496,12 @@ public void testGetIdWithPlusSign() throws Exception { appData.setMsg("foo"); { - IndexResponse indexResponse = javaClient().index(b -> b - .index("index").id(id).document(appData)); + IndexResponse indexResponse = javaClient().index(b -> b.index("index").id(id).document(appData)); assertEquals("index", indexResponse.index()); assertEquals(id, indexResponse.id()); } { - GetResponse getResponse = javaClient().get(b -> b - .index("index").id(id), AppData.class); + GetResponse getResponse = javaClient().get(b -> b.index("index").id(id), AppData.class); assertTrue(getResponse.found()); assertEquals("index", getResponse.index()); assertEquals(id, getResponse.id()); @@ -605,4 +528,4 @@ public void setMsg(String msg) { this.msg = msg; } } -} \ No newline at end of file +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractHighlightIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractHighlightIT.java index 6698f9a828..0b95463975 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractHighlightIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractHighlightIT.java @@ -8,6 +8,11 @@ package org.opensearch.client.opensearch.integTest; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; import org.junit.Test; import org.opensearch.client.opensearch._types.Refresh; import org.opensearch.client.opensearch.core.SearchResponse; @@ -15,12 +20,6 @@ import org.opensearch.client.opensearch.core.search.Hit; import org.opensearch.client.util.ObjectBuilder; -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - public abstract class AbstractHighlightIT extends OpenSearchJavaClientTestCase { @Test @@ -28,8 +27,7 @@ public void testDefaultHighlight() throws Exception { String index = "queries_highlight"; createTestDocuments(index); - List>> highlights = highlightQuery("spread", - h -> h.fields("content", b -> b)); + List>> highlights = highlightQuery("spread", h -> h.fields("content", b -> b)); assertEquals(2, highlights.size()); for (Map> hit : highlights) { @@ -44,8 +42,10 @@ public void testMultiFieldHighlight() throws Exception { String index = "queries_highlight"; createTestDocuments(index); - List>> highlights = highlightQuery("real decades", - h -> h.fields("title", b -> b).fields("content", b -> b)); + List>> highlights = highlightQuery( + "real decades", + h -> h.fields("title", b -> b).fields("content", b -> b) + ); assertEquals(3, highlights.size()); for (Map> hit : highlights) { @@ -62,8 +62,10 @@ public void testDifferentMarkersHighlight() throws Exception { String index = "queries_highlight"; createTestDocuments(index); - List>> highlights = highlightQuery("spread", - h -> h.preTags("").postTags("").fields("content", b -> b)); + List>> highlights = highlightQuery( + "spread", + h -> h.preTags("").postTags("").fields("content", b -> b) + ); assertEquals(2, highlights.size()); for (Map> hit : highlights) { @@ -88,8 +90,7 @@ public void testAnalyzerOffset() throws Exception { if (major >= 2 && minor >= 2) { String index = "queries_highlight"; createTestDocuments(index); - List>> highlights = highlightQuery("real", - h -> h.maxAnalyzerOffset("5").fields("title", b -> b)); + List>> highlights = highlightQuery("real", h -> h.maxAnalyzerOffset("5").fields("title", b -> b)); assertEquals(3, highlights.size()); assertEquals(0, highlights.get(0).size()); @@ -101,29 +102,40 @@ public void testAnalyzerOffset() throws Exception { } } - private List>> highlightQuery(String query, - Function> fn) - throws IOException { - SearchResponse

response = javaClient() - .search(s -> s - .query(q -> q - .simpleQueryString(sqs -> sqs - .fields("title", "content", "author") - .query(query))) - .highlight(fn), Article.class); + private List>> highlightQuery(String query, Function> fn) + throws IOException { + SearchResponse
response = javaClient().search( + s -> s.query(q -> q.simpleQueryString(sqs -> sqs.fields("title", "content", "author").query(query))).highlight(fn), + Article.class + ); return response.hits().hits().stream().map(Hit::highlight).collect(Collectors.toList()); } private void createTestDocuments(String index) throws IOException { - javaClient().create(_1 -> _1.index(index).id("1").document(createArticle( - "Superheroes are Real", "Meet these man avoid the fire spread during last decades.", "John Doe")) - .refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("2").document(createArticle( - "Real Slow Ideas", "Why some innovations spread quick while others take decades to catch hold.", "John Foo")) - .refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("3").document(createArticle( - "Real Two Degrees", "How the world failed on climate change during the decades.", "Anne Doe")) - .refresh(Refresh.True)); + javaClient().create( + _1 -> _1.index(index) + .id("1") + .document(createArticle("Superheroes are Real", "Meet these man avoid the fire spread during last decades.", "John Doe")) + .refresh(Refresh.True) + ); + javaClient().create( + _1 -> _1.index(index) + .id("2") + .document( + createArticle( + "Real Slow Ideas", + "Why some innovations spread quick while others take decades to catch hold.", + "John Foo" + ) + ) + .refresh(Refresh.True) + ); + javaClient().create( + _1 -> _1.index(index) + .id("3") + .document(createArticle("Real Two Degrees", "How the world failed on climate change during the decades.", "Anne Doe")) + .refresh(Refresh.True) + ); } private Article createArticle(String title, String content, String author) { @@ -135,8 +147,7 @@ public static class Article { public String content; public String author; - public Article() { - } + public Article() {} public Article(String title, String content, String author) { this.title = title; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java index 869e63cc1c..a3f360d245 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java @@ -8,6 +8,10 @@ package org.opensearch.client.opensearch.integTest; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import org.opensearch.client.opensearch.OpenSearchAsyncClient; import org.opensearch.client.opensearch._types.OpenSearchException; import org.opensearch.client.opensearch.indices.CreateDataStreamResponse; @@ -25,11 +29,6 @@ import org.opensearch.client.opensearch.indices.PutIndexTemplateResponse; import org.opensearch.common.settings.Settings; -import java.io.IOException; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; - public abstract class AbstractIndicesClientIT extends OpenSearchJavaClientTestCase { public void testIndicesExists() throws IOException { @@ -56,9 +55,7 @@ public void testIndicesExists() throws IOException { } catch (OpenSearchException ex) { assertNotNull(ex); assertEquals(ex.status(), 404); - assertEquals(ex.getMessage(), - "Request failed: [index_not_found_exception] " + - "no such index [non_existent_index]"); + assertEquals(ex.getMessage(), "Request failed: [index_not_found_exception] " + "no such index [non_existent_index]"); } } @@ -100,8 +97,7 @@ public void testGetSettingsNonExistentIndex() throws IOException { String nonExistentIndex = "index_that_doesnt_exist"; - GetIndicesSettingsRequest getIndicesSettingsRequest = new GetIndicesSettingsRequest.Builder() - .index(nonExistentIndex).build(); + GetIndicesSettingsRequest getIndicesSettingsRequest = new GetIndicesSettingsRequest.Builder().index(nonExistentIndex).build(); try { javaClient().indices().getSettings(getIndicesSettingsRequest); @@ -109,9 +105,7 @@ public void testGetSettingsNonExistentIndex() throws IOException { } catch (OpenSearchException ex) { assertNotNull(ex); assertEquals(ex.status(), 404); - assertEquals(ex.getMessage(), - "Request failed: [index_not_found_exception] " + - "no such index [index_that_doesnt_exist]"); + assertEquals(ex.getMessage(), "Request failed: [index_not_found_exception] " + "no such index [index_that_doesnt_exist]"); } } @@ -123,21 +117,19 @@ public void testDataStream() throws IOException { // create an index template before creating data streams PutIndexTemplateResponse putIndexTemplateResponse = javaClient().indices() - .putIndexTemplate(b -> b.name(dataStreamIndexTemplateName) - .dataStream(new DataStream.Builder() - .timestampField(bd -> bd.name(timestampFieldName)) - .build()) - .indexPatterns(namePattern)); + .putIndexTemplate( + b -> b.name(dataStreamIndexTemplateName) + .dataStream(new DataStream.Builder().timestampField(bd -> bd.name(timestampFieldName)).build()) + .indexPatterns(namePattern) + ); assertTrue(putIndexTemplateResponse.acknowledged()); // create data streams follow the index pattern - CreateDataStreamResponse createDataStreamResponse1 = javaClient().indices() - .createDataStream(b -> b.name(dataStreamName)); + CreateDataStreamResponse createDataStreamResponse1 = javaClient().indices().createDataStream(b -> b.name(dataStreamName)); assertTrue(createDataStreamResponse1.acknowledged()); // get data stream - GetDataStreamResponse getDataStreamResponse = javaClient().indices() - .getDataStream(b -> b.name(dataStreamName)); + GetDataStreamResponse getDataStreamResponse = javaClient().indices().getDataStream(b -> b.name(dataStreamName)); assertEquals(1, getDataStreamResponse.dataStreams().size()); assertEquals(dataStreamName, getDataStreamResponse.dataStreams().get(0).name()); assertEquals(timestampFieldName, getDataStreamResponse.dataStreams().get(0).timestampField().name()); @@ -149,8 +141,7 @@ public void testDataStream() throws IOException { assertEquals(1, getAllDataStreamsResponse.dataStreams().size()); // get one data stream stats - DataStreamsStatsResponse dataStreamStatsResponse = javaClient().indices() - .dataStreamsStats(b -> b.name(dataStreamName).human(true)); + DataStreamsStatsResponse dataStreamStatsResponse = javaClient().indices().dataStreamsStats(b -> b.name(dataStreamName).human(true)); assertNotNull(dataStreamStatsResponse.shards()); assertEquals(1, dataStreamStatsResponse.dataStreamCount()); assertEquals(1, dataStreamStatsResponse.backingIndices()); @@ -163,8 +154,7 @@ public void testDataStream() throws IOException { assertNotNull(dataStreamStatsResponse.dataStreams().get(0).storeSize()); // get all data streams stats - DataStreamsStatsResponse allDataStreamsStatsResponse = javaClient().indices() - .dataStreamsStats(b -> b.human(true)); + DataStreamsStatsResponse allDataStreamsStatsResponse = javaClient().indices().dataStreamsStats(b -> b.human(true)); assertNotNull(allDataStreamsStatsResponse.shards()); assertEquals(1, allDataStreamsStatsResponse.dataStreamCount()); assertEquals(1, allDataStreamsStatsResponse.backingIndices()); @@ -177,8 +167,7 @@ public void testDataStream() throws IOException { assertNotNull(allDataStreamsStatsResponse.dataStreams().get(0).storeSize()); // delete data stream - DeleteDataStreamResponse deleteDataStreamResponse = javaClient().indices() - .deleteDataStream(b -> b.name(namePattern)); + DeleteDataStreamResponse deleteDataStreamResponse = javaClient().indices().deleteDataStream(b -> b.name(namePattern)); assertTrue(deleteDataStreamResponse.acknowledged()); // verify data stream is deleted @@ -200,9 +189,7 @@ public void testGetNotExistingIndexAlias() throws Exception { } catch (OpenSearchException ex) { assertNotNull(ex); assertEquals(ex.status(), 404); - assertEquals(ex.getMessage(), - "Request failed: [string_error] " + - "alias [alias_not_exists] missing"); + assertEquals(ex.getMessage(), "Request failed: [string_error] " + "alias [alias_not_exists] missing"); } } -} \ No newline at end of file +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractKnnIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractKnnIT.java index ae3f6bee5d..c2d0d87585 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractKnnIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractKnnIT.java @@ -24,54 +24,44 @@ public void testCanIndexAndSearchKnn() throws Exception { final String indexName = "knn-index-and-search"; - var createIndexResponse = javaClient() - .indices() - .create(c -> c - .index(indexName) - .settings(s -> s - .knn(true) - .knnAlgoParamEfSearch(100)) - .mappings(m -> m - .properties("vector", p -> p - .knnVector(k -> k - .dimension(4) - .method(km -> km - .name("hnsw") - .spaceType("l2") - .engine("nmslib") - .parameters("ef_construction", JsonData.of(256)) - .parameters("m", JsonData.of(16))))))); + var createIndexResponse = javaClient().indices() + .create( + c -> c.index(indexName) + .settings(s -> s.knn(true).knnAlgoParamEfSearch(100)) + .mappings( + m -> m.properties( + "vector", + p -> p.knnVector( + k -> k.dimension(4) + .method( + km -> km.name("hnsw") + .spaceType("l2") + .engine("nmslib") + .parameters("ef_construction", JsonData.of(256)) + .parameters("m", JsonData.of(16)) + ) + ) + ) + ) + ); assertEquals(true, createIndexResponse.acknowledged()); var docs = new Doc[] { - new Doc(new float[] { 1.5f, 5.5f, 4.5f, 6.4f }, 10.3f), - new Doc(new float[] { 2.5f, 3.5f, 5.6f, 6.7f }, 5.5f), - new Doc(new float[] { 4.5f, 5.5f, 6.7f, 3.7f }, 4.4f), - new Doc(new float[] { 1.5f, 5.5f, 4.5f, 6.4f }, 8.9f) - }; - var bulkOps = Arrays.stream(docs) - .map(d -> BulkOperation.of(o -> o.index(i -> i.document(d)))) - .collect(Collectors.toList()); - - var bulkResponse = javaClient() - .bulk(b -> b - .index(indexName) - .refresh(Refresh.WaitFor) - .operations(bulkOps)); + new Doc(new float[] { 1.5f, 5.5f, 4.5f, 6.4f }, 10.3f), + new Doc(new float[] { 2.5f, 3.5f, 5.6f, 6.7f }, 5.5f), + new Doc(new float[] { 4.5f, 5.5f, 6.7f, 3.7f }, 4.4f), + new Doc(new float[] { 1.5f, 5.5f, 4.5f, 6.4f }, 8.9f) }; + var bulkOps = Arrays.stream(docs).map(d -> BulkOperation.of(o -> o.index(i -> i.document(d)))).collect(Collectors.toList()); + + var bulkResponse = javaClient().bulk(b -> b.index(indexName).refresh(Refresh.WaitFor).operations(bulkOps)); assertFalse(bulkResponse.errors()); - var searchResponse = javaClient() - .search(s -> s - .index(indexName) - .size(2) - .query(q -> q - .knn(k -> k - .field("vector") - .vector(new float[] { 2.0f, 3.0f, 5.0f, 6.0f }) - .k(2))), - Doc.class); + var searchResponse = javaClient().search( + s -> s.index(indexName).size(2).query(q -> q.knn(k -> k.field("vector").vector(new float[] { 2.0f, 3.0f, 5.0f, 6.0f }).k(2))), + Doc.class + ); var hits = searchResponse.hits().hits(); @@ -83,10 +73,7 @@ public void testCanIndexAndSearchKnn() throws Exception { private void assumeKnnInstalled() throws IOException { var plugins = javaClient().cat().plugins(); - var knnInstalled = plugins - .valueBody() - .stream() - .anyMatch(p -> Objects.equals(p.component(), "opensearch-knn")); + var knnInstalled = plugins.valueBody().stream().anyMatch(p -> Objects.equals(p.component(), "opensearch-knn")); assumeTrue("KNN plugin is not installed", knnInstalled); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractMultiSearchRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractMultiSearchRequestIT.java index c082d9b043..273412817c 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractMultiSearchRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractMultiSearchRequestIT.java @@ -8,6 +8,13 @@ package org.opensearch.client.opensearch.integTest; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; import org.junit.Test; import org.opensearch.client.json.JsonData; import org.opensearch.client.opensearch._types.FieldSort; @@ -31,363 +38,350 @@ import org.opensearch.client.opensearch.core.search.SourceConfig; import org.opensearch.client.util.ObjectBuilder; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; - public abstract class AbstractMultiSearchRequestIT extends OpenSearchJavaClientTestCase { - @Test - public void shouldReturnMultipleSearches() throws Exception { - String index = "multiple_searches_request"; - createTestDocuments(index); - - RequestItem largeItemsQuery = createMSearchQuery("large"); - RequestItem mediumItemsQuery = createMSearchQuery("medium"); - RequestItem smallItemsQuery = createMSearchQuery("small"); - - MsearchResponse response = sendMSearchRequest(index, List.of(largeItemsQuery, mediumItemsQuery, smallItemsQuery)); - assertEquals(3, response.responses().size()); - assertEquals(2, response.responses().get(0).result().hits().hits().size()); - assertEquals(2, response.responses().get(1).result().hits().hits().size()); - assertEquals(2, response.responses().get(2).result().hits().hits().size()); - } - - @Test - public void shouldReturnHighlightsInMultipleSearches() throws Exception { - String index = "multiple_searches_request_with_highlights"; - createTestDocuments(index); - - RequestItem largeItemsQuery = createMSearchQueryWithHighlight("large"); - RequestItem mediumItemsQuery = createMSearchQueryWithHighlight("medium"); - RequestItem smallItemsQuery = createMSearchQueryWithHighlight("small"); - - MsearchResponse response = sendMSearchRequest(index, List.of(largeItemsQuery, mediumItemsQuery, smallItemsQuery)); - assertEquals(3, response.responses().size()); - assertResponseHighlights(response.responses().get(0)); - assertResponseHighlights(response.responses().get(1)); - assertResponseHighlights(response.responses().get(2)); - } - - @Test - public void shouldReturnMultiSearchesWithSelectedSourceFieldsOnly() throws Exception { - String index = "multiple_searches_request_with_sources"; - createTestDocuments(index); - - RequestItem largeItemsQuery = createMSearchQueryWithSelectedSourceFields("large"); - RequestItem mediumItemsQuery = createMSearchQueryWithSelectedSourceFields("medium"); - RequestItem smallItemsQuery = createMSearchQueryWithSelectedSourceFields("small"); - - MsearchResponse response = sendMSearchRequest(index, List.of(largeItemsQuery, mediumItemsQuery, smallItemsQuery)); - assertEquals(3, response.responses().size()); - assertResponseSources(response.responses().get(0)); - assertResponseSources(response.responses().get(1)); - assertResponseSources(response.responses().get(2)); - } - - @Test - public void shouldReturnMultiSearchesSorted() throws Exception { - String index = "multiple_searches_request_sort"; - createTestDocuments(index); - - RequestItem sortedItemsQuery = createMSearchSortedFuzzyRequest(); - - MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); - assertEquals(1, response.responses().size()); - var hits = response.responses().get(0).result().hits().hits(); - assertEquals(3, hits.size()); - - assertEquals("hammer", hits.get(2).source().getName()); - } - - @Test - public void shouldReturnMultiSearchesTrackingScores() throws Exception { - String index = "multiple_searches_request_track_scores"; - createTestDocuments(index); - - RequestItem sortedItemsQuery = createMSearchSortedFuzzyRequest(); - - MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); - assertEquals(1, response.responses().size()); - var hits = response.responses().get(0).result().hits().hits(); - assertEquals(3, hits.size()); - assertNull(hits.get(0).score()); - assertNull(hits.get(1).score()); - assertNull(hits.get(2).score()); - - RequestItem trackScoreItemsQuery = createMSearchSortedFuzzyRequest(b -> b.trackScores(true)); - - MsearchResponse responseTrackingScore = sendMSearchRequest(index, List.of(trackScoreItemsQuery)); - assertEquals(1, responseTrackingScore.responses().size()); - var hitsTrackingScore = responseTrackingScore.responses().get(0).result().hits().hits(); - assertEquals(3, hitsTrackingScore.size()); - assertNotNull(hitsTrackingScore.get(0).score()); - assertNotNull(hitsTrackingScore.get(1).score()); - assertNotNull(hitsTrackingScore.get(2).score()); - } - - @Test - public void shouldReturnMultiSearchesAboveMinScore() throws Exception { - String index = "multiple_searches_request_min_score"; - createTestDocuments(index); - - RequestItem sortedItemsQuery = createMSearchFuzzyRequest(); - - MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); - assertEquals(1, response.responses().size()); - var hits = response.responses().get(0).result().hits().hits(); - assertEquals(3, hits.size()); - - double minScore = hits.get(2).score(); - double scoreBetweenFirstAndSecondLowest = (hits.get(1).score() + minScore) / 2; - - RequestItem minScoredItemsQuery = createMSearchFuzzyRequest(b -> b.minScore(scoreBetweenFirstAndSecondLowest)); - - MsearchResponse responseAboveMinScore = sendMSearchRequest(index, List.of(minScoredItemsQuery)); - assertEquals(1, responseAboveMinScore.responses().size()); - assertEquals(2, responseAboveMinScore.responses().get(0).result().hits().hits().size()); - } - - @Test - public void shouldReturnMultiSearchesApplyingPostFilter() throws Exception { - String index = "multiple_searches_request_post_filter"; - createTestDocuments(index); - - RequestItem filteredItemsQuery = createMSearchFuzzyRequest(b -> b.postFilter(createItemSizeSearchQuery("large"))); - - MsearchResponse response = sendMSearchRequest(index, List.of(filteredItemsQuery)); - assertEquals(1, response.responses().size()); - assertEquals(1, response.responses().get(0).result().hits().hits().size()); - } - - @Test - public void shouldReturnMultiSearchesSearchAfter() throws Exception { - String index = "multiple_searches_request_search_after"; - createTestDocuments(index); - - RequestItem sortedItemsQuery = createMSearchSortedFuzzyRequest(); - - MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); - assertEquals(1, response.responses().size()); - assertEquals(3, response.responses().get(0).result().hits().hits().size()); - - List sorts = response.responses().get(0).result().hits().hits().get(1).sort(); - RequestItem sortedAfterItemsQuery = createMSearchSortedFuzzyRequest(b -> b.searchAfter(sorts)); - - MsearchResponse response2 = sendMSearchRequest(index, List.of(sortedAfterItemsQuery)); - assertEquals(1, response2.responses().size()); - assertEquals(1, response2.responses().get(0).result().hits().hits().size()); - } - - @Test - public void shouldReturnMultiSearchesScriptFields() throws Exception { - String index = "multiple_searches_request_script_fields"; - createTestDocuments(index); - - RequestItem sortedItemsQuery = createMSearchSortedFuzzyRequest(); - - MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); - assertEquals(1, response.responses().size()); - var hits = response.responses().get(0).result().hits().hits(); - assertEquals(3, hits.size()); - assertNull(hits.get(0).score()); - assertNull(hits.get(1).score()); - assertNull(hits.get(2).score()); - - Map scriptFields = new HashMap<>(); - scriptFields.put("quantity", new ScriptField.Builder().script(Script.of(s -> s.inline(new InlineScript.Builder() - .lang("painless") - .source("doc['quantity'].value + params.inc") - .params("inc", JsonData.of(1)) - .build()))) - .build()); - - - RequestItem requestItem = createMSearchQueryWithScriptFields("small", scriptFields); - - MsearchResponse responseWithScriptFields = sendMSearchRequest(index, List.of(requestItem)); - var hitsWithScriptFields = responseWithScriptFields.responses().get(0).result().hits().hits(); - assertEquals(2, hitsWithScriptFields.size()); - // validating that the quantity for small items is increased by 1 - ObjectMapper mapper = new ObjectMapper(); - JsonNode node = mapper.readTree(hitsWithScriptFields.get(0).fields().get("quantity").toString()); - assertEquals(2, (int) mapper.treeToValue(node.get(0), int.class)); - - node = mapper.readTree(hitsWithScriptFields.get(1).fields().get("quantity").toString()); - assertEquals(3, (int) mapper.treeToValue(node.get(0), int.class)); - } - - - private void assertResponseSources(MultiSearchResponseItem response) { - List> hitsWithHighlights = response.result().hits().hits(); - assertEquals(2, hitsWithHighlights.size()); - for (Hit shopItemHit : hitsWithHighlights) { - assertNotNull(shopItemHit.source()); - assertNotNull(shopItemHit.source().getName()); - assertNotNull(shopItemHit.source().getSize()); - assertNull(shopItemHit.source().getCompany()); - } - } - - private void assertResponseHighlights(MultiSearchResponseItem response) { - List> hitsWithHighlights = response.result().hits().hits(); - assertEquals(2, hitsWithHighlights.size()); - assertEquals(1, hitsWithHighlights.get(0).highlight().size()); - assertEquals(1, hitsWithHighlights.get(1).highlight().size()); - } - - private RequestItem createMSearchQuery(String itemSize) { - return createMSearchQuery(itemSize, null, List.of()); - } - - private RequestItem createMSearchQueryWithScriptFields(String itemSize, Map scriptFields) { - return RequestItem.of(item -> item.header(header -> header) - .body(body -> body.query(createItemSizeSearchQuery(itemSize)) - .scriptFields(scriptFields) - ) - ); - } - - private RequestItem createMSearchQueryWithHighlight(String itemSize) { - return createMSearchQuery(itemSize, "size", List.of()); - } - - private RequestItem createMSearchQueryWithSelectedSourceFields(String itemSize) { - return createMSearchQuery(itemSize, null, List.of("name", "size")); - } - - private RequestItem createMSearchQuery(String itemSize, String fieldName, List sources) { - return RequestItem.of(item -> item.header(header -> header) - .body(body -> body.query(createItemSizeSearchQuery(itemSize)) - .highlight(createHighlight(fieldName)) - .source(createSourcesConfig(sources)) - ) - ); - } - - private RequestItem createMSearchSortedFuzzyRequest() { - return createMSearchSortedFuzzyRequest(b -> b); - } - - private RequestItem createMSearchSortedFuzzyRequest(Function> additional) { - return createMSearchFuzzyRequest(b -> additional.apply(b - .sort(SortOptions.of(sort -> sort.field(FieldSort.of(f -> f.field("quantity").order(SortOrder.Asc))))))); - } - - private RequestItem createMSearchFuzzyRequest() { - return createMSearchFuzzyRequest(b -> b); - } - - private RequestItem createMSearchFuzzyRequest(Function> additional) { - return RequestItem.of(item -> item.header(header -> header) - .body(b -> additional.apply(b.query(createNameSearchFuzzyQuery()))) - ); - } - - private Query createNameSearchFuzzyQuery() { - return Query.of(filter -> filter.fuzzy( - FuzzyQuery.of(term -> term.field("name") - .value(FieldValue.of("rammer")) - ) - ) - ); - } - - private SourceConfig createSourcesConfig(List sources) { - return sources.isEmpty() ? null : SourceConfig.of(builder -> builder.filter(filter -> filter.includes(sources))); - } - - private Highlight createHighlight(String fieldName) { - return fieldName != null ? Highlight.of( - builder -> builder.fields( - fieldName, HighlightField.of( - field -> field.preTags("") - .postTags("") - ) - ) - ) : null; - } - - private Query createItemSizeSearchQuery(String itemSize) { - return Query.of(query -> query.bool(builder -> builder.filter( - filter -> filter.term( - TermQuery.of(term -> term.field("size") - .value(FieldValue.of(itemSize)) - ) - ) - ))); - } - - private MsearchResponse sendMSearchRequest(String index, List searches) throws IOException { - return javaClient().msearch(builder -> builder.index(List.of(index)).searches(searches), ShopItem.class); - } - - private void createTestDocuments(String index) throws IOException { - javaClient().create(_1 -> _1.index(index).id("1").document(createItem("hummer", "huge", "yes", 2)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("2").document(createItem("jammer", "huge", "yes", 1)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("3").document(createItem("hammer", "large", "yes", 3)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("4").document(createItem("drill", "large", "yes", 3)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("5").document(createItem("jack", "medium", "yes", 2)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("6").document(createItem("wrench", "medium", "no", 3)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("7").document(createItem("screws", "small", "no", 1)).refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("8").document(createItem("nuts", "small", "no", 2)).refresh(Refresh.True)); - } - - private ShopItem createItem(String name, String size, String company, int quantity) { - return new ShopItem(name, size, company, quantity); - } - - public static class ShopItem { - private String name; - private String size; - private String company; - private int quantity; - - public ShopItem() { - } - - public ShopItem(String name, String size, String company, int quantity) { - this.name = name; - this.size = size; - this.company = company; - this.quantity = quantity; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getSize() { - return size; - } - - public void setSize(String size) { - this.size = size; - } - - public String getCompany() { - return company; - } - - public void setCompany(String company) { - this.company = company; - } - - public int getQuantity() { - return quantity; - } - - public void setQuantity(int quantity) { - this.quantity = quantity; - } - } + @Test + public void shouldReturnMultipleSearches() throws Exception { + String index = "multiple_searches_request"; + createTestDocuments(index); + + RequestItem largeItemsQuery = createMSearchQuery("large"); + RequestItem mediumItemsQuery = createMSearchQuery("medium"); + RequestItem smallItemsQuery = createMSearchQuery("small"); + + MsearchResponse response = sendMSearchRequest(index, List.of(largeItemsQuery, mediumItemsQuery, smallItemsQuery)); + assertEquals(3, response.responses().size()); + assertEquals(2, response.responses().get(0).result().hits().hits().size()); + assertEquals(2, response.responses().get(1).result().hits().hits().size()); + assertEquals(2, response.responses().get(2).result().hits().hits().size()); + } + + @Test + public void shouldReturnHighlightsInMultipleSearches() throws Exception { + String index = "multiple_searches_request_with_highlights"; + createTestDocuments(index); + + RequestItem largeItemsQuery = createMSearchQueryWithHighlight("large"); + RequestItem mediumItemsQuery = createMSearchQueryWithHighlight("medium"); + RequestItem smallItemsQuery = createMSearchQueryWithHighlight("small"); + + MsearchResponse response = sendMSearchRequest(index, List.of(largeItemsQuery, mediumItemsQuery, smallItemsQuery)); + assertEquals(3, response.responses().size()); + assertResponseHighlights(response.responses().get(0)); + assertResponseHighlights(response.responses().get(1)); + assertResponseHighlights(response.responses().get(2)); + } + + @Test + public void shouldReturnMultiSearchesWithSelectedSourceFieldsOnly() throws Exception { + String index = "multiple_searches_request_with_sources"; + createTestDocuments(index); + + RequestItem largeItemsQuery = createMSearchQueryWithSelectedSourceFields("large"); + RequestItem mediumItemsQuery = createMSearchQueryWithSelectedSourceFields("medium"); + RequestItem smallItemsQuery = createMSearchQueryWithSelectedSourceFields("small"); + + MsearchResponse response = sendMSearchRequest(index, List.of(largeItemsQuery, mediumItemsQuery, smallItemsQuery)); + assertEquals(3, response.responses().size()); + assertResponseSources(response.responses().get(0)); + assertResponseSources(response.responses().get(1)); + assertResponseSources(response.responses().get(2)); + } + + @Test + public void shouldReturnMultiSearchesSorted() throws Exception { + String index = "multiple_searches_request_sort"; + createTestDocuments(index); + + RequestItem sortedItemsQuery = createMSearchSortedFuzzyRequest(); + + MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); + assertEquals(1, response.responses().size()); + var hits = response.responses().get(0).result().hits().hits(); + assertEquals(3, hits.size()); + + assertEquals("hammer", hits.get(2).source().getName()); + } + + @Test + public void shouldReturnMultiSearchesTrackingScores() throws Exception { + String index = "multiple_searches_request_track_scores"; + createTestDocuments(index); + + RequestItem sortedItemsQuery = createMSearchSortedFuzzyRequest(); + + MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); + assertEquals(1, response.responses().size()); + var hits = response.responses().get(0).result().hits().hits(); + assertEquals(3, hits.size()); + assertNull(hits.get(0).score()); + assertNull(hits.get(1).score()); + assertNull(hits.get(2).score()); + + RequestItem trackScoreItemsQuery = createMSearchSortedFuzzyRequest(b -> b.trackScores(true)); + + MsearchResponse responseTrackingScore = sendMSearchRequest(index, List.of(trackScoreItemsQuery)); + assertEquals(1, responseTrackingScore.responses().size()); + var hitsTrackingScore = responseTrackingScore.responses().get(0).result().hits().hits(); + assertEquals(3, hitsTrackingScore.size()); + assertNotNull(hitsTrackingScore.get(0).score()); + assertNotNull(hitsTrackingScore.get(1).score()); + assertNotNull(hitsTrackingScore.get(2).score()); + } + + @Test + public void shouldReturnMultiSearchesAboveMinScore() throws Exception { + String index = "multiple_searches_request_min_score"; + createTestDocuments(index); + + RequestItem sortedItemsQuery = createMSearchFuzzyRequest(); + + MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); + assertEquals(1, response.responses().size()); + var hits = response.responses().get(0).result().hits().hits(); + assertEquals(3, hits.size()); + + double minScore = hits.get(2).score(); + double scoreBetweenFirstAndSecondLowest = (hits.get(1).score() + minScore) / 2; + + RequestItem minScoredItemsQuery = createMSearchFuzzyRequest(b -> b.minScore(scoreBetweenFirstAndSecondLowest)); + + MsearchResponse responseAboveMinScore = sendMSearchRequest(index, List.of(minScoredItemsQuery)); + assertEquals(1, responseAboveMinScore.responses().size()); + assertEquals(2, responseAboveMinScore.responses().get(0).result().hits().hits().size()); + } + + @Test + public void shouldReturnMultiSearchesApplyingPostFilter() throws Exception { + String index = "multiple_searches_request_post_filter"; + createTestDocuments(index); + + RequestItem filteredItemsQuery = createMSearchFuzzyRequest(b -> b.postFilter(createItemSizeSearchQuery("large"))); + + MsearchResponse response = sendMSearchRequest(index, List.of(filteredItemsQuery)); + assertEquals(1, response.responses().size()); + assertEquals(1, response.responses().get(0).result().hits().hits().size()); + } + + @Test + public void shouldReturnMultiSearchesSearchAfter() throws Exception { + String index = "multiple_searches_request_search_after"; + createTestDocuments(index); + + RequestItem sortedItemsQuery = createMSearchSortedFuzzyRequest(); + + MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); + assertEquals(1, response.responses().size()); + assertEquals(3, response.responses().get(0).result().hits().hits().size()); + + List sorts = response.responses().get(0).result().hits().hits().get(1).sort(); + RequestItem sortedAfterItemsQuery = createMSearchSortedFuzzyRequest(b -> b.searchAfter(sorts)); + + MsearchResponse response2 = sendMSearchRequest(index, List.of(sortedAfterItemsQuery)); + assertEquals(1, response2.responses().size()); + assertEquals(1, response2.responses().get(0).result().hits().hits().size()); + } + + @Test + public void shouldReturnMultiSearchesScriptFields() throws Exception { + String index = "multiple_searches_request_script_fields"; + createTestDocuments(index); + + RequestItem sortedItemsQuery = createMSearchSortedFuzzyRequest(); + + MsearchResponse response = sendMSearchRequest(index, List.of(sortedItemsQuery)); + assertEquals(1, response.responses().size()); + var hits = response.responses().get(0).result().hits().hits(); + assertEquals(3, hits.size()); + assertNull(hits.get(0).score()); + assertNull(hits.get(1).score()); + assertNull(hits.get(2).score()); + + Map scriptFields = new HashMap<>(); + scriptFields.put( + "quantity", + new ScriptField.Builder().script( + Script.of( + s -> s.inline( + new InlineScript.Builder().lang("painless") + .source("doc['quantity'].value + params.inc") + .params("inc", JsonData.of(1)) + .build() + ) + ) + ).build() + ); + + RequestItem requestItem = createMSearchQueryWithScriptFields("small", scriptFields); + + MsearchResponse responseWithScriptFields = sendMSearchRequest(index, List.of(requestItem)); + var hitsWithScriptFields = responseWithScriptFields.responses().get(0).result().hits().hits(); + assertEquals(2, hitsWithScriptFields.size()); + // validating that the quantity for small items is increased by 1 + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(hitsWithScriptFields.get(0).fields().get("quantity").toString()); + assertEquals(2, (int) mapper.treeToValue(node.get(0), int.class)); + + node = mapper.readTree(hitsWithScriptFields.get(1).fields().get("quantity").toString()); + assertEquals(3, (int) mapper.treeToValue(node.get(0), int.class)); + } + + private void assertResponseSources(MultiSearchResponseItem response) { + List> hitsWithHighlights = response.result().hits().hits(); + assertEquals(2, hitsWithHighlights.size()); + for (Hit shopItemHit : hitsWithHighlights) { + assertNotNull(shopItemHit.source()); + assertNotNull(shopItemHit.source().getName()); + assertNotNull(shopItemHit.source().getSize()); + assertNull(shopItemHit.source().getCompany()); + } + } + + private void assertResponseHighlights(MultiSearchResponseItem response) { + List> hitsWithHighlights = response.result().hits().hits(); + assertEquals(2, hitsWithHighlights.size()); + assertEquals(1, hitsWithHighlights.get(0).highlight().size()); + assertEquals(1, hitsWithHighlights.get(1).highlight().size()); + } + + private RequestItem createMSearchQuery(String itemSize) { + return createMSearchQuery(itemSize, null, List.of()); + } + + private RequestItem createMSearchQueryWithScriptFields(String itemSize, Map scriptFields) { + return RequestItem.of( + item -> item.header(header -> header).body(body -> body.query(createItemSizeSearchQuery(itemSize)).scriptFields(scriptFields)) + ); + } + + private RequestItem createMSearchQueryWithHighlight(String itemSize) { + return createMSearchQuery(itemSize, "size", List.of()); + } + + private RequestItem createMSearchQueryWithSelectedSourceFields(String itemSize) { + return createMSearchQuery(itemSize, null, List.of("name", "size")); + } + + private RequestItem createMSearchQuery(String itemSize, String fieldName, List sources) { + return RequestItem.of( + item -> item.header(header -> header) + .body( + body -> body.query(createItemSizeSearchQuery(itemSize)) + .highlight(createHighlight(fieldName)) + .source(createSourcesConfig(sources)) + ) + ); + } + + private RequestItem createMSearchSortedFuzzyRequest() { + return createMSearchSortedFuzzyRequest(b -> b); + } + + private RequestItem createMSearchSortedFuzzyRequest(Function> additional) { + return createMSearchFuzzyRequest( + b -> additional.apply(b.sort(SortOptions.of(sort -> sort.field(FieldSort.of(f -> f.field("quantity").order(SortOrder.Asc)))))) + ); + } + + private RequestItem createMSearchFuzzyRequest() { + return createMSearchFuzzyRequest(b -> b); + } + + private RequestItem createMSearchFuzzyRequest(Function> additional) { + return RequestItem.of(item -> item.header(header -> header).body(b -> additional.apply(b.query(createNameSearchFuzzyQuery())))); + } + + private Query createNameSearchFuzzyQuery() { + return Query.of(filter -> filter.fuzzy(FuzzyQuery.of(term -> term.field("name").value(FieldValue.of("rammer"))))); + } + + private SourceConfig createSourcesConfig(List sources) { + return sources.isEmpty() ? null : SourceConfig.of(builder -> builder.filter(filter -> filter.includes(sources))); + } + + private Highlight createHighlight(String fieldName) { + return fieldName != null + ? Highlight.of( + builder -> builder.fields( + fieldName, + HighlightField.of(field -> field.preTags("").postTags("")) + ) + ) + : null; + } + + private Query createItemSizeSearchQuery(String itemSize) { + return Query.of( + query -> query.bool( + builder -> builder.filter(filter -> filter.term(TermQuery.of(term -> term.field("size").value(FieldValue.of(itemSize))))) + ) + ); + } + + private MsearchResponse sendMSearchRequest(String index, List searches) throws IOException { + return javaClient().msearch(builder -> builder.index(List.of(index)).searches(searches), ShopItem.class); + } + + private void createTestDocuments(String index) throws IOException { + javaClient().create(_1 -> _1.index(index).id("1").document(createItem("hummer", "huge", "yes", 2)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("2").document(createItem("jammer", "huge", "yes", 1)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("3").document(createItem("hammer", "large", "yes", 3)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("4").document(createItem("drill", "large", "yes", 3)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("5").document(createItem("jack", "medium", "yes", 2)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("6").document(createItem("wrench", "medium", "no", 3)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("7").document(createItem("screws", "small", "no", 1)).refresh(Refresh.True)); + javaClient().create(_1 -> _1.index(index).id("8").document(createItem("nuts", "small", "no", 2)).refresh(Refresh.True)); + } + + private ShopItem createItem(String name, String size, String company, int quantity) { + return new ShopItem(name, size, company, quantity); + } + + public static class ShopItem { + private String name; + private String size; + private String company; + private int quantity; + + public ShopItem() {} + + public ShopItem(String name, String size, String company, int quantity) { + this.name = name; + this.size = size; + this.company = company; + this.quantity = quantity; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSize() { + return size; + } + + public void setSize(String size) { + this.size = size; + } + + public String getCompany() { + return company; + } + + public void setCompany(String company) { + this.company = company; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractNodesIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractNodesIT.java index 3cb83a6793..e8376e572a 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractNodesIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractNodesIT.java @@ -14,7 +14,6 @@ import java.io.IOException; import java.util.Arrays; - import org.opensearch.client.opensearch.nodes.NodesStatsResponse; import org.opensearch.client.opensearch.tasks.ListRequest; import org.opensearch.client.opensearch.tasks.ListResponse; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractPingAndInfoIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractPingAndInfoIT.java index e529da8339..9310764dba 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractPingAndInfoIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractPingAndInfoIT.java @@ -8,15 +8,14 @@ package org.opensearch.client.opensearch.integTest; +import java.io.IOException; +import java.util.Map; import org.apache.hc.client5.http.classic.methods.HttpGet; import org.opensearch.client.Request; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch.core.InfoResponse; import org.opensearch.client.transport.endpoints.BooleanResponse; -import java.io.IOException; -import java.util.Map; - public abstract class AbstractPingAndInfoIT extends OpenSearchJavaClientTestCase { public void testPing() throws IOException { BooleanResponse ping = javaClient().ping(); @@ -43,4 +42,4 @@ public void testInfo() throws IOException { assertEquals(versionMap.get("lucene_version"), info.version().luceneVersion()); assertTrue(versionMap.get("number").toString().startsWith(info.version().number())); } -} \ No newline at end of file +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java index c52908bbcb..8280cae0d1 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java @@ -32,6 +32,14 @@ package org.opensearch.client.opensearch.integTest; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import org.junit.Test; import org.opensearch.Version; import org.opensearch.client.opensearch.OpenSearchAsyncClient; @@ -86,15 +94,6 @@ import org.opensearch.client.opensearch.model.ModelTestCase; import org.opensearch.client.transport.endpoints.BooleanResponse; -import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; - public abstract class AbstractRequestIT extends OpenSearchJavaClientTestCase { @Test @@ -129,15 +128,19 @@ public void testIndexCreation() throws Exception { @Test public void testIndexSettings() throws Exception { var createResponse = javaClient().indices() - .create(r -> r.index("test-settings") - .settings(s -> s - .translog(Translog.of(tl -> tl.syncInterval(Time.of(t -> t.time("10s"))))) - .mapping(m -> m - .fieldNameLength(f -> f.limit(300L)) - .totalFields(f -> f.limit(30L)) - .nestedFields(f -> f.limit(10L)) - .depth(f -> f.limit(3L)) - .nestedObjects(f -> f.limit(9L))))); + .create( + r -> r.index("test-settings") + .settings( + s -> s.translog(Translog.of(tl -> tl.syncInterval(Time.of(t -> t.time("10s"))))) + .mapping( + m -> m.fieldNameLength(f -> f.limit(300L)) + .totalFields(f -> f.limit(30L)) + .nestedFields(f -> f.limit(10L)) + .depth(f -> f.limit(3L)) + .nestedObjects(f -> f.limit(9L)) + ) + ) + ); assertTrue(createResponse.acknowledged()); assertTrue(createResponse.shardsAcknowledged()); @@ -163,16 +166,20 @@ public void testIndexSettings() throws Exception { assertNotNull(createdMappingSettings.nestedObjects()); assertEquals(9L, (Object) createdMappingSettings.nestedObjects().limit()); - var putSettingsResponse = javaClient().indices().putSettings(r -> r - .index("test-settings") - .settings(s -> s - .translog(Translog.of(tl -> tl.syncInterval(Time.of(t -> t.time("5s"))))) - .mapping(m -> m - .fieldNameLength(f -> f.limit(400L)) - .totalFields(f -> f.limit(130L)) - .nestedFields(f -> f.limit(110L)) - .depth(f -> f.limit(13L)) - .nestedObjects(f -> f.limit(19L))))); + var putSettingsResponse = javaClient().indices() + .putSettings( + r -> r.index("test-settings") + .settings( + s -> s.translog(Translog.of(tl -> tl.syncInterval(Time.of(t -> t.time("5s"))))) + .mapping( + m -> m.fieldNameLength(f -> f.limit(400L)) + .totalFields(f -> f.limit(130L)) + .nestedFields(f -> f.limit(110L)) + .depth(f -> f.limit(13L)) + .nestedObjects(f -> f.limit(19L)) + ) + ) + ); assertTrue(putSettingsResponse.acknowledged()); var updatedSettings = javaClient().indices().getSettings(r -> r.index("test-settings")); @@ -204,9 +211,7 @@ public void testDataIngestion() throws Exception { String index = "ingest-test"; // Create an index - CreateIndexResponse createIndexResponse = javaClient().indices().create(b -> b - .index(index) - ); + CreateIndexResponse createIndexResponse = javaClient().indices().create(b -> b.index(index)); assertEquals(index, createIndexResponse.index()); @@ -219,8 +224,8 @@ public void testDataIngestion() throws Exception { appData.setIntValue(1337); appData.setMsg("foo"); - String docId = javaClient().index(b -> b - .index(index) + String docId = javaClient().index( + b -> b.index(index) .id("my/Id") // test with url-unsafe string .document(appData) .refresh(Refresh.True) // Make it visible for search @@ -230,36 +235,22 @@ public void testDataIngestion() throws Exception { // Check auto-created mapping GetMappingResponse mapping = javaClient().indices().getMapping(b -> b.index(index)); - assertEquals( - Property.Kind.Long, - mapping.get("ingest-test").mappings().properties().get("intValue")._kind() - ); + assertEquals(Property.Kind.Long, mapping.get("ingest-test").mappings().properties().get("intValue")._kind()); // Query by id - AppData esData = javaClient().get(b -> b - .index(index) - .id(docId) - , AppData.class - ).source(); + AppData esData = javaClient().get(b -> b.index(index).id(docId), AppData.class).source(); assertEquals(1337, esData.getIntValue()); assertEquals("foo", esData.getMsg()); // Query by id a non-existing document - final GetResponse notExists = javaClient().get(b -> b - .index(index) - .id("some-random-id") - , AppData.class - ); + final GetResponse notExists = javaClient().get(b -> b.index(index).id("some-random-id"), AppData.class); assertFalse(notExists.found()); assertNull(notExists.source()); // Search - SearchResponse search = javaClient().search(b -> b - .index(index) - , AppData.class - ); + SearchResponse search = javaClient().search(b -> b.index(index), AppData.class); long hits = search.hits().total().value(); assertEquals(1, hits); @@ -268,21 +259,14 @@ public void testDataIngestion() throws Exception { assertEquals(1337, esData.getIntValue()); assertEquals("foo", esData.getMsg()); - RequestItem item = RequestItem.of(_1 -> _1 - .header(_2 -> _2.index("test")) - .body(_2 -> _2.size(4)) - ); + RequestItem item = RequestItem.of(_1 -> _1.header(_2 -> _2.index("test")).body(_2 -> _2.size(4))); // MSearch: 1st search on an existing index, 2nd one on a non-existing index - final MsearchResponse msearch = javaClient().msearch(_0 -> _0 - .searches(_1 -> _1 - .header(_3 -> _3.index(index)) - .body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5))) - ).searches(_1 -> _1 - .header(_3 -> _3.index("non-existing")) - .body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5))) - ) - , AppData.class); + final MsearchResponse msearch = javaClient().msearch( + _0 -> _0.searches(_1 -> _1.header(_3 -> _3.index(index)).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))) + .searches(_1 -> _1.header(_3 -> _3.index("non-existing")).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))), + AppData.class + ); assertEquals(2, msearch.responses().size()); assertTrue(msearch.responses().get(0).isResult()); @@ -317,19 +301,9 @@ public void testBulkRequest() throws IOException { appData.setIntValue(42); appData.setMsg("Some message"); - BulkResponse bulk = javaClient().bulk(_0 -> _0 - .operations(_1 -> _1 - .create(_2 -> _2 - .index("foo") - .id("abc") - .document(appData) - )) - .operations(_1 -> _1 - .create(_2 -> _2 - .index("foo") - .id("def") - .document(appData) - )) + BulkResponse bulk = javaClient().bulk( + _0 -> _0.operations(_1 -> _1.create(_2 -> _2.index("foo").id("abc").document(appData))) + .operations(_1 -> _1.create(_2 -> _2.index("foo").id("def").document(appData))) ); assertFalse(bulk.errors()); @@ -353,19 +327,12 @@ public void testTrackTotalHitsFalse() throws Exception { appData.setIntValue(1337); appData.setMsg("foo"); - javaClient().index(b -> b - .index(index) - .id("myId") - .document(appData) - .refresh(Refresh.True) // Make it visible for search + javaClient().index( + b -> b.index(index).id("myId").document(appData).refresh(Refresh.True) // Make it visible for search ).id(); // Search - SearchResponse search = javaClient().search(b -> b - .index(index) - .trackTotalHits(t -> t.enabled(false)) - , AppData.class - ); + SearchResponse search = javaClient().search(b -> b.index(index).trackTotalHits(t -> t.enabled(false)), AppData.class); List> hits = search.hits().hits(); AppData appDataResult = search.hits().hits().get(0).source(); @@ -379,12 +346,7 @@ public void testRefresh() throws IOException { appData.setIntValue(42); appData.setMsg("Some message"); - IndexResponse ir = javaClient().index(_0 -> _0 - .index("test") - .id("1") - .document(appData) - .refresh(Refresh.WaitFor) - ); + IndexResponse ir = javaClient().index(_0 -> _0.index("test").id("1").document(appData).refresh(Refresh.WaitFor)); assertEquals("1", ir.id()); } @@ -395,21 +357,16 @@ public void errorResponse() throws Exception { assertFalse(exists.value()); OpenSearchException ex = assertThrows(OpenSearchException.class, () -> { - GetResponse response = javaClient().get( - _0 -> _0.index("doesnotexist").id("reallynot"), String.class - ); + GetResponse response = javaClient().get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class); }); assertEquals(404, ex.status()); assertEquals("index_not_found_exception", ex.error().type()); assertEquals("doesnotexist", ex.error().metadata().get("index").to(String.class)); - ExecutionException ee = assertThrows(ExecutionException.class, () -> { OpenSearchAsyncClient aClient = new OpenSearchAsyncClient(javaClient()._transport()); - GetResponse response = aClient.get( - _0 -> _0.index("doesnotexist").id("reallynot"), String.class - ).get(); + GetResponse response = aClient.get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class).get(); }); ex = ((OpenSearchException) ee.getCause()); @@ -423,8 +380,8 @@ public void testErrorStatusCodeWithValidResponse() throws IOException { // Transports should first try to decode an error, and if they fail because of missing properties for // the error type, then try to decode the regular request. - final ClearScrollResponse clearResponse = javaClient().clearScroll(b -> b.scrollId( - "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==") + final ClearScrollResponse clearResponse = javaClient().clearScroll( + b -> b.scrollId("DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==") ); assertEquals(0, clearResponse.numFreed()); } @@ -436,20 +393,15 @@ public void testSearchAggregation() throws IOException { javaClient().create(_1 -> _1.index("products").id("B").document(new Product(15)).refresh(Refresh.True)); javaClient().create(_1 -> _1.index("products").id("C").document(new Product(25)).refresh(Refresh.True)); - SearchResponse searchResponse = javaClient().search(_1 -> _1 - .index("products") - .size(0) - .aggregations( - "prices", _3 -> _3 - .histogram(_4 -> _4 - .field("price") - .interval(10.0) - ).aggregations( - "average", _5 -> _5 - .avg(_6 -> _6.field("price")) - ) - ) - , Product.class + SearchResponse searchResponse = javaClient().search( + _1 -> _1.index("products") + .size(0) + .aggregations( + "prices", + _3 -> _3.histogram(_4 -> _4.field("price").interval(10.0)) + .aggregations("average", _5 -> _5.avg(_6 -> _6.field("price"))) + ), + Product.class ); HistogramAggregate prices = searchResponse.aggregations().get("prices").histogram(); @@ -471,23 +423,21 @@ public void testSubAggregation() throws IOException { List fieldValues = List.of(FieldValue.of("Blue")); - SearchRequest searchRequest = SearchRequest.of(_1 -> _1 - .index("products") + SearchRequest searchRequest = SearchRequest.of( + _1 -> _1.index("products") .size(0) .aggregations( - "price", _3 -> _3 - .aggregations(Map.of("price", TermsAggregation.of(_4 -> _4 - .field("price")) - ._toAggregation())) - .filter(BoolQuery.of(_5 -> _5 - .filter(List.of(TermsQuery.of(_6 -> _6 - .field("color.keyword") - .terms(_7 -> _7 - .value(fieldValues))) - ._toQuery()))) - ._toQuery() + "price", + _3 -> _3.aggregations(Map.of("price", TermsAggregation.of(_4 -> _4.field("price"))._toAggregation())) + .filter( + BoolQuery.of( + _5 -> _5.filter( + List.of(TermsQuery.of(_6 -> _6.field("color.keyword").terms(_7 -> _7.value(fieldValues)))._toQuery()) ) - )); + )._toQuery() + ) + ) + ); SearchResponse searchResponse = javaClient().search(searchRequest, Product.class); Aggregate prices = searchResponse.aggregations().get("price")._get()._toAggregate(); @@ -507,14 +457,23 @@ public void testChildAggregation() throws IOException { String question = "question"; String answer = "answer"; - javaClient().indices().create(c -> c.index(index).mappings(m -> m.properties("join", p -> p - .join(j -> j.relations(Collections.singletonMap(question, Collections.singletonList(answer))))))); + javaClient().indices() + .create( + c -> c.index(index) + .mappings( + m -> m.properties( + "join", + p -> p.join(j -> j.relations(Collections.singletonMap(question, Collections.singletonList(answer)))) + ) + ) + ); javaClient().index(i -> i.index(index).id("1").document(new Question("exists")).refresh(Refresh.True)); javaClient().index(i -> i.index(index).id("2").routing("1").document(new Answer("true", "1")).refresh(Refresh.True)); javaClient().index(i -> i.index(index).id("3").routing("1").document(new Answer("false", "1")).refresh(Refresh.True)); - SearchRequest searchRequest = SearchRequest.of(r -> r.index(index).size(0) - .aggregations(answer, a -> a.children(c -> c.type(answer)))); + SearchRequest searchRequest = SearchRequest.of( + r -> r.index(index).size(0).aggregations(answer, a -> a.children(c -> c.type(answer))) + ); SearchResponse searchResponse = javaClient().search(searchRequest, Void.class); @@ -529,18 +488,14 @@ public void testGetMapping() throws Exception { Map fields = Collections.singletonMap("keyword", Property.of(p -> p.keyword(k -> k.ignoreAbove(256)))); Property text = Property.of(p -> p.text(t -> t.fields(fields))); - javaClient().indices().create(c -> c - .index(index) - .mappings(m -> m - .properties("id", text) - .properties("name", p -> p - .object(o -> o - .properties("first", text) - .properties("last", text) - ) - ) - ) - ); + javaClient().indices() + .create( + c -> c.index(index) + .mappings( + m -> m.properties("id", text) + .properties("name", p -> p.object(o -> o.properties("first", text).properties("last", text))) + ) + ); GetMappingResponse mr = javaClient().indices().getMapping(mrb -> mrb.index(index)); @@ -567,54 +522,28 @@ public void testCompletionSuggesterFailure() throws IOException { String index = "test-completion-suggester-failure"; - - Property intValueProp = new Property.Builder() - .long_(v -> v) - .build(); - Property msgCompletionProp = new Property.Builder() - .completion(c -> c) - .build(); - javaClient().indices().create(c -> c - .index(index) - .mappings(m -> m - .properties("intValue", intValueProp) - .properties("msg", msgCompletionProp))); + Property intValueProp = new Property.Builder().long_(v -> v).build(); + Property msgCompletionProp = new Property.Builder().completion(c -> c).build(); + javaClient().indices() + .create(c -> c.index(index).mappings(m -> m.properties("intValue", intValueProp).properties("msg", msgCompletionProp))); AppData appData = new AppData(); appData.setIntValue(1337); appData.setMsg("foo"); - javaClient().index(b -> b - .index(index) - .id("1") - .document(appData) - .refresh(Refresh.True)); + javaClient().index(b -> b.index(index).id("1").document(appData).refresh(Refresh.True)); appData.setIntValue(1338); appData.setMsg("foobar"); - javaClient().index(b -> b - .index(index) - .id("2") - .document(appData) - .refresh(Refresh.True)); + javaClient().index(b -> b.index(index).id("2").document(appData).refresh(Refresh.True)); String suggesterName = "msgSuggester"; - CompletionSuggester completionSuggester = FieldSuggesterBuilders.completion() - .field("msg") - .size(1) - .build(); - FieldSuggester fieldSuggester = new FieldSuggester.Builder().prefix("xyz") - .completion(completionSuggester) - .build(); - Suggester suggester = new Suggester.Builder() - .suggesters(Collections.singletonMap(suggesterName, fieldSuggester)) - .build(); - SearchRequest searchRequest = new SearchRequest.Builder() - .index(index) - .suggest(suggester) - .build(); + CompletionSuggester completionSuggester = FieldSuggesterBuilders.completion().field("msg").size(1).build(); + FieldSuggester fieldSuggester = new FieldSuggester.Builder().prefix("xyz").completion(completionSuggester).build(); + Suggester suggester = new Suggester.Builder().suggesters(Collections.singletonMap(suggesterName, fieldSuggester)).build(); + SearchRequest searchRequest = new SearchRequest.Builder().index(index).suggest(suggester).build(); SearchResponse response = javaClient().search(searchRequest, AppData.class); assertTrue(response.suggest().size() > 0); @@ -625,271 +554,196 @@ public void testCompletionSuggesterFailure() throws IOException { @Test public void testPit() throws IOException { - InfoResponse info = javaClient().info(); - String version = info.version().number(); - if (version.contains("SNAPSHOT")) { - version = version.split("-")[0]; - } - assumeTrue("The PIT is supported in OpenSearch 2.4.0 and later", - Version.fromString(version).onOrAfter(Version.fromString("2.4.0"))); - - String index = "test-point-in-time"; - - javaClient().indices().create(c -> c - .index(index)); - - AppData appData = new AppData(); - appData.setIntValue(1337); - appData.setMsg("foo"); - - javaClient().index(b -> b - .index(index) - .id("1") - .document(appData) - .refresh(Refresh.True)); - CreatePitRequest createPitRequest = new CreatePitRequest.Builder() - .targetIndexes(Collections.singletonList(index)) - .keepAlive(new Time.Builder().time("100m").build()).build(); - - CreatePitResponse createPitResponse = javaClient() - .createPit(createPitRequest); - - assertNotNull(createPitResponse); - assertNotNull(createPitResponse.pitId()); - assertEquals(createPitResponse.shards().total(), - createPitResponse.shards().successful()); - - ListAllPitResponse listAllPitResponse = javaClient().listAllPit(); - - assertNotNull(listAllPitResponse); - assertNotNull(listAllPitResponse.pits()); - assertEquals(listAllPitResponse.pits().get(0).pitId(), createPitResponse.pitId()); - assertEquals(listAllPitResponse.pits().get(0).keepAlive(), Long.valueOf(6000000L)); - - DeletePitRequest deletePitRequest = new DeletePitRequest.Builder() - .pitId(Collections.singletonList(createPitResponse.pitId())).build(); - - DeletePitResponse deletePitResponse = javaClient() - .deletePit(deletePitRequest); - - assertNotNull(deletePitResponse); - assertNotNull(deletePitResponse.pits()); - assertEquals(deletePitResponse.pits().get(0).pitId(), createPitResponse.pitId()); - assertTrue(deletePitResponse.pits().get(0).successful()); + InfoResponse info = javaClient().info(); + String version = info.version().number(); + if (version.contains("SNAPSHOT")) { + version = version.split("-")[0]; + } + assumeTrue( + "The PIT is supported in OpenSearch 2.4.0 and later", + Version.fromString(version).onOrAfter(Version.fromString("2.4.0")) + ); + + String index = "test-point-in-time"; + + javaClient().indices().create(c -> c.index(index)); + + AppData appData = new AppData(); + appData.setIntValue(1337); + appData.setMsg("foo"); + + javaClient().index(b -> b.index(index).id("1").document(appData).refresh(Refresh.True)); + CreatePitRequest createPitRequest = new CreatePitRequest.Builder().targetIndexes(Collections.singletonList(index)) + .keepAlive(new Time.Builder().time("100m").build()) + .build(); + + CreatePitResponse createPitResponse = javaClient().createPit(createPitRequest); + + assertNotNull(createPitResponse); + assertNotNull(createPitResponse.pitId()); + assertEquals(createPitResponse.shards().total(), createPitResponse.shards().successful()); + + ListAllPitResponse listAllPitResponse = javaClient().listAllPit(); + + assertNotNull(listAllPitResponse); + assertNotNull(listAllPitResponse.pits()); + assertEquals(listAllPitResponse.pits().get(0).pitId(), createPitResponse.pitId()); + assertEquals(listAllPitResponse.pits().get(0).keepAlive(), Long.valueOf(6000000L)); + + DeletePitRequest deletePitRequest = new DeletePitRequest.Builder().pitId(Collections.singletonList(createPitResponse.pitId())) + .build(); + + DeletePitResponse deletePitResponse = javaClient().deletePit(deletePitRequest); + + assertNotNull(deletePitResponse); + assertNotNull(deletePitResponse.pits()); + assertEquals(deletePitResponse.pits().get(0).pitId(), createPitResponse.pitId()); + assertTrue(deletePitResponse.pits().get(0).successful()); } public void testCompletionSuggester() throws IOException { - String index = "test-completion-suggester"; - - Property intValueProp = new Property.Builder() - .long_(v -> v) - .build(); - Property msgCompletionProp = new Property.Builder() - .completion(c -> c) - .build(); - javaClient().indices().create(c -> c - .index(index) - .mappings(m -> m - .properties("intValue", intValueProp) - .properties("msg", msgCompletionProp))); - - AppData appData = new AppData(); - appData.setIntValue(1337); - appData.setMsg("foo"); - - javaClient().index(b -> b - .index(index) - .id("1") - .document(appData) - .refresh(Refresh.True)); - - appData.setIntValue(1338); - appData.setMsg("foobar"); - - javaClient().index(b -> b - .index(index) - .id("2") - .document(appData) - .refresh(Refresh.True)); - - String suggesterName = "msgSuggester"; - - CompletionSuggester completionSuggester = FieldSuggesterBuilders.completion() - .field("msg") - .size(1) - .build(); - FieldSuggester fieldSuggester = new FieldSuggester.Builder().prefix("foo") - .completion(completionSuggester) - .build(); - Suggester suggester = new Suggester.Builder() - .suggesters(Collections.singletonMap(suggesterName, fieldSuggester)) - .build(); - SearchRequest searchRequest = new SearchRequest.Builder() - .index(index) - .suggest(suggester) - .build(); - - SearchResponse response = javaClient().search(searchRequest, AppData.class); - assertTrue(response.suggest().size() > 0); - assertTrue(response.suggest().keySet().contains(suggesterName)); - assertNotNull(response.suggest().get(suggesterName)); - assertNotNull(response.suggest().get(suggesterName).get(0).completion().options()); - assertTrue(response.suggest().get(suggesterName).get(0).isCompletion()); - assertNotNull(response.suggest().get(suggesterName).get(0).completion().options()); - assertEquals(response.suggest().get(suggesterName).get(0).completion().options().get(0) - .text(), "foo"); + String index = "test-completion-suggester"; + + Property intValueProp = new Property.Builder().long_(v -> v).build(); + Property msgCompletionProp = new Property.Builder().completion(c -> c).build(); + javaClient().indices() + .create(c -> c.index(index).mappings(m -> m.properties("intValue", intValueProp).properties("msg", msgCompletionProp))); + + AppData appData = new AppData(); + appData.setIntValue(1337); + appData.setMsg("foo"); + + javaClient().index(b -> b.index(index).id("1").document(appData).refresh(Refresh.True)); + + appData.setIntValue(1338); + appData.setMsg("foobar"); + + javaClient().index(b -> b.index(index).id("2").document(appData).refresh(Refresh.True)); + + String suggesterName = "msgSuggester"; + + CompletionSuggester completionSuggester = FieldSuggesterBuilders.completion().field("msg").size(1).build(); + FieldSuggester fieldSuggester = new FieldSuggester.Builder().prefix("foo").completion(completionSuggester).build(); + Suggester suggester = new Suggester.Builder().suggesters(Collections.singletonMap(suggesterName, fieldSuggester)).build(); + SearchRequest searchRequest = new SearchRequest.Builder().index(index).suggest(suggester).build(); + + SearchResponse response = javaClient().search(searchRequest, AppData.class); + assertTrue(response.suggest().size() > 0); + assertTrue(response.suggest().keySet().contains(suggesterName)); + assertNotNull(response.suggest().get(suggesterName)); + assertNotNull(response.suggest().get(suggesterName).get(0).completion().options()); + assertTrue(response.suggest().get(suggesterName).get(0).isCompletion()); + assertNotNull(response.suggest().get(suggesterName).get(0).completion().options()); + assertEquals(response.suggest().get(suggesterName).get(0).completion().options().get(0).text(), "foo"); } @Test public void testTermSuggester() throws IOException { - String index = "test-term-suggester"; - - // term suggester does not require a special mapping - javaClient().indices().create(c -> c - .index(index)); - - AppData appData = new AppData(); - appData.setIntValue(1337); - appData.setMsg("foo"); - - javaClient().index(b -> b - .index(index) - .id("1") - .document(appData) - .refresh(Refresh.True)); - - appData.setIntValue(1338); - appData.setMsg("foobar"); - - javaClient().index(b -> b - .index(index) - .id("2") - .document(appData) - .refresh(Refresh.True)); - - String suggesterName = "msgSuggester"; - - TermSuggester termSuggester = FieldSuggesterBuilders.term() - .field("msg") - .size(1) - .build(); - FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("fool") - .term(termSuggester) - .build(); - Suggester suggester = new Suggester.Builder() - .suggesters(Collections.singletonMap(suggesterName, fieldSuggester)) - .build(); - SearchRequest searchRequest = new SearchRequest.Builder() - .index(index) - .suggest(suggester) - .build(); - - SearchResponse response = javaClient().search(searchRequest, AppData.class); - assertTrue(response.suggest().size() > 0); - assertTrue(response.suggest().keySet().contains(suggesterName)); - assertNotNull(response.suggest().get(suggesterName)); - assertTrue(response.suggest().get(suggesterName).get(0).isTerm()); - assertNotNull(response.suggest().get(suggesterName).get(0).term().options()); - assertEquals(response.suggest().get(suggesterName).get(0).term().options().get(0) - .text(), "foo"); + String index = "test-term-suggester"; + + // term suggester does not require a special mapping + javaClient().indices().create(c -> c.index(index)); + + AppData appData = new AppData(); + appData.setIntValue(1337); + appData.setMsg("foo"); + + javaClient().index(b -> b.index(index).id("1").document(appData).refresh(Refresh.True)); + + appData.setIntValue(1338); + appData.setMsg("foobar"); + + javaClient().index(b -> b.index(index).id("2").document(appData).refresh(Refresh.True)); + + String suggesterName = "msgSuggester"; + + TermSuggester termSuggester = FieldSuggesterBuilders.term().field("msg").size(1).build(); + FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("fool").term(termSuggester).build(); + Suggester suggester = new Suggester.Builder().suggesters(Collections.singletonMap(suggesterName, fieldSuggester)).build(); + SearchRequest searchRequest = new SearchRequest.Builder().index(index).suggest(suggester).build(); + + SearchResponse response = javaClient().search(searchRequest, AppData.class); + assertTrue(response.suggest().size() > 0); + assertTrue(response.suggest().keySet().contains(suggesterName)); + assertNotNull(response.suggest().get(suggesterName)); + assertTrue(response.suggest().get(suggesterName).get(0).isTerm()); + assertNotNull(response.suggest().get(suggesterName).get(0).term().options()); + assertEquals(response.suggest().get(suggesterName).get(0).term().options().get(0).text(), "foo"); + } + + @Test + public void testPhraseSuggester() throws IOException { + + String index = "test-phrase-suggester"; + + ShingleTokenFilter shingleTokenFilter = new ShingleTokenFilter.Builder().minShingleSize("2").maxShingleSize("3").build(); + + Analyzer analyzer = new Analyzer.Builder().custom( + new CustomAnalyzer.Builder().tokenizer("standard").filter(Arrays.asList("lowercase", "shingle")).build() + ).build(); + + TokenFilter tokenFilter = new TokenFilter.Builder().definition( + new TokenFilterDefinition.Builder().shingle(shingleTokenFilter).build() + ).build(); + + IndexSettingsAnalysis indexSettingsAnalysis = new IndexSettingsAnalysis.Builder().analyzer("trigram", analyzer) + .filter("shingle", tokenFilter) + .build(); + + IndexSettings settings = new IndexSettings.Builder().analysis(indexSettingsAnalysis).build(); + + TypeMapping mapping = new TypeMapping.Builder().properties( + "msg", + new Property.Builder().text( + new TextProperty.Builder().fields( + "trigram", + new Property.Builder().text(new TextProperty.Builder().analyzer("trigram").build()).build() + ).build() + ).build() + ).build(); + + javaClient().indices().create(c -> c.index(index).settings(settings).mappings(mapping)); + + AppData appData = new AppData(); + appData.setIntValue(1337); + appData.setMsg("Design Patterns"); + + javaClient().index(b -> b.index(index).id("1").document(appData).refresh(Refresh.True)); + + appData.setIntValue(1338); + appData.setMsg("Software Architecture Patterns Explained"); + + javaClient().index(b -> b.index(index).id("2").document(appData).refresh(Refresh.True)); + + String suggesterName = "msgSuggester"; + + PhraseSuggester phraseSuggester = FieldSuggesterBuilders.phrase().field("msg.trigram").build(); + FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("design paterns").phrase(phraseSuggester).build(); + Suggester suggester = new Suggester.Builder().suggesters(Collections.singletonMap(suggesterName, fieldSuggester)).build(); + SearchRequest searchRequest = new SearchRequest.Builder().index(index).suggest(suggester).build(); + + SearchResponse response = javaClient().search(searchRequest, AppData.class); + assertTrue(response.suggest().size() > 0); + assertTrue(response.suggest().keySet().contains(suggesterName)); + assertNotNull(response.suggest().get(suggesterName)); + assertNotNull(response.suggest().get(suggesterName).get(0)); + assertTrue(response.suggest().get(suggesterName).get(0).isPhrase()); + assertNotNull(response.suggest().get(suggesterName).get(0).phrase().options()); + assertEquals(response.suggest().get(suggesterName).get(0).phrase().options().get(0).text(), "design patterns"); } - @Test - public void testPhraseSuggester() throws IOException { - - String index = "test-phrase-suggester"; - - ShingleTokenFilter shingleTokenFilter = new ShingleTokenFilter.Builder().minShingleSize("2") - .maxShingleSize("3") - .build(); - - Analyzer analyzer = new Analyzer.Builder() - .custom(new CustomAnalyzer.Builder().tokenizer("standard") - .filter(Arrays.asList("lowercase","shingle")).build()) - .build(); - - TokenFilter tokenFilter = new TokenFilter.Builder() - .definition(new TokenFilterDefinition.Builder() - .shingle(shingleTokenFilter).build()) - .build(); - - IndexSettingsAnalysis indexSettingsAnalysis = new IndexSettingsAnalysis.Builder() - .analyzer("trigram", analyzer) - .filter("shingle", tokenFilter) - .build(); - - IndexSettings settings = new IndexSettings.Builder().analysis(indexSettingsAnalysis).build(); - - TypeMapping mapping = new TypeMapping.Builder().properties("msg", new Property.Builder() - .text(new TextProperty.Builder().fields("trigram", new Property.Builder() - .text(new TextProperty.Builder().analyzer("trigram").build()) - .build()).build()) - .build()).build(); - - - javaClient().indices().create(c -> c - .index(index) - .settings(settings) - .mappings(mapping)); - - AppData appData = new AppData(); - appData.setIntValue(1337); - appData.setMsg("Design Patterns"); - - javaClient().index(b -> b - .index(index) - .id("1") - .document(appData) - .refresh(Refresh.True)); - - appData.setIntValue(1338); - appData.setMsg("Software Architecture Patterns Explained"); - - javaClient().index(b -> b - .index(index) - .id("2") - .document(appData) - .refresh(Refresh.True)); - - String suggesterName = "msgSuggester"; - - PhraseSuggester phraseSuggester = FieldSuggesterBuilders.phrase() - .field("msg.trigram") - .build(); - FieldSuggester fieldSuggester = new FieldSuggester.Builder().text("design paterns") - .phrase(phraseSuggester) - .build(); - Suggester suggester = new Suggester.Builder() - .suggesters(Collections.singletonMap(suggesterName, fieldSuggester)) - .build(); - SearchRequest searchRequest = new SearchRequest.Builder() - .index(index) - .suggest(suggester) - .build(); - - SearchResponse response = javaClient().search(searchRequest, AppData.class); - assertTrue(response.suggest().size() > 0); - assertTrue(response.suggest().keySet().contains(suggesterName)); - assertNotNull(response.suggest().get(suggesterName)); - assertNotNull(response.suggest().get(suggesterName).get(0)); - assertTrue(response.suggest().get(suggesterName).get(0).isPhrase()); - assertNotNull(response.suggest().get(suggesterName).get(0).phrase().options()); - assertEquals(response.suggest().get(suggesterName).get(0).phrase().options().get(0) - .text(), "design patterns"); - } - -// @Test -// public void testValueBodyResponse() throws Exception { -// DiskUsageResponse resp = highLevelClient().indices().diskUsage(b -> b -// .index("*") -// .allowNoIndices(true) -// .runExpensiveTasks(true) -// ); -// -// assertNotNull(resp.valueBody().toJson().asJsonObject().get("_shards")); -// } + // @Test + // public void testValueBodyResponse() throws Exception { + // DiskUsageResponse resp = highLevelClient().indices().diskUsage(b -> b + // .index("*") + // .allowNoIndices(true) + // .runExpensiveTasks(true) + // ); + // + // assertNotNull(resp.valueBody().toJson().asJsonObject().get("_shards")); + // } public static class AppData { @@ -918,8 +772,7 @@ public static class Product { public double price; public String color; - public Product() { - } + public Product() {} public Product(double price) { this.price = price; @@ -944,8 +797,7 @@ public static class Join { public String name; public String parent; - Join() { - } + Join() {} Join(String name) { this.name = name; @@ -962,8 +814,7 @@ public static class Question { public String title; public Join join; - Question() { - } + Question() {} Question(String title) { this.title = title; @@ -976,8 +827,7 @@ public static class Answer { public String body; public Join join; - Answer() { - } + Answer() {} Answer(String body) { this.body = body; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java index da8f1007e7..9e029fe95f 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractSearchTemplateRequestIT.java @@ -10,7 +10,6 @@ import java.io.IOException; import java.util.Map; - import org.junit.Test; import org.opensearch.client.json.JsonData; import org.opensearch.client.opensearch._types.Refresh; @@ -18,189 +17,183 @@ import org.opensearch.client.opensearch.core.PutScriptRequest; import org.opensearch.client.opensearch.core.SearchTemplateResponse; -public abstract class AbstractSearchTemplateRequestIT - extends - OpenSearchJavaClientTestCase { - - private static final String TEST_SEARCH_TEMPLATE = "search-template-integration-test"; - - @Test - public void testTemplateSearchResults() throws Exception { - var index = "test-search-template-search"; - createDocuments(index); - - var searchResponse = sendTemplateRequest(index, "NoDocuments", false, - false); - assertEquals(0, searchResponse.hits().hits().size()); - assertEquals(0, searchResponse.suggest().size()); - assertEquals(0, searchResponse.aggregations().size()); - - searchResponse = sendTemplateRequest(index, "Document", false, false); - assertEquals(4, searchResponse.hits().hits().size()); - assertEquals(0, searchResponse.suggest().size()); - assertEquals(0, searchResponse.aggregations().size()); - - searchResponse = sendTemplateRequest(index, "1", false, false); - assertEquals(1, searchResponse.hits().hits().size()); - assertEquals(1, searchResponse.hits().hits().get(0).source().getValue().intValue()); - assertEquals(0, searchResponse.suggest().size()); - assertEquals(0, searchResponse.aggregations().size()); - } - - @Test - public void testTemplateSearchSuggest() throws Exception { - var index = "test-search-template-suggs"; - createDocuments(index); - - var searchResponse = sendTemplateRequest(index, "Document", true, false); - assertEquals(4, searchResponse.hits().hits().size()); - assertEquals(1, searchResponse.suggest().size()); - assertEquals(0, searchResponse.aggregations().size()); - - // intentional typo - searchResponse = sendTemplateRequest(index, "Docuent", true, false); - assertEquals(0, searchResponse.hits().hits().size()); - assertEquals(1, searchResponse.suggest().size()); - var options = searchResponse.suggest().get("test-suggest").get(0).term().options(); - assertEquals(1, options.size()); - assertEquals("document", options.get(0).text()); - assertEquals(0, searchResponse.aggregations().size()); - - } - - @Test - public void testTemplateSearchAggregations() throws Exception { - var index = "test-search-template-aggs"; - createDocuments(index); - - var searchResponse = sendTemplateRequest(index, "NoDocuments", false, true); - assertEquals(0, searchResponse.hits().hits().size()); - assertEquals(0, searchResponse.suggest().size()); - assertEquals(1, searchResponse.aggregations().size()); - - searchResponse = sendTemplateRequest(index, "Document", false, true); - assertEquals(4, searchResponse.hits().hits().size()); - assertEquals(0, searchResponse.suggest().size()); - assertEquals(1, searchResponse.aggregations().size()); - var buckets = searchResponse.aggregations().get("test-aggs").sterms().buckets().array(); - assertEquals(2, buckets.size()); - assertEquals(2, buckets.get(0).docCount()); - assertEquals(2, buckets.get(1).docCount()); - - } - - private SearchTemplateResponse sendTemplateRequest(String index, - String title, boolean suggs, boolean aggs) throws IOException { - return javaClient().searchTemplate( - request -> request - .index(index) - .id(TEST_SEARCH_TEMPLATE) - .params(Map.of( - "title", JsonData.of(title), - "suggs", JsonData.of(suggs), - "aggs", JsonData.of(aggs))), - SimpleDoc.class); - } - - private void createDocuments(String index) throws IOException { - createIndex(index); - javaClient().create(_1 -> _1.index(index).id("1") - .document(createDoc("Document 1", "The text of document 1", 1)) - .refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("2") - .document(createDoc("Document 2", "The text of document 2", 1)) - .refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("3") - .document(createDoc("Document 3", "The text of document 3", 2)) - .refresh(Refresh.True)); - javaClient().create(_1 -> _1.index(index).id("4") - .document(createDoc("Document 4", "The text of document 4", 2)) - .refresh(Refresh.True)); - } - - private void createIndex(String index) throws IOException { - Property titleProp = new Property.Builder().text(v -> v).build(); - Property textProp = new Property.Builder().text(v -> v).build(); - Property valueProp = new Property.Builder().keyword(v -> v).build(); - javaClient().indices().create(c -> c.index(index) - .mappings(m -> m - .properties("title", titleProp) - .properties("text", textProp) - .properties("value", valueProp))); - createSearchTemplate(); - } - - /* - * """{ - "query": { - "match": { - "title": "{{title}}" - } - } - {{#suggs}} - ,"suggest" : { - "test-suggest" : { - "text" : "{{title}}", - "term" : { - "field" : "title" - } - } - } - {{/suggs}} - {{#aggs}} - ,"aggs": { - "test-aggs": { - "terms": { - "field": "value" - } - } - } - {{/aggs}} - }""" - */ - private void createSearchTemplate() throws IOException { - var templateReq = new PutScriptRequest.Builder() - .id(TEST_SEARCH_TEMPLATE) - .script(s -> s.lang("mustache").source( - "{ \"query\": { \"match\": { \"title\": \"{{title}}\" } } " - + "{{#suggs}},\"suggest\" : { \"test-suggest\" : { \"text\" : " - + "\"{{title}}\", \"term\" : { \"field\" : \"title\" } } } {{/suggs}}" - + "{{#aggs}} ,\"aggs\": {\"test-aggs\": { \"terms\": " - + "{ \"field\": \"value\" } } }{{/aggs}} }" - ) - ); - javaClient().putScript(templateReq.build()); - } - - private SimpleDoc createDoc(String title, String text, Integer value) { - return new SimpleDoc(title, text, value); - } - - public static class SimpleDoc { - private String title; - private String text; - private Integer value; - - public SimpleDoc() { - } - - public SimpleDoc(String title, String text, Integer value) { - this.title = title; - this.text = text; - this.value = value; - } - - public String getTitle() { - return title; - } - - public String getText() { - return text; - } - - public Integer getValue() { - return value; - } - } +public abstract class AbstractSearchTemplateRequestIT extends OpenSearchJavaClientTestCase { + + private static final String TEST_SEARCH_TEMPLATE = "search-template-integration-test"; + + @Test + public void testTemplateSearchResults() throws Exception { + var index = "test-search-template-search"; + createDocuments(index); + + var searchResponse = sendTemplateRequest(index, "NoDocuments", false, false); + assertEquals(0, searchResponse.hits().hits().size()); + assertEquals(0, searchResponse.suggest().size()); + assertEquals(0, searchResponse.aggregations().size()); + + searchResponse = sendTemplateRequest(index, "Document", false, false); + assertEquals(4, searchResponse.hits().hits().size()); + assertEquals(0, searchResponse.suggest().size()); + assertEquals(0, searchResponse.aggregations().size()); + + searchResponse = sendTemplateRequest(index, "1", false, false); + assertEquals(1, searchResponse.hits().hits().size()); + assertEquals(1, searchResponse.hits().hits().get(0).source().getValue().intValue()); + assertEquals(0, searchResponse.suggest().size()); + assertEquals(0, searchResponse.aggregations().size()); + } + + @Test + public void testTemplateSearchSuggest() throws Exception { + var index = "test-search-template-suggs"; + createDocuments(index); + + var searchResponse = sendTemplateRequest(index, "Document", true, false); + assertEquals(4, searchResponse.hits().hits().size()); + assertEquals(1, searchResponse.suggest().size()); + assertEquals(0, searchResponse.aggregations().size()); + + // intentional typo + searchResponse = sendTemplateRequest(index, "Docuent", true, false); + assertEquals(0, searchResponse.hits().hits().size()); + assertEquals(1, searchResponse.suggest().size()); + var options = searchResponse.suggest().get("test-suggest").get(0).term().options(); + assertEquals(1, options.size()); + assertEquals("document", options.get(0).text()); + assertEquals(0, searchResponse.aggregations().size()); + + } + + @Test + public void testTemplateSearchAggregations() throws Exception { + var index = "test-search-template-aggs"; + createDocuments(index); + + var searchResponse = sendTemplateRequest(index, "NoDocuments", false, true); + assertEquals(0, searchResponse.hits().hits().size()); + assertEquals(0, searchResponse.suggest().size()); + assertEquals(1, searchResponse.aggregations().size()); + + searchResponse = sendTemplateRequest(index, "Document", false, true); + assertEquals(4, searchResponse.hits().hits().size()); + assertEquals(0, searchResponse.suggest().size()); + assertEquals(1, searchResponse.aggregations().size()); + var buckets = searchResponse.aggregations().get("test-aggs").sterms().buckets().array(); + assertEquals(2, buckets.size()); + assertEquals(2, buckets.get(0).docCount()); + assertEquals(2, buckets.get(1).docCount()); + + } + + private SearchTemplateResponse sendTemplateRequest(String index, String title, boolean suggs, boolean aggs) + throws IOException { + return javaClient().searchTemplate( + request -> request.index(index) + .id(TEST_SEARCH_TEMPLATE) + .params(Map.of("title", JsonData.of(title), "suggs", JsonData.of(suggs), "aggs", JsonData.of(aggs))), + SimpleDoc.class + ); + } + + private void createDocuments(String index) throws IOException { + createIndex(index); + javaClient().create( + _1 -> _1.index(index).id("1").document(createDoc("Document 1", "The text of document 1", 1)).refresh(Refresh.True) + ); + javaClient().create( + _1 -> _1.index(index).id("2").document(createDoc("Document 2", "The text of document 2", 1)).refresh(Refresh.True) + ); + javaClient().create( + _1 -> _1.index(index).id("3").document(createDoc("Document 3", "The text of document 3", 2)).refresh(Refresh.True) + ); + javaClient().create( + _1 -> _1.index(index).id("4").document(createDoc("Document 4", "The text of document 4", 2)).refresh(Refresh.True) + ); + } + + private void createIndex(String index) throws IOException { + Property titleProp = new Property.Builder().text(v -> v).build(); + Property textProp = new Property.Builder().text(v -> v).build(); + Property valueProp = new Property.Builder().keyword(v -> v).build(); + javaClient().indices() + .create( + c -> c.index(index) + .mappings(m -> m.properties("title", titleProp).properties("text", textProp).properties("value", valueProp)) + ); + createSearchTemplate(); + } + + /* + * """{ + "query": { + "match": { + "title": "{{title}}" + } + } + {{#suggs}} + ,"suggest" : { + "test-suggest" : { + "text" : "{{title}}", + "term" : { + "field" : "title" + } + } + } + {{/suggs}} + {{#aggs}} + ,"aggs": { + "test-aggs": { + "terms": { + "field": "value" + } + } + } + {{/aggs}} + }""" + */ + private void createSearchTemplate() throws IOException { + var templateReq = new PutScriptRequest.Builder().id(TEST_SEARCH_TEMPLATE) + .script( + s -> s.lang("mustache") + .source( + "{ \"query\": { \"match\": { \"title\": \"{{title}}\" } } " + + "{{#suggs}},\"suggest\" : { \"test-suggest\" : { \"text\" : " + + "\"{{title}}\", \"term\" : { \"field\" : \"title\" } } } {{/suggs}}" + + "{{#aggs}} ,\"aggs\": {\"test-aggs\": { \"terms\": " + + "{ \"field\": \"value\" } } }{{/aggs}} }" + ) + ); + javaClient().putScript(templateReq.build()); + } + + private SimpleDoc createDoc(String title, String text, Integer value) { + return new SimpleDoc(title, text, value); + } + + public static class SimpleDoc { + private String title; + private String text; + private Integer value; + + public SimpleDoc() {} + + public SimpleDoc(String title, String text, Integer value) { + this.title = title; + this.text = text; + this.value = value; + } + + public String getTitle() { + return title; + } + + public String getText() { + return text; + } + + public Integer getValue() { + return value; + } + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java index e62416c64f..6403569faa 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java @@ -8,6 +8,13 @@ package org.opensearch.client.opensearch.integTest; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.TreeSet; +import javax.net.ssl.SSLEngine; import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; @@ -20,6 +27,9 @@ import org.apache.hc.core5.http.nio.ssl.TlsStrategy; import org.apache.hc.core5.reactor.ssl.TlsDetails; import org.apache.hc.core5.ssl.SSLContextBuilder; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; import org.opensearch.Version; import org.opensearch.client.RestClient; import org.opensearch.client.RestClientBuilder; @@ -31,21 +41,9 @@ import org.opensearch.client.opensearch.indices.DeleteIndexRequest; import org.opensearch.client.opensearch.nodes.NodesInfoResponse; import org.opensearch.client.opensearch.nodes.info.NodeInfo; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; import org.opensearch.common.settings.Settings; import org.opensearch.test.rest.OpenSearchRestTestCase; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.TreeSet; - -import javax.net.ssl.SSLEngine; - public abstract class OpenSearchJavaClientTestCase extends OpenSearchRestTestCase implements OpenSearchTransportSupport { private static final List systemIndices = List.of(".opensearch-observability", ".opendistro_security", ".plugins-ml-config"); private static OpenSearchClient javaClient; @@ -96,16 +94,17 @@ protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOE builder.setHttpClientConfigCallback(httpClientBuilder -> { String userName = Optional.ofNullable(System.getProperty("user")).orElse("admin"); String password = Optional.ofNullable(System.getProperty("password")).orElse("admin"); - + final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - for (final HttpHost host: hosts) { - credentialsProvider.setCredentials(new AuthScope(host), - new UsernamePasswordCredentials(userName, password.toCharArray())); + for (final HttpHost host : hosts) { + credentialsProvider.setCredentials( + new AuthScope(host), + new UsernamePasswordCredentials(userName, password.toCharArray()) + ); } - + try { - final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder - .create() + final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create() .setSslContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()) // disable the certificate since our testing cluster just uses the default security configuration .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) @@ -117,14 +116,12 @@ public TlsDetails create(final SSLEngine sslEngine) { } }) .build(); - + final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create() .setTlsStrategy(tlsStrategy) .build(); - - return httpClientBuilder - .setDefaultCredentialsProvider(credentialsProvider) - .setConnectionManager(connectionManager); + + return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(connectionManager); } catch (Exception e) { throw new RuntimeException(e); } @@ -157,8 +154,7 @@ protected void wipeAllOSIndices() throws IOException { adminJavaClient().indices().deleteDataStream(b -> b.name("*")); // wipe all indices - final IndicesResponse response = adminJavaClient() - .cat() + final IndicesResponse response = adminJavaClient().cat() .indices(r -> r.headers("index,creation.date").expandWildcards(ExpandWildcard.All)); for (IndicesRecord index : response.valueBody()) { @@ -174,7 +170,7 @@ public static void cleanupJavaClient() throws IOException { if (javaClient != null) { IOUtils.closeQueitly(javaClient._transport()); } - + if (adminJavaClient != null) { IOUtils.closeQueitly(adminJavaClient._transport()); } @@ -190,4 +186,4 @@ public static void cleanupJavaClient() throws IOException { protected boolean preserveIndicesUponCompletion() { return true; } -} \ No newline at end of file +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchTransportSupport.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchTransportSupport.java index dd5fc2457a..22b6db5de1 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchTransportSupport.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/OpenSearchTransportSupport.java @@ -10,7 +10,6 @@ import java.io.IOException; import java.util.Optional; - import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.transport.OpenSearchTransport; @@ -18,9 +17,7 @@ public interface OpenSearchTransportSupport { default boolean isHttps() { - return Optional.ofNullable(System.getProperty("https")) - .map("true"::equalsIgnoreCase) - .orElse(false); + return Optional.ofNullable(System.getProperty("https")).map("true"::equalsIgnoreCase).orElse(false); } default OpenSearchClient buildJavaClient(Settings settings, HttpHost[] hosts) throws IOException { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2BulkRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2BulkRequestIT.java index 36672582ac..759909cddb 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2BulkRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2BulkRequestIT.java @@ -8,8 +8,10 @@ package org.opensearch.client.opensearch.integTest.aws; -import org.junit.Test; +import java.util.ArrayList; +import java.util.List; import org.junit.Assert; +import org.junit.Test; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch._types.Refresh; import org.opensearch.client.opensearch._types.query_dsl.Query; @@ -20,9 +22,6 @@ import org.opensearch.client.opensearch.core.bulk.BulkOperation; import org.opensearch.client.opensearch.core.bulk.IndexOperation; -import java.util.ArrayList; -import java.util.List; - public class AwsSdk2BulkRequestIT extends AwsSdk2TransportTestCase { @Test @@ -32,33 +31,23 @@ public void testBulkRequest() throws Exception { ArrayList ops = new ArrayList<>(); SimplePojo doc1 = new SimplePojo("Document 1", "The text of document 1"); - ops.add(new BulkOperation.Builder().index( - IndexOperation.of(io -> io.index(TEST_INDEX).id("id1").document(doc1)) - ).build()); + ops.add(new BulkOperation.Builder().index(IndexOperation.of(io -> io.index(TEST_INDEX).id("id1").document(doc1))).build()); SimplePojo doc2 = new SimplePojo("Document 2", "The text of document 2"); - ops.add(new BulkOperation.Builder().index( - IndexOperation.of(io -> io.index(TEST_INDEX).id("id2").document(doc2)) - ).build()); + ops.add(new BulkOperation.Builder().index(IndexOperation.of(io -> io.index(TEST_INDEX).id("id2").document(doc2))).build()); SimplePojo doc3 = getLongDoc("Long Document 3", 100000); - ops.add(new BulkOperation.Builder().index( - IndexOperation.of(io -> io.index(TEST_INDEX).id("id3").document(doc3)) - ).build()); + ops.add(new BulkOperation.Builder().index(IndexOperation.of(io -> io.index(TEST_INDEX).id("id3").document(doc3))).build()); - BulkRequest.Builder bulkReq = new BulkRequest.Builder() - .index(TEST_INDEX) - .operations(ops) - .refresh(Refresh.WaitFor); + BulkRequest.Builder bulkReq = new BulkRequest.Builder().index(TEST_INDEX).operations(ops).refresh(Refresh.WaitFor); BulkResponse bulkResponse = client.bulk(bulkReq.build()); Assert.assertEquals(3, bulkResponse.items().size()); Query query = Query.of(qb -> qb.match(mb -> mb.field("title").query(fv -> fv.stringValue("Document")))); - final SearchRequest.Builder searchReq = new SearchRequest.Builder() - .allowPartialSearchResults(false) - .index(List.of(TEST_INDEX)) - .size(10) - .source(sc -> sc.fetch(false)) - .ignoreThrottled(false) - .query(query); + final SearchRequest.Builder searchReq = new SearchRequest.Builder().allowPartialSearchResults(false) + .index(List.of(TEST_INDEX)) + .size(10) + .source(sc -> sc.fetch(false)) + .ignoreThrottled(false) + .query(query); SearchResponse searchResponse = client.search(searchReq.build(), SimplePojo.class); Assert.assertEquals(3, searchResponse.hits().hits().size()); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SearchIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SearchIT.java index 1e7f4ce427..343c1f18d1 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SearchIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SearchIT.java @@ -8,8 +8,10 @@ package org.opensearch.client.opensearch.integTest.aws; -import org.junit.Test; +import java.util.List; +import java.util.concurrent.CompletableFuture; import org.junit.Assert; +import org.junit.Test; import org.opensearch.client.opensearch.OpenSearchAsyncClient; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch._types.OpenSearchException; @@ -19,9 +21,6 @@ import org.opensearch.client.opensearch.indices.CreateIndexRequest; import org.opensearch.client.opensearch.indices.OpenSearchIndicesClient; -import java.util.List; -import java.util.concurrent.CompletableFuture; - public class AwsSdk2SearchIT extends AwsSdk2TransportTestCase { @Test @@ -54,7 +53,7 @@ void testClient(boolean async) throws Exception { addDoc(client, "id2", doc2); SimplePojo doc3 = getLongDoc("Long Document 3", 1000000); addDoc(client, "id3", doc3); - + // wait for the document to index Thread.sleep(1000); @@ -82,8 +81,7 @@ void testClientAsync(boolean async) throws Exception { SimplePojo doc2 = new SimplePojo("Document 2", "The text of document 2"); CompletableFuture add2 = addDoc(client, "id2", doc2); SimplePojo doc3 = getLongDoc("Long Document 3", 1000000); - CompletableFuture add3 = CompletableFuture.allOf(add1, add2).thenCompose( - unused -> addDoc(client, "id3", doc3)); + CompletableFuture add3 = CompletableFuture.allOf(add1, add2).thenCompose(unused -> addDoc(client, "id3", doc3)); // wait for the document to index Thread.sleep(1000); @@ -92,11 +90,7 @@ void testClientAsync(boolean async) throws Exception { CompletableFuture> r1 = query(client, "NotPresent", null); CompletableFuture> r2 = query(client, "Document", null); CompletableFuture> r3 = query(client, "1", null); - return CompletableFuture.allOf(r1, r2, r3).thenApply(u2 -> List.of( - r1.getNow(null), - r2.getNow(null), - r3.getNow(null)) - ); + return CompletableFuture.allOf(r1, r2, r3).thenApply(u2 -> List.of(r1.getNow(null), r2.getNow(null), r3.getNow(null))); }).get(); SearchResponse response = results.get(0); @@ -110,22 +104,13 @@ void testClientAsync(boolean async) throws Exception { Assert.assertEquals(doc1, response.hits().hits().get(0).source()); } - private void addDoc(OpenSearchClient client, String id, SimplePojo doc) throws Exception { - IndexRequest.Builder req = new IndexRequest.Builder() - .index(TEST_INDEX) - .document(doc) - .id(id); + IndexRequest.Builder req = new IndexRequest.Builder().index(TEST_INDEX).document(doc).id(id); client.index(req.build()); } - private CompletableFuture addDoc( - OpenSearchAsyncClient client, String id, SimplePojo doc - ) { - IndexRequest.Builder req = new IndexRequest.Builder() - .index(TEST_INDEX) - .document(doc) - .id(id); + private CompletableFuture addDoc(OpenSearchAsyncClient client, String id, SimplePojo doc) { + IndexRequest.Builder req = new IndexRequest.Builder().index(TEST_INDEX).document(doc).id(id); try { return client.index(req.build()); } catch (Exception e) { @@ -140,9 +125,7 @@ public void testDoubleWrappedException() throws Exception { // attempt to create the same index a second time OpenSearchIndicesClient client = getIndexesClient(false, null, null); var req = new CreateIndexRequest.Builder().index(TEST_INDEX); - Exception exception = Assert.assertThrows(OpenSearchException.class, () -> { - client.create(req.build()); - }); + Exception exception = Assert.assertThrows(OpenSearchException.class, () -> { client.create(req.build()); }); // error message contains the actual error, not a generic [http_exception] Assert.assertTrue(exception.getMessage().contains("[resource_already_exists_exception]")); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SecurityIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SecurityIT.java index aef59a3abf..6ec60590db 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SecurityIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SecurityIT.java @@ -8,24 +8,22 @@ package org.opensearch.client.opensearch.integTest.aws; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; + import org.junit.Test; import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch._types.OpenSearchException; import org.opensearch.client.opensearch.cluster.GetClusterSettingsRequest; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThrows; - public class AwsSdk2SecurityIT extends AwsSdk2TransportTestCase { private static final String DEFAULT_MESSAGE = "authentication/authorization failure"; + @Test public void testUnAuthorizedException() { final OpenSearchClient client = getClient(false, null, null); - final GetClusterSettingsRequest request = new GetClusterSettingsRequest.Builder() - .includeDefaults(true) - .build(); - final OpenSearchException ex = assertThrows( - OpenSearchException.class, () -> client.cluster().getSettings(request)); + final GetClusterSettingsRequest request = new GetClusterSettingsRequest.Builder().includeDefaults(true).build(); + final OpenSearchException ex = assertThrows(OpenSearchException.class, () -> client.cluster().getSettings(request)); assertFalse(ex.getMessage().contains(DEFAULT_MESSAGE)); } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2TransportTestCase.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2TransportTestCase.java index 8adcd4300b..a1ec172c41 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2TransportTestCase.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2TransportTestCase.java @@ -11,6 +11,11 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import java.io.IOException; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import javax.annotation.CheckForNull; import org.junit.AfterClass; import org.junit.Assume; import org.junit.Before; @@ -35,12 +40,6 @@ import software.amazon.awssdk.http.crt.AwsCrtAsyncHttpClient; import software.amazon.awssdk.regions.Region; -import javax.annotation.CheckForNull; -import java.io.IOException; -import java.util.List; -import java.util.Objects; -import java.util.concurrent.CompletableFuture; - public abstract class AwsSdk2TransportTestCase { public static final String TEST_INDEX = "opensearch-java-integtest"; @@ -71,74 +70,79 @@ protected AwsSdk2TransportOptions.Builder getTransportOptions() { return AwsSdk2TransportOptions.builder(); } - protected OpenSearchClient getClient( - boolean async, - @CheckForNull JsonpMapper mapper, - @CheckForNull TransportOptions options) { + protected OpenSearchClient getClient(boolean async, @CheckForNull JsonpMapper mapper, @CheckForNull TransportOptions options) { AwsSdk2Transport transport; if (async) { transport = new AwsSdk2Transport( - getAsyncHttpClient(), - getTestClusterHost(), - getTestClusterServiceName(), - getTestClusterRegion(), - getTransportOptions().build()); + getAsyncHttpClient(), + getTestClusterHost(), + getTestClusterServiceName(), + getTestClusterRegion(), + getTransportOptions().build() + ); } else { transport = new AwsSdk2Transport( - getHttpClient(), - getTestClusterHost(), - getTestClusterServiceName(), - getTestClusterRegion(), - getTransportOptions().build()); + getHttpClient(), + getTestClusterHost(), + getTestClusterServiceName(), + getTestClusterRegion(), + getTransportOptions().build() + ); } return new OpenSearchClient(transport); } protected OpenSearchAsyncClient getAsyncClient( - boolean async, - @CheckForNull JsonpMapper mapper, - @CheckForNull TransportOptions options) { + boolean async, + @CheckForNull JsonpMapper mapper, + @CheckForNull TransportOptions options + ) { AwsSdk2Transport transport; if (async) { transport = new AwsSdk2Transport( - getAsyncHttpClient(), - getTestClusterHost(), - getTestClusterServiceName(), - getTestClusterRegion(), - getTransportOptions().build()); + getAsyncHttpClient(), + getTestClusterHost(), + getTestClusterServiceName(), + getTestClusterRegion(), + getTransportOptions().build() + ); } else { transport = new AwsSdk2Transport( - getHttpClient(), - getTestClusterHost(), - getTestClusterServiceName(), - getTestClusterRegion(), - getTransportOptions().build()); + getHttpClient(), + getTestClusterHost(), + getTestClusterServiceName(), + getTestClusterRegion(), + getTransportOptions().build() + ); } return new OpenSearchAsyncClient(transport); } protected OpenSearchIndicesClient getIndexesClient( - boolean async, - @CheckForNull JsonpMapper mapper, - @CheckForNull TransportOptions options) { + boolean async, + @CheckForNull JsonpMapper mapper, + @CheckForNull TransportOptions options + ) { AwsSdk2Transport transport; if (async) { transport = new AwsSdk2Transport( - getAsyncHttpClient(), - getTestClusterHost(), - getTestClusterServiceName(), - getTestClusterRegion(), - getTransportOptions().build()); + getAsyncHttpClient(), + getTestClusterHost(), + getTestClusterServiceName(), + getTestClusterRegion(), + getTransportOptions().build() + ); } else { transport = new AwsSdk2Transport( - getHttpClient(), - getTestClusterHost(), - getTestClusterServiceName(), - getTestClusterRegion(), - getTransportOptions().build()); + getHttpClient(), + getTestClusterHost(), + getTestClusterServiceName(), + getTestClusterRegion(), + getTransportOptions().build() + ); } return new OpenSearchIndicesClient(transport); } @@ -207,8 +211,7 @@ public void resetTestIndex(boolean async) throws Exception { if (indexExists) { client.delete(b -> b.index(List.of(TEST_INDEX))); } - var req = new CreateIndexRequest.Builder() - .index(TEST_INDEX); + var req = new CreateIndexRequest.Builder().index(TEST_INDEX); client.create(req.build()); } @@ -222,21 +225,20 @@ protected SearchResponse query(OpenSearchClient client, String title } return qb; }); - final SearchRequest.Builder req = new SearchRequest.Builder() - .allowPartialSearchResults(false) - .index(List.of(TEST_INDEX)) - .size(10) - .ignoreThrottled(false) - .sort( - new SortOptions.Builder().score(o -> o.order(SortOrder.Desc)).build(), - new SortOptions.Builder().doc(o -> o.order(SortOrder.Desc)).build()) - .query(query); + final SearchRequest.Builder req = new SearchRequest.Builder().allowPartialSearchResults(false) + .index(List.of(TEST_INDEX)) + .size(10) + .ignoreThrottled(false) + .sort( + new SortOptions.Builder().score(o -> o.order(SortOrder.Desc)).build(), + new SortOptions.Builder().doc(o -> o.order(SortOrder.Desc)).build() + ) + .query(query); return client.search(req.build(), SimplePojo.class); } - protected CompletableFuture> query( - OpenSearchAsyncClient client, String title, String text) { + protected CompletableFuture> query(OpenSearchAsyncClient client, String title, String text) { var query = Query.of(qb -> { if (title != null) { qb.match(mb -> mb.field("title").query(vb -> vb.stringValue(title))); @@ -246,15 +248,15 @@ protected CompletableFuture> query( } return qb; }); - final SearchRequest.Builder req = new SearchRequest.Builder() - .allowPartialSearchResults(false) - .index(List.of(TEST_INDEX)) - .size(10) - .ignoreThrottled(false) - .sort( - new SortOptions.Builder().score(o -> o.order(SortOrder.Desc)).build(), - new SortOptions.Builder().doc(o -> o.order(SortOrder.Desc)).build()) - .query(query); + final SearchRequest.Builder req = new SearchRequest.Builder().allowPartialSearchResults(false) + .index(List.of(TEST_INDEX)) + .size(10) + .ignoreThrottled(false) + .sort( + new SortOptions.Builder().score(o -> o.order(SortOrder.Desc)).build(), + new SortOptions.Builder().doc(o -> o.order(SortOrder.Desc)).build() + ) + .query(query); try { return client.search(req.build(), SimplePojo.class); @@ -269,9 +271,7 @@ public static class SimplePojo { final String title; final String text; - public SimplePojo( - @JsonProperty("title") String title, - @JsonProperty("text") String text) { + public SimplePojo(@JsonProperty("title") String title, @JsonProperty("text") String text) { this.title = title; this.text = text; } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/AggregationRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/AggregationRequestIT.java index b91aec9819..195f0b7cd8 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/AggregationRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/AggregationRequestIT.java @@ -10,5 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractAggregationRequestIT; -public class AggregationRequestIT extends AbstractAggregationRequestIT implements HttpClient5TransportSupport{ -} +public class AggregationRequestIT extends AbstractAggregationRequestIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/CatIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/CatIT.java index f152e23268..5fa4110b3d 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/CatIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/CatIT.java @@ -10,7 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractCatClientIT; -public class CatIT extends AbstractCatClientIT implements HttpClient5TransportSupport { -} - - +public class CatIT extends AbstractCatClientIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/ClusterClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/ClusterClientIT.java index 5e13da89a2..fe0932fc84 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/ClusterClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/ClusterClientIT.java @@ -9,7 +9,6 @@ package org.opensearch.client.opensearch.integTest.httpclient5; import java.io.IOException; - import org.opensearch.client.opensearch.integTest.AbstractClusterClientIT; import org.opensearch.client.transport.httpclient5.ResponseException; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/CrudIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/CrudIT.java index 1e4c995944..4445d068ab 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/CrudIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/CrudIT.java @@ -10,5 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractCrudIT; -public class CrudIT extends AbstractCrudIT implements HttpClient5TransportSupport { -} +public class CrudIT extends AbstractCrudIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/HighlightIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/HighlightIT.java index 1f097e2497..2b0e71a77c 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/HighlightIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/HighlightIT.java @@ -10,5 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractHighlightIT; -public class HighlightIT extends AbstractHighlightIT implements HttpClient5TransportSupport { -} +public class HighlightIT extends AbstractHighlightIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/HttpClient5TransportSupport.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/HttpClient5TransportSupport.java index 2b0c4088f8..617304f83b 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/HttpClient5TransportSupport.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/HttpClient5TransportSupport.java @@ -17,10 +17,8 @@ import java.security.NoSuchAlgorithmException; import java.util.Map; import java.util.Optional; - import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; - import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; @@ -54,19 +52,18 @@ default OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) private void configure(ApacheHttpClient5TransportBuilder builder, Settings settings, HttpHost[] hosts) throws IOException { if (isHttps()) { try { - final SSLContext sslcontext = SSLContextBuilder - .create() - .loadTrustMaterial(null, (chains, authType) -> true) - .build(); + final SSLContext sslcontext = SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build(); builder.setHttpClientConfigCallback(httpClientBuilder -> { String userName = Optional.ofNullable(System.getProperty("user")).orElse("admin"); String password = Optional.ofNullable(System.getProperty("password")).orElse("admin"); - + final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - for (final HttpHost host: hosts) { - credentialsProvider.setCredentials(new AuthScope(host), - new UsernamePasswordCredentials(userName, password.toCharArray())); + for (final HttpHost host : hosts) { + credentialsProvider.setCredentials( + new AuthScope(host), + new UsernamePasswordCredentials(userName, password.toCharArray()) + ); } final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create() @@ -86,9 +83,7 @@ public TlsDetails create(final SSLEngine sslEngine) { .setTlsStrategy(tlsStrategy) .build(); - return httpClientBuilder - .setDefaultCredentialsProvider(credentialsProvider) - .setConnectionManager(connectionManager); + return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(connectionManager); }); } catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) { throw new RuntimeException("Error setting up ssl", e); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/IndicesClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/IndicesClientIT.java index ecd1469d3b..682022dbc9 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/IndicesClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/IndicesClientIT.java @@ -10,5 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractIndicesClientIT; -public class IndicesClientIT extends AbstractIndicesClientIT implements HttpClient5TransportSupport { -} +public class IndicesClientIT extends AbstractIndicesClientIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/KnnIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/KnnIT.java index a709f7c2a4..2fd41b4487 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/KnnIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/KnnIT.java @@ -10,5 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractKnnIT; -public class KnnIT extends AbstractKnnIT implements HttpClient5TransportSupport { -} +public class KnnIT extends AbstractKnnIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/MultiSearchRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/MultiSearchRequestIT.java index e4b85a7891..0737562229 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/MultiSearchRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/MultiSearchRequestIT.java @@ -10,5 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractMultiSearchRequestIT; -public class MultiSearchRequestIT extends AbstractMultiSearchRequestIT implements HttpClient5TransportSupport { -} +public class MultiSearchRequestIT extends AbstractMultiSearchRequestIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/NodesIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/NodesIT.java index 286ef7d15d..0efce55170 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/NodesIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/NodesIT.java @@ -10,5 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractNodesIT; -public class NodesIT extends AbstractNodesIT implements HttpClient5TransportSupport { -} +public class NodesIT extends AbstractNodesIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/PingAndInfoIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/PingAndInfoIT.java index 9bc6f883b2..d9397b9f85 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/PingAndInfoIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/PingAndInfoIT.java @@ -10,6 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractPingAndInfoIT; -public class PingAndInfoIT extends AbstractPingAndInfoIT implements HttpClient5TransportSupport { -} - +public class PingAndInfoIT extends AbstractPingAndInfoIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/RequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/RequestIT.java index bfa6d90798..fab7c06e07 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/RequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/RequestIT.java @@ -10,5 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractRequestIT; -public class RequestIT extends AbstractRequestIT implements HttpClient5TransportSupport { -} +public class RequestIT extends AbstractRequestIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/SearchTemplateRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/SearchTemplateRequestIT.java index d0a1ebe940..42777826a5 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/SearchTemplateRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/httpclient5/SearchTemplateRequestIT.java @@ -10,5 +10,4 @@ import org.opensearch.client.opensearch.integTest.AbstractSearchTemplateRequestIT; -public class SearchTemplateRequestIT extends AbstractSearchTemplateRequestIT implements HttpClient5TransportSupport{ -} +public class SearchTemplateRequestIT extends AbstractSearchTemplateRequestIT implements HttpClient5TransportSupport {} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/AggregationRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/AggregationRequestIT.java index 30afb678bd..0f1bc019ef 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/AggregationRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/AggregationRequestIT.java @@ -8,6 +8,7 @@ package org.opensearch.client.opensearch.integTest.restclient; +import java.io.IOException; import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractAggregationRequestIT; @@ -15,8 +16,6 @@ import org.opensearch.client.transport.rest_client.RestClientTransport; import org.opensearch.common.settings.Settings; -import java.io.IOException; - public class AggregationRequestIT extends AbstractAggregationRequestIT { @Override public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/CatIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/CatIT.java index 897734d97d..5dbb4fd540 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/CatIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/CatIT.java @@ -8,6 +8,7 @@ package org.opensearch.client.opensearch.integTest.restclient; +import java.io.IOException; import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractCatClientIT; @@ -15,8 +16,6 @@ import org.opensearch.client.transport.rest_client.RestClientTransport; import org.opensearch.common.settings.Settings; -import java.io.IOException; - public class CatIT extends AbstractCatClientIT { @Override public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/ClusterClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/ClusterClientIT.java index 718f846930..b5c1ef722f 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/ClusterClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/ClusterClientIT.java @@ -9,7 +9,6 @@ package org.opensearch.client.opensearch.integTest.restclient; import java.io.IOException; - import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractClusterClientIT; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/CrudIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/CrudIT.java index 27e7785bf2..ad38bc4d73 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/CrudIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/CrudIT.java @@ -9,7 +9,6 @@ package org.opensearch.client.opensearch.integTest.restclient; import java.io.IOException; - import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractCrudIT; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/HighlightIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/HighlightIT.java index e16c26712c..2e874fa4de 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/HighlightIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/HighlightIT.java @@ -8,6 +8,7 @@ package org.opensearch.client.opensearch.integTest.restclient; +import java.io.IOException; import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractHighlightIT; @@ -15,8 +16,6 @@ import org.opensearch.client.transport.rest_client.RestClientTransport; import org.opensearch.common.settings.Settings; -import java.io.IOException; - public class HighlightIT extends AbstractHighlightIT { @Override public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/IndicesClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/IndicesClientIT.java index a9b549bc72..66c7587a67 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/IndicesClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/IndicesClientIT.java @@ -9,7 +9,6 @@ package org.opensearch.client.opensearch.integTest.restclient; import java.io.IOException; - import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractIndicesClientIT; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/MultiSearchRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/MultiSearchRequestIT.java index a4f10c46dd..aab4b33f2e 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/MultiSearchRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/MultiSearchRequestIT.java @@ -8,6 +8,7 @@ package org.opensearch.client.opensearch.integTest.restclient; +import java.io.IOException; import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractMultiSearchRequestIT; @@ -15,11 +16,9 @@ import org.opensearch.client.transport.rest_client.RestClientTransport; import org.opensearch.common.settings.Settings; -import java.io.IOException; - public class MultiSearchRequestIT extends AbstractMultiSearchRequestIT { - @Override - public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException { - return new RestClientTransport(buildClient(settings, hosts), new JacksonJsonpMapper()); - } + @Override + public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException { + return new RestClientTransport(buildClient(settings, hosts), new JacksonJsonpMapper()); + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/NodesIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/NodesIT.java index bc3c5ab507..e49355e167 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/NodesIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/NodesIT.java @@ -9,7 +9,6 @@ package org.opensearch.client.opensearch.integTest.restclient; import java.io.IOException; - import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractNodesIT; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/PingAndInfoIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/PingAndInfoIT.java index 541835265c..7460b0fc55 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/PingAndInfoIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/PingAndInfoIT.java @@ -9,7 +9,6 @@ package org.opensearch.client.opensearch.integTest.restclient; import java.io.IOException; - import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractPingAndInfoIT; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/RequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/RequestIT.java index 8fa2675aab..51969e4bf8 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/RequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/RequestIT.java @@ -8,9 +8,10 @@ package org.opensearch.client.opensearch.integTest.restclient; +import static org.hamcrest.CoreMatchers.equalTo; + import java.io.IOException; import java.util.Optional; - import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; @@ -37,8 +38,6 @@ import org.opensearch.client.transport.rest_client.RestClientTransport; import org.opensearch.common.settings.Settings; -import static org.hamcrest.CoreMatchers.equalTo; - public class RequestIT extends AbstractRequestIT { @Override public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException { @@ -51,45 +50,37 @@ public void testUnauthorized() throws Exception { Assume.assumeThat(isHttps(), equalTo(true)); final String userName = Optional.ofNullable(System.getProperty("user")).orElse("admin"); - final String wrongPassword = Optional.ofNullable(System.getProperty("password")).orElse("admin") - + "wrong"; + final String wrongPassword = Optional.ofNullable(System.getProperty("password")).orElse("admin") + "wrong"; final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider.setCredentials(new AuthScope(getClusterHosts().get(0)), - new UsernamePasswordCredentials(userName, wrongPassword.toCharArray())); + credentialsProvider.setCredentials( + new AuthScope(getClusterHosts().get(0)), + new UsernamePasswordCredentials(userName, wrongPassword.toCharArray()) + ); - final RestClient restClient = RestClient - .builder(getClusterHosts().toArray(new HttpHost[0])) - .setHttpClientConfigCallback(httpClientBuilder -> { - try { - final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder - .create() - .setSslContext( - SSLContextBuilder.create().loadTrustMaterial( - null, (chains, authType) -> true).build()) - .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .setTlsDetailsFactory( - sslEngine -> new TlsDetails( - sslEngine.getSession(), sslEngine.getApplicationProtocol())) - .build(); + final RestClient restClient = RestClient.builder(getClusterHosts().toArray(new HttpHost[0])) + .setHttpClientConfigCallback(httpClientBuilder -> { + try { + final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create() + .setSslContext(SSLContextBuilder.create().loadTrustMaterial(null, (chains, authType) -> true).build()) + .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .setTlsDetailsFactory(sslEngine -> new TlsDetails(sslEngine.getSession(), sslEngine.getApplicationProtocol())) + .build(); - final PoolingAsyncClientConnectionManager connectionManager = - PoolingAsyncClientConnectionManagerBuilder.create() - .setTlsStrategy(tlsStrategy) - .build(); + final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create() + .setTlsStrategy(tlsStrategy) + .build(); - return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) - .setConnectionManager(connectionManager); - } catch (Exception e) { - throw new RuntimeException(e); - } - }) - .build(); + return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(connectionManager); + } catch (Exception e) { + throw new RuntimeException(e); + } + }) + .build(); final RestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); final OpenSearchClient client = new OpenSearchClient(transport); - final TransportException transportException = assertThrows(TransportException.class, - () -> client.cluster().getSettings()); + final TransportException transportException = assertThrows(TransportException.class, () -> client.cluster().getSettings()); assertEquals("Unauthorized access", transportException.getMessage()); restClient.close(); } @@ -98,15 +89,15 @@ public void testUnauthorized() throws Exception { public void testForbidden() throws Exception { final OpenSearchClient openSearchClient = javaClient(); final String testIndex = "test-index"; - final CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder() - .index(testIndex) - .settings(new IndexSettings.Builder() - .blocksRead(true).build()) - .build(); + final CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(testIndex) + .settings(new IndexSettings.Builder().blocksRead(true).build()) + .build(); openSearchClient.indices().create(createIndexRequest); final SearchRequest searchRequest = new SearchRequest.Builder().index(testIndex).build(); - final TransportException transportException = assertThrows(TransportException.class, - () -> openSearchClient.search(searchRequest, JsonData.class)); + final TransportException transportException = assertThrows( + TransportException.class, + () -> openSearchClient.search(searchRequest, JsonData.class) + ); assertEquals("Forbidden access", transportException.getMessage()); } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/SearchTemplateRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/SearchTemplateRequestIT.java index 680b936ac8..2450075cc1 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/SearchTemplateRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/restclient/SearchTemplateRequestIT.java @@ -8,6 +8,7 @@ package org.opensearch.client.opensearch.integTest.restclient; +import java.io.IOException; import org.apache.hc.core5.http.HttpHost; import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.opensearch.integTest.AbstractSearchTemplateRequestIT; @@ -15,8 +16,6 @@ import org.opensearch.client.transport.rest_client.RestClientTransport; import org.opensearch.common.settings.Settings; -import java.io.IOException; - public class SearchTemplateRequestIT extends AbstractSearchTemplateRequestIT { @Override public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/BehaviorsTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/BehaviorsTest.java index 300eeef743..02f6058af3 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/BehaviorsTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/BehaviorsTest.java @@ -32,6 +32,8 @@ package org.opensearch.client.opensearch.model; +import org.junit.Test; +import org.opensearch.client.json.JsonData; import org.opensearch.client.opensearch._types.ErrorCause; import org.opensearch.client.opensearch._types.FieldValue; import org.opensearch.client.opensearch._types.GeoLocation; @@ -42,20 +44,14 @@ import org.opensearch.client.opensearch._types.query_dsl.Query; import org.opensearch.client.opensearch._types.query_dsl.ShapeQuery; import org.opensearch.client.opensearch._types.query_dsl.TermQuery; -import org.opensearch.client.json.JsonData; import org.opensearch.client.opensearch._types.query_dsl.WrapperQuery; import org.opensearch.client.util.MapBuilder; -import org.junit.Test; public class BehaviorsTest extends ModelTestCase { @Test public void testSingleKeyDictionary() { - TermQuery q = new TermQuery.Builder() - .queryName("query-name") - .field("field-name") - .value(FieldValue.of("some-value")) - .build(); + TermQuery q = new TermQuery.Builder().queryName("query-name").field("field-name").value(FieldValue.of("some-value")).build(); q = checkJsonRoundtrip(q, "{\"field-name\":{\"_name\":\"query-name\",\"value\":\"some-value\"}}"); @@ -66,18 +62,13 @@ public void testSingleKeyDictionary() { @Test public void testAdditionalPropertyOnClass() { - ShapeQuery q = new ShapeQuery.Builder() - .queryName("query-name") + ShapeQuery q = new ShapeQuery.Builder().queryName("query-name") .field("field-name") - .shape(_0 -> _0 - .relation(GeoShapeRelation.Disjoint) - ) + .shape(_0 -> _0.relation(GeoShapeRelation.Disjoint)) .ignoreUnmapped(true) .build(); - q = checkJsonRoundtrip(q, - "{\"field-name\":{\"relation\":\"disjoint\"},\"_name\":\"query-name\",\"ignore_unmapped\":true}" - ); + q = checkJsonRoundtrip(q, "{\"field-name\":{\"relation\":\"disjoint\"},\"_name\":\"query-name\",\"ignore_unmapped\":true}"); assertEquals("query-name", q.queryName()); assertTrue(q.ignoreUnmapped()); @@ -87,13 +78,9 @@ public void testAdditionalPropertyOnClass() { @Test public void testWrapperQuery() { - WrapperQuery q = new WrapperQuery.Builder() - .query("encoded_query") - .build(); + WrapperQuery q = new WrapperQuery.Builder().query("encoded_query").build(); - q = checkJsonRoundtrip(q, - "{\"query\":\"encoded_query\"}" - ); + q = checkJsonRoundtrip(q, "{\"query\":\"encoded_query\"}"); assertEquals("encoded_query", q.query()); } @@ -102,9 +89,7 @@ public void testWrapperQuery() { public void testAdditionalPropertyOnContainer() { // Regular variant { - SortOptions so = SortOptions.of(_0 -> _0 - .doc(_1 -> _1.order(SortOrder.Asc)) - ); + SortOptions so = SortOptions.of(_0 -> _0.doc(_1 -> _1.order(SortOrder.Asc))); so = checkJsonRoundtrip(so, "{\"_doc\":{\"order\":\"asc\"}}"); assertEquals(SortOptions.Kind.Doc, so._kind()); @@ -126,8 +111,7 @@ public void testAdditionalPropertyOnContainer() { } { - SortOptions so = SortOptions.of(_0 -> _0 - .score(_1 -> _1.order(SortOrder.Asc))); + SortOptions so = SortOptions.of(_0 -> _0.score(_1 -> _1.order(SortOrder.Asc))); so = checkJsonRoundtrip(so, "{\"_score\":{\"order\":\"asc\"}}"); assertEquals(SortOptions.Kind.Score, so._kind()); @@ -135,9 +119,7 @@ public void testAdditionalPropertyOnContainer() { } { - SortOptions so = SortOptions.of(_0 -> _0 - .script(_1 -> _1.script(_2 -> _2.inline(_3 -> _3.source("blah")))) - ); + SortOptions so = SortOptions.of(_0 -> _0.script(_1 -> _1.script(_2 -> _2.inline(_3 -> _3.source("blah"))))); so = checkJsonRoundtrip(so, "{\"_script\":{\"script\":{\"source\":\"blah\"}}}"); assertEquals("blah", so.script().script().inline().source()); @@ -145,9 +127,7 @@ public void testAdditionalPropertyOnContainer() { // Additional property variant { - SortOptions so = SortOptions.of(_0 -> _0 - .field(_1 -> _1.field("foo").order(SortOrder.Desc)) - ); + SortOptions so = SortOptions.of(_0 -> _0.field(_1 -> _1.field("foo").order(SortOrder.Desc))); so = checkJsonRoundtrip(so, "{\"foo\":{\"order\":\"desc\"}}"); assertEquals(SortOptions.Kind.Field, so._kind()); @@ -156,23 +136,15 @@ public void testAdditionalPropertyOnContainer() { } } - @Test public void testAdditionalProperties() { // Check that additional property map is initialized even if not set explicitly - ErrorCause err = new ErrorCause.Builder() - .reason("Foo") - .type("Bar") - .build(); + ErrorCause err = new ErrorCause.Builder().reason("Foo").type("Bar").build(); assertEquals(0, err.metadata().size()); - err = new ErrorCause.Builder() - .reason("Some failure") + err = new ErrorCause.Builder().reason("Some failure") .type("Some type") - .metadata(MapBuilder.of( - "index", JsonData.of("test"), - "retries", JsonData.of(1) - )) + .metadata(MapBuilder.of("index", JsonData.of("test"), "retries", JsonData.of(1))) .build(); err = checkJsonRoundtrip(err, "{\"index\":\"test\",\"retries\":1,\"type\":\"Some type\",\"reason\":\"Some failure\"}"); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/BuiltinTypesTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/BuiltinTypesTest.java index c501ff12d6..49a412beff 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/BuiltinTypesTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/BuiltinTypesTest.java @@ -32,6 +32,8 @@ package org.opensearch.client.opensearch.model; +import java.util.ArrayList; +import java.util.List; import org.junit.Test; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.opensearch._types.FieldValue; @@ -43,9 +45,6 @@ import org.opensearch.client.opensearch.core.SearchRequest; import org.opensearch.client.opensearch.indices.IndexSettings; -import java.util.ArrayList; -import java.util.List; - public class BuiltinTypesTest extends ModelTestCase { @Test @@ -68,10 +67,7 @@ public void testLenientArray() { @Test public void testSpanGapQuery() { // Hand-written class - SpanGapQuery q = SpanGapQuery.of(b -> b - .field("a-field") - .spanWidth(12) - ); + SpanGapQuery q = SpanGapQuery.of(b -> b.field("a-field").spanWidth(12)); q = checkJsonRoundtrip(q, "{\"a-field\":12}"); @@ -87,39 +83,43 @@ public void testSortOptions() { SortOptions sort; // Arbitrary field - sort = fromJson( - "{ \"post_date\" : {\"order\" : \"asc\", \"format\": \"strict_date_optional_time_nanos\"}}", - SortOptions.class); + sort = fromJson("{ \"post_date\" : {\"order\" : \"asc\", \"format\": \"strict_date_optional_time_nanos\"}}", SortOptions.class); assertEquals("post_date", sort.field().field()); assertEquals(SortOrder.Asc, sort.field().order()); - sort = fromJson("{\n" + - " \"offer.price\" : {\n" + - " \"mode\" : \"avg\",\n" + - " \"order\" : \"asc\",\n" + - " \"nested\": {\n" + - " \"path\": \"offer\",\n" + - " \"filter\": {\n" + - " \"term\" : { \"offer.color\" : \"blue\" }\n" + - " }\n" + - " }\n" + - " }\n" + - " }", SortOptions.class); + sort = fromJson( + "{\n" + + " \"offer.price\" : {\n" + + " \"mode\" : \"avg\",\n" + + " \"order\" : \"asc\",\n" + + " \"nested\": {\n" + + " \"path\": \"offer\",\n" + + " \"filter\": {\n" + + " \"term\" : { \"offer.color\" : \"blue\" }\n" + + " }\n" + + " }\n" + + " }\n" + + " }", + SortOptions.class + ); assertEquals("blue", sort.field().nested().filter().term().value().stringValue()); // Geo distance - sort = fromJson("{\n" + - " \"_geo_distance\" : {\n" + - " \"pin.location\" : {\n" + - " \"lat\" : 40,\n" + - " \"lon\" : -70\n" + - " },\n" + - " \"order\" : \"asc\",\n" + - " \"unit\" : \"km\"\n" + - " }\n" + - " }", SortOptions.class); + sort = fromJson( + "{\n" + + " \"_geo_distance\" : {\n" + + " \"pin.location\" : {\n" + + " \"lat\" : 40,\n" + + " \"lon\" : -70\n" + + " },\n" + + " \"order\" : \"asc\",\n" + + " \"unit\" : \"km\"\n" + + " }\n" + + " }", + SortOptions.class + ); assertEquals(40, sort.geoDistance().location().get(0).latlon().lat(), 0.1); @@ -181,12 +181,13 @@ public void testNullableDouble() { StatsAggregate stats; // Regular values - stats = StatsAggregate.statsAggregateOf(b -> b // Parent classes can't have an overloaded "of" method - .count(10) - .min(1.0) - .avg(1.5) - .max(2.0) - .sum(5.0) + stats = StatsAggregate.statsAggregateOf( + b -> b // Parent classes can't have an overloaded "of" method + .count(10) + .min(1.0) + .avg(1.5) + .max(2.0) + .sum(5.0) ); stats = checkJsonRoundtrip(stats, "{\"count\":10,\"min\":1.0,\"max\":2.0,\"avg\":1.5,\"sum\":5.0}"); @@ -212,13 +213,7 @@ public void testNullableDouble() { @Test public void testNullableInt() { - StringStatsAggregate stats = StringStatsAggregate.of(b -> b - .count(1) - .minLength(2) - .avgLength(3) - .maxLength(4) - .entropy(0) - ); + StringStatsAggregate stats = StringStatsAggregate.of(b -> b.count(1).minLength(2).avgLength(3).maxLength(4).entropy(0)); stats = checkJsonRoundtrip(stats, "{\"count\":1,\"min_length\":2,\"max_length\":4,\"avg_length\":3.0,\"entropy\":0.0}"); assertEquals(2, stats.minLength()); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/ClassStructureTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/ClassStructureTest.java index 2d456ae0b6..db0842ce40 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/ClassStructureTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/ClassStructureTest.java @@ -32,6 +32,16 @@ package org.opensearch.client.opensearch.model; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import org.junit.Test; +import org.opensearch.client.json.JsonData; import org.opensearch.client.opensearch._types.ErrorCause; import org.opensearch.client.opensearch._types.aggregations.Aggregate; import org.opensearch.client.opensearch._types.aggregations.Aggregation; @@ -47,20 +57,9 @@ import org.opensearch.client.opensearch.core.search.HitsMetadata; import org.opensearch.client.opensearch.core.search.TotalHits; import org.opensearch.client.opensearch.core.search.TotalHitsRelation; -import org.opensearch.client.json.JsonData; -import org.opensearch.client.util.MissingRequiredPropertyException; import org.opensearch.client.util.ApiTypeHelper; +import org.opensearch.client.util.MissingRequiredPropertyException; import org.opensearch.client.util.ObjectBuilder; -import org.junit.Test; - -import javax.annotation.Nullable; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; /** * Tests that verify common features of generated classes. @@ -82,8 +81,7 @@ public void testPrimitiveTypes() throws Exception { // Builder setter for optional primitive should use the boxed type and be @Nullable { Method searchBuilderSize = SearchRequest.Builder.class.getMethod("size", Integer.class); - assertTrue(Arrays.stream(searchBuilderSize.getParameterAnnotations()[0]) - .anyMatch(a -> a.annotationType() == Nullable.class)); + assertTrue(Arrays.stream(searchBuilderSize.getParameterAnnotations()[0]).anyMatch(a -> a.annotationType() == Nullable.class)); } // Required primitive should use the primitive type and not be @Nullable @@ -96,8 +94,9 @@ public void testPrimitiveTypes() throws Exception { // Builder setter for required primitive should use the primitive type and not be @Nullable { Method totalHitsBuilderValue = TotalHits.Builder.class.getMethod("value", long.class); - assertFalse(Arrays.stream(totalHitsBuilderValue.getParameterAnnotations()[0]) - .anyMatch(a -> a.annotationType() == Nullable.class)); + assertFalse( + Arrays.stream(totalHitsBuilderValue.getParameterAnnotations()[0]).anyMatch(a -> a.annotationType() == Nullable.class) + ); } // Not setting a required primitive should throw an exception @@ -113,10 +112,7 @@ public void testPrimitiveTypes() throws Exception { public void testDataClassInheritedFieldAssignment() { // 1 ancestor { - CardinalityAggregate card = CardinalityAggregate.of(b -> b - .meta(Collections.singletonMap("foo", JsonData.of("bar"))) - .value(1) - ); + CardinalityAggregate card = CardinalityAggregate.of(b -> b.meta(Collections.singletonMap("foo", JsonData.of("bar"))).value(1)); assertAncestorCount(1, card); assertEquals("bar", card.meta().get("foo").to(String.class)); @@ -125,9 +121,9 @@ public void testDataClassInheritedFieldAssignment() { // 3 ancestors { - DateRangeAggregate date = DateRangeAggregate.of(_1 -> _1 - .meta(Collections.singletonMap("foo", JsonData.of("bar"))) - .buckets(Buckets.of(b -> b.array(Collections.singletonList(RangeBucket.of(_2 -> _2.docCount(1)))))) + DateRangeAggregate date = DateRangeAggregate.of( + _1 -> _1.meta(Collections.singletonMap("foo", JsonData.of("bar"))) + .buckets(Buckets.of(b -> b.array(Collections.singletonList(RangeBucket.of(_2 -> _2.docCount(1)))))) ); assertAncestorCount(3, date); @@ -141,11 +137,7 @@ public void testDataClassInheritedFieldAssignment() { */ @Test public void testUnionInheritedFieldAssignment() { - IntervalsQuery iq = IntervalsQuery.of(_1 -> _1 - .boost(2.0f) - .field("foo") - .allOf(b -> b.intervals(Collections.emptyList())) - ); + IntervalsQuery iq = IntervalsQuery.of(_1 -> _1.boost(2.0f).field("foo").allOf(b -> b.intervals(Collections.emptyList()))); assertAncestorCount(1, iq); assertEquals(2.0f, iq.boost(), 0.01); assertEquals("{\"foo\":{\"boost\":2.0,\"all_of\":{\"intervals\":[]}}}", toJson(iq)); @@ -155,8 +147,10 @@ public void testUnionInheritedFieldAssignment() { public void testUndefinedCollections() { // Not setting a required list should throw an exception { - MissingRequiredPropertyException ex = assertThrows(MissingRequiredPropertyException.class, - () -> HitsMetadata.of(_1 -> _1.total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq)))); + MissingRequiredPropertyException ex = assertThrows( + MissingRequiredPropertyException.class, + () -> HitsMetadata.of(_1 -> _1.total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq))) + ); assertTrue(ex.getMessage().contains(".hits")); } @@ -189,10 +183,7 @@ public void testUndefinedCollections() { // Setting an empty map defines it { - CardinalityAggregate card = CardinalityAggregate.of(b -> b - .value(1) - .meta(Collections.emptyMap()) - ); + CardinalityAggregate card = CardinalityAggregate.of(b -> b.value(1).meta(Collections.emptyMap())); assertNotNull(card.meta()); assertEquals(0, card.meta().size()); assertTrue(ApiTypeHelper.isDefined(card.meta())); @@ -206,47 +197,35 @@ public void testListSetters() { { // Appending doesn't modify the original collection - SearchRequest search = SearchRequest.of(b -> b - .storedFields(fields) - .storedFields("c") - .storedFields("d", "e", "f") - ); + SearchRequest search = SearchRequest.of(b -> b.storedFields(fields).storedFields("c").storedFields("d", "e", "f")); assertEquals(Arrays.asList("a", "b"), fields); assertEquals(Arrays.asList("a", "b", "c", "d", "e", "f"), search.storedFields()); } { // Appending doesn't modify the original collection (appending the same list twice) - SearchRequest search = SearchRequest.of(b -> b - .storedFields(fields) - .storedFields(fields) - ); + SearchRequest search = SearchRequest.of(b -> b.storedFields(fields).storedFields(fields)); assertEquals(Arrays.asList("a", "b"), fields); assertEquals(Arrays.asList("a", "b", "a", "b"), search.storedFields()); } - { // List cannot be null List nullFields = null; - assertThrows(NullPointerException.class, () -> { - SearchRequest.of(b -> b - .storedFields(nullFields) - ); - }); + assertThrows(NullPointerException.class, () -> { SearchRequest.of(b -> b.storedFields(nullFields)); }); } { // Combine value and builder FieldAndFormat fieldA = FieldAndFormat.of(f -> f.field("a")); - SearchRequest search = SearchRequest.of(b -> b - .docvalueFields(fieldA) - .docvalueFields(f -> f.field("b")) - .docvalueFields(f -> f.field("c")) + SearchRequest search = SearchRequest.of( + b -> b.docvalueFields(fieldA).docvalueFields(f -> f.field("b")).docvalueFields(f -> f.field("c")) ); - assertEquals(Arrays.asList("a", "b", "c"), search.docvalueFields().stream() - .map(FieldAndFormat::field).collect(Collectors.toList())); + assertEquals( + Arrays.asList("a", "b", "c"), + search.docvalueFields().stream().map(FieldAndFormat::field).collect(Collectors.toList()) + ); } } @@ -262,10 +241,10 @@ public void testMapSetters() { { // Appending doesn't modify the original collection - SearchRequest search = SearchRequest.of(b -> b - .aggregations(aggs) - .aggregations("aggC", countC._toAggregation()) - .aggregations("aggD", a -> a.valueCount(c -> c.field("d"))) + SearchRequest search = SearchRequest.of( + b -> b.aggregations(aggs) + .aggregations("aggC", countC._toAggregation()) + .aggregations("aggD", a -> a.valueCount(c -> c.field("d"))) ); // Original map wasn't modified @@ -315,7 +294,7 @@ public void testRequiredProperty() { // Missing id property throws an exception MissingRequiredPropertyException ex = assertThrows(MissingRequiredPropertyException.class, () -> { - GetRequest r1 = GetRequest.of(b -> b.index("foo")); + GetRequest r1 = GetRequest.of(b -> b.index("foo")); }); assertEquals("id", ex.getPropertyName()); @@ -326,15 +305,13 @@ public void testRequiredProperty() { } // Checks are enabled again after the try block - ex = assertThrows(MissingRequiredPropertyException.class, () -> { - GetRequest r1 = GetRequest.of(b -> b.index("foo")); - }); + ex = assertThrows(MissingRequiredPropertyException.class, () -> { GetRequest r1 = GetRequest.of(b -> b.index("foo")); }); assertEquals("id", ex.getPropertyName()); } private void assertAncestorCount(int count, Object obj) { Class clazz = obj.getClass(); - while(count-- >= 0) { + while (count-- >= 0) { clazz = clazz.getSuperclass(); } assertEquals(Object.class, clazz); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/EndpointTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/EndpointTest.java index 14c95d3778..e434e12b85 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/EndpointTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/EndpointTest.java @@ -32,10 +32,10 @@ package org.opensearch.client.opensearch.model; -import org.opensearch.client.opensearch._types.ExpandWildcard; -import org.opensearch.client.opensearch.indices.RefreshRequest; import org.junit.Assert; import org.junit.Test; +import org.opensearch.client.opensearch._types.ExpandWildcard; +import org.opensearch.client.opensearch.indices.RefreshRequest; public class EndpointTest extends Assert { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/EnumTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/EnumTest.java index 7faaee7105..f16f0795a8 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/EnumTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/EnumTest.java @@ -32,13 +32,12 @@ package org.opensearch.client.opensearch.model; +import java.util.Arrays; import org.junit.Test; import org.opensearch.client.opensearch._types.Bytes; import org.opensearch.client.opensearch._types.mapping.DynamicMapping; import org.opensearch.client.opensearch._types.mapping.GeoOrientation; -import java.util.Arrays; - public class EnumTest extends ModelTestCase { @Test diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/ModelTestCase.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/ModelTestCase.java index 8408d68f6f..51babb7001 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/ModelTestCase.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/ModelTestCase.java @@ -32,19 +32,18 @@ package org.opensearch.client.opensearch.model; -import org.opensearch.client.json.JsonpDeserializer; -import org.opensearch.client.json.JsonpMapper; -import org.opensearch.client.json.jackson.JacksonJsonpMapper; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; import jakarta.json.spi.JsonProvider; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; -import org.junit.Assert; - import java.io.StringReader; import java.io.StringWriter; import java.lang.reflect.Method; import java.util.Random; +import org.junit.Assert; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.jackson.JacksonJsonpMapper; +import org.opensearch.client.json.jsonb.JsonbJsonpMapper; /** * Base class for tests that encode/decode json @@ -110,7 +109,7 @@ protected T fromJson(String json, Class clazz) { @SuppressWarnings("unchecked") protected T checkJsonRoundtrip(T value, String expectedJson) { assertEquals(expectedJson, toJson(value)); - return fromJson(expectedJson, (Class)value.getClass()); + return fromJson(expectedJson, (Class) value.getClass()); } protected T fromJson(String json, JsonpDeserializer deserializer) { diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java index 05219e16c5..61a57e1091 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java @@ -32,9 +32,9 @@ package org.opensearch.client.opensearch.model; -import org.opensearch.client.opensearch.core.SearchRequest; -import org.opensearch.client.json.jsonb.JsonbJsonpMapper; import org.junit.Test; +import org.opensearch.client.json.jsonb.JsonbJsonpMapper; +import org.opensearch.client.opensearch.core.SearchRequest; public class RequestEncodingTest extends ModelTestCase { @@ -44,16 +44,9 @@ public void testParametersNotInJson() { // This checks that path parameters ("q") are not serialized as json // and variant containers ser/deser - SearchRequest request = new SearchRequest.Builder() - .q("blah") - .query(b1 -> b1 - .type(b2 -> b2 - .value("foo")) - ) - .aggregations( - "myagg", b2 -> b2 - .avg(b3 -> b3.field("foo")) - ) + SearchRequest request = new SearchRequest.Builder().q("blah") + .query(b1 -> b1.type(b2 -> b2.value("foo"))) + .aggregations("myagg", b2 -> b2.avg(b3 -> b3.field("foo"))) .build(); JsonbJsonpMapper mapper = new JsonbJsonpMapper(); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/SerializationTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/SerializationTest.java index b61b15f723..ceb8641e0d 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/SerializationTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/SerializationTest.java @@ -52,17 +52,13 @@ public class SerializationTest extends ModelTestCase { @Test public void loadAllDeserializers() throws Exception { - ScanResult scan = new ClassGraph() - .acceptPackages("org.opensearch.client") - .enableAnnotationInfo() - .enableFieldInfo() - .scan(); + ScanResult scan = new ClassGraph().acceptPackages("org.opensearch.client").enableAnnotationInfo().enableFieldInfo().scan(); ClassInfoList withAnnotation = scan.getClassesWithAnnotation(JsonpDeserializable.class.getName()); assertFalse("No JsonpDeserializable classes", withAnnotation.isEmpty()); - for (ClassInfo info: withAnnotation) { + for (ClassInfo info : withAnnotation) { Class clazz = Class.forName(info.getName()); JsonpDeserializer deserializer = JsonpMapperBase.findDeserializer(clazz); assertNotNull(deserializer); @@ -75,24 +71,23 @@ public void loadAllDeserializers() throws Exception { ClassInfoList withDeserializer = scan.getAllClasses().filter((c) -> c.hasDeclaredField("_DESERIALIZER")); assertFalse("No classes with a _DESERIALIZER field", withDeserializer.isEmpty()); -// Disabled for now, empty response classes still need a deserializer object -// e.g. ExistsIndexTemplateResponse, PingResponse, ExistsResponse, ExistsAliasResponse -// -// Set annotationNames = withAnnotation.stream().map(c -> c.getName()).collect(Collectors.toSet()); -// Set withFieldNames = withDeserializer.stream().map(c -> c.getName()).collect(Collectors.toSet()); -// -// withFieldNames.removeAll(annotationNames); -// -// assertFalse("Some classes with the field but not the annotation: " + withFieldNames, !withFieldNames.isEmpty()); + // Disabled for now, empty response classes still need a deserializer object + // e.g. ExistsIndexTemplateResponse, PingResponse, ExistsResponse, ExistsAliasResponse + // + // Set annotationNames = withAnnotation.stream().map(c -> c.getName()).collect(Collectors.toSet()); + // Set withFieldNames = withDeserializer.stream().map(c -> c.getName()).collect(Collectors.toSet()); + // + // withFieldNames.removeAll(annotationNames); + // + // assertFalse("Some classes with the field but not the annotation: " + withFieldNames, !withFieldNames.isEmpty()); } @Test public void testArrayValueBody() { - NodesResponse nr = NodesResponse.of(_0 -> _0 - .valueBody(_1 -> _1.bulkTotalOperations("1")) - .valueBody(_1 -> _1.bulkTotalOperations("2")) + NodesResponse nr = NodesResponse.of( + _0 -> _0.valueBody(_1 -> _1.bulkTotalOperations("1")).valueBody(_1 -> _1.bulkTotalOperations("2")) ); checkJsonRoundtrip(nr, "[{\"bulk.total_operations\":\"1\"},{\"bulk.total_operations\":\"2\"}]"); @@ -105,15 +100,14 @@ public void testArrayValueBody() { @Test public void testGenericValueBody() { - GetSourceResponse r = GetSourceResponse.of(_0 -> _0 - .valueBody("The value") - ); + GetSourceResponse r = GetSourceResponse.of(_0 -> _0.valueBody("The value")); String json = toJson(r); assertEquals("\"The value\"", json); - JsonpDeserializer> deserializer = - GetSourceResponse.createGetSourceResponseDeserializer(JsonpDeserializer.stringDeserializer()); + JsonpDeserializer> deserializer = GetSourceResponse.createGetSourceResponseDeserializer( + JsonpDeserializer.stringDeserializer() + ); r = fromJson(json, deserializer); @@ -132,8 +126,6 @@ public void testJsonpValuesToString() { assertEquals("null", JsonpUtils.toString(JsonValue.NULL)); assertEquals("a,b,c", JsonpUtils.toString(Json.createArrayBuilder().add("a").add("b").add("c").build())); - assertThrows(IllegalArgumentException.class, () -> { - JsonpUtils.toString(Json.createObjectBuilder().build()); - }); + assertThrows(IllegalArgumentException.class, () -> { JsonpUtils.toString(Json.createObjectBuilder().build()); }); } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/TypedKeysTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/TypedKeysTest.java index 968a8c9401..df69bc98a7 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/TypedKeysTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/TypedKeysTest.java @@ -32,41 +32,33 @@ package org.opensearch.client.opensearch.model; +import java.util.Collections; +import org.junit.Test; +import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.opensearch._types.aggregations.Aggregate; import org.opensearch.client.opensearch._types.aggregations.AvgAggregate; import org.opensearch.client.opensearch._types.aggregations.StringTermsAggregate; import org.opensearch.client.opensearch._types.aggregations.StringTermsBucket; import org.opensearch.client.opensearch.core.SearchResponse; import org.opensearch.client.opensearch.core.search.TotalHitsRelation; -import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.util.ListBuilder; import org.opensearch.client.util.MapBuilder; -import org.junit.Test; - -import java.util.Collections; public class TypedKeysTest extends ModelTestCase { @Test public void testMapProperty() { - SearchResponse resp = new SearchResponse.Builder() - .aggregations( - "foo", _2 -> _2 - .avg(_3 -> _3.value(3.14)) - ) + SearchResponse resp = new SearchResponse.Builder().aggregations("foo", _2 -> _2.avg(_3 -> _3.value(3.14))) // Required properties on a SearchResponse .took(1) .shards(_1 -> _1.successful(1).failed(0).total(1)) - .hits(_1 -> _1 - .total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq)) - .hits(Collections.emptyList()) - ) + .hits(_1 -> _1.total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq)).hits(Collections.emptyList())) .timedOut(false) .build(); - String json = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0}," + - "\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]},\"aggregations\":{\"avg#foo\":{\"value\":3.14}}}"; + String json = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0}," + + "\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]},\"aggregations\":{\"avg#foo\":{\"value\":3.14}}}"; assertEquals(json, toJson(resp)); @@ -83,44 +75,32 @@ public void testAdditionalProperties() { Aggregate avg1 = AvgAggregate.of(_1 -> _1.value(1.0))._toAggregate(); Aggregate avg2 = AvgAggregate.of(_1 -> _1.value(2.0))._toAggregate(); - Aggregate aggregate = StringTermsAggregate.of(_0 -> _0 - .sumOtherDocCount(1) - .buckets(b -> b.array( - ListBuilder.of(StringTermsBucket.Builder::new) - .add(_1 -> _1 - .key("key_1") - .docCount(1) - .aggregations(MapBuilder.of("bar", avg1)) + Aggregate aggregate = StringTermsAggregate.of( + _0 -> _0.sumOtherDocCount(1) + .buckets( + b -> b.array( + ListBuilder.of(StringTermsBucket.Builder::new) + .add(_1 -> _1.key("key_1").docCount(1).aggregations(MapBuilder.of("bar", avg1))) + .add(_1 -> _1.key("key_2").docCount(2).aggregations(MapBuilder.of("bar", avg2))) + .build() + ) ) - .add(_1 -> _1 - .key("key_2") - .docCount(2) - .aggregations(MapBuilder.of("bar", avg2)) - ) - .build() - )) - ) - ._toAggregate(); + )._toAggregate(); - SearchResponse resp = new SearchResponse.Builder() - .aggregations("foo", aggregate) + SearchResponse resp = new SearchResponse.Builder().aggregations("foo", aggregate) // Required properties on a SearchResponse .took(1) .shards(_1 -> _1.successful(1).failed(0).total(1)) - .hits(_1 -> _1 - .total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq)) - .hits(Collections.emptyList()) - ) + .hits(_1 -> _1.total(_2 -> _2.value(0).relation(TotalHitsRelation.Eq)).hits(Collections.emptyList())) .timedOut(false) .build(); - - String json = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0}," + - "\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]}," + - "\"aggregations\":{\"sterms#foo\":{\"buckets\":[" + - "{\"avg#bar\":{\"value\":1.0},\"doc_count\":1,\"key\":\"key_1\"}," + - "{\"avg#bar\":{\"value\":2.0},\"doc_count\":2,\"key\":\"key_2\"}" + - "],\"sum_other_doc_count\":1}}}"; + String json = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"failed\":0.0,\"successful\":1.0,\"total\":1.0}," + + "\"hits\":{\"total\":{\"relation\":\"eq\",\"value\":0},\"hits\":[]}," + + "\"aggregations\":{\"sterms#foo\":{\"buckets\":[" + + "{\"avg#bar\":{\"value\":1.0},\"doc_count\":1,\"key\":\"key_1\"}," + + "{\"avg#bar\":{\"value\":2.0},\"doc_count\":2,\"key\":\"key_2\"}" + + "],\"sum_other_doc_count\":1}}}"; assertEquals(json, toJson(resp)); resp = fromJson(json, SearchResponse.createSearchResponseDeserializer(JsonpDeserializer.voidDeserializer())); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/UnionTests.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/UnionTests.java index ee884eb6dc..b05df6a544 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/UnionTests.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/UnionTests.java @@ -32,10 +32,10 @@ package org.opensearch.client.opensearch.model; +import org.junit.Test; import org.opensearch.client.opensearch._types.Script; import org.opensearch.client.opensearch._types.query_dsl.SimpleQueryStringFlag; import org.opensearch.client.opensearch._types.query_dsl.SimpleQueryStringFlags; -import org.junit.Test; public class UnionTests extends ModelTestCase { @@ -43,21 +43,13 @@ public class UnionTests extends ModelTestCase { public void testScriptDeserializer() { // A union discriminated by its field names (source -> inline, id -> stored) { - Script s = Script.of(_1 -> _1 - .inline(_2 -> _2 - .source("a script") - ) - ); + Script s = Script.of(_1 -> _1.inline(_2 -> _2.source("a script"))); s = checkJsonRoundtrip(s, "{\"source\":\"a script\"}"); assertEquals("a script", s.inline().source()); } { - Script s = Script.of(_1 -> _1 - .stored(_2 -> _2 - .id("script_id") - ) - ); + Script s = Script.of(_1 -> _1.stored(_2 -> _2.id("script_id"))); s = checkJsonRoundtrip(s, "{\"id\":\"script_id\"}"); assertEquals("script_id", s.stored().id()); } @@ -75,12 +67,11 @@ public void testSimpleQueryStringFlag() { SimpleQueryStringFlags f; - f= fromJson("\"OR\"", SimpleQueryStringFlags.class); + f = fromJson("\"OR\"", SimpleQueryStringFlags.class); assertEquals(SimpleQueryStringFlag.Or, f.single()); f = fromJson("\"OR|AND\"", SimpleQueryStringFlags.class); assertEquals("OR|AND", f.multiple()); - } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/VariantsTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/VariantsTest.java index 2124f3e729..b511f4ec1d 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/VariantsTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/VariantsTest.java @@ -32,14 +32,14 @@ package org.opensearch.client.opensearch.model; +import org.junit.Test; +import org.opensearch.client.json.JsonData; import org.opensearch.client.opensearch._types.mapping.Property; import org.opensearch.client.opensearch._types.mapping.TypeMapping; import org.opensearch.client.opensearch._types.query_dsl.Query; import org.opensearch.client.opensearch._types.query_dsl.QueryBuilders; import org.opensearch.client.opensearch.core.SearchRequest; import org.opensearch.client.opensearch.indices.GetMappingResponse; -import org.opensearch.client.json.JsonData; -import org.junit.Test; public class VariantsTest extends ModelTestCase { @@ -49,19 +49,11 @@ public void testNested() { // intervals is a single key dictionary // query has container properties - Query q = Query.of(_0 -> _0 - .intervals(_1 -> _1 - .queryName("my-query") - .field("a_field") - .anyOf(_2 -> _2 - .intervals(_3 -> _3 - .match( - _5 -> _5 - .query("match-query") - .analyzer("lowercase") - ) - ) - ) + Query q = Query.of( + _0 -> _0.intervals( + _1 -> _1.queryName("my-query") + .field("a_field") + .anyOf(_2 -> _2.intervals(_3 -> _3.match(_5 -> _5.query("match-query").analyzer("lowercase")))) ) ); @@ -73,8 +65,11 @@ public void testNested() { String json = toJson(q); - assertEquals("{\"intervals\":{\"a_field\":{\"_name\":\"my-query\"," + - "\"any_of\":{\"intervals\":[{\"match\":{\"analyzer\":\"lowercase\",\"query\":\"match-query\"}}]}}}}", json); + assertEquals( + "{\"intervals\":{\"a_field\":{\"_name\":\"my-query\"," + + "\"any_of\":{\"intervals\":[{\"match\":{\"analyzer\":\"lowercase\",\"query\":\"match-query\"}}]}}}}", + json + ); Query q2 = fromJson(json, Query.class); assertEquals(json, toJson(q2)); @@ -89,21 +84,10 @@ public void testNested() { @Test public void testInternalTag() { - String expected = "{\"type\":\"ip\",\"fields\":{\"a-field\":{\"type\":\"float\",\"coerce\":true}},\"boost\":1" + - ".0,\"index\":true}"; - - Property p = Property.of(_0 -> _0 - .ip(_1 -> _1 - .index(true) - .boost(1.0) - .fields( - "a-field", _3 -> _3 - .float_(_4 -> _4 - .coerce(true) - ) - ) - ) - ); + String expected = "{\"type\":\"ip\",\"fields\":{\"a-field\":{\"type\":\"float\",\"coerce\":true}},\"boost\":1" + + ".0,\"index\":true}"; + + Property p = Property.of(_0 -> _0.ip(_1 -> _1.index(true).boost(1.0).fields("a-field", _3 -> _3.float_(_4 -> _4.coerce(true))))); assertEquals(expected, toJson(p)); @@ -121,7 +105,6 @@ public void testBuilders() { assertEquals("{\"exists\":{\"field\":\"foo\"}}", toJson(q)); } - @Test public void testNestedTaggedUnionWithDefaultTag() { @@ -132,57 +115,64 @@ public void testNestedTaggedUnionWithDefaultTag() { // Mappings are therefore a hierarchy of internally-tagged unions based on the "type" property // with a default "object" tag value if the "type" property is missing. - String json = - "{\n" + - " \"testindex\" : {\n" + - " \"mappings\" : {\n" + - " \"properties\" : {\n" + - " \"id\" : {\n" + - " \"type\" : \"text\",\n" + - " \"fields\" : {\n" + - " \"keyword\" : {\n" + - " \"type\" : \"keyword\",\n" + - " \"ignore_above\" : 256\n" + - " }\n" + - " }\n" + - " },\n" + - " \"name\" : {\n" + - " \"properties\" : {\n" + - " \"first\" : {\n" + - " \"type\" : \"text\",\n" + - " \"fields\" : {\n" + - " \"keyword\" : {\n" + - " \"type\" : \"keyword\",\n" + - " \"ignore_above\" : 256\n" + - " }\n" + - " }\n" + - " },\n" + - " \"last\" : {\n" + - " \"type\" : \"text\",\n" + - " \"fields\" : {\n" + - " \"keyword\" : {\n" + - " \"type\" : \"keyword\",\n" + - " \"ignore_above\" : 256\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"; + String json = "{\n" + + " \"testindex\" : {\n" + + " \"mappings\" : {\n" + + " \"properties\" : {\n" + + " \"id\" : {\n" + + " \"type\" : \"text\",\n" + + " \"fields\" : {\n" + + " \"keyword\" : {\n" + + " \"type\" : \"keyword\",\n" + + " \"ignore_above\" : 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"name\" : {\n" + + " \"properties\" : {\n" + + " \"first\" : {\n" + + " \"type\" : \"text\",\n" + + " \"fields\" : {\n" + + " \"keyword\" : {\n" + + " \"type\" : \"keyword\",\n" + + " \"ignore_above\" : 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"last\" : {\n" + + " \"type\" : \"text\",\n" + + " \"fields\" : {\n" + + " \"keyword\" : {\n" + + " \"type\" : \"keyword\",\n" + + " \"ignore_above\" : 256\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; GetMappingResponse response = fromJson(json, GetMappingResponse.class); TypeMapping mappings = response.get("testindex").mappings(); assertTrue(mappings.properties().get("name").isObject()); - assertEquals(256, mappings - .properties().get("name").object() - .properties().get("first").text() - .fields().get("keyword").keyword(). - ignoreAbove().longValue() + assertEquals( + 256, + mappings.properties() + .get("name") + .object() + .properties() + .get("first") + .text() + .fields() + .get("keyword") + .keyword() + .ignoreAbove() + .longValue() ); assertTrue(mappings.properties().get("id").isText()); @@ -201,10 +191,10 @@ public void testEmptyProperty() { @Test public void testNestedVariantsWithContainerProperties() { - SearchRequest search = SearchRequest.of(s -> s - .aggregations( - "agg1", a -> a - .meta("m1", JsonData.of("m1 value")) + SearchRequest search = SearchRequest.of( + s -> s.aggregations( + "agg1", + a -> a.meta("m1", JsonData.of("m1 value")) // Here we can choose any aggregation type, but build() isn't accessible .valueCount(v -> v.field("f")) // Here we can only set container properties (meta and (sub)aggregations) or build()

- * See Enumerations in Serde.rs that clearly explains the - * various strategies to represent polymorphic values in JSON. - */ - public static class ExternallyTagged { + // ------------------------------------------------------------------------------------------------------------------- /** - * Creates a parser for externally tagged variants + * Variant serializer/deserializer using externally tagged JSON. + *