Skip to content

Commit

Permalink
Merge branch 'feature/hibernate-orm-panache' into develop
Browse files Browse the repository at this point in the history
Implementazione esempio di uso ORM con Hibernate-Panache + JAX-RS
Resources
  • Loading branch information
amusarra committed May 9, 2024
2 parents 254d54b + 04b5f60 commit 7eacb6d
Show file tree
Hide file tree
Showing 24 changed files with 927 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_via_maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ name: Build with Maven

on:
push:
branches: [ "main" ]
branches: [ "main", "develop" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "develop" ]

jobs:
build:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- main
- develop
pull_request:
types: [opened, synchronize, reopened]
jobs:
Expand Down
29 changes: 28 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added

- Aggiornamento versione Quarkus a 3.10.0
- Aggiunti i moduli Quarkus:
- io.quarkus:quarkus-hibernate-orm-panache
- io.quarkus:quarkus-jdbc-h2
- Aggiunte le classi di entità:
- it.dontesta.eventbus.orm.panache.entity.Horse
- it.dontesta.eventbus.orm.panache.entity.Owner
- Aggiunte le classi di repository:
- it.dontesta.eventbus.orm.panache.repository.HorseRepository
- it.dontesta.eventbus.orm.panache.repository.OwnerRepository
- Aggiunte le classi di risorse (JAX-RS)
- it.dontesta.eventbus.ws.resources.endpoint.repository.v1.HorseRepositoryResources
- it.dontesta.eventbus.ws.resources.endpoint.repository.v1.OwnerRepositoryResources
- Aggiunta la classe di Error Mapping (JAX-RS)
- it.dontesta.eventbus.ws.mappers.ErrorMapper
- Aggiunto il file `src/main/resources/load_data.sql` per l'inizializzazione del database H2
- Aggiunte le classi di test:
- it.dontesta.eventbus.orm.panache.repository.HorseRepositoryIntegrationTest
- it.dontesta.eventbus.orm.panache.repository.OwnerRepositoryIntegrationTest
- it.dontesta.eventbus.orm.panache.repository.PanacheRepositoryMockedTest
- it.dontesta.eventbus.ws.resources.endpoint.repository.v1.HorseRepositoryResourcesTest
- it.dontesta.eventbus.ws.resources.endpoint.repository.v1.OwnerRepositoryResourcesTest

### Changed
- Revisione del file di configurazione per l'uso di Hibernate ORM Panache

### Fixed
- Null Pointer Exception sul filtro JAX-RS per il tracking delle richieste quando il body della richiesta è vuoto
- Google Checkstyle violations

### Removed

Expand Down
35 changes: 21 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>it.dontesta.eventbus</groupId>
<artifactId>eventbus-logging-filter-jaxrs</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>

<name>eventbus-logging-filter-jaxrs</name>
<description>Event Bus Logging Filter JAX-RS</description>
Expand Down Expand Up @@ -44,7 +44,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.9.2</quarkus.platform.version>
<quarkus.platform.version>3.10.0</quarkus.platform.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.2.5</surefire-plugin.version>
<sonar.organization>amusarra-github</sonar.organization>
Expand Down Expand Up @@ -108,6 +108,14 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jacoco</artifactId>
Expand All @@ -118,6 +126,11 @@
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand Down Expand Up @@ -209,8 +222,7 @@
<goal>generateKeyPair</goal>
</goals>
<configuration>
<keystore>${project.build.outputDirectory}/${quarkus.http.ssl.certificate.key-store-file}
</keystore>
<keystore>${project.build.outputDirectory}/${quarkus.http.ssl.certificate.key-store-file}</keystore>
<dname>cn=${project.artifactId}, ou=${project.groupId}, o=dontesta.it, c=IT</dname>
<keypass>${quarkus.http.ssl.certificate.key-store-password}</keypass>
<storepass>${quarkus.http.ssl.certificate.key-store-password}</storepass>
Expand Down Expand Up @@ -241,15 +253,9 @@
</plugins>
</build>
<scm>
<connection>
scm:git:[email protected]:amusarra/eventbus-logging-filter-jaxrs.git
</connection>
<url>
scm:git:[email protected]:amusarra/eventbus-logging-filter-jaxrs.git
</url>
<developerConnection>
scm:git:[email protected]:amusarra/eventbus-logging-filter-jaxrs.git
</developerConnection>
<connection>scm:git:[email protected]:amusarra/eventbus-logging-filter-jaxrs.git</connection>
<url>scm:git:[email protected]:amusarra/eventbus-logging-filter-jaxrs.git</url>
<developerConnection>scm:git:[email protected]:amusarra/eventbus-logging-filter-jaxrs.git</developerConnection>
<tag>HEAD</tag>
</scm>
<profiles>
Expand All @@ -262,7 +268,8 @@
</activation>
<properties>
<skipITs>false</skipITs>
<quarkus.package.type>native</quarkus.package.type>
<quarkus.native.enabled>true</quarkus.native.enabled>
<quarkus.package.jar.enabled>false</quarkus.package.jar.enabled>
</properties>
</profile>
</profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ void onStop(@Observes ShutdownEvent ev) {
if (consumer != null) {
consumer.unregisterAndAwait();
log.debugf(
"Unregistering the Dispatcher from the event bus for the event handler at addresses: {%s}",
"Unregistering the Dispatcher from the event bus for the event handler at addresses:{%s}",
dispatcherVirtualAddress);
}
}

// Method to handle the event
/**
* Method to handle the event.
*
* @param message The message received from the event bus
*/
public void handleEvent(Message<JsonObject> message) {
// Leggere gli header dalle DeliveryOptions
String sourceVirtualAddress = message.headers().get(SOURCE_VIRTUAL_ADDRESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void onStop(@Observes ShutdownEvent ev) {
mongoDbEventHandlerVirtualAddress);
}
}

/**
* Metodo per gestire l'evento ricevuto dall'event bus.
*
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package it.dontesta.eventbus.orm.panache.entity;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Cacheable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToMany;
import java.time.LocalDate;
import java.util.List;

/**
* This class represents the Horse entity.
*/
@Entity
@Cacheable
public class Horse extends PanacheEntity {

@Column(length = 60)
public String name;

@Column(length = 15)
public String coat;

@Column(length = 60)
public String breed;

public LocalDate dateOfBirth;

@ManyToMany
public List<Owner> owners;
}
48 changes: 48 additions & 0 deletions src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package it.dontesta.eventbus.orm.panache.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Cacheable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToMany;
import java.util.List;

/**
* This class represents the Owner entity.
*/
@Entity
@Cacheable
public class Owner extends PanacheEntity {

@Column(length = 60)
public String name;

@Column(length = 60)
public String surname;

@Column(length = 60, unique = true)
public String email;

@Column(length = 20)
public String phoneNumber;

@Column(length = 60)
public String address;

@Column(length = 60)
public String city;

@Column(length = 2)
public String state;

@Column(length = 10)
public String zipCode;

@Column(length = 60)
public String country;

@ManyToMany(mappedBy = "owners")
@JsonBackReference
public List<Horse> horses;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.dontesta.eventbus.orm.panache.repository;

import io.quarkus.hibernate.orm.panache.PanacheRepository;
import it.dontesta.eventbus.orm.panache.entity.Horse;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.List;

/**
* This class is a Panache repository for the Horse entity.
*/
@ApplicationScoped
public class HorseRepository implements PanacheRepository<Horse> {
public List<Horse> findOrderedByName() {
return find("ORDER BY name").list();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.dontesta.eventbus.orm.panache.repository;

import io.quarkus.hibernate.orm.panache.PanacheRepository;
import it.dontesta.eventbus.orm.panache.entity.Owner;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.List;

/**
* This class represents the Panache repository for the Owner entity.
*/
@ApplicationScoped
public class OwnerRepository implements PanacheRepository<Owner> {
public List<Owner> findOrderedByName() {
return find("ORDER BY name").list();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
/**
* JAX-RS Application class that defines the base URI for the RESTful web services.
*
* <p>
* <a href="https://jakarta.ee/specifications/restful-ws/3.1/jakarta-restful-ws-spec-3.1.html#application">JAX-RS Application</a>
* <p><a href="https://jakarta.ee/specifications/restful-ws/3.1/jakarta-restful-ws-spec-3.1.html#application">JAX-RS Application</a>
*
* @author Antonio Musarra
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ public void filter(ContainerRequestContext requestContext,
return;
}

// Ottieni l'URI della richiesta
String requestUri = uriInfo.getRequestUri().getPath();

// Recupera l'ID di correlazione dalla richiesta
String correlationId =
Expand All @@ -145,6 +143,9 @@ public void filter(ContainerRequestContext requestContext,
// Aggiungi il cookie alla risposta HTTP
setCookieUserTracing(requestContext, responseContext);

// Ottieni l'URI della richiesta
String requestUri = uriInfo.getRequestUri().getPath();

// Aggiungi l'header X-Pod-Name alla risposta
responseContext.getHeaders().add(POD_NAME_HEADER, System.getenv("HOSTNAME"));

Expand Down Expand Up @@ -251,6 +252,10 @@ private String getRequestBody(ContainerRequestContext requestContext) throws IOE
private JsonObject prepareMessage(ContainerRequestContext requestContext) {
JsonObject jsonObject;
try {
String mediaType = requestContext.getMediaType() == null ? null :
"%s/%s".formatted(requestContext.getMediaType().getType(),
requestContext.getMediaType().getSubtype());

jsonObject = new JsonObject()
.put(CORRELATION_ID_HEADER, requestContext.getProperty(CORRELATION_ID_HEADER))
.put("remote-ip-address", routingContext.request().remoteAddress().host())
Expand All @@ -259,8 +264,7 @@ private JsonObject prepareMessage(ContainerRequestContext requestContext) {
.put("uri-info", requestContext.getUriInfo().getRequestUri().toString())
.put(LOCAL_DATE_TIME_IN, requestContext.getProperty(LOCAL_DATE_TIME_IN).toString())
.put("method", requestContext.getMethod())
.put("media-type", "%s/%s".formatted(requestContext.getMediaType().getType(),
requestContext.getMediaType().getSubtype()))
.put("media-type", mediaType)
.put("acceptable-language", requestContext.getAcceptableLanguages().toString())
.put("acceptable-media-types", requestContext.getAcceptableMediaTypes().toString());
} catch (IOException ioException) {
Expand Down Expand Up @@ -291,7 +295,8 @@ private JsonObject prepareMessage(ContainerResponseContext responseContext) {
.put("status-info-family-name", responseContext.getStatusInfo().getFamily().name())
.put("status-info-reason", responseContext.getStatusInfo().getReasonPhrase())
.put("headers", getResponseHeaders(responseContext))
.put("body", responseContext.getEntity().toString());
.put("body",
responseContext.getEntity() == null ? null : responseContext.getEntity().toString());
}

/**
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/it/dontesta/eventbus/ws/mappers/ErrorMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package it.dontesta.eventbus.ws.mappers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import org.jboss.logging.Logger;

/**
* This class represents the error mapper for the RESTful web services.
*/
@Provider
public class ErrorMapper implements ExceptionMapper<Throwable> {

@Inject
Logger log;

@Inject
ObjectMapper objectMapper;

@Override
public Response toResponse(Throwable exception) {
log.error("An error occurred", exception);

int code = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();

if (exception instanceof WebApplicationException webAppException) {
code = webAppException.getResponse().getStatus();
}

ObjectNode exceptionJson = objectMapper.createObjectNode();
exceptionJson.put("code", code);
exceptionJson.put("exceptionType", exception.getClass().getName());

if (exception.getMessage() != null) {
exceptionJson.put("error", exception.getMessage());
}

return Response.status(code).entity(exceptionJson).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

/**
* This class represents the REST endpoint for the Echo resource.
*/
@Path("rest")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Expand Down
Loading

0 comments on commit 7eacb6d

Please sign in to comment.