diff --git a/core-java-modules/core-java-lang-2/pom.xml b/core-java-modules/core-java-lang-2/pom.xml index 4702b7350b..a5fb5ca859 100644 --- a/core-java-modules/core-java-lang-2/pom.xml +++ b/core-java-modules/core-java-lang-2/pom.xml @@ -15,6 +15,11 @@ + + org.apache.commons + commons-lang3 + 3.9 + commons-beanutils commons-beanutils diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java new file mode 100644 index 0000000000..2f505443c2 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java @@ -0,0 +1,20 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.Comparator; +import java.util.List; + +class MultipleReturnValuesUsingApacheCommonsPair { + + static ImmutablePair getMostDistantPoint( + List coordinatesList, + Coordinates target) { + return coordinatesList.stream() + .map(coordinates -> ImmutablePair.of(coordinates, coordinates.calculateDistance(target))) + .max(Comparator.comparingDouble(Pair::getRight)) + .get(); + + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java new file mode 100644 index 0000000000..4da297a51f --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java @@ -0,0 +1,23 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutableTriple; + +import java.util.List; +import java.util.stream.Collectors; + +class MultipleReturnValuesUsingApacheCommonsTriple { + + static ImmutableTriple getMinAvgMaxTriple( + List coordinatesList, + Coordinates target) { + + List distanceList = coordinatesList.stream() + .map(coordinates -> coordinates.calculateDistance(target)) + .collect(Collectors.toList()); + Double minDistance = distanceList.stream().mapToDouble(Double::doubleValue).min().getAsDouble(); + Double avgDistance = distanceList.stream().mapToDouble(Double::doubleValue).average().orElse(0.0D); + Double maxDistance = distanceList.stream().mapToDouble(Double::doubleValue).max().getAsDouble(); + + return ImmutableTriple.of(minDistance, avgDistance, maxDistance); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java new file mode 100644 index 0000000000..ca5736fe50 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MultipleReturnValuesUsingApacheCommonsPairUnitTest { + + @Test + void whenUsingPair_thenMultipleFieldsAreReturned() { + + List coordinatesList = new ArrayList<>(); + coordinatesList.add(new Coordinates(1, 1, "home")); + coordinatesList.add(new Coordinates(2, 2, "school")); + coordinatesList.add(new Coordinates(3, 3, "hotel")); + + Coordinates target = new Coordinates(5, 5, "gym"); + + ImmutablePair mostDistantPoint = MultipleReturnValuesUsingApacheCommonsPair.getMostDistantPoint(coordinatesList, target); + + assertEquals(1, mostDistantPoint.getLeft().getLongitude()); + assertEquals(1, mostDistantPoint.getLeft().getLatitude()); + assertEquals("home", mostDistantPoint.getLeft().getPlaceName()); + assertEquals(5.66, BigDecimal.valueOf(mostDistantPoint.getRight()).setScale(2, RoundingMode.HALF_UP).doubleValue()); + + } + +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java new file mode 100644 index 0000000000..d23036f5e6 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutableTriple; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MultipleReturnValuesUsingApacheCommonsTripleUnitTest { + + @Test + void whenUsingTriple_thenMultipleFieldsAreReturned() { + + List coordinatesList = new ArrayList<>(); + coordinatesList.add(new Coordinates(1, 1, "home")); + coordinatesList.add(new Coordinates(2, 2, "school")); + coordinatesList.add(new Coordinates(3, 3, "hotel")); + + Coordinates target = new Coordinates(5, 5, "gym"); + + ImmutableTriple minAvgMax = MultipleReturnValuesUsingApacheCommonsTriple.getMinAvgMaxTriple(coordinatesList, target); + + assertEquals(2.83, scaleDouble(minAvgMax.left)); //min + assertEquals(4.24, scaleDouble(minAvgMax.middle)); //avg + assertEquals(5.66, scaleDouble(minAvgMax.right)); //max + } + + private double scaleDouble(Double d) { + return BigDecimal.valueOf(d).setScale(2, RoundingMode.HALF_UP).doubleValue(); + } +} diff --git a/open-liberty/pom.xml b/open-liberty/pom.xml new file mode 100644 index 0000000000..d6588ce49a --- /dev/null +++ b/open-liberty/pom.xml @@ -0,0 +1,130 @@ + + + 4.0.0 + + com.baeldung + open-liberty + 1.0-SNAPSHOT + war + + + + jakarta.platform + jakarta.jakartaee-web-api + ${version.jakarta.jakartaee-web-api} + provided + + + org.eclipse.microprofile + microprofile + ${version.microprofile} + pom + provided + + + org.apache.derby + derby + ${version.derby} + + + + + junit + junit + ${version.junit} + test + + + org.eclipse + yasson + ${version.yasson} + test + + + org.apache.cxf + cxf-rt-rs-client + ${version.cxf-rt-rs-client} + test + + + org.glassfish + javax.json + ${version.javax.json} + test + + + org.apache.cxf + cxf-rt-rs-mp-client + ${version.cxf-rt-rs-mp-client} + test + + + + + ${project.artifactId} + + + + io.openliberty.tools + liberty-maven-plugin + ${version.liberty-maven-plugin} + + + org.apache.maven.plugins + maven-dependency-plugin + ${version.maven-dependency-plugin} + + + copy-derby-dependency + package + + copy-dependencies + + + derby + ${project.build.directory}/liberty/wlp/usr/shared/resources/ + + ${testServerHttpPort} + + + + + + + org.apache.maven.plugins + maven-war-plugin + ${version.maven-war-plugin} + + + + + + + 1.8 + 1.8 + UTF-8 + UTF-8 + false + + + 8.0.0 + 3.2 + 10.14.2.0 + 3.1 + 2.10 + 3.2.3 + 4.12 + 1.0.5 + 3.2.6 + 1.0.4 + 3.3.1 + + + openliberty + 9080 + 9443 + 7070 + + \ No newline at end of file diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/person/dao/PersonDao.java b/open-liberty/src/main/java/com/baeldung/openliberty/person/dao/PersonDao.java new file mode 100644 index 0000000000..e2d408d8b0 --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/person/dao/PersonDao.java @@ -0,0 +1,24 @@ +package com.baeldung.openliberty.person.dao; + +import javax.enterprise.context.RequestScoped; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import com.baeldung.openliberty.person.model.Person; + +@RequestScoped +public class PersonDao { + + @PersistenceContext(name = "jpa-unit") + private EntityManager em; + + public Person createPerson(Person person) { + em.persist(person); + return person; + } + + public Person readPerson(int personId) { + return em.find(Person.class, personId); + } + +} \ No newline at end of file diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/person/model/Person.java b/open-liberty/src/main/java/com/baeldung/openliberty/person/model/Person.java new file mode 100644 index 0000000000..79e8c16911 --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/person/model/Person.java @@ -0,0 +1,58 @@ +package com.baeldung.openliberty.person.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; + +@Entity +public class Person { + + @GeneratedValue(strategy = GenerationType.AUTO) + @Id + private int id; + + private String username; + private String email; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Person(int id, @NotBlank String username, @Email String email) { + super(); + this.id = id; + this.username = username; + this.email = email; + } + + public Person() { + super(); + } + + public String toString() { + return this.id + ":" +this.username; + } +} diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java b/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java new file mode 100644 index 0000000000..0fb86860b8 --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java @@ -0,0 +1,52 @@ +package com.baeldung.openliberty.person.resource; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.transaction.Transactional; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +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 com.baeldung.openliberty.person.dao.PersonDao; +import com.baeldung.openliberty.person.model.Person; + +@RequestScoped +@Path("persons") +public class PersonResource { + + @Inject + private PersonDao personDao; + + @GET + @Produces(MediaType.APPLICATION_JSON) + public List getAllPersons() { + return Arrays.asList(new Person(1, "normanlewis", "normanlewis@email.com")); + } + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + public Response addPerson(Person person) { + personDao.createPerson(person); + String respMessage = "Person #" + person.getId() + " created successfully."; + return Response.status(Response.Status.CREATED).entity(respMessage).build(); + } + + @GET + @Path("{id}") + @Produces(MediaType.APPLICATION_JSON) + @Transactional + public Person getPerson(@PathParam("id") int id) { + Person person = personDao.readPerson(id); + return person; + } +} diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/ApiApplication.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/ApiApplication.java new file mode 100644 index 0000000000..176eaccaed --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/rest/ApiApplication.java @@ -0,0 +1,9 @@ +package com.baeldung.openliberty.rest; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/api") +public class ApiApplication extends Application { + +} diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java new file mode 100644 index 0000000000..8073c408dd --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java @@ -0,0 +1,18 @@ +package com.baeldung.openliberty.rest.consumes; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.Response; + +public class RestConsumer { + + public static String consumeWithJsonb(String targetUrl) { + Client client = ClientBuilder.newClient(); + Response response = client.target(targetUrl).request().get(); + String result = response.readEntity(String.class); + response.close(); + client.close(); + return result; + } + +} diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/servlet/AppServlet.java b/open-liberty/src/main/java/com/baeldung/openliberty/servlet/AppServlet.java new file mode 100644 index 0000000000..d8c8d159c4 --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/servlet/AppServlet.java @@ -0,0 +1,27 @@ +package com.baeldung.openliberty.servlet; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet(urlPatterns="/app") +public class AppServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String htmlOutput = "

Hello! Welcome to Open Liberty

"; + response.getWriter().append(htmlOutput); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + doGet(request, response); + } +} \ No newline at end of file diff --git a/open-liberty/src/main/liberty/config/server.xml b/open-liberty/src/main/liberty/config/server.xml new file mode 100644 index 0000000000..bc99905058 --- /dev/null +++ b/open-liberty/src/main/liberty/config/server.xml @@ -0,0 +1,28 @@ + + + mpHealth-2.0 + servlet-4.0 + jaxrs-2.1 + jsonp-1.1 + jsonb-1.0 + cdi-2.0 + jpa-2.2 + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/open-liberty/src/main/resources/META-INF/persistence.xml b/open-liberty/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..ca8ad1a5c9 --- /dev/null +++ b/open-liberty/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,14 @@ + + + + jdbc/jpadatasource + + + + + + \ No newline at end of file diff --git a/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java b/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java new file mode 100644 index 0000000000..4978483ca0 --- /dev/null +++ b/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java @@ -0,0 +1,40 @@ +package com.baeldung.openliberty; + +import static org.junit.Assert.assertEquals; + +import javax.json.bind.JsonbBuilder; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.openliberty.person.model.Person; +import com.baeldung.openliberty.rest.consumes.RestConsumer; + +public class RestClientTest { + + private static String BASE_URL; + + private final String API_PERSON = "api/persons"; + + @BeforeClass + public static void oneTimeSetup() { + BASE_URL = "http://localhost:9080/"; + } + + @Test + public void testSuite() { + //run the test only when liberty server is started + //this.whenConsumeWithJsonb_thenGetPerson(); + } + + public void whenConsumeWithJsonb_thenGetPerson() { + String url = BASE_URL + API_PERSON + "/1"; + String result = RestConsumer.consumeWithJsonb(url); + + Person person = JsonbBuilder.create().fromJson(result, Person.class); + assertEquals(1, person.getId()); + assertEquals("normanlewis", person.getUsername()); + assertEquals("normanlewis@email.com", person.getEmail()); + } + +} diff --git a/pom.xml b/pom.xml index 379c741648..b0d3f2297d 100644 --- a/pom.xml +++ b/pom.xml @@ -538,6 +538,7 @@ netflix-modules ninja + open-liberty oauth2-framework-impl optaplanner @@ -1069,6 +1070,7 @@ netflix-modules ninja + open-liberty oauth2-framework-impl optaplanner diff --git a/spring-boot-bootstrap/src/main/resources/application-mysql.properties b/spring-boot-bootstrap/src/main/resources/application-mysql.properties index a1823b5d7f..069bf2e9d6 100644 --- a/spring-boot-bootstrap/src/main/resources/application-mysql.properties +++ b/spring-boot-bootstrap/src/main/resources/application-mysql.properties @@ -1 +1,4 @@ -spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect \ No newline at end of file +spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect +spring.application.name = spring-boot-bootstrap +spring.datasource.username = ${SPRING_DATASOURCE_USER} +spring.datasource.password = ${SPRING_DATASOURCE_PASSWORD} \ No newline at end of file