From 56660bd3726a43cef41266c10217db9bb3c684f0 Mon Sep 17 00:00:00 2001 From: sshre31 Date: Tue, 21 Jan 2025 10:57:50 -0500 Subject: [PATCH 1/9] feature(elastic-client): Add config for elastic client. --- client/pom.xml | 1 + .../synapse-client-elastic-search/README.md | 5 ++ client/synapse-client-elastic-search/pom.xml | 80 +++++++++++++++++++ .../config/BaseClientConfig.java | 67 ++++++++++++++++ pom.xml | 1 + 5 files changed, 154 insertions(+) create mode 100644 client/synapse-client-elastic-search/README.md create mode 100644 client/synapse-client-elastic-search/pom.xml create mode 100644 client/synapse-client-elastic-search/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseClientConfig.java diff --git a/client/pom.xml b/client/pom.xml index c1b89d5b0..636e577a9 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -27,6 +27,7 @@ client-samples + synapse-client-elastic-search synapse-client-graphql synapse-client-rest synapse-client-soap diff --git a/client/synapse-client-elastic-search/README.md b/client/synapse-client-elastic-search/README.md new file mode 100644 index 000000000..6a4e91225 --- /dev/null +++ b/client/synapse-client-elastic-search/README.md @@ -0,0 +1,5 @@ +# synapse-client-rest + +## Description + +## Usage diff --git a/client/synapse-client-elastic-search/pom.xml b/client/synapse-client-elastic-search/pom.xml new file mode 100644 index 000000000..9bf84600c --- /dev/null +++ b/client/synapse-client-elastic-search/pom.xml @@ -0,0 +1,80 @@ + + + + io.americanexpress.synapse + synapse + 0.4.0-SNAPSHOT + + + 4.0.0 + synapse-client-elastic-search + + + + + + + io.americanexpress.synapse + synapse-framework-exception + + + io.americanexpress.synapse + synapse-framework-test + + + io.americanexpress.synapse + synapse-utilities-common + + + + + + org.slf4j + slf4j-ext + + + org.springframework.boot + spring-boot-starter-webflux + + + co.elastic.clients + elasticsearch-java + + + + + + + maven-checkstyle-plugin + + + maven-compiler-plugin + + + maven-dependency-plugin + + + maven-jar-plugin + + + maven-pmd-plugin + + + maven-surefire-plugin + + + + + + + + maven-checkstyle-plugin + + + maven-pmd-plugin + + + + diff --git a/client/synapse-client-elastic-search/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseClientConfig.java b/client/synapse-client-elastic-search/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseClientConfig.java new file mode 100644 index 000000000..24ec61211 --- /dev/null +++ b/client/synapse-client-elastic-search/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseClientConfig.java @@ -0,0 +1,67 @@ +package io.americanexpress.synapse.client.elasticsearch.config; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.json.jackson.JacksonJsonpMapper; +import co.elastic.clients.transport.rest_client.RestClientTransport; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.americanexpress.synapse.framework.exception.config.ExceptionConfig; +import io.americanexpress.synapse.utilities.common.config.UtilitiesCommonConfig; +import org.apache.http.Header; +import org.apache.http.HttpHost; +import org.apache.http.message.BasicHeader; +import org.elasticsearch.client.RestClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.env.Environment; +import static org.springframework.http.HttpHeaders.AUTHORIZATION; + +/** + * {@code BaseClientConfig} + * + * @author sshre31 + */ +@Configuration +@ComponentScan(basePackages = "io.americanexpress.synapse.client.elasticsearch") +@Import({ExceptionConfig.class, UtilitiesCommonConfig.class}) +public class BaseClientConfig { + + /** + * Default object mapper. + */ + private final ObjectMapper defaultObjectMapper; + + /** + * Constructor taking in objectMapper & metricInterceptor. + * + * @param defaultObjectMapper the default object mapper + */ + public BaseClientConfig(ObjectMapper defaultObjectMapper) { + this.defaultObjectMapper = defaultObjectMapper; + } + + /** + * Creates an instance of {@link ElasticsearchClient} to interact with the ElasticSearch cluster. + * + * @return the {@link ElasticsearchClient} instance. + */ + @Bean + public ElasticsearchClient elasticsearchClient(@Autowired Environment environment) { + var elasticSearchUrl = environment.getRequiredProperty("elastic-client.url"); + var elasticSearchApiKey = environment.getRequiredProperty("elastic-client.apikey"); + + var restClient = RestClient + .builder(HttpHost.create(elasticSearchUrl)) + .setDefaultHeaders(new Header[]{ + new BasicHeader(AUTHORIZATION, "ApiKey " + elasticSearchApiKey) + }) + .build(); + + var transport = new RestClientTransport( + restClient, new JacksonJsonpMapper(defaultObjectMapper)); + + return new ElasticsearchClient(transport); + } +} diff --git a/pom.xml b/pom.xml index 5293c4907..c6f6df53d 100644 --- a/pom.xml +++ b/pom.xml @@ -96,6 +96,7 @@ publisher subscriber utility + client/synapse-client-elastic-search From d0893984c296ba33c23389bad6e71f62248b5b5f Mon Sep 17 00:00:00 2001 From: sshre31 Date: Wed, 22 Jan 2025 10:55:12 -0500 Subject: [PATCH 2/9] elastic-search-client: add sample test code --- .github/workflows/codeql-analysis.yml | 6 +- client/client-samples/pom.xml | 1 + .../pom.xml | 26 +++++ ...ateProductElasticSearchDocumentClient.java | 29 ++++++ ...eteProductElasticSearchDocumentClient.java | 30 ++++++ ...eadProductElasticSearchDocumentClient.java | 30 ++++++ ...ateProductElasticSearchDocumentClient.java | 28 ++++++ .../config/ProductElasticSearchConfig.java | 26 +++++ .../elasticsearch/product/model/Product.java | 95 +++++++++++++++++++ .../CreateProductElasticSearchDocumentIT.java | 33 +++++++ .../ProductElasticSearchConfigTest.java | 16 ++++ client/pom.xml | 2 +- .../README.md | 0 .../pom.xml | 2 +- ...BaseCreateElasticSearchDocumentClient.java | 39 ++++++++ ...BaseDeleteElasticSearchDocumentClient.java | 37 ++++++++ .../client/BaseElasticClient.java | 39 ++++++++ .../BaseReadElasticSearchDocumentClient.java | 62 ++++++++++++ ...BaseUpdateElasticSearchDocumentClient.java | 39 ++++++++ .../BaseElasticSearchClientConfig.java} | 12 ++- .../data/book/repository/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../book/service/BookPersistenceService.java | 4 +- .../data/book/dao/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../function/book/function/BookFunction.java | 2 +- pom.xml | 6 +- .../book/rest/service/CreateBookService.java | 4 +- .../book/rest/service/DeleteBookService.java | 4 +- .../book/rest/service/ReadBookService.java | 4 +- .../book/rest/service/UpdateBookService.java | 4 +- .../service/CreateBookReactiveService.java | 2 +- .../service/DeleteBookReactiveService.java | 2 +- .../rest/service/GetBookReactiveService.java | 2 +- .../rest/service/ReadBookReactiveService.java | 2 +- .../service/UpdateBookReactiveService.java | 2 +- .../service/CreateBookReactiveService.java | 2 +- .../service/DeleteBookReactiveService.java | 2 +- .../service/ReadPolyBookReactiveService.java | 2 +- .../service/UpdateBookReactiveService.java | 2 +- .../book/rest/service/CreateBookService.java | 2 +- .../book/rest/service/DeleteBookService.java | 2 +- .../book/rest/service/GetBookService.java | 2 +- .../book/rest/service/ReadBookService.java | 2 +- .../book/rest/service/UpdateBookService.java | 2 +- .../book/rest/config/BookTestConfig.java | 2 +- .../book/rest/service/CreateBookService.java | 2 +- .../book/rest/service/DeleteBookService.java | 2 +- .../book/rest/service/ReadBookService.java | 2 +- 52 files changed, 583 insertions(+), 47 deletions(-) create mode 100644 client/client-samples/sample-client-elasticsearch-product/pom.xml create mode 100644 client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/CreateProductElasticSearchDocumentClient.java create mode 100644 client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/DeleteProductElasticSearchDocumentClient.java create mode 100644 client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/ReadProductElasticSearchDocumentClient.java create mode 100644 client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/UpdateProductElasticSearchDocumentClient.java create mode 100644 client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/config/ProductElasticSearchConfig.java create mode 100644 client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/model/Product.java create mode 100644 client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/CreateProductElasticSearchDocumentIT.java create mode 100644 client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/config/ProductElasticSearchConfigTest.java rename client/{synapse-client-elastic-search => synapse-client-elasticsearch}/README.md (100%) rename client/{synapse-client-elastic-search => synapse-client-elasticsearch}/pom.xml (97%) create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentClient.java rename client/{synapse-client-elastic-search/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseClientConfig.java => synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java} (85%) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 25de84aeb..ca4ba20cd 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -1,11 +1,11 @@ # For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. +# to commit it to your client. # # You may wish to alter this file to override the set of languages analyzed, # or to provide custom queries or build logic. # # ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check +# We have attempted to detect the languages in your client. Please check # the `language` matrix defined below to confirm you have the correct set of # supported CodeQL languages. # @@ -38,7 +38,7 @@ jobs: # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed steps: - - name: Checkout repository + - name: Checkout client uses: actions/checkout@v2 # Initializes the CodeQL tools for scanning. diff --git a/client/client-samples/pom.xml b/client/client-samples/pom.xml index f0dda631a..f68a765f3 100644 --- a/client/client-samples/pom.xml +++ b/client/client-samples/pom.xml @@ -26,6 +26,7 @@ + sample-client-elasticsearch-product sample-client-graphql-book sample-client-reactive-football sample-client-reactive-weather diff --git a/client/client-samples/sample-client-elasticsearch-product/pom.xml b/client/client-samples/sample-client-elasticsearch-product/pom.xml new file mode 100644 index 000000000..3ed07bea0 --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + io.americanexpress.synapse + client-samples + 0.4.0-SNAPSHOT + + + sample-client-elasticsearch-product + + + + io.americanexpress.synapse + synapse-client-elasticsearch + + + io.americanexpress.synapse + synapse-client-test + + + + + diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/CreateProductElasticSearchDocumentClient.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/CreateProductElasticSearchDocumentClient.java new file mode 100644 index 000000000..c6f001585 --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/CreateProductElasticSearchDocumentClient.java @@ -0,0 +1,29 @@ +package io.americanexpress.sample.client.elasticsearch.product.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import io.americanexpress.sample.client.elasticsearch.product.model.Product; +import io.americanexpress.synapse.client.elasticsearch.client.BaseCreateElasticSearchDocumentClient; +import org.springframework.stereotype.Component; + +/** + * {@code CreateProductElasticSearchDocumentClient} creates a document in ElasticSearch. + * + * @author sshre31 + */ +@Component +public class CreateProductElasticSearchDocumentClient extends BaseCreateElasticSearchDocumentClient { + + /** + * Index name. + */ + private static final String INDEX_NAME = "product"; + + /** + * Create an instance of CreateProductElasticSearchDocumentClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + */ + public CreateProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient) { + super(elasticsearchClient, INDEX_NAME, Product.class); + } +} diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/DeleteProductElasticSearchDocumentClient.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/DeleteProductElasticSearchDocumentClient.java new file mode 100644 index 000000000..1a54feac3 --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/DeleteProductElasticSearchDocumentClient.java @@ -0,0 +1,30 @@ +package io.americanexpress.sample.client.elasticsearch.product.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import io.americanexpress.sample.client.elasticsearch.product.model.Product; +import io.americanexpress.synapse.client.elasticsearch.client.BaseDeleteElasticSearchDocumentClient; +import org.springframework.stereotype.Component; + +/** + * {@code DeleteProductElasticSearchDocumentClient} deletes a document from ElasticSearch. + * + * @author sshre31 + */ +@Component +public class DeleteProductElasticSearchDocumentClient extends BaseDeleteElasticSearchDocumentClient { + + /** + * Index name. + */ + private static final String INDEX_NAME = "product"; + + /** + * Create an instance of BaseDeleteElasticSearchDocumentClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param clazz clazz + */ + public DeleteProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, Class clazz) { + super(elasticsearchClient, INDEX_NAME, clazz); + } +} diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/ReadProductElasticSearchDocumentClient.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/ReadProductElasticSearchDocumentClient.java new file mode 100644 index 000000000..cf39f6896 --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/ReadProductElasticSearchDocumentClient.java @@ -0,0 +1,30 @@ +package io.americanexpress.sample.client.elasticsearch.product.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import io.americanexpress.sample.client.elasticsearch.product.model.Product; +import io.americanexpress.synapse.client.elasticsearch.client.BaseReadElasticSearchDocumentClient; +import org.springframework.stereotype.Component; + +/** + * {@code ReadProductElasticSearchDocumentClient} reads a document from ElasticSearch. + * + * @author sshre31 + */ +@Component +public class ReadProductElasticSearchDocumentClient extends BaseReadElasticSearchDocumentClient { + + /** + * Index name. + */ + private static final String INDEX_NAME = "product"; + + /** + * Create an instance of BaseReadElasticSearchDocumentClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param clazz clazz + */ + public ReadProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, Class clazz) { + super(elasticsearchClient, INDEX_NAME, clazz); + } +} diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/UpdateProductElasticSearchDocumentClient.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/UpdateProductElasticSearchDocumentClient.java new file mode 100644 index 000000000..99f626cf8 --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/UpdateProductElasticSearchDocumentClient.java @@ -0,0 +1,28 @@ +package io.americanexpress.sample.client.elasticsearch.product.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import io.americanexpress.sample.client.elasticsearch.product.model.Product; +import io.americanexpress.synapse.client.elasticsearch.client.BaseUpdateElasticSearchDocumentClient; + +/** + * {@code UpdateProductElasticSearchDocumentClient} + * + * @author sshre31 + */ +public class UpdateProductElasticSearchDocumentClient extends BaseUpdateElasticSearchDocumentClient { + + /** + * Index name. + */ + private static final String INDEX_NAME = "product"; + + /** + * Create an instance of BaseUpdateElasticSearchDocumentClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param clazz clazz + */ + public UpdateProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, Class clazz) { + super(elasticsearchClient, INDEX_NAME, clazz); + } +} diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/config/ProductElasticSearchConfig.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/config/ProductElasticSearchConfig.java new file mode 100644 index 000000000..ab535351a --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/config/ProductElasticSearchConfig.java @@ -0,0 +1,26 @@ +package io.americanexpress.sample.client.elasticsearch.product.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.americanexpress.synapse.client.elasticsearch.config.BaseElasticSearchClientConfig; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; + +/** + * {@code ProductElasticSearchConfig} specifies the configuration for the ElasticSearch client. + * + * @author sshre31 + */ +@ComponentScan("io.americanexpress.sample.client.elasticsearch.product") +@Configuration +public class ProductElasticSearchConfig extends BaseElasticSearchClientConfig { + + /** + * Constructor taking in objectMapper & metricInterceptor. + * + * @param defaultObjectMapper the default object mapper + */ + public ProductElasticSearchConfig(ObjectMapper defaultObjectMapper, Environment environment) { + super(defaultObjectMapper, environment); + } +} diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/model/Product.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/model/Product.java new file mode 100644 index 000000000..1743ea90c --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/model/Product.java @@ -0,0 +1,95 @@ +package io.americanexpress.sample.client.elasticsearch.product.model; + +import java.util.List; +import java.util.UUID; + +/** + * {@code Product} represents a product. + * + * @author sshre31 + */ +public class Product { + + /** + * ID of this product. + */ + private UUID id; + + /** + * Name of the product. + */ + private String name; + + /** + * Description of the product. + */ + private String description; + + /** + * Test of the product. + */ + private List test; + + /** + * Default constructor creates a new instance of Product with default values. + */ + public Product(UUID id, String name, String description, List test) { + this.id = id; + this.name = name; + this.description = description; + } + + /** + * Get the id. + * + * @return the id + */ + public UUID getId() { + return id; + } + + /** + * Set the id. + * + * @param id the id to set + */ + public void setId(UUID id) { + this.id = id; + } + + /** + * Get the name. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Set the name. + * + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * Get the description. + * + * @return description + */ + public String getDescription() { + return description; + } + + /** + * Set the description. + * + * @param description the description to set + */ + public void setDescription(String description) { + this.description = description; + } +} diff --git a/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/CreateProductElasticSearchDocumentIT.java b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/CreateProductElasticSearchDocumentIT.java new file mode 100644 index 000000000..aa5aa1f8e --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/CreateProductElasticSearchDocumentIT.java @@ -0,0 +1,33 @@ +package io.americanexpress.sample.client.elasticsearch.client; + +import io.americanexpress.sample.client.elasticsearch.config.ProductElasticSearchConfigTest; +import io.americanexpress.sample.client.elasticsearch.product.client.CreateProductElasticSearchDocumentClient; +import io.americanexpress.sample.client.elasticsearch.product.model.Product; +import java.io.IOException; +import java.util.Collections; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * {@code CreateProductElasticSearchDocumentIT} + * + * @author sshre31 + */ +@ContextConfiguration(classes = ProductElasticSearchConfigTest.class) +@ExtendWith(SpringExtension.class) +public class CreateProductElasticSearchDocumentIT { + + @Autowired + CreateProductElasticSearchDocumentClient createProductElasticSearchDocument; + + @Test + void save_providedValidProduct_expectedSuccess() throws IOException { + var product = new Product(UUID.randomUUID(), "Product 1", "Product 1 description", Collections.emptyList()); + + createProductElasticSearchDocument.save(product.getId().toString(), product); + } +} diff --git a/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/config/ProductElasticSearchConfigTest.java b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/config/ProductElasticSearchConfigTest.java new file mode 100644 index 000000000..5dacb3c05 --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/config/ProductElasticSearchConfigTest.java @@ -0,0 +1,16 @@ +package io.americanexpress.sample.client.elasticsearch.config; + +import io.americanexpress.sample.client.elasticsearch.product.config.ProductElasticSearchConfig; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * {@code ProductElasticSearchConfigTest} + * + * @author sshre31 + */ +@Configuration +@Import(ProductElasticSearchConfig.class) +public class ProductElasticSearchConfigTest { + +} diff --git a/client/pom.xml b/client/pom.xml index 636e577a9..197e7f4dc 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -27,7 +27,7 @@ client-samples - synapse-client-elastic-search + synapse-client-elasticsearch synapse-client-graphql synapse-client-rest synapse-client-soap diff --git a/client/synapse-client-elastic-search/README.md b/client/synapse-client-elasticsearch/README.md similarity index 100% rename from client/synapse-client-elastic-search/README.md rename to client/synapse-client-elasticsearch/README.md diff --git a/client/synapse-client-elastic-search/pom.xml b/client/synapse-client-elasticsearch/pom.xml similarity index 97% rename from client/synapse-client-elastic-search/pom.xml rename to client/synapse-client-elasticsearch/pom.xml index 9bf84600c..056dcde38 100644 --- a/client/synapse-client-elastic-search/pom.xml +++ b/client/synapse-client-elasticsearch/pom.xml @@ -9,7 +9,7 @@ 4.0.0 - synapse-client-elastic-search + synapse-client-elasticsearch diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentClient.java new file mode 100644 index 000000000..588d81da6 --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentClient.java @@ -0,0 +1,39 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import java.io.IOException; + +/** + * {@code BaseCreateElasticSearchDocumentClient} creates a document in ElasticSearch. + * + * @author sshre31 + */ +public abstract class BaseCreateElasticSearchDocumentClient extends BaseElasticClient { + + /** + * Create an instance of BaseCreateElasticSearchDocumentClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + * @param clazz clazz + */ + public BaseCreateElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, + String indexName, Class clazz) { + super(elasticsearchClient, indexName, clazz); + } + + /** + * Save the document in ElasticSearch. + * + * @param id the identifier + * @param document the document to save + * @throws IOException if an error occurs while saving the document + */ + public void save(String id, T document) throws IOException { + this.client.index(i -> i + .index(this.indexName) + .id(id) + .document(document) + ); + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentClient.java new file mode 100644 index 000000000..947f4465a --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentClient.java @@ -0,0 +1,37 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import java.io.IOException; + +/** + * {@code BaseDeleteElasticSearchDocumentClient} deletes a document in ElasticSearch. + * + * @author sshre31 + */ +public abstract class BaseDeleteElasticSearchDocumentClient extends BaseElasticClient { + + /** + * Create an instance of BaseDeleteElasticSearchDocumentClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + * @param clazz clazz + */ + public BaseDeleteElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, String indexName, + Class clazz) { + super(elasticsearchClient, indexName, clazz); + } + + /** + * Delete the document in ElasticSearch. + * + * @param id the identifier + * @throws IOException if an error occurs while deleting the document + */ + public void deleteById(String id) throws IOException { + this.client.delete(d -> d + .index(this.indexName) + .id(id) + ); + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticClient.java new file mode 100644 index 000000000..d393cace5 --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticClient.java @@ -0,0 +1,39 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; + +/** + * {@code BaseElasticClient} is the base class for all ElasticSearch clients. + * + * @author sshre31 + */ +public abstract class BaseElasticClient { + + /** + * Elasticsearch client. + */ + protected final ElasticsearchClient client; + + /** + * Index name. + */ + protected final String indexName; + + /** + * Class of the document. + */ + protected final Class clazz; + + /** + * Create an instance of BaseElasticClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + * @param clazz clazz + */ + public BaseElasticClient(ElasticsearchClient elasticsearchClient, String indexName, Class clazz) { + this.client = elasticsearchClient; + this.indexName = indexName; + this.clazz = clazz; + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentClient.java new file mode 100644 index 000000000..6a909579a --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentClient.java @@ -0,0 +1,62 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.elasticsearch.core.GetResponse; +import co.elastic.clients.elasticsearch.core.SearchResponse; +import co.elastic.clients.elasticsearch.core.search.Hit; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * {@code BaseReadElasticSearchDocumentClient} reads a document in ElasticSearch. + * + * @author sshre31 + */ +public abstract class BaseReadElasticSearchDocumentClient extends BaseElasticClient { + + /** + * Create an instance of BaseReadElasticSearchDocumentClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + * @param clazz clazz + */ + public BaseReadElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, String indexName, + Class clazz) { + super(elasticsearchClient, indexName, clazz); + } + + /** + * Find the document by the identifier. + * + * @param id the identifier + * @return the document + * @throws IOException if an error occurs while finding the document + */ + public T findById(String id) throws IOException { + GetResponse response = client.get(g -> g + .index(indexName) + .id(id), clazz + ); + return response.found() ? response.source() : null; + } + + /** + * Find all the documents. + * + * @return the list of documents + * @throws IOException if an error occurs while finding the documents + */ + public List findAll() throws IOException { + SearchResponse response = client.search(s -> s + .index(indexName) + .query(q -> q.matchAll(m -> m)), clazz + ); + List results = new ArrayList<>(); + for (Hit hit : response.hits().hits()) { + results.add(hit.source()); + } + return results; + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentClient.java new file mode 100644 index 000000000..145d6b3fe --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentClient.java @@ -0,0 +1,39 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import java.io.IOException; + +/** + * {@code BaseUpdateElasticSearchDocumentClient} + * + * @author sshre31 + */ +public abstract class BaseUpdateElasticSearchDocumentClient extends BaseElasticClient { + + /** + * Create an instance of BaseUpdateElasticSearchDocumentClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + * @param clazz clazz + */ + public BaseUpdateElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, String indexName, + Class clazz) { + super(elasticsearchClient, indexName, clazz); + } + + /** + * Update the document in ElasticSearch. + * + * @param id the identifier + * @param document the document to update + * @throws IOException if an error occurs while updating the document + */ + public void update(String id, T document) throws IOException { + client.update(u -> u + .index(indexName) + .id(id) + .doc(document), clazz + ); + } +} diff --git a/client/synapse-client-elastic-search/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseClientConfig.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java similarity index 85% rename from client/synapse-client-elastic-search/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseClientConfig.java rename to client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java index 24ec61211..9c25b42ce 100644 --- a/client/synapse-client-elastic-search/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseClientConfig.java +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java @@ -10,7 +10,6 @@ import org.apache.http.HttpHost; import org.apache.http.message.BasicHeader; import org.elasticsearch.client.RestClient; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -19,27 +18,30 @@ import static org.springframework.http.HttpHeaders.AUTHORIZATION; /** - * {@code BaseClientConfig} + * {@code BaseElasticSearchClientConfig} specifies the base configuration for the ElasticSearch client. * * @author sshre31 */ @Configuration @ComponentScan(basePackages = "io.americanexpress.synapse.client.elasticsearch") @Import({ExceptionConfig.class, UtilitiesCommonConfig.class}) -public class BaseClientConfig { +public class BaseElasticSearchClientConfig { /** * Default object mapper. */ private final ObjectMapper defaultObjectMapper; + private final Environment environment; + /** * Constructor taking in objectMapper & metricInterceptor. * * @param defaultObjectMapper the default object mapper */ - public BaseClientConfig(ObjectMapper defaultObjectMapper) { + public BaseElasticSearchClientConfig(ObjectMapper defaultObjectMapper, Environment environment) { this.defaultObjectMapper = defaultObjectMapper; + this.environment = environment; } /** @@ -48,7 +50,7 @@ public BaseClientConfig(ObjectMapper defaultObjectMapper) { * @return the {@link ElasticsearchClient} instance. */ @Bean - public ElasticsearchClient elasticsearchClient(@Autowired Environment environment) { + public ElasticsearchClient elasticsearchClient() { var elasticSearchUrl = environment.getRequiredProperty("elastic-client.url"); var elasticSearchApiKey = environment.getRequiredProperty("elastic-client.apikey"); diff --git a/data/data-samples/sample-data-cassandra-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-cassandra-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index e0d3a34fe..0f6383587 100644 --- a/data/data-samples/sample-data-cassandra-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-cassandra-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -22,7 +22,7 @@ import java.util.UUID; /** - * {@code BookRepository} is the dao repository to handle the queries for the books table. + * {@code BookRepository} is the dao client to handle the queries for the books table. */ @Repository public interface BookRepository extends CassandraRepository { diff --git a/data/data-samples/sample-data-cassandra-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-cassandra-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 3e945ad39..738f97607 100644 --- a/data/data-samples/sample-data-cassandra-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-cassandra-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -22,7 +22,7 @@ import java.util.UUID; /** - * {@code BookRepository} is the dao repository to handle the queries for the books table. + * {@code BookRepository} is the dao client to handle the queries for the books table. */ @Repository public interface BookRepository extends ReactiveCassandraRepository { diff --git a/data/data-samples/sample-data-db2-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-db2-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 2dcf3cdbc..22381f2f4 100644 --- a/data/data-samples/sample-data-db2-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-db2-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -18,7 +18,7 @@ import org.springframework.stereotype.Repository; /** - * {@code BookRepository} is the dao repository to handle the queries for the books table. + * {@code BookRepository} is the dao client to handle the queries for the books table. * @author tisla4 */ @Repository diff --git a/data/data-samples/sample-data-mongodb-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-mongodb-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 5af810536..f1b28adad 100644 --- a/data/data-samples/sample-data-mongodb-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-mongodb-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -18,7 +18,7 @@ import org.springframework.stereotype.Repository; /** - * {@code BookRepository} is the dao repository to handle the queries for the books table. + * {@code BookRepository} is the dao client to handle the queries for the books table. */ @Repository public interface BookRepository extends MongoRepository { diff --git a/data/data-samples/sample-data-mongodb-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-mongodb-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 2e2d3d730..b6f4ea3eb 100644 --- a/data/data-samples/sample-data-mongodb-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-mongodb-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -20,7 +20,7 @@ import reactor.core.publisher.Mono; /** - * {@code BookRepository} is the dao repository to handle the queries for the books table. + * {@code BookRepository} is the dao client to handle the queries for the books table. */ @Repository public interface BookRepository extends ReactiveMongoRepository { diff --git a/data/data-samples/sample-data-oracle-reactive-cp-book/src/main/java/io/americanexpress/data/oracle/cp/book/service/BookPersistenceService.java b/data/data-samples/sample-data-oracle-reactive-cp-book/src/main/java/io/americanexpress/data/oracle/cp/book/service/BookPersistenceService.java index d842e2394..d8f328e83 100644 --- a/data/data-samples/sample-data-oracle-reactive-cp-book/src/main/java/io/americanexpress/data/oracle/cp/book/service/BookPersistenceService.java +++ b/data/data-samples/sample-data-oracle-reactive-cp-book/src/main/java/io/americanexpress/data/oracle/cp/book/service/BookPersistenceService.java @@ -25,7 +25,7 @@ * {@code BookPersistenceService} A persistence service layer for managing data using * a connection pool for efficient database interactions. * The service encapsulates methods to perform all CRUD (Create, Read, Update, & Delete) operations on - * {@link BookEntity} repository which manges the lifecycle of the underlying database connections. + * {@link BookEntity} client which manges the lifecycle of the underlying database connections. * It ensures that connections are acquired, used, and released correctly using a connection pool, * allowing for better performance and resource management. */ @@ -61,7 +61,7 @@ public Mono executeFindByTitleAndAuthor(String title, String author) /** * Retrieves all {@link BookEntity} records asynchronously. - * @return A {@link Flux} emitting all {@link BookEntity} records in the repository. + * @return A {@link Flux} emitting all {@link BookEntity} records in the client. */ public Flux executeFindAll() { return Flux.usingWhen(connectionPool.create(), diff --git a/data/data-samples/sample-data-postgres-book/src/main/java/io/americanexpress/data/book/dao/BookRepository.java b/data/data-samples/sample-data-postgres-book/src/main/java/io/americanexpress/data/book/dao/BookRepository.java index 7ff42f9ba..e3983d0fe 100644 --- a/data/data-samples/sample-data-postgres-book/src/main/java/io/americanexpress/data/book/dao/BookRepository.java +++ b/data/data-samples/sample-data-postgres-book/src/main/java/io/americanexpress/data/book/dao/BookRepository.java @@ -20,7 +20,7 @@ import java.util.Optional; /** - * BookRepository is the dao repository to handle the queries for the book table. + * BookRepository is the dao client to handle the queries for the book table. */ @Repository public interface BookRepository extends JpaRepository { diff --git a/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index ee41fa1e0..e510e6bfd 100644 --- a/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -21,7 +21,7 @@ import java.util.UUID; /** - * {@code BookRepository} is the dao repository to handle the queries for the books table. + * {@code BookRepository} is the dao client to handle the queries for the books table. */ @Repository public interface BookRepository extends CrudRepository { diff --git a/function/function-samples/sample-function-book-aws/src/main/java/io/americanexpress/function/book/function/BookFunction.java b/function/function-samples/sample-function-book-aws/src/main/java/io/americanexpress/function/book/function/BookFunction.java index 60605982e..1f89ebc52 100644 --- a/function/function-samples/sample-function-book-aws/src/main/java/io/americanexpress/function/book/function/BookFunction.java +++ b/function/function-samples/sample-function-book-aws/src/main/java/io/americanexpress/function/book/function/BookFunction.java @@ -51,7 +51,7 @@ public class BookFunction { /** * Instantiates a new Book function. * - * @param bookRepository the book repository + * @param bookRepository the book client */ public BookFunction(BookRepository bookRepository) { this.bookRepository = bookRepository; diff --git a/pom.xml b/pom.xml index c6f6df53d..da820f056 100644 --- a/pom.xml +++ b/pom.xml @@ -96,7 +96,6 @@ publisher subscriber utility - client/synapse-client-elastic-search @@ -271,6 +270,11 @@ + + io.americanexpress.synapse + synapse-client-elasticsearch + ${project.version} + io.americanexpress.synapse synapse-client-graphql diff --git a/service/service-samples/sample-service-db2-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java b/service/service-samples/sample-service-db2-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java index 101d16664..7d0afe0fc 100644 --- a/service/service-samples/sample-service-db2-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java +++ b/service/service-samples/sample-service-db2-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java @@ -28,7 +28,7 @@ public class CreateBookService extends BaseCreateService { /** - * The book repository for querying the database. + * The book client for querying the database. */ private final BookRepository bookRepository; @@ -39,7 +39,7 @@ public class CreateBookService extends BaseCreateService { /** - * The book repository for querying the database. + * The book client for querying the database. */ private final BookRepository bookRepository; @@ -41,7 +41,7 @@ public class ReadBookService extends BaseReadMonoService { /** - * The book repository for querying the database. + * The book client for querying the database. */ private final BookRepository bookRepository; @@ -41,7 +41,7 @@ public class UpdateBookService extends BaseUpdateService { /** * Instantiates a new GetBookService. * - * @param bookRepository the book repository + * @param bookRepository the book client */ public GetBookService(BookRepository bookRepository) { this.bookRepository = bookRepository; diff --git a/service/service-samples/sample-service-rest-cassandra-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookService.java b/service/service-samples/sample-service-rest-cassandra-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookService.java index 73979a13b..3f88d6d38 100644 --- a/service/service-samples/sample-service-rest-cassandra-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookService.java +++ b/service/service-samples/sample-service-rest-cassandra-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookService.java @@ -35,7 +35,7 @@ public class ReadBookService extends BaseReadMonoService { /** * Instantiates a new UpdateBookService. * - * @param bookRepository the book repository + * @param bookRepository the book client */ public UpdateBookService(BookRepository bookRepository) { this.bookRepository = bookRepository; diff --git a/service/service-samples/sample-service-rest-cassandra-book/src/test/java/io/americanexpress/service/book/rest/config/BookTestConfig.java b/service/service-samples/sample-service-rest-cassandra-book/src/test/java/io/americanexpress/service/book/rest/config/BookTestConfig.java index 32dc17094..ebdf2eb49 100644 --- a/service/service-samples/sample-service-rest-cassandra-book/src/test/java/io/americanexpress/service/book/rest/config/BookTestConfig.java +++ b/service/service-samples/sample-service-rest-cassandra-book/src/test/java/io/americanexpress/service/book/rest/config/BookTestConfig.java @@ -17,7 +17,7 @@ //import org.springframework.boot.test.context.TestConfiguration; //import org.springframework.boot.test.mock.mockito.MockBean; //import org.springframework.context.annotation.Import; -//import io.americanexpress.data.book.repository.BookRepository; +//import io.americanexpress.data.book.client.BookRepository; //import org.springframework.context.annotation.Primary; // ///** diff --git a/service/service-samples/sample-service-rest-postgres-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java b/service/service-samples/sample-service-rest-postgres-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java index 0f78847d9..b06220909 100644 --- a/service/service-samples/sample-service-rest-postgres-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java +++ b/service/service-samples/sample-service-rest-postgres-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java @@ -33,7 +33,7 @@ public class CreateBookService extends BaseCreateService Date: Mon, 10 Mar 2025 01:00:09 -0400 Subject: [PATCH 3/9] feature(elastic-client): Add client and client sample. --- ...ateProductElasticSearchDocumentClient.java | 12 +-- ...eteProductElasticSearchDocumentClient.java | 17 ++-- ...eadProductElasticSearchDocumentClient.java | 17 ++-- ...ateProductElasticSearchDocumentClient.java | 19 ++-- .../config/ProductElasticSearchConfig.java | 5 ++ .../elasticsearch/product/model/Product.java | 42 +-------- .../CreateProductElasticSearchDocumentIT.java | 25 ++++-- .../ReadProductElasticSearchDocumentIT.java | 50 +++++++++++ ...BaseCreateElasticSearchDocumentClient.java | 39 -------- ...eateElasticSearchDocumentSearchClient.java | 45 ++++++++++ ...BaseDeleteElasticSearchDocumentClient.java | 37 -------- ...leteElasticSearchDocumentSearchClient.java | 49 ++++++++++ .../client/BaseElasticClient.java | 39 -------- .../client/BaseElasticSearchClient.java | 67 ++++++++++++++ .../BaseReadElasticSearchDocumentClient.java | 62 ------------- ...ReadElasticSearchDocumentSearchClient.java | 89 +++++++++++++++++++ ...BaseUpdateElasticSearchDocumentClient.java | 39 -------- ...dateElasticSearchDocumentSearchClient.java | 53 +++++++++++ .../config/BaseElasticSearchClientConfig.java | 7 +- .../model/BaseElasticSearchData.java | 30 +++++++ 20 files changed, 437 insertions(+), 306 deletions(-) create mode 100644 client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/ReadProductElasticSearchDocumentIT.java delete mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentSearchClient.java delete mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentSearchClient.java delete mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticSearchClient.java delete mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java delete mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentSearchClient.java create mode 100644 client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/model/BaseElasticSearchData.java diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/CreateProductElasticSearchDocumentClient.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/CreateProductElasticSearchDocumentClient.java index c6f001585..e394d1e9a 100644 --- a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/CreateProductElasticSearchDocumentClient.java +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/CreateProductElasticSearchDocumentClient.java @@ -2,8 +2,9 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient; import io.americanexpress.sample.client.elasticsearch.product.model.Product; -import io.americanexpress.synapse.client.elasticsearch.client.BaseCreateElasticSearchDocumentClient; +import io.americanexpress.synapse.client.elasticsearch.client.BaseCreateElasticSearchDocumentSearchClient; import org.springframework.stereotype.Component; +import static io.americanexpress.sample.client.elasticsearch.product.config.ProductElasticSearchConfig.INDEX_NAME; /** * {@code CreateProductElasticSearchDocumentClient} creates a document in ElasticSearch. @@ -11,12 +12,7 @@ * @author sshre31 */ @Component -public class CreateProductElasticSearchDocumentClient extends BaseCreateElasticSearchDocumentClient { - - /** - * Index name. - */ - private static final String INDEX_NAME = "product"; +public class CreateProductElasticSearchDocumentClient extends BaseCreateElasticSearchDocumentSearchClient { /** * Create an instance of CreateProductElasticSearchDocumentClient with the specified parameters. @@ -24,6 +20,6 @@ public class CreateProductElasticSearchDocumentClient extends BaseCreateElasticS * @param elasticsearchClient elasticsearchClient */ public CreateProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient) { - super(elasticsearchClient, INDEX_NAME, Product.class); + super(elasticsearchClient, INDEX_NAME); } } diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/DeleteProductElasticSearchDocumentClient.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/DeleteProductElasticSearchDocumentClient.java index 1a54feac3..d449d8bbf 100644 --- a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/DeleteProductElasticSearchDocumentClient.java +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/DeleteProductElasticSearchDocumentClient.java @@ -2,8 +2,9 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient; import io.americanexpress.sample.client.elasticsearch.product.model.Product; -import io.americanexpress.synapse.client.elasticsearch.client.BaseDeleteElasticSearchDocumentClient; +import io.americanexpress.synapse.client.elasticsearch.client.BaseDeleteElasticSearchDocumentSearchClient; import org.springframework.stereotype.Component; +import static io.americanexpress.sample.client.elasticsearch.product.config.ProductElasticSearchConfig.INDEX_NAME; /** * {@code DeleteProductElasticSearchDocumentClient} deletes a document from ElasticSearch. @@ -11,20 +12,14 @@ * @author sshre31 */ @Component -public class DeleteProductElasticSearchDocumentClient extends BaseDeleteElasticSearchDocumentClient { +public class DeleteProductElasticSearchDocumentClient extends BaseDeleteElasticSearchDocumentSearchClient { /** - * Index name. - */ - private static final String INDEX_NAME = "product"; - - /** - * Create an instance of BaseDeleteElasticSearchDocumentClient with the specified parameters. + * Create an instance of BaseDeleteElasticSearchDocumentSearchClient with the specified parameters. * * @param elasticsearchClient elasticsearchClient - * @param clazz clazz */ - public DeleteProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, Class clazz) { - super(elasticsearchClient, INDEX_NAME, clazz); + public DeleteProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient) { + super(elasticsearchClient, INDEX_NAME); } } diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/ReadProductElasticSearchDocumentClient.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/ReadProductElasticSearchDocumentClient.java index cf39f6896..195b6b377 100644 --- a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/ReadProductElasticSearchDocumentClient.java +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/ReadProductElasticSearchDocumentClient.java @@ -2,8 +2,9 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient; import io.americanexpress.sample.client.elasticsearch.product.model.Product; -import io.americanexpress.synapse.client.elasticsearch.client.BaseReadElasticSearchDocumentClient; +import io.americanexpress.synapse.client.elasticsearch.client.BaseReadElasticSearchDocumentSearchClient; import org.springframework.stereotype.Component; +import static io.americanexpress.sample.client.elasticsearch.product.config.ProductElasticSearchConfig.INDEX_NAME; /** * {@code ReadProductElasticSearchDocumentClient} reads a document from ElasticSearch. @@ -11,20 +12,14 @@ * @author sshre31 */ @Component -public class ReadProductElasticSearchDocumentClient extends BaseReadElasticSearchDocumentClient { +public class ReadProductElasticSearchDocumentClient extends BaseReadElasticSearchDocumentSearchClient { /** - * Index name. - */ - private static final String INDEX_NAME = "product"; - - /** - * Create an instance of BaseReadElasticSearchDocumentClient with the specified parameters. + * Create an instance of BaseReadElasticSearchDocumentSearchClient with the specified parameters. * * @param elasticsearchClient elasticsearchClient - * @param clazz clazz */ - public ReadProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, Class clazz) { - super(elasticsearchClient, INDEX_NAME, clazz); + public ReadProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient) { + super(elasticsearchClient, INDEX_NAME); } } diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/UpdateProductElasticSearchDocumentClient.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/UpdateProductElasticSearchDocumentClient.java index 99f626cf8..fe6cfaf0b 100644 --- a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/UpdateProductElasticSearchDocumentClient.java +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/client/UpdateProductElasticSearchDocumentClient.java @@ -2,27 +2,24 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient; import io.americanexpress.sample.client.elasticsearch.product.model.Product; -import io.americanexpress.synapse.client.elasticsearch.client.BaseUpdateElasticSearchDocumentClient; +import io.americanexpress.synapse.client.elasticsearch.client.BaseUpdateElasticSearchDocumentSearchClient; +import org.springframework.stereotype.Component; +import static io.americanexpress.sample.client.elasticsearch.product.config.ProductElasticSearchConfig.INDEX_NAME; /** * {@code UpdateProductElasticSearchDocumentClient} * * @author sshre31 */ -public class UpdateProductElasticSearchDocumentClient extends BaseUpdateElasticSearchDocumentClient { +@Component +public class UpdateProductElasticSearchDocumentClient extends BaseUpdateElasticSearchDocumentSearchClient { /** - * Index name. - */ - private static final String INDEX_NAME = "product"; - - /** - * Create an instance of BaseUpdateElasticSearchDocumentClient with the specified parameters. + * Create an instance of BaseUpdateElasticSearchDocumentSearchClient with the specified parameters. * * @param elasticsearchClient elasticsearchClient - * @param clazz clazz */ - public UpdateProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, Class clazz) { - super(elasticsearchClient, INDEX_NAME, clazz); + public UpdateProductElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient) { + super(elasticsearchClient, INDEX_NAME); } } diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/config/ProductElasticSearchConfig.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/config/ProductElasticSearchConfig.java index ab535351a..33f12e828 100644 --- a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/config/ProductElasticSearchConfig.java +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/config/ProductElasticSearchConfig.java @@ -15,6 +15,11 @@ @Configuration public class ProductElasticSearchConfig extends BaseElasticSearchClientConfig { + /** + * Index name. + */ + public static final String INDEX_NAME = "products"; + /** * Constructor taking in objectMapper & metricInterceptor. * diff --git a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/model/Product.java b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/model/Product.java index 1743ea90c..facc0a77c 100644 --- a/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/model/Product.java +++ b/client/client-samples/sample-client-elasticsearch-product/src/main/java/io/americanexpress/sample/client/elasticsearch/product/model/Product.java @@ -1,19 +1,13 @@ package io.americanexpress.sample.client.elasticsearch.product.model; -import java.util.List; -import java.util.UUID; +import io.americanexpress.synapse.client.elasticsearch.model.BaseElasticSearchData; /** * {@code Product} represents a product. * * @author sshre31 */ -public class Product { - - /** - * ID of this product. - */ - private UUID id; +public class Product extends BaseElasticSearchData { /** * Name of the product. @@ -25,38 +19,6 @@ public class Product { */ private String description; - /** - * Test of the product. - */ - private List test; - - /** - * Default constructor creates a new instance of Product with default values. - */ - public Product(UUID id, String name, String description, List test) { - this.id = id; - this.name = name; - this.description = description; - } - - /** - * Get the id. - * - * @return the id - */ - public UUID getId() { - return id; - } - - /** - * Set the id. - * - * @param id the id to set - */ - public void setId(UUID id) { - this.id = id; - } - /** * Get the name. * diff --git a/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/CreateProductElasticSearchDocumentIT.java b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/CreateProductElasticSearchDocumentIT.java index aa5aa1f8e..226bea418 100644 --- a/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/CreateProductElasticSearchDocumentIT.java +++ b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/CreateProductElasticSearchDocumentIT.java @@ -3,31 +3,42 @@ import io.americanexpress.sample.client.elasticsearch.config.ProductElasticSearchConfigTest; import io.americanexpress.sample.client.elasticsearch.product.client.CreateProductElasticSearchDocumentClient; import io.americanexpress.sample.client.elasticsearch.product.model.Product; -import java.io.IOException; -import java.util.Collections; +import io.americanexpress.synapse.framework.exception.ApplicationClientException; import java.util.UUID; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; /** - * {@code CreateProductElasticSearchDocumentIT} + * {@code CreateProductElasticSearchDocumentIT} tests the {@link CreateProductElasticSearchDocumentClient} class. * * @author sshre31 */ @ContextConfiguration(classes = ProductElasticSearchConfigTest.class) @ExtendWith(SpringExtension.class) -public class CreateProductElasticSearchDocumentIT { +class CreateProductElasticSearchDocumentIT { @Autowired CreateProductElasticSearchDocumentClient createProductElasticSearchDocument; @Test - void save_providedValidProduct_expectedSuccess() throws IOException { - var product = new Product(UUID.randomUUID(), "Product 1", "Product 1 description", Collections.emptyList()); + void save_providedValidProduct_expectedSuccess() { + var product = new Product(); + product.setId(UUID.randomUUID().toString()); + product.setName("Ice Cream"); + product.setDescription("Fudge Ice Cream."); + assertDoesNotThrow(() -> createProductElasticSearchDocument.save(product)); + } - createProductElasticSearchDocument.save(product.getId().toString(), product); + @Test + void read_providedValidProduct_expectedException() { + var product = new Product(); + product.setName("Ice Cream"); + product.setDescription("Fudge Ice Cream."); + assertThrows(ApplicationClientException.class, () -> createProductElasticSearchDocument.save(product)); } } diff --git a/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/ReadProductElasticSearchDocumentIT.java b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/ReadProductElasticSearchDocumentIT.java new file mode 100644 index 000000000..336aaeeec --- /dev/null +++ b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/ReadProductElasticSearchDocumentIT.java @@ -0,0 +1,50 @@ +package io.americanexpress.sample.client.elasticsearch.client; + +import io.americanexpress.sample.client.elasticsearch.config.ProductElasticSearchConfigTest; +import io.americanexpress.sample.client.elasticsearch.product.client.ReadProductElasticSearchDocumentClient; +import java.io.IOException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * {@code ReadProductElasticSearchDocumentIT} + * + * @author sshre31 + */ +@ContextConfiguration(classes = ProductElasticSearchConfigTest.class) +@ExtendWith(SpringExtension.class) +class ReadProductElasticSearchDocumentIT { + + + @Autowired + ReadProductElasticSearchDocumentClient readProductElasticSearchDocumentClient; + + @Test + void findById_providedValidId_expectedSuccess() throws IOException { + var product = readProductElasticSearchDocumentClient.findById("{id}"); + assertNotNull(product); + } + + @Test + void findAll_providedValidRequest_expectedSuccess() throws IOException { + var allProducts = readProductElasticSearchDocumentClient.findAll(); + assertNotNull(allProducts); + } + + @Test + void searchByKey_providedValidKeyword_expectedSuccess() throws IOException { + var products = readProductElasticSearchDocumentClient.searchByKey("name", "Ice Cream"); + assertNotNull(products); + } + + @Test + void doesExists_providedValidId_expectedSuccess() throws IOException { + var products = readProductElasticSearchDocumentClient.doesExists("{id}"); + assertTrue(products); + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentClient.java deleted file mode 100644 index 588d81da6..000000000 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentClient.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.americanexpress.synapse.client.elasticsearch.client; - -import co.elastic.clients.elasticsearch.ElasticsearchClient; -import java.io.IOException; - -/** - * {@code BaseCreateElasticSearchDocumentClient} creates a document in ElasticSearch. - * - * @author sshre31 - */ -public abstract class BaseCreateElasticSearchDocumentClient extends BaseElasticClient { - - /** - * Create an instance of BaseCreateElasticSearchDocumentClient with the specified parameters. - * - * @param elasticsearchClient elasticsearchClient - * @param indexName indexName - * @param clazz clazz - */ - public BaseCreateElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, - String indexName, Class clazz) { - super(elasticsearchClient, indexName, clazz); - } - - /** - * Save the document in ElasticSearch. - * - * @param id the identifier - * @param document the document to save - * @throws IOException if an error occurs while saving the document - */ - public void save(String id, T document) throws IOException { - this.client.index(i -> i - .index(this.indexName) - .id(id) - .document(document) - ); - } -} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentSearchClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentSearchClient.java new file mode 100644 index 000000000..d2bcdf276 --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentSearchClient.java @@ -0,0 +1,45 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import io.americanexpress.synapse.client.elasticsearch.model.BaseElasticSearchData; +import io.americanexpress.synapse.framework.exception.ApplicationClientException; +import io.americanexpress.synapse.framework.exception.model.ErrorCode; +import io.micrometer.common.util.StringUtils; +import java.io.IOException; + +/** + * {@code BaseCreateElasticSearchDocumentSearchClient} creates a document in ElasticSearch. + * + * @author sshre31 + */ +public abstract class BaseCreateElasticSearchDocumentSearchClient extends BaseElasticSearchClient { + + /** + * Create an instance of BaseCreateElasticSearchDocumentSearchClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + */ + public BaseCreateElasticSearchDocumentSearchClient(ElasticsearchClient elasticsearchClient, + String indexName) { + super(elasticsearchClient, indexName); + } + + /** + * Save the document in ElasticSearch. + * + * @param document the document to save + * @throws IOException if an error occurs while saving the document + */ + public void save(T document) throws IOException { + if (StringUtils.isBlank(document.getId())) { + throw new ApplicationClientException("Document ID is required.", ErrorCode.GENERIC_4XX_ERROR); + } + + this.elasticsearchClient.index(i -> i + .index(this.indexName) + .id(document.getId()) + .document(document) + ); + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentClient.java deleted file mode 100644 index 947f4465a..000000000 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentClient.java +++ /dev/null @@ -1,37 +0,0 @@ -package io.americanexpress.synapse.client.elasticsearch.client; - -import co.elastic.clients.elasticsearch.ElasticsearchClient; -import java.io.IOException; - -/** - * {@code BaseDeleteElasticSearchDocumentClient} deletes a document in ElasticSearch. - * - * @author sshre31 - */ -public abstract class BaseDeleteElasticSearchDocumentClient extends BaseElasticClient { - - /** - * Create an instance of BaseDeleteElasticSearchDocumentClient with the specified parameters. - * - * @param elasticsearchClient elasticsearchClient - * @param indexName indexName - * @param clazz clazz - */ - public BaseDeleteElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, String indexName, - Class clazz) { - super(elasticsearchClient, indexName, clazz); - } - - /** - * Delete the document in ElasticSearch. - * - * @param id the identifier - * @throws IOException if an error occurs while deleting the document - */ - public void deleteById(String id) throws IOException { - this.client.delete(d -> d - .index(this.indexName) - .id(id) - ); - } -} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentSearchClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentSearchClient.java new file mode 100644 index 000000000..9a063c4f6 --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseDeleteElasticSearchDocumentSearchClient.java @@ -0,0 +1,49 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import io.americanexpress.synapse.client.elasticsearch.model.BaseElasticSearchData; +import java.io.IOException; + +/** + * {@code BaseDeleteElasticSearchDocumentSearchClient} deletes a document in ElasticSearch. + * + * @author sshre31 + */ +public abstract class BaseDeleteElasticSearchDocumentSearchClient extends BaseElasticSearchClient { + + /** + * Create an instance of BaseDeleteElasticSearchDocumentSearchClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + */ + public BaseDeleteElasticSearchDocumentSearchClient(ElasticsearchClient elasticsearchClient, String indexName) { + super(elasticsearchClient, indexName); + } + + /** + * Delete the document in ElasticSearch. + * + * @param id the identifier + * @throws IOException if an error occurs while deleting the document + */ + public void deleteById(String id) throws IOException { + this.elasticsearchClient.delete(d -> d + .index(this.indexName) + .id(id) + ); + } + + /** + * Delete the document in ElasticSearch. + * + * @param document the document to delete + * @throws IOException if an error occurs while deleting the document + */ + public void delete(T document) throws IOException { + this.elasticsearchClient.delete(d -> d + .index(this.indexName) + .id(document.getId()) + ); + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticClient.java deleted file mode 100644 index d393cace5..000000000 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticClient.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.americanexpress.synapse.client.elasticsearch.client; - -import co.elastic.clients.elasticsearch.ElasticsearchClient; - -/** - * {@code BaseElasticClient} is the base class for all ElasticSearch clients. - * - * @author sshre31 - */ -public abstract class BaseElasticClient { - - /** - * Elasticsearch client. - */ - protected final ElasticsearchClient client; - - /** - * Index name. - */ - protected final String indexName; - - /** - * Class of the document. - */ - protected final Class clazz; - - /** - * Create an instance of BaseElasticClient with the specified parameters. - * - * @param elasticsearchClient elasticsearchClient - * @param indexName indexName - * @param clazz clazz - */ - public BaseElasticClient(ElasticsearchClient elasticsearchClient, String indexName, Class clazz) { - this.client = elasticsearchClient; - this.indexName = indexName; - this.clazz = clazz; - } -} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticSearchClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticSearchClient.java new file mode 100644 index 000000000..59e22bfd6 --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticSearchClient.java @@ -0,0 +1,67 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.elasticsearch.core.SearchResponse; +import co.elastic.clients.elasticsearch.core.search.Hit; +import io.americanexpress.synapse.client.elasticsearch.model.BaseElasticSearchData; +import java.lang.reflect.ParameterizedType; +import java.util.ArrayList; +import java.util.List; + +/** + * {@code BaseElasticSearchClient} is the base class for all ElasticSearch clients. + * + * @author sshre31 + */ +public abstract class BaseElasticSearchClient { + + /** + * Elasticsearch client. + */ + protected final ElasticsearchClient elasticsearchClient; + + /** + * Index name. + */ + protected final String indexName; + + /** + * Class of the document. + */ + protected Class documentType; + + /** + * Create an instance of BaseElasticSearchClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + */ + public BaseElasticSearchClient(ElasticsearchClient elasticsearchClient, String indexName) { + this.elasticsearchClient = elasticsearchClient; + this.indexName = indexName; + initialize(); + } + + /** + * Initialize the document type. + */ + @SuppressWarnings("unchecked") + private void initialize() { + var parameterizedType = ((ParameterizedType) getClass().getGenericSuperclass()); + this.documentType = (Class) parameterizedType.getActualTypeArguments()[0]; + } + + /** + * Render the results from the search response. + * + * @param response the search response + * @return the list of results + */ + List renderResults(SearchResponse response) { + List results = new ArrayList<>(); + for (Hit hit : response.hits().hits()) { + results.add(hit.source()); + } + return results; + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentClient.java deleted file mode 100644 index 6a909579a..000000000 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentClient.java +++ /dev/null @@ -1,62 +0,0 @@ -package io.americanexpress.synapse.client.elasticsearch.client; - -import co.elastic.clients.elasticsearch.ElasticsearchClient; -import co.elastic.clients.elasticsearch.core.GetResponse; -import co.elastic.clients.elasticsearch.core.SearchResponse; -import co.elastic.clients.elasticsearch.core.search.Hit; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -/** - * {@code BaseReadElasticSearchDocumentClient} reads a document in ElasticSearch. - * - * @author sshre31 - */ -public abstract class BaseReadElasticSearchDocumentClient extends BaseElasticClient { - - /** - * Create an instance of BaseReadElasticSearchDocumentClient with the specified parameters. - * - * @param elasticsearchClient elasticsearchClient - * @param indexName indexName - * @param clazz clazz - */ - public BaseReadElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, String indexName, - Class clazz) { - super(elasticsearchClient, indexName, clazz); - } - - /** - * Find the document by the identifier. - * - * @param id the identifier - * @return the document - * @throws IOException if an error occurs while finding the document - */ - public T findById(String id) throws IOException { - GetResponse response = client.get(g -> g - .index(indexName) - .id(id), clazz - ); - return response.found() ? response.source() : null; - } - - /** - * Find all the documents. - * - * @return the list of documents - * @throws IOException if an error occurs while finding the documents - */ - public List findAll() throws IOException { - SearchResponse response = client.search(s -> s - .index(indexName) - .query(q -> q.matchAll(m -> m)), clazz - ); - List results = new ArrayList<>(); - for (Hit hit : response.hits().hits()) { - results.add(hit.source()); - } - return results; - } -} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java new file mode 100644 index 000000000..250941271 --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java @@ -0,0 +1,89 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.elasticsearch.core.GetResponse; +import io.americanexpress.synapse.client.elasticsearch.model.BaseElasticSearchData; +import java.io.IOException; +import java.util.List; + +/** + * {@code BaseReadElasticSearchDocumentSearchClient} reads a document in ElasticSearch. + * + * @author sshre31 + */ +public abstract class BaseReadElasticSearchDocumentSearchClient extends BaseElasticSearchClient { + + /** + * Create an instance of BaseReadElasticSearchDocumentSearchClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + */ + public BaseReadElasticSearchDocumentSearchClient(ElasticsearchClient elasticsearchClient, String indexName) { + super(elasticsearchClient, indexName); + } + + /** + * Find the document by the identifier. + * + * @param id the identifier + * @return the document + * @throws IOException if an error occurs while finding the document + */ + public T findById(String id) throws IOException { + var response = elasticsearchClient.get(g -> g + .index(indexName) + .id(id), this.documentType + ); + return response.found() ? response.source() : null; + } + + /** + * Find all the documents. + * + * @return the list of documents + * @throws IOException if an error occurs while finding the documents + */ + public List findAll() throws IOException { + var response = elasticsearchClient.search(s -> s + .index(indexName) + .query(q -> q.matchAll(m -> m)), + this.documentType + ); + return renderResults(response); + } + + /** + * Find the document by the key. + * + * @param key the key + * @param value the value + * @throws IOException if an error occurs while finding the documents + */ + public List searchByKey(String key, String value) throws IOException { + var response = elasticsearchClient.search(s -> s + .index(indexName) + .query(q -> q.match(t -> t + .field(key) + .query(value) + ) + ), this.documentType + ); + return renderResults(response); + } + + /** + * Check if the document exists. + * + * @param id the identifier + * @return true if the document exists, false otherwise + * @throws IOException if an error occurs while checking if the document exists + */ + public boolean doesExists(String id) throws IOException { + GetResponse response = elasticsearchClient.get(g -> g + .index(indexName) + .id(id), this.documentType + ); + return response.found(); + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentClient.java deleted file mode 100644 index 145d6b3fe..000000000 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentClient.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.americanexpress.synapse.client.elasticsearch.client; - -import co.elastic.clients.elasticsearch.ElasticsearchClient; -import java.io.IOException; - -/** - * {@code BaseUpdateElasticSearchDocumentClient} - * - * @author sshre31 - */ -public abstract class BaseUpdateElasticSearchDocumentClient extends BaseElasticClient { - - /** - * Create an instance of BaseUpdateElasticSearchDocumentClient with the specified parameters. - * - * @param elasticsearchClient elasticsearchClient - * @param indexName indexName - * @param clazz clazz - */ - public BaseUpdateElasticSearchDocumentClient(ElasticsearchClient elasticsearchClient, String indexName, - Class clazz) { - super(elasticsearchClient, indexName, clazz); - } - - /** - * Update the document in ElasticSearch. - * - * @param id the identifier - * @param document the document to update - * @throws IOException if an error occurs while updating the document - */ - public void update(String id, T document) throws IOException { - client.update(u -> u - .index(indexName) - .id(id) - .doc(document), clazz - ); - } -} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentSearchClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentSearchClient.java new file mode 100644 index 000000000..087a1e509 --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseUpdateElasticSearchDocumentSearchClient.java @@ -0,0 +1,53 @@ +package io.americanexpress.synapse.client.elasticsearch.client; + +import co.elastic.clients.elasticsearch.ElasticsearchClient; +import io.americanexpress.synapse.client.elasticsearch.model.BaseElasticSearchData; +import java.io.IOException; + +/** + * {@code BaseUpdateElasticSearchDocumentSearchClient} + * + * @author sshre31 + */ +public abstract class BaseUpdateElasticSearchDocumentSearchClient extends BaseElasticSearchClient { + + /** + * Create an instance of BaseUpdateElasticSearchDocumentSearchClient with the specified parameters. + * + * @param elasticsearchClient elasticsearchClient + * @param indexName indexName + */ + public BaseUpdateElasticSearchDocumentSearchClient(ElasticsearchClient elasticsearchClient, String indexName) { + super(elasticsearchClient, indexName); + } + + /** + * Update the document in ElasticSearch. + * + * @param document the document to update + * @throws IOException if an error occurs while updating the document + */ + public void update(T document) throws IOException { + elasticsearchClient.update(u -> u + .index(indexName) + .id(document.getId()) + .doc(document), + this.documentType + ); + } + + /** + * Upsert the document in ElasticSearch. + * + * @param document the document to update + * @throws IOException if an error occurs while upserting the document + */ + public void upsert(T document) throws IOException { + elasticsearchClient.update(u -> u + .index(indexName) + .id(document.getId()) + .upsert(document), + this.documentType + ); + } +} diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java index 9c25b42ce..30a5bb37b 100644 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java @@ -32,6 +32,9 @@ public class BaseElasticSearchClientConfig { */ private final ObjectMapper defaultObjectMapper; + /** + * The environment. + */ private final Environment environment; /** @@ -51,8 +54,8 @@ public BaseElasticSearchClientConfig(ObjectMapper defaultObjectMapper, Environme */ @Bean public ElasticsearchClient elasticsearchClient() { - var elasticSearchUrl = environment.getRequiredProperty("elastic-client.url"); - var elasticSearchApiKey = environment.getRequiredProperty("elastic-client.apikey"); + var elasticSearchUrl = "https://localhost:9200"; + var elasticSearchApiKey = "ZWR4Y0xwTUJUQmZSMW1pVl9DMXA6ZVRCaWtWc21UQy02RTdRYklXbXFkdw=="; var restClient = RestClient .builder(HttpHost.create(elasticSearchUrl)) diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/model/BaseElasticSearchData.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/model/BaseElasticSearchData.java new file mode 100644 index 000000000..f291264a3 --- /dev/null +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/model/BaseElasticSearchData.java @@ -0,0 +1,30 @@ +package io.americanexpress.synapse.client.elasticsearch.model; + +/** + * {@code BaseElasticSearchData} is the base class for all ElasticSearch data. + * + * @author sshre31 + */ +public abstract class BaseElasticSearchData { + + /** + * The id of the entity. + */ + private String id; + + /** + * Gets the entity's id. + * @return the id + */ + public String getId() { + return id; + } + + /** + * Sets the entity's id. + * @param id the id of this entity + */ + public void setId(String id) { + this.id = id; + } +} From c981b28cfd99b9ab5bf45ef14a3d29f52a7a2edd Mon Sep 17 00:00:00 2001 From: sshre31 Date: Mon, 10 Mar 2025 01:14:57 -0400 Subject: [PATCH 4/9] feature(elastic-client): Add client and client sample. --- .../client-samples/sample-client-elasticsearch-product/pom.xml | 2 +- client/synapse-client-elasticsearch/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/client-samples/sample-client-elasticsearch-product/pom.xml b/client/client-samples/sample-client-elasticsearch-product/pom.xml index 3ed07bea0..3e4fe2c96 100644 --- a/client/client-samples/sample-client-elasticsearch-product/pom.xml +++ b/client/client-samples/sample-client-elasticsearch-product/pom.xml @@ -6,7 +6,7 @@ io.americanexpress.synapse client-samples - 0.4.0-SNAPSHOT + 0.4.10-SNAPSHOT sample-client-elasticsearch-product diff --git a/client/synapse-client-elasticsearch/pom.xml b/client/synapse-client-elasticsearch/pom.xml index 056dcde38..b6fa003fb 100644 --- a/client/synapse-client-elasticsearch/pom.xml +++ b/client/synapse-client-elasticsearch/pom.xml @@ -5,7 +5,7 @@ io.americanexpress.synapse synapse - 0.4.0-SNAPSHOT + 0.4.10-SNAPSHOT 4.0.0 From 1bf096759444abd8a29116f5b9b980966d468bb6 Mon Sep 17 00:00:00 2001 From: sshre31 Date: Mon, 10 Mar 2025 01:21:40 -0400 Subject: [PATCH 5/9] feature(elastic-client): Remove comments. --- .github/workflows/codeql-analysis.yml | 6 +++--- .../data/book/repository/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../data/oracle/cp/book/service/BookPersistenceService.java | 4 ++-- .../io/americanexpress/data/book/dao/BookRepository.java | 2 +- .../data/book/repository/BookRepository.java | 2 +- .../function/book/function/BookFunction.java | 2 +- .../service/book/rest/service/CreateBookService.java | 4 ++-- .../service/book/rest/service/DeleteBookService.java | 4 ++-- .../service/book/rest/service/ReadBookService.java | 4 ++-- .../service/book/rest/service/UpdateBookService.java | 4 ++-- .../book/rest/service/CreateBookReactiveService.java | 2 +- .../book/rest/service/DeleteBookReactiveService.java | 2 +- .../service/book/rest/service/GetBookReactiveService.java | 2 +- .../service/book/rest/service/ReadBookReactiveService.java | 2 +- .../book/rest/service/UpdateBookReactiveService.java | 2 +- .../book/rest/service/CreateBookReactiveService.java | 2 +- .../book/rest/service/DeleteBookReactiveService.java | 2 +- .../book/rest/service/ReadPolyBookReactiveService.java | 2 +- .../book/rest/service/UpdateBookReactiveService.java | 2 +- .../service/book/rest/service/CreateBookService.java | 2 +- .../service/book/rest/service/DeleteBookService.java | 2 +- .../service/book/rest/service/GetBookService.java | 2 +- .../service/book/rest/service/ReadBookService.java | 2 +- .../service/book/rest/service/UpdateBookService.java | 2 +- .../service/book/rest/config/BookTestConfig.java | 2 +- .../service/book/rest/service/CreateBookService.java | 2 +- .../service/book/rest/service/DeleteBookService.java | 2 +- .../service/book/rest/service/ReadBookService.java | 2 +- 32 files changed, 39 insertions(+), 39 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index ca4ba20cd..25de84aeb 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -1,11 +1,11 @@ # For most projects, this workflow file will not need changing; you simply need -# to commit it to your client. +# to commit it to your repository. # # You may wish to alter this file to override the set of languages analyzed, # or to provide custom queries or build logic. # # ******** NOTE ******** -# We have attempted to detect the languages in your client. Please check +# We have attempted to detect the languages in your repository. Please check # the `language` matrix defined below to confirm you have the correct set of # supported CodeQL languages. # @@ -38,7 +38,7 @@ jobs: # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed steps: - - name: Checkout client + - name: Checkout repository uses: actions/checkout@v2 # Initializes the CodeQL tools for scanning. diff --git a/data/data-samples/sample-data-cassandra-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-cassandra-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 0f6383587..e0d3a34fe 100644 --- a/data/data-samples/sample-data-cassandra-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-cassandra-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -22,7 +22,7 @@ import java.util.UUID; /** - * {@code BookRepository} is the dao client to handle the queries for the books table. + * {@code BookRepository} is the dao repository to handle the queries for the books table. */ @Repository public interface BookRepository extends CassandraRepository { diff --git a/data/data-samples/sample-data-cassandra-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-cassandra-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 738f97607..3e945ad39 100644 --- a/data/data-samples/sample-data-cassandra-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-cassandra-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -22,7 +22,7 @@ import java.util.UUID; /** - * {@code BookRepository} is the dao client to handle the queries for the books table. + * {@code BookRepository} is the dao repository to handle the queries for the books table. */ @Repository public interface BookRepository extends ReactiveCassandraRepository { diff --git a/data/data-samples/sample-data-db2-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-db2-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index 22381f2f4..2dcf3cdbc 100644 --- a/data/data-samples/sample-data-db2-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-db2-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -18,7 +18,7 @@ import org.springframework.stereotype.Repository; /** - * {@code BookRepository} is the dao client to handle the queries for the books table. + * {@code BookRepository} is the dao repository to handle the queries for the books table. * @author tisla4 */ @Repository diff --git a/data/data-samples/sample-data-mongodb-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-mongodb-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index f1b28adad..5af810536 100644 --- a/data/data-samples/sample-data-mongodb-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-mongodb-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -18,7 +18,7 @@ import org.springframework.stereotype.Repository; /** - * {@code BookRepository} is the dao client to handle the queries for the books table. + * {@code BookRepository} is the dao repository to handle the queries for the books table. */ @Repository public interface BookRepository extends MongoRepository { diff --git a/data/data-samples/sample-data-mongodb-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-mongodb-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index b6f4ea3eb..2e2d3d730 100644 --- a/data/data-samples/sample-data-mongodb-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-mongodb-reactive/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -20,7 +20,7 @@ import reactor.core.publisher.Mono; /** - * {@code BookRepository} is the dao client to handle the queries for the books table. + * {@code BookRepository} is the dao repository to handle the queries for the books table. */ @Repository public interface BookRepository extends ReactiveMongoRepository { diff --git a/data/data-samples/sample-data-oracle-reactive-cp-book/src/main/java/io/americanexpress/data/oracle/cp/book/service/BookPersistenceService.java b/data/data-samples/sample-data-oracle-reactive-cp-book/src/main/java/io/americanexpress/data/oracle/cp/book/service/BookPersistenceService.java index d8f328e83..d842e2394 100644 --- a/data/data-samples/sample-data-oracle-reactive-cp-book/src/main/java/io/americanexpress/data/oracle/cp/book/service/BookPersistenceService.java +++ b/data/data-samples/sample-data-oracle-reactive-cp-book/src/main/java/io/americanexpress/data/oracle/cp/book/service/BookPersistenceService.java @@ -25,7 +25,7 @@ * {@code BookPersistenceService} A persistence service layer for managing data using * a connection pool for efficient database interactions. * The service encapsulates methods to perform all CRUD (Create, Read, Update, & Delete) operations on - * {@link BookEntity} client which manges the lifecycle of the underlying database connections. + * {@link BookEntity} repository which manges the lifecycle of the underlying database connections. * It ensures that connections are acquired, used, and released correctly using a connection pool, * allowing for better performance and resource management. */ @@ -61,7 +61,7 @@ public Mono executeFindByTitleAndAuthor(String title, String author) /** * Retrieves all {@link BookEntity} records asynchronously. - * @return A {@link Flux} emitting all {@link BookEntity} records in the client. + * @return A {@link Flux} emitting all {@link BookEntity} records in the repository. */ public Flux executeFindAll() { return Flux.usingWhen(connectionPool.create(), diff --git a/data/data-samples/sample-data-postgres-book/src/main/java/io/americanexpress/data/book/dao/BookRepository.java b/data/data-samples/sample-data-postgres-book/src/main/java/io/americanexpress/data/book/dao/BookRepository.java index e3983d0fe..7ff42f9ba 100644 --- a/data/data-samples/sample-data-postgres-book/src/main/java/io/americanexpress/data/book/dao/BookRepository.java +++ b/data/data-samples/sample-data-postgres-book/src/main/java/io/americanexpress/data/book/dao/BookRepository.java @@ -20,7 +20,7 @@ import java.util.Optional; /** - * BookRepository is the dao client to handle the queries for the book table. + * BookRepository is the dao repository to handle the queries for the book table. */ @Repository public interface BookRepository extends JpaRepository { diff --git a/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java b/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java index e510e6bfd..ee41fa1e0 100644 --- a/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java +++ b/data/data-samples/sample-data-redis-book/src/main/java/io/americanexpress/data/book/repository/BookRepository.java @@ -21,7 +21,7 @@ import java.util.UUID; /** - * {@code BookRepository} is the dao client to handle the queries for the books table. + * {@code BookRepository} is the dao repository to handle the queries for the books table. */ @Repository public interface BookRepository extends CrudRepository { diff --git a/function/function-samples/sample-function-book-aws/src/main/java/io/americanexpress/function/book/function/BookFunction.java b/function/function-samples/sample-function-book-aws/src/main/java/io/americanexpress/function/book/function/BookFunction.java index 1f89ebc52..60605982e 100644 --- a/function/function-samples/sample-function-book-aws/src/main/java/io/americanexpress/function/book/function/BookFunction.java +++ b/function/function-samples/sample-function-book-aws/src/main/java/io/americanexpress/function/book/function/BookFunction.java @@ -51,7 +51,7 @@ public class BookFunction { /** * Instantiates a new Book function. * - * @param bookRepository the book client + * @param bookRepository the book repository */ public BookFunction(BookRepository bookRepository) { this.bookRepository = bookRepository; diff --git a/service/service-samples/sample-service-db2-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java b/service/service-samples/sample-service-db2-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java index 7d0afe0fc..101d16664 100644 --- a/service/service-samples/sample-service-db2-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java +++ b/service/service-samples/sample-service-db2-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java @@ -28,7 +28,7 @@ public class CreateBookService extends BaseCreateService { /** - * The book client for querying the database. + * The book repository for querying the database. */ private final BookRepository bookRepository; @@ -39,7 +39,7 @@ public class CreateBookService extends BaseCreateService { /** - * The book client for querying the database. + * The book repository for querying the database. */ private final BookRepository bookRepository; @@ -41,7 +41,7 @@ public class ReadBookService extends BaseReadMonoService { /** - * The book client for querying the database. + * The book repository for querying the database. */ private final BookRepository bookRepository; @@ -41,7 +41,7 @@ public class UpdateBookService extends BaseUpdateService { /** * Instantiates a new GetBookService. * - * @param bookRepository the book client + * @param bookRepository the book repository */ public GetBookService(BookRepository bookRepository) { this.bookRepository = bookRepository; diff --git a/service/service-samples/sample-service-rest-cassandra-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookService.java b/service/service-samples/sample-service-rest-cassandra-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookService.java index 3f88d6d38..73979a13b 100644 --- a/service/service-samples/sample-service-rest-cassandra-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookService.java +++ b/service/service-samples/sample-service-rest-cassandra-book/src/main/java/io/americanexpress/service/book/rest/service/ReadBookService.java @@ -35,7 +35,7 @@ public class ReadBookService extends BaseReadMonoService { /** * Instantiates a new UpdateBookService. * - * @param bookRepository the book client + * @param bookRepository the book repository */ public UpdateBookService(BookRepository bookRepository) { this.bookRepository = bookRepository; diff --git a/service/service-samples/sample-service-rest-cassandra-book/src/test/java/io/americanexpress/service/book/rest/config/BookTestConfig.java b/service/service-samples/sample-service-rest-cassandra-book/src/test/java/io/americanexpress/service/book/rest/config/BookTestConfig.java index ebdf2eb49..32dc17094 100644 --- a/service/service-samples/sample-service-rest-cassandra-book/src/test/java/io/americanexpress/service/book/rest/config/BookTestConfig.java +++ b/service/service-samples/sample-service-rest-cassandra-book/src/test/java/io/americanexpress/service/book/rest/config/BookTestConfig.java @@ -17,7 +17,7 @@ //import org.springframework.boot.test.context.TestConfiguration; //import org.springframework.boot.test.mock.mockito.MockBean; //import org.springframework.context.annotation.Import; -//import io.americanexpress.data.book.client.BookRepository; +//import io.americanexpress.data.book.repository.BookRepository; //import org.springframework.context.annotation.Primary; // ///** diff --git a/service/service-samples/sample-service-rest-postgres-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java b/service/service-samples/sample-service-rest-postgres-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java index b06220909..0f78847d9 100644 --- a/service/service-samples/sample-service-rest-postgres-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java +++ b/service/service-samples/sample-service-rest-postgres-book/src/main/java/io/americanexpress/service/book/rest/service/CreateBookService.java @@ -33,7 +33,7 @@ public class CreateBookService extends BaseCreateService Date: Tue, 11 Mar 2025 13:49:51 -0400 Subject: [PATCH 6/9] feature(elastic-client): fix dependency. --- client/synapse-client-elasticsearch/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/synapse-client-elasticsearch/pom.xml b/client/synapse-client-elasticsearch/pom.xml index b6fa003fb..8af3d3656 100644 --- a/client/synapse-client-elasticsearch/pom.xml +++ b/client/synapse-client-elasticsearch/pom.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> io.americanexpress.synapse - synapse + client 0.4.10-SNAPSHOT From 4323c2e922ee178eacaa87bec1f992c2fb3d8004 Mon Sep 17 00:00:00 2001 From: sshre31 Date: Wed, 12 Mar 2025 22:47:15 -0400 Subject: [PATCH 7/9] feature(elastic-client): Update client. --- .../config/BaseElasticSearchClientConfig.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java index 30a5bb37b..5db0e97e3 100644 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/config/BaseElasticSearchClientConfig.java @@ -6,16 +6,16 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.americanexpress.synapse.framework.exception.config.ExceptionConfig; import io.americanexpress.synapse.utilities.common.config.UtilitiesCommonConfig; -import org.apache.http.Header; import org.apache.http.HttpHost; -import org.apache.http.message.BasicHeader; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.impl.client.BasicCredentialsProvider; import org.elasticsearch.client.RestClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.env.Environment; -import static org.springframework.http.HttpHeaders.AUTHORIZATION; /** * {@code BaseElasticSearchClientConfig} specifies the base configuration for the ElasticSearch client. @@ -54,14 +54,17 @@ public BaseElasticSearchClientConfig(ObjectMapper defaultObjectMapper, Environme */ @Bean public ElasticsearchClient elasticsearchClient() { - var elasticSearchUrl = "https://localhost:9200"; - var elasticSearchApiKey = "ZWR4Y0xwTUJUQmZSMW1pVl9DMXA6ZVRCaWtWc21UQy02RTdRYklXbXFkdw=="; + var elasticSearchUrl = environment.getRequiredProperty("elastic-client.url"); + var elasticSearchUser = environment.getRequiredProperty("elastic-client.username"); + var elasticSearchPassword = environment.getRequiredProperty("elastic-client.password"); + + var basicCredentialsProvider = new BasicCredentialsProvider(); + basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elasticSearchUser, elasticSearchPassword)); var restClient = RestClient .builder(HttpHost.create(elasticSearchUrl)) - .setDefaultHeaders(new Header[]{ - new BasicHeader(AUTHORIZATION, "ApiKey " + elasticSearchApiKey) - }) + .setHttpClientConfigCallback(httpAsyncClientBuilder -> + httpAsyncClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider)) .build(); var transport = new RestClientTransport( From 6f5b3664c30ab8d0746f676e7ab49ad989faaec3 Mon Sep 17 00:00:00 2001 From: sanilshrmiu <129837713+sanilshrmiu@users.noreply.github.com> Date: Mon, 17 Mar 2025 01:36:15 -0400 Subject: [PATCH 8/9] client-elastic-search: Update access modifiers to protected --- client/synapse-client-elasticsearch/README.md | 33 ++++++++++++++++++- ...eateElasticSearchDocumentSearchClient.java | 2 +- ...leteElasticSearchDocumentSearchClient.java | 2 +- .../client/BaseElasticSearchClient.java | 2 +- ...ReadElasticSearchDocumentSearchClient.java | 2 +- ...dateElasticSearchDocumentSearchClient.java | 2 +- 6 files changed, 37 insertions(+), 6 deletions(-) diff --git a/client/synapse-client-elasticsearch/README.md b/client/synapse-client-elasticsearch/README.md index 6a4e91225..e9bb9da5f 100644 --- a/client/synapse-client-elasticsearch/README.md +++ b/client/synapse-client-elasticsearch/README.md @@ -1,5 +1,36 @@ -# synapse-client-rest +# synapse-client-elasticsearch ## Description +The `synapse-client-elasticsearch` module provides a client interface for interacting with Elasticsearch within the Synapse framework. It includes functionalities for indexing, searching, and managing Elasticsearch documents. + +## Features +- Indexing documents into Elasticsearch +- Searching documents with various query types +- Managing Elasticsearch indices +- Handling Elasticsearch exceptions + +## Installation +To include the `synapse-client-elasticsearch` module in your project, add the following dependency to your `pom.xml`: ## Usage + +Add the configuration to your `application.properties` file: + +```properties +elastic-client.url={elastic-url} +elastic-client.username={elastic-username} +elastic-client.password={elastic-password} +``` + +- To utilize this module, add the following dependency to the pom.xml file: +```xml + + io.americanexpress.synapse + synapse-client-elasticsearch + 4.0.0 + +``` +Or add the following to the build.gradle file: +``` +implementation 'io.americanexpress.synapse:synapse-client-graphql:0.3.32-SNAPSHOT' +``` diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentSearchClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentSearchClient.java index d2bcdf276..a9cf0f64b 100644 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentSearchClient.java +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseCreateElasticSearchDocumentSearchClient.java @@ -20,7 +20,7 @@ public abstract class BaseCreateElasticSearchDocumentSearchClient { * @param elasticsearchClient elasticsearchClient * @param indexName indexName */ - public BaseElasticSearchClient(ElasticsearchClient elasticsearchClient, String indexName) { + protected BaseElasticSearchClient(ElasticsearchClient elasticsearchClient, String indexName) { this.elasticsearchClient = elasticsearchClient; this.indexName = indexName; initialize(); diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java index 250941271..25eaa18bd 100644 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java @@ -19,7 +19,7 @@ public abstract class BaseReadElasticSearchDocumentSearchClient Date: Mon, 21 Apr 2025 13:28:47 -0400 Subject: [PATCH 9/9] Add Pagination to response --- .../ReadProductElasticSearchDocumentIT.java | 4 +- client/synapse-client-elasticsearch/pom.xml | 4 ++ .../client/BaseElasticSearchClient.java | 48 +++++++++++++++++-- ...ReadElasticSearchDocumentSearchClient.java | 23 +++++---- 4 files changed, 63 insertions(+), 16 deletions(-) diff --git a/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/ReadProductElasticSearchDocumentIT.java b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/ReadProductElasticSearchDocumentIT.java index 336aaeeec..5e955c414 100644 --- a/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/ReadProductElasticSearchDocumentIT.java +++ b/client/client-samples/sample-client-elasticsearch-product/src/test/java/io/americanexpress/sample/client/elasticsearch/client/ReadProductElasticSearchDocumentIT.java @@ -32,13 +32,13 @@ void findById_providedValidId_expectedSuccess() throws IOException { @Test void findAll_providedValidRequest_expectedSuccess() throws IOException { - var allProducts = readProductElasticSearchDocumentClient.findAll(); + var allProducts = readProductElasticSearchDocumentClient.findAll(0, 10); assertNotNull(allProducts); } @Test void searchByKey_providedValidKeyword_expectedSuccess() throws IOException { - var products = readProductElasticSearchDocumentClient.searchByKey("name", "Ice Cream"); + var products = readProductElasticSearchDocumentClient.searchByKey("name", "Ice Cream", 0, 10); assertNotNull(products); } diff --git a/client/synapse-client-elasticsearch/pom.xml b/client/synapse-client-elasticsearch/pom.xml index 8af3d3656..ece66ade3 100644 --- a/client/synapse-client-elasticsearch/pom.xml +++ b/client/synapse-client-elasticsearch/pom.xml @@ -42,6 +42,10 @@ co.elastic.clients elasticsearch-java + + org.springframework.boot + spring-boot-starter-data-jpa + diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticSearchClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticSearchClient.java index 82f39f474..fc7a64c05 100644 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticSearchClient.java +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseElasticSearchClient.java @@ -3,10 +3,17 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient; import co.elastic.clients.elasticsearch.core.SearchResponse; import co.elastic.clients.elasticsearch.core.search.Hit; +import co.elastic.clients.elasticsearch.core.search.HitsMetadata; +import co.elastic.clients.elasticsearch.core.search.TotalHits; import io.americanexpress.synapse.client.elasticsearch.model.BaseElasticSearchData; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; + import java.lang.reflect.ParameterizedType; import java.util.ArrayList; import java.util.List; +import java.util.Optional; /** * {@code BaseElasticSearchClient} is the base class for all ElasticSearch clients. @@ -58,10 +65,41 @@ private void initialize() { * @return the list of results */ List renderResults(SearchResponse response) { - List results = new ArrayList<>(); - for (Hit hit : response.hits().hits()) { - results.add(hit.source()); - } - return results; + return Optional.ofNullable(response) + .map(SearchResponse::hits) + .map(HitsMetadata::hits) + .orElseGet(ArrayList::new) + .stream() + .map(Hit::source) + .toList(); + } + + /** + * Render the page results from the search response. + * + * @param response the search response + * @param page the page number + * @param size the page size + * @return the page of results + */ + Page renderPageResults(SearchResponse response, int page, int size) { + var hitsMetadata = Optional.ofNullable(response) + .map(SearchResponse::hits) + .orElse(null); + + long totalHits = Optional.ofNullable(hitsMetadata) + .map(HitsMetadata::total) + .map(TotalHits::value) + .orElse(0L); + + List results = Optional.ofNullable(hitsMetadata) + .map(HitsMetadata::hits) + .orElseGet(ArrayList::new) + .stream() + .map(Hit::source) + .toList(); + + var pageable = PageRequest.of(page, size); + return new PageImpl<>(results, pageable, totalHits); } } diff --git a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java index 25eaa18bd..453af0212 100644 --- a/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java +++ b/client/synapse-client-elasticsearch/src/main/java/io/americanexpress/synapse/client/elasticsearch/client/BaseReadElasticSearchDocumentSearchClient.java @@ -4,7 +4,7 @@ import co.elastic.clients.elasticsearch.core.GetResponse; import io.americanexpress.synapse.client.elasticsearch.model.BaseElasticSearchData; import java.io.IOException; -import java.util.List; +import org.springframework.data.domain.Page; /** * {@code BaseReadElasticSearchDocumentSearchClient} reads a document in ElasticSearch. @@ -44,13 +44,16 @@ public T findById(String id) throws IOException { * @return the list of documents * @throws IOException if an error occurs while finding the documents */ - public List findAll() throws IOException { + public Page findAll(int page, int size) throws IOException { var response = elasticsearchClient.search(s -> s - .index(indexName) - .query(q -> q.matchAll(m -> m)), + .index(indexName) + .query(q -> q.matchAll(m -> m)) + .from(page) + .size(size), this.documentType ); - return renderResults(response); + + return renderPageResults(response, page, size); } /** @@ -60,16 +63,18 @@ public List findAll() throws IOException { * @param value the value * @throws IOException if an error occurs while finding the documents */ - public List searchByKey(String key, String value) throws IOException { + public Page searchByKey(String key, String value, int page, int size) throws IOException { var response = elasticsearchClient.search(s -> s .index(indexName) .query(q -> q.match(t -> t .field(key) .query(value) - ) - ), this.documentType + )) + .from(page) + .size(size), this.documentType ); - return renderResults(response); + + return renderPageResults(response, page, size); } /**