Skip to content

Commit

Permalink
Merge pull request #1401 from rsvoboda/elasticsearch-rest-client
Browse files Browse the repository at this point in the history
Test coverage for elasticsearch-rest-client
  • Loading branch information
rsvoboda authored Sep 8, 2023
2 parents acf7a59 + a5108c7 commit 58bd475
Show file tree
Hide file tree
Showing 16 changed files with 473 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,10 @@ Reactive equivalent of `nosql-db/mongodb`. Uses reactive ReactiveMongoClient (wi

Provides some coverage similar to `infinispan-client` module, but with focus on connecting to local cluster and Dev mode and not on working with OpenShift

### `nosql-db/elasticsearch`

Provides coverage for quarkus-elasticsearch-rest-client

### `websockets/quarkus-websockets`
Coverage for sending messages over websockets

Expand Down
27 changes: 27 additions & 0 deletions nosql-db/elasticsearch/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.quarkus.ts.qe</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<artifactId>elasticsearch</artifactId>
<packaging>jar</packaging>
<name>Quarkus QE TS: NoSQL Database: Elasticsearch</name>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-elasticsearch-rest-client</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.ts.elasticsearch;

import java.util.Date;
import java.util.List;
import java.util.Objects;

public class DataTypes {
public String id;
public String name;
public List<Fruit> fruits;
public Date date;
public float floatNum;
public double doubleNum;

@Override
public String toString() {
return "DataTypes{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", fruits=" + fruits +
", date=" + date +
", floatNum=" + floatNum +
", doubleNum=" + doubleNum +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
DataTypes dataTypes = (DataTypes) o;
return Float.compare(floatNum, dataTypes.floatNum) == 0 && Double.compare(doubleNum, dataTypes.doubleNum) == 0
&& Objects.equals(id, dataTypes.id) && Objects.equals(name, dataTypes.name)
&& Objects.equals(fruits, dataTypes.fruits) && Objects.equals(date, dataTypes.date);
}

@Override
public int hashCode() {
return Objects.hash(id, name, fruits, date, floatNum, doubleNum);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.ts.elasticsearch;

import java.io.IOException;

import jakarta.inject.Inject;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;

@Path("/data-types")
public class DataTypesResource {

@Inject
DataTypesService dataTypesService;

@POST
public DataTypes indexAndGet(DataTypes dataTypes) throws IOException {
return dataTypesService.indexAndGet(dataTypes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.ts.elasticsearch;

import java.io.IOException;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;

import io.vertx.core.json.JsonObject;

@ApplicationScoped
public class DataTypesService {
@Inject
RestClient restClient;

public DataTypes indexAndGet(DataTypes dataTypes) throws IOException {
Request request = new Request(
"PUT",
"/foos/_doc/" + dataTypes.id);
request.setJsonEntity(JsonObject.mapFrom(dataTypes).toString());
restClient.performRequest(request);

request = new Request(
"GET",
"/foos/_doc/" + dataTypes.id);
Response response = restClient.performRequest(request);
String responseBody = EntityUtils.toString(response.getEntity());

JsonObject json = new JsonObject(responseBody);
DataTypes dataTypesRet = json.getJsonObject("_source").mapTo(DataTypes.class);
return dataTypesRet;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.ts.elasticsearch;

import java.util.Objects;
import java.util.UUID;

public class Fruit {
public String id;
public String name;
public String color;

public Fruit(String name, String color) {
this.id = UUID.randomUUID().toString();
this.name = name;
this.color = color;
}

@Override
public String toString() {
return "Fruit{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", color='" + color + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Fruit fruit = (Fruit) o;
return Objects.equals(id, fruit.id) && Objects.equals(name, fruit.name) && Objects.equals(color, fruit.color);
}

@Override
public int hashCode() {
return Objects.hash(id, name, color);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.quarkus.ts.elasticsearch;

import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.UUID;

import jakarta.inject.Inject;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;

import org.jboss.resteasy.reactive.RestQuery;

@Path("/fruits")
public class FruitResource {

@Inject
FruitService fruitService;

@POST
public Response index(Fruit fruit) throws IOException {
if (fruit.id == null) {
fruit.id = UUID.randomUUID().toString();
}
fruitService.index(fruit);
return Response.created(URI.create("/fruits/" + fruit.id)).build();
}

@GET
@Path("/{id}")
public Fruit get(String id) throws IOException {
return fruitService.get(id);
}

@DELETE
@Path("/{id}")
public void delete(String id) throws IOException {
fruitService.delete(id);
}

@GET
@Path("/search")
public List<Fruit> search(@RestQuery String name, @RestQuery String color) throws IOException {
if (name != null) {
return fruitService.searchByName(name);
} else if (color != null) {
return fruitService.searchByColor(color);
} else {
throw new BadRequestException("Should provide name or color query parameter");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.quarkus.ts.elasticsearch;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

@ApplicationScoped
public class FruitService {
@Inject
RestClient restClient;

public void index(Fruit fruit) throws IOException {
Request request = new Request(
"PUT",
"/fruits/_doc/" + fruit.id);
request.setJsonEntity(JsonObject.mapFrom(fruit).toString());
restClient.performRequest(request);
}

public Fruit get(String id) throws IOException {
Request request = new Request(
"GET",
"/fruits/_doc/" + id);
Response response = restClient.performRequest(request);
String responseBody = EntityUtils.toString(response.getEntity());
JsonObject json = new JsonObject(responseBody);
return json.getJsonObject("_source").mapTo(Fruit.class);
}

public void delete(String id) throws IOException {
Request request = new Request(
"DELETE",
"/fruits/_doc/" + id);
restClient.performRequest(request);
}

public List<Fruit> searchByColor(String color) throws IOException {
return search("color", color);
}

public List<Fruit> searchByName(String name) throws IOException {
return search("name", name);
}

private List<Fruit> search(String term, String match) throws IOException {
Request request = new Request(
"GET",
"/fruits/_search");
//construct a JSON query like {"query": {"match": {"<term>": "<match"}}
JsonObject termJson = new JsonObject().put(term, match);
JsonObject matchJson = new JsonObject().put("match", termJson);
JsonObject queryJson = new JsonObject().put("query", matchJson);
request.setJsonEntity(queryJson.encode());
Response response = restClient.performRequest(request);
String responseBody = EntityUtils.toString(response.getEntity());

JsonObject json = new JsonObject(responseBody);
JsonArray hits = json.getJsonObject("hits").getJsonArray("hits");
List<Fruit> results = new ArrayList<>(hits.size());
for (int i = 0; i < hits.size(); i++) {
JsonObject hit = hits.getJsonObject(i);
Fruit fruit = hit.getJsonObject("_source").mapTo(Fruit.class);
results.add(fruit);
}
return results;
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.quarkus.ts.elasticsearch;

import static io.restassured.RestAssured.given;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.containsString;

import java.util.concurrent.TimeUnit;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Test;

import io.quarkus.test.bootstrap.RestService;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.DevModeQuarkusApplication;
import io.restassured.http.ContentType;

@QuarkusScenario
public class DevModeElasticsearchIT {

@DevModeQuarkusApplication()
static RestService devModeApp = new RestService();

@Test
void dataCreatedAndAccessible() {
devModeApp.given()
.contentType(ContentType.JSON)
.body("{\"name\": \"banány\", \"color\": \"žlutá\"}")
.when()
.post("fruits")
.then()
.statusCode(HttpStatus.SC_CREATED);

await().ignoreExceptions().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
devModeApp.given()
.when()
.get("fruits/search?color=žlutá")
.then()
.statusCode(HttpStatus.SC_OK)
.body(containsString("banány"));
});
}
}
Loading

0 comments on commit 58bd475

Please sign in to comment.