Skip to content

Commit

Permalink
Increase elasticsearch-rest-client test coverage
Browse files Browse the repository at this point in the history
Fixes #5737
Fixes #6163
Fixes #6164
  • Loading branch information
jamesnetherton committed Jun 10, 2024
1 parent 525b348 commit d4633f3
Show file tree
Hide file tree
Showing 11 changed files with 799 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-elasticsearch-rest-client-deployment</artifactId>
<artifactId>quarkus-elasticsearch-rest-client-common-deployment</artifactId>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion extensions-jvm/elasticsearch-rest-client/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-elasticsearch-rest-client</artifactId>
<artifactId>quarkus-elasticsearch-rest-client-common</artifactId>
</dependency>
</dependencies>

Expand Down
48 changes: 48 additions & 0 deletions integration-tests-jvm/elasticsearch-rest-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,26 @@
<description>Integration tests for Camel Quarkus Elasticsearch Low level Rest Client extension</description>

<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-elasticsearch-rest-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down Expand Up @@ -67,6 +79,16 @@
<artifactId>quarkus-junit4-mock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-tests-support-certificate-generator</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand All @@ -79,6 +101,19 @@
</activation>
<dependencies>
<!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-elasticsearch-rest-client-deployment</artifactId>
Expand All @@ -92,6 +127,19 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>
</profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.elasticsearch.rest.client.it;

import java.util.Objects;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection(fields = false)
public class Document {
private String id;
private String value;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Document document = (Document) o;
return Objects.equals(id, document.id) && Objects.equals(value, document.value);
}

@Override
public int hashCode() {
return Objects.hash(id, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,147 @@
*/
package org.apache.camel.quarkus.component.elasticsearch.rest.client.it;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.jboss.logging.Logger;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.component.elasticsearch.rest.client.ElasticSearchRestClientConstant;
import org.elasticsearch.client.ResponseException;

@Path("/elasticsearch-rest-client")
@ApplicationScoped
public class ElasticsearchRestClientResource {

private static final Logger LOG = Logger.getLogger(ElasticsearchRestClientResource.class);

private static final String COMPONENT_ELASTICSEARCH_REST_CLIENT = "elasticsearch-rest-client";
@Inject
CamelContext context;
private static final String HEADER_COMPONENT = "component";

@Inject
ProducerTemplate producerTemplate;
FluentProducerTemplate fluentProducerTemplate;

@Path("/load/component/elasticsearch-rest-client")
@Path("/get")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getData(@QueryParam("indexName") String indexName, @QueryParam("indexId") String indexId) {
try {
Document document = fluentProducerTemplate.to("direct:get")
.withHeader(ElasticSearchRestClientConstant.INDEX_NAME, indexName)
.withHeader(ElasticSearchRestClientConstant.ID, indexId)
.request(Document.class);
return Response.ok().entity(document).build();
} catch (CamelExecutionException e) {
if (e.getCause() instanceof ResponseException responseException) {
return Response.status(responseException.getResponse().getStatusLine().getStatusCode()).build();
}
}
return Response.serverError().build();
}

@Path("/index/create")
@POST
@Produces(MediaType.TEXT_PLAIN)
public Response loadComponentElasticsearchRestClient() throws Exception {
/* This is an autogenerated test */
if (context.getComponent(COMPONENT_ELASTICSEARCH_REST_CLIENT) != null) {
return Response.ok().build();
public Response createIndex(
@QueryParam("indexName") String indexName,
String settings) throws Exception {

Map<String, Object> headers = new HashMap<>();
headers.put(ElasticSearchRestClientConstant.INDEX_NAME, indexName);
if (settings != null) {
headers.put(ElasticSearchRestClientConstant.INDEX_SETTINGS, settings);
}
LOG.warnf("Could not load [%s] from the Camel context", COMPONENT_ELASTICSEARCH_REST_CLIENT);
return Response.status(500, COMPONENT_ELASTICSEARCH_REST_CLIENT + " could not be loaded from the Camel context")

boolean success = fluentProducerTemplate.to("direct:createIndex")
.withHeaders(headers)
.request(boolean.class);

return Response.created(new URI("https://camel.apache.org/"))
.entity(success)
.build();
}

@Path("/index/{indexName}")
@GET
@Path("/index")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response createIndex(@PathParam("indexName") String indexName) throws ExecutionException, InterruptedException {
String endpointUri = String.format(
"elasticsearch-rest-client:my-cluster?operation=CREATE_INDEX&indexName=%s", indexName);
CompletableFuture<Boolean> ack = producerTemplate.asyncRequestBody(endpointUri, null, Boolean.class);
Boolean response = ack.get();

if (response) {
return Response.ok().build();
public Response indexData(
@QueryParam("indexName") String indexName,
@QueryParam("indexId") String indexId,
Document document) throws Exception {

Map<String, Object> headers = new HashMap<>();
headers.put(ElasticSearchRestClientConstant.INDEX_NAME, indexName);
if (indexId != null) {
headers.put(ElasticSearchRestClientConstant.ID, indexId);
}
return Response.status(500, "Could not create index")
.build();

String result = fluentProducerTemplate.to("direct:index")
.withBody(document)
.withHeaders(headers)
.request(String.class);

if (indexId != null) {
return Response.ok(result).build();
} else {
return Response.created(new URI("https://camel.apache.org/"))
.entity(result)
.build();
}
}

@Path("/delete")
@DELETE
@Produces(MediaType.TEXT_PLAIN)
public Response deleteData(@QueryParam("indexName") String indexName, @QueryParam("indexId") String indexId) {
fluentProducerTemplate.to("direct:delete")
.withBody(indexId)
.withHeader(ElasticSearchRestClientConstant.INDEX_NAME, indexName)
.withHeader(ElasticSearchRestClientConstant.ID, indexId)
.request();

return Response.noContent().build();
}

@Path("/delete/index")
@DELETE
@Produces(MediaType.TEXT_PLAIN)
public Response deleteIndexData(@QueryParam("indexName") String indexName) {
boolean result = fluentProducerTemplate.to("direct:deleteIndex")
.withHeader(ElasticSearchRestClientConstant.INDEX_NAME, indexName)
.request(boolean.class);
return Response.ok(result).build();
}

@Path("/search")
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response search(@QueryParam("indexName") String indexName, Map<String, String> criteria) {
String result = fluentProducerTemplate.to("direct:search")
.withHeader(ElasticSearchRestClientConstant.INDEX_NAME, indexName)
.withBody(criteria)
.request(String.class);
return Response.ok(result).build();
}

@Path("/search")
@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public Response searchByJSON(@QueryParam("indexName") String indexName, String query) {
String result = fluentProducerTemplate.to("direct:search")
.withHeader(ElasticSearchRestClientConstant.INDEX_NAME, indexName)
.withHeader(ElasticSearchRestClientConstant.SEARCH_QUERY, query)
.request(String.class);
return Response.ok(result).build();
}
}
Loading

0 comments on commit d4633f3

Please sign in to comment.