orpiske commented on code in PR #10549: URL: https://github.com/apache/camel/pull/10549#discussion_r1247473890
########## components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchTestSupport.java: ########## @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.opensearch.integration; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.component.opensearch.OpensearchComponent; +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.TestInstance; +import org.junit.jupiter.api.extension.RegisterExtension; +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.rest_client.RestClientTransport; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class OpensearchTestSupport extends CamelTestSupport { Review Comment: Just as a general note: I'd prefer if our new tests would stop using the `CamelTestSupport` in favor of the new JUnit 5 extension (simpler to use, maintain and debug) introduced on [CAMEL-18957](https://issues.apache.org/jira/browse/CAMEL-18957). But, for now, that's just a non-binding suggestion: it's OK to do it at a later moment. ########## components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchGetSearchDeleteExistsUpdateIT.java: ########## @@ -0,0 +1,913 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.opensearch.integration; + +import java.io.ByteArrayInputStream; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; +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; +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; +import org.opensearch.client.opensearch._types.query_dsl.MatchQuery; +import org.opensearch.client.opensearch._types.query_dsl.Query; +import org.opensearch.client.opensearch.core.DeleteRequest; +import org.opensearch.client.opensearch.core.GetRequest; +import org.opensearch.client.opensearch.core.GetResponse; +import org.opensearch.client.opensearch.core.IndexRequest; +import org.opensearch.client.opensearch.core.MsearchRequest; +import org.opensearch.client.opensearch.core.SearchRequest; +import org.opensearch.client.opensearch.core.mget.MultiGetResponseItem; +import org.opensearch.client.opensearch.core.msearch.MultiSearchResponseItem; +import org.opensearch.client.opensearch.core.msearch.MultisearchBody; +import org.opensearch.client.opensearch.core.msearch.MultisearchHeader; +import org.opensearch.client.opensearch.core.msearch.RequestItem; +import org.opensearch.client.opensearch.core.search.HitsMetadata; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { + + @Test + void testIndexWithMap() { + //first, Index a value + Map<String, String> map = createIndexedData(); + 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); + 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"); + String key = map.keySet().iterator().next(); + assertTrue(((ObjectNode) response.source()).has(key)); + assertEquals(map.get(key), ((ObjectNode) response.source()).get(key).asText()); + } + + @Test + void testIndexWithString() { + //first, Index a value + 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); + 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("testIndexWithString")); + assertEquals("some-value", ((ObjectNode) response.source()).get("testIndexWithString").asText()); + } + + @Test + void testIndexWithReader() { + //first, Index a 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); + 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("testIndexWithReader")); + assertEquals("some-value", ((ObjectNode) response.source()).get("testIndexWithReader").asText()); + } + + @Test + void testIndexWithBytes() { + //first, Index a value + 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); + 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("testIndexWithBytes")); + assertEquals("some-value", ((ObjectNode) response.source()).get("testIndexWithBytes").asText()); + } + + @Test + void testIndexWithInputStream() { + //first, Index a value + 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); + 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("testIndexWithInputStream")); + assertEquals("some-value", ((ObjectNode) response.source()).get("testIndexWithInputStream").asText()); + } + + @Test + void testIndexWithDocumentType() { + Product product = new Product(); + product.setId("book-world-records-2021"); + product.setStockAvailable(1); + product.setPrice(100); + product.setDescription("The book of the year!"); + product.setName("Guinness book of records 2021"); + + //first, Index a value + 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, + OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class, GetResponse.class); + assertNotNull(response, "response should not be null"); + assertNotNull(response.source(), "response source should not be null"); + assertInstanceOf(Product.class, response.source(), "response source should be a Product"); + Product actual = (Product) response.source(); + assertNotSame(product, actual); + assertEquals(product, actual); + } + + @Test + void testGetWithString() { + //first, Index a value + Map<String, String> map = createIndexedData(); + 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); + assertNotNull(response, "response should not be null"); + assertNotNull(response.source(), "response source should not be null"); + assertInstanceOf(ObjectNode.class, response.source()); + } + + @Test + void testGetWithDocumentType() { + //first, Index a value + Product product = new Product(); + product.setId("book-world-records-1890"); + product.setStockAvailable(0); + product.setPrice(200); + product.setDescription("The book of the year!"); + product.setName("Guinness book of records 1890"); + + String indexId = template.requestBody("direct:index", product, String.class); + assertNotNull(indexId, "indexId should be set"); + + //now, verify GET succeeded + 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"); + assertInstanceOf(Product.class, response.source()); + Product p = (Product) response.source(); + assertEquals(product, p); + } + + @Test + void testMGetWithString() { + //first, Index a value + Map<String, String> map = createIndexedData(); + 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); + assertNotNull(response, "response should not be null"); + assertEquals(1, response.size(), "response should contain one result"); + assertTrue(response.get(0).isResult()); + assertNotNull(response.get(0).result().source(), "response source should not be null"); + assertInstanceOf(ObjectNode.class, response.get(0).result().source()); + } + + @Test + void testMGetWithDocumentType() { + //first, Index a value + Product product = new Product(); + product.setId("book-world-records-1890"); + product.setStockAvailable(0); + product.setPrice(200); + product.setDescription("The book of the year!"); + product.setName("Guinness book of records 1890"); + + 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( + "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"); + assertTrue(response.get(0).isResult()); + assertNotNull(response.get(0).result().source(), "response source should not be null"); + assertInstanceOf(Product.class, response.get(0).result().source()); + Product p = (Product) response.get(0).result().source(); + assertEquals(product, p); + } + + @Test + void testDeleteWithString() { + //first, Index a value + Map<String, String> map = createIndexedData(); + 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); + 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); + assertNotNull(deleteResponse, "response should not be null"); + + //now, verify GET fails to find the indexed value + 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() { + //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, + String.class); + + // No match + Map<String, Object> actualQuery = new HashMap<>(); + actualQuery.put("doc.testSearchWithMapQuery1", "bar"); + Map<String, Object> match = new HashMap<>(); + match.put("match", actualQuery); + Map<String, Object> query = new HashMap<>(); + query.put("query", match); + 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()); + }); Review Comment: I think it would be better to split this so that `untilAsserted` tests for only 1 assertion. Combining them usually makes it harder to identify the root cause of failures. ########## components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/integration/OpensearchGetSearchDeleteExistsUpdateIT.java: ########## @@ -0,0 +1,913 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.opensearch.integration; + +import java.io.ByteArrayInputStream; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; +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; +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; +import org.opensearch.client.opensearch._types.query_dsl.MatchQuery; +import org.opensearch.client.opensearch._types.query_dsl.Query; +import org.opensearch.client.opensearch.core.DeleteRequest; +import org.opensearch.client.opensearch.core.GetRequest; +import org.opensearch.client.opensearch.core.GetResponse; +import org.opensearch.client.opensearch.core.IndexRequest; +import org.opensearch.client.opensearch.core.MsearchRequest; +import org.opensearch.client.opensearch.core.SearchRequest; +import org.opensearch.client.opensearch.core.mget.MultiGetResponseItem; +import org.opensearch.client.opensearch.core.msearch.MultiSearchResponseItem; +import org.opensearch.client.opensearch.core.msearch.MultisearchBody; +import org.opensearch.client.opensearch.core.msearch.MultisearchHeader; +import org.opensearch.client.opensearch.core.msearch.RequestItem; +import org.opensearch.client.opensearch.core.search.HitsMetadata; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class OpensearchGetSearchDeleteExistsUpdateIT extends OpensearchTestSupport { + + @Test + void testIndexWithMap() { + //first, Index a value + Map<String, String> map = createIndexedData(); + 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); + 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"); + String key = map.keySet().iterator().next(); + assertTrue(((ObjectNode) response.source()).has(key)); + assertEquals(map.get(key), ((ObjectNode) response.source()).get(key).asText()); + } + + @Test + void testIndexWithString() { + //first, Index a value + 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); + 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("testIndexWithString")); + assertEquals("some-value", ((ObjectNode) response.source()).get("testIndexWithString").asText()); + } + + @Test + void testIndexWithReader() { + //first, Index a 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); + 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("testIndexWithReader")); + assertEquals("some-value", ((ObjectNode) response.source()).get("testIndexWithReader").asText()); + } + + @Test + void testIndexWithBytes() { + //first, Index a value + 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); + 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("testIndexWithBytes")); + assertEquals("some-value", ((ObjectNode) response.source()).get("testIndexWithBytes").asText()); + } + + @Test + void testIndexWithInputStream() { + //first, Index a value + 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); + 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("testIndexWithInputStream")); + assertEquals("some-value", ((ObjectNode) response.source()).get("testIndexWithInputStream").asText()); + } + + @Test + void testIndexWithDocumentType() { + Product product = new Product(); + product.setId("book-world-records-2021"); + product.setStockAvailable(1); + product.setPrice(100); + product.setDescription("The book of the year!"); + product.setName("Guinness book of records 2021"); + + //first, Index a value + 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, + OpensearchConstants.PARAM_DOCUMENT_CLASS, Product.class, GetResponse.class); + assertNotNull(response, "response should not be null"); + assertNotNull(response.source(), "response source should not be null"); + assertInstanceOf(Product.class, response.source(), "response source should be a Product"); + Product actual = (Product) response.source(); + assertNotSame(product, actual); + assertEquals(product, actual); + } + + @Test + void testGetWithString() { + //first, Index a value + Map<String, String> map = createIndexedData(); + 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); + assertNotNull(response, "response should not be null"); + assertNotNull(response.source(), "response source should not be null"); + assertInstanceOf(ObjectNode.class, response.source()); + } + + @Test + void testGetWithDocumentType() { + //first, Index a value + Product product = new Product(); + product.setId("book-world-records-1890"); + product.setStockAvailable(0); + product.setPrice(200); + product.setDescription("The book of the year!"); + product.setName("Guinness book of records 1890"); + + String indexId = template.requestBody("direct:index", product, String.class); + assertNotNull(indexId, "indexId should be set"); + + //now, verify GET succeeded + 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"); + assertInstanceOf(Product.class, response.source()); + Product p = (Product) response.source(); + assertEquals(product, p); + } + + @Test + void testMGetWithString() { + //first, Index a value + Map<String, String> map = createIndexedData(); + 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); + assertNotNull(response, "response should not be null"); + assertEquals(1, response.size(), "response should contain one result"); + assertTrue(response.get(0).isResult()); + assertNotNull(response.get(0).result().source(), "response source should not be null"); + assertInstanceOf(ObjectNode.class, response.get(0).result().source()); + } + + @Test + void testMGetWithDocumentType() { + //first, Index a value + Product product = new Product(); + product.setId("book-world-records-1890"); + product.setStockAvailable(0); + product.setPrice(200); + product.setDescription("The book of the year!"); + product.setName("Guinness book of records 1890"); + + 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( + "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"); + assertTrue(response.get(0).isResult()); + assertNotNull(response.get(0).result().source(), "response source should not be null"); + assertInstanceOf(Product.class, response.get(0).result().source()); + Product p = (Product) response.get(0).result().source(); + assertEquals(product, p); + } + + @Test + void testDeleteWithString() { + //first, Index a value + Map<String, String> map = createIndexedData(); + 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); + 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); + assertNotNull(deleteResponse, "response should not be null"); + + //now, verify GET fails to find the indexed value + 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() { + //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, + String.class); + + // No match + Map<String, Object> actualQuery = new HashMap<>(); + actualQuery.put("doc.testSearchWithMapQuery1", "bar"); + Map<String, Object> match = new HashMap<>(); + match.put("match", actualQuery); + Map<String, Object> query = new HashMap<>(); + query.put("query", match); + 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()); + }); + } + + @Test + void testSearchWithStringQuery() { + //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, + String.class); + + // No match + String query = """ + { + "query" : { "match" : { "doc.testSearchWithStringQuery1" : "bar" }} + } + """; + + 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 + String q = """ + { + "query" : { "match" : { "doc.testSearchWithStringQuery1" : "foo" }} + } + """; + // the result may see stale data so use Awaitility + Awaitility.await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { Review Comment: I think it would be better to split this so that `untilAsserted` tests for only 1 assertion. Combining them usually makes it harder to identify the root cause of failures. The same note for all the others using the same pattern. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org