Skip to content

Commit 2146496

Browse files
authored
Fixes #4103 - Update Piranha Core Profile - Create a JSON REST service guide (#4153)
1 parent b7a2685 commit 2146496

File tree

6 files changed

+361
-2
lines changed

6 files changed

+361
-2
lines changed

test/coreprofile/json/pom.xml

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project
4+
xmlns="http://maven.apache.org/POM/4.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
7+
<modelVersion>4.0.0</modelVersion>
8+
<parent>
9+
<groupId>cloud.piranha.test.coreprofile</groupId>
10+
<artifactId>project</artifactId>
11+
<version>24.11.0-SNAPSHOT</version>
12+
</parent>
13+
<artifactId>piranha-test-coreprofile-json</artifactId>
14+
<packaging>war</packaging>
15+
<name>Piranha - Test - Core Profile - Create a JSON temperature service</name>
16+
<properties>
17+
<!-- other -->
18+
<piranha.version>${project.version}</piranha.version>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>jakarta.platform</groupId>
24+
<artifactId>jakarta.jakartaee-core-api</artifactId>
25+
<scope>provided</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.junit.jupiter</groupId>
29+
<artifactId>junit-jupiter-api</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter-engine</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-params</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
<build>
44+
<finalName>temperature</finalName>
45+
<plugins>
46+
<plugin>
47+
<groupId>cloud.piranha.maven</groupId>
48+
<artifactId>piranha-maven-plugin</artifactId>
49+
<version>${piranha.version}</version>
50+
<executions>
51+
<execution>
52+
<id>pre-integration-test</id>
53+
<phase>pre-integration-test</phase>
54+
<goals>
55+
<goal>start</goal>
56+
</goals>
57+
</execution>
58+
<execution>
59+
<id>post-integration-test</id>
60+
<phase>post-integration-test</phase>
61+
<goals>
62+
<goal>stop</goal>
63+
</goals>
64+
</execution>
65+
</executions>
66+
<configuration>
67+
<httpPort>${httpPort}</httpPort>
68+
</configuration>
69+
</plugin>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-compiler-plugin</artifactId>
73+
<version>${maven-compiler-plugin.version}</version>
74+
<configuration>
75+
<release>${java.version}</release>
76+
</configuration>
77+
</plugin>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-failsafe-plugin</artifactId>
81+
<version>${maven-failsafe-plugin.version}</version>
82+
<executions>
83+
<execution>
84+
<goals>
85+
<goal>integration-test</goal>
86+
<goal>verify</goal>
87+
</goals>
88+
</execution>
89+
</executions>
90+
<configuration>
91+
<forkCount>1</forkCount>
92+
<systemPropertyVariables>
93+
<httpPort>${httpPort}</httpPort>
94+
</systemPropertyVariables>
95+
</configuration>
96+
</plugin>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-war-plugin</artifactId>
100+
<version>${maven-war-plugin.version}</version>
101+
<configuration>
102+
<failOnMissingWebXml>false</failOnMissingWebXml>
103+
</configuration>
104+
</plugin>
105+
<plugin>
106+
<groupId>org.codehaus.mojo</groupId>
107+
<artifactId>build-helper-maven-plugin</artifactId>
108+
<executions>
109+
<execution>
110+
<id>reserve-network-port</id>
111+
<goals>
112+
<goal>reserve-network-port</goal>
113+
</goals>
114+
<phase>package</phase>
115+
<configuration>
116+
<portNames>
117+
<portName>httpPort</portName>
118+
</portNames>
119+
</configuration>
120+
</execution>
121+
</executions>
122+
</plugin>
123+
</plugins>
124+
</build>
125+
</project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* 3. Neither the name of the copyright holder nor the names of its
13+
* contributors may be used to endorse or promote products derived from
14+
* this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
package temperature;
29+
30+
public class Temperature {
31+
32+
/**
33+
* Defines the temperature scale enum.
34+
*/
35+
public enum TemperatureScale {
36+
/**
37+
* Celsius constant.
38+
*/
39+
CELSIUS,
40+
/**
41+
* Fahrenheit constant.
42+
*/
43+
FAHRENHEIT
44+
}
45+
46+
/**
47+
* Stores the temperature scale.
48+
*/
49+
private TemperatureScale scale;
50+
51+
/**
52+
* Stores the temperature.
53+
*/
54+
private double temperature;
55+
56+
/**
57+
* Get the temperature scale.
58+
*
59+
* @return the temperature scale.
60+
*/
61+
public TemperatureScale getScale() {
62+
return scale;
63+
}
64+
65+
/**
66+
* Get the temperature.
67+
*
68+
* @return the temperature.
69+
*/
70+
public double getTemperature() {
71+
return temperature;
72+
}
73+
74+
/**
75+
* Set the temperature scale.
76+
*
77+
* @param scale the temperature scale.
78+
*/
79+
public void setScale(TemperatureScale scale) {
80+
this.scale = scale;
81+
}
82+
83+
/**
84+
* Set the temperature.
85+
*
86+
* @param temperature the temperature.
87+
*/
88+
public void setTemperature(double temperature) {
89+
this.temperature = temperature;
90+
}
91+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* 3. Neither the name of the copyright holder nor the names of its
13+
* contributors may be used to endorse or promote products derived from
14+
* this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
package temperature;
29+
30+
import jakarta.ws.rs.ApplicationPath;
31+
import jakarta.ws.rs.core.Application;
32+
33+
@ApplicationPath("")
34+
public class TemperatureApplication extends Application {
35+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* 3. Neither the name of the copyright holder nor the names of its
13+
* contributors may be used to endorse or promote products derived from
14+
* this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
package temperature;
29+
30+
import jakarta.enterprise.context.RequestScoped;
31+
import jakarta.ws.rs.GET;
32+
import jakarta.ws.rs.Path;
33+
import jakarta.ws.rs.PathParam;
34+
import jakarta.ws.rs.Produces;
35+
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
36+
import static temperature.Temperature.TemperatureScale.CELSIUS;
37+
import static temperature.Temperature.TemperatureScale.FAHRENHEIT;
38+
39+
@RequestScoped
40+
@Path("")
41+
public class TemperatureBean {
42+
43+
/**
44+
* Get the temperature in celsius.
45+
*
46+
* @param celsius the celsius temperature.
47+
* @return the celsius tempature.
48+
*/
49+
@GET
50+
@Produces(APPLICATION_JSON)
51+
@Path("/celsius/{celsius}")
52+
public Temperature celsius(@PathParam("celsius") double celsius) {
53+
Temperature temp = new Temperature();
54+
temp.setScale(CELSIUS);
55+
temp.setTemperature(celsius);
56+
return temp;
57+
}
58+
59+
/**
60+
* Get the temperature in fahrenheit.
61+
*
62+
* @param fahrenheit the fahrenheit temperature.
63+
* @return the fahrenheit tempature.
64+
*/
65+
@GET
66+
@Produces("application/json")
67+
@Path("/fahrenheit/{fahrenheit}")
68+
public Temperature fahrenheit(@PathParam("fahrenheit") double fahrenheit) {
69+
Temperature temp = new Temperature();
70+
temp.setScale(FAHRENHEIT);
71+
temp.setTemperature(fahrenheit);
72+
return temp;
73+
}
74+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package temperature;
2+
3+
import java.net.URI;
4+
import java.net.http.HttpClient;
5+
import java.net.http.HttpRequest;
6+
import java.net.http.HttpResponse;
7+
import java.net.http.HttpResponse.BodyHandlers;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import org.junit.jupiter.api.Test;
10+
11+
class TemperatureIT {
12+
13+
private String httpPort = System.getProperty("httpPort");
14+
15+
@Test
16+
void testCelsius() throws Exception {
17+
HttpClient client = HttpClient.newHttpClient();
18+
HttpRequest request = HttpRequest
19+
.newBuilder(new URI("http://localhost:" + httpPort + "/temperature/celsius/18.5"))
20+
.build();
21+
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
22+
assertEquals("{\"scale\":\"CELSIUS\",\"temperature\":18.5}", response.body());
23+
}
24+
25+
@Test
26+
void testFahrenheit() throws Exception {
27+
HttpClient client = HttpClient.newHttpClient();
28+
HttpRequest request = HttpRequest
29+
.newBuilder(new URI("http://localhost:" + httpPort + "/temperature/fahrenheit/68.0"))
30+
.build();
31+
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
32+
assertEquals("{\"scale\":\"FAHRENHEIT\",\"temperature\":68.0}", response.body());
33+
}
34+
}

test/coreprofile/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
<artifactId>piranha-dist-coreprofile</artifactId>
2727
<version>${project.version}</version>
2828
<scope>provided</scope>
29-
<type>jar</type>
30-
<optional>true</optional>
29+
<type>pom</type>
3130
</dependency>
3231
</dependencies>
3332
<modules>
3433
<module>helloarquillian</module>
3534
<module>hello</module>
35+
<module>json</module>
3636
</modules>
3737
</project>

0 commit comments

Comments
 (0)