From a1bf41548abd36052df58f9e0c3c38b589b54f9c Mon Sep 17 00:00:00 2001 From: Andriy Redko Date: Wed, 21 Feb 2024 14:53:52 -0500 Subject: [PATCH] Consolidate and cleanup the server version checks for tests (#865) Signed-off-by: Andriy Redko --- .../integTest/AbstractAggregationRequestIT.java | 11 +---------- .../opensearch/integTest/AbstractCatClientIT.java | 14 +++----------- .../opensearch/integTest/AbstractRequestIT.java | 10 +--------- .../integTest/AbstractSearchRequestIT.java | 8 +------- .../integTest/OpenSearchJavaClientTestCase.java | 12 ++++++++++++ 5 files changed, 18 insertions(+), 37 deletions(-) diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractAggregationRequestIT.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractAggregationRequestIT.java index 18c5589f19..48f72f1023 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractAggregationRequestIT.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractAggregationRequestIT.java @@ -25,7 +25,6 @@ import org.opensearch.client.opensearch._types.aggregations.MultiTermsAggregation; import org.opensearch.client.opensearch._types.aggregations.RangeAggregation; import org.opensearch.client.opensearch._types.mapping.Property; -import org.opensearch.client.opensearch.core.InfoResponse; import org.opensearch.client.opensearch.core.SearchResponse; public abstract class AbstractAggregationRequestIT extends OpenSearchJavaClientTestCase { @@ -101,15 +100,7 @@ public void testMultiTermsAggregationWithSizeBiggerThenBucketsSize() throws Exce } 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")) - ); + assumeTrue("multi_terms is supported in OpenSearch 2.1.0 and later", getServerVersion().onOrAfter(Version.V_2_1_0)); } private Aggregation getExpiryDateRangeAggregation() { diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCatClientIT.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCatClientIT.java index 230f067ce8..dcc626fe44 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCatClientIT.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractCatClientIT.java @@ -34,7 +34,6 @@ import org.opensearch.client.opensearch.cat.segments.SegmentsRecord; import org.opensearch.client.opensearch.cat.shards.ShardsRecord; import org.opensearch.client.opensearch.core.IndexResponse; -import org.opensearch.client.opensearch.core.InfoResponse; import org.opensearch.client.opensearch.core.pit.CreatePitResponse; import org.opensearch.client.opensearch.indices.CreateIndexResponse; @@ -238,15 +237,8 @@ public void testCatSegments() throws Exception { @Test public void testCatPitSegments() throws Exception { - 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")) - ); + final Version version = getServerVersion(); + assumeTrue("The PIT is supported in OpenSearch 2.4.0 and later", version.onOrAfter(Version.V_2_4_0)); createIndex("cat-pit-segments-test-index"); final IndexResponse index = javaClient().index( @@ -264,7 +256,7 @@ public void testCatPitSegments() throws Exception { assertNotNull("PitSegmentsResponse.segments() is null", PitSegmentsResponse.valueBody()); - if (Version.fromString(version).onOrAfter(Version.fromString("2.10.0"))) { + if (version.onOrAfter(Version.V_2_10_0)) { assertTrue("PitSegmentsResponse.segments().size() == 0", PitSegmentsResponse.valueBody().isEmpty()); } else { assertTrue("PitSegmentsResponse.segments().size() == 0", PitSegmentsResponse.valueBody().size() > 0); diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java index 5179443cc4..d78e124f51 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java @@ -617,15 +617,7 @@ 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")) - ); + assumeTrue("The PIT is supported in OpenSearch 2.4.0 and later", getServerVersion().onOrAfter(Version.V_2_4_0)); String index = "test-point-in-time"; diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractSearchRequestIT.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractSearchRequestIT.java index 1d9a76f200..47541fda78 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractSearchRequestIT.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractSearchRequestIT.java @@ -19,7 +19,6 @@ import org.opensearch.client.opensearch._types.query_dsl.MatchQuery; import org.opensearch.client.opensearch._types.query_dsl.Query; import org.opensearch.client.opensearch._types.query_dsl.TermQuery; -import org.opensearch.client.opensearch.core.InfoResponse; import org.opensearch.client.opensearch.core.SearchRequest; import org.opensearch.client.opensearch.core.SearchResponse; import org.opensearch.client.opensearch.indices.DeleteIndexRequest; @@ -63,12 +62,7 @@ public void shouldReturnSearchResults() throws Exception { @Test public void hybridSearchShouldReturnSearchResults() throws Exception { - InfoResponse info = javaClient().info(); - String version = info.version().number(); - if (version.contains("SNAPSHOT")) { - version = version.split("-")[0]; - } - assumeTrue("Hybrid search is supported from 2.10.0", Version.fromString(version).onOrAfter(Version.fromString("2.10.0"))); + assumeTrue("Hybrid search is supported from 2.10.0", getServerVersion().onOrAfter(Version.V_2_10_0)); final String index = "hybrid_search_request"; try { createIndex(index); diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java index 6403569faa..d8a3741cf1 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/OpenSearchJavaClientTestCase.java @@ -38,6 +38,7 @@ import org.opensearch.client.opensearch._types.ExpandWildcard; import org.opensearch.client.opensearch.cat.IndicesResponse; import org.opensearch.client.opensearch.cat.indices.IndicesRecord; +import org.opensearch.client.opensearch.core.InfoResponse; import org.opensearch.client.opensearch.indices.DeleteIndexRequest; import org.opensearch.client.opensearch.nodes.NodesInfoResponse; import org.opensearch.client.opensearch.nodes.info.NodeInfo; @@ -186,4 +187,15 @@ public static void cleanupJavaClient() throws IOException { protected boolean preserveIndicesUponCompletion() { return true; } + + protected Version getServerVersion() throws IOException { + final InfoResponse info = javaClient().info(); + + String version = info.version().number(); + if (version.contains("SNAPSHOT")) { + version = version.split("-")[0]; + } + + return Version.fromString(version); + } }