-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1401 from rsvoboda/elasticsearch-rest-client
Test coverage for elasticsearch-rest-client
- Loading branch information
Showing
16 changed files
with
473 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
43 changes: 43 additions & 0 deletions
43
nosql-db/elasticsearch/src/main/java/io/quarkus/ts/elasticsearch/DataTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
nosql-db/elasticsearch/src/main/java/io/quarkus/ts/elasticsearch/DataTypesResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
nosql-db/elasticsearch/src/main/java/io/quarkus/ts/elasticsearch/DataTypesService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
nosql-db/elasticsearch/src/main/java/io/quarkus/ts/elasticsearch/Fruit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
nosql-db/elasticsearch/src/main/java/io/quarkus/ts/elasticsearch/FruitResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
nosql-db/elasticsearch/src/main/java/io/quarkus/ts/elasticsearch/FruitService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
42 changes: 42 additions & 0 deletions
42
nosql-db/elasticsearch/src/test/java/io/quarkus/ts/elasticsearch/DevModeElasticsearchIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}); | ||
} | ||
} |
Oops, something went wrong.