From 41b5d2eeecaa577a3641b269550fd7fb624ff863 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Mon, 26 Jun 2023 19:21:52 -0300 Subject: [PATCH 1/3] feat: create jnosql-bean-validation sample project Signed-off-by: Maximillian Arruda --- jnosql-bean-validation/README.md | 57 +++++++++ jnosql-bean-validation/docker-compose.yaml | 20 ++++ jnosql-bean-validation/pom.xml | 113 ++++++++++++++++++ .../JNoSQLBeanValidationRestApplication.java | 11 ++ .../jnosql/bean/validation/Student.java | 50 ++++++++ .../bean/validation/StudentsResource.java | 46 +++++++ .../bean/validation/openapi/Booking.java | 34 ++++++ .../validation/openapi/BookingController.java | 46 +++++++ .../bean/validation/openapi/Destination.java | 30 +++++ .../src/main/liberty/config/server.xml | 26 ++++ .../src/main/resources/.gitkeep | 0 .../META-INF/microprofile-config.properties | 5 + .../src/main/webapp/.gitkeep | 0 .../src/main/webapp/WEB-INF/beans.xml | 6 + .../src/main/webapp/index.html | 39 ++++++ pom.xml | 1 + 16 files changed, 484 insertions(+) create mode 100644 jnosql-bean-validation/README.md create mode 100644 jnosql-bean-validation/docker-compose.yaml create mode 100644 jnosql-bean-validation/pom.xml create mode 100644 jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/JNoSQLBeanValidationRestApplication.java create mode 100644 jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/Student.java create mode 100644 jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/StudentsResource.java create mode 100644 jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/Booking.java create mode 100644 jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/BookingController.java create mode 100644 jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/Destination.java create mode 100644 jnosql-bean-validation/src/main/liberty/config/server.xml create mode 100644 jnosql-bean-validation/src/main/resources/.gitkeep create mode 100644 jnosql-bean-validation/src/main/resources/META-INF/microprofile-config.properties create mode 100644 jnosql-bean-validation/src/main/webapp/.gitkeep create mode 100644 jnosql-bean-validation/src/main/webapp/WEB-INF/beans.xml create mode 100644 jnosql-bean-validation/src/main/webapp/index.html diff --git a/jnosql-bean-validation/README.md b/jnosql-bean-validation/README.md new file mode 100644 index 0000000..e8b5fc7 --- /dev/null +++ b/jnosql-bean-validation/README.md @@ -0,0 +1,57 @@ +# MicroProfile generated Application + +## Introduction + +MicroProfile Starter has generated this MicroProfile application for you. + +The generation of the executable jar file can be performed by issuing the following command + + + mvn clean package + +This will create an executable jar file **jnosql-bean-validation.jar** within the _target_ maven folder. This can be started by executing the following command + + java -jar target/jnosql-bean-validation.jar + + + +### Liberty Dev Mode + +During development, you can use Liberty's development mode (dev mode) to code while observing and testing your changes on the fly. +With the dev mode, you can code along and watch the change reflected in the running server right away; +unit and integration tests are run on pressing Enter in the command terminal; you can attach a debugger to the running server at any time to step through your code. + + + mvn liberty:dev + + + + + +To launch the test page, open your browser at the following URL + + http://localhost:9080/index.html + + + +## Specification examples + +By default, there is always the creation of a JAX-RS application class to define the path on which the JAX-RS endpoints are available. + +Also, a simple Hello world endpoint is created, have a look at the class **HelloController**. + +More information on MicroProfile can be found [here](https://microprofile.io/) + + +### Config + +Configuration of your application parameters. Specification [here](https://microprofile.io/project/eclipse/microprofile-config) + +The example class **ConfigTestController** shows you how to inject a configuration parameter and how you can retrieve it programmatically. + +### Open API + +Exposes the information about your endpoints in the format of the OpenAPI v3 specification. Specification [here](https://microprofile.io/project/eclipse/microprofile-open-api) + +The index page contains a link to the OpenAPI information of your endpoints. + diff --git a/jnosql-bean-validation/docker-compose.yaml b/jnosql-bean-validation/docker-compose.yaml new file mode 100644 index 0000000..7f7b97b --- /dev/null +++ b/jnosql-bean-validation/docker-compose.yaml @@ -0,0 +1,20 @@ +services: + + mongo: + image: mongo + restart: always + environment: + MONGO_INITDB_ROOT_USERNAME: root + MONGO_INITDB_ROOT_PASSWORD: example + ports: + - 27017:27017 + + mongo-express: + image: mongo-express + restart: always + ports: + - 8081:8081 + environment: + ME_CONFIG_MONGODB_ADMINUSERNAME: root + ME_CONFIG_MONGODB_ADMINPASSWORD: example + ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/ \ No newline at end of file diff --git a/jnosql-bean-validation/pom.xml b/jnosql-bean-validation/pom.xml new file mode 100644 index 0000000..ba24f7a --- /dev/null +++ b/jnosql-bean-validation/pom.xml @@ -0,0 +1,113 @@ + + + 4.0.0 + org.eclipse.jnosql.demoee + jnosql-bean-validation + 1.0-SNAPSHOT + war + + 3.5.1 + 17 + false + 1.5.0 + 1.7.30 + 17 + jnosql-bean-validation + 1.7.30 + 1.0.0 + + + + org.eclipse.microprofile + microprofile + 5.0 + pom + provided + + + org.eclipse.jnosql.databases + jnosql-mongodb + ${jnosql.version} + + + org.eclipse.jnosql.mapping + jnosql-mapping-validation + ${jnosql.version} + + + jakarta.validation + jakarta.validation-api + 3.0.2 + provided + + + + jnosql-bean-validation + + + + liberty + + true + + + + + maven-war-plugin + 3.3.2 + + false + + + + io.openliberty.tools + liberty-maven-plugin + ${openliberty.maven.version} + + + package-server + package + + create + install-feature + deploy + package + + + target/wlp-package + + + + + runnable + ${final.name} + + ${final.name} + https://server.example.com + / + + + + + + + + io.jaegertracing + jaeger-client + ${jaeger.client.version} + + + org.slf4j + slf4j-api + ${slf4j.api.version} + + + org.slf4j + slf4j-jdk14 + ${slf4j.jdk.version} + + + + + diff --git a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/JNoSQLBeanValidationRestApplication.java b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/JNoSQLBeanValidationRestApplication.java new file mode 100644 index 0000000..a3a880d --- /dev/null +++ b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/JNoSQLBeanValidationRestApplication.java @@ -0,0 +1,11 @@ +package org.eclipse.jnosql.demoee.jnosql.bean.validation; + +import jakarta.ws.rs.ApplicationPath; +import jakarta.ws.rs.core.Application; + +/** + * + */ +@ApplicationPath("/api") +public class JNoSQLBeanValidationRestApplication extends Application { +} diff --git a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/Student.java b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/Student.java new file mode 100644 index 0000000..1b1a2a2 --- /dev/null +++ b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/Student.java @@ -0,0 +1,50 @@ +package org.eclipse.jnosql.demoee.jnosql.bean.validation; + +import jakarta.nosql.Column; +import jakarta.nosql.Entity; +import jakarta.nosql.Id; +import org.eclipse.microprofile.openapi.annotations.media.Schema; + +@Schema(name = "Student") +@Entity +public class Student { + + @Id + @Schema(description = "Student id") + private String id; + + @Column + @Schema(description = "Student name") + private String name; + + @Column + @Schema(description = "Student age") + private int age; + + public Student() { + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + public void setId(String id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/StudentsResource.java b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/StudentsResource.java new file mode 100644 index 0000000..37d3739 --- /dev/null +++ b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/StudentsResource.java @@ -0,0 +1,46 @@ +package org.eclipse.jnosql.demoee.jnosql.bean.validation; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.nosql.Template; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import org.eclipse.microprofile.openapi.annotations.OpenAPIDefinition; +import org.eclipse.microprofile.openapi.annotations.info.Info; +import org.eclipse.microprofile.openapi.annotations.media.Content; +import org.eclipse.microprofile.openapi.annotations.media.Schema; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; + + +@ApplicationScoped +@OpenAPIDefinition(info = @Info(title = "Students endpoint", version = "1.0")) + +@Path("students") +@Consumes({MediaType.APPLICATION_JSON}) +@Produces({MediaType.APPLICATION_JSON}) +public class StudentsResource { + + + @Inject + Template template; + + + @APIResponses(value = { + @APIResponse( + responseCode = "201", + description = "Student", + content = @Content( + mediaType = MediaType.APPLICATION_JSON, + schema = @Schema( + ref = "Student")) + )}) + @POST + public Student add(Student newStudent){ + return template.insert(newStudent); + } + +} diff --git a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/Booking.java b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/Booking.java new file mode 100644 index 0000000..bb2521d --- /dev/null +++ b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/Booking.java @@ -0,0 +1,34 @@ +package org.eclipse.jnosql.demoee.jnosql.bean.validation.openapi; + +import org.eclipse.microprofile.openapi.annotations.media.Schema; + +@Schema(name="Booking") +public class Booking { + @Schema(required = true, description = "Booking id") + private String id; + @Schema(required = true, description = "Description of the destination") + private Destination destination; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Destination getDestination() { + return destination; + } + + public void setDestination(Destination destination) { + this.destination = destination; + } + + public static Booking booking(String id, Destination destination) { + Booking result = new Booking(); + result.setId(id); + result.setDestination(destination); + return result; + } +} diff --git a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/BookingController.java b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/BookingController.java new file mode 100644 index 0000000..610e374 --- /dev/null +++ b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/BookingController.java @@ -0,0 +1,46 @@ +package org.eclipse.jnosql.demoee.jnosql.bean.validation.openapi; + +import org.eclipse.microprofile.openapi.annotations.OpenAPIDefinition; +import org.eclipse.microprofile.openapi.annotations.info.Info; +import org.eclipse.microprofile.openapi.annotations.media.Content; +import org.eclipse.microprofile.openapi.annotations.media.Schema; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +@Path("/booking") +@ApplicationScoped +@OpenAPIDefinition(info = @Info(title = "Booking endpoint", version = "1.0")) +public class BookingController { + + @APIResponses(value = { + @APIResponse( + responseCode = "200", + description = "Booking for id", + content = @Content( + mediaType = MediaType.APPLICATION_JSON, + schema = @Schema( + ref = "Booking")) + ), + @APIResponse( + responseCode = "404", + description = "No booking found for the id.") + }) + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("{bookingId}") + public Response getBooking(@PathParam("bookingId") String bookingId) { + return Response + .status(Response.Status.OK) + .entity(Booking.booking(bookingId, Destination.destination("New Rearendia", "Wheeli"))) + .build(); + } + +} diff --git a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/Destination.java b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/Destination.java new file mode 100644 index 0000000..033ea1b --- /dev/null +++ b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/openapi/Destination.java @@ -0,0 +1,30 @@ +package org.eclipse.jnosql.demoee.jnosql.bean.validation.openapi; + +public class Destination { + + private String country; + private String city; + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public static Destination destination(String country, String city) { + Destination result = new Destination(); + result.setCountry(country); + result.setCity(city); + return result; + } +} diff --git a/jnosql-bean-validation/src/main/liberty/config/server.xml b/jnosql-bean-validation/src/main/liberty/config/server.xml new file mode 100644 index 0000000..bc677de --- /dev/null +++ b/jnosql-bean-validation/src/main/liberty/config/server.xml @@ -0,0 +1,26 @@ + + + + + microProfile-5.0 + beanValidation-3.0 + + + + + + + + + + + + + + + + + + diff --git a/jnosql-bean-validation/src/main/resources/.gitkeep b/jnosql-bean-validation/src/main/resources/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/jnosql-bean-validation/src/main/resources/META-INF/microprofile-config.properties b/jnosql-bean-validation/src/main/resources/META-INF/microprofile-config.properties new file mode 100644 index 0000000..d893178 --- /dev/null +++ b/jnosql-bean-validation/src/main/resources/META-INF/microprofile-config.properties @@ -0,0 +1,5 @@ +jnosql.document.database=coffeewithjava +jnosql.mongodb.host=localhost:27017 +jnosql.mongodb.user=root +jnosql.mongodb.password=example +jnosql.mongodb.authentication.source=admin \ No newline at end of file diff --git a/jnosql-bean-validation/src/main/webapp/.gitkeep b/jnosql-bean-validation/src/main/webapp/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/jnosql-bean-validation/src/main/webapp/WEB-INF/beans.xml b/jnosql-bean-validation/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000..2777559 --- /dev/null +++ b/jnosql-bean-validation/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,6 @@ + + + diff --git a/jnosql-bean-validation/src/main/webapp/index.html b/jnosql-bean-validation/src/main/webapp/index.html new file mode 100644 index 0000000..feb093c --- /dev/null +++ b/jnosql-bean-validation/src/main/webapp/index.html @@ -0,0 +1,39 @@ + + + + + Eclipse MicroProfile demo + + + +

MicroProfile

+ +Hello JAX-RS endpoint
+ + +

Config

+Injected config values
+Config values by lookup
+ + + + + + + + + + + +

Open API

+Open API Documentation
+ + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index b13ab15..a047fec 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ liberty-mongodb wildfly-mongodb + jnosql-bean-validation \ No newline at end of file From bd3092fe4bded87e727fd6570ad638315073717a Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Wed, 28 Jun 2023 13:04:55 -0300 Subject: [PATCH 2/3] chore: add an example with JNoSQL + Bean Validation Signed-off-by: Maximillian Arruda --- jnosql-bean-validation/pom.xml | 3 +- .../jnosql/bean/validation/NewStudent.java | 47 +++++++++++++++++++ .../jnosql/bean/validation/Student.java | 6 +++ .../bean/validation/StudentsResource.java | 11 +++-- 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/NewStudent.java diff --git a/jnosql-bean-validation/pom.xml b/jnosql-bean-validation/pom.xml index ba24f7a..f43a0b1 100644 --- a/jnosql-bean-validation/pom.xml +++ b/jnosql-bean-validation/pom.xml @@ -16,6 +16,7 @@ jnosql-bean-validation 1.7.30 1.0.0 + 3.0.2 @@ -38,7 +39,7 @@ jakarta.validation jakarta.validation-api - 3.0.2 + ${jakarta.validation-api.version} provided diff --git a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/NewStudent.java b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/NewStudent.java new file mode 100644 index 0000000..3e04c6c --- /dev/null +++ b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/NewStudent.java @@ -0,0 +1,47 @@ +package org.eclipse.jnosql.demoee.jnosql.bean.validation; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Positive; +import org.eclipse.microprofile.openapi.annotations.media.Schema; + +import java.util.UUID; +@Schema(name = "NewStudent") +public class NewStudent { + + @NotBlank + @Schema(description = "Student name") + private String name; + + @Positive + @Min(18) + @Schema(description = "Student age") + private int age; + + public NewStudent() { + } + + Student toModel() { + Student student = new Student(); + student.setId(UUID.randomUUID().toString()); + student.setName(this.getName()); + student.setAge(this.getAge()); + return student; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/Student.java b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/Student.java index 1b1a2a2..3b04af5 100644 --- a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/Student.java +++ b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/Student.java @@ -3,6 +3,9 @@ import jakarta.nosql.Column; import jakarta.nosql.Entity; import jakarta.nosql.Id; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Positive; import org.eclipse.microprofile.openapi.annotations.media.Schema; @Schema(name = "Student") @@ -15,8 +18,11 @@ public class Student { @Column @Schema(description = "Student name") + @NotBlank private String name; + @Positive + @Min(18) @Column @Schema(description = "Student age") private int age; diff --git a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/StudentsResource.java b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/StudentsResource.java index 37d3739..7f85e14 100644 --- a/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/StudentsResource.java +++ b/jnosql-bean-validation/src/main/java/org/eclipse/jnosql/demoee/jnosql/bean/validation/StudentsResource.java @@ -3,6 +3,7 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.nosql.Template; +import jakarta.validation.Valid; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.POST; import jakarta.ws.rs.Path; @@ -36,11 +37,15 @@ public class StudentsResource { content = @Content( mediaType = MediaType.APPLICATION_JSON, schema = @Schema( - ref = "Student")) + ref = "NewStudent")) )}) @POST - public Student add(Student newStudent){ - return template.insert(newStudent); + public Student add(@Valid NewStudent newStudent){ + + var student = newStudent.toModel(); + + return template.insert(student); } + } From b3708c3832792823281d41bfaa0e73a5f92daebe Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Wed, 28 Jun 2023 13:06:38 -0300 Subject: [PATCH 3/3] docs: add info about jnosql + bean validation integration Signed-off-by: Maximillian Arruda --- README.adoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.adoc b/README.adoc index 2a678f1..c54b8c0 100644 --- a/README.adoc +++ b/README.adoc @@ -16,4 +16,8 @@ The primary goal of this repository is exploring the Jakarta NoSQL on several da |Full Profile - Wildfly |MongoDB +|jnosql-bean-validation +|MicroProfile - OpenLiberty +|MongoDB + |===