Add ElasticSearch to chats service
This commit is contained in:
parent
cae18e206d
commit
2922c7b34a
18
.editorconfig
Normal file
18
.editorconfig
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
indent_size = 2
|
||||||
|
indent_style = tab
|
||||||
|
insert_final_newline = true
|
||||||
|
max_line_length = 120
|
||||||
|
tab_width = 2
|
||||||
|
|
||||||
|
[{*.java,*.jsh}]
|
||||||
|
indent_size = 4
|
||||||
|
tab_width = 4
|
||||||
|
|
||||||
|
[{*.markdown,*.md}]
|
||||||
|
indent_size = 4
|
||||||
|
tab_width = 4
|
@ -16,6 +16,7 @@
|
|||||||
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
|
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
|
||||||
<quarkus.platform.version>2.4.1.Final</quarkus.platform.version>
|
<quarkus.platform.version>2.4.1.Final</quarkus.platform.version>
|
||||||
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
|
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
|
||||||
|
<elasticsearch-reactive.version>0.2.0</elasticsearch-reactive.version>
|
||||||
</properties>
|
</properties>
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
@ -48,11 +49,12 @@
|
|||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.quarkus</groupId>
|
<groupId>io.quarkus</groupId>
|
||||||
<artifactId>quarkus-reactive-pg-client</artifactId>
|
<artifactId>quarkus-smallrye-openapi</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- We are using RESTEasy Reactive in this quickstart -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.quarkus</groupId>
|
<groupId>io.quarkus</groupId>
|
||||||
<artifactId>quarkus-smallrye-openapi</artifactId>
|
<artifactId>quarkus-resteasy-reactive</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.quarkus</groupId>
|
<groupId>io.quarkus</groupId>
|
||||||
@ -62,17 +64,23 @@
|
|||||||
<groupId>io.quarkus</groupId>
|
<groupId>io.quarkus</groupId>
|
||||||
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
|
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- Hibernate Reactive uses the reactive-pg-client with PostgreSQL under the hood -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.quarkus</groupId>
|
||||||
|
<artifactId>quarkus-reactive-pg-client</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.quarkus</groupId>
|
<groupId>io.quarkus</groupId>
|
||||||
<artifactId>quarkus-hibernate-validator</artifactId>
|
<artifactId>quarkus-hibernate-validator</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.quarkus</groupId>
|
<groupId>org.elasticsearch.client</groupId>
|
||||||
<artifactId>quarkus-arc</artifactId>
|
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.quarkus</groupId>
|
<groupId>io.quarkiverse.quarkus-elasticsearch-reactive</groupId>
|
||||||
<artifactId>quarkus-resteasy-reactive</artifactId>
|
<artifactId>quarkus-elasticsearch-reactive</artifactId>
|
||||||
|
<version>${elasticsearch-reactive.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -90,6 +98,21 @@
|
|||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>testcontainers</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>elasticsearch</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
|||||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;
|
import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;
|
||||||
|
import io.smallrye.mutiny.Uni;
|
||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
import javax.persistence.Cacheable;
|
import javax.persistence.Cacheable;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
@ -13,7 +14,7 @@ import javax.persistence.Id;
|
|||||||
import javax.validation.constraints.Max;
|
import javax.validation.constraints.Max;
|
||||||
import javax.validation.constraints.Pattern;
|
import javax.validation.constraints.Pattern;
|
||||||
import javax.validation.constraints.Positive;
|
import javax.validation.constraints.Positive;
|
||||||
import org.hibernate.validator.constraints.Length;
|
import javax.validation.constraints.Size;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Cacheable
|
@Cacheable
|
||||||
@ -28,9 +29,9 @@ public class Chat extends PanacheEntityBase {
|
|||||||
public Long id;
|
public Long id;
|
||||||
@Column(length = 128)
|
@Column(length = 128)
|
||||||
public String name;
|
public String name;
|
||||||
@Length(message = "Username length is not valid", min = 5)
|
@Size(message = "Username length is not valid", min = 5)
|
||||||
@Column(length = 48)
|
@Column(length = 48)
|
||||||
@Length(message = "Username must not be an empty string", min = 1, max = 12 + 32)
|
@Size(message = "Username must not be an empty string", min = 1, max = 12 + 32)
|
||||||
@Pattern(message = "Username contains invalid characters", regexp = "^(?:[a-zA-Z\\d][_]?)+$")
|
@Pattern(message = "Username contains invalid characters", regexp = "^(?:[a-zA-Z\\d][_]?)+$")
|
||||||
@Pattern(message = "Username is not valid", regexp = "^(?:translation_|mv_)?[a-zA-Z]([a-zA-Z_\\d]){1,30}[a-zA-Z\\d]$")
|
@Pattern(message = "Username is not valid", regexp = "^(?:translation_|mv_)?[a-zA-Z]([a-zA-Z_\\d]){1,30}[a-zA-Z\\d]$")
|
||||||
public String username;
|
public String username;
|
||||||
@ -55,4 +56,13 @@ public class Chat extends PanacheEntityBase {
|
|||||||
.add("status=" + status)
|
.add("status=" + status)
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Uni<Chat> findUsername(String username) {
|
||||||
|
if (username == null) {
|
||||||
|
throw new NullPointerException("Username must not be null");
|
||||||
|
} else if (username.isBlank()) {
|
||||||
|
throw new NullPointerException("Username must not be blank");
|
||||||
|
}
|
||||||
|
return find("from Chat where username = ?1", username).firstResult();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,26 +7,26 @@ import javax.enterprise.context.ApplicationScoped;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class ChatsService {
|
public class ChatEventBusService {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
EventBus bus;
|
EventBus bus;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ChatResource chatResource;
|
ChatService chatService;
|
||||||
|
|
||||||
@ConsumeEvent(value = "chats.list")
|
@ConsumeEvent(value = "chats.list")
|
||||||
public void listChats(Message<Void> msg) {
|
public void listChats(Message<Void> msg) {
|
||||||
chatResource.listSessions().collect().asList().subscribe().with(msg::reply);
|
chatService.listAll().collect().asList().subscribe().with(msg::reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConsumeEvent(value = "chats.get")
|
@ConsumeEvent(value = "chats.get")
|
||||||
public void get(Message<Long> msg) {
|
public void get(Message<Long> msg) {
|
||||||
chatResource.get(msg.body()).subscribe().with(msg::reply);
|
chatService.get(msg.body()).subscribe().with(msg::reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConsumeEvent(value = "chats.update")
|
@ConsumeEvent(value = "chats.update")
|
||||||
public void update(Message<Chat> msg) {
|
public void update(Message<Chat> msg) {
|
||||||
chatResource.update(msg.body().id, msg.body()).subscribe().with(msg::reply);
|
chatService.update(msg.body().id, msg.body()).subscribe().with(msg::reply);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,22 +0,0 @@
|
|||||||
package io.volvox.chats;
|
|
||||||
|
|
||||||
import io.quarkus.hibernate.reactive.panache.PanacheRepositoryBase;
|
|
||||||
import io.smallrye.mutiny.Uni;
|
|
||||||
import javax.enterprise.context.ApplicationScoped;
|
|
||||||
import org.hibernate.annotations.NamedQueries;
|
|
||||||
import org.hibernate.annotations.NamedQuery;
|
|
||||||
|
|
||||||
@ApplicationScoped
|
|
||||||
@NamedQueries({
|
|
||||||
@NamedQuery(name = "Chat.getByName", query = "from Chat where name = ?1"),
|
|
||||||
@NamedQuery(name = "Chat.getByUsername", query = "from Chat where username = ?1"),
|
|
||||||
@NamedQuery(name = "Chat.countByStatus", query = "select count(*) from Chat p where p.status = :status"),
|
|
||||||
@NamedQuery(name = "Chat.updateStatusById", query = "update Chat p set p.status = :status where p.id = :id"),
|
|
||||||
@NamedQuery(name = "Chat.deleteById", query = "delete from Chat p where p.id = ?1")
|
|
||||||
})
|
|
||||||
public class ChatRepository implements PanacheRepositoryBase<Chat, Long> {
|
|
||||||
|
|
||||||
public Uni<Chat> findByUsername(String username) {
|
|
||||||
return find("#Chat.getByUsername", username).firstResult();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +1,13 @@
|
|||||||
package io.volvox.chats;
|
package io.volvox.chats;
|
||||||
|
|
||||||
import io.quarkus.hibernate.reactive.panache.Panache;
|
|
||||||
import io.smallrye.mutiny.Multi;
|
import io.smallrye.mutiny.Multi;
|
||||||
import io.smallrye.mutiny.Uni;
|
import io.smallrye.mutiny.Uni;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import javax.enterprise.context.ApplicationScoped;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.DELETE;
|
import javax.ws.rs.DELETE;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.NotFoundException;
|
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
import javax.ws.rs.PUT;
|
import javax.ws.rs.PUT;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
@ -18,65 +17,52 @@ import javax.ws.rs.core.MediaType;
|
|||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
@Path("/chats")
|
@Path("/chats")
|
||||||
|
@ApplicationScoped
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
public class ChatResource {
|
public class ChatResource {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ChatRepository chatRepository;
|
ChatService chatService;
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
public Multi<Chat> listSessions() {
|
public Multi<Chat> list() {
|
||||||
return chatRepository.streamAll();
|
return chatService.listAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/{id}")
|
@Path("/{id}")
|
||||||
public Uni<Chat> get(@PathParam("id") Long id) {
|
public Uni<Chat> get(@PathParam("id") Long id) {
|
||||||
return chatRepository.findById(id);
|
return chatService.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
public Uni<Response> create(Chat chat) {
|
public Uni<Response> create(Chat chat) {
|
||||||
return Panache.withTransaction(() -> chatRepository.persist(chat))
|
return chatService.create(chat)
|
||||||
.onItem().transform(inserted -> Response.created(URI.create("/chats/" + chat.id)).build());
|
.onItem().transform(inserted -> Response.created(URI.create("/chats/" + chat.id)).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
@Path("/{id}")
|
@Path("/{id}")
|
||||||
public Uni<Chat> update(@PathParam("id") Long id, Chat chat) {
|
public Uni<Chat> update(@PathParam("id") Long id, Chat chat) {
|
||||||
// Find chat by id
|
return chatService.update(id, chat);
|
||||||
return Panache.withTransaction(() -> chatRepository.findById(id)
|
|
||||||
.flatMap(entity -> {
|
|
||||||
if (entity == null) {
|
|
||||||
// Persist the chat if not found
|
|
||||||
return chatRepository.persist(chat);
|
|
||||||
} else {
|
|
||||||
// Update all fields
|
|
||||||
entity.name = chat.name;
|
|
||||||
// Return the updated item
|
|
||||||
return Uni.createFrom().item(entity);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@Path("/{id}")
|
@Path("/{id}")
|
||||||
public Uni<Void> delete(@PathParam("id") Long id) {
|
public Uni<Void> delete(@PathParam("id") Long id) {
|
||||||
return Panache.withTransaction(() -> chatRepository.findById(id)
|
return chatService.delete(id);
|
||||||
.onItem().ifNull().failWith(NotFoundException::new)
|
|
||||||
.flatMap(chatRepository::delete));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/search/{username}")
|
@Path("/by-username/{username}")
|
||||||
public Uni<Chat> search(@PathParam("username") String username) {
|
public Uni<Chat> resolveByUsername(@PathParam("username") String username) {
|
||||||
return chatRepository.findByUsername(username);
|
return chatService.resolveByUsername(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/count")
|
@Path("/count")
|
||||||
public Uni<Long> count() {
|
public Uni<Long> count() {
|
||||||
return chatRepository.count();
|
return chatService.count();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
140
service-chats/src/main/java/io/volvox/chats/ChatService.java
Normal file
140
service-chats/src/main/java/io/volvox/chats/ChatService.java
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
package io.volvox.chats;
|
||||||
|
|
||||||
|
import io.quarkus.hibernate.reactive.panache.Panache;
|
||||||
|
import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;
|
||||||
|
import io.reactiverse.elasticsearch.client.mutiny.RestHighLevelClient;
|
||||||
|
import io.smallrye.mutiny.Multi;
|
||||||
|
import io.smallrye.mutiny.Uni;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import javax.enterprise.context.ApplicationScoped;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.ws.rs.NotFoundException;
|
||||||
|
import org.elasticsearch.action.delete.DeleteRequest;
|
||||||
|
import org.elasticsearch.action.delete.DeleteResponse;
|
||||||
|
import org.elasticsearch.action.get.GetRequest;
|
||||||
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
|
import org.elasticsearch.client.RequestOptions;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.elasticsearch.search.SearchHit;
|
||||||
|
import org.elasticsearch.search.SearchHits;
|
||||||
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class ChatService {
|
||||||
|
@Inject
|
||||||
|
RestHighLevelClient restHighLevelClient;
|
||||||
|
|
||||||
|
private Uni<IndexResponse> updateIndex(Chat chat) {
|
||||||
|
var request = new IndexRequest("chats");
|
||||||
|
request.id(ChatId.toString(chat.id));
|
||||||
|
request.source(JsonObject.mapFrom(chat).toString(), XContentType.JSON);
|
||||||
|
return restHighLevelClient.indexAsync(request, RequestOptions.DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Uni<DeleteResponse> removeFromIndex(Long id) {
|
||||||
|
var request = new DeleteRequest("chats");
|
||||||
|
request.id(ChatId.toString(id));
|
||||||
|
return restHighLevelClient.deleteAsync(request, RequestOptions.DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Chat> get(Long id) {
|
||||||
|
return Chat.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Chat> getFromIndex(Long id) {
|
||||||
|
GetRequest getRequest = new GetRequest("chats", ChatId.toString(id));
|
||||||
|
return restHighLevelClient.getAsync(getRequest, RequestOptions.DEFAULT)
|
||||||
|
.map(getResponse -> {
|
||||||
|
if (getResponse.isExists()) {
|
||||||
|
String sourceAsString = getResponse.getSourceAsString();
|
||||||
|
JsonObject json = new JsonObject(sourceAsString);
|
||||||
|
return json.mapTo(Chat.class);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Void> create(Chat chat) {
|
||||||
|
return Panache.withTransaction(() -> Chat.persist(chat).replaceWith(updateIndex(chat)))
|
||||||
|
.replaceWithVoid();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Void> delete(Long id) {
|
||||||
|
return Panache.withTransaction(() -> Chat.findById(id)
|
||||||
|
.onItem().ifNull().failWith(NotFoundException::new)
|
||||||
|
.flatMap(PanacheEntityBase::delete)
|
||||||
|
.replaceWith(removeFromIndex(id))
|
||||||
|
.onItem().transform(DeleteResponse::status)
|
||||||
|
.replaceWithVoid()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Chat> update(Long id, Chat chat) {
|
||||||
|
if (chat.id != null && id != null && !Objects.equals(chat.id, id)) {
|
||||||
|
throw new IllegalArgumentException("Chat id is different than id");
|
||||||
|
}
|
||||||
|
// Find chat by id
|
||||||
|
return Panache.withTransaction(() -> Chat.<Chat>findById(id)
|
||||||
|
.flatMap(entity -> {
|
||||||
|
if (entity == null) {
|
||||||
|
// Persist the chat if not found
|
||||||
|
return Chat.persist(chat)
|
||||||
|
// Return the chat
|
||||||
|
.replaceWith(chat);
|
||||||
|
} else {
|
||||||
|
// Update all fields
|
||||||
|
entity.name = chat.name;
|
||||||
|
// Return the updated item
|
||||||
|
return Uni.createFrom().item(entity);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// Update index
|
||||||
|
.onItem().transformToUni(updatedChat -> updateIndex(updatedChat).replaceWith(updatedChat))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<List<Chat>> searchByUsername(String username) {
|
||||||
|
return search("username", username);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<List<Chat>> searchByName(String name) {
|
||||||
|
return search("name", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Chat> resolveByUsername(String username) {
|
||||||
|
return Chat.findUsername(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Uni<List<Chat>> search(String term, String match) {
|
||||||
|
SearchRequest searchRequest = new SearchRequest("chats");
|
||||||
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||||
|
searchSourceBuilder.query(QueryBuilders.matchQuery(term, match));
|
||||||
|
searchRequest.source(searchSourceBuilder);
|
||||||
|
|
||||||
|
return restHighLevelClient.searchAsync(searchRequest, RequestOptions.DEFAULT)
|
||||||
|
.map(searchResponse -> {
|
||||||
|
SearchHits hits = searchResponse.getHits();
|
||||||
|
List<Chat> results = new ArrayList<>(hits.getHits().length);
|
||||||
|
for (SearchHit hit : hits.getHits()) {
|
||||||
|
String sourceAsString = hit.getSourceAsString();
|
||||||
|
JsonObject json = new JsonObject(sourceAsString);
|
||||||
|
results.add(json.mapTo(Chat.class));
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public Multi<Chat> listAll() {
|
||||||
|
return Chat.streamAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Uni<Long> count() {
|
||||||
|
return Chat.count();
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,6 @@
|
|||||||
quarkus.http.port=8282
|
quarkus.http.port=8282
|
||||||
|
# we don't need SSL here, let's disable it to have a more compact native executable
|
||||||
|
quarkus.ssl.native=false
|
||||||
|
|
||||||
%prod.quarkus.datasource.db-kind=postgresql
|
%prod.quarkus.datasource.db-kind=postgresql
|
||||||
%prod.quarkus.datasource.username=quarkus_test
|
%prod.quarkus.datasource.username=quarkus_test
|
||||||
@ -10,3 +12,6 @@ quarkus.hibernate-orm.sql-load-script=import.sql
|
|||||||
|
|
||||||
# Reactive config
|
# Reactive config
|
||||||
%prod.quarkus.datasource.reactive.url=vertx-reactive:postgresql://localhost/quarkus_test
|
%prod.quarkus.datasource.reactive.url=vertx-reactive:postgresql://localhost/quarkus_test
|
||||||
|
|
||||||
|
quarkus.elasticsearch.health.enabled=false
|
||||||
|
quarkus.elasticsearch.reactive.health.enabled=true
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
INSERT INTO Chat(id, name, username, status) VALUES (9007199256673076, 'My Supergroup', 'mysupergroup', 1);
|
INSERT INTO chat(id, name, username, status) VALUES (9007199256673076, 'My Supergroup', 'mysupergroup', 1);
|
||||||
INSERT INTO Chat(id, name, username, status) VALUES (777000, 'Telegram', 'telegram', null);
|
INSERT INTO chat(id, name, username, status) VALUES (777000, 'Telegram', 'telegram', null);
|
||||||
INSERT INTO Chat(id, name, username, status) VALUES (4503599627464345, 'School group', null, 1);
|
INSERT INTO chat(id, name, username, status) VALUES (4503599627464345, 'School group', null, 1);
|
||||||
INSERT INTO Chat(id, name, username, status) VALUES (4503599627382355, 'Old school group', null, 0);
|
INSERT INTO chat(id, name, username, status) VALUES (4503599627382355, 'Old school group', null, 0);
|
||||||
|
@ -2,22 +2,22 @@ package io.volvox.chats;
|
|||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.RestAssured.given;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.emptyString;
|
import static org.hamcrest.Matchers.emptyString;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
import io.quarkus.hibernate.reactive.panache.Panache;
|
import io.quarkus.hibernate.reactive.panache.Panache;
|
||||||
|
import io.quarkus.test.common.QuarkusTestResource;
|
||||||
import io.quarkus.test.junit.QuarkusTest;
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
import io.restassured.response.Response;
|
import io.restassured.response.Response;
|
||||||
import javax.inject.Inject;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@QuarkusTest
|
@QuarkusTest
|
||||||
|
@QuarkusTestResource(ElasticsearchContainerTestResource.class)
|
||||||
public class ChatsEndpointTest {
|
public class ChatsEndpointTest {
|
||||||
|
|
||||||
@Inject
|
|
||||||
ChatRepository chatRepository;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testListAllChats() {
|
public void testListAllChats() {
|
||||||
//List all, should have all 3 usernames the database has initially:
|
//List all, should have all 3 usernames the database has initially:
|
||||||
@ -123,8 +123,21 @@ public class ChatsEndpointTest {
|
|||||||
.statusCode(500);
|
.statusCode(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHealth() {
|
||||||
|
given()
|
||||||
|
.when()
|
||||||
|
.get("/q/health/ready")
|
||||||
|
.then()
|
||||||
|
.statusCode(200)
|
||||||
|
.contentType("application/json")
|
||||||
|
.body("status", is("UP"),
|
||||||
|
"checks.status", containsInAnyOrder("UP"),
|
||||||
|
"checks.name", containsInAnyOrder("Elasticsearch cluster health check"));
|
||||||
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void tearDown(){
|
public void tearDown(){
|
||||||
Panache.withTransaction(() -> chatRepository.deleteById(777234L)).await().indefinitely();
|
Panache.withTransaction(() -> Chat.deleteById(777234L)).await().indefinitely();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package io.volvox.chats;
|
||||||
|
|
||||||
|
import io.quarkus.test.junit.NativeImageTest;
|
||||||
|
|
||||||
|
@NativeImageTest
|
||||||
|
public class ChatsEndpointTestIT extends ChatsEndpointTest {
|
||||||
|
|
||||||
|
// Execute the same tests but in native mode.
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package io.volvox.chats;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.testcontainers.elasticsearch.ElasticsearchContainer;
|
||||||
|
import org.testcontainers.utility.DockerImageName;
|
||||||
|
|
||||||
|
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
|
||||||
|
|
||||||
|
public class ElasticsearchContainerTestResource implements QuarkusTestResourceLifecycleManager {
|
||||||
|
|
||||||
|
static ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(
|
||||||
|
DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch-oss")
|
||||||
|
.withTag("7.10.2"));
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> start() {
|
||||||
|
elasticsearchContainer.withEnv("action.auto_create_index", "true");
|
||||||
|
elasticsearchContainer.start();
|
||||||
|
return Map.of(
|
||||||
|
"quarkus.elasticsearch.hosts", elasticsearchContainer.getHttpHostAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
elasticsearchContainer.stop();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user