From 21464961862efe37ca7de8e5dd9032e1702ab50f Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Sat, 2 Nov 2024 08:26:10 -0500 Subject: [PATCH] Fixes #4103 - Update Piranha Core Profile - Create a JSON REST service guide (#4153) --- test/coreprofile/json/pom.xml | 125 ++++++++++++++++++ .../main/java/temperature/Temperature.java | 91 +++++++++++++ .../temperature/TemperatureApplication.java | 35 +++++ .../java/temperature/TemperatureBean.java | 74 +++++++++++ .../test/java/temperature/TemperatureIT.java | 34 +++++ test/coreprofile/pom.xml | 4 +- 6 files changed, 361 insertions(+), 2 deletions(-) create mode 100644 test/coreprofile/json/pom.xml create mode 100644 test/coreprofile/json/src/main/java/temperature/Temperature.java create mode 100644 test/coreprofile/json/src/main/java/temperature/TemperatureApplication.java create mode 100644 test/coreprofile/json/src/main/java/temperature/TemperatureBean.java create mode 100644 test/coreprofile/json/src/test/java/temperature/TemperatureIT.java diff --git a/test/coreprofile/json/pom.xml b/test/coreprofile/json/pom.xml new file mode 100644 index 000000000..9ccf1a381 --- /dev/null +++ b/test/coreprofile/json/pom.xml @@ -0,0 +1,125 @@ + + + + 4.0.0 + + cloud.piranha.test.coreprofile + project + 24.11.0-SNAPSHOT + + piranha-test-coreprofile-json + war + Piranha - Test - Core Profile - Create a JSON temperature service + + + ${project.version} + UTF-8 + + + + jakarta.platform + jakarta.jakartaee-core-api + provided + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.jupiter + junit-jupiter-params + test + + + + temperature + + + cloud.piranha.maven + piranha-maven-plugin + ${piranha.version} + + + pre-integration-test + pre-integration-test + + start + + + + post-integration-test + post-integration-test + + stop + + + + + ${httpPort} + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${java.version} + + + + org.apache.maven.plugins + maven-failsafe-plugin + ${maven-failsafe-plugin.version} + + + + integration-test + verify + + + + + 1 + + ${httpPort} + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + org.codehaus.mojo + build-helper-maven-plugin + + + reserve-network-port + + reserve-network-port + + package + + + httpPort + + + + + + + + diff --git a/test/coreprofile/json/src/main/java/temperature/Temperature.java b/test/coreprofile/json/src/main/java/temperature/Temperature.java new file mode 100644 index 000000000..c9840d36c --- /dev/null +++ b/test/coreprofile/json/src/main/java/temperature/Temperature.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package temperature; + +public class Temperature { + + /** + * Defines the temperature scale enum. + */ + public enum TemperatureScale { + /** + * Celsius constant. + */ + CELSIUS, + /** + * Fahrenheit constant. + */ + FAHRENHEIT + } + + /** + * Stores the temperature scale. + */ + private TemperatureScale scale; + + /** + * Stores the temperature. + */ + private double temperature; + + /** + * Get the temperature scale. + * + * @return the temperature scale. + */ + public TemperatureScale getScale() { + return scale; + } + + /** + * Get the temperature. + * + * @return the temperature. + */ + public double getTemperature() { + return temperature; + } + + /** + * Set the temperature scale. + * + * @param scale the temperature scale. + */ + public void setScale(TemperatureScale scale) { + this.scale = scale; + } + + /** + * Set the temperature. + * + * @param temperature the temperature. + */ + public void setTemperature(double temperature) { + this.temperature = temperature; + } +} diff --git a/test/coreprofile/json/src/main/java/temperature/TemperatureApplication.java b/test/coreprofile/json/src/main/java/temperature/TemperatureApplication.java new file mode 100644 index 000000000..450c881bc --- /dev/null +++ b/test/coreprofile/json/src/main/java/temperature/TemperatureApplication.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package temperature; + +import jakarta.ws.rs.ApplicationPath; +import jakarta.ws.rs.core.Application; + +@ApplicationPath("") +public class TemperatureApplication extends Application { +} diff --git a/test/coreprofile/json/src/main/java/temperature/TemperatureBean.java b/test/coreprofile/json/src/main/java/temperature/TemperatureBean.java new file mode 100644 index 000000000..90dd239ca --- /dev/null +++ b/test/coreprofile/json/src/main/java/temperature/TemperatureBean.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package temperature; + +import jakarta.enterprise.context.RequestScoped; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; +import static temperature.Temperature.TemperatureScale.CELSIUS; +import static temperature.Temperature.TemperatureScale.FAHRENHEIT; + +@RequestScoped +@Path("") +public class TemperatureBean { + + /** + * Get the temperature in celsius. + * + * @param celsius the celsius temperature. + * @return the celsius tempature. + */ + @GET + @Produces(APPLICATION_JSON) + @Path("/celsius/{celsius}") + public Temperature celsius(@PathParam("celsius") double celsius) { + Temperature temp = new Temperature(); + temp.setScale(CELSIUS); + temp.setTemperature(celsius); + return temp; + } + + /** + * Get the temperature in fahrenheit. + * + * @param fahrenheit the fahrenheit temperature. + * @return the fahrenheit tempature. + */ + @GET + @Produces("application/json") + @Path("/fahrenheit/{fahrenheit}") + public Temperature fahrenheit(@PathParam("fahrenheit") double fahrenheit) { + Temperature temp = new Temperature(); + temp.setScale(FAHRENHEIT); + temp.setTemperature(fahrenheit); + return temp; + } +} diff --git a/test/coreprofile/json/src/test/java/temperature/TemperatureIT.java b/test/coreprofile/json/src/test/java/temperature/TemperatureIT.java new file mode 100644 index 000000000..010b489e7 --- /dev/null +++ b/test/coreprofile/json/src/test/java/temperature/TemperatureIT.java @@ -0,0 +1,34 @@ +package temperature; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandlers; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +class TemperatureIT { + + private String httpPort = System.getProperty("httpPort"); + + @Test + void testCelsius() throws Exception { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest + .newBuilder(new URI("http://localhost:" + httpPort + "/temperature/celsius/18.5")) + .build(); + HttpResponse response = client.send(request, BodyHandlers.ofString()); + assertEquals("{\"scale\":\"CELSIUS\",\"temperature\":18.5}", response.body()); + } + + @Test + void testFahrenheit() throws Exception { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest + .newBuilder(new URI("http://localhost:" + httpPort + "/temperature/fahrenheit/68.0")) + .build(); + HttpResponse response = client.send(request, BodyHandlers.ofString()); + assertEquals("{\"scale\":\"FAHRENHEIT\",\"temperature\":68.0}", response.body()); + } +} diff --git a/test/coreprofile/pom.xml b/test/coreprofile/pom.xml index 5b6cd7ec5..13ff181df 100644 --- a/test/coreprofile/pom.xml +++ b/test/coreprofile/pom.xml @@ -26,12 +26,12 @@ piranha-dist-coreprofile ${project.version} provided - jar - true + pom helloarquillian hello + json