From 444af0c3e5e593d845a01539f7a63d8799a83af0 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Tue, 7 May 2024 22:43:56 +0200 Subject: [PATCH 01/23] Initial commit integrazione esempio quarkus-hibernate-orm-panache --- pom.xml | 28 +++--- .../eventbus/orm/panache/entity/Horse.java | 28 ++++++ .../eventbus/orm/panache/entity/Owner.java | 27 +++++ .../panache/repository/HorseRepository.java | 13 +++ .../panache/repository/OwnerRepository.java | 13 +++ src/main/resources/application.properties | 7 ++ src/main/resources/load_data.sql | 17 ++++ .../HorseRepositoryIntegrationTest.java | 45 +++++++++ .../OwnerRepositoryIntegrationTest.java | 45 +++++++++ .../PanacheRepositoryMockedTest.java | 99 +++++++++++++++++++ 10 files changed, 311 insertions(+), 11 deletions(-) create mode 100644 src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java create mode 100644 src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java create mode 100644 src/main/java/it/dontesta/eventbus/orm/panache/repository/HorseRepository.java create mode 100644 src/main/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepository.java create mode 100644 src/main/resources/load_data.sql create mode 100644 src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java create mode 100644 src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java create mode 100644 src/test/java/it/dontesta/eventbus/orm/panache/repository/PanacheRepositoryMockedTest.java diff --git a/pom.xml b/pom.xml index 1e2d410..7f85814 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,14 @@ io.quarkus quarkus-smallrye-health + + io.quarkus + quarkus-hibernate-orm-panache + + + io.quarkus + quarkus-jdbc-h2 + io.quarkus quarkus-jacoco @@ -118,6 +126,11 @@ quarkus-junit5 test + + io.quarkus + quarkus-junit5-mockito + test + io.rest-assured rest-assured @@ -209,8 +222,7 @@ generateKeyPair - ${project.build.outputDirectory}/${quarkus.http.ssl.certificate.key-store-file} - + ${project.build.outputDirectory}/${quarkus.http.ssl.certificate.key-store-file} cn=${project.artifactId}, ou=${project.groupId}, o=dontesta.it, c=IT ${quarkus.http.ssl.certificate.key-store-password} ${quarkus.http.ssl.certificate.key-store-password} @@ -241,15 +253,9 @@ - - scm:git:git@github.com:amusarra/eventbus-logging-filter-jaxrs.git - - - scm:git:git@github.com:amusarra/eventbus-logging-filter-jaxrs.git - - - scm:git:git@github.com:amusarra/eventbus-logging-filter-jaxrs.git - + scm:git:git@github.com:amusarra/eventbus-logging-filter-jaxrs.git + scm:git:git@github.com:amusarra/eventbus-logging-filter-jaxrs.git + scm:git:git@github.com:amusarra/eventbus-logging-filter-jaxrs.git HEAD diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java new file mode 100644 index 0000000..24216b3 --- /dev/null +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java @@ -0,0 +1,28 @@ +package it.dontesta.eventbus.orm.panache.entity; + +import io.quarkus.hibernate.orm.panache.PanacheEntity; +import jakarta.persistence.Cacheable; +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; +import java.time.LocalDate; +import java.util.List; + +@Entity +@Cacheable +public class Horse extends PanacheEntity { + + public String name; + public String coat; + public String breed; + public LocalDate dateOfBirth; + + @ManyToMany + @JoinTable( + name = "horse_owner", + joinColumns = @JoinColumn(name = "horse_id"), + inverseJoinColumns = @JoinColumn(name = "owner_id") + ) + public List owners; +} diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java new file mode 100644 index 0000000..708102f --- /dev/null +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java @@ -0,0 +1,27 @@ +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 jakarta.persistence.OneToMany; +import java.util.List; + +@Entity +@Cacheable +public class Owner extends PanacheEntity { + + public String name; + public String surname; + public String email; + public String phoneNumber; + public String address; + public String city; + public String state; + public String zipCode; + public String country; + + @ManyToMany(mappedBy = "owners") + public List horses; +} diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/repository/HorseRepository.java b/src/main/java/it/dontesta/eventbus/orm/panache/repository/HorseRepository.java new file mode 100644 index 0000000..32c52ee --- /dev/null +++ b/src/main/java/it/dontesta/eventbus/orm/panache/repository/HorseRepository.java @@ -0,0 +1,13 @@ +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; + +@ApplicationScoped +public class HorseRepository implements PanacheRepository { + public List findOrderedByName() { + return find("ORDER BY name").list(); + } +} diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepository.java b/src/main/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepository.java new file mode 100644 index 0000000..833f560 --- /dev/null +++ b/src/main/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepository.java @@ -0,0 +1,13 @@ +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; + +@ApplicationScoped +public class OwnerRepository implements PanacheRepository { + public List findOrderedByName() { + return find("ORDER BY name").list(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f56f747..fc7d70e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -101,6 +101,13 @@ quarkus.openshift.base-jvm-image=registry.access.redhat.com/ubi9/openjdk-21:1.18 # -Dcom.sun.management.jmxremote.local.only=false this option allows remote JMX connections quarkus.openshift.env.vars.java-opts=-Xms100M -Xmx500M -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:MaxGCPauseMillis=200 -XX:InitiatingHeapOccupancyPercent=45 -XX:G1ReservePercent=10 -XX:ConcGCThreads=4 -XX:G1NewSizePercent=5 -XX:G1MaxNewSizePercent=60 -XX:ParallelGCThreads=4 -XX:+ExitOnOutOfMemoryError -Dcom.sun.management.jmxremote.port=9091 -Dcom.sun.management.jmxremote.rmi.port=9091 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.local.only=false +## +## This section configure the Hibernate ORM extension +## +quarkus.hibernate-orm.database.generation=drop-and-create +quarkus.hibernate-orm.log.sql=true +quarkus.hibernate-orm.sql-load-script=load_data.sql + ## ## Application configuration properties ## diff --git a/src/main/resources/load_data.sql b/src/main/resources/load_data.sql new file mode 100644 index 0000000..5cd8451 --- /dev/null +++ b/src/main/resources/load_data.sql @@ -0,0 +1,17 @@ +-- Description: Load data into the database + +-- Insert data into the tables Horse +INSERT INTO horse(id, name, breed, coat, dateOfBirth) VALUES (1, 'Judio XXXV', 'Pura Raza Española - PRE', 'Black', '2012-05-01'); +INSERT INTO horse(id, name, breed, coat, dateOfBirth) VALUES (2, 'Fuego de Cardenas', 'Pura Raza Española - PRE', 'Grey', '2010-03-01'); +INSERT INTO horse(id, name, breed, coat, dateOfBirth) VALUES (3, 'Francisco', 'Puro Sangue Arabo - PSA', 'Grey', '2010-03-01'); +ALTER SEQUENCE Horse_SEQ RESTART WITH 4; + +-- Insert data into the tables Owner +INSERT INTO owner(id, name, surname, email, phoneNumber) VALUES (1, 'John', 'Doe', 'john.doe@dontesta.it', '123456789'); +INSERT INTO owner(id, name, surname, email, phoneNumber) VALUES (2, 'Mario', 'Rossi', 'mario.rossi@dontesta.it', '987654321'); +ALTER SEQUENCE Owner_SEQ RESTART WITH 3; + +-- Insert data into the tables Horse_Owner +INSERT INTO horse_owner(horse_id, owner_id) VALUES (1, 1); +INSERT INTO horse_owner(horse_id, owner_id) VALUES (2, 1); +INSERT INTO horse_owner(horse_id, owner_id) VALUES (3, 2); \ No newline at end of file diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java new file mode 100644 index 0000000..8f14d3c --- /dev/null +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java @@ -0,0 +1,45 @@ +package it.dontesta.eventbus.orm.panache.repository; + +import io.quarkus.test.junit.QuarkusTest; +import it.dontesta.eventbus.orm.panache.entity.Horse; +import jakarta.inject.Inject; +import java.time.LocalDate; +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +@QuarkusTest +class HorseRepositoryIntegrationTest { + + @Inject + HorseRepository horseRepository; + + @Test + void testFindById() { + Horse horse = horseRepository.findById(1L); + Assertions.assertNotNull(horse); + Assertions.assertEquals("Judio XXXV", horse.name); + } + + @Test + void testFindAll() { + List horses = horseRepository.listAll(); + Assertions.assertFalse(horses.isEmpty()); + } + + @Test + void testFindOrderedByName() { + List horses = horseRepository.findOrderedByName(); + Assertions.assertFalse(horses.isEmpty()); + Assertions.assertEquals("Francisco", horses.get(0).name); + Assertions.assertInstanceOf(LocalDate.class, horses.get(0).dateOfBirth); + } + + @Test + void testFindByName() { + Horse horse = horseRepository.find("name", "Francisco").firstResult(); + Assertions.assertNotNull(horse); + Assertions.assertEquals("Francisco", horse.name); + Assertions.assertEquals("Mario", horse.owners.get(0).name); + } +} diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java new file mode 100644 index 0000000..63d1c0a --- /dev/null +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java @@ -0,0 +1,45 @@ +package it.dontesta.eventbus.orm.panache.repository; + +import io.quarkus.test.junit.QuarkusTest; +import it.dontesta.eventbus.orm.panache.entity.Owner; +import jakarta.inject.Inject; +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +@QuarkusTest +class OwnerRepositoryIntegrationTest { + + @Inject + OwnerRepository ownerRepository; + + @Test + void testFindById() { + Owner owner = ownerRepository.findById(1L); + Assertions.assertNotNull(owner); + Assertions.assertEquals("John", owner.name); + } + + @Test + void testFindAll() { + List owners = ownerRepository.listAll(); + Assertions.assertFalse(owners.isEmpty()); + } + + @Test + void testFindOrderedByName() { + List owners = ownerRepository.findOrderedByName(); + Assertions.assertEquals("John", owners.get(0).name); + Assertions.assertFalse(owners.isEmpty()); + } + + @Test + void testFindByName() { + Owner owner = ownerRepository.find("name", "Mario").firstResult(); + Assertions.assertNotNull(owner); + Assertions.assertNotNull(owner.horses); + Assertions.assertEquals(1, owner.horses.size()); + Assertions.assertEquals("Francisco", owner.horses.get(0).name); + Assertions.assertEquals("Mario", owner.name); + } +} diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/PanacheRepositoryMockedTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/PanacheRepositoryMockedTest.java new file mode 100644 index 0000000..42f412b --- /dev/null +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/PanacheRepositoryMockedTest.java @@ -0,0 +1,99 @@ +package it.dontesta.eventbus.orm.panache.repository; + +import io.quarkus.test.InjectMock; +import io.quarkus.test.junit.QuarkusTest; +import it.dontesta.eventbus.orm.panache.entity.Horse; +import it.dontesta.eventbus.orm.panache.entity.Owner; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +@QuarkusTest +class PanacheRepositoryMockedTest { + + @InjectMock + HorseRepository horseRepository; + + @InjectMock + OwnerRepository ownerRepository; + + @Test + void testOwnerRepository() { + // Mocked classed always return a default value + Assertions.assertEquals(0, ownerRepository.count()); + + // Now let's specify the return value + Mockito.when(ownerRepository.count()).thenReturn(1L); + Assertions.assertEquals(1, ownerRepository.count()); + + // Now let's specify the return value + Mockito.when(ownerRepository.count()).thenReturn(2L); + Assertions.assertEquals(2, ownerRepository.count()); + + // Now let's call the original method + Mockito.when(ownerRepository.count()).thenCallRealMethod(); + Assertions.assertEquals(2, ownerRepository.count()); + + // Check that we called it 4 times + Mockito.verify(ownerRepository, Mockito.times(4)).count(); + + // Mock only with specific parameters + Owner owner = new Owner(); + Mockito.when(ownerRepository.findById(1L)).thenReturn(owner); + Assertions.assertSame(owner, ownerRepository.findById(1L)); + Assertions.assertNull(ownerRepository.findById(4L)); + + Mockito.when(ownerRepository.findOrderedByName()).thenReturn(Collections.emptyList()); + Assertions.assertTrue(ownerRepository.findOrderedByName().isEmpty()); + + // We can even mock your custom methods + Mockito.verify(ownerRepository).findOrderedByName(); + Mockito.verify(ownerRepository, Mockito.atLeastOnce()).findById(Mockito.any()); + Mockito.verifyNoMoreInteractions(ownerRepository); + } + + @Test + void testHorseRepository() { + // Mocked classed always return a default value + Assertions.assertEquals(0, horseRepository.count()); + + // Now let's specify the return value + Mockito.when(horseRepository.count()).thenReturn(1L); + Assertions.assertEquals(1, horseRepository.count()); + + // Now let's specify the return value + Mockito.when(horseRepository.count()).thenReturn(2L); + Assertions.assertEquals(2, horseRepository.count()); + + // Now let's call the original method + Mockito.when(horseRepository.count()).thenCallRealMethod(); + Assertions.assertEquals(3, horseRepository.count()); + + Horse horse = new Horse(); + horse.name = "Judio XXXV"; + Mockito.when(horseRepository.findById(1L)).thenReturn(horse); + Assertions.assertEquals("Judio XXXV", horseRepository.findById(1L).name); + + // Check that we called it 4 times + Mockito.verify(horseRepository, Mockito.times(4)).count(); + + // Mock only with specific parameters + horse = new Horse(); + horse.owners = List.of(new Owner()); + + Mockito.when(horseRepository.findById(1L)).thenReturn(horse); + Assertions.assertSame(horse, horseRepository.findById(1L)); + Assertions.assertNull(horseRepository.findById(4L)); + Assertions.assertEquals(1, horseRepository.findById(1L).owners.size()); + + Mockito.when(horseRepository.findOrderedByName()).thenReturn(Collections.emptyList()); + Assertions.assertTrue(horseRepository.findOrderedByName().isEmpty()); + + // We can even mock your custom methods + Mockito.verify(horseRepository).findOrderedByName(); + Mockito.verify(horseRepository, Mockito.atLeastOnce()).findById(Mockito.any()); + Mockito.verifyNoMoreInteractions(horseRepository); + } +} From b88777ee0da1394638239564c58925c7d4b4eac3 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Tue, 7 May 2024 23:01:46 +0200 Subject: [PATCH 02/23] Code Review Session --- .../java/it/dontesta/eventbus/orm/panache/entity/Owner.java | 2 -- .../panache/repository/HorseRepositoryIntegrationTest.java | 6 +++--- .../panache/repository/OwnerRepositoryIntegrationTest.java | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java index 708102f..0b5a6e1 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java @@ -2,10 +2,8 @@ import io.quarkus.hibernate.orm.panache.PanacheEntity; import jakarta.persistence.Cacheable; -import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.ManyToMany; -import jakarta.persistence.OneToMany; import java.util.List; @Entity diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java index 8f14d3c..368df69 100644 --- a/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java @@ -31,8 +31,8 @@ void testFindAll() { void testFindOrderedByName() { List horses = horseRepository.findOrderedByName(); Assertions.assertFalse(horses.isEmpty()); - Assertions.assertEquals("Francisco", horses.get(0).name); - Assertions.assertInstanceOf(LocalDate.class, horses.get(0).dateOfBirth); + Assertions.assertEquals("Francisco", horses.getFirst().name); + Assertions.assertInstanceOf(LocalDate.class, horses.getFirst().dateOfBirth); } @Test @@ -40,6 +40,6 @@ void testFindByName() { Horse horse = horseRepository.find("name", "Francisco").firstResult(); Assertions.assertNotNull(horse); Assertions.assertEquals("Francisco", horse.name); - Assertions.assertEquals("Mario", horse.owners.get(0).name); + Assertions.assertEquals("Mario", horse.owners.getFirst().name); } } diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java index 63d1c0a..07174ad 100644 --- a/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java @@ -29,7 +29,7 @@ void testFindAll() { @Test void testFindOrderedByName() { List owners = ownerRepository.findOrderedByName(); - Assertions.assertEquals("John", owners.get(0).name); + Assertions.assertEquals("John", owners.getFirst().name); Assertions.assertFalse(owners.isEmpty()); } @@ -39,7 +39,7 @@ void testFindByName() { Assertions.assertNotNull(owner); Assertions.assertNotNull(owner.horses); Assertions.assertEquals(1, owner.horses.size()); - Assertions.assertEquals("Francisco", owner.horses.get(0).name); + Assertions.assertEquals("Francisco", owner.horses.getFirst().name); Assertions.assertEquals("Mario", owner.name); } } From c6b55f71901ebfb87d39f29c7faf3bcdeb193c0c Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 09:25:41 +0200 Subject: [PATCH 03/23] Fixed the NullPointerException --- .../ws/filter/TraceJaxRsRequestResponseFilter.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/it/dontesta/eventbus/ws/filter/TraceJaxRsRequestResponseFilter.java b/src/main/java/it/dontesta/eventbus/ws/filter/TraceJaxRsRequestResponseFilter.java index b8e27b4..f5bcfcd 100644 --- a/src/main/java/it/dontesta/eventbus/ws/filter/TraceJaxRsRequestResponseFilter.java +++ b/src/main/java/it/dontesta/eventbus/ws/filter/TraceJaxRsRequestResponseFilter.java @@ -251,6 +251,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()) @@ -259,8 +263,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) { @@ -291,7 +294,7 @@ 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()); } /** From 6ae60fc4b6f60c2d4227239f91794f54656c79a2 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 09:26:21 +0200 Subject: [PATCH 04/23] Aggiunti due nuovi test per body empty --- .../endpoint/EchoResourceEndPointTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/EchoResourceEndPointTest.java b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/EchoResourceEndPointTest.java index 3a70a78..2b91a2b 100644 --- a/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/EchoResourceEndPointTest.java +++ b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/EchoResourceEndPointTest.java @@ -32,4 +32,21 @@ void testEchoValidation() { .body("title", is("Constraint Violation")); } + @Test + void testEchoBodyEmpty() { + given() + .contentType(ContentType.JSON) + .when().post("/api/rest/echo") + .then() + .statusCode(400); + } + + @Test + void testEchoBodyEmptyWithoutContentType() { + given() + .when().post("/api/rest/echo") + .then() + .statusCode(415); + } + } From bd45768e4cc95210c26be2af94338a5867e31414 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 09:47:07 +0200 Subject: [PATCH 05/23] =?UTF-8?q?Risoluzione=20"problema=20di=20serializza?= =?UTF-8?q?zione=20infinita"=20o=20"loop=20infinito=20di=20Jackson":=20Per?= =?UTF-8?q?=20risolvere=20questo=20problema,=20=C3=A8=20necessario=20utili?= =?UTF-8?q?zzare=20l'annotazione=20@JsonManagedReference=20e=20@JsonBackRe?= =?UTF-8?q?ference=20di=20Jackson.=20Queste=20annotazioni=20aiutano=20Jack?= =?UTF-8?q?son=20a=20risolvere=20il=20problema=20della=20serializzazione?= =?UTF-8?q?=20infinita=20causato=20dalle=20relazioni=20bidirezionali=20tra?= =?UTF-8?q?=20le=20entit=C3=A0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/it/dontesta/eventbus/orm/panache/entity/Horse.java | 2 ++ .../java/it/dontesta/eventbus/orm/panache/entity/Owner.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java index 24216b3..9a7ba44 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java @@ -1,5 +1,6 @@ package it.dontesta.eventbus.orm.panache.entity; +import com.fasterxml.jackson.annotation.JsonManagedReference; import io.quarkus.hibernate.orm.panache.PanacheEntity; import jakarta.persistence.Cacheable; import jakarta.persistence.Entity; @@ -24,5 +25,6 @@ public class Horse extends PanacheEntity { joinColumns = @JoinColumn(name = "horse_id"), inverseJoinColumns = @JoinColumn(name = "owner_id") ) + @JsonManagedReference public List owners; } diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java index 0b5a6e1..5fe964f 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java @@ -1,5 +1,6 @@ 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.Entity; @@ -21,5 +22,6 @@ public class Owner extends PanacheEntity { public String country; @ManyToMany(mappedBy = "owners") + @JsonBackReference public List horses; } From 86fbaa8d6726ecfe71d67746e57fb2f1ef77f315 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 10:02:42 +0200 Subject: [PATCH 06/23] Modificate insert query per Owner --- src/main/resources/load_data.sql | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/resources/load_data.sql b/src/main/resources/load_data.sql index 5cd8451..13881bc 100644 --- a/src/main/resources/load_data.sql +++ b/src/main/resources/load_data.sql @@ -7,11 +7,15 @@ INSERT INTO horse(id, name, breed, coat, dateOfBirth) VALUES (3, 'Francisco', 'P ALTER SEQUENCE Horse_SEQ RESTART WITH 4; -- Insert data into the tables Owner -INSERT INTO owner(id, name, surname, email, phoneNumber) VALUES (1, 'John', 'Doe', 'john.doe@dontesta.it', '123456789'); -INSERT INTO owner(id, name, surname, email, phoneNumber) VALUES (2, 'Mario', 'Rossi', 'mario.rossi@dontesta.it', '987654321'); +INSERT INTO owner(id, name, surname, email, phoneNumber, address, city, state, zipCode, country) +VALUES (1, 'John', 'Doe', 'john.doe@dontesta.it', '123456789', 'Via Roma 1', 'Rome', 'RM', '00100', 'Italy'); +INSERT INTO owner(id, name, surname, email, phoneNumber, address, city, state, zipCode, country) +VALUES (2, 'Mario', 'Rossi', 'mario.rossi@dontesta.it', '987654321', 'Via Garibaldi 1', 'Milan', 'MI', '20100', + 'Italy'); ALTER SEQUENCE Owner_SEQ RESTART WITH 3; -- Insert data into the tables Horse_Owner INSERT INTO horse_owner(horse_id, owner_id) VALUES (1, 1); +INSERT INTO horse_owner(horse_id, owner_id) VALUES (1, 2); INSERT INTO horse_owner(horse_id, owner_id) VALUES (2, 1); INSERT INTO horse_owner(horse_id, owner_id) VALUES (3, 2); \ No newline at end of file From 1db6034859cab2cec24da3eca2850ca7f2d1cd2d Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 10:03:19 +0200 Subject: [PATCH 07/23] Aggiornate entity con @Column annotation --- .../eventbus/orm/panache/entity/Horse.java | 7 +++++++ .../eventbus/orm/panache/entity/Owner.java | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java index 9a7ba44..92423b9 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonManagedReference; import io.quarkus.hibernate.orm.panache.PanacheEntity; import jakarta.persistence.Cacheable; +import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; @@ -14,9 +15,15 @@ @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 diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java index 5fe964f..2b14360 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java @@ -3,6 +3,7 @@ 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; @@ -11,14 +12,31 @@ @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") From 93b39b130e14da112ad446b8cefce70f6e35fb42 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 10:03:57 +0200 Subject: [PATCH 08/23] SF: Source formatting --- .../eventbus/orm/panache/entity/Owner.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java index 2b14360..f8252f4 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java @@ -12,34 +12,34 @@ @Cacheable public class Owner extends PanacheEntity { - @Column(length = 60) - public String name; + @Column(length = 60) + public String name; - @Column(length = 60) - public String surname; + @Column(length = 60) + public String surname; - @Column(length = 60, unique = true) - public String email; + @Column(length = 60, unique = true) + public String email; - @Column(length = 20) - public String phoneNumber; + @Column(length = 20) + public String phoneNumber; - @Column(length = 60) - public String address; + @Column(length = 60) + public String address; - @Column(length = 60) - public String city; + @Column(length = 60) + public String city; - @Column(length = 2) - public String state; + @Column(length = 2) + public String state; - @Column(length = 10) - public String zipCode; + @Column(length = 10) + public String zipCode; - @Column(length = 60) - public String country; + @Column(length = 60) + public String country; - @ManyToMany(mappedBy = "owners") - @JsonBackReference - public List horses; + @ManyToMany(mappedBy = "owners") + @JsonBackReference + public List horses; } From 3ec379d811807b786b3eafedd7db9b7682c30ec8 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 19:04:42 +0200 Subject: [PATCH 09/23] Aggiunto custom JAX-RS ErrorMapper --- .../eventbus/ws/mappers/ErrorMapper.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/java/it/dontesta/eventbus/ws/mappers/ErrorMapper.java diff --git a/src/main/java/it/dontesta/eventbus/ws/mappers/ErrorMapper.java b/src/main/java/it/dontesta/eventbus/ws/mappers/ErrorMapper.java new file mode 100644 index 0000000..66a4369 --- /dev/null +++ b/src/main/java/it/dontesta/eventbus/ws/mappers/ErrorMapper.java @@ -0,0 +1,41 @@ +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; + +@Provider +public class ErrorMapper implements ExceptionMapper { + + @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(); + } +} From a37b3cbdd50d43f458e682f1c18b1253ffa4ee7c Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 19:05:45 +0200 Subject: [PATCH 10/23] Rimossa annotazione @JoinTable --- .../it/dontesta/eventbus/orm/panache/entity/Horse.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java index 92423b9..acbcf0f 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java @@ -1,12 +1,9 @@ package it.dontesta.eventbus.orm.panache.entity; -import com.fasterxml.jackson.annotation.JsonManagedReference; import io.quarkus.hibernate.orm.panache.PanacheEntity; import jakarta.persistence.Cacheable; import jakarta.persistence.Column; import jakarta.persistence.Entity; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToMany; import java.time.LocalDate; import java.util.List; @@ -27,11 +24,5 @@ public class Horse extends PanacheEntity { public LocalDate dateOfBirth; @ManyToMany - @JoinTable( - name = "horse_owner", - joinColumns = @JoinColumn(name = "horse_id"), - inverseJoinColumns = @JoinColumn(name = "owner_id") - ) - @JsonManagedReference public List owners; } From 32a8ac4ef67c6c6f23d3f6a1242db6044bbd48e0 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 19:07:00 +0200 Subject: [PATCH 11/23] Aggiornamento file SQL load_data.sql --- src/main/resources/load_data.sql | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/resources/load_data.sql b/src/main/resources/load_data.sql index 13881bc..d4ba25a 100644 --- a/src/main/resources/load_data.sql +++ b/src/main/resources/load_data.sql @@ -4,7 +4,9 @@ INSERT INTO horse(id, name, breed, coat, dateOfBirth) VALUES (1, 'Judio XXXV', 'Pura Raza Española - PRE', 'Black', '2012-05-01'); INSERT INTO horse(id, name, breed, coat, dateOfBirth) VALUES (2, 'Fuego de Cardenas', 'Pura Raza Española - PRE', 'Grey', '2010-03-01'); INSERT INTO horse(id, name, breed, coat, dateOfBirth) VALUES (3, 'Francisco', 'Puro Sangue Arabo - PSA', 'Grey', '2010-03-01'); -ALTER SEQUENCE Horse_SEQ RESTART WITH 4; +INSERT INTO horse(id, name, breed, coat, dateOfBirth) VALUES (4, 'Shirus', 'Quarab', 'Grey', '2012-05-01'); +INSERT INTO horse(id, name, breed, coat, dateOfBirth) VALUES (5, 'Artemis', 'San Fratellano', 'Black', '2024-05-01'); +ALTER SEQUENCE Horse_SEQ RESTART WITH 6; -- Insert data into the tables Owner INSERT INTO owner(id, name, surname, email, phoneNumber, address, city, state, zipCode, country) @@ -12,10 +14,17 @@ VALUES (1, 'John', 'Doe', 'john.doe@dontesta.it', '123456789', 'Via Roma 1', 'Ro INSERT INTO owner(id, name, surname, email, phoneNumber, address, city, state, zipCode, country) VALUES (2, 'Mario', 'Rossi', 'mario.rossi@dontesta.it', '987654321', 'Via Garibaldi 1', 'Milan', 'MI', '20100', 'Italy'); -ALTER SEQUENCE Owner_SEQ RESTART WITH 3; +INSERT INTO owner(id, name, surname, email, phoneNumber, address, city, state, zipCode, country) +VALUES (3, 'Valentina', 'Rossi', 'valentina.rossi@dontesta.it', '987654321', 'Via Bronte 1', 'Bronte', 'CT', '95034', + 'Italy'); +ALTER SEQUENCE Owner_SEQ RESTART WITH 4; -- Insert data into the tables Horse_Owner -INSERT INTO horse_owner(horse_id, owner_id) VALUES (1, 1); -INSERT INTO horse_owner(horse_id, owner_id) VALUES (1, 2); -INSERT INTO horse_owner(horse_id, owner_id) VALUES (2, 1); -INSERT INTO horse_owner(horse_id, owner_id) VALUES (3, 2); \ No newline at end of file +INSERT INTO horse_owner(horses_id, owners_id) VALUES (1, 1); +INSERT INTO horse_owner(horses_id, owners_id) VALUES (1, 2); +INSERT INTO horse_owner(horses_id, owners_id) VALUES (2, 1); +INSERT INTO horse_owner(horses_id, owners_id) VALUES (3, 2); +INSERT INTO horse_owner(horses_id, owners_id) VALUES (4, 1); +INSERT INTO horse_owner(horses_id, owners_id) VALUES (4, 2); +INSERT INTO horse_owner(horses_id, owners_id) VALUES (5, 1); +INSERT INTO horse_owner(horses_id, owners_id) VALUES (5, 3); \ No newline at end of file From 740e181161174fde3d077f8e88079839249fb7f0 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 19:08:01 +0200 Subject: [PATCH 12/23] Aggiornamento versione Quarkus a 3.10.0 --- pom.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 7f85814..81d78a0 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ UTF-8 quarkus-bom io.quarkus.platform - 3.9.2 + 3.10.0 true 3.2.5 amusarra-github @@ -268,7 +268,8 @@ false - native + true + false From 07f6fa6801f372af3c4b31c7f069e6656d53aab5 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 19:16:19 +0200 Subject: [PATCH 13/23] Initial commit of the HorseRepositoryResources --- .../v1/HorseRepositoryResources.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResources.java diff --git a/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResources.java b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResources.java new file mode 100644 index 0000000..cedc966 --- /dev/null +++ b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResources.java @@ -0,0 +1,77 @@ +package it.dontesta.eventbus.ws.resources.endpoint.repository.v1; + +import it.dontesta.eventbus.orm.panache.entity.Horse; +import it.dontesta.eventbus.orm.panache.repository.HorseRepository; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; +import jakarta.validation.constraints.NotNull; +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.Produces; +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import java.util.List; +import org.jboss.logging.Logger; + +@Path("rest/repository/horse/v1") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class HorseRepositoryResources { + + @Inject + HorseRepository horseRepository; + + @Inject + Logger log; + + @GET + public List getAllHorses() { + return horseRepository.findAll().list(); + } + + @GET + @Path("{id}") + public Horse getHorseById(@NotNull Long id) { + Horse horse = horseRepository.findById(id); + + if (horse == null) { + throw new WebApplicationException("Horse with id of " + id + " not found", + Response.Status.NOT_FOUND); + } + + return horse; + } + + @POST + @Transactional + public Response createHorse(@NotNull Horse horse) { + // Return a 422 Unprocessable Entity if an ID was provided + // Not used the constant Response.Status.UNPROCESSABLE_ENTITY because it is not available in the + // version of the Jakarta EE API used by Quarkus + if (horse.id != null) { + throw new WebApplicationException("Id was invalidly set on request.", 422); + } + + horseRepository.persist(horse); + return Response.ok(horse).status(Response.Status.CREATED).build(); + } + + @DELETE + @Path("{id}") + @Transactional + public Response deleteHorse(@NotNull Long id) { + Horse horse = horseRepository.findById(id); + + if (horse == null) { + throw new WebApplicationException("Horse with id of " + id + " not found", + Response.Status.NOT_FOUND); + } + + horseRepository.delete(horse); + return Response.status(Response.Status.NO_CONTENT).build(); + } +} From d28dbd9ee0fdb7d048b683c297918823a1fb1bde Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 19:16:41 +0200 Subject: [PATCH 14/23] Initial commit of the HorseRepositoryResourcesTest --- .../v1/HorseRepositoryResourcesTest.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResourcesTest.java diff --git a/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResourcesTest.java b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResourcesTest.java new file mode 100644 index 0000000..a19cd3d --- /dev/null +++ b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResourcesTest.java @@ -0,0 +1,68 @@ +package it.dontesta.eventbus.ws.resources.endpoint.repository.v1; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import org.junit.jupiter.api.Test; + +@QuarkusTest +class HorseRepositoryResourcesTest { + + @Test + void getAllHorsesSuccess() { + given() + .contentType(ContentType.JSON) + .when().get("/api/rest/repository/horse/v1") + .then() + .statusCode(200); + } + + @Test + void getHorseByIdSuccess() { + given() + .contentType(ContentType.JSON) + .when().get("/api/rest/repository/horse/v1/1") + .then() + .body("name", is("Judio XXXV")) + .statusCode(200); + } + + @Test + void getHorseByIdNotFound() { + given() + .contentType(ContentType.JSON) + .when().get("/api/rest/repository/horse/v1/100") + .then() + .statusCode(404) + .body("error", is("Horse with id of 100 not found")); + } + + @Test + void testCreateHorseBodyNull() { + given() + .contentType(ContentType.JSON) + .when().post("/api/rest/repository/horse/v1") + .then() + .statusCode(400) + .body("title", is("Constraint Violation")); + } + + @Test + void testDeleteHorseByIdSuccess() { + given() + .when().delete("/api/rest/repository/horse/v1/1") + .then() + .statusCode(204); + } + + @Test + void testDeleteHorseByIdNotFound() { + given() + .when().delete("/api/rest/repository/horse/v1/100") + .then() + .statusCode(404); + } + +} From 833b6976d7545c61ff552364dfe43fdeaa2ca6a1 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 19:17:12 +0200 Subject: [PATCH 15/23] Review Unit Test and Integration Test --- .../panache/repository/HorseRepositoryIntegrationTest.java | 2 +- .../panache/repository/OwnerRepositoryIntegrationTest.java | 4 ++-- .../orm/panache/repository/PanacheRepositoryMockedTest.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java index 368df69..2ddada9 100644 --- a/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java @@ -31,7 +31,7 @@ void testFindAll() { void testFindOrderedByName() { List horses = horseRepository.findOrderedByName(); Assertions.assertFalse(horses.isEmpty()); - Assertions.assertEquals("Francisco", horses.getFirst().name); + Assertions.assertEquals("Artemis", horses.getFirst().name); Assertions.assertInstanceOf(LocalDate.class, horses.getFirst().dateOfBirth); } diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java index 07174ad..b11f0e9 100644 --- a/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java @@ -38,8 +38,8 @@ void testFindByName() { Owner owner = ownerRepository.find("name", "Mario").firstResult(); Assertions.assertNotNull(owner); Assertions.assertNotNull(owner.horses); - Assertions.assertEquals(1, owner.horses.size()); - Assertions.assertEquals("Francisco", owner.horses.getFirst().name); + Assertions.assertEquals(3, owner.horses.size()); + Assertions.assertEquals("Judio XXXV", owner.horses.getFirst().name); Assertions.assertEquals("Mario", owner.name); } } diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/PanacheRepositoryMockedTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/PanacheRepositoryMockedTest.java index 42f412b..7927818 100644 --- a/src/test/java/it/dontesta/eventbus/orm/panache/repository/PanacheRepositoryMockedTest.java +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/PanacheRepositoryMockedTest.java @@ -34,7 +34,7 @@ void testOwnerRepository() { // Now let's call the original method Mockito.when(ownerRepository.count()).thenCallRealMethod(); - Assertions.assertEquals(2, ownerRepository.count()); + Assertions.assertEquals(3, ownerRepository.count()); // Check that we called it 4 times Mockito.verify(ownerRepository, Mockito.times(4)).count(); @@ -69,7 +69,7 @@ void testHorseRepository() { // Now let's call the original method Mockito.when(horseRepository.count()).thenCallRealMethod(); - Assertions.assertEquals(3, horseRepository.count()); + Assertions.assertEquals(5, horseRepository.count()); Horse horse = new Horse(); horse.name = "Judio XXXV"; From f7d5fb347f5e17fb92c47af081fd4678a03ee4f1 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 22:12:59 +0200 Subject: [PATCH 16/23] Review Unit Test and Integration Test --- .../HorseRepositoryIntegrationTest.java | 46 +++++++++++++++++++ .../OwnerRepositoryIntegrationTest.java | 14 ++++++ .../v1/HorseRepositoryResourcesTest.java | 46 ++++++++++++++++--- 3 files changed, 99 insertions(+), 7 deletions(-) diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java index 2ddada9..5c4444e 100644 --- a/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/HorseRepositoryIntegrationTest.java @@ -3,18 +3,30 @@ import io.quarkus.test.junit.QuarkusTest; import it.dontesta.eventbus.orm.panache.entity.Horse; import jakarta.inject.Inject; +import jakarta.transaction.Transactional; import java.time.LocalDate; import java.util.List; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; @QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class HorseRepositoryIntegrationTest { @Inject HorseRepository horseRepository; @Test + @Order(1) + void testCount() { + Assertions.assertEquals(5, horseRepository.count()); + } + + @Test + @Order(2) void testFindById() { Horse horse = horseRepository.findById(1L); Assertions.assertNotNull(horse); @@ -22,12 +34,14 @@ void testFindById() { } @Test + @Order(3) void testFindAll() { List horses = horseRepository.listAll(); Assertions.assertFalse(horses.isEmpty()); } @Test + @Order(4) void testFindOrderedByName() { List horses = horseRepository.findOrderedByName(); Assertions.assertFalse(horses.isEmpty()); @@ -36,10 +50,42 @@ void testFindOrderedByName() { } @Test + @Order(5) void testFindByName() { Horse horse = horseRepository.find("name", "Francisco").firstResult(); Assertions.assertNotNull(horse); Assertions.assertEquals("Francisco", horse.name); Assertions.assertEquals("Mario", horse.owners.getFirst().name); } + + @Test + @Order(6) + @Transactional + void testCreateHorse() { + Horse horse = new Horse(); + horse.name = "Test Horse"; + horse.coat = "Bay"; + horse.breed = "Arabian"; + horse.dateOfBirth = LocalDate.of(2010, 1, 1); + horseRepository.persist(horse); + Assertions.assertNotNull(horse.id); + } + + @Test + @Order(7) + @Transactional + void testUpdateHorse() { + Horse horse = horseRepository.findById(5L); + horse.name = "Updated Horse"; + horseRepository.persist(horse); + Assertions.assertEquals("Updated Horse", horseRepository.findById(5L).name); + } + + @Test + @Order(8) + @Transactional + void testDeleteById() { + horseRepository.deleteById(5L); + Assertions.assertNull(horseRepository.findById(5L)); + } } diff --git a/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java b/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java index b11f0e9..09b63d7 100644 --- a/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java +++ b/src/test/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepositoryIntegrationTest.java @@ -5,15 +5,26 @@ import jakarta.inject.Inject; import java.util.List; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; @QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class OwnerRepositoryIntegrationTest { @Inject OwnerRepository ownerRepository; @Test + @Order(1) + void testCount() { + Assertions.assertEquals(3, ownerRepository.count()); + } + + @Test + @Order(2) void testFindById() { Owner owner = ownerRepository.findById(1L); Assertions.assertNotNull(owner); @@ -21,12 +32,14 @@ void testFindById() { } @Test + @Order(3) void testFindAll() { List owners = ownerRepository.listAll(); Assertions.assertFalse(owners.isEmpty()); } @Test + @Order(4) void testFindOrderedByName() { List owners = ownerRepository.findOrderedByName(); Assertions.assertEquals("John", owners.getFirst().name); @@ -34,6 +47,7 @@ void testFindOrderedByName() { } @Test + @Order(5) void testFindByName() { Owner owner = ownerRepository.find("name", "Mario").firstResult(); Assertions.assertNotNull(owner); diff --git a/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResourcesTest.java b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResourcesTest.java index a19cd3d..279d40a 100644 --- a/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResourcesTest.java +++ b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResourcesTest.java @@ -5,64 +5,96 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; @QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class HorseRepositoryResourcesTest { @Test + @Order(1) void getAllHorsesSuccess() { given() .contentType(ContentType.JSON) .when().get("/api/rest/repository/horse/v1") .then() - .statusCode(200); + .statusCode(Response.Status.OK.getStatusCode()); } @Test + @Order(2) void getHorseByIdSuccess() { given() .contentType(ContentType.JSON) .when().get("/api/rest/repository/horse/v1/1") .then() .body("name", is("Judio XXXV")) - .statusCode(200); + .statusCode(Response.Status.OK.getStatusCode()); } @Test + @Order(3) void getHorseByIdNotFound() { given() .contentType(ContentType.JSON) .when().get("/api/rest/repository/horse/v1/100") .then() - .statusCode(404) + .statusCode(Response.Status.NOT_FOUND.getStatusCode()) .body("error", is("Horse with id of 100 not found")); } @Test + @Order(4) void testCreateHorseBodyNull() { given() .contentType(ContentType.JSON) .when().post("/api/rest/repository/horse/v1") .then() - .statusCode(400) + .statusCode(Response.Status.BAD_REQUEST.getStatusCode()) .body("title", is("Constraint Violation")); } @Test + @Order(5) + void testCreateHorse() { + String json = """ + { + "name": "Santos XVVI", + "coat": "Gray", + "breed": "Pura Raza Española - PRE", + "dateOfBirth": "2024-05-01", + "owners": [{"id": 3}] + } + """; + + given() + .contentType(ContentType.JSON) + .body(json) + .when().post("/api/rest/repository/horse/v1") + .then() + .statusCode(Response.Status.CREATED.getStatusCode()) + .body("name", is("Santos XVVI")); + } + + @Test + @Order(6) void testDeleteHorseByIdSuccess() { given() .when().delete("/api/rest/repository/horse/v1/1") .then() - .statusCode(204); + .statusCode(Response.Status.NO_CONTENT.getStatusCode()); } @Test + @Order(7) void testDeleteHorseByIdNotFound() { given() .when().delete("/api/rest/repository/horse/v1/100") .then() - .statusCode(404); + .statusCode(Response.Status.NOT_FOUND.getStatusCode()); } - } From 14472d6962e26be14f1608522b64142f3dabf871 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 22:31:12 +0200 Subject: [PATCH 17/23] Added the OwnerRepositoryResourcesTest and OwnerRepositoryResources --- .../v1/OwnerRepositoryResources.java | 77 +++++++++++++++ .../v1/OwnerRepositoryResourcesTest.java | 96 +++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResources.java create mode 100644 src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResourcesTest.java diff --git a/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResources.java b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResources.java new file mode 100644 index 0000000..bc9e794 --- /dev/null +++ b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResources.java @@ -0,0 +1,77 @@ +package it.dontesta.eventbus.ws.resources.endpoint.repository.v1; + +import it.dontesta.eventbus.orm.panache.entity.Owner; +import it.dontesta.eventbus.orm.panache.repository.OwnerRepository; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; +import jakarta.validation.constraints.NotNull; +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.Produces; +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import java.util.List; +import org.jboss.logging.Logger; + +@Path("rest/repository/owner/v1") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class OwnerRepositoryResources { + + @Inject + OwnerRepository ownerRepository; + + @Inject + Logger log; + + @GET + public List getAllOwners() { + return ownerRepository.findAll().list(); + } + + @GET + @Path("{id}") + public Owner getOwnerById(@NotNull Long id) { + Owner owner = ownerRepository.findById(id); + + if (owner == null) { + throw new WebApplicationException("Owner with id of " + id + " not found", + Response.Status.NOT_FOUND); + } + + return owner; + } + + @POST + @Transactional + public Response createOwner(@NotNull Owner owner) { + // Return a 422 Unprocessable Entity if an ID was provided + // Not used the constant Response.Status.UNPROCESSABLE_ENTITY because it is not available in the + // version of the Jakarta EE API used by Quarkus + if (owner.id != null) { + throw new WebApplicationException("Id was invalidly set on request.", 422); + } + + ownerRepository.persist(owner); + return Response.ok(owner).status(Response.Status.CREATED).build(); + } + + @DELETE + @Path("{id}") + @Transactional + public Response deleteOwner(@NotNull Long id) { + Owner owner = ownerRepository.findById(id); + + if (owner == null) { + throw new WebApplicationException("Owner with id of " + id + " not found", + Response.Status.NOT_FOUND); + } + + ownerRepository.delete(owner); + return Response.status(Response.Status.NO_CONTENT).build(); + } +} diff --git a/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResourcesTest.java b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResourcesTest.java new file mode 100644 index 0000000..17178d0 --- /dev/null +++ b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResourcesTest.java @@ -0,0 +1,96 @@ +package it.dontesta.eventbus.ws.resources.endpoint.repository.v1; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import jakarta.ws.rs.core.Response; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class OwnerRepositoryResourcesTest { + + @Test + @Order(1) + void getAllOwnersSuccess() { + given() + .contentType(ContentType.JSON) + .when().get("/api/rest/repository/owner/v1") + .then() + .statusCode(Response.Status.OK.getStatusCode()); + } + + @Test + @Order(2) + void getOwnerByIdSuccess() { + given() + .contentType(ContentType.JSON) + .when().get("/api/rest/repository/owner/v1/1") + .then() + .body("name", is("John")) + .statusCode(Response.Status.OK.getStatusCode()); + } + + @Test + @Order(3) + void getOwnerByIdNotFound() { + given() + .contentType(ContentType.JSON) + .when().get("/api/rest/repository/owner/v1/100") + .then() + .statusCode(Response.Status.NOT_FOUND.getStatusCode()) + .body("error", is("Owner with id of 100 not found")); + } + + @Test + @Order(4) + void testCreateOwnerBodyNull() { + given() + .contentType(ContentType.JSON) + .when().post("/api/rest/repository/owner/v1") + .then() + .statusCode(Response.Status.BAD_REQUEST.getStatusCode()) + .body("title", is("Constraint Violation")); + } + + @Test + @Order(5) + void testCreateOwner() { + String json = """ + { + "name": "Rossella", + "surname": "Germanà", + "email": "rossella.germana@dontesta.it", + "phoneNumber": "987654321", + "address": "Via Palermo 1", + "city": "Catania", + "state": "CT", + "zipCode": "95100", + "country": "Italy", + "horses": [{"id": 1}] + } + """; + + given() + .contentType(ContentType.JSON) + .body(json) + .when().post("/api/rest/repository/owner/v1") + .then() + .statusCode(Response.Status.CREATED.getStatusCode()) + .body("name", is("Rossella")); + } + + @Test + @Order(6) + void testDeleteOwnerByIdNotFound() { + given() + .when().delete("/api/rest/repository/owner/v1/100") + .then() + .statusCode(Response.Status.NOT_FOUND.getStatusCode()); + } +} From c3f9a582eae3f727a023f51cd1a0fc40f2e7669c Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 22:34:24 +0200 Subject: [PATCH 18/23] Reviewed the OwnerRepositoryResourcesTest --- .../endpoint/repository/v1/OwnerRepositoryResourcesTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResourcesTest.java b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResourcesTest.java index 17178d0..2de7394 100644 --- a/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResourcesTest.java +++ b/src/test/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResourcesTest.java @@ -71,8 +71,7 @@ void testCreateOwner() { "city": "Catania", "state": "CT", "zipCode": "95100", - "country": "Italy", - "horses": [{"id": 1}] + "country": "Italy" } """; From 3f3014d5a1b8d0eb9f7546527be7559a3718e322 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 23:07:29 +0200 Subject: [PATCH 19/23] Avanzamento della minor version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 81d78a0..e32ac10 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 it.dontesta.eventbus eventbus-logging-filter-jaxrs - 1.1.1-SNAPSHOT + 1.2.0-SNAPSHOT eventbus-logging-filter-jaxrs Event Bus Logging Filter JAX-RS From 62ecbd4c51abcc1500744253046db295d6d1c658 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 23:08:52 +0200 Subject: [PATCH 20/23] Aggiornamento del CHANGELOG.md --- CHANGELOG.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0db3a51..feb7cbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,34 @@ 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 ### Removed From e99b8e9e1ffa3768cbb3ea122e9aa5e8c75e8a1f Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 23:10:14 +0200 Subject: [PATCH 21/23] Aggiunto il branch develop --- .github/workflows/build_via_maven.yml | 4 ++-- .github/workflows/sonarcloud.yml | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_via_maven.yml b/.github/workflows/build_via_maven.yml index a475d3a..29fa1e3 100644 --- a/.github/workflows/build_via_maven.yml +++ b/.github/workflows/build_via_maven.yml @@ -10,9 +10,9 @@ name: Build with Maven on: push: - branches: [ "main" ] + branches: [ "main", "develop" ] pull_request: - branches: [ "main" ] + branches: [ "main", "develop" ] jobs: build: diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 3e519fc..9d77176 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -3,6 +3,7 @@ on: push: branches: - main + - develop pull_request: types: [opened, synchronize, reopened] jobs: From 3f9f6a89e1600eb4615b2682c92f2260973d9f8f Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 23:36:55 +0200 Subject: [PATCH 22/23] Code Review for Google Checkstyle --- .../consumers/events/handlers/Dispatcher.java | 8 ++++-- .../handlers/nosql/MongoDbEventHandler.java | 1 + .../eventbus/orm/panache/entity/Horse.java | 3 +++ .../eventbus/orm/panache/entity/Owner.java | 3 +++ .../panache/repository/HorseRepository.java | 3 +++ .../panache/repository/OwnerRepository.java | 3 +++ .../eventbus/ws/EventBusApplication.java | 3 +-- .../TraceJaxRsRequestResponseFilter.java | 8 +++--- .../eventbus/ws/mappers/ErrorMapper.java | 3 +++ .../endpoint/EchoResourceEndPoint.java | 3 +++ .../v1/HorseRepositoryResources.java | 26 +++++++++++++++++++ .../v1/OwnerRepositoryResources.java | 21 +++++++++++++++ 12 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/main/java/it/dontesta/eventbus/consumers/events/handlers/Dispatcher.java b/src/main/java/it/dontesta/eventbus/consumers/events/handlers/Dispatcher.java index fa6238b..55252fd 100644 --- a/src/main/java/it/dontesta/eventbus/consumers/events/handlers/Dispatcher.java +++ b/src/main/java/it/dontesta/eventbus/consumers/events/handlers/Dispatcher.java @@ -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 message) { // Leggere gli header dalle DeliveryOptions String sourceVirtualAddress = message.headers().get(SOURCE_VIRTUAL_ADDRESS); diff --git a/src/main/java/it/dontesta/eventbus/consumers/events/handlers/nosql/MongoDbEventHandler.java b/src/main/java/it/dontesta/eventbus/consumers/events/handlers/nosql/MongoDbEventHandler.java index d2bf261..477dbfe 100644 --- a/src/main/java/it/dontesta/eventbus/consumers/events/handlers/nosql/MongoDbEventHandler.java +++ b/src/main/java/it/dontesta/eventbus/consumers/events/handlers/nosql/MongoDbEventHandler.java @@ -79,6 +79,7 @@ void onStop(@Observes ShutdownEvent ev) { mongoDbEventHandlerVirtualAddress); } } + /** * Metodo per gestire l'evento ricevuto dall'event bus. * diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java index acbcf0f..f2a7276 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Horse.java @@ -8,6 +8,9 @@ import java.time.LocalDate; import java.util.List; +/** + * This class represents the Horse entity. + */ @Entity @Cacheable public class Horse extends PanacheEntity { diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java index f8252f4..a25ff07 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/entity/Owner.java @@ -8,6 +8,9 @@ import jakarta.persistence.ManyToMany; import java.util.List; +/** + * This class represents the Owner entity. + */ @Entity @Cacheable public class Owner extends PanacheEntity { diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/repository/HorseRepository.java b/src/main/java/it/dontesta/eventbus/orm/panache/repository/HorseRepository.java index 32c52ee..9695dfa 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/repository/HorseRepository.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/repository/HorseRepository.java @@ -5,6 +5,9 @@ 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 { public List findOrderedByName() { diff --git a/src/main/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepository.java b/src/main/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepository.java index 833f560..3c65ea3 100644 --- a/src/main/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepository.java +++ b/src/main/java/it/dontesta/eventbus/orm/panache/repository/OwnerRepository.java @@ -5,6 +5,9 @@ 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 { public List findOrderedByName() { diff --git a/src/main/java/it/dontesta/eventbus/ws/EventBusApplication.java b/src/main/java/it/dontesta/eventbus/ws/EventBusApplication.java index 9059ffc..88d22dc 100644 --- a/src/main/java/it/dontesta/eventbus/ws/EventBusApplication.java +++ b/src/main/java/it/dontesta/eventbus/ws/EventBusApplication.java @@ -6,8 +6,7 @@ /** * JAX-RS Application class that defines the base URI for the RESTful web services. * - *

- * JAX-RS Application + *

JAX-RS Application * * @author Antonio Musarra */ diff --git a/src/main/java/it/dontesta/eventbus/ws/filter/TraceJaxRsRequestResponseFilter.java b/src/main/java/it/dontesta/eventbus/ws/filter/TraceJaxRsRequestResponseFilter.java index f5bcfcd..63602ca 100644 --- a/src/main/java/it/dontesta/eventbus/ws/filter/TraceJaxRsRequestResponseFilter.java +++ b/src/main/java/it/dontesta/eventbus/ws/filter/TraceJaxRsRequestResponseFilter.java @@ -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 = @@ -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")); @@ -294,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() == null ? null : responseContext.getEntity().toString()); + .put("body", + responseContext.getEntity() == null ? null : responseContext.getEntity().toString()); } /** diff --git a/src/main/java/it/dontesta/eventbus/ws/mappers/ErrorMapper.java b/src/main/java/it/dontesta/eventbus/ws/mappers/ErrorMapper.java index 66a4369..d6e9585 100644 --- a/src/main/java/it/dontesta/eventbus/ws/mappers/ErrorMapper.java +++ b/src/main/java/it/dontesta/eventbus/ws/mappers/ErrorMapper.java @@ -9,6 +9,9 @@ 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 { diff --git a/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/EchoResourceEndPoint.java b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/EchoResourceEndPoint.java index 4c7269c..4b5ab2d 100644 --- a/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/EchoResourceEndPoint.java +++ b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/EchoResourceEndPoint.java @@ -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) diff --git a/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResources.java b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResources.java index cedc966..cdb7547 100644 --- a/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResources.java +++ b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/HorseRepositoryResources.java @@ -17,6 +17,9 @@ import java.util.List; import org.jboss.logging.Logger; +/** + * This class represents the REST endpoint for the Horse repository. + */ @Path("rest/repository/horse/v1") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @@ -28,11 +31,22 @@ public class HorseRepositoryResources { @Inject Logger log; + /** + * Retrieves all horses from the repository. + * + * @return A list of horses. + */ @GET public List getAllHorses() { return horseRepository.findAll().list(); } + /** + * Retrieves horse by ID from the repository. + * + * @param id The ID of the horse. + * @return The horse. + */ @GET @Path("{id}") public Horse getHorseById(@NotNull Long id) { @@ -46,6 +60,12 @@ public Horse getHorseById(@NotNull Long id) { return horse; } + /** + * This method creates a new horse. + * + * @param horse The horse to create. + * @return The created horse. + */ @POST @Transactional public Response createHorse(@NotNull Horse horse) { @@ -60,6 +80,12 @@ public Response createHorse(@NotNull Horse horse) { return Response.ok(horse).status(Response.Status.CREATED).build(); } + /** + * This method deletes a horse by its ID. + * + * @param id The ID of the horse. + * @return The response. + */ @DELETE @Path("{id}") @Transactional diff --git a/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResources.java b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResources.java index bc9e794..b4d5672 100644 --- a/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResources.java +++ b/src/main/java/it/dontesta/eventbus/ws/resources/endpoint/repository/v1/OwnerRepositoryResources.java @@ -17,6 +17,9 @@ import java.util.List; import org.jboss.logging.Logger; +/** + * This class represents the REST endpoint for the Owner repository. + */ @Path("rest/repository/owner/v1") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @@ -33,6 +36,12 @@ public List getAllOwners() { return ownerRepository.findAll().list(); } + /** + * Retrieves owner by ID from the repository. + * + * @param id The ID of the owner. + * @return The owner. + */ @GET @Path("{id}") public Owner getOwnerById(@NotNull Long id) { @@ -46,6 +55,12 @@ public Owner getOwnerById(@NotNull Long id) { return owner; } + /** + * Creates a new owner. + * + * @param owner The owner to create. + * @return The created owner. + */ @POST @Transactional public Response createOwner(@NotNull Owner owner) { @@ -60,6 +75,12 @@ public Response createOwner(@NotNull Owner owner) { return Response.ok(owner).status(Response.Status.CREATED).build(); } + /** + * Delete existing owner. + * + * @param id The owner id to delete. + * @return The updated owner. + */ @DELETE @Path("{id}") @Transactional From 04b5f606c4f9aab3283782f7ac092ca37eb8193b Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Thu, 9 May 2024 23:38:08 +0200 Subject: [PATCH 23/23] Aggiornamento CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index feb7cbb..9af93ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Null Pointer Exception sul filtro JAX-RS per il tracking delle richieste quando il body della richiesta è vuoto +- Google Checkstyle violations ### Removed