This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 1e1d85d24e4a388283419199bc97a3051f5b2cff Author: Adriano Machado <admac...@redhat.com> AuthorDate: Fri Jun 30 09:28:25 2023 -0400 [CAMEL-18837] Refactoring tests --- components/camel-opensearch/pom.xml | 11 +- .../opensearch/integration/OpensearchBulkIT.java | 57 +-- .../integration/OpensearchClusterIndexIT.java | 7 +- .../OpensearchGetSearchDeleteExistsUpdateIT.java | 394 +++++++++++---------- .../opensearch/integration/OpensearchIndexIT.java | 28 +- .../opensearch/integration/OpensearchPingIT.java | 2 +- .../integration/OpensearchScrollSearchIT.java | 12 +- .../integration/OpensearchSizeLimitIT.java | 27 +- .../integration/OpensearchTestSupport.java | 94 +++-- 9 files changed, 348 insertions(+), 284 deletions(-) diff --git a/components/camel-opensearch/pom.xml b/components/camel-opensearch/pom.xml index 06901b275a2..968c4fca974 100644 --- a/components/camel-opensearch/pom.xml +++ b/components/camel-opensearch/pom.xml @@ -75,14 +75,15 @@ <artifactId>camel-core-catalog</artifactId> <scope>test</scope> </dependency> + + <!-- test infra --> <dependency> - <groupId>org.awaitility</groupId> - <artifactId>awaitility</artifactId> - <version>${awaitility-version}</version> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-core</artifactId> + <version>${project.version}</version> <scope>test</scope> + <type>test-jar</type> </dependency> - - <!-- test infra --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-test-infra-opensearch</artifactId> diff --git a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchBulkIT.java b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchBulkIT.java index cb3bf9e98a8..77540d6d86a 100644 --- a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchBulkIT.java +++ b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchBulkIT.java @@ -60,7 +60,7 @@ class OpensearchBulkIT extends OpensearchTestSupport { documents.add(document1); documents.add(document2); - List<?> indexIds = template.requestBody("direct:bulk", documents, List.class); + List<?> indexIds = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", documents, List.class); assertNotNull(indexIds, "indexIds should be set"); assertCollectionSize("Indexed documents should match the size of documents", indexIds, documents.size()); } @@ -70,7 +70,7 @@ class OpensearchBulkIT extends OpensearchTestSupport { List<String> documents = List.of( "{\"testBulkWithString1\": \"some-value\"}", "{\"testBulkWithString2\": \"some-value\"}"); - List<?> indexIds = template.requestBody("direct:bulk", documents, List.class); + List<?> indexIds = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", documents, List.class); assertNotNull(indexIds, "indexIds should be set"); assertCollectionSize("Indexed documents should match the size of documents", indexIds, documents.size()); } @@ -81,7 +81,7 @@ class OpensearchBulkIT extends OpensearchTestSupport { "{\"testBulkWithBytes1\": \"some-value\"}".getBytes(StandardCharsets.UTF_8), "{\"testBulkWithBytes2\": \"some-value\"}".getBytes(StandardCharsets.UTF_8)); - List<?> indexIds = template.requestBody("direct:bulk", documents, List.class); + List<?> indexIds = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", documents, List.class); assertNotNull(indexIds, "indexIds should be set"); assertCollectionSize("Indexed documents should match the size of documents", indexIds, documents.size()); } @@ -92,7 +92,7 @@ class OpensearchBulkIT extends OpensearchTestSupport { new StringReader("{\"testBulkWithReader1\": \"some-value\"}"), new StringReader("{\"testBulkWithReader2\": \"some-value\"}")); - List<?> indexIds = template.requestBody("direct:bulk", documents, List.class); + List<?> indexIds = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", documents, List.class); assertNotNull(indexIds, "indexIds should be set"); assertCollectionSize("Indexed documents should match the size of documents", indexIds, documents.size()); } @@ -105,14 +105,14 @@ class OpensearchBulkIT extends OpensearchTestSupport { new ByteArrayInputStream( "{\"testBulkWithInputStream2\": \"some-value\"}".getBytes(StandardCharsets.UTF_8))); - List<?> indexIds = template.requestBody("direct:bulk", documents, List.class); + List<?> indexIds = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", documents, List.class); assertNotNull(indexIds, "indexIds should be set"); assertCollectionSize("Indexed documents should match the size of documents", indexIds, documents.size()); } @Test void testBulkListRequestBody() { - String prefix = createPrefix(); + String prefix = getPrefix(); // given List<Map<String, String>> request = new ArrayList<>(); @@ -121,7 +121,8 @@ class OpensearchBulkIT extends OpensearchTestSupport { valueMap.put("content", prefix + "hello"); request.add(valueMap); // when - List<?> indexedDocumentIds = template.requestBody("direct:bulk", request, List.class); + List<?> indexedDocumentIds + = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", request, List.class); // then assertThat(indexedDocumentIds, notNullValue()); @@ -130,7 +131,7 @@ class OpensearchBulkIT extends OpensearchTestSupport { @Test void testBulkRequestBody() { - String prefix = createPrefix(); + String prefix = getPrefix(); // given BulkRequest.Builder builder = new BulkRequest.Builder(); @@ -142,7 +143,8 @@ class OpensearchBulkIT extends OpensearchTestSupport { // when @SuppressWarnings("unchecked") - List<BulkResponseItem> response = template.requestBody("direct:bulk", builder, List.class); + List<BulkResponseItem> response + = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", builder, List.class); // then assertThat(response, notNullValue()); @@ -153,7 +155,7 @@ class OpensearchBulkIT extends OpensearchTestSupport { @Test void bulkRequestBody() { - String prefix = createPrefix(); + String prefix = getPrefix(); // given BulkRequest.Builder builder = new BulkRequest.Builder(); @@ -164,7 +166,8 @@ class OpensearchBulkIT extends OpensearchTestSupport { .build()); // when @SuppressWarnings("unchecked") - List<BulkResponseItem> response = template.requestBody("direct:bulk", builder, List.class); + List<BulkResponseItem> response + = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", builder, List.class); // then assertThat(response, notNullValue()); @@ -175,18 +178,20 @@ class OpensearchBulkIT extends OpensearchTestSupport { void bulkDeleteOperation() { // given Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = getCamelContextExtension().getProducerTemplate().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); DeleteOperation.Builder builder = new DeleteOperation.Builder().index("twitter").id(indexId); // when @SuppressWarnings("unchecked") - List<BulkResponseItem> response = template.requestBody("direct:bulk", List.of(builder), List.class); + List<BulkResponseItem> response + = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", List.of(builder), List.class); // then assertThat(response, notNullValue()); assertEquals(indexId, response.get(0).id()); - GetResponse<?> resp = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> resp + = getCamelContextExtension().getProducerTemplate().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(resp, "response should not be null"); assertNull(resp.source(), "response source should be null"); } @@ -194,17 +199,19 @@ class OpensearchBulkIT extends OpensearchTestSupport { @Test void bulkCreateOperation() { // given - String prefix = createPrefix(); + String prefix = getPrefix(); CreateOperation.Builder<?> builder = new CreateOperation.Builder<>().index("twitter").document(Map.of(prefix + "content", prefix + "hello")); // when @SuppressWarnings("unchecked") - List<BulkResponseItem> response = template.requestBody("direct:bulk", List.of(builder), List.class); + List<BulkResponseItem> response + = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", List.of(builder), List.class); // then assertThat(response, notNullValue()); - GetResponse<?> resp = template.requestBody("direct:get", response.get(0).id(), GetResponse.class); + GetResponse<?> resp = getCamelContextExtension().getProducerTemplate().requestBody("direct:get", response.get(0).id(), + GetResponse.class); assertNotNull(resp, "response should not be null"); assertNotNull(resp.source(), "response source should not be null"); } @@ -212,11 +219,13 @@ class OpensearchBulkIT extends OpensearchTestSupport { @Test void bulkUpdateOperation() { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = getCamelContextExtension().getProducerTemplate().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); + String prefix = getPrefix(); + Map<String, String> document - = Map.of(String.format("%skey2", createPrefix()), String.format("%svalue2", createPrefix())); + = Map.of(String.format("%skey2", prefix), String.format("%svalue2", prefix)); UpdateOperation<?> builder = new UpdateOperation.Builder<>() .index("twitter") @@ -225,16 +234,18 @@ class OpensearchBulkIT extends OpensearchTestSupport { .build(); @SuppressWarnings("unchecked") - List<BulkResponseItem> response = template.requestBody("direct:bulk", List.of(builder), List.class); + List<BulkResponseItem> response + = getCamelContextExtension().getProducerTemplate().requestBody("direct:bulk", List.of(builder), List.class); //now, verify GET succeeded assertThat(response, notNullValue()); - GetResponse<?> resp = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> resp + = getCamelContextExtension().getProducerTemplate().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(resp, "response should not be null"); assertNotNull(resp.source(), "response source should not be null"); assertInstanceOf(ObjectNode.class, resp.source(), "response source should be a ObjectNode"); - assertTrue(((ObjectNode) resp.source()).has(createPrefix() + "key2")); - assertEquals(createPrefix() + "value2", ((ObjectNode) resp.source()).get(createPrefix() + "key2").asText()); + assertTrue(((ObjectNode) resp.source()).has(prefix + "key2")); + assertEquals(prefix + "value2", ((ObjectNode) resp.source()).get(prefix + "key2").asText()); } @Override diff --git a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchClusterIndexIT.java b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchClusterIndexIT.java index e2ad8dbbfe6..2f663a68d6a 100644 --- a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchClusterIndexIT.java +++ b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchClusterIndexIT.java @@ -33,6 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; class OpensearchClusterIndexIT extends OpensearchTestSupport { + @Test void indexWithIpAndPort() throws Exception { Map<String, String> map = createIndexedData(); @@ -41,10 +42,10 @@ class OpensearchClusterIndexIT extends OpensearchTestSupport { headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); headers.put(OpensearchConstants.PARAM_INDEX_ID, "1"); - String indexId = template.requestBodyAndHeaders("direct:indexWithIpAndPort", map, headers, String.class); + String indexId = template().requestBodyAndHeaders("direct:indexWithIpAndPort", map, headers, String.class); assertNotNull(indexId, "indexId should be set"); - indexId = template.requestBodyAndHeaders("direct:indexWithIpAndPort", map, headers, String.class); + indexId = template().requestBodyAndHeaders("direct:indexWithIpAndPort", map, headers, String.class); assertNotNull(indexId, "indexId should be set"); assertTrue(client.get(new GetRequest.Builder().index("twitter").id("1").build(), ObjectNode.class).found(), @@ -59,7 +60,7 @@ class OpensearchClusterIndexIT extends OpensearchTestSupport { headers.put(OpensearchConstants.PARAM_INDEX_NAME, "facebook"); headers.put(OpensearchConstants.PARAM_INDEX_ID, "4"); - String indexId = template.requestBodyAndHeaders("direct:indexWithSniffer", map, headers, String.class); + String indexId = template().requestBodyAndHeaders("direct:indexWithSniffer", map, headers, String.class); assertNotNull(indexId, "indexId should be set"); assertTrue(client.get(new GetRequest.Builder().index("facebook").id("4").build(), ObjectNode.class).found(), diff --git a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchGetSearchDeleteExistsUpdateIT.java b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchGetSearchDeleteExistsUpdateIT.java index a5e63a99349..ca604e31953 100644 --- a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchGetSearchDeleteExistsUpdateIT.java +++ b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchGetSearchDeleteExistsUpdateIT.java @@ -23,7 +23,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.concurrent.TimeUnit; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.JsonNode; @@ -31,7 +30,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.opensearch.OpensearchConstants; import org.apache.camel.component.opensearch.OpensearchOperation; -import org.awaitility.Awaitility; import org.junit.jupiter.api.Test; import org.opensearch.client.opensearch._types.FieldValue; import org.opensearch.client.opensearch._types.Result; @@ -67,11 +65,11 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { void testIndexWithMap() { //first, Index a value Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); assertInstanceOf(ObjectNode.class, response.source(), "response source should be a ObjectNode"); @@ -83,11 +81,11 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { @Test void testIndexWithString() { //first, Index a value - String indexId = template.requestBody("direct:index", "{\"testIndexWithString\": \"some-value\"}", String.class); + String indexId = template().requestBody("direct:index", "{\"testIndexWithString\": \"some-value\"}", String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); assertInstanceOf(ObjectNode.class, response.source(), "response source should be a ObjectNode"); @@ -98,12 +96,12 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { @Test void testIndexWithReader() { //first, Index a value - String indexId = template.requestBody("direct:index", new StringReader("{\"testIndexWithReader\": \"some-value\"}"), + String indexId = template().requestBody("direct:index", new StringReader("{\"testIndexWithReader\": \"some-value\"}"), String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); assertInstanceOf(ObjectNode.class, response.source(), "response source should be a ObjectNode"); @@ -114,12 +112,12 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { @Test void testIndexWithBytes() { //first, Index a value - String indexId = template.requestBody("direct:index", + String indexId = template().requestBody("direct:index", "{\"testIndexWithBytes\": \"some-value\"}".getBytes(StandardCharsets.UTF_8), String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); assertInstanceOf(ObjectNode.class, response.source(), "response source should be a ObjectNode"); @@ -130,13 +128,13 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { @Test void testIndexWithInputStream() { //first, Index a value - String indexId = template.requestBody("direct:index", + String indexId = template().requestBody("direct:index", new ByteArrayInputStream("{\"testIndexWithInputStream\": \"some-value\"}".getBytes(StandardCharsets.UTF_8)), String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); assertInstanceOf(ObjectNode.class, response.source(), "response source should be a ObjectNode"); @@ -154,11 +152,11 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { product.setName("Guinness book of records 2021"); //first, Index a value - String indexId = template.requestBody("direct:index-product", product, String.class); + String indexId = template().requestBody("direct:index-product", product, String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBodyAndHeader("direct:get", indexId, + GetResponse<?> response = template().requestBodyAndHeader("direct:get", indexId, OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); @@ -172,11 +170,11 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { void testGetWithString() { //first, Index a value Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); assertInstanceOf(ObjectNode.class, response.source()); @@ -192,11 +190,11 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { product.setDescription("The book of the year!"); product.setName("Guinness book of records 1890"); - String indexId = template.requestBody("direct:index", product, String.class); + String indexId = template().requestBody("direct:index", product, String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBodyAndHeader( + GetResponse<?> response = template().requestBodyAndHeader( "direct:get", indexId, OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); @@ -209,12 +207,12 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { void testMGetWithString() { //first, Index a value Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded @SuppressWarnings("unchecked") - List<MultiGetResponseItem<?>> response = template.requestBody("direct:multiget", List.of(indexId), List.class); + List<MultiGetResponseItem<?>> response = template().requestBody("direct:multiget", List.of(indexId), List.class); assertNotNull(response, "response should not be null"); assertEquals(1, response.size(), "response should contain one result"); assertTrue(response.get(0).isResult()); @@ -232,12 +230,12 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { product.setDescription("The book of the year!"); product.setName("Guinness book of records 1890"); - String indexId = template.requestBody("direct:index", product, String.class); + String indexId = template().requestBody("direct:index", product, String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded @SuppressWarnings("unchecked") - List<MultiGetResponseItem<?>> response = template.requestBodyAndHeader( + List<MultiGetResponseItem<?>> response = template().requestBodyAndHeader( "direct:multiget", List.of(indexId), OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class, List.class); assertNotNull(response, "response should not be null"); assertEquals(1, response.size(), "response should contain one result"); @@ -252,33 +250,33 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { void testDeleteWithString() { //first, Index a value Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); //now, perform Delete - Result deleteResponse = template.requestBody("direct:delete", indexId, Result.class); + Result deleteResponse = template().requestBody("direct:delete", indexId, Result.class); assertNotNull(deleteResponse, "response should not be null"); //now, verify GET fails to find the indexed value - response = template.requestBody("direct:get", indexId, GetResponse.class); + response = template().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(response, "response should not be null"); assertNull(response.source(), "response source should be null"); } @Test - void testSearchWithMapQuery() { + void testSearchWithMapQuery() throws Exception { //first, Index a value Map<String, String> map1 = Map.of("testSearchWithMapQuery1", "foo"); Map<String, String> map2 = Map.of("testSearchWithMapQuery2", "bar"); Map<String, Object> headers = Map.of( OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Bulk, OpensearchConstants.PARAM_INDEX_NAME, "twitter"); - template.requestBodyAndHeaders("direct:start", List.of(Map.of("doc", map1), Map.of("doc", map2)), headers, + template().requestBodyAndHeaders("direct:start", List.of(Map.of("doc", map1), Map.of("doc", map2)), headers, String.class); // No match @@ -288,38 +286,40 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { match.put("match", actualQuery); Map<String, Object> query = new HashMap<>(); query.put("query", match); - HitsMetadata<?> response = template.requestBody("direct:search", query, HitsMetadata.class); + HitsMetadata<?> response = template().requestBody("direct:search", query, HitsMetadata.class); assertNotNull(response, "response should not be null"); assertNotNull(response.total()); assertEquals(0, response.total().value(), "response hits should be == 0"); // Match actualQuery.put("doc.testSearchWithMapQuery1", "foo"); - // the result may see stale data so use Awaitility - Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { - HitsMetadata<?> resp = template.requestBody("direct:search", query, HitsMetadata.class); - assertNotNull(resp, "response should not be null"); - assertNotNull(resp.total()); - assertEquals(1, resp.total().value(), "response hits should be == 1"); - assertEquals(1, resp.hits().size(), "response hits should be == 1"); - Object result = resp.hits().get(0).source(); - assertInstanceOf(ObjectNode.class, result); - assertTrue(((ObjectNode) result).has("doc")); - JsonNode node = ((ObjectNode) result).get("doc"); - assertTrue(node.has("testSearchWithMapQuery1")); - assertEquals("foo", node.get("testSearchWithMapQuery1").asText()); - }); + + // Delay the execution, because the search is getting stale results + Thread.sleep(2000); + + HitsMetadata<?> resp = template().requestBody("direct:search", query, HitsMetadata.class); + + assertNotNull(resp, "response should not be null"); + assertNotNull(resp.total()); + assertEquals(1, resp.total().value(), "response hits should be == 1"); + assertEquals(1, resp.hits().size(), "response hits should be == 1"); + Object result = resp.hits().get(0).source(); + assertInstanceOf(ObjectNode.class, result); + assertTrue(((ObjectNode) result).has("doc")); + JsonNode node = ((ObjectNode) result).get("doc"); + assertTrue(node.has("testSearchWithMapQuery1")); + assertEquals("foo", node.get("testSearchWithMapQuery1").asText()); } @Test - void testSearchWithStringQuery() { + void testSearchWithStringQuery() throws Exception { //first, Index a value Map<String, String> map1 = Map.of("testSearchWithStringQuery1", "foo"); Map<String, String> map2 = Map.of("testSearchWithStringQuery2", "bar"); Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Bulk); headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); - template.requestBodyAndHeaders("direct:start", List.of(Map.of("doc", map1), Map.of("doc", map2)), headers, + template().requestBodyAndHeaders("direct:start", List.of(Map.of("doc", map1), Map.of("doc", map2)), headers, String.class); // No match @@ -329,42 +329,43 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { } """; - HitsMetadata<?> response = template.requestBody("direct:search", query, HitsMetadata.class); + HitsMetadata<?> response = template().requestBody("direct:search", query, HitsMetadata.class); assertNotNull(response, "response should not be null"); assertNotNull(response.total()); assertEquals(0, response.total().value(), "response hits should be == 0"); + // Delay the execution, because the search is getting stale results + Thread.sleep(2000); + // Match String q = """ { "query" : { "match" : { "doc.testSearchWithStringQuery1" : "foo" }} } """; - // the result may see stale data so use Awaitility - Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { - HitsMetadata<?> resp = template.requestBody("direct:search", q, HitsMetadata.class); - assertNotNull(resp, "response should not be null"); - assertNotNull(resp.total()); - assertEquals(1, resp.total().value(), "response hits should be == 1"); - assertEquals(1, resp.hits().size(), "response hits should be == 1"); - Object result = resp.hits().get(0).source(); - assertInstanceOf(ObjectNode.class, result); - assertTrue(((ObjectNode) result).has("doc")); - JsonNode node = ((ObjectNode) result).get("doc"); - assertTrue(node.has("testSearchWithStringQuery1")); - assertEquals("foo", node.get("testSearchWithStringQuery1").asText()); - }); + + HitsMetadata<?> resp = template().requestBody("direct:search", q, HitsMetadata.class); + assertNotNull(resp, "response should not be null"); + assertNotNull(resp.total()); + assertEquals(1, resp.total().value(), "response hits should be == 1"); + assertEquals(1, resp.hits().size(), "response hits should be == 1"); + Object result = resp.hits().get(0).source(); + assertInstanceOf(ObjectNode.class, result); + assertTrue(((ObjectNode) result).has("doc")); + JsonNode node = ((ObjectNode) result).get("doc"); + assertTrue(node.has("testSearchWithStringQuery1")); + assertEquals("foo", node.get("testSearchWithStringQuery1").asText()); } @Test - void testSearchWithBuilder() { + void testSearchWithBuilder() throws Exception { //first, Index a value Map<String, String> map1 = Map.of("testSearchWithBuilder1", "foo"); Map<String, String> map2 = Map.of("testSearchWithBuilder2", "bar"); Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Bulk); headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); - template.requestBodyAndHeaders("direct:start", List.of(Map.of("doc", map1), Map.of("doc", map2)), headers, + template().requestBodyAndHeaders("direct:start", List.of(Map.of("doc", map1), Map.of("doc", map2)), headers, String.class); // No match @@ -372,36 +373,37 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { .query(new Query.Builder() .match(new MatchQuery.Builder().field("doc.testSearchWithBuilder1").query(FieldValue.of("bar")).build()) .build()); - HitsMetadata<?> response = template.requestBody("direct:search", builder, HitsMetadata.class); + HitsMetadata<?> response = template().requestBody("direct:search", builder, HitsMetadata.class); assertNotNull(response, "response should not be null"); assertNotNull(response.total()); assertEquals(0, response.total().value(), "response hits should be == 0"); + // Delay the execution, because the search is getting stale results + Thread.sleep(2000); + + SearchRequest.Builder b = new SearchRequest.Builder() + .query(new Query.Builder() + .match(new MatchQuery.Builder().field("doc.testSearchWithBuilder1").query(FieldValue.of("foo")) + .build()) + .build()); + // Match - // the result may see stale data so use Awaitility - Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { - SearchRequest.Builder b = new SearchRequest.Builder() - .query(new Query.Builder() - .match(new MatchQuery.Builder().field("doc.testSearchWithBuilder1").query(FieldValue.of("foo")) - .build()) - .build()); - - HitsMetadata<?> resp = template.requestBody("direct:search", b, HitsMetadata.class); - assertNotNull(resp, "response should not be null"); - assertNotNull(resp.total()); - assertEquals(1, resp.total().value(), "response hits should be == 1"); - assertEquals(1, resp.hits().size(), "response hits should be == 1"); - Object result = resp.hits().get(0).source(); - assertInstanceOf(ObjectNode.class, result); - assertTrue(((ObjectNode) result).has("doc")); - JsonNode node = ((ObjectNode) result).get("doc"); - assertTrue(node.has("testSearchWithBuilder1")); - assertEquals("foo", node.get("testSearchWithBuilder1").asText()); - }); + HitsMetadata<?> resp = template().requestBody("direct:search", b, HitsMetadata.class); + + assertNotNull(resp, "response should not be null"); + assertNotNull(resp.total()); + assertEquals(1, resp.total().value(), "response hits should be == 1"); + assertEquals(1, resp.hits().size(), "response hits should be == 1"); + Object result = resp.hits().get(0).source(); + assertInstanceOf(ObjectNode.class, result); + assertTrue(((ObjectNode) result).has("doc")); + JsonNode node = ((ObjectNode) result).get("doc"); + assertTrue(node.has("testSearchWithBuilder1")); + assertEquals("foo", node.get("testSearchWithBuilder1").asText()); } @Test - void testSearchWithDocumentType() { + void testSearchWithDocumentType() throws Exception { //first, Index a value Product product1 = new Product(); product1.setId("book-world-records-2020"); @@ -419,70 +421,72 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Bulk); headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); - template.requestBodyAndHeaders("direct:start", List.of(product1, product2), headers, String.class); + template().requestBodyAndHeaders("direct:start", List.of(product1, product2), headers, String.class); // No match SearchRequest.Builder builder = new SearchRequest.Builder() .query(new Query.Builder().match(new MatchQuery.Builder().field("doc.id").query(FieldValue.of("bar")).build()) .build()); - HitsMetadata<?> response = template.requestBodyAndHeader( + HitsMetadata<?> response = template().requestBodyAndHeader( "direct:search", builder, OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class, HitsMetadata.class); assertNotNull(response, "response should not be null"); assertNotNull(response.total()); assertEquals(0, response.total().value(), "response hits should be == 0"); + SearchRequest.Builder b = new SearchRequest.Builder() + .query(new Query.Builder().match(new MatchQuery.Builder().field("id").query(FieldValue.of("2020")).build()) + .build()); + + // Delay the execution, because the search is getting stale results + Thread.sleep(2000); + // Match - // the result may see stale data so use Awaitility - Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { - SearchRequest.Builder b = new SearchRequest.Builder() - .query(new Query.Builder().match(new MatchQuery.Builder().field("id").query(FieldValue.of("2020")).build()) - .build()); - - HitsMetadata<?> resp = template.requestBodyAndHeader( - "direct:search", b, OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class, HitsMetadata.class); - assertNotNull(resp, "response should not be null"); - assertNotNull(resp.total()); - assertEquals(1, resp.total().value(), "response hits should be == 1"); - assertEquals(1, resp.hits().size(), "response hits should be == 1"); - Object result = resp.hits().get(0).source(); - assertInstanceOf(Product.class, result); - Product p = (Product) result; - assertEquals(product1, p); - }); + HitsMetadata<?> resp = template().requestBodyAndHeader("direct:search", b, OpensearchConstants.PARAM_DOCUMENT_CLASS, + Product.class, HitsMetadata.class); + + assertNotNull(resp, "response should not be null"); + assertNotNull(resp.total()); + assertEquals(1, resp.total().value(), "response hits should be == 1"); + assertEquals(1, resp.hits().size(), "response hits should be == 1"); + Object result = resp.hits().get(0).source(); + assertInstanceOf(Product.class, result); + Product p = (Product) result; + assertEquals(product1, p); } @Test - void testMultiSearch() { + void testMultiSearch() throws Exception { //first, Index a value Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); - // the result may see stale data so use Awaitility - Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { - //now, verify GET succeeded - MsearchRequest.Builder builder = new MsearchRequest.Builder().index("twitter").searches( - new RequestItem.Builder().header(new MultisearchHeader.Builder().build()) - .body(new MultisearchBody.Builder().query(b -> b.matchAll(x -> x)).build()).build(), - new RequestItem.Builder().header(new MultisearchHeader.Builder().build()) - .body(new MultisearchBody.Builder().query(b -> b.matchAll(x -> x)).build()).build()); - @SuppressWarnings("unchecked") - List<MultiSearchResponseItem<?>> response = template.requestBody("direct:multiSearch", builder, List.class); - assertNotNull(response, "response should not be null"); - assertEquals(2, response.size(), "response should be == 2"); - assertInstanceOf(MultiSearchResponseItem.class, response.get(0)); - assertTrue(response.get(0).isResult()); - assertNotNull(response.get(0).result()); - assertTrue(response.get(0).result().hits().total().value() > 0); - assertInstanceOf(MultiSearchResponseItem.class, response.get(1)); - assertTrue(response.get(1).isResult()); - assertNotNull(response.get(1).result()); - assertTrue(response.get(1).result().hits().total().value() > 0); - }); + MsearchRequest.Builder builder = new MsearchRequest.Builder().index("twitter").searches( + new RequestItem.Builder().header(new MultisearchHeader.Builder().build()) + .body(new MultisearchBody.Builder().query(b -> b.matchAll(x -> x)).build()).build(), + new RequestItem.Builder().header(new MultisearchHeader.Builder().build()) + .body(new MultisearchBody.Builder().query(b -> b.matchAll(x -> x)).build()).build()); + + // Delay the execution, because the search is getting stale results + Thread.sleep(2000); + + @SuppressWarnings("unchecked") + List<MultiSearchResponseItem<?>> response = template().requestBody("direct:multiSearch", builder, List.class); + + assertNotNull(response, "response should not be null"); + assertEquals(2, response.size(), "response should be == 2"); + assertInstanceOf(MultiSearchResponseItem.class, response.get(0)); + assertTrue(response.get(0).isResult()); + assertNotNull(response.get(0).result()); + assertTrue(response.get(0).result().hits().total().value() > 0); + assertInstanceOf(MultiSearchResponseItem.class, response.get(1)); + assertTrue(response.get(1).isResult()); + assertNotNull(response.get(1).result()); + assertTrue(response.get(1).result().hits().total().value() > 0); } @Test - void testMultiSearchWithDocumentType() { + void testMultiSearchWithDocumentType() throws Exception { //first, Index a value Product product = new Product(); product.setId("book-world-records-2022"); @@ -490,58 +494,60 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { product.setPrice(100); product.setDescription("The book of the year!"); product.setName("Guinness book of records 2022"); - String indexId = template.requestBodyAndHeader("direct:index", product, OpensearchConstants.PARAM_INDEX_NAME, + String indexId = template().requestBodyAndHeader("direct:index", product, OpensearchConstants.PARAM_INDEX_NAME, "multi-search", String.class); assertNotNull(indexId, "indexId should be set"); - // the result may see stale data so use Awaitility - Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { - //now, verify GET succeeded - MsearchRequest.Builder builder = new MsearchRequest.Builder().index("multi-search").searches( - new RequestItem.Builder().header(new MultisearchHeader.Builder().build()) - .body(new MultisearchBody.Builder().query(b -> b.matchAll(x -> x)).build()).build(), - new RequestItem.Builder().header(new MultisearchHeader.Builder().build()) - .body(new MultisearchBody.Builder().query(b -> b.matchAll(x -> x)).build()).build()); - @SuppressWarnings("unchecked") - List<MultiSearchResponseItem<?>> response = template.requestBodyAndHeaders( - "direct:multiSearch", builder, - Map.of( - OpensearchConstants.PARAM_INDEX_NAME, "multi-search", - OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class), - List.class); - assertNotNull(response, "response should not be null"); - assertEquals(2, response.size(), "response should be == 2"); - assertInstanceOf(MultiSearchResponseItem.class, response.get(0)); - assertTrue(response.get(0).isResult()); - assertNotNull(response.get(0).result()); - assertTrue(response.get(0).result().hits().total().value() > 0); - assertInstanceOf(MultiSearchResponseItem.class, response.get(1)); - assertTrue(response.get(1).isResult()); - assertNotNull(response.get(1).result()); - assertTrue(response.get(1).result().hits().total().value() > 0); - }); + MsearchRequest.Builder builder = new MsearchRequest.Builder().index("multi-search").searches( + new RequestItem.Builder().header(new MultisearchHeader.Builder().build()) + .body(new MultisearchBody.Builder().query(b -> b.matchAll(x -> x)).build()).build(), + new RequestItem.Builder().header(new MultisearchHeader.Builder().build()) + .body(new MultisearchBody.Builder().query(b -> b.matchAll(x -> x)).build()).build()); + + // Delay the execution, because the search is getting stale results + Thread.sleep(2000); + + @SuppressWarnings("unchecked") + List<MultiSearchResponseItem<?>> response = template().requestBodyAndHeaders( + "direct:multiSearch", builder, + Map.of( + OpensearchConstants.PARAM_INDEX_NAME, "multi-search", + OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class), + List.class); + + assertNotNull(response, "response should not be null"); + assertEquals(2, response.size(), "response should be == 2"); + assertInstanceOf(MultiSearchResponseItem.class, response.get(0)); + assertTrue(response.get(0).isResult()); + assertNotNull(response.get(0).result()); + assertTrue(response.get(0).result().hits().total().value() > 0); + assertInstanceOf(MultiSearchResponseItem.class, response.get(1)); + assertTrue(response.get(1).isResult()); + assertNotNull(response.get(1).result()); + assertTrue(response.get(1).result().hits().total().value() > 0); } @Test void testUpdateWithMap() { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); Map<String, String> newMap = new HashMap<>(); - newMap.put(createPrefix() + "key2", createPrefix() + "value2"); + String prefix = getPrefix(); + newMap.put(prefix + "key2", prefix + "value2"); Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_INDEX_ID, indexId); - indexId = template.requestBodyAndHeaders("direct:update", Map.of("doc", newMap), headers, String.class); + indexId = template().requestBodyAndHeaders("direct:update", Map.of("doc", newMap), headers, String.class); assertNotNull(indexId, "indexId should be set"); //now, verify GET succeeded - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); assertInstanceOf(ObjectNode.class, response.source(), "response source should be a ObjectNode"); - assertTrue(((ObjectNode) response.source()).has(createPrefix() + "key2")); - assertEquals(createPrefix() + "value2", ((ObjectNode) response.source()).get(createPrefix() + "key2").asText()); + assertTrue(((ObjectNode) response.source()).has(prefix + "key2")); + assertEquals(prefix + "value2", ((ObjectNode) response.source()).get(prefix + "key2").asText()); } @Test @@ -552,11 +558,11 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Index); headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); - String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + String indexId = template().requestBodyAndHeaders("direct:start", map, headers, String.class); //now, verify GET headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.GetById); - GetResponse<?> response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); + GetResponse<?> response = template().requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); } @@ -569,12 +575,12 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Index); headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); - template.requestBodyAndHeaders("direct:start", map, headers, String.class); + template().requestBodyAndHeaders("direct:start", map, headers, String.class); //now, verify GET headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Exists); headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); - Boolean exists = template.requestBodyAndHeaders("direct:exists", "", headers, Boolean.class); + Boolean exists = template().requestBodyAndHeaders("direct:exists", "", headers, Boolean.class); assertNotNull(exists, "response should not be null"); assertTrue(exists, "Index should exists"); } @@ -584,7 +590,7 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Exists); headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter-tweet"); - Boolean exists = template.requestBodyAndHeaders("direct:exists", "", headers, Boolean.class); + Boolean exists = template().requestBodyAndHeaders("direct:exists", "", headers, Boolean.class); assertNotNull(exists, "response should not be null"); assertFalse(exists, "Index should not exists"); } @@ -597,23 +603,23 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Index); headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); - String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + String indexId = template().requestBodyAndHeaders("direct:start", map, headers, String.class); //now, verify GET headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.GetById); - GetResponse<?> response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); + GetResponse<?> response = template().requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); assertNotNull(response, "response should not be null"); assertNotNull(response.source(), "response source should not be null"); //now, perform Delete headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Delete); Result deleteResponse - = template.requestBodyAndHeaders("direct:start", indexId, headers, Result.class); + = template().requestBodyAndHeaders("direct:start", indexId, headers, Result.class); assertEquals(Result.Deleted, deleteResponse, "response should not be null"); //now, verify GET fails to find the indexed value headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.GetById); - response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); + response = template().requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class); assertNotNull(response, "response should not be null"); assertNull(response.source(), "response source should be null"); } @@ -626,32 +632,32 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); headers.put(OpensearchConstants.PARAM_INDEX_ID, "123"); - String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + String indexId = template().requestBodyAndHeaders("direct:start", map, headers, String.class); assertNotNull(indexId, "indexId should be set"); assertEquals("123", indexId, "indexId should be equals to the provided id"); headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Update); - indexId = template.requestBodyAndHeaders("direct:start", Map.of("doc", map), headers, String.class); + indexId = template().requestBodyAndHeaders("direct:start", Map.of("doc", map), headers, String.class); assertNotNull(indexId, "indexId should be set"); assertEquals("123", indexId, "indexId should be equals to the provided id"); } @Test void testGetRequestBody() { - String prefix = createPrefix(); + String prefix = getPrefix(); // given GetRequest.Builder builder = new GetRequest.Builder().index(prefix + "foo"); // when - String documentId = template.requestBody("direct:index", + String documentId = template().requestBody("direct:index", new IndexRequest.Builder<>() .index(prefix + "foo") .id(prefix + "testId") .document(Map.of(prefix + "content", prefix + "hello")), String.class); - GetResponse<?> response = template.requestBody("direct:get", + GetResponse<?> response = template().requestBody("direct:get", builder.id(documentId), GetResponse.class); // then @@ -665,29 +671,29 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { @Test void testDeleteWithBuilder() { - String prefix = createPrefix(); + String prefix = getPrefix(); // given - String documentId = template.requestBody("direct:index", + String documentId = template().requestBody("direct:index", new IndexRequest.Builder<>() .index(prefix + "foo") .id(prefix + "testId") .document(Map.of(prefix + "content", prefix + "hello")), String.class); - GetResponse<?> getResponse = template.requestBodyAndHeader( + GetResponse<?> getResponse = template().requestBodyAndHeader( "direct:get", documentId, OpensearchConstants.PARAM_INDEX_NAME, prefix + "foo", GetResponse.class); assertNotNull(getResponse, "response should not be null"); assertNotNull(getResponse.source(), "response source should not be null"); // when Result response - = template.requestBody("direct:delete", new DeleteRequest.Builder().index(prefix + "foo").id(documentId), + = template().requestBody("direct:delete", new DeleteRequest.Builder().index(prefix + "foo").id(documentId), Result.class); // then assertThat(response, equalTo(Result.Deleted)); - getResponse = template.requestBodyAndHeader( + getResponse = template().requestBodyAndHeader( "direct:get", documentId, OpensearchConstants.PARAM_INDEX_NAME, prefix + "foo", GetResponse.class); assertNotNull(getResponse, "response should not be null"); assertNull(getResponse.source(), "response source should be null"); @@ -696,17 +702,17 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { @Test void testUpdateWithString() { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); String key = map.keySet().iterator().next(); Object body = String.format("{ \"doc\": {\"%s\" : \"testUpdateWithString-updated\"}}", key); Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_INDEX_ID, indexId); - indexId = template.requestBodyAndHeaders("direct:update", body, headers, String.class); + indexId = template().requestBodyAndHeaders("direct:update", body, headers, String.class); assertNotNull(indexId, "indexId should be set"); - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertThat(response.source(), notNullValue()); ObjectNode node = (ObjectNode) response.source(); assertThat(node.has(key), equalTo(true)); @@ -716,17 +722,17 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { @Test void testUpdateWithReader() { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); String key = map.keySet().iterator().next(); Object body = new StringReader(String.format("{ \"doc\": {\"%s\" : \"testUpdateWithReader-updated\"}}", key)); Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_INDEX_ID, indexId); - indexId = template.requestBodyAndHeaders("direct:update", body, headers, String.class); + indexId = template().requestBodyAndHeaders("direct:update", body, headers, String.class); assertNotNull(indexId, "indexId should be set"); - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertThat(response.source(), notNullValue()); ObjectNode node = (ObjectNode) response.source(); assertThat(node.has(key), equalTo(true)); @@ -736,7 +742,7 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { @Test void testUpdateWithBytes() { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); String key = map.keySet().iterator().next(); Object body @@ -744,10 +750,10 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_INDEX_ID, indexId); - indexId = template.requestBodyAndHeaders("direct:update", body, headers, String.class); + indexId = template().requestBodyAndHeaders("direct:update", body, headers, String.class); assertNotNull(indexId, "indexId should be set"); - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertThat(response.source(), notNullValue()); ObjectNode node = (ObjectNode) response.source(); assertThat(node.has(key), equalTo(true)); @@ -757,7 +763,7 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { @Test void testUpdateWithInputStream() { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); String key = map.keySet().iterator().next(); Object body = new ByteArrayInputStream( @@ -766,10 +772,10 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_INDEX_ID, indexId); - indexId = template.requestBodyAndHeaders("direct:update", body, headers, String.class); + indexId = template().requestBodyAndHeaders("direct:update", body, headers, String.class); assertNotNull(indexId, "indexId should be set"); - GetResponse<?> response = template.requestBody("direct:get", indexId, GetResponse.class); + GetResponse<?> response = template().requestBody("direct:get", indexId, GetResponse.class); assertThat(response.source(), notNullValue()); ObjectNode node = (ObjectNode) response.source(); assertThat(node.has(key), equalTo(true)); @@ -785,7 +791,7 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { product.setDescription("The book of the year!"); product.setName("Guinness book of records 2010"); - String indexId = template.requestBody("direct:index", product, String.class); + String indexId = template().requestBody("direct:index", product, String.class); assertNotNull(indexId, "indexId should be set"); Product productUpdate = new Product(); @@ -796,10 +802,10 @@ class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { Map<String, Object> headers = new HashMap<>(); headers.put(OpensearchConstants.PARAM_INDEX_ID, indexId); headers.put(OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class); - indexId = template.requestBodyAndHeaders("direct:update", productUpdate, headers, String.class); + indexId = template().requestBodyAndHeaders("direct:update", productUpdate, headers, String.class); assertNotNull(indexId, "indexId should be set"); - GetResponse<?> response = template.requestBodyAndHeader( + GetResponse<?> response = template().requestBodyAndHeader( "direct:get", indexId, OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class, GetResponse.class); assertThat(response.source(), notNullValue()); Product actual = (Product) response.source(); diff --git a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchIndexIT.java b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchIndexIT.java index d5174a84b59..469377f977c 100644 --- a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchIndexIT.java +++ b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchIndexIT.java @@ -35,40 +35,40 @@ class OpensearchIndexIT extends OpensearchTestSupport { @Test void testIndex() { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); } @Test void testIndexDeleteWithBuilder() { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); - boolean exists = template.requestBody("direct:exists", null, Boolean.class); + boolean exists = template().requestBody("direct:exists", null, Boolean.class); assertTrue(exists, "index should be present"); DeleteIndexRequest.Builder builder = new DeleteIndexRequest.Builder().index("twitter"); - Boolean status = template.requestBody("direct:deleteIndex", builder, Boolean.class); + Boolean status = template().requestBody("direct:deleteIndex", builder, Boolean.class); assertEquals(true, status, "status should be 200"); - exists = template.requestBody("direct:exists", null, Boolean.class); + exists = template().requestBody("direct:exists", null, Boolean.class); assertFalse(exists, "index should be absent"); } @Test void testIndexDeleteWithString() { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:index", map, String.class); + String indexId = template().requestBody("direct:index", map, String.class); assertNotNull(indexId, "indexId should be set"); - boolean exists = template.requestBody("direct:exists", null, Boolean.class); + boolean exists = template().requestBody("direct:exists", null, Boolean.class); assertTrue(exists, "index should be present"); - Boolean status = template.requestBody("direct:deleteIndex", "twitter", Boolean.class); + Boolean status = template().requestBody("direct:deleteIndex", "twitter", Boolean.class); assertEquals(true, status, "status should be 200"); - exists = template.requestBody("direct:exists", null, Boolean.class); + exists = template().requestBody("direct:exists", null, Boolean.class); assertFalse(exists, "index should be absent"); } @@ -79,7 +79,7 @@ class OpensearchIndexIT extends OpensearchTestSupport { headers.put(OpensearchConstants.PARAM_OPERATION, OpensearchOperation.Index); headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); - String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + String indexId = template().requestBodyAndHeaders("direct:start", map, headers, String.class); assertNotNull(indexId, "indexId should be set"); } @@ -91,21 +91,21 @@ class OpensearchIndexIT extends OpensearchTestSupport { headers.put(OpensearchConstants.PARAM_INDEX_NAME, "twitter"); headers.put(OpensearchConstants.PARAM_INDEX_ID, "123"); - String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class); + String indexId = template().requestBodyAndHeaders("direct:start", map, headers, String.class); assertNotNull(indexId, "indexId should be set"); assertEquals("123", indexId, "indexId should be equals to the provided id"); } @Test void testExists() { - boolean exists = template.requestBodyAndHeader( + boolean exists = template().requestBodyAndHeader( "direct:exists", null, OpensearchConstants.PARAM_INDEX_NAME, "test_exists", Boolean.class); assertFalse(exists, "index should be absent"); Map<String, String> map = createIndexedData(); - template.sendBodyAndHeader("direct:index", map, OpensearchConstants.PARAM_INDEX_NAME, "test_exists"); + template().sendBodyAndHeader("direct:index", map, OpensearchConstants.PARAM_INDEX_NAME, "test_exists"); - exists = template.requestBodyAndHeader( + exists = template().requestBodyAndHeader( "direct:exists", null, OpensearchConstants.PARAM_INDEX_NAME, "test_exists", Boolean.class); assertTrue(exists, "index should be present"); } diff --git a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchPingIT.java b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchPingIT.java index 23e0fb0c96a..a06bdb5076b 100644 --- a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchPingIT.java +++ b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchPingIT.java @@ -25,7 +25,7 @@ class OpensearchPingIT extends OpensearchTestSupport { @Test void testPing() { - boolean pingResult = template.requestBody("direct:ping", "test", Boolean.class); + boolean pingResult = template().requestBody("direct:ping", "test", Boolean.class); assertTrue(pingResult, "indexId should be set"); } diff --git a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchScrollSearchIT.java b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchScrollSearchIT.java index 585290db046..88cd7b4675c 100644 --- a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchScrollSearchIT.java +++ b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchScrollSearchIT.java @@ -54,7 +54,7 @@ class OpensearchScrollSearchIT extends OpensearchTestSupport { // add some documents for (int i = 0; i < 10; i++) { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:scroll-index", map, String.class); + String indexId = template().requestBody("direct:scroll-index", map, String.class); assertNotNull(indexId, "indexId should be set"); } @@ -65,13 +65,13 @@ class OpensearchScrollSearchIT extends OpensearchTestSupport { SearchRequest.Builder req = getScrollSearchRequestBuilder(TWITTER_OPENSEARCH_INDEX_NAME); - Exchange exchange = ExchangeBuilder.anExchange(context) + Exchange exchange = ExchangeBuilder.anExchange(camelContext()) .withHeader(PARAM_SCROLL_KEEP_ALIVE_MS, 50000) .withHeader(PARAM_SCROLL, true) .withBody(req) .build(); - exchange = template.send("direct:scroll-search", exchange); + exchange = template().send("direct:scroll-search", exchange); try (OpensearchScrollRequestIterator<?> scrollRequestIterator = exchange.getIn().getBody(OpensearchScrollRequestIterator.class)) { @@ -96,7 +96,7 @@ class OpensearchScrollSearchIT extends OpensearchTestSupport { // add some documents for (int i = 0; i < 10; i++) { Map<String, String> map = createIndexedData(); - String indexId = template.requestBody("direct:scroll-n-split-index", map, String.class); + String indexId = template().requestBody("direct:scroll-n-split-index", map, String.class); assertNotNull(indexId, "indexId should be set"); } @@ -111,8 +111,8 @@ class OpensearchScrollSearchIT extends OpensearchTestSupport { SearchRequest.Builder req = getScrollSearchRequestBuilder(SPLIT_TWITTER_OPENSEARCH_INDEX_NAME); - Exchange exchange = ExchangeBuilder.anExchange(context).withBody(req).build(); - exchange = template.send("direct:scroll-n-split-search", exchange); + Exchange exchange = ExchangeBuilder.anExchange(camelContext()).withBody(req).build(); + exchange = template().send("direct:scroll-n-split-search", exchange); // wait for aggregation mock.assertIsSatisfied(); diff --git a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchSizeLimitIT.java b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchSizeLimitIT.java index cac6a8063a2..4ca3b3d9bb8 100644 --- a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchSizeLimitIT.java +++ b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchSizeLimitIT.java @@ -18,22 +18,22 @@ package org.apache.camel.component.opensearch.integration; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.TimeUnit; import org.apache.camel.builder.RouteBuilder; -import org.awaitility.Awaitility; import org.junit.jupiter.api.Test; import org.opensearch.client.opensearch.core.search.HitsMetadata; +import static org.junit.jupiter.api.Assertions.assertEquals; + class OpensearchSizeLimitIT extends OpensearchTestSupport { @Test - void testSize() { + void testSize() throws Exception { //put 4 - template.requestBody("direct:index", getContent("content"), String.class); - template.requestBody("direct:index", getContent("content1"), String.class); - template.requestBody("direct:index", getContent("content2"), String.class); - template.requestBody("direct:index", getContent("content3"), String.class); + template().requestBody("direct:index", getContent("content"), String.class); + template().requestBody("direct:index", getContent("content1"), String.class); + template().requestBody("direct:index", getContent("content2"), String.class); + template().requestBody("direct:index", getContent("content3"), String.class); String query = """ { @@ -43,12 +43,13 @@ class OpensearchSizeLimitIT extends OpensearchTestSupport { } """; - // the result may see stale data so use Awaitility - Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> { - HitsMetadata<?> searchWithSizeTwo = template.requestBody("direct:searchWithSizeTwo", query, HitsMetadata.class); - HitsMetadata<?> searchFrom3 = template.requestBody("direct:searchFrom3", query, HitsMetadata.class); - return searchWithSizeTwo.hits().size() == 2 && searchFrom3.hits().size() == 1; - }); + // Delay the execution, because the search is getting stale results + Thread.sleep(2000); + + HitsMetadata<?> searchWithSizeTwo = template().requestBody("direct:searchWithSizeTwo", query, HitsMetadata.class); + HitsMetadata<?> searchFrom3 = template().requestBody("direct:searchFrom3", query, HitsMetadata.class); + assertEquals(2, searchWithSizeTwo.hits().size()); + assertEquals(1, searchFrom3.hits().size()); } @Override diff --git a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchTestSupport.java b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchTestSupport.java index 431217a51e6..051068e6a6b 100644 --- a/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchTestSupport.java +++ b/components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchTestSupport.java @@ -16,19 +16,32 @@ */ package org.apache.camel.component.opensearch.integration; +import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.opensearch.OpensearchComponent; +import org.apache.camel.test.infra.core.CamelContextExtension; +import org.apache.camel.test.infra.core.DefaultCamelContextExtension; +import org.apache.camel.test.infra.core.annotations.ContextFixture; +import org.apache.camel.test.infra.core.annotations.RouteFixture; +import org.apache.camel.test.infra.core.api.CamelTestSupportHelper; +import org.apache.camel.test.infra.core.api.ConfigurableContext; +import org.apache.camel.test.infra.core.api.ConfigurableRoute; import org.apache.camel.test.infra.opensearch.services.OpenSearchService; import org.apache.camel.test.infra.opensearch.services.OpenSearchServiceFactory; -import org.apache.camel.test.junit5.CamelTestSupport; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.RegisterExtension; import org.opensearch.client.RestClient; @@ -40,19 +53,26 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; @TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class OpensearchTestSupport extends CamelTestSupport { +public abstract class OpensearchTestSupport implements CamelTestSupportHelper, ConfigurableRoute, ConfigurableContext { + @Order(1) @RegisterExtension - protected static OpenSearchService service = OpenSearchServiceFactory.createSingletonService(); + public static final OpenSearchService service = OpenSearchServiceFactory.createSingletonService(); + + @Order(2) + @RegisterExtension + public static final CamelContextExtension contextExtension = new DefaultCamelContextExtension(); protected static String clusterName = "docker-cluster"; - protected static RestClient restClient; - protected static OpenSearchClient client; private static final Logger LOG = LoggerFactory.getLogger(OpensearchTestSupport.class); - @Override - protected void setupResources() throws Exception { - super.setupResources(); + protected RestClient restClient; + protected OpenSearchClient client; + + private String prefix; + + @BeforeEach + public void beforeEach(TestInfo testInfo) { HttpHost host = new HttpHost(service.getOpenSearchHost(), service.getPort(), "http"); final RestClientBuilder builder = RestClient.builder(host); @@ -66,27 +86,33 @@ public class OpensearchTestSupport extends CamelTestSupport { }); restClient = builder.build(); client = new OpenSearchClient(new RestClientTransport(restClient, new JacksonJsonpMapper())); + + // make use of the test method name to avoid collision + prefix = testInfo.getDisplayName().toLowerCase() + "-"; } - @Override - protected void cleanupResources() throws Exception { - super.cleanupResources(); + @AfterEach + public void afterEach() throws IOException { if (restClient != null) { restClient.close(); } } @Override - protected CamelContext createCamelContext() throws Exception { - final OpensearchComponent openSearchComponent = new OpensearchComponent(); - openSearchComponent.setHostAddresses(String.format("%s:%d", service.getOpenSearchHost(), service.getPort())); - openSearchComponent.setUser(service.getUsername()); - openSearchComponent.setPassword(service.getPassword()); + public CamelContextExtension getCamelContextExtension() { + return contextExtension; + } - CamelContext context = super.createCamelContext(); - context.addComponent("opensearch", openSearchComponent); + protected String getPrefix() { + return prefix; + } + + protected CamelContext camelContext() { + return getCamelContextExtension().getContext(); + } - return context; + protected ProducerTemplate template() { + return getCamelContextExtension().getProducerTemplate(); } /** @@ -94,7 +120,7 @@ public class OpensearchTestSupport extends CamelTestSupport { * slower), we need to make sure there's no side effect of the same used data through creating unique indexes. */ Map<String, String> createIndexedData(String... additionalPrefixes) { - String prefix = createPrefix(); + String prefix = getPrefix(); // take over any potential prefixes we may have been asked for if (additionalPrefixes.length > 0) { @@ -114,12 +140,30 @@ public class OpensearchTestSupport extends CamelTestSupport { return map; } - String createPrefix() { - // make use of the test method name to avoid collision - return getCurrentTestName().toLowerCase() + "-"; - } - RestClient getClient() { return restClient; } + + @ContextFixture + @Override + public void configureContext(CamelContext context) { + final OpensearchComponent openSearchComponent = new OpensearchComponent(); + openSearchComponent.setHostAddresses(String.format("%s:%d", service.getOpenSearchHost(), service.getPort())); + openSearchComponent.setUser(service.getUsername()); + openSearchComponent.setPassword(service.getPassword()); + + context.addComponent("opensearch", openSearchComponent); + } + + @RouteFixture + @Override + public void createRouteBuilder(CamelContext context) throws Exception { + final RouteBuilder routeBuilder = createRouteBuilder(); + + if (routeBuilder != null) { + context.addRoutes(routeBuilder); + } + } + + protected abstract RouteBuilder createRouteBuilder(); }