From b89230ef337cbef6ef2d6d8d7e7a85a7a1e521d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Tue, 17 Sep 2019 18:06:20 +0200 Subject: [PATCH 01/16] feat: solution for step 1 --- .../fr/loicmathieu/bookmarkit/Bookmark.java | 4 +- .../bookmarkit/BookmarkResource.java | 44 ++++++++++++++----- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/Bookmark.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/Bookmark.java index 0f70044..5daff8f 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/Bookmark.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/Bookmark.java @@ -1,9 +1,11 @@ package fr.loicmathieu.bookmarkit; +import io.quarkus.hibernate.orm.panache.PanacheEntity; + import javax.persistence.Entity; @Entity -public class Bookmark { +public class Bookmark extends PanacheEntity { public String title; public String url; public String description; diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index a232188..15f5efd 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,10 +1,18 @@ package fr.loicmathieu.bookmarkit; +import javax.enterprise.context.ApplicationScoped; +import javax.transaction.Transactional; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.net.URI; import java.util.List; @Path("/bookmarks") @@ -12,23 +20,39 @@ @Consumes(MediaType.APPLICATION_JSON) public class BookmarkResource { - public List listBookmarks() { - throw new UnsupportedOperationException("not yet implemented"); + @GET + public List listAll(){ + return Bookmark.listAll(); } - public Bookmark getBookmark(Long id) { - throw new UnsupportedOperationException("not yet implemented"); + @GET + @Path("/{id}") + public Bookmark get(@PathParam("id") Long id) { + return Bookmark.findById(id); } - public Response createBookmark(Bookmark bookmark) { - throw new UnsupportedOperationException("not yet implemented"); + @POST + @Transactional + public Response create(Bookmark bookmark){ + bookmark.persist(); + return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); } - public Response updateBookmark(Bookmark bookmark, Long id) { - throw new UnsupportedOperationException("not yet implemented"); + @PUT + @Path("/{id}") + @Transactional + public void update(Bookmark bookmark){ + Bookmark existing = Bookmark.findById(bookmark.id); + existing.url = bookmark.url; + existing.description = bookmark.description; + existing.title = bookmark.title; } - public Response deleteBookmark(Long id) { - throw new UnsupportedOperationException("not yet implemented"); + @DELETE + @Path("/{id}") + @Transactional + public void delete(@PathParam("id")Long id){ + Bookmark existing = Bookmark.findById(id); + existing.delete(); } } From 64e86f98c44dbce573a77d0fb3786fed3fb9c6a9 Mon Sep 17 00:00:00 2001 From: Sparow199 Date: Tue, 29 Oct 2019 21:11:13 +0100 Subject: [PATCH 02/16] feat(*): step 1 feat: solution for step 1 refactor(*): Update Postman collection --- .../main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java | 1 - 1 file changed, 1 deletion(-) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 15f5efd..60ff233 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,6 +1,5 @@ package fr.loicmathieu.bookmarkit; -import javax.enterprise.context.ApplicationScoped; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; From b4b0bc507eaa799437fa39f053e1f0de823abdee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Sun, 24 May 2020 17:55:56 +0200 Subject: [PATCH 03/16] fix: bad merge --- .../fr/loicmathieu/bookmarkit/BookmarkResource.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 60ff233..150ed7d 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -20,19 +20,19 @@ public class BookmarkResource { @GET - public List listAll(){ + public List listBookmarks(){ return Bookmark.listAll(); } @GET @Path("/{id}") - public Bookmark get(@PathParam("id") Long id) { + public Bookmark getBookmark(@PathParam("id") Long id) { return Bookmark.findById(id); } @POST @Transactional - public Response create(Bookmark bookmark){ + public Response createBookmark(Bookmark bookmark){ bookmark.persist(); return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); } @@ -40,7 +40,7 @@ public Response create(Bookmark bookmark){ @PUT @Path("/{id}") @Transactional - public void update(Bookmark bookmark){ + public void updateBookmark(Bookmark bookmark){ Bookmark existing = Bookmark.findById(bookmark.id); existing.url = bookmark.url; existing.description = bookmark.description; @@ -50,7 +50,7 @@ public void update(Bookmark bookmark){ @DELETE @Path("/{id}") @Transactional - public void delete(@PathParam("id")Long id){ + public void deleteBookmark(@PathParam("id")Long id){ Bookmark existing = Bookmark.findById(id); existing.delete(); } From 117b2fd3df5c813421688002e1b0c50141127ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 18 Sep 2019 15:27:04 +0200 Subject: [PATCH 04/16] feat: solution for step 2 --- .../bookmarkit/BookmarkResource.java | 19 +++++++++++++++++++ .../loicmathieu/bookmarkit/MyHealCheck.java | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100755 bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 150ed7d..41be2e6 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,5 +1,9 @@ package fr.loicmathieu.bookmarkit; +import org.eclipse.microprofile.metrics.annotation.Counted; +import org.eclipse.microprofile.metrics.annotation.Timed; +import org.eclipse.microprofile.openapi.annotations.Operation; + import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -20,18 +24,27 @@ public class BookmarkResource { @GET + @Operation(summary = "List all bookmarks") + @Counted(name = "listAll.count") + @Timed(name="listAll.time") public List listBookmarks(){ return Bookmark.listAll(); } @GET @Path("/{id}") + @Operation(summary = "Get a bookmark") + @Counted(name = "get.count") + @Timed(name="get.time") public Bookmark getBookmark(@PathParam("id") Long id) { return Bookmark.findById(id); } @POST @Transactional + @Operation(summary = "Create a bookmark") + @Counted(name = "create.count") + @Timed(name="create.time") public Response createBookmark(Bookmark bookmark){ bookmark.persist(); return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); @@ -40,6 +53,9 @@ public Response createBookmark(Bookmark bookmark){ @PUT @Path("/{id}") @Transactional + @Operation(summary = "Update a bookmark") + @Counted(name = "update.count") + @Timed(name="update.time") public void updateBookmark(Bookmark bookmark){ Bookmark existing = Bookmark.findById(bookmark.id); existing.url = bookmark.url; @@ -50,6 +66,9 @@ public void updateBookmark(Bookmark bookmark){ @DELETE @Path("/{id}") @Transactional + @Operation(summary = "Delete a bookmark") + @Counted(name = "delete.count") + @Timed(name="delete.time") public void deleteBookmark(@PathParam("id")Long id){ Bookmark existing = Bookmark.findById(id); existing.delete(); diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java new file mode 100755 index 0000000..6806791 --- /dev/null +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java @@ -0,0 +1,19 @@ +package fr.loicmathieu.bookmarkit; + +import org.eclipse.microprofile.health.Health; +import org.eclipse.microprofile.health.HealthCheck; +import org.eclipse.microprofile.health.HealthCheckResponse; +import org.eclipse.microprofile.health.HealthCheckResponseBuilder; +import org.eclipse.microprofile.health.Readiness; +import org.jboss.resteasy.annotations.LinkHeaderParam; + +import java.lang.annotation.Annotation; + +@Readiness +public class MyHealCheck implements HealthCheck { + + @Override + public HealthCheckResponse call() { + return HealthCheckResponse.builder().name("custom").withData("key", "value").up().build(); + } +} From 5556829152cfa2040075f84823f71c0c35efd70b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 18 Sep 2019 15:27:04 +0200 Subject: [PATCH 05/16] feat: solution for step 2 --- .../fr/loicmathieu/bookmarkit/BookmarkResource.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 41be2e6..d1c74f1 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -27,7 +27,7 @@ public class BookmarkResource { @Operation(summary = "List all bookmarks") @Counted(name = "listAll.count") @Timed(name="listAll.time") - public List listBookmarks(){ + public List listAll(){ return Bookmark.listAll(); } @@ -36,7 +36,7 @@ public List listBookmarks(){ @Operation(summary = "Get a bookmark") @Counted(name = "get.count") @Timed(name="get.time") - public Bookmark getBookmark(@PathParam("id") Long id) { + public Bookmark get(@PathParam("id") Long id) { return Bookmark.findById(id); } @@ -45,7 +45,7 @@ public Bookmark getBookmark(@PathParam("id") Long id) { @Operation(summary = "Create a bookmark") @Counted(name = "create.count") @Timed(name="create.time") - public Response createBookmark(Bookmark bookmark){ + public Response create(Bookmark bookmark){ bookmark.persist(); return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); } @@ -56,7 +56,7 @@ public Response createBookmark(Bookmark bookmark){ @Operation(summary = "Update a bookmark") @Counted(name = "update.count") @Timed(name="update.time") - public void updateBookmark(Bookmark bookmark){ + public void update(Bookmark bookmark){ Bookmark existing = Bookmark.findById(bookmark.id); existing.url = bookmark.url; existing.description = bookmark.description; @@ -69,7 +69,7 @@ public void updateBookmark(Bookmark bookmark){ @Operation(summary = "Delete a bookmark") @Counted(name = "delete.count") @Timed(name="delete.time") - public void deleteBookmark(@PathParam("id")Long id){ + public void delete(@PathParam("id")Long id){ Bookmark existing = Bookmark.findById(id); existing.delete(); } From 03f25ad29f8e364db9cbab6e1f9ae1c59a8fe3a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Tue, 17 Sep 2019 18:06:20 +0200 Subject: [PATCH 06/16] feat: solution for step 1 --- .../bookmarkit/BookmarkResource.java | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index d1c74f1..15f5efd 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,9 +1,6 @@ package fr.loicmathieu.bookmarkit; -import org.eclipse.microprofile.metrics.annotation.Counted; -import org.eclipse.microprofile.metrics.annotation.Timed; -import org.eclipse.microprofile.openapi.annotations.Operation; - +import javax.enterprise.context.ApplicationScoped; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -24,27 +21,18 @@ public class BookmarkResource { @GET - @Operation(summary = "List all bookmarks") - @Counted(name = "listAll.count") - @Timed(name="listAll.time") public List listAll(){ return Bookmark.listAll(); } @GET @Path("/{id}") - @Operation(summary = "Get a bookmark") - @Counted(name = "get.count") - @Timed(name="get.time") public Bookmark get(@PathParam("id") Long id) { return Bookmark.findById(id); } @POST @Transactional - @Operation(summary = "Create a bookmark") - @Counted(name = "create.count") - @Timed(name="create.time") public Response create(Bookmark bookmark){ bookmark.persist(); return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); @@ -53,9 +41,6 @@ public Response create(Bookmark bookmark){ @PUT @Path("/{id}") @Transactional - @Operation(summary = "Update a bookmark") - @Counted(name = "update.count") - @Timed(name="update.time") public void update(Bookmark bookmark){ Bookmark existing = Bookmark.findById(bookmark.id); existing.url = bookmark.url; @@ -66,9 +51,6 @@ public void update(Bookmark bookmark){ @DELETE @Path("/{id}") @Transactional - @Operation(summary = "Delete a bookmark") - @Counted(name = "delete.count") - @Timed(name="delete.time") public void delete(@PathParam("id")Long id){ Bookmark existing = Bookmark.findById(id); existing.delete(); From 4d99d300a9c860d65a875f17a3c7db2c23eb7e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 18 Sep 2019 15:27:04 +0200 Subject: [PATCH 07/16] feat: solution for step 2 --- .../bookmarkit/BookmarkResource.java | 20 +++++++++- .../bookmarkit/BookmarkStep2Test.java | 38 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 15f5efd..d1c74f1 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,6 +1,9 @@ package fr.loicmathieu.bookmarkit; -import javax.enterprise.context.ApplicationScoped; +import org.eclipse.microprofile.metrics.annotation.Counted; +import org.eclipse.microprofile.metrics.annotation.Timed; +import org.eclipse.microprofile.openapi.annotations.Operation; + import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -21,18 +24,27 @@ public class BookmarkResource { @GET + @Operation(summary = "List all bookmarks") + @Counted(name = "listAll.count") + @Timed(name="listAll.time") public List listAll(){ return Bookmark.listAll(); } @GET @Path("/{id}") + @Operation(summary = "Get a bookmark") + @Counted(name = "get.count") + @Timed(name="get.time") public Bookmark get(@PathParam("id") Long id) { return Bookmark.findById(id); } @POST @Transactional + @Operation(summary = "Create a bookmark") + @Counted(name = "create.count") + @Timed(name="create.time") public Response create(Bookmark bookmark){ bookmark.persist(); return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); @@ -41,6 +53,9 @@ public Response create(Bookmark bookmark){ @PUT @Path("/{id}") @Transactional + @Operation(summary = "Update a bookmark") + @Counted(name = "update.count") + @Timed(name="update.time") public void update(Bookmark bookmark){ Bookmark existing = Bookmark.findById(bookmark.id); existing.url = bookmark.url; @@ -51,6 +66,9 @@ public void update(Bookmark bookmark){ @DELETE @Path("/{id}") @Transactional + @Operation(summary = "Delete a bookmark") + @Counted(name = "delete.count") + @Timed(name="delete.time") public void delete(@PathParam("id")Long id){ Bookmark existing = Bookmark.findById(id); existing.delete(); diff --git a/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java b/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java new file mode 100644 index 0000000..33dce76 --- /dev/null +++ b/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java @@ -0,0 +1,38 @@ +package fr.loicmathieu.bookmarkit; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.RestAssured; +import io.restassured.parsing.Parser; +import org.hamcrest.CoreMatchers; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; + +@QuarkusTest +public class BookmarkStep2Test { + @Test + public void step2() { + RestAssured.defaultParser = Parser.JSON; + + //check that a summary has been added to the default OpenAPI + given().accept("application/json") + .when().get("/openapi") + .then() + .statusCode(200) + .body("paths.'/bookmarks'.get.summary", CoreMatchers.is(CoreMatchers.notNullValue())); + + //check that metrics exist for each operations + given().accept("application/json") + .when().get("/metrics/application") + .then() + .statusCode(200) + .body("size()", CoreMatchers.is(10)); + + //check that a custom readiness check has been created + given().accept("application/json") + .when().get("/health/ready") + .then() + .statusCode(200) + .body("checks[0].name", CoreMatchers.is("custom")); + } +} From cead24cbdbd53efb0e59ffabf51f5a01d5aea1b0 Mon Sep 17 00:00:00 2001 From: Sparow199 Date: Tue, 29 Oct 2019 21:34:06 +0100 Subject: [PATCH 08/16] feat(*): step 2 --- .../bookmarkit/AppHealthCheck.java | 14 +++++ .../bookmarkit/BookmarkResource.java | 58 ++++++++++--------- .../loicmathieu/bookmarkit/MyHealCheck.java | 19 ------ .../bookmarkit/BookmarkStep2Test.java | 38 ------------ 4 files changed, 46 insertions(+), 83 deletions(-) create mode 100644 bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/AppHealthCheck.java delete mode 100755 bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java delete mode 100644 bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/AppHealthCheck.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/AppHealthCheck.java new file mode 100644 index 0000000..11cb5bf --- /dev/null +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/AppHealthCheck.java @@ -0,0 +1,14 @@ +package fr.loicmathieu.bookmarkit; + +import org.eclipse.microprofile.health.HealthCheck; +import org.eclipse.microprofile.health.HealthCheckResponse; +import org.eclipse.microprofile.health.Readiness; + +@Readiness +public class AppHealthCheck implements HealthCheck { + + @Override + public HealthCheckResponse call() { + return HealthCheckResponse.builder().name("bookmark").withData("is", "always").up().build(); + } +} diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index d1c74f1..26c079a 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -13,8 +13,10 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; import java.net.URI; import java.util.List; @@ -25,52 +27,56 @@ public class BookmarkResource { @GET @Operation(summary = "List all bookmarks") - @Counted(name = "listAll.count") - @Timed(name="listAll.time") - public List listAll(){ + @Counted(name = "listBookmarks.count") + @Timed(name = "listBookmarks.time") + public List listBookmarks() { return Bookmark.listAll(); } @GET - @Path("/{id}") + @Path("{id}") @Operation(summary = "Get a bookmark") - @Counted(name = "get.count") - @Timed(name="get.time") - public Bookmark get(@PathParam("id") Long id) { + @Counted(name = "getBookmark.count") + @Timed(name = "getBookmark.time") + public Bookmark getBookmark(@PathParam("id") Long id) { return Bookmark.findById(id); } @POST @Transactional @Operation(summary = "Create a bookmark") - @Counted(name = "create.count") - @Timed(name="create.time") - public Response create(Bookmark bookmark){ - bookmark.persist(); - return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); + @Counted(name = "createBookmark.count") + @Timed(name = "createBookmark.time") + public Response createBookmark(Bookmark bookmark) { + bookmark.persist(); + return Response.status(Response.Status.CREATED).entity(bookmark).build(); } @PUT - @Path("/{id}") + @Path("{id}") @Transactional @Operation(summary = "Update a bookmark") - @Counted(name = "update.count") - @Timed(name="update.time") - public void update(Bookmark bookmark){ - Bookmark existing = Bookmark.findById(bookmark.id); - existing.url = bookmark.url; - existing.description = bookmark.description; - existing.title = bookmark.title; + @Counted(name = "updateBookmark.count") + @Timed(name = "updateBookmark.time") + public void updateBookmark(Bookmark bookmark, @PathParam("id") Long id) { + Bookmark entity = Bookmark.findById(id); + entity.description = bookmark.description; + entity.location = bookmark.location; + entity.title = bookmark.title; + entity.url = bookmark.url; } @DELETE - @Path("/{id}") + @Path("{id}") @Transactional @Operation(summary = "Delete a bookmark") - @Counted(name = "delete.count") - @Timed(name="delete.time") - public void delete(@PathParam("id")Long id){ - Bookmark existing = Bookmark.findById(id); - existing.delete(); + @Counted(name = "deleteBookmark.count") + @Timed(name = "deleteBookmark.time") + public Response deleteBookmark(@PathParam("id") Long id) { + Bookmark bookmark = Bookmark.findById(id); + if (bookmark != null) { + bookmark.delete(); + } + return Response.noContent().build(); } } diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java deleted file mode 100755 index 6806791..0000000 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java +++ /dev/null @@ -1,19 +0,0 @@ -package fr.loicmathieu.bookmarkit; - -import org.eclipse.microprofile.health.Health; -import org.eclipse.microprofile.health.HealthCheck; -import org.eclipse.microprofile.health.HealthCheckResponse; -import org.eclipse.microprofile.health.HealthCheckResponseBuilder; -import org.eclipse.microprofile.health.Readiness; -import org.jboss.resteasy.annotations.LinkHeaderParam; - -import java.lang.annotation.Annotation; - -@Readiness -public class MyHealCheck implements HealthCheck { - - @Override - public HealthCheckResponse call() { - return HealthCheckResponse.builder().name("custom").withData("key", "value").up().build(); - } -} diff --git a/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java b/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java deleted file mode 100644 index 33dce76..0000000 --- a/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package fr.loicmathieu.bookmarkit; - -import io.quarkus.test.junit.QuarkusTest; -import io.restassured.RestAssured; -import io.restassured.parsing.Parser; -import org.hamcrest.CoreMatchers; -import org.junit.jupiter.api.Test; - -import static io.restassured.RestAssured.given; - -@QuarkusTest -public class BookmarkStep2Test { - @Test - public void step2() { - RestAssured.defaultParser = Parser.JSON; - - //check that a summary has been added to the default OpenAPI - given().accept("application/json") - .when().get("/openapi") - .then() - .statusCode(200) - .body("paths.'/bookmarks'.get.summary", CoreMatchers.is(CoreMatchers.notNullValue())); - - //check that metrics exist for each operations - given().accept("application/json") - .when().get("/metrics/application") - .then() - .statusCode(200) - .body("size()", CoreMatchers.is(10)); - - //check that a custom readiness check has been created - given().accept("application/json") - .when().get("/health/ready") - .then() - .statusCode(200) - .body("checks[0].name", CoreMatchers.is("custom")); - } -} From 102df79a8980dc4a461e1bb9d9dd6e5988f23d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 18 Sep 2019 15:27:04 +0200 Subject: [PATCH 09/16] feat: solution for step 3 --- .../java/fr/loicmathieu/bookmarkit/BookmarkResource.java | 8 ++++++++ .../src/main/resources/application.properties | 3 +++ 2 files changed, 11 insertions(+) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 26c079a..5ec10ec 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,9 +1,11 @@ package fr.loicmathieu.bookmarkit; +import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.metrics.annotation.Counted; import org.eclipse.microprofile.metrics.annotation.Timed; import org.eclipse.microprofile.openapi.annotations.Operation; +import javax.annotation.PostConstruct; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -24,6 +26,12 @@ @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class BookmarkResource { + @ConfigProperty(name="greeting") String greeting; + + @PostConstruct + void init(){ + System.out.println("Hello " + greeting); + } @GET @Operation(summary = "List all bookmarks") diff --git a/bookmark-service/src/main/resources/application.properties b/bookmark-service/src/main/resources/application.properties index 4747645..10e1519 100644 --- a/bookmark-service/src/main/resources/application.properties +++ b/bookmark-service/src/main/resources/application.properties @@ -6,3 +6,6 @@ quarkus.datasource.password=quarkus # drop and create the database at startup (use `update` to only update the schema) quarkus.hibernate-orm.database.generation=drop-and-create quarkus.hibernate-orm.sql-load-script=import.sql + +greeting=World +%dev.greeting=Dev \ No newline at end of file From a74a35ff9fe4dfe325cf9b05804328a8cea6369f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 18 Sep 2019 15:27:04 +0200 Subject: [PATCH 10/16] feat: solution for step 2 --- .../bookmarkit/BookmarkResource.java | 47 +++++++++---------- .../loicmathieu/bookmarkit/MyHealCheck.java | 19 ++++++++ 2 files changed, 41 insertions(+), 25 deletions(-) create mode 100755 bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 5ec10ec..5f0f4ea 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,11 +1,9 @@ package fr.loicmathieu.bookmarkit; -import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.metrics.annotation.Counted; import org.eclipse.microprofile.metrics.annotation.Timed; import org.eclipse.microprofile.openapi.annotations.Operation; -import javax.annotation.PostConstruct; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -35,53 +33,52 @@ void init(){ @GET @Operation(summary = "List all bookmarks") - @Counted(name = "listBookmarks.count") - @Timed(name = "listBookmarks.time") - public List listBookmarks() { + @Counted(name = "listAll.count") + @Timed(name="listAll.time") + public List listAll(){ return Bookmark.listAll(); } @GET - @Path("{id}") + @Path("/{id}") @Operation(summary = "Get a bookmark") - @Counted(name = "getBookmark.count") - @Timed(name = "getBookmark.time") - public Bookmark getBookmark(@PathParam("id") Long id) { + @Counted(name = "get.count") + @Timed(name="get.time") + public Bookmark get(@PathParam("id") Long id) { return Bookmark.findById(id); } @POST @Transactional @Operation(summary = "Create a bookmark") - @Counted(name = "createBookmark.count") - @Timed(name = "createBookmark.time") - public Response createBookmark(Bookmark bookmark) { - bookmark.persist(); - return Response.status(Response.Status.CREATED).entity(bookmark).build(); + @Counted(name = "create.count") + @Timed(name="create.time") + public Response create(Bookmark bookmark){ + bookmark.persist(); + return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); } @PUT @Path("{id}") @Transactional @Operation(summary = "Update a bookmark") - @Counted(name = "updateBookmark.count") - @Timed(name = "updateBookmark.time") - public void updateBookmark(Bookmark bookmark, @PathParam("id") Long id) { - Bookmark entity = Bookmark.findById(id); - entity.description = bookmark.description; + @Counted(name = "update.count") + @Timed(name="update.time") + public void update(Bookmark bookmark){ + Bookmark existing = Bookmark.findById(bookmark.id); + existing.url = bookmark.url; + existing.description = bookmark.description; + existing.title = bookmark.title; entity.location = bookmark.location; - entity.title = bookmark.title; - entity.url = bookmark.url; } @DELETE @Path("{id}") @Transactional @Operation(summary = "Delete a bookmark") - @Counted(name = "deleteBookmark.count") - @Timed(name = "deleteBookmark.time") - public Response deleteBookmark(@PathParam("id") Long id) { - Bookmark bookmark = Bookmark.findById(id); + @Counted(name = "delete.count") + @Timed(name="delete.time") + public void delete(@PathParam("id")Long id){ if (bookmark != null) { bookmark.delete(); } diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java new file mode 100755 index 0000000..6806791 --- /dev/null +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java @@ -0,0 +1,19 @@ +package fr.loicmathieu.bookmarkit; + +import org.eclipse.microprofile.health.Health; +import org.eclipse.microprofile.health.HealthCheck; +import org.eclipse.microprofile.health.HealthCheckResponse; +import org.eclipse.microprofile.health.HealthCheckResponseBuilder; +import org.eclipse.microprofile.health.Readiness; +import org.jboss.resteasy.annotations.LinkHeaderParam; + +import java.lang.annotation.Annotation; + +@Readiness +public class MyHealCheck implements HealthCheck { + + @Override + public HealthCheckResponse call() { + return HealthCheckResponse.builder().name("custom").withData("key", "value").up().build(); + } +} From fd460bc6e32c1a77dcaa305ab7c4853920d59493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 18 Sep 2019 15:47:02 +0200 Subject: [PATCH 11/16] feat: solution for step 3 --- .../main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 5f0f4ea..0874cd8 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,9 +1,11 @@ package fr.loicmathieu.bookmarkit; +import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.metrics.annotation.Counted; import org.eclipse.microprofile.metrics.annotation.Timed; import org.eclipse.microprofile.openapi.annotations.Operation; +import javax.annotation.PostConstruct; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; From 8c659e09788ad22bab54351ddd942eb5cb8e7c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Tue, 17 Sep 2019 18:06:20 +0200 Subject: [PATCH 12/16] feat: solution for step 1 --- .../bookmarkit/BookmarkResource.java | 27 +------------------ 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 0874cd8..d9f18d6 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,11 +1,6 @@ package fr.loicmathieu.bookmarkit; -import org.eclipse.microprofile.config.inject.ConfigProperty; -import org.eclipse.microprofile.metrics.annotation.Counted; -import org.eclipse.microprofile.metrics.annotation.Timed; -import org.eclipse.microprofile.openapi.annotations.Operation; - -import javax.annotation.PostConstruct; +import javax.enterprise.context.ApplicationScoped; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -28,33 +23,19 @@ public class BookmarkResource { @ConfigProperty(name="greeting") String greeting; - @PostConstruct - void init(){ - System.out.println("Hello " + greeting); - } - @GET - @Operation(summary = "List all bookmarks") - @Counted(name = "listAll.count") - @Timed(name="listAll.time") public List listAll(){ return Bookmark.listAll(); } @GET @Path("/{id}") - @Operation(summary = "Get a bookmark") - @Counted(name = "get.count") - @Timed(name="get.time") public Bookmark get(@PathParam("id") Long id) { return Bookmark.findById(id); } @POST @Transactional - @Operation(summary = "Create a bookmark") - @Counted(name = "create.count") - @Timed(name="create.time") public Response create(Bookmark bookmark){ bookmark.persist(); return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); @@ -63,9 +44,6 @@ public Response create(Bookmark bookmark){ @PUT @Path("{id}") @Transactional - @Operation(summary = "Update a bookmark") - @Counted(name = "update.count") - @Timed(name="update.time") public void update(Bookmark bookmark){ Bookmark existing = Bookmark.findById(bookmark.id); existing.url = bookmark.url; @@ -77,9 +55,6 @@ public void update(Bookmark bookmark){ @DELETE @Path("{id}") @Transactional - @Operation(summary = "Delete a bookmark") - @Counted(name = "delete.count") - @Timed(name="delete.time") public void delete(@PathParam("id")Long id){ if (bookmark != null) { bookmark.delete(); From 8c9229af4835a940357d30a2ff311522354204f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 18 Sep 2019 15:27:04 +0200 Subject: [PATCH 13/16] feat: solution for step 2 --- .../bookmarkit/BookmarkResource.java | 20 +++++++++- .../bookmarkit/BookmarkStep2Test.java | 38 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index d9f18d6..61acd1e 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,6 +1,9 @@ package fr.loicmathieu.bookmarkit; -import javax.enterprise.context.ApplicationScoped; +import org.eclipse.microprofile.metrics.annotation.Counted; +import org.eclipse.microprofile.metrics.annotation.Timed; +import org.eclipse.microprofile.openapi.annotations.Operation; + import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -24,18 +27,27 @@ public class BookmarkResource { @ConfigProperty(name="greeting") String greeting; @GET + @Operation(summary = "List all bookmarks") + @Counted(name = "listAll.count") + @Timed(name="listAll.time") public List listAll(){ return Bookmark.listAll(); } @GET @Path("/{id}") + @Operation(summary = "Get a bookmark") + @Counted(name = "get.count") + @Timed(name="get.time") public Bookmark get(@PathParam("id") Long id) { return Bookmark.findById(id); } @POST @Transactional + @Operation(summary = "Create a bookmark") + @Counted(name = "create.count") + @Timed(name="create.time") public Response create(Bookmark bookmark){ bookmark.persist(); return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); @@ -44,6 +56,9 @@ public Response create(Bookmark bookmark){ @PUT @Path("{id}") @Transactional + @Operation(summary = "Update a bookmark") + @Counted(name = "update.count") + @Timed(name="update.time") public void update(Bookmark bookmark){ Bookmark existing = Bookmark.findById(bookmark.id); existing.url = bookmark.url; @@ -55,6 +70,9 @@ public void update(Bookmark bookmark){ @DELETE @Path("{id}") @Transactional + @Operation(summary = "Delete a bookmark") + @Counted(name = "delete.count") + @Timed(name="delete.time") public void delete(@PathParam("id")Long id){ if (bookmark != null) { bookmark.delete(); diff --git a/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java b/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java new file mode 100644 index 0000000..33dce76 --- /dev/null +++ b/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java @@ -0,0 +1,38 @@ +package fr.loicmathieu.bookmarkit; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.RestAssured; +import io.restassured.parsing.Parser; +import org.hamcrest.CoreMatchers; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; + +@QuarkusTest +public class BookmarkStep2Test { + @Test + public void step2() { + RestAssured.defaultParser = Parser.JSON; + + //check that a summary has been added to the default OpenAPI + given().accept("application/json") + .when().get("/openapi") + .then() + .statusCode(200) + .body("paths.'/bookmarks'.get.summary", CoreMatchers.is(CoreMatchers.notNullValue())); + + //check that metrics exist for each operations + given().accept("application/json") + .when().get("/metrics/application") + .then() + .statusCode(200) + .body("size()", CoreMatchers.is(10)); + + //check that a custom readiness check has been created + given().accept("application/json") + .when().get("/health/ready") + .then() + .statusCode(200) + .body("checks[0].name", CoreMatchers.is("custom")); + } +} From 88567e72471fa4b93ac21c5c1a14dc9ab96b14f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Wed, 18 Sep 2019 15:47:02 +0200 Subject: [PATCH 14/16] feat: solution for step 3 --- .../java/fr/loicmathieu/bookmarkit/BookmarkResource.java | 7 +++++++ bookmark-service/src/main/resources/application.properties | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 61acd1e..0874cd8 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,9 +1,11 @@ package fr.loicmathieu.bookmarkit; +import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.metrics.annotation.Counted; import org.eclipse.microprofile.metrics.annotation.Timed; import org.eclipse.microprofile.openapi.annotations.Operation; +import javax.annotation.PostConstruct; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -26,6 +28,11 @@ public class BookmarkResource { @ConfigProperty(name="greeting") String greeting; + @PostConstruct + void init(){ + System.out.println("Hello " + greeting); + } + @GET @Operation(summary = "List all bookmarks") @Counted(name = "listAll.count") diff --git a/bookmark-service/src/main/resources/application.properties b/bookmark-service/src/main/resources/application.properties index 10e1519..c4a5f7f 100644 --- a/bookmark-service/src/main/resources/application.properties +++ b/bookmark-service/src/main/resources/application.properties @@ -5,7 +5,6 @@ quarkus.datasource.username=quarkus quarkus.datasource.password=quarkus # drop and create the database at startup (use `update` to only update the schema) quarkus.hibernate-orm.database.generation=drop-and-create -quarkus.hibernate-orm.sql-load-script=import.sql greeting=World %dev.greeting=Dev \ No newline at end of file From 3001597bbafe6ecd948e59b9b7c91bcd99ab6824 Mon Sep 17 00:00:00 2001 From: Sparow199 Date: Tue, 29 Oct 2019 21:21:40 +0100 Subject: [PATCH 15/16] feat(*): step 3 feat(*): Add step 5 feat(*): Add step 3 feat: small enhancements refactor(*): Update Postman collection --- .../bookmarkit/BookmarkResource.java | 59 ++++++++++--------- .../loicmathieu/bookmarkit/MyHealCheck.java | 19 ------ .../src/main/resources/application.properties | 1 + .../bookmarkit/BookmarkStep2Test.java | 38 ------------ 4 files changed, 33 insertions(+), 84 deletions(-) delete mode 100755 bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java delete mode 100644 bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 0874cd8..7bda265 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -4,6 +4,8 @@ import org.eclipse.microprofile.metrics.annotation.Counted; import org.eclipse.microprofile.metrics.annotation.Timed; import org.eclipse.microprofile.openapi.annotations.Operation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.annotation.PostConstruct; import javax.transaction.Transactional; @@ -15,72 +17,75 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriInfo; -import java.net.URI; import java.util.List; @Path("/bookmarks") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class BookmarkResource { - @ConfigProperty(name="greeting") String greeting; + + private static final Logger LOGGER = LoggerFactory.getLogger(BookmarkResource.class); + + @ConfigProperty(name = "greeting") + private String greeting; @PostConstruct - void init(){ - System.out.println("Hello " + greeting); + void init() { + LOGGER.info("Hello {}", greeting); } + @GET @Operation(summary = "List all bookmarks") - @Counted(name = "listAll.count") - @Timed(name="listAll.time") - public List listAll(){ + @Counted(name = "listBookmarks.count") + @Timed(name = "listBookmarks.time") + public List listBookmarks() { return Bookmark.listAll(); } @GET - @Path("/{id}") + @Path("{id}") @Operation(summary = "Get a bookmark") - @Counted(name = "get.count") - @Timed(name="get.time") - public Bookmark get(@PathParam("id") Long id) { + @Counted(name = "getBookmark.count") + @Timed(name = "getBookmark.time") + public Bookmark getBookmark(@PathParam("id") Long id) { return Bookmark.findById(id); } @POST @Transactional @Operation(summary = "Create a bookmark") - @Counted(name = "create.count") - @Timed(name="create.time") - public Response create(Bookmark bookmark){ + @Counted(name = "createBookmark.count") + @Timed(name = "createBookmark.time") + public Response createBookmark(Bookmark bookmark) { bookmark.persist(); - return Response.created(URI.create("/bookmarks/" + bookmark.id)).build(); + return Response.status(Response.Status.CREATED).entity(bookmark).build(); } @PUT @Path("{id}") @Transactional @Operation(summary = "Update a bookmark") - @Counted(name = "update.count") - @Timed(name="update.time") - public void update(Bookmark bookmark){ - Bookmark existing = Bookmark.findById(bookmark.id); - existing.url = bookmark.url; - existing.description = bookmark.description; - existing.title = bookmark.title; + @Counted(name = "updateBookmark.count") + @Timed(name = "updateBookmark.time") + public void updateBookmark(Bookmark bookmark, @PathParam("id") Long id) { + Bookmark entity = Bookmark.findById(id); + entity.description = bookmark.description; entity.location = bookmark.location; + entity.title = bookmark.title; + entity.url = bookmark.url; } @DELETE @Path("{id}") @Transactional @Operation(summary = "Delete a bookmark") - @Counted(name = "delete.count") - @Timed(name="delete.time") - public void delete(@PathParam("id")Long id){ + @Counted(name = "deleteBookmark.count") + @Timed(name = "deleteBookmark.time") + public Response deleteBookmark(@PathParam("id") Long id) { + Bookmark bookmark = Bookmark.findById(id); if (bookmark != null) { bookmark.delete(); } diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java deleted file mode 100755 index 6806791..0000000 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/MyHealCheck.java +++ /dev/null @@ -1,19 +0,0 @@ -package fr.loicmathieu.bookmarkit; - -import org.eclipse.microprofile.health.Health; -import org.eclipse.microprofile.health.HealthCheck; -import org.eclipse.microprofile.health.HealthCheckResponse; -import org.eclipse.microprofile.health.HealthCheckResponseBuilder; -import org.eclipse.microprofile.health.Readiness; -import org.jboss.resteasy.annotations.LinkHeaderParam; - -import java.lang.annotation.Annotation; - -@Readiness -public class MyHealCheck implements HealthCheck { - - @Override - public HealthCheckResponse call() { - return HealthCheckResponse.builder().name("custom").withData("key", "value").up().build(); - } -} diff --git a/bookmark-service/src/main/resources/application.properties b/bookmark-service/src/main/resources/application.properties index c4a5f7f..10e1519 100644 --- a/bookmark-service/src/main/resources/application.properties +++ b/bookmark-service/src/main/resources/application.properties @@ -5,6 +5,7 @@ quarkus.datasource.username=quarkus quarkus.datasource.password=quarkus # drop and create the database at startup (use `update` to only update the schema) quarkus.hibernate-orm.database.generation=drop-and-create +quarkus.hibernate-orm.sql-load-script=import.sql greeting=World %dev.greeting=Dev \ No newline at end of file diff --git a/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java b/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java deleted file mode 100644 index 33dce76..0000000 --- a/bookmark-service/src/test/java/fr/loicmathieu/bookmarkit/BookmarkStep2Test.java +++ /dev/null @@ -1,38 +0,0 @@ -package fr.loicmathieu.bookmarkit; - -import io.quarkus.test.junit.QuarkusTest; -import io.restassured.RestAssured; -import io.restassured.parsing.Parser; -import org.hamcrest.CoreMatchers; -import org.junit.jupiter.api.Test; - -import static io.restassured.RestAssured.given; - -@QuarkusTest -public class BookmarkStep2Test { - @Test - public void step2() { - RestAssured.defaultParser = Parser.JSON; - - //check that a summary has been added to the default OpenAPI - given().accept("application/json") - .when().get("/openapi") - .then() - .statusCode(200) - .body("paths.'/bookmarks'.get.summary", CoreMatchers.is(CoreMatchers.notNullValue())); - - //check that metrics exist for each operations - given().accept("application/json") - .when().get("/metrics/application") - .then() - .statusCode(200) - .body("size()", CoreMatchers.is(10)); - - //check that a custom readiness check has been created - given().accept("application/json") - .when().get("/health/ready") - .then() - .statusCode(200) - .body("checks[0].name", CoreMatchers.is("custom")); - } -} From f17a81f1595cbb58dd6745c60ebb0c783afc1f03 Mon Sep 17 00:00:00 2001 From: Sparow199 Date: Tue, 29 Oct 2019 21:21:40 +0100 Subject: [PATCH 16/16] feat(*): step 3 feat(*): Add step 5 feat(*): Add step 3 feat: small enhancements refactor(*): Update Postman collection --- .../java/fr/loicmathieu/bookmarkit/BookmarkResource.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index 7bda265..3a1953c 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -4,8 +4,7 @@ import org.eclipse.microprofile.metrics.annotation.Counted; import org.eclipse.microprofile.metrics.annotation.Timed; import org.eclipse.microprofile.openapi.annotations.Operation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.jboss.logging.Logger; import javax.annotation.PostConstruct; import javax.transaction.Transactional; @@ -26,14 +25,14 @@ @Consumes(MediaType.APPLICATION_JSON) public class BookmarkResource { - private static final Logger LOGGER = LoggerFactory.getLogger(BookmarkResource.class); + private static final Logger LOGGER = Logger.getLogger(BookmarkResource.class); @ConfigProperty(name = "greeting") private String greeting; @PostConstruct void init() { - LOGGER.info("Hello {}", greeting); + LOGGER.infof("Hello %s", greeting); }