From 9e36dd2a607b5ea6f2d85eeca8042ee146f2a9e4 Mon Sep 17 00:00:00 2001 From: Azhwani Date: Sat, 19 Mar 2022 20:09:32 +0100 Subject: [PATCH 01/94] first commit --- resteasy/pom.xml | 4 +-- .../server/RestEasyClientLiveTest.java | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/resteasy/pom.xml b/resteasy/pom.xml index d35fc852ba..e4ab6d84b2 100644 --- a/resteasy/pom.xml +++ b/resteasy/pom.xml @@ -40,7 +40,7 @@ org.jboss.resteasy - resteasy-jackson-provider + resteasy-jackson2-provider ${resteasy.version} @@ -134,7 +134,7 @@ - 3.0.19.Final + 4.7.2.Final 1.6.1 diff --git a/resteasy/src/test/java/com/baeldung/server/RestEasyClientLiveTest.java b/resteasy/src/test/java/com/baeldung/server/RestEasyClientLiveTest.java index 7e709edb96..ba2878cc21 100644 --- a/resteasy/src/test/java/com/baeldung/server/RestEasyClientLiveTest.java +++ b/resteasy/src/test/java/com/baeldung/server/RestEasyClientLiveTest.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Locale; import javax.naming.NamingException; +import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; @@ -14,21 +15,21 @@ import org.apache.commons.io.IOUtils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.codehaus.jackson.map.DeserializationConfig; -import org.codehaus.jackson.map.ObjectMapper; import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; -import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine; +import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine; import org.junit.Before; import org.junit.Test; import com.baeldung.client.ServicesInterface; import com.baeldung.model.Movie; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; public class RestEasyClientLiveTest { - public static final UriBuilder FULL_PATH = UriBuilder.fromPath("http://127.0.0.1:8082/RestEasyTutorial/rest"); + public static final UriBuilder FULL_PATH = UriBuilder.fromPath("http://127.0.0.1:8082/resteasy/rest"); Movie transformerMovie = null; Movie batmanMovie = null; ObjectMapper jsonMapper = null; @@ -36,8 +37,8 @@ public class RestEasyClientLiveTest { @Before public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { - jsonMapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); - jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + jsonMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); final SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); jsonMapper.setDateFormat(sdf); @@ -60,8 +61,8 @@ public class RestEasyClientLiveTest { @Test public void testListAllMovies() { - - final ResteasyClient client = new ResteasyClientBuilder().build(); + + final ResteasyClient client = (ResteasyClient)ClientBuilder.newClient(); final ResteasyWebTarget target = client.target(FULL_PATH); final ServicesInterface proxy = target.proxy(ServicesInterface.class); @@ -79,7 +80,7 @@ public class RestEasyClientLiveTest { final String transformerImdbId = "tt0418279"; - final ResteasyClient client = new ResteasyClientBuilder().build(); + final ResteasyClient client = (ResteasyClient) ClientBuilder.newClient(); final ResteasyWebTarget target = client.target(FULL_PATH); final ServicesInterface proxy = target.proxy(ServicesInterface.class); @@ -93,7 +94,7 @@ public class RestEasyClientLiveTest { @Test public void testAddMovie() { - final ResteasyClient client = new ResteasyClientBuilder().build(); + final ResteasyClient client = (ResteasyClient) ClientBuilder.newClient(); final ResteasyWebTarget target = client.target(FULL_PATH); final ServicesInterface proxy = target.proxy(ServicesInterface.class); @@ -114,8 +115,8 @@ public class RestEasyClientLiveTest { final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build(); - final ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient); - final ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build(); + final ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine(httpClient); + final ResteasyClient client = ((ResteasyClientBuilder) ClientBuilder.newBuilder()).httpEngine(engine).build(); final ResteasyWebTarget target = client.target(FULL_PATH); final ServicesInterface proxy = target.proxy(ServicesInterface.class); @@ -138,7 +139,7 @@ public class RestEasyClientLiveTest { @Test public void testDeleteMovie() { - final ResteasyClient client = new ResteasyClientBuilder().build(); + final ResteasyClient client = (ResteasyClient) ClientBuilder.newClient(); final ResteasyWebTarget target = client.target(FULL_PATH); final ServicesInterface proxy = target.proxy(ServicesInterface.class); @@ -158,7 +159,7 @@ public class RestEasyClientLiveTest { @Test public void testUpdateMovie() { - final ResteasyClient client = new ResteasyClientBuilder().build(); + final ResteasyClient client = (ResteasyClient) ClientBuilder.newClient(); final ResteasyWebTarget target = client.target(FULL_PATH); final ServicesInterface proxy = target.proxy(ServicesInterface.class); From f493bfc95929895edb751739d5684948f1699505 Mon Sep 17 00:00:00 2001 From: Arvind Kumar Avinash Date: Thu, 31 Mar 2022 08:59:10 +0100 Subject: [PATCH 02/94] Update UserController.java Changed the name and email address of the second and the third user to avoid confusion for the readers. --- .../baeldung/bootcustomfilters/controller/UserController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/bootcustomfilters/controller/UserController.java b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/bootcustomfilters/controller/UserController.java index 50d5f4ea71..9ef1fcbc8e 100644 --- a/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/bootcustomfilters/controller/UserController.java +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/bootcustomfilters/controller/UserController.java @@ -27,8 +27,8 @@ public class UserController { LOG.info("Fetching all the users"); return Arrays.asList( new User(UUID.randomUUID().toString(), "User1", "user1@test.com"), - new User(UUID.randomUUID().toString(), "User1", "user1@test.com"), - new User(UUID.randomUUID().toString(), "User1", "user1@test.com")); + new User(UUID.randomUUID().toString(), "User2", "user2@test.com"), + new User(UUID.randomUUID().toString(), "User3", "user3@test.com")); } } From 891e044e4ae63524456165a52efc7db5c2407cd5 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Sun, 10 Apr 2022 17:39:01 -0300 Subject: [PATCH 03/94] BAEL-5157 - Exception Handling with Jersey First draft: https://drafts.baeldung.com/wp-admin/post.php?post=131880&action=edit --- .../ExceptionHandlingConfig.java | 19 ++++ .../jersey/exceptionhandling/data/Stock.java | 33 +++++++ .../jersey/exceptionhandling/data/Wallet.java | 51 ++++++++++ .../jersey/exceptionhandling/repo/Db.java | 32 ++++++ .../exceptionhandling/repo/Identifiable.java | 17 ++++ .../rest/StocksResource.java | 42 ++++++++ .../rest/WalletsResource.java | 98 ++++++++++++++++++ .../IllegalArgumentExceptionMapper.java | 23 +++++ .../exceptions/InvalidTradeException.java | 17 ++++ .../rest/exceptions/RestErrorResponse.java | 34 +++++++ .../exceptions/ServerExceptionMapper.java | 35 +++++++ .../exceptionhandling/service/Repository.java | 10 ++ .../rest/StocksResourceIntegrationTest.java | 99 +++++++++++++++++++ 13 files changed, 510 insertions(+) create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/ExceptionHandlingConfig.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/data/Stock.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/data/Wallet.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/repo/Db.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/repo/Identifiable.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/WalletsResource.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/IllegalArgumentExceptionMapper.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/InvalidTradeException.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/RestErrorResponse.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java create mode 100644 jersey/src/main/java/com/baeldung/jersey/exceptionhandling/service/Repository.java create mode 100644 jersey/src/test/java/com/baeldung/jersey/exceptionhandling/rest/StocksResourceIntegrationTest.java diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/ExceptionHandlingConfig.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/ExceptionHandlingConfig.java new file mode 100644 index 0000000000..d4cc1a81a1 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/ExceptionHandlingConfig.java @@ -0,0 +1,19 @@ +package com.baeldung.jersey.exceptionhandling; + +import javax.ws.rs.ApplicationPath; + +import org.glassfish.jersey.server.ResourceConfig; + +import com.baeldung.jersey.exceptionhandling.rest.exceptions.IllegalArgumentExceptionMapper; +import com.baeldung.jersey.exceptionhandling.rest.exceptions.ServerExceptionMapper; + +@ApplicationPath("/exception-handling/*") +public class ExceptionHandlingConfig extends ResourceConfig { + + public ExceptionHandlingConfig() { + packages("com.baeldung.jersey.exceptionhandling.rest"); + register(IllegalArgumentExceptionMapper.class); + register(ServerExceptionMapper.class); + } + +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/data/Stock.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/data/Stock.java new file mode 100644 index 0000000000..9a3f321651 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/data/Stock.java @@ -0,0 +1,33 @@ +package com.baeldung.jersey.exceptionhandling.data; + +import com.baeldung.jersey.exceptionhandling.repo.Identifiable; + +public class Stock implements Identifiable { + private static final long serialVersionUID = 1L; + private String id; + private Double price; + + public Stock() { + } + + public Stock(String id, Double price) { + this.id = id; + this.price = price; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/data/Wallet.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/data/Wallet.java new file mode 100644 index 0000000000..8ef47b4c99 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/data/Wallet.java @@ -0,0 +1,51 @@ +package com.baeldung.jersey.exceptionhandling.data; + +import com.baeldung.jersey.exceptionhandling.repo.Identifiable; + +public class Wallet implements Identifiable { + private static final long serialVersionUID = 1L; + + public static final Double MIN_CHARGE = 50.0; + public static final String MIN_CHARGE_MSG = "minimum charge is: " + MIN_CHARGE; + + private String id; + private Double balance = 0.0; + + public Wallet() { + } + + public Wallet(String id) { + this.id = id; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Double getBalance() { + return balance; + } + + public void setBalance(Double balance) { + this.balance = balance; + } + + public Double addBalance(Double amount) { + if (balance == null) + balance = 0.0; + + return balance += amount; + } + + public boolean hasFunds(Double amount) { + if (balance == null || amount == null) { + return false; + } + + return (balance - amount) >= 0; + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/repo/Db.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/repo/Db.java new file mode 100644 index 0000000000..c91085f25b --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/repo/Db.java @@ -0,0 +1,32 @@ +package com.baeldung.jersey.exceptionhandling.repo; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; + +public class Db { + private Map db = new HashMap<>(); + + public Optional findById(String id) { + return Optional.ofNullable(db.get(id)); + } + + public String save(T t) { + String id = t.getId(); + if (id == null) { + id = UUID.randomUUID() + .toString(); + t.setId(id); + } + db.put(id, t); + return id; + } + + public void remove(T t) { + db.entrySet() + .removeIf(entry -> entry.getValue() + .getId() + .equals(t.getId())); + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/repo/Identifiable.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/repo/Identifiable.java new file mode 100644 index 0000000000..11af44bcc5 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/repo/Identifiable.java @@ -0,0 +1,17 @@ +package com.baeldung.jersey.exceptionhandling.repo; + +import java.io.Serializable; + +public interface Identifiable extends Serializable { + void setId(String id); + + String getId(); + + public static void assertValid(Identifiable i) { + if (i == null) + throw new IllegalArgumentException("object cannot be null"); + + if (i.getId() == null) + throw new IllegalArgumentException("object id cannot be null"); + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java new file mode 100644 index 0000000000..94ce329ad0 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java @@ -0,0 +1,42 @@ +package com.baeldung.jersey.exceptionhandling.rest; + +import java.util.Optional; + +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.jersey.exceptionhandling.data.Stock; +import com.baeldung.jersey.exceptionhandling.repo.Db; +import com.baeldung.jersey.exceptionhandling.service.Repository; + +@Path("/stocks") +public class StocksResource { + private static final Db stocks = Repository.STOCKS_DB; + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response post(Stock stock) { + stocks.save(stock); + + return Response.ok(stock) + .build(); + } + + @GET + @Path("/{ticker}") + @Produces(MediaType.APPLICATION_JSON) + public Response get(@PathParam("ticker") String id) { + Optional stock = stocks.findById(id); + stock.orElseThrow(IllegalArgumentException::new); + + return Response.ok(stock.get()) + .build(); + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/WalletsResource.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/WalletsResource.java new file mode 100644 index 0000000000..e5f8ddec06 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/WalletsResource.java @@ -0,0 +1,98 @@ +package com.baeldung.jersey.exceptionhandling.rest; + +import java.util.Optional; + +import javax.ws.rs.Consumes; +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.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import com.baeldung.jersey.exceptionhandling.data.Stock; +import com.baeldung.jersey.exceptionhandling.data.Wallet; +import com.baeldung.jersey.exceptionhandling.repo.Db; +import com.baeldung.jersey.exceptionhandling.rest.exceptions.InvalidTradeException; +import com.baeldung.jersey.exceptionhandling.rest.exceptions.RestErrorResponse; +import com.baeldung.jersey.exceptionhandling.service.Repository; + +@Path("/wallets") +public class WalletsResource { + private static final Db stocks = Repository.STOCKS_DB; + private static final Db wallets = Repository.WALLETS_DB; + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response post(Wallet wallet) { + wallets.save(wallet); + + return Response.ok(wallet) + .build(); + } + + @GET + @Path("/{id}") + @Produces(MediaType.APPLICATION_JSON) + public Response get(@PathParam("id") String id) { + Optional wallet = wallets.findById(id); + wallet.orElseThrow(IllegalArgumentException::new); + + return Response.ok(wallet.get()) + .build(); + } + + @PUT + @Path("/{id}/{amount}") + @Produces(MediaType.APPLICATION_JSON) + public Response putAmount(@PathParam("id") String id, @PathParam("amount") Double amount) { + Optional wallet = wallets.findById(id); + wallet.orElseThrow(IllegalArgumentException::new); + + if (amount < Wallet.MIN_CHARGE) { + throw new InvalidTradeException(Wallet.MIN_CHARGE_MSG); + } + + wallet.get() + .addBalance(amount); + wallets.save(wallet.get()); + + return Response.ok(wallet) + .build(); + } + + @POST + @Path("/{wallet}/buy/{ticker}") + @Produces(MediaType.APPLICATION_JSON) + public Response postBuyStock(@PathParam("wallet") String walletId, @PathParam("ticker") String id) { + Optional stock = stocks.findById(id); + stock.orElseThrow(InvalidTradeException::new); + + Optional w = wallets.findById(walletId); + w.orElseThrow(InvalidTradeException::new); + + Wallet wallet = w.get(); + Double price = stock.get() + .getPrice(); + + if (!wallet.hasFunds(price)) { + RestErrorResponse response = new RestErrorResponse(); + response.setSubject(wallet); + response.setMessage("insufficient balance"); + throw new WebApplicationException(Response.status(Status.NOT_ACCEPTABLE) + .entity(response) + .build()); + } + + wallet.addBalance(-price); + wallets.save(wallet); + + return Response.ok(wallet) + .build(); + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/IllegalArgumentExceptionMapper.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/IllegalArgumentExceptionMapper.java new file mode 100644 index 0000000000..b577121027 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/IllegalArgumentExceptionMapper.java @@ -0,0 +1,23 @@ +package com.baeldung.jersey.exceptionhandling.rest.exceptions; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +public class IllegalArgumentExceptionMapper implements ExceptionMapper { + public static final String DEFAULT_MESSAGE = "an illegal argument was provided"; + + @Override + public Response toResponse(final IllegalArgumentException exception) { + return Response.status(Response.Status.EXPECTATION_FAILED) + .entity(build(exception.getMessage())) + .type(MediaType.APPLICATION_JSON) + .build(); + } + + private RestErrorResponse build(String message) { + RestErrorResponse response = new RestErrorResponse(); + response.setMessage(DEFAULT_MESSAGE + ": " + message); + return response; + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/InvalidTradeException.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/InvalidTradeException.java new file mode 100644 index 0000000000..11277c048a --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/InvalidTradeException.java @@ -0,0 +1,17 @@ +package com.baeldung.jersey.exceptionhandling.rest.exceptions; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +public class InvalidTradeException extends WebApplicationException { + private static final long serialVersionUID = 1L; + private static final String MESSAGE = "invalid trade operation"; + + public InvalidTradeException() { + super(MESSAGE, Response.Status.NOT_ACCEPTABLE); + } + + public InvalidTradeException(String detail) { + super(MESSAGE + ": " + detail, Response.Status.NOT_ACCEPTABLE); + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/RestErrorResponse.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/RestErrorResponse.java new file mode 100644 index 0000000000..dd193ab059 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/RestErrorResponse.java @@ -0,0 +1,34 @@ +package com.baeldung.jersey.exceptionhandling.rest.exceptions; + +public class RestErrorResponse { + private Object subject; + private String message; + + public RestErrorResponse() { + } + + public RestErrorResponse(String message) { + this.message = message; + } + + public RestErrorResponse(Object subject, String message) { + this.subject = subject; + this.message = message; + } + + public Object getSubject() { + return subject; + } + + public void setSubject(Object subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java new file mode 100644 index 0000000000..a6e9cc7f39 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java @@ -0,0 +1,35 @@ +package com.baeldung.jersey.exceptionhandling.rest.exceptions; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import javax.ws.rs.ext.ExceptionMapper; + +public class ServerExceptionMapper implements ExceptionMapper { + public static final String HTTP_405_MESSAGE = "METHOD_NOT_ALLOWED"; + + @Override + public Response toResponse(final WebApplicationException exception) { + String message = exception.getMessage(); + Response response = exception.getResponse(); + Status status = response.getStatusInfo() + .toEnum(); + + switch (status) { + case METHOD_NOT_ALLOWED: + message = HTTP_405_MESSAGE; + break; + case INTERNAL_SERVER_ERROR: + message = "internal validation - " + exception; + break; + default: + message = "[unhandled response code] " + exception; + } + + return Response.status(status) + .entity(status + ": " + message) + .type(MediaType.TEXT_PLAIN) + .build(); + } +} diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/service/Repository.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/service/Repository.java new file mode 100644 index 0000000000..459b062068 --- /dev/null +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/service/Repository.java @@ -0,0 +1,10 @@ +package com.baeldung.jersey.exceptionhandling.service; + +import com.baeldung.jersey.exceptionhandling.data.Stock; +import com.baeldung.jersey.exceptionhandling.data.Wallet; +import com.baeldung.jersey.exceptionhandling.repo.Db; + +public class Repository { + public static Db STOCKS_DB = new Db<>(); + public static Db WALLETS_DB = new Db<>(); +} diff --git a/jersey/src/test/java/com/baeldung/jersey/exceptionhandling/rest/StocksResourceIntegrationTest.java b/jersey/src/test/java/com/baeldung/jersey/exceptionhandling/rest/StocksResourceIntegrationTest.java new file mode 100644 index 0000000000..1648116918 --- /dev/null +++ b/jersey/src/test/java/com/baeldung/jersey/exceptionhandling/rest/StocksResourceIntegrationTest.java @@ -0,0 +1,99 @@ +package com.baeldung.jersey.exceptionhandling.rest; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.HashMap; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Test; + +import com.baeldung.jersey.exceptionhandling.ExceptionHandlingConfig; +import com.baeldung.jersey.exceptionhandling.data.Stock; +import com.baeldung.jersey.exceptionhandling.data.Wallet; +import com.baeldung.jersey.exceptionhandling.rest.exceptions.IllegalArgumentExceptionMapper; +import com.baeldung.jersey.exceptionhandling.rest.exceptions.RestErrorResponse; +import com.baeldung.jersey.exceptionhandling.rest.exceptions.ServerExceptionMapper; + +public class StocksResourceIntegrationTest extends JerseyTest { + private static final Entity EMPTY_BODY = Entity.json(""); + private static final Stock STOCK = new Stock("BAEL", 51.57); + private static final String MY_WALLET = "MY-WALLET"; + private static final Wallet WALLET = new Wallet(MY_WALLET); + private static final int INSUFFICIENT_AMOUNT = (int) (Wallet.MIN_CHARGE - 1); + + @Override + protected Application configure() { + return new ExceptionHandlingConfig(); + } + + private Invocation.Builder stocks(String path) { + return target("/stocks" + path).request(); + } + + private Invocation.Builder wallets(String path, Object... args) { + return target("/wallets" + String.format(path, args)).request(); + } + + private Entity entity(Object object) { + return Entity.entity(object, MediaType.APPLICATION_JSON_TYPE); + } + + @Test + public void whenMethodNotAllowed_thenCustomMessage() { + Response response = stocks("").get(); + + assertEquals(Status.METHOD_NOT_ALLOWED.getStatusCode(), response.getStatus()); + + String content = response.readEntity(String.class); + assertThat(content, containsString(ServerExceptionMapper.HTTP_405_MESSAGE)); + } + + @Test + public void whenTickerNotExists_thenRestErrorResponse() { + Response response = stocks("/UNDEFINED").get(); + + assertEquals(Status.EXPECTATION_FAILED.getStatusCode(), response.getStatus()); + + RestErrorResponse content = response.readEntity(RestErrorResponse.class); + assertThat(content.getMessage(), startsWith(IllegalArgumentExceptionMapper.DEFAULT_MESSAGE)); + } + + @Test + public void givenAmountLessThanMinimum_whenAddingToWallet_thenInvalidTradeException() { + wallets("").post(entity(WALLET)); + Response response = wallets("/%s/%d", MY_WALLET, INSUFFICIENT_AMOUNT).put(EMPTY_BODY); + + assertEquals(Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus()); + + String content = response.readEntity(String.class); + assertThat(content, containsString(Wallet.MIN_CHARGE_MSG)); + } + + @Test + public void givenInsifficientFunds_whenBuyingStock_thenWebApplicationException() { + stocks("").post(entity(STOCK)); + wallets("").post(entity(WALLET)); + + Response response = wallets("/%s/buy/%s", MY_WALLET, STOCK.getId()).post(EMPTY_BODY); + assertEquals(Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus()); + + RestErrorResponse content = response.readEntity(RestErrorResponse.class); + assertNotNull(content.getSubject()); + + HashMap subject = (HashMap) content.getSubject(); + assertEquals(subject.get("id"), WALLET.getId()); + assertTrue(WALLET.getBalance() < Wallet.MIN_CHARGE); + } +} From 3b92f0b83477e29e41bf4c399d2fdf31c8351183 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Mon, 18 Apr 2022 10:46:13 -0300 Subject: [PATCH 04/94] BAEL-5157 * more meaningful exception messages --- .../jersey/exceptionhandling/rest/StocksResource.java | 2 +- .../rest/exceptions/ServerExceptionMapper.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java index 94ce329ad0..64b645a1c6 100644 --- a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java @@ -34,7 +34,7 @@ public class StocksResource { @Produces(MediaType.APPLICATION_JSON) public Response get(@PathParam("ticker") String id) { Optional stock = stocks.findById(id); - stock.orElseThrow(IllegalArgumentException::new); + stock.orElseThrow(() -> new IllegalArgumentException("ticker")); return Response.ok(stock.get()) .build(); diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java index a6e9cc7f39..adfac000e8 100644 --- a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java @@ -7,7 +7,7 @@ import javax.ws.rs.core.Response.Status; import javax.ws.rs.ext.ExceptionMapper; public class ServerExceptionMapper implements ExceptionMapper { - public static final String HTTP_405_MESSAGE = "METHOD_NOT_ALLOWED"; + public static final String HTTP_405_MESSAGE = "use one of"; @Override public Response toResponse(final WebApplicationException exception) { @@ -18,7 +18,7 @@ public class ServerExceptionMapper implements ExceptionMapper Date: Tue, 19 Apr 2022 12:27:00 -0300 Subject: [PATCH 05/94] BAEL-5157 * midified integration test to use the first available port --- .../exceptionhandling/rest/StocksResourceIntegrationTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jersey/src/test/java/com/baeldung/jersey/exceptionhandling/rest/StocksResourceIntegrationTest.java b/jersey/src/test/java/com/baeldung/jersey/exceptionhandling/rest/StocksResourceIntegrationTest.java index 1648116918..ceb59adc1f 100644 --- a/jersey/src/test/java/com/baeldung/jersey/exceptionhandling/rest/StocksResourceIntegrationTest.java +++ b/jersey/src/test/java/com/baeldung/jersey/exceptionhandling/rest/StocksResourceIntegrationTest.java @@ -17,6 +17,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.TestProperties; import org.junit.Test; import com.baeldung.jersey.exceptionhandling.ExceptionHandlingConfig; @@ -35,6 +36,7 @@ public class StocksResourceIntegrationTest extends JerseyTest { @Override protected Application configure() { + forceSet(TestProperties.CONTAINER_PORT, "0"); return new ExceptionHandlingConfig(); } From 4c7406b65c42920b417d5822a711721234ad6dfa Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Sat, 30 Apr 2022 21:49:57 +0530 Subject: [PATCH 06/94] JAVA-11775 Remove usage of deprecated JUnitPlatform.class in junit-5-basics module --- testing-modules/junit-5/pom.xml | 6 ------ .../com/baeldung/junit5/mockito/UserServiceUnitTest.java | 8 ++++---- .../java/com/baeldung/param/PersonValidatorUnitTest.java | 3 --- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/testing-modules/junit-5/pom.xml b/testing-modules/junit-5/pom.xml index f794c3990f..047eddcbcb 100644 --- a/testing-modules/junit-5/pom.xml +++ b/testing-modules/junit-5/pom.xml @@ -32,12 +32,6 @@ junit-platform-engine ${junit-platform.version} - - org.junit.platform - junit-platform-runner - ${junit-platform.version} - test - org.junit.platform junit-platform-console-standalone diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java index e13b4e24be..6aa3dd4a7f 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java @@ -3,14 +3,15 @@ package com.baeldung.junit5.mockito; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.platform.runner.JUnitPlatform; -import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; @@ -25,7 +26,6 @@ import com.baeldung.junit5.mockito.service.Errors; import com.baeldung.junit5.mockito.service.UserService; @ExtendWith(MockitoExtension.class) -@RunWith(JUnitPlatform.class) public class UserServiceUnitTest { UserService userService; diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/param/PersonValidatorUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/param/PersonValidatorUnitTest.java index 3db44c9d63..ce9b188afa 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/param/PersonValidatorUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/param/PersonValidatorUnitTest.java @@ -8,10 +8,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.platform.runner.JUnitPlatform; -import org.junit.runner.RunWith; -@RunWith(JUnitPlatform.class) @DisplayName("Testing PersonValidator") public class PersonValidatorUnitTest { From da3f8ad4a06720592457b730a040be401d9e3933 Mon Sep 17 00:00:00 2001 From: sharifi Date: Tue, 3 May 2022 17:54:14 +0430 Subject: [PATCH 07/94] bael-5249: add application-properties --- .../src/main/resources/application-embedded.properties | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 spring-boot-modules/spring-boot-keycloak/src/main/resources/application-embedded.properties diff --git a/spring-boot-modules/spring-boot-keycloak/src/main/resources/application-embedded.properties b/spring-boot-modules/spring-boot-keycloak/src/main/resources/application-embedded.properties new file mode 100644 index 0000000000..7e1985f0ad --- /dev/null +++ b/spring-boot-modules/spring-boot-keycloak/src/main/resources/application-embedded.properties @@ -0,0 +1,9 @@ +### server port +server.port=8080 + +#Keycloak Configuration +keycloak.auth-server-url=http://localhost:8083/auth +keycloak.realm=baeldung +keycloak.resource=customerClient +keycloak.public-client=true +keycloak.principal-attribute=preferred_username \ No newline at end of file From b9da120df02930c2d5a1ba0ff8ffc35faf7aa284 Mon Sep 17 00:00:00 2001 From: sharifi Date: Tue, 3 May 2022 17:55:30 +0430 Subject: [PATCH 08/94] bael-5249: add User ID --- .../com/baeldung/keycloak/CustomUserAttrController.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spring-boot-modules/spring-boot-keycloak/src/main/java/com/baeldung/keycloak/CustomUserAttrController.java b/spring-boot-modules/spring-boot-keycloak/src/main/java/com/baeldung/keycloak/CustomUserAttrController.java index 1959590e5a..5b267ae19e 100644 --- a/spring-boot-modules/spring-boot-keycloak/src/main/java/com/baeldung/keycloak/CustomUserAttrController.java +++ b/spring-boot-modules/spring-boot-keycloak/src/main/java/com/baeldung/keycloak/CustomUserAttrController.java @@ -24,6 +24,8 @@ public class CustomUserAttrController { final Principal principal = (Principal) authentication.getPrincipal(); String dob = ""; + String userIdByToken = ""; + String userIdByMapper = ""; if (principal instanceof KeycloakPrincipal) { @@ -31,6 +33,9 @@ public class CustomUserAttrController { IDToken token = kPrincipal.getKeycloakSecurityContext() .getIdToken(); + userIdByToken = token.getSubject(); + userIdByMapper = token.getOtherClaims().get("user_id").toString(); + Map customClaims = token.getOtherClaims(); if (customClaims.containsKey("DOB")) { @@ -39,6 +44,8 @@ public class CustomUserAttrController { } model.addAttribute("username", principal.getName()); + model.addAttribute("userIDByToken", userIdByToken); + model.addAttribute("userIDByMapper", userIdByMapper); model.addAttribute("dob", dob); return "userInfo"; } From 23e2df0c99aaed57d8eb602ba4e0b129f9823983 Mon Sep 17 00:00:00 2001 From: sharifi Date: Tue, 3 May 2022 17:55:48 +0430 Subject: [PATCH 09/94] bael-5249: add User ID to view --- .../src/main/resources/templates/userInfo.html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spring-boot-modules/spring-boot-keycloak/src/main/resources/templates/userInfo.html b/spring-boot-modules/spring-boot-keycloak/src/main/resources/templates/userInfo.html index 1446fe2124..7f772398c1 100644 --- a/spring-boot-modules/spring-boot-keycloak/src/main/resources/templates/userInfo.html +++ b/spring-boot-modules/spring-boot-keycloak/src/main/resources/templates/userInfo.html @@ -7,6 +7,12 @@

Hello, --name--.

+

+ User ID By Token: --userID--. +

+

+ User ID By Mapper: --userID--. +

Your Date of Birth as per our records is .

From bfbcabada0f10e01b0d79f358cfb7e9e2fcd4fe0 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 18:49:26 +0800 Subject: [PATCH 10/94] Update README.md --- spring-security-modules/spring-security-web-rest/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-security-modules/spring-security-web-rest/README.md b/spring-security-modules/spring-security-web-rest/README.md index fd1f86f6b8..5a94504762 100644 --- a/spring-security-modules/spring-security-web-rest/README.md +++ b/spring-security-modules/spring-security-web-rest/README.md @@ -9,7 +9,7 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com ### Relevant Articles: -- [Setting Up Swagger 2 with a Spring REST API](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) +- [Setting Up Swagger 2 with a Spring REST API Using Springfox](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) - [Custom Error Message Handling for REST API](https://www.baeldung.com/global-error-handler-in-a-spring-rest-api) - [Spring Security Context Propagation with @Async](https://www.baeldung.com/spring-security-async-principal-propagation) - [Servlet 3 Async Support with Spring MVC and Spring Security](https://www.baeldung.com/spring-mvc-async-security) From 8dbffdcd8b9bbf929986968d3c4fab3bd04b7c92 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 18:51:06 +0800 Subject: [PATCH 11/94] Update README.md --- spring-boot-modules/spring-boot-mvc/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-mvc/README.md b/spring-boot-modules/spring-boot-mvc/README.md index cdb2bd0fce..fdd7c70af2 100644 --- a/spring-boot-modules/spring-boot-mvc/README.md +++ b/spring-boot-modules/spring-boot-mvc/README.md @@ -7,7 +7,7 @@ This module contains articles about Spring Web MVC in Spring Boot projects. - [Custom Validation MessageSource in Spring Boot](https://www.baeldung.com/spring-custom-validation-message-source) - [Display RSS Feed with Spring MVC](https://www.baeldung.com/spring-mvc-rss-feed) - [A Controller, Service and DAO Example with Spring Boot and JSF](https://www.baeldung.com/jsf-spring-boot-controller-service-dao) -- [Setting Up Swagger 2 with a Spring REST API](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) +- [Setting Up Swagger 2 with a Spring REST API Using Springfox](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api) - [Using Spring ResponseEntity to Manipulate the HTTP Response](https://www.baeldung.com/spring-response-entity) - [The @ServletComponentScan Annotation in Spring Boot](https://www.baeldung.com/spring-servletcomponentscan) - [Guide to Internationalization in Spring Boot](https://www.baeldung.com/spring-boot-internationalization) From 6c0561e0e3539395b4325e194b0288b0dac8f055 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 20:22:08 +0800 Subject: [PATCH 12/94] Update README.md --- spring-boot-modules/spring-boot-springdoc/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-boot-modules/spring-boot-springdoc/README.md b/spring-boot-modules/spring-boot-springdoc/README.md index 4ac4147da6..5daca79bd2 100644 --- a/spring-boot-modules/spring-boot-springdoc/README.md +++ b/spring-boot-modules/spring-boot-springdoc/README.md @@ -4,3 +4,4 @@ - [Spring REST Docs vs OpenAPI](https://www.baeldung.com/spring-rest-docs-vs-openapi) - [Hiding Endpoints From Swagger Documentation in Spring Boot](https://www.baeldung.com/spring-swagger-hiding-endpoints) - [Swagger @Api Description Is Deprecated](https://www.baeldung.com/java-swagger-api-description-deprecated) +- [Set List of Objects in Swagger API Response](https://www.baeldung.com/java-swagger-set-list-response) From d953a9f5179ab434e5ee65cbce918fa4c758e0de Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 20:49:20 +0800 Subject: [PATCH 13/94] Update README.md --- persistence-modules/spring-data-mongodb/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/spring-data-mongodb/README.md b/persistence-modules/spring-data-mongodb/README.md index acc978c68e..7dd0a82def 100644 --- a/persistence-modules/spring-data-mongodb/README.md +++ b/persistence-modules/spring-data-mongodb/README.md @@ -11,6 +11,7 @@ - [Spring Data MongoDB: Projections and Aggregations](http://www.baeldung.com/spring-data-mongodb-projections-aggregations) - [Spring Data Annotations](http://www.baeldung.com/spring-data-annotations) - [Spring Data MongoDB Transactions](https://www.baeldung.com/spring-data-mongodb-transactions) +- [Return Only Specific Fields for a Query in Spring Data MongoDB](https://www.baeldung.com/mongodb-return-specific-fields) ## Spring Data MongoDB Live Testing From be54650992661ff2504ae1cd640792d043ad3841 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 20:52:49 +0800 Subject: [PATCH 14/94] Update README.md --- spring-boot-modules/spring-boot-mvc-4/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-boot-modules/spring-boot-mvc-4/README.md b/spring-boot-modules/spring-boot-mvc-4/README.md index d215525ab5..a7a341deee 100644 --- a/spring-boot-modules/spring-boot-mvc-4/README.md +++ b/spring-boot-modules/spring-boot-mvc-4/README.md @@ -9,3 +9,4 @@ This module contains articles about Spring Web MVC in Spring Boot projects. - [Configure a Spring Boot Web Application](https://www.baeldung.com/spring-boot-application-configuration) - [A Quick Intro to the SpringBootServletInitializer](https://www.baeldung.com/spring-boot-servlet-initializer) - [A Guide to Spring in Eclipse STS](https://www.baeldung.com/eclipse-sts-spring) +- [Hide a Request Field in Swagger API](https://www.baeldung.com/spring-swagger-hide-field) From cf74afc3954bb3ea5b401eabc47095d9fd70b297 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 20:55:35 +0800 Subject: [PATCH 15/94] Update README.md --- spring-boot-modules/spring-boot-data-2/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-data-2/README.md b/spring-boot-modules/spring-boot-data-2/README.md index e7d39a78e9..9ff2b2caf8 100644 --- a/spring-boot-modules/spring-boot-data-2/README.md +++ b/spring-boot-modules/spring-boot-data-2/README.md @@ -5,6 +5,6 @@ - [“HttpMessageNotWritableException: No converter found for return value of type”](https://www.baeldung.com/spring-no-converter-found) - [Creating a Read-Only Repository with Spring Data](https://www.baeldung.com/spring-data-read-only-repository) - [Using JaVers for Data Model Auditing in Spring Data](https://www.baeldung.com/spring-data-javers-audit) -- [BootstrapMode for JPA Repositories](https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-data-2) +- [BootstrapMode for JPA Repositories](https://www.baeldung.com/jpa-bootstrap-mode) - [Dynamic DTO Validation Config Retrieved from the Database](https://www.baeldung.com/spring-dynamic-dto-validation) From 72b8e53e59a73942295e1dd2bc4a80a0ad6e3ae9 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 21:59:31 +0800 Subject: [PATCH 16/94] Update README.md --- spring-boot-rest/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-boot-rest/README.md b/spring-boot-rest/README.md index 365a21781c..ece6c5ab63 100644 --- a/spring-boot-rest/README.md +++ b/spring-boot-rest/README.md @@ -5,6 +5,7 @@ This module contains articles about Spring Boot RESTful APIs. ### Relevant Articles - [Versioning a REST API](https://www.baeldung.com/rest-versioning) +- [REST Pagination in Spring](https://www.baeldung.com/rest-api-pagination-in-spring) ### E-book From de2964e2bd86cb36b8a0adae39ffa746821b3c4f Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 22:00:02 +0800 Subject: [PATCH 17/94] Update README.md --- spring-boot-rest/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-boot-rest/README.md b/spring-boot-rest/README.md index ece6c5ab63..365a21781c 100644 --- a/spring-boot-rest/README.md +++ b/spring-boot-rest/README.md @@ -5,7 +5,6 @@ This module contains articles about Spring Boot RESTful APIs. ### Relevant Articles - [Versioning a REST API](https://www.baeldung.com/rest-versioning) -- [REST Pagination in Spring](https://www.baeldung.com/rest-api-pagination-in-spring) ### E-book From 56f41f1485af9ae33c8892ebaa946c77fc2cd28e Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 22:59:48 +0800 Subject: [PATCH 18/94] Update README.md --- persistence-modules/spring-boot-persistence-mongodb/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/persistence-modules/spring-boot-persistence-mongodb/README.md b/persistence-modules/spring-boot-persistence-mongodb/README.md index 6659e82677..91dd8718e1 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/README.md +++ b/persistence-modules/spring-boot-persistence-mongodb/README.md @@ -8,4 +8,3 @@ - [A Guide to @DBRef in MongoDB](https://www.baeldung.com/spring-mongodb-dbref-annotation) - [Import Data to MongoDB From JSON File Using Java](https://www.baeldung.com/java-import-json-mongodb) - [Logging MongoDB Queries with Spring Boot](https://www.baeldung.com/spring-boot-mongodb-logging) -- [Return Only Specific Fields for a Query in Spring Data MongoDB](https://www.baeldung.com/mongodb-return-specific-fields) From 10c1ce6cd2038e1faaaba9f326757fbaa9669a5a Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 5 May 2022 23:03:28 +0800 Subject: [PATCH 19/94] Delete README.md --- .../src/main/java/com/baeldung/swaggerresponseapi/README.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/README.md diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/README.md b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/README.md deleted file mode 100644 index a7ff3285ee..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/README.md +++ /dev/null @@ -1,3 +0,0 @@ -### Relevant Articles: - -- [Set List of Objects in Swagger API Response](https://www.baeldung.com/java-swagger-set-list-response) From 67db9161fa301a6f41c2315648ca2c03297be5ed Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 5 May 2022 23:06:57 +0300 Subject: [PATCH 20/94] Create GraphQL collection.postman_collection.json --- ...GraphQL collection.postman_collection.json | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 spring-boot-modules/spring-boot-libraries/src/test/resources/GraphQL collection.postman_collection.json diff --git a/spring-boot-modules/spring-boot-libraries/src/test/resources/GraphQL collection.postman_collection.json b/spring-boot-modules/spring-boot-libraries/src/test/resources/GraphQL collection.postman_collection.json new file mode 100644 index 0000000000..8245152bdd --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/test/resources/GraphQL collection.postman_collection.json @@ -0,0 +1,169 @@ +{ + "info": { + "_postman_id": "910d9690-f629-4491-bbbd-adb30982a386", + "name": "GraphQL collection", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "mutations", + "item": [ + { + "name": "writePost", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "graphql", + "graphql": { + "query": "mutation writePost ($title: String!, $text: String!, $category: String) {\n writePost (title: $title, text: $text, category: $category) {\n id\n title\n text\n category\n }\n}", + "variables": "{\n \"title\": \"\",\n \"text\": \"\",\n \"category\": \"\"\n}" + }, + "options": { + "graphql": {} + } + }, + "url": { + "raw": "http://localhost:9090/springbootapp/graphql", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9090", + "path": [ + "springbootapp", + "graphql" + ] + } + }, + "response": [] + } + ], + "protocolProfileBehavior": {} + }, + { + "name": "queries", + "item": [ + { + "name": "get recent posts", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "graphql", + "graphql": { + "query": "{\r\n recentPosts(count: 10, offset: 0) {\r\n id\r\n title\r\n category\r\n text\r\n author {\r\n id\r\n name\r\n thumbnail\r\n }\r\n }\r\n}", + "variables": "" + } + }, + "url": { + "raw": "http://localhost:9090/springbootapp/graphql", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9090", + "path": [ + "springbootapp", + "graphql" + ] + } + }, + "response": [] + }, + { + "name": "recentPosts - variables", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "graphql", + "graphql": { + "query": "query recentPosts ($count: Int, $offset: Int) {\n recentPosts (count: $count, offset: $offset) {\n id\n title\n text\n category\n }\n}", + "variables": "{\n \"count\": 1,\n \"offset\": 0\n}" + }, + "options": { + "graphql": {} + } + }, + "url": { + "raw": "http://localhost:9090/springbootapp/graphql", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9090", + "path": [ + "springbootapp", + "graphql" + ] + } + }, + "response": [] + }, + { + "name": "get recent posts - raw", + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/graphql", + "type": "text" + } + ], + "body": { + "mode": "raw", + "raw": "query {\r\n recentPosts(count: 10, offset: 0) {\r\n id\r\n title\r\n category\r\n author {\r\n id\r\n name\r\n thumbnail\r\n }\r\n }\r\n}" + }, + "url": { + "raw": "http://localhost:9090/springbootapp/graphql", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "9090", + "path": [ + "springbootapp", + "graphql" + ] + } + }, + "response": [] + } + ], + "protocolProfileBehavior": {} + } + ], + "event": [ + { + "listen": "prerequest", + "script": { + "id": "b54f267b-c450-4f2d-8105-2f23bab4c922", + "type": "text/javascript", + "exec": [ + "" + ] + } + }, + { + "listen": "test", + "script": { + "id": "00b575be-03d4-4b29-b137-733ead139638", + "type": "text/javascript", + "exec": [ + "" + ] + } + } + ], + "variable": [ + { + "id": "20a274e5-6d51-40d6-81cb-af9eb115b21b", + "key": "url", + "value": "", + "type": "string" + } + ], + "protocolProfileBehavior": {} +} From 7ac41c7736eb151f9852443ac448d448ad6db601 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Fri, 6 May 2022 14:09:49 +0800 Subject: [PATCH 21/94] Create README.md --- graphql/graphql-error-handling/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 graphql/graphql-error-handling/README.md diff --git a/graphql/graphql-error-handling/README.md b/graphql/graphql-error-handling/README.md new file mode 100644 index 0000000000..06a2957ac1 --- /dev/null +++ b/graphql/graphql-error-handling/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Error Handling in GraphQL With Spring Boot](https://www.baeldung.com/spring-graphql-error-handling) From 44bb0319336e2e1637db3c319f5e51c8adc7b928 Mon Sep 17 00:00:00 2001 From: kpentaris Date: Sat, 7 May 2022 06:27:31 +0300 Subject: [PATCH 22/94] Add AtomicInteger.set() and .lazySet() usage example (#12099) --- .../baeldung/setvslazyset/Application.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/setvslazyset/Application.java diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/setvslazyset/Application.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/setvslazyset/Application.java new file mode 100644 index 0000000000..37424147a7 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/setvslazyset/Application.java @@ -0,0 +1,35 @@ +package com.baeldung.setvslazyset; + +import java.util.concurrent.atomic.AtomicInteger; + +public class Application { + + AtomicInteger atomic = new AtomicInteger(0); + + public static void main(String[] args) { + Application app = new Application(); + new Thread(() -> { + for (int i = 0; i < 10; i++) { + //app.atomic.set(i); + app.atomic.lazySet(i); + System.out.println("Set: " + i); + try { + Thread.sleep(100); + } catch (InterruptedException e) {} + } + }).start(); + + new Thread(() -> { + for (int i = 0; i < 10; i++) { + synchronized (app.atomic) { + int counter = app.atomic.get(); + System.out.println("Get: " + counter); + } + try { + Thread.sleep(100); + } catch (InterruptedException e) {} + } + }).start(); + } + +} From c9b2e65561e3a5fbc151477ed931176876d9acea Mon Sep 17 00:00:00 2001 From: panagiotiskakos Date: Sat, 7 May 2022 10:40:06 +0300 Subject: [PATCH 23/94] [JAVA-11770] Added build step to copy native libraries into a specific folder --- aws-modules/aws-miscellaneous/pom.xml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/aws-modules/aws-miscellaneous/pom.xml b/aws-modules/aws-miscellaneous/pom.xml index f05764f10a..08e4e36c73 100644 --- a/aws-modules/aws-miscellaneous/pom.xml +++ b/aws-modules/aws-miscellaneous/pom.xml @@ -78,6 +78,25 @@ + + org.apache.maven.plugins + maven-dependency-plugin + 3.1.1 + + + copy + compile + + copy-dependencies + + + + so,dll,dylib + native-libs + + + + From 1ce88958918aa490c1643bf80cc416b7ade79eef Mon Sep 17 00:00:00 2001 From: Mayank Aggarwal Date: Sat, 7 May 2022 17:52:36 +0530 Subject: [PATCH 24/94] Bael 5438 (#12176) * [BAEL-5438] Added Criteria Queries for Employee * [BAEL-5438] Added tests and entities for named queries and criteria queries * [BAEL-5438] Removed unused sorting files * [BAEL-5438] Ignored spring context test * BAEL-5438 Indented with 4 spaces Co-authored-by: Mayank Agarwal --- .../hibernate/criteria/model/Employee.java | 121 +++++++++--------- .../view/EmployeeCriteriaQueries.java | 57 ++++----- .../EmployeeCriteriaIntegrationTest.java | 36 +++--- .../hibernate/criteria/model/Employee.hbm.xml | 28 ++-- .../spring/data/jpa/entity/Employee.java | 10 +- .../jpa/repository/EmployeeRepository.java | 41 +++--- 6 files changed, 151 insertions(+), 142 deletions(-) diff --git a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/model/Employee.java b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/model/Employee.java index 994d3f3800..8771e02e0b 100644 --- a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/model/Employee.java +++ b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/model/Employee.java @@ -3,74 +3,81 @@ package com.baeldung.hibernate.criteria.model; import java.io.Serializable; import javax.persistence.Entity; -@org.hibernate.annotations.NamedQueries({ @org.hibernate.annotations.NamedQuery(name = "Employee_findByEmployeeId", query = "from Employee where id = :employeeId"), - @org.hibernate.annotations.NamedQuery(name = "Employee_findAllByEmployeeSalary", query = "from Employee where salary = :employeeSalary")}) -@org.hibernate.annotations.NamedNativeQueries({ @org.hibernate.annotations.NamedNativeQuery(name = "Employee_FindByEmployeeId", query = "select * from employee emp where employeeId=:employeeId", resultClass = Employee.class)}) +@org.hibernate.annotations.NamedQueries({ + @org.hibernate.annotations.NamedQuery(name = "Employee_findByEmployeeId", query = "from Employee where id = :employeeId"), + @org.hibernate.annotations.NamedQuery(name = "Employee_findAllByEmployeeSalary", query = "from Employee where salary = :employeeSalary")}) +@org.hibernate.annotations.NamedNativeQueries({ + @org.hibernate.annotations.NamedNativeQuery(name = "Employee_FindByEmployeeId", query = "select * from employee emp where employeeId=:employeeId", resultClass = Employee.class)}) @Entity public class Employee implements Serializable { - private static final long serialVersionUID = 1L; - private Integer id; - private String name; - private Long salary; + private static final long serialVersionUID = 1L; + private Integer id; + private String name; + private Long salary; - // constructors - public Employee() { - } + // constructors + public Employee() { + } - public Employee(final Integer id, final String name, final Long salary) { - super(); - this.id = id; - this.name = name; - this.salary = salary; - } + public Employee(final Integer id, final String name, final Long salary) { + super(); + this.id = id; + this.name = name; + this.salary = salary; + } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((id == null) ? 0 : id.hashCode()); - return result; - } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } - @Override - public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - final Employee other = (Employee) obj; - if (id == null) { - if (other.id != null) - return false; - } else if (!id.equals(other.id)) - return false; - return true; - } + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Employee other = (Employee) obj; + if (id == null) { + if (other.id != null) { + return false; + } + } else if (!id.equals(other.id)) { + return false; + } + return true; + } - public Integer getId() { - return id; - } + public Integer getId() { + return id; + } - public void setId(Integer id) { - this.id = id; - } + public void setId(Integer id) { + this.id = id; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } - public Long getSalary() { - return salary; - } + public Long getSalary() { + return salary; + } - public void setSalary(Long salary) { - this.salary = salary; - } + public void setSalary(Long salary) { + this.salary = salary; + } } diff --git a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/view/EmployeeCriteriaQueries.java b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/view/EmployeeCriteriaQueries.java index 04e27d2ec1..f8c525611b 100644 --- a/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/view/EmployeeCriteriaQueries.java +++ b/persistence-modules/hibernate-queries/src/main/java/com/baeldung/hibernate/criteria/view/EmployeeCriteriaQueries.java @@ -11,35 +11,34 @@ import org.hibernate.query.Query; public class EmployeeCriteriaQueries { - public List getAllEmployees() { - final Session session = HibernateUtil.getHibernateSession(); - final CriteriaBuilder cb = session.getCriteriaBuilder(); - final CriteriaQuery cr = cb.createQuery(Employee.class); - final Root root = cr.from(Employee.class); - cr.select(root); - Query query = session.createQuery(cr); - List results = query.getResultList(); - session.close(); - return results; - } - - // To get items having salary more than 50000 - public String[] greaterThanCriteria() { - final Session session = HibernateUtil.getHibernateSession(); - final CriteriaBuilder cb = session.getCriteriaBuilder(); - final CriteriaQuery cr = cb.createQuery(Employee.class); - final Root root = cr.from(Employee.class); - cr.select(root) - .where(cb.gt(root.get("salary"), 50000)); - Query query = session.createQuery(cr); - final List greaterThanEmployeeList = query.getResultList(); - final String employeeWithGreaterSalary[] = new String[greaterThanEmployeeList.size()]; - for (int i = 0; i < greaterThanEmployeeList.size(); i++) { - employeeWithGreaterSalary[i] = greaterThanEmployeeList.get(i) - .getName(); + public List getAllEmployees() { + final Session session = HibernateUtil.getHibernateSession(); + final CriteriaBuilder cb = session.getCriteriaBuilder(); + final CriteriaQuery cr = cb.createQuery(Employee.class); + final Root root = cr.from(Employee.class); + cr.select(root); + Query query = session.createQuery(cr); + List results = query.getResultList(); + session.close(); + return results; } - session.close(); - return employeeWithGreaterSalary; - } + // To get items having salary more than 50000 + public String[] greaterThanCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + final CriteriaBuilder cb = session.getCriteriaBuilder(); + final CriteriaQuery cr = cb.createQuery(Employee.class); + final Root root = cr.from(Employee.class); + cr.select(root) + .where(cb.gt(root.get("salary"), 50000)); + Query query = session.createQuery(cr); + final List greaterThanEmployeeList = query.getResultList(); + final String employeeWithGreaterSalary[] = new String[greaterThanEmployeeList.size()]; + for (int i = 0; i < greaterThanEmployeeList.size(); i++) { + employeeWithGreaterSalary[i] = greaterThanEmployeeList.get(i) + .getName(); + } + session.close(); + return employeeWithGreaterSalary; + } } diff --git a/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java b/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java index b2ad4240bf..61f54aaea8 100644 --- a/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java +++ b/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java @@ -11,26 +11,26 @@ import org.junit.Test; public class EmployeeCriteriaIntegrationTest { - final private EmployeeCriteriaQueries employeeCriteriaQueries = new EmployeeCriteriaQueries(); + final private EmployeeCriteriaQueries employeeCriteriaQueries = new EmployeeCriteriaQueries(); - @Test - public void testGreaterThanCriteriaQuery() { - final Session session = HibernateUtil.getHibernateSession(); - final List expectedGreaterThanList = session.createQuery("From Employee where salary>50000").list(); - final String expectedGreaterThanEmployees[] = new String[expectedGreaterThanList.size()]; - for (int i = 0; i < expectedGreaterThanList.size(); i++) { - expectedGreaterThanEmployees[i] = expectedGreaterThanList.get(i).getName(); + @Test + public void testGreaterThanCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedGreaterThanList = session.createQuery("From Employee where salary>50000").list(); + final String expectedGreaterThanEmployees[] = new String[expectedGreaterThanList.size()]; + for (int i = 0; i < expectedGreaterThanList.size(); i++) { + expectedGreaterThanEmployees[i] = expectedGreaterThanList.get(i).getName(); + } + session.close(); + assertArrayEquals(expectedGreaterThanEmployees, employeeCriteriaQueries.greaterThanCriteria()); } - session.close(); - assertArrayEquals(expectedGreaterThanEmployees, employeeCriteriaQueries.greaterThanCriteria()); - } - @Test - public void testGetAllEmployeesQuery() { - final Session session = HibernateUtil.getHibernateSession(); - final List expectedSortCritEmployeeList = session.createQuery("From Employee").list(); - session.close(); - assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray()); - } + @Test + public void testGetAllEmployeesQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedSortCritEmployeeList = session.createQuery("From Employee").list(); + session.close(); + assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray()); + } } diff --git a/persistence-modules/hibernate-queries/src/test/resources/com/baeldung/hibernate/criteria/model/Employee.hbm.xml b/persistence-modules/hibernate-queries/src/test/resources/com/baeldung/hibernate/criteria/model/Employee.hbm.xml index 90e1c2fefd..0cc1c54680 100644 --- a/persistence-modules/hibernate-queries/src/test/resources/com/baeldung/hibernate/criteria/model/Employee.hbm.xml +++ b/persistence-modules/hibernate-queries/src/test/resources/com/baeldung/hibernate/criteria/model/Employee.hbm.xml @@ -1,19 +1,19 @@ + "-//Hibernate/Hibernate Mapping DTD 3.0//EN" + "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/entity/Employee.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/entity/Employee.java index 214cb09e57..a511686f1b 100644 --- a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/entity/Employee.java +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/entity/Employee.java @@ -7,12 +7,12 @@ import javax.persistence.Id; @Entity public class Employee { - @Id - @GeneratedValue - private Integer id; + @Id + @GeneratedValue + private Integer id; - private String name; + private String name; - private Long salary; + private Long salary; } diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/repository/EmployeeRepository.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/repository/EmployeeRepository.java index 652b7b93d2..85d1a1b324 100644 --- a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/repository/EmployeeRepository.java +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/repository/EmployeeRepository.java @@ -9,29 +9,32 @@ import org.springframework.data.repository.query.Param; public interface EmployeeRepository extends JpaRepository { - @Query(value = "SELECT e FROM Employee e") - List findAllEmployees(Sort sort); + @Query(value = "SELECT e FROM Employee e") + List findAllEmployees(Sort sort); - @Query("SELECT e FROM Employee e WHERE e.salary = ?1") - Employee findAllEmployeesWithSalary(Long salary); + @Query("SELECT e FROM Employee e WHERE e.salary = ?1") + Employee findAllEmployeesWithSalary(Long salary); - @Query("SELECT e FROM Employee e WHERE e.name = ?1 and e.salary = ?2") - Employee findUserByNameAndSalary(String name, Long salary); + @Query("SELECT e FROM Employee e WHERE e.name = ?1 and e.salary = ?2") + Employee findUserByNameAndSalary(String name, Long salary); - @Query( - value = "SELECT * FROM Employee e WHERE e.salary = ?1", - nativeQuery = true) - Employee findUserBySalaryNative(Long salary); + @Query( + value = "SELECT * FROM Employee e WHERE e.salary = ?1", + nativeQuery = true + ) + Employee findUserBySalaryNative(Long salary); - @Query("SELECT e FROM Employee e WHERE e.name = :name and e.salary = :salary") - Employee findUserByEmployeeNameAndSalaryNamedParameters( - @Param("name") String employeeName, - @Param("salary") Long employeeSalary); + @Query("SELECT e FROM Employee e WHERE e.name = :name and e.salary = :salary") + Employee findUserByEmployeeNameAndSalaryNamedParameters( + @Param("name") String employeeName, + @Param("salary") Long employeeSalary); - @Query(value = "SELECT * FROM Employee e WHERE e.name = :name and e.salary = :salary", - nativeQuery = true) - Employee findUserByNameAndSalaryNamedParamsNative( - @Param("name") String employeeName, - @Param("salary") Long employeeSalary); + @Query( + value = "SELECT * FROM Employee e WHERE e.name = :name and e.salary = :salary", + nativeQuery = true + ) + Employee findUserByNameAndSalaryNamedParamsNative( + @Param("name") String employeeName, + @Param("salary") Long employeeSalary); } From c69f4d207f19190213f6fd857c977ebfb9f4a593 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Sat, 7 May 2022 17:24:56 -0300 Subject: [PATCH 25/94] BAEL-5396 - Configure MongoDB Collection Name for a Class in Spring Data First Draft. --- .../baeldung/boot/collection/name/Naming.java | 35 ++++++++ .../SpringBootCollectionNameApplication.java | 19 +++++ .../name/dao/CompilationRepository.java | 9 +++ .../name/dao/MusicAlbumRepository.java | 9 +++ .../name/dao/MusicTrackRepository.java | 9 +++ .../collection/name/dao/StoreRepository.java | 9 +++ .../collection/name/data/Compilation.java | 36 +++++++++ .../boot/collection/name/data/MusicAlbum.java | 48 +++++++++++ .../boot/collection/name/data/MusicTrack.java | 50 ++++++++++++ .../boot/collection/name/data/Store.java | 36 +++++++++ .../name/service/MusicStoreService.java | 62 ++++++++++++++ .../name/web/CollectionController.java | 24 ++++++ .../name/web/MusicStoreController.java | 63 +++++++++++++++ .../boot.collection.name/app.properties | 1 + .../MusicStoreServiceIntegrationTest.java | 81 +++++++++++++++++++ 15 files changed, 491 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/Naming.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Store.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/resources/boot.collection.name/app.properties create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/Naming.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/Naming.java new file mode 100644 index 0000000000..9808ecccb6 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/Naming.java @@ -0,0 +1,35 @@ +package com.baeldung.boot.collection.name; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.data.util.ParsingUtils; +import org.springframework.util.StringUtils; + +public class Naming { + public static void main(String[] args) { + String r = new Naming().fix(args[0]); + System.out.println(r); + } + + public String fix(String name) { + List parts = ParsingUtils.splitCamelCaseToLower(name); + List result = new ArrayList<>(); + + for (String part : parts) { + if (StringUtils.hasText(part)) { + result.add(part); + } + } + + return StringUtils.collectionToDelimitedString(result, "_"); + } + + public String convert(Class type) { + return fix(type.getSimpleName()); + } + + public String convert(Object instance) { + return convert(instance.getClass()); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java new file mode 100644 index 0000000000..0a5c36db29 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java @@ -0,0 +1,19 @@ +package com.baeldung.boot.collection.name; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; + +@SpringBootApplication +@PropertySource("classpath:boot.collection.name/app.properties") +public class SpringBootCollectionNameApplication { + public static void main(String... args) { + SpringApplication.run(SpringBootCollectionNameApplication.class, args); + } + + @Bean + public Naming naming() { + return new Naming(); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java new file mode 100644 index 0000000000..3f83ad4548 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.Compilation; + +public interface CompilationRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java new file mode 100644 index 0000000000..98709361d7 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.MusicAlbum; + +public interface MusicAlbumRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java new file mode 100644 index 0000000000..0964a8de00 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.MusicTrack; + +public interface MusicTrackRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java new file mode 100644 index 0000000000..b446a7d98d --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.Store; + +public interface StoreRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java new file mode 100644 index 0000000000..ce081acf25 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java @@ -0,0 +1,36 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class Compilation { + @Id + private String id; + + private String name; + + public Compilation() { + } + + public Compilation(String name) { + super(); + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java new file mode 100644 index 0000000000..ce2e084504 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java @@ -0,0 +1,48 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("albums") +public class MusicAlbum { + @Id + private String id; + + private String name; + + private String artist; + + public MusicAlbum() { + + } + + public MusicAlbum(String name, String artist) { + super(); + this.name = name; + this.artist = artist; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getArtist() { + return artist; + } + + public void setArtist(String artist) { + this.artist = artist; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java new file mode 100644 index 0000000000..39ce3994bb --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java @@ -0,0 +1,50 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("#{@naming.fix('MusicTrack')}") +public class MusicTrack { + @Id + private String id; + + private String name; + + private String artist; + + public MusicTrack() { + } + + public MusicTrack(String name, String artist) { + this.name = name; + this.artist = artist; + } + + public MusicTrack(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getArtist() { + return artist; + } + + public void setArtist(String artist) { + this.artist = artist; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Store.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Store.java new file mode 100644 index 0000000000..83f5017a13 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Store.java @@ -0,0 +1,36 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("store-#{@environment.getProperty('collection.suffix')}") +public class Store { + @Id + private String id; + + private String name; + + public Store() { + } + + public Store(String name) { + super(); + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java new file mode 100644 index 0000000000..6083e3d0c3 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java @@ -0,0 +1,62 @@ +package com.baeldung.boot.collection.name.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.boot.collection.name.dao.CompilationRepository; +import com.baeldung.boot.collection.name.dao.MusicAlbumRepository; +import com.baeldung.boot.collection.name.dao.MusicTrackRepository; +import com.baeldung.boot.collection.name.dao.StoreRepository; +import com.baeldung.boot.collection.name.data.Compilation; +import com.baeldung.boot.collection.name.data.MusicAlbum; +import com.baeldung.boot.collection.name.data.MusicTrack; +import com.baeldung.boot.collection.name.data.Store; + +@Service +public class MusicStoreService { + @Autowired + private MusicAlbumRepository albumRepository; + + @Autowired + private CompilationRepository compilationRepository; + + @Autowired + private StoreRepository storeRepository; + + @Autowired + private MusicTrackRepository trackRepository; + + public MusicAlbum add(MusicAlbum item) { + return albumRepository.save(item); + } + + public List getAlbumList() { + return albumRepository.findAll(); + } + + public Compilation add(Compilation item) { + return compilationRepository.save(item); + } + + public List getCompilationList() { + return compilationRepository.findAll(); + } + + public Store add(Store item) { + return storeRepository.save(item); + } + + public List getStoreList() { + return storeRepository.findAll(); + } + + public MusicTrack add(MusicTrack item) { + return trackRepository.save(item); + } + + public List getTrackList() { + return trackRepository.findAll(); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java new file mode 100644 index 0000000000..2efca361b9 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java @@ -0,0 +1,24 @@ +package com.baeldung.boot.collection.name.web; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.mongodb.DBObject; + +@RestController +@RequestMapping("/collection") +public class CollectionController { + @Autowired + private MongoTemplate mongo; + + @GetMapping("/{name}") + public List get(@PathVariable String name) { + return mongo.findAll(DBObject.class, name); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java new file mode 100644 index 0000000000..8c510121c2 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java @@ -0,0 +1,63 @@ +package com.baeldung.boot.collection.name.web; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.boot.collection.name.data.Compilation; +import com.baeldung.boot.collection.name.data.MusicAlbum; +import com.baeldung.boot.collection.name.data.MusicTrack; +import com.baeldung.boot.collection.name.data.Store; +import com.baeldung.boot.collection.name.service.MusicStoreService; + +@RestController +@RequestMapping("/music") +public class MusicStoreController { + @Autowired + private MusicStoreService service; + + @PostMapping("/album") + public MusicAlbum post(@RequestBody MusicAlbum item) { + return service.add(item); + } + + @GetMapping("/album") + public List getAlbumList() { + return service.getAlbumList(); + } + + @PostMapping("/compilation") + public Compilation post(@RequestBody Compilation item) { + return service.add(item); + } + + @GetMapping("/compilation") + public List getCompilationList() { + return service.getCompilationList(); + } + + @PostMapping("/store") + public Store post(@RequestBody Store item) { + return service.add(item); + } + + @GetMapping("/store") + public List getStoreList() { + return service.getStoreList(); + } + + @PostMapping("/track") + public MusicTrack post(@RequestBody MusicTrack item) { + return service.add(item); + } + + @GetMapping("/track") + public List getTrackList() { + return service.getTrackList(); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/boot.collection.name/app.properties b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/boot.collection.name/app.properties new file mode 100644 index 0000000000..98945a76e1 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/boot.collection.name/app.properties @@ -0,0 +1 @@ +collection.suffix=db diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java new file mode 100644 index 0000000000..eda8b8aafb --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java @@ -0,0 +1,81 @@ +package com.baeldung.boot.collection.name.service; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.boot.collection.name.data.Compilation; +import com.baeldung.boot.collection.name.data.MusicAlbum; +import com.baeldung.boot.collection.name.data.MusicTrack; +import com.baeldung.boot.collection.name.data.Store; + +@SpringBootTest +@DirtiesContext +@RunWith(SpringRunner.class) +public class MusicStoreServiceIntegrationTest { + @Autowired + private MusicStoreService service; + + @Autowired + private MongoTemplate mongoDb; + + @Test + public void givenAnnotation_whenSearchingByCollectionName_thenFound() { + List list = service.getCompilationList(); + int sizeBefore = list.size(); + + service.add(new Compilation("Spring Hits")); + + list = mongoDb.findAll(Compilation.class, "compilation"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } + + @Test + public void givenAnnotationWithValue_whenSearchingByCollectionName_thenFound() { + List list = service.getAlbumList(); + int sizeBefore = list.size(); + + service.add(new MusicAlbum("Album 1", "Artist A")); + + list = mongoDb.findAll(MusicAlbum.class, "albums"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } + + @Test + public void givenAnnotationWithSpELEnvironment_whenSearchingByCollectionName_thenFound() { + List list = service.getStoreList(); + int sizeBefore = list.size(); + + service.add(new Store("Store A")); + + list = mongoDb.findAll(Store.class, "store-db"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } + + @Test + public void givenAnnotationWithSpELBean_whenSearchingByCollectionName_thenFound() { + List list = service.getTrackList(); + int sizeBefore = list.size(); + + service.add(new MusicTrack("Track 1")); + + list = mongoDb.findAll(MusicTrack.class, "music_track"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } +} From d8b4f64525240e94269e5e0f78cdd3bc1dc2ac68 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sun, 8 May 2022 10:41:49 +0530 Subject: [PATCH 26/94] BAEL-5483 - Java HttpClient with SSL (#12144) * BAEL-5483 - Java HttpClient with SSL * BAEL-5483 - Java HttpClient with SSL * BAEL-5483 - Java HttpClient with SSL - changing test case url * BAEL-5483 - Two space indentation for line continuation Co-authored-by: Abhinav Pandey --- .../ssl/HttpClientSSLBypassUnitTest.java | 32 +++++++++++++++ .../httpclient/ssl/HttpClientSSLUnitTest.java | 41 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 core-java-modules/core-java-11-2/src/test/java/com/baeldung/httpclient/ssl/HttpClientSSLBypassUnitTest.java create mode 100644 core-java-modules/core-java-11-2/src/test/java/com/baeldung/httpclient/ssl/HttpClientSSLUnitTest.java diff --git a/core-java-modules/core-java-11-2/src/test/java/com/baeldung/httpclient/ssl/HttpClientSSLBypassUnitTest.java b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/httpclient/ssl/HttpClientSSLBypassUnitTest.java new file mode 100644 index 0000000000..29c1538c85 --- /dev/null +++ b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/httpclient/ssl/HttpClientSSLBypassUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.httpclient.ssl; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Properties; + +public class HttpClientSSLBypassUnitTest { + + @Test + public void whenHttpsRequest_thenCorrect() throws IOException, InterruptedException { + final Properties props = System.getProperties(); + props.setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.TRUE.toString()); + + HttpClient httpClient = HttpClient.newBuilder() + .build(); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://www.testingmcafeesites.com/")) + .build(); + + HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + props.setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.FALSE.toString()); + + Assertions.assertEquals(200, response.statusCode()); + } +} diff --git a/core-java-modules/core-java-11-2/src/test/java/com/baeldung/httpclient/ssl/HttpClientSSLUnitTest.java b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/httpclient/ssl/HttpClientSSLUnitTest.java new file mode 100644 index 0000000000..8eddd2d329 --- /dev/null +++ b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/httpclient/ssl/HttpClientSSLUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.httpclient.ssl; + +import org.junit.Test; + +import javax.net.ssl.SSLHandshakeException; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +import static org.junit.Assert.assertEquals; + +public class HttpClientSSLUnitTest { + + @Test + public void whenValidHttpsRequest_thenCorrect() throws URISyntaxException, IOException, InterruptedException { + HttpClient httpClient = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://www.google.com/")) + .build(); + + HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + + assertEquals(200, response.statusCode()); + } + + @Test(expected = SSLHandshakeException.class) + public void whenInvalidHttpsRequest_thenInCorrect() throws IOException, InterruptedException { + HttpClient httpClient = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://expired.badssl.com/")) + .build(); + + HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + + assertEquals(200, response.statusCode()); + } + +} From 0ffd9d81b0b49d4435a6ad3933d5614232dc6cf4 Mon Sep 17 00:00:00 2001 From: ACHRAF TAITAI <43656331+achraftt@users.noreply.github.com> Date: Sun, 8 May 2022 08:35:16 +0200 Subject: [PATCH 27/94] BAEL-5432: Update spring-statemachine-core version 1.2.3.RELEASE to 3.1.0 (#12154) * BAEL-5432: Update spring-statemachine-core version 1.2.3.RELEASE to 3.1.0 * BAEL-5432: Update spring-statemachine-core version 1.2.3.RELEASE to 3.2.0 --- spring-state-machine/pom.xml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-state-machine/pom.xml b/spring-state-machine/pom.xml index bc2b67cc38..741361b3fa 100644 --- a/spring-state-machine/pom.xml +++ b/spring-state-machine/pom.xml @@ -18,6 +18,11 @@ spring-statemachine-core ${spring-statemachine-core.version}
+ + org.springframework + spring-context + ${spring-context.version} + org.springframework spring-test @@ -32,7 +37,8 @@ - 1.2.3.RELEASE + 3.2.0 + 5.3.19 4.3.7.RELEASE 1.7.0 From fa471700258ecc5ec946b360751d86eb48ff913e Mon Sep 17 00:00:00 2001 From: apeterlic Date: Sun, 8 May 2022 08:38:49 +0200 Subject: [PATCH 28/94] Difference between Iterable and Iterator (#12079) * Difference between Iterable and Iterator Add examples of the Iterable and the Iterator usage. * Minor changes Add UnitTests for Iterable examples * Fix iterable example Add Custom collection for the Iterable implementation example. * Fix iterable example Change the example for the Iterable interface implementation * Revert "Fix iterable example" This reverts commit 4015cbc038b1b657c54fa666193c0c7019a474ad. * Minor changes --- .../iterable/CustomIterableClient.java | 17 +++++ .../collections/iterable/IterableExample.java | 32 +++++++++ .../collections/iterable/Product.java | 28 ++++++++ .../collections/iterable/ShoppingCart.java | 72 +++++++++++++++++++ .../iterator/CustomIteratorClient.java | 14 ++++ .../collections/iterator/Numbers.java | 60 ++++++++++++++++ .../iterable/IterableUnitTest.java | 54 ++++++++++++++ 7 files changed, 277 insertions(+) create mode 100644 core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomIterableClient.java create mode 100644 core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/IterableExample.java create mode 100644 core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/Product.java create mode 100644 core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/ShoppingCart.java create mode 100644 core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterator/CustomIteratorClient.java create mode 100644 core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterator/Numbers.java create mode 100644 core-java-modules/core-java-collections-2/src/test/java/com/baeldung/collections/iterable/IterableUnitTest.java diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomIterableClient.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomIterableClient.java new file mode 100644 index 0000000000..14f7b18014 --- /dev/null +++ b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/CustomIterableClient.java @@ -0,0 +1,17 @@ +package com.baeldung.collections.iterable; + +class CustomIterableClient { + + public static void main(String[] args) { + + ShoppingCart shoppingCart = new ShoppingCart<>(); + shoppingCart.add(new Product("Tuna", 42)); + shoppingCart.add(new Product("Eggplant", 65)); + shoppingCart.add(new Product("Salad", 45)); + shoppingCart.add(new Product("Banana", 29)); + + for (Product product : shoppingCart) { + System.out.println(product.getName()); + } + } +} diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/IterableExample.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/IterableExample.java new file mode 100644 index 0000000000..9110d6a15f --- /dev/null +++ b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/IterableExample.java @@ -0,0 +1,32 @@ +package com.baeldung.collections.iterable; + +import java.util.Iterator; +import java.util.List; + +public class IterableExample { + + public void iterateUsingIterator(List numbers) { + Iterator iterator = numbers.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + + public void iterateUsingEnhancedForLoop(List numbers) { + for (Integer number : numbers) { + System.out.println(number); + } + } + + public void iterateUsingForEachLoop(List numbers) { + numbers.forEach(System.out::println); + } + + public void removeElementsUsingIterator(List numbers) { + Iterator iterator = numbers.iterator(); + while (iterator.hasNext()) { + iterator.next(); + iterator.remove(); + } + } +} diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/Product.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/Product.java new file mode 100644 index 0000000000..b29088f6f0 --- /dev/null +++ b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/Product.java @@ -0,0 +1,28 @@ +package com.baeldung.collections.iterable; + +class Product { + + private String name; + private double price; + + public Product(String code, double price) { + this.name = code; + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } +} diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/ShoppingCart.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/ShoppingCart.java new file mode 100644 index 0000000000..f1b481da08 --- /dev/null +++ b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterable/ShoppingCart.java @@ -0,0 +1,72 @@ +package com.baeldung.collections.iterable; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ShoppingCart implements Iterable { + + private E[] elementData; + private int size; + + public ShoppingCart() { + this.elementData = (E[]) new Object[]{}; + } + + public void add(E element) { + ensureCapacity(size + 1); + elementData[size++] = element; + } + + private void ensureCapacity(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + + @Override + public Iterator iterator() { + return new ShoppingCartIterator(); + } + + public class ShoppingCartIterator implements Iterator { + int cursor; + int lastReturned = -1; + + public boolean hasNext() { + return cursor != size; + } + + public E next() { + return getNextElement(); + } + + private E getNextElement() { + int current = cursor; + exist(current); + + E[] elements = ShoppingCart.this.elementData; + validate(elements, current); + + cursor = current + 1; + lastReturned = current; + return elements[lastReturned]; + } + + private void exist(int current) { + if (current >= size) { + throw new NoSuchElementException(); + } + } + + private void validate(E[] elements, int current) { + if (current >= elements.length) { + throw new ConcurrentModificationException(); + } + } + } +} diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterator/CustomIteratorClient.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterator/CustomIteratorClient.java new file mode 100644 index 0000000000..30c8f57c0b --- /dev/null +++ b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterator/CustomIteratorClient.java @@ -0,0 +1,14 @@ +package com.baeldung.collections.iterator; + +import java.util.Iterator; + +class CustomIteratorClient { + + public static void main(String[] args) { + Iterator iterator = Numbers.iterator(); + + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } +} diff --git a/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterator/Numbers.java b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterator/Numbers.java new file mode 100644 index 0000000000..23b3344c08 --- /dev/null +++ b/core-java-modules/core-java-collections-2/src/main/java/com/baeldung/collections/iterator/Numbers.java @@ -0,0 +1,60 @@ +package com.baeldung.collections.iterator; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +class Numbers { + + private static final List NUMBER_LIST = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + private Numbers() { + } + + public static Iterator iterator() { + return new PrimeIterator(); + } + + private static class PrimeIterator implements Iterator { + + private int cursor; + + @Override + public Integer next() { + exist(cursor); + return NUMBER_LIST.get(cursor++); + } + + private void exist(int current) { + if (current >= NUMBER_LIST.size()) { + throw new NoSuchElementException(); + } + } + + @Override + public boolean hasNext() { + if (cursor > NUMBER_LIST.size()) { + return false; + } + + for (int i = cursor; i < NUMBER_LIST.size(); i++) { + if (isPrime(NUMBER_LIST.get(i))) { + cursor = i; + return true; + } + } + + return false; + } + + private boolean isPrime(int number) { + for (int i = 2; i <= number / 2; ++i) { + if (number % i == 0) { + return false; + } + } + return true; + } + } +} diff --git a/core-java-modules/core-java-collections-2/src/test/java/com/baeldung/collections/iterable/IterableUnitTest.java b/core-java-modules/core-java-collections-2/src/test/java/com/baeldung/collections/iterable/IterableUnitTest.java new file mode 100644 index 0000000000..745dcf52b7 --- /dev/null +++ b/core-java-modules/core-java-collections-2/src/test/java/com/baeldung/collections/iterable/IterableUnitTest.java @@ -0,0 +1,54 @@ +package com.baeldung.collections.iterable; + +import com.baeldung.collections.iterable.IterableExample; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class IterableUnitTest { + + private static List getNumbers() { + List numbers = new ArrayList<>(); + numbers.add(10); + numbers.add(20); + numbers.add(30); + numbers.add(40); + return numbers; + } + + @Test + void givenNumbers_whenUsingIterator_thenCorrectSize() { + List numbers = getNumbers(); + IterableExample iterableExample = new IterableExample(); + iterableExample.iterateUsingIterator(numbers); + assertEquals(4, numbers.size()); + } + + @Test + void givenNumbers_whenRemoveElements_thenEmptyList() { + List numbers = getNumbers(); + IterableExample iterableExample = new IterableExample(); + iterableExample.removeElementsUsingIterator(numbers); + assertEquals(0, numbers.size()); + } + + @Test + void givenNumbers_whenIterateUsingEnhancedForLoop_thenCorrectSize() { + List numbers = getNumbers(); + IterableExample iterableExample = new IterableExample(); + iterableExample.iterateUsingEnhancedForLoop(numbers); + assertEquals(4, numbers.size()); + } + + @Test + void givenNumbers_whenIterateUsingForEachLoop_thenCorrectSize() { + List numbers = getNumbers(); + IterableExample iterableExample = new IterableExample(); + iterableExample.iterateUsingForEachLoop(numbers); + assertEquals(4, numbers.size()); + } +} From 9a1d66f85251d8f3ec698df2be08baaf49fed5e3 Mon Sep 17 00:00:00 2001 From: 515882294 <515882294@qq.com> Date: Sun, 8 May 2022 16:00:41 +0800 Subject: [PATCH 29/94] BAEL-4463: What is com.sun.proxy.$Proxy --- .../reflection/proxy/AdvancedOperation.java | 7 ++++ .../reflection/proxy/BasicOperation.java | 7 ++++ .../reflection/proxy/DollarProxyUnitTest.java | 35 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/AdvancedOperation.java create mode 100644 core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/BasicOperation.java create mode 100644 core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/proxy/DollarProxyUnitTest.java diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/AdvancedOperation.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/AdvancedOperation.java new file mode 100644 index 0000000000..3262b255f0 --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/AdvancedOperation.java @@ -0,0 +1,7 @@ +package com.baeldung.reflection.proxy; + +public interface AdvancedOperation { + int multiple(int a, int b); + + int divide(int a, int b); +} \ No newline at end of file diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/BasicOperation.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/BasicOperation.java new file mode 100644 index 0000000000..b39eb0118e --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/BasicOperation.java @@ -0,0 +1,7 @@ +package com.baeldung.reflection.proxy; + +public interface BasicOperation { + int add(int a, int b); + + int sub(int a, int b); +} \ No newline at end of file diff --git a/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/proxy/DollarProxyUnitTest.java b/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/proxy/DollarProxyUnitTest.java new file mode 100644 index 0000000000..ef6c481778 --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/proxy/DollarProxyUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.reflection.proxy; + +import org.junit.Test; + +import java.lang.reflect.Proxy; +import java.util.function.Consumer; + +import static org.junit.Assert.assertTrue; + +public class DollarProxyUnitTest { + @Test + public void givenProxy_whenInvokingGetProxyClass_thenGeneratingProxyClass() { + // Java 8: -Dsun.misc.ProxyGenerator.saveGeneratedFiles=true + // Java 9 or later: -Djdk.proxy.ProxyGenerator.saveGeneratedFiles=true + // Note: System.setProperty() doesn't work here + // because ProxyGenerator.saveGeneratedFiles read its property only once. + // The @Test annotation in this method will generate a $Proxy class. + + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + Class[] interfaces = {BasicOperation.class, AdvancedOperation.class}; + Class proxyClass = Proxy.getProxyClass(classLoader, interfaces); + + boolean isProxyClass = Proxy.isProxyClass(proxyClass); + assertTrue(isProxyClass); + } + + @Test + public void givenReflection_whenReadingAnnotation_thenGeneratingProxyClass() { + FunctionalInterface instance = Consumer.class.getDeclaredAnnotation(FunctionalInterface.class); + Class clazz = instance.getClass(); + + boolean isProxyClass = Proxy.isProxyClass(clazz); + assertTrue(isProxyClass); + } +} From aed8f9100b83c37221c966dc235cd49a9fd7ff6a Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Sun, 8 May 2022 16:36:25 +0530 Subject: [PATCH 30/94] JAVA-11792: Fix formatting of POMs --- apache-poi-2/pom.xml | 7 +- aws-modules/aws-s3/pom.xml | 3 +- aws-modules/pom.xml | 2 +- .../core-java-exceptions-3/pom.xml | 4 +- .../core-java-exceptions-4/pom.xml | 4 +- core-java-modules/core-java-uuid/pom.xml | 1 - .../multi-module-caching/core/pom.xml | 5 +- .../multi-module-caching/pom.xml | 16 +- .../multi-module-caching/runner/pom.xml | 5 +- .../single-module-caching/pom.xml | 6 +- docker/docker-internal-dto/pom.xml | 6 +- docker/docker-sample-app/pom.xml | 23 +- docker/docker-spring-boot-postgres/pom.xml | 68 ++-- docker/docker-spring-boot/pom.xml | 21 +- docker/heap-sizing/pom.xml | 3 +- docker/pom.xml | 13 +- feign/pom.xml | 10 +- graphql/graphql-error-handling/pom.xml | 15 +- graphql/graphql-java/pom.xml | 8 +- graphql/graphql-spqr/pom.xml | 1 + jakarta-ee/pom.xml | 5 - jib/pom.xml | 1 + libraries-3/pom.xml | 6 +- .../empty-phase/pom.xml | 2 +- .../disable-plugin-examples/pom.xml | 30 +- .../maven-simple/parent-project/core/pom.xml | 1 + .../maven-simple/parent-project/pom.xml | 1 + .../parent-project/webapp/pom.xml | 2 +- maven-modules/pom.xml | 4 +- muleesb/pom.xml | 7 +- .../hibernate-annotations/pom.xml | 4 +- persistence-modules/hibernate-queries/pom.xml | 9 +- persistence-modules/java-mongodb-2/pom.xml | 2 +- persistence-modules/pom.xml | 2 +- spring-boot-modules/pom.xml | 6 +- .../spring-boot-artifacts/pom.xml | 6 +- spring-boot-modules/spring-boot-mvc-4/pom.xml | 8 +- .../pom.xml | 31 +- .../spring-boot-validation/pom.xml | 2 +- spring-cloud/pom.xml | 2 +- spring-reactive/pom.xml | 2 - spring-roo/pom.xml | 368 +++++++++--------- .../spring-security-opa/pom.xml | 93 +++-- .../spring-security-web-boot-3/pom.xml | 6 +- .../spring-security-web-login/pom.xml | 12 +- .../pom.xml | 1 - .../pom.xml | 1 - .../pom.xml | 3 - .../spring-swagger-codegen-api-client/pom.xml | 3 - .../spring-resttemplate/pom.xml | 14 +- testing-modules/junit-5-basics/pom.xml | 10 +- testing-modules/testng-command-line/pom.xml | 5 +- 52 files changed, 418 insertions(+), 452 deletions(-) diff --git a/apache-poi-2/pom.xml b/apache-poi-2/pom.xml index 30270cd7be..af959292fa 100644 --- a/apache-poi-2/pom.xml +++ b/apache-poi-2/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 apache-poi-2 0.0.1-SNAPSHOT @@ -25,5 +25,4 @@ 5.2.0 - - + \ No newline at end of file diff --git a/aws-modules/aws-s3/pom.xml b/aws-modules/aws-s3/pom.xml index 65ad6f27f8..e7e04782b1 100644 --- a/aws-modules/aws-s3/pom.xml +++ b/aws-modules/aws-s3/pom.xml @@ -20,7 +20,6 @@ aws-java-sdk ${aws-java-sdk.version} - commons-io commons-io @@ -58,7 +57,7 @@ - + 1.11.290 diff --git a/aws-modules/pom.xml b/aws-modules/pom.xml index 4bdffa789a..3c5459d46c 100644 --- a/aws-modules/pom.xml +++ b/aws-modules/pom.xml @@ -19,6 +19,6 @@ aws-miscellaneous aws-reactive aws-s3 - + \ No newline at end of file diff --git a/core-java-modules/core-java-exceptions-3/pom.xml b/core-java-modules/core-java-exceptions-3/pom.xml index 455f769757..7eaa57edf1 100644 --- a/core-java-modules/core-java-exceptions-3/pom.xml +++ b/core-java-modules/core-java-exceptions-3/pom.xml @@ -32,8 +32,8 @@ 3.8.1 - - + + diff --git a/core-java-modules/core-java-exceptions-4/pom.xml b/core-java-modules/core-java-exceptions-4/pom.xml index cc81fdc40b..e9a2d05180 100644 --- a/core-java-modules/core-java-exceptions-4/pom.xml +++ b/core-java-modules/core-java-exceptions-4/pom.xml @@ -32,8 +32,8 @@ 3.8.1 - - + + diff --git a/core-java-modules/core-java-uuid/pom.xml b/core-java-modules/core-java-uuid/pom.xml index 28519a1a68..7d851292f5 100644 --- a/core-java-modules/core-java-uuid/pom.xml +++ b/core-java-modules/core-java-uuid/pom.xml @@ -35,7 +35,6 @@ true - org.apache.maven.plugins diff --git a/docker/docker-caching/multi-module-caching/core/pom.xml b/docker/docker-caching/multi-module-caching/core/pom.xml index eeeb5a6e5b..bcfc4b5783 100644 --- a/docker/docker-caching/multi-module-caching/core/pom.xml +++ b/docker/docker-caching/multi-module-caching/core/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 core @@ -22,4 +22,5 @@ 8 8 + \ No newline at end of file diff --git a/docker/docker-caching/multi-module-caching/pom.xml b/docker/docker-caching/multi-module-caching/pom.xml index 7968114385..e02ced2dca 100644 --- a/docker/docker-caching/multi-module-caching/pom.xml +++ b/docker/docker-caching/multi-module-caching/pom.xml @@ -1,6 +1,7 @@ - + 4.0.0 com.baeldung multi-module-caching @@ -8,6 +9,11 @@ Multi-module Maven caching example pom + + runner + core + + @@ -22,8 +28,4 @@ 1.8 - - runner - core - - + \ No newline at end of file diff --git a/docker/docker-caching/multi-module-caching/runner/pom.xml b/docker/docker-caching/multi-module-caching/runner/pom.xml index e3654bff17..e3f234bac0 100644 --- a/docker/docker-caching/multi-module-caching/runner/pom.xml +++ b/docker/docker-caching/multi-module-caching/runner/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 runner @@ -53,4 +53,5 @@ 8 8 + \ No newline at end of file diff --git a/docker/docker-caching/single-module-caching/pom.xml b/docker/docker-caching/single-module-caching/pom.xml index 386b040138..74bb477fb2 100644 --- a/docker/docker-caching/single-module-caching/pom.xml +++ b/docker/docker-caching/single-module-caching/pom.xml @@ -1,9 +1,8 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung single-module-caching 1.0-SNAPSHOT @@ -50,4 +49,5 @@ 8 8 + \ No newline at end of file diff --git a/docker/docker-internal-dto/pom.xml b/docker/docker-internal-dto/pom.xml index 55cef257fe..09013d2fc3 100644 --- a/docker/docker-internal-dto/pom.xml +++ b/docker/docker-internal-dto/pom.xml @@ -3,13 +3,13 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + docker-internal-dto + docker-internal-dto + com.baeldung.docker docker 0.0.1 - docker-internal-dto - docker-internal-dto - diff --git a/docker/docker-sample-app/pom.xml b/docker/docker-sample-app/pom.xml index 6841fabcee..24fede56fd 100644 --- a/docker/docker-sample-app/pom.xml +++ b/docker/docker-sample-app/pom.xml @@ -1,21 +1,18 @@ - + 4.0.0 + docker-sample-app + docker-sample-app + Demo project for Spring Boot and Docker + com.baeldung.docker docker 0.0.1 - docker-sample-app - docker-sample-app - Demo project for Spring Boot and Docker - - - 11 - - org.springframework.boot @@ -42,4 +39,8 @@ - + + 11 + + + \ No newline at end of file diff --git a/docker/docker-spring-boot-postgres/pom.xml b/docker/docker-spring-boot-postgres/pom.xml index d08ae130db..7a4ed1db3c 100644 --- a/docker/docker-spring-boot-postgres/pom.xml +++ b/docker/docker-spring-boot-postgres/pom.xml @@ -1,13 +1,14 @@ - - 4.0.0 - com.baeldung.docker - docker-spring-boot-postgres - 0.0.1-SNAPSHOT - docker-spring-boot-postgres - Demo project showing Spring Boot, PostgreSQL, and Docker - + + 4.0.0 + com.baeldung.docker + docker-spring-boot-postgres + 0.0.1-SNAPSHOT + docker-spring-boot-postgres + Demo project showing Spring Boot, PostgreSQL, and Docker + com.baeldung parent-boot-2 @@ -15,33 +16,32 @@ ../../parent-boot-2 - - - org.springframework.boot - spring-boot-starter-data-jpa - + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.postgresql + postgresql + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + - - org.postgresql - postgresql - runtime - - - org.springframework.boot - spring-boot-starter-test - test - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - - 11 diff --git a/docker/docker-spring-boot/pom.xml b/docker/docker-spring-boot/pom.xml index 74bd1561cf..fbc891ca2c 100644 --- a/docker/docker-spring-boot/pom.xml +++ b/docker/docker-spring-boot/pom.xml @@ -3,33 +3,26 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + docker-spring-boot + docker-spring-boot + Demo project showing Spring Boot and Docker + com.baeldung.docker docker 0.0.1 - docker-spring-boot - - docker-spring-boot - Demo project showing Spring Boot and Docker - - - 11 - - org.springframework.boot spring-boot-starter-web - com.baeldung.docker docker-internal-dto 0.0.1 - org.springframework.boot spring-boot-starter-test @@ -58,4 +51,8 @@ - + + 11 + + + \ No newline at end of file diff --git a/docker/heap-sizing/pom.xml b/docker/heap-sizing/pom.xml index 2cc354f6cf..32c200c1c0 100644 --- a/docker/heap-sizing/pom.xml +++ b/docker/heap-sizing/pom.xml @@ -48,7 +48,6 @@ com.google.cloud.tools jib-maven-plugin 2.7.1 - heapsizing-demo-jib @@ -62,4 +61,4 @@ 11 - + \ No newline at end of file diff --git a/docker/pom.xml b/docker/pom.xml index 5c6267c6dd..d4fa8d46d7 100644 --- a/docker/pom.xml +++ b/docker/pom.xml @@ -3,14 +3,13 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung.docker docker 0.0.1 docker Demo project showing Spring Boot and Docker pom - + com.baeldung parent-boot-2 @@ -18,10 +17,6 @@ ../parent-boot-2 - - 11 - - docker-internal-dto docker-spring-boot @@ -30,4 +25,8 @@ docker-caching/multi-module-caching - + + 11 + + + \ No newline at end of file diff --git a/feign/pom.xml b/feign/pom.xml index 026afdfc7a..8fa864fa46 100644 --- a/feign/pom.xml +++ b/feign/pom.xml @@ -1,5 +1,7 @@ - + 4.0.0 com.baeldung.feign feign @@ -69,6 +71,7 @@ test + @@ -92,10 +95,8 @@ src/main/resources/users.xsd - - org.jvnet.jaxb2.maven2 maven-jaxb2-plugin @@ -111,7 +112,6 @@ *.xsd - com.baeldung.feign.soap target/generated-sources/jaxb @@ -119,6 +119,6 @@ - + \ No newline at end of file diff --git a/graphql/graphql-error-handling/pom.xml b/graphql/graphql-error-handling/pom.xml index ea1cf96a0e..92696d8ed7 100644 --- a/graphql/graphql-error-handling/pom.xml +++ b/graphql/graphql-error-handling/pom.xml @@ -1,12 +1,12 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 graphql-error-handling 1.0 - jar graphql-error-handling + jar com.baeldung.graphql @@ -19,56 +19,47 @@ org.springframework.boot spring-boot-starter-data-jpa - org.springframework.boot spring-boot-starter-web - com.graphql-java graphql-spring-boot-starter ${graphql-spring-boot-starter.version} - com.graphql-java graphql-java-tools ${graphql-java-tools.version} - org.projectlombok lombok ${lombok.version} - com.h2database h2 ${h2.version} - org.springframework.boot spring-boot-test test - com.graphql-java graphql-spring-boot-starter-test test ${graphql-spring-boot-starter.version} - org.skyscreamer jsonassert ${jsonassert.version} test - diff --git a/graphql/graphql-java/pom.xml b/graphql/graphql-java/pom.xml index 5e5bc8f648..b0b2c15359 100644 --- a/graphql/graphql-java/pom.xml +++ b/graphql/graphql-java/pom.xml @@ -85,7 +85,6 @@ httpclient ${httpclient.version} - org.mock-server mockserver-netty @@ -98,13 +97,11 @@ ${mockserver-client-java.version} test - com.graphql-java graphql-java-extended-scalars ${graphql-java-extended-scalars.version} - @@ -155,14 +152,11 @@ 1.9.0 0.5.0 4.5.13 - 5.13.2 5.13.2 - 10.0.7 - 1.18 2022-04-06T00-10-27-a70541e - + \ No newline at end of file diff --git a/graphql/graphql-spqr/pom.xml b/graphql/graphql-spqr/pom.xml index ad040c1557..75a1b5d79b 100644 --- a/graphql/graphql-spqr/pom.xml +++ b/graphql/graphql-spqr/pom.xml @@ -28,4 +28,5 @@ 0.0.6 + \ No newline at end of file diff --git a/jakarta-ee/pom.xml b/jakarta-ee/pom.xml index dea4437345..afd31d7ece 100644 --- a/jakarta-ee/pom.xml +++ b/jakarta-ee/pom.xml @@ -51,25 +51,20 @@ admin password - ${local.glassfish.domain} 8080 4848 - ${project.artifactId} target/${project.build.finalName}.war - true false true - - diff --git a/jib/pom.xml b/jib/pom.xml index 8208eebdf7..bbc9a3c623 100644 --- a/jib/pom.xml +++ b/jib/pom.xml @@ -43,4 +43,5 @@ 2.5.0 + \ No newline at end of file diff --git a/libraries-3/pom.xml b/libraries-3/pom.xml index c51b264e83..d68a9e0703 100644 --- a/libraries-3/pom.xml +++ b/libraries-3/pom.xml @@ -112,7 +112,7 @@ structurizr-plantuml ${structurizr.version} - + org.immutables value ${immutables.version} @@ -177,7 +177,7 @@ - -XepExcludedPaths:(.*)/test/.*|(.*)/jcabi/.* @@ -190,7 +190,7 @@ plexus-compiler-javac-errorprone 2.8 - com.google.errorprone diff --git a/maven-modules/maven-parent-pom-resolution/disable-plugin-examples/empty-phase/pom.xml b/maven-modules/maven-parent-pom-resolution/disable-plugin-examples/empty-phase/pom.xml index 28ea8b6359..2dfa34568e 100644 --- a/maven-modules/maven-parent-pom-resolution/disable-plugin-examples/empty-phase/pom.xml +++ b/maven-modules/maven-parent-pom-resolution/disable-plugin-examples/empty-phase/pom.xml @@ -21,7 +21,7 @@ enforce-file-exists - + diff --git a/maven-modules/maven-parent-pom-resolution/disable-plugin-examples/pom.xml b/maven-modules/maven-parent-pom-resolution/disable-plugin-examples/pom.xml index 2a2e5b00ea..4d16a94838 100644 --- a/maven-modules/maven-parent-pom-resolution/disable-plugin-examples/pom.xml +++ b/maven-modules/maven-parent-pom-resolution/disable-plugin-examples/pom.xml @@ -13,7 +13,14 @@ maven-parent-pom-resolution 1.0.0-SNAPSHOT - + + + plugin-enabled + skip-parameter + phase-none + empty-phase + + @@ -27,26 +34,19 @@ enforce - - - - ${project.basedir}/src/file-that-must-exist.txt - - - + + + + ${project.basedir}/src/file-that-must-exist.txt + + + - - - plugin-enabled - skip-parameter - phase-none - empty-phase - UTF-8 diff --git a/maven-modules/maven-simple/parent-project/core/pom.xml b/maven-modules/maven-simple/parent-project/core/pom.xml index 6553889c24..2e3a6eee7d 100644 --- a/maven-modules/maven-simple/parent-project/core/pom.xml +++ b/maven-modules/maven-simple/parent-project/core/pom.xml @@ -23,4 +23,5 @@ 4.3.30.RELEASE + \ No newline at end of file diff --git a/maven-modules/maven-simple/parent-project/pom.xml b/maven-modules/maven-simple/parent-project/pom.xml index bde903b1b5..2f28eff49f 100644 --- a/maven-modules/maven-simple/parent-project/pom.xml +++ b/maven-modules/maven-simple/parent-project/pom.xml @@ -33,4 +33,5 @@ 5.3.16 + \ No newline at end of file diff --git a/maven-modules/maven-simple/parent-project/webapp/pom.xml b/maven-modules/maven-simple/parent-project/webapp/pom.xml index f6cee60cbf..ce964c222f 100644 --- a/maven-modules/maven-simple/parent-project/webapp/pom.xml +++ b/maven-modules/maven-simple/parent-project/webapp/pom.xml @@ -5,7 +5,6 @@ 4.0.0 webapp webapp - war @@ -30,4 +29,5 @@ 3.3.2 + \ No newline at end of file diff --git a/maven-modules/pom.xml b/maven-modules/pom.xml index a9fefbbf5d..253f5d9fa0 100644 --- a/maven-modules/pom.xml +++ b/maven-modules/pom.xml @@ -15,8 +15,8 @@ - animal-sniffer-mvn-plugin - maven-archetype + animal-sniffer-mvn-plugin + maven-archetype maven-copy-files maven-custom-plugin diff --git a/muleesb/pom.xml b/muleesb/pom.xml index d78cebada2..a2204c15b7 100644 --- a/muleesb/pom.xml +++ b/muleesb/pom.xml @@ -1,6 +1,7 @@ - + 4.0.0 com.mycompany muleesb @@ -218,4 +219,4 @@ 2.2.1 - + \ No newline at end of file diff --git a/persistence-modules/hibernate-annotations/pom.xml b/persistence-modules/hibernate-annotations/pom.xml index 023e5aa30f..48e678af43 100644 --- a/persistence-modules/hibernate-annotations/pom.xml +++ b/persistence-modules/hibernate-annotations/pom.xml @@ -16,7 +16,7 @@ - + org.springframework spring-context @@ -32,7 +32,7 @@ hibernate-core ${hibernate-core.version} - + org.hsqldb hsqldb ${hsqldb.version} diff --git a/persistence-modules/hibernate-queries/pom.xml b/persistence-modules/hibernate-queries/pom.xml index 4f5de5c06e..ff5a9fe221 100644 --- a/persistence-modules/hibernate-queries/pom.xml +++ b/persistence-modules/hibernate-queries/pom.xml @@ -14,7 +14,7 @@ - + org.springframework spring-context @@ -30,8 +30,7 @@ tomcat-dbcp ${tomcat-dbcp.version} - - + com.google.guava @@ -45,7 +44,6 @@ ${org.springframework.version} test - org.hibernate hibernate-core @@ -81,8 +79,7 @@ jmh-generator-annprocess ${jmh-generator.version} - - + 5.0.2.RELEASE diff --git a/persistence-modules/java-mongodb-2/pom.xml b/persistence-modules/java-mongodb-2/pom.xml index ffc8da0b64..9475a86bee 100644 --- a/persistence-modules/java-mongodb-2/pom.xml +++ b/persistence-modules/java-mongodb-2/pom.xml @@ -50,4 +50,4 @@ 1.5.3 - + \ No newline at end of file diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 2e7dafb472..3f9f3068b4 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -108,4 +108,4 @@ 42.2.20 - + \ No newline at end of file diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 003a52db13..b96384e6f8 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -6,8 +6,8 @@ com.baeldung.spring-boot-modules spring-boot-modules 1.0.0-SNAPSHOT - pom spring-boot-modules + pom com.baeldung @@ -16,7 +16,7 @@ ../parent-boot-2 - + spring-boot-admin spring-boot-angular spring-boot-annotations @@ -98,4 +98,4 @@ - + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-artifacts/pom.xml b/spring-boot-modules/spring-boot-artifacts/pom.xml index 0292dc95cf..7709c6a316 100644 --- a/spring-boot-modules/spring-boot-artifacts/pom.xml +++ b/spring-boot-modules/spring-boot-artifacts/pom.xml @@ -101,7 +101,7 @@ maven-failsafe-plugin 2.18 - integration-tests @@ -110,7 +110,7 @@ verify - **/ExternalPropertyFileLoaderIntegrationTest.java @@ -195,4 +195,4 @@ 4.5.8 - + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-mvc-4/pom.xml b/spring-boot-modules/spring-boot-mvc-4/pom.xml index 5b58b326ab..b1c079b715 100644 --- a/spring-boot-modules/spring-boot-mvc-4/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-4/pom.xml @@ -1,14 +1,13 @@ - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-boot-mvc-4 spring-boot-mvc-4 jar Module For Spring Boot MVC Web - + com.baeldung.spring-boot-modules spring-boot-modules @@ -70,7 +69,6 @@ - 3.0.0 com.baeldung.springboot.swagger.ArticleApplication diff --git a/spring-boot-modules/spring-boot-properties-migrator-demo/pom.xml b/spring-boot-modules/spring-boot-properties-migrator-demo/pom.xml index d44a8ce6f1..95dc06b155 100644 --- a/spring-boot-modules/spring-boot-properties-migrator-demo/pom.xml +++ b/spring-boot-modules/spring-boot-properties-migrator-demo/pom.xml @@ -1,9 +1,8 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - spring-boot-properties-migrator-demo 1.0-SNAPSHOT @@ -13,18 +12,13 @@ 1.0.0-SNAPSHOT ../pom.xml - - - - - - - - - - 8 - 8 - + + + + + + + @@ -54,4 +48,9 @@ - + + 8 + 8 + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-validation/pom.xml b/spring-boot-modules/spring-boot-validation/pom.xml index 639a62059d..1412a57e2a 100644 --- a/spring-boot-modules/spring-boot-validation/pom.xml +++ b/spring-boot-modules/spring-boot-validation/pom.xml @@ -22,7 +22,7 @@ org.hibernate.validator hibernate-validator - + org.springframework.boot spring-boot-starter-data-jpa diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index 9205416cd5..f58e1aec5c 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -92,4 +92,4 @@ 3.1.3 - + \ No newline at end of file diff --git a/spring-reactive/pom.xml b/spring-reactive/pom.xml index 37df1a820d..396fa3ee4a 100644 --- a/spring-reactive/pom.xml +++ b/spring-reactive/pom.xml @@ -59,7 +59,6 @@ integration-lite-first - @@ -76,7 +75,6 @@ integration-lite-second - diff --git a/spring-roo/pom.xml b/spring-roo/pom.xml index ea42095d92..fa84ec9558 100644 --- a/spring-roo/pom.xml +++ b/spring-roo/pom.xml @@ -18,6 +18,190 @@ + + + + + org.springframework.roo + org.springframework.roo.annotations + ${roo.version} + pom + provided + + + io.springlets + springlets-data-jpa + ${springlets.version} + + + io.springlets + springlets-data-commons + ${springlets.version} + + + io.springlets + springlets-context + ${springlets.version} + + + org.springframework.roo + org.springframework.roo.querydsl.processor + ${querydsl-processor.version} + + + io.tracee.binding + tracee-springmvc + ${tracee.version} + + + io.springlets + springlets-boot-starter-web + ${springlets.version} + + + com.github.mxab.thymeleaf.extras + thymeleaf-extras-data-attribute + ${thymeleaf-data-dialect.version} + + + ar.com.fdvs + DynamicJasper + ${dynamicjasper.version} + + + ar.com.fdvs + DynamicJasper-core-fonts + ${dynamicjasper-fonts.version} + + + org.webjars.bower + bootstrap + ${bootstrap.version} + + + org.webjars.bower + datatables + ${datatables.version} + + + org.webjars.bower + datatables.net-bs + ${datatables-bs.version} + + + org.webjars.bower + datatables.net-buttons + ${datatables-buttons.version} + + + org.webjars.bower + datatables.net-buttons-bs + ${datatables-buttons-bs.version} + + + org.webjars.bower + datatables.net-responsive + ${datatables-responsive.version} + + + org.webjars.bower + datatables.net-responsive-bs + ${datatables-responsive-bs.version} + + + org.webjars.bower + datatables.net-select + ${datatables-select.version} + + + org.webjars.bower + datatables.net-select-bs + ${datatables-select-bs.version} + + + org.webjars.npm + jquery-datatables-checkboxes + ${datatables-checkboxes.version} + + + org.webjars.npm + jquery + + + org.webjars.npm + datatables.net + + + + + org.webjars.bower + github-com-julmot-datatables-mark-js + ${datatables-mark.version} + + + org.webjars.bower + datetimepicker + ${datetimepicker.version} + + + org.webjars.bower + font-awesome + ${fontawesome.version} + + + org.webjars.bower + jquery + ${jquery.version} + + + org.webjars + jquery.inputmask + ${jquery-inputmask.version} + + + org.webjars + jquery + + + + + org.webjars.bower + jquery-validation + ${jquery-validation.version} + + + org.webjars.bower + momentjs + ${momentjs.version} + + + org.webjars.bower + select2 + ${select2.version} + + + org.webjars.bower + select2-bootstrap-theme + ${select2-bootstrap-theme.version} + + + org.webjars + respond + ${respond.version} + + + org.webjars + html5shiv + ${html5shiv.version} + + + org.webjars.bower + ie10-viewport-bug-workaround + ${bootstrap.ie10-viewport-bug-workaround.version} + + + + @@ -404,190 +588,6 @@ - - - - - org.springframework.roo - org.springframework.roo.annotations - ${roo.version} - pom - provided - - - io.springlets - springlets-data-jpa - ${springlets.version} - - - io.springlets - springlets-data-commons - ${springlets.version} - - - io.springlets - springlets-context - ${springlets.version} - - - org.springframework.roo - org.springframework.roo.querydsl.processor - ${querydsl-processor.version} - - - io.tracee.binding - tracee-springmvc - ${tracee.version} - - - io.springlets - springlets-boot-starter-web - ${springlets.version} - - - com.github.mxab.thymeleaf.extras - thymeleaf-extras-data-attribute - ${thymeleaf-data-dialect.version} - - - ar.com.fdvs - DynamicJasper - ${dynamicjasper.version} - - - ar.com.fdvs - DynamicJasper-core-fonts - ${dynamicjasper-fonts.version} - - - org.webjars.bower - bootstrap - ${bootstrap.version} - - - org.webjars.bower - datatables - ${datatables.version} - - - org.webjars.bower - datatables.net-bs - ${datatables-bs.version} - - - org.webjars.bower - datatables.net-buttons - ${datatables-buttons.version} - - - org.webjars.bower - datatables.net-buttons-bs - ${datatables-buttons-bs.version} - - - org.webjars.bower - datatables.net-responsive - ${datatables-responsive.version} - - - org.webjars.bower - datatables.net-responsive-bs - ${datatables-responsive-bs.version} - - - org.webjars.bower - datatables.net-select - ${datatables-select.version} - - - org.webjars.bower - datatables.net-select-bs - ${datatables-select-bs.version} - - - org.webjars.npm - jquery-datatables-checkboxes - ${datatables-checkboxes.version} - - - org.webjars.npm - jquery - - - org.webjars.npm - datatables.net - - - - - org.webjars.bower - github-com-julmot-datatables-mark-js - ${datatables-mark.version} - - - org.webjars.bower - datetimepicker - ${datetimepicker.version} - - - org.webjars.bower - font-awesome - ${fontawesome.version} - - - org.webjars.bower - jquery - ${jquery.version} - - - org.webjars - jquery.inputmask - ${jquery-inputmask.version} - - - org.webjars - jquery - - - - - org.webjars.bower - jquery-validation - ${jquery-validation.version} - - - org.webjars.bower - momentjs - ${momentjs.version} - - - org.webjars.bower - select2 - ${select2.version} - - - org.webjars.bower - select2-bootstrap-theme - ${select2-bootstrap-theme.version} - - - org.webjars - respond - ${respond.version} - - - org.webjars - html5shiv - ${html5shiv.version} - - - org.webjars.bower - ie10-viewport-bug-workaround - ${bootstrap.ie10-viewport-bug-workaround.version} - - - - 2.0.0.RELEASE 8 diff --git a/spring-security-modules/spring-security-opa/pom.xml b/spring-security-modules/spring-security-opa/pom.xml index 6665c33db3..72b0574253 100644 --- a/spring-security-modules/spring-security-opa/pom.xml +++ b/spring-security-modules/spring-security-opa/pom.xml @@ -1,49 +1,48 @@ - - 4.0.0 - - com.baeldung - parent-boot-2 - 0.0.1-SNAPSHOT - ../../parent-boot-2 - - spring-security-opa - Spring Security with OPA authorization + + 4.0.0 + spring-security-opa + Spring Security with OPA authorization + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.boot + spring-boot-starter-security + + + org.projectlombok + lombok + + + com.google.guava + guava + 31.0.1-jre + + + org.springframework.boot + spring-boot-devtools + + + org.springframework.security + spring-security-test + + + org.springframework.boot + spring-boot-configuration-processor + true + + - - - org.springframework.boot - spring-boot-starter-webflux - - - - org.springframework.boot - spring-boot-starter-security - - - - org.projectlombok - lombok - - - - com.google.guava - guava - 31.0.1-jre - - - - org.springframework.boot - spring-boot-devtools - - - - org.springframework.security - spring-security-test - - - org.springframework.boot - spring-boot-configuration-processor - true - - \ No newline at end of file diff --git a/spring-security-modules/spring-security-web-boot-3/pom.xml b/spring-security-modules/spring-security-web-boot-3/pom.xml index 5da993acd9..0984c25e07 100644 --- a/spring-security-modules/spring-security-web-boot-3/pom.xml +++ b/spring-security-modules/spring-security-web-boot-3/pom.xml @@ -1,6 +1,7 @@ - + 4.0.0 spring-security-web-boot-3 0.0.1-SNAPSHOT @@ -73,5 +74,4 @@ 3.6.0 - \ No newline at end of file diff --git a/spring-security-modules/spring-security-web-login/pom.xml b/spring-security-modules/spring-security-web-login/pom.xml index 346338cbcd..c2369abc14 100644 --- a/spring-security-modules/spring-security-web-login/pom.xml +++ b/spring-security-modules/spring-security-web-login/pom.xml @@ -118,7 +118,7 @@ runtime - + org.springframework.boot spring-boot-starter-test ${spring-boot.version} @@ -136,11 +136,11 @@ ${spring-security.version} test - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + diff --git a/spring-security-modules/spring-security-web-x509/spring-security-web-x509-basic-auth/pom.xml b/spring-security-modules/spring-security-web-x509/spring-security-web-x509-basic-auth/pom.xml index 9598843b63..12153c91f2 100644 --- a/spring-security-modules/spring-security-web-x509/spring-security-web-x509-basic-auth/pom.xml +++ b/spring-security-modules/spring-security-web-x509/spring-security-web-x509-basic-auth/pom.xml @@ -21,7 +21,6 @@ org.springframework.boot spring-boot-maven-plugin - org.apache.maven.plugins maven-surefire-plugin diff --git a/spring-security-modules/spring-security-web-x509/spring-security-web-x509-client-auth/pom.xml b/spring-security-modules/spring-security-web-x509/spring-security-web-x509-client-auth/pom.xml index f310ab1e5c..f3ea2728f2 100644 --- a/spring-security-modules/spring-security-web-x509/spring-security-web-x509-client-auth/pom.xml +++ b/spring-security-modules/spring-security-web-x509/spring-security-web-x509-client-auth/pom.xml @@ -21,7 +21,6 @@ org.springframework.boot spring-boot-maven-plugin - org.apache.maven.plugins maven-surefire-plugin diff --git a/spring-swagger-codegen/spring-openapi-generator-api-client/pom.xml b/spring-swagger-codegen/spring-openapi-generator-api-client/pom.xml index 2afde0b07d..f125018fb0 100644 --- a/spring-swagger-codegen/spring-openapi-generator-api-client/pom.xml +++ b/spring-swagger-codegen/spring-openapi-generator-api-client/pom.xml @@ -9,13 +9,11 @@ jar https://github.com/openapitools/openapi-generator OpenAPI Java - scm:git:git@github.com:openapitools/openapi-generator.git scm:git:git@github.com:openapitools/openapi-generator.git https://github.com/openapitools/openapi-generator - Unlicense @@ -23,7 +21,6 @@ repo - OpenAPI-Generator Contributors diff --git a/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml b/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml index 66e9d1dda2..c9ba912feb 100644 --- a/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml +++ b/spring-swagger-codegen/spring-swagger-codegen-api-client/pom.xml @@ -8,13 +8,11 @@ jar https://github.com/swagger-api/swagger-codegen Swagger Java - scm:git:git@github.com:swagger-api/swagger-codegen.git scm:git:git@github.com:swagger-api/swagger-codegen.git https://github.com/swagger-api/swagger-codegen - Unlicense @@ -22,7 +20,6 @@ repo - Swagger diff --git a/spring-web-modules/spring-resttemplate/pom.xml b/spring-web-modules/spring-resttemplate/pom.xml index e5a8ba5ea9..4abaac5628 100644 --- a/spring-web-modules/spring-resttemplate/pom.xml +++ b/spring-web-modules/spring-resttemplate/pom.xml @@ -37,12 +37,12 @@ org.springframework.boot spring-boot-starter-test - + au.com.dius pact-jvm-provider-junit5_2.12 ${pact.version} - - + + au.com.dius pact-jvm-consumer-junit5_2.12 ${pact.version} @@ -119,10 +119,10 @@ spring-test - org.mockito - mockito-junit-jupiter - ${mockito.version} - test + org.mockito + mockito-junit-jupiter + ${mockito.version} + test diff --git a/testing-modules/junit-5-basics/pom.xml b/testing-modules/junit-5-basics/pom.xml index e240efe514..a758d79069 100644 --- a/testing-modules/junit-5-basics/pom.xml +++ b/testing-modules/junit-5-basics/pom.xml @@ -15,12 +15,12 @@ - - org.junit.platform - junit-platform-suite - ${junit-platform.version} + + org.junit.platform + junit-platform-suite + ${junit-platform.version} test - + org.junit.jupiter junit-jupiter-migrationsupport diff --git a/testing-modules/testng-command-line/pom.xml b/testing-modules/testng-command-line/pom.xml index efc49b187d..a71238f4fb 100644 --- a/testing-modules/testng-command-line/pom.xml +++ b/testing-modules/testng-command-line/pom.xml @@ -78,7 +78,6 @@ - ExecuteTestSuite @@ -101,6 +100,7 @@ + UTF-8 1.8 @@ -112,4 +112,5 @@ 3.8.0 2.22.1 - + + \ No newline at end of file From 22d59b01933a34fb12eaa9be4cd28850982a3d6a Mon Sep 17 00:00:00 2001 From: panagiotiskakos Date: Mon, 9 May 2022 09:45:22 +0300 Subject: [PATCH 31/94] [JAVA-10597] Increased JOOQ version --- persistence-modules/spring-jooq/pom.xml | 2 +- .../jooq/introduction/CountQueryIntegrationTest.java | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/persistence-modules/spring-jooq/pom.xml b/persistence-modules/spring-jooq/pom.xml index c842922fe5..6a9fb0ef06 100644 --- a/persistence-modules/spring-jooq/pom.xml +++ b/persistence-modules/spring-jooq/pom.xml @@ -195,7 +195,7 @@ - 3.12.4 + 3.14.15 1.0.0 1.5 1.0.0 diff --git a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java index 7edcc2cd4b..361569554c 100644 --- a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java +++ b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java @@ -2,8 +2,6 @@ package com.baeldung.jooq.introduction; import static com.baeldung.jooq.introduction.db.public_.tables.Author.AUTHOR; -import java.util.ArrayList; -import java.util.List; import org.jooq.Condition; import org.jooq.DSLContext; @@ -68,12 +66,7 @@ public class CountQueryIntegrationTest { @Test public void givenValidData_whenFetchCountWithMultipleConditions_thenSucceed() { - Condition firstCond = AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan"); - Condition secondCond = AUTHOR.ID.notEqual(1); - List conditions = new ArrayList<>(); - conditions.add(firstCond); - conditions.add(secondCond); - int count = dsl.fetchCount(AUTHOR, conditions); + int count = dsl.fetchCount(AUTHOR, AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan").and(AUTHOR.ID.notEqual(1))); Assert.assertEquals(1, count); } From 1cc8a0a4d1088f779c2282150f9b87a7cce55531 Mon Sep 17 00:00:00 2001 From: panagiotiskakos Date: Mon, 9 May 2022 12:26:15 +0300 Subject: [PATCH 32/94] [JAVA-11770] Using properties for maven plugin --- aws-modules/aws-miscellaneous/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aws-modules/aws-miscellaneous/pom.xml b/aws-modules/aws-miscellaneous/pom.xml index 08e4e36c73..888a2a561f 100644 --- a/aws-modules/aws-miscellaneous/pom.xml +++ b/aws-modules/aws-miscellaneous/pom.xml @@ -81,7 +81,7 @@ org.apache.maven.plugins maven-dependency-plugin - 3.1.1 + ${maven-plugins-version} copy @@ -118,6 +118,7 @@ 1.10.L001 0.9.4.0006L 3.0.0 + 3.1.1 \ No newline at end of file From 78807f6d1a82fa5336140e20157f1b9b60b5f826 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Mon, 9 May 2022 20:56:15 +0530 Subject: [PATCH 33/94] JAVA-11367 Update article - Setting the MySQL JDBC Timezone --- persistence-modules/spring-boot-mysql/pom.xml | 2 +- .../spring-boot-mysql/src/main/resources/application.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/persistence-modules/spring-boot-mysql/pom.xml b/persistence-modules/spring-boot-mysql/pom.xml index ed3f7d9279..239378c7b1 100644 --- a/persistence-modules/spring-boot-mysql/pom.xml +++ b/persistence-modules/spring-boot-mysql/pom.xml @@ -39,7 +39,7 @@ - 8.0.12 + 8.0.23 \ No newline at end of file diff --git a/persistence-modules/spring-boot-mysql/src/main/resources/application.yml b/persistence-modules/spring-boot-mysql/src/main/resources/application.yml index 5404555d49..f660ab4759 100644 --- a/persistence-modules/spring-boot-mysql/src/main/resources/application.yml +++ b/persistence-modules/spring-boot-mysql/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: datasource: - url: jdbc:mysql://localhost:3306/test?useLegacyDatetimeCode=false + url: jdbc:mysql://localhost:3306/test? username: root password: @@ -9,6 +9,6 @@ spring: ddl-auto: update properties: hibernate: - dialect: org.hibernate.dialect.MySQL5Dialect + dialect: org.hibernate.dialect.MySQL8Dialect jdbc: time_zone: UTC \ No newline at end of file From 81b95583428d4f8b8b508e269e6c76d8c14fd65d Mon Sep 17 00:00:00 2001 From: Mayank Aggarwal Date: Tue, 10 May 2022 00:34:56 +0530 Subject: [PATCH 34/94] BAEL-5558: Sorting By Date in Java (#12132) * [BAEL-5438] Added Criteria Queries for Employee * [BAEL-5558] Sorting By Date in Java * BAEL-5558: Added and refactored tests name * [BAEL-5558] Refactored the code. * [BAEL-5558] Resolved PMD violation. Co-authored-by: Mayank Agarwal --- .../core-java-collections-4/pom.xml | 8 + .../collections/sorting/Employee.java | 72 +++++++++ .../EmployeeSortingByDateUnitTest.java | 146 ++++++++++++++++++ .../EmployeeCriteriaIntegrationTest.java | 1 - 4 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/sorting/Employee.java create mode 100644 core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/sorting/EmployeeSortingByDateUnitTest.java diff --git a/core-java-modules/core-java-collections-4/pom.xml b/core-java-modules/core-java-collections-4/pom.xml index 2193b5118a..68d7df66d0 100644 --- a/core-java-modules/core-java-collections-4/pom.xml +++ b/core-java-modules/core-java-collections-4/pom.xml @@ -14,4 +14,12 @@ 0.0.1-SNAPSHOT + + + commons-lang + commons-lang + 2.2 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/sorting/Employee.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/sorting/Employee.java new file mode 100644 index 0000000000..d41ad54295 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/sorting/Employee.java @@ -0,0 +1,72 @@ +package com.baeldung.collections.sorting; + +import java.util.Date; + +public class Employee implements Comparable{ + + private String name; + private int age; + private double salary; + private Date joiningDate; + + public Employee(String name, int age, double salary, Date joiningDate) { + this.name = name; + this.age = age; + this.salary = salary; + this.joiningDate = joiningDate; + } + + 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; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + this.salary = salary; + } + + public Date getJoiningDate() { + return joiningDate; + } + + public void setJoiningDate(Date joiningDate) { + this.joiningDate = joiningDate; + } + + @Override + public boolean equals(Object obj) { + return ((Employee) obj).getName() + .equals(getName()); + } + + @Override + public String toString() { + return new StringBuffer().append("(") + .append(getName()).append(",") + .append(getAge()) + .append(",") + .append(getSalary()).append(",").append(getJoiningDate()) + .append(")") + .toString(); + } + + @Override + public int compareTo(Employee employee) { + return getJoiningDate().compareTo(employee.getJoiningDate()); + } +} diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/sorting/EmployeeSortingByDateUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/sorting/EmployeeSortingByDateUnitTest.java new file mode 100644 index 0000000000..b8acb789a6 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/collections/sorting/EmployeeSortingByDateUnitTest.java @@ -0,0 +1,146 @@ +package com.baeldung.collections.sorting; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.List; +import org.apache.commons.lang.time.DateUtils; +import org.junit.Before; +import org.junit.Test; + +public class EmployeeSortingByDateUnitTest { + + private List employees = new ArrayList<>(); + private List employeesSortedByDateAsc = new ArrayList<>(); + private List employeesSortedByDateDesc = new ArrayList<>(); + + @Before + public void initVariables() { + + Collections.addAll(employees, + new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)), + new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)), + new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)), + new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)), + new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)), + new Employee("John", 23, 5000, new Date()) + ); + + Collections.addAll(employeesSortedByDateDesc, + new Employee("John", 23, 5000, new Date()), + new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)), + new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)), + new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)), + new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)), + new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)) + ); + + Collections.addAll(employeesSortedByDateAsc, + new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)), + new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)), + new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)), + new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)), + new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)), + new Employee("John", 23, 5000, new Date()) + ); + } + + @Test + public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() { + Collections.sort(employees); + + assertEquals(employees, employeesSortedByDateAsc); + } + + @Test + public void givenEmpList_SortEmpList_thenCheckSortedList() { + + Collections.sort(employees, new Comparator() { + public int compare(Employee o1, Employee o2) { + return o1.getJoiningDate().compareTo(o2.getJoiningDate()); + } + }); + + assertEquals(employees, employeesSortedByDateAsc); + } + + @Test + public void givenEmpList_SortEmpList_thenCheckSortedListV1() { + + Collections.sort(employees, new Comparator() { + public int compare(Employee emp1, Employee emp2) { + if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null) + return 0; + return emp1.getJoiningDate().compareTo(emp2.getJoiningDate()); + } + }); + + assertEquals(employees, employeesSortedByDateAsc); + } + + @Test + public void givenEmpList_SortEmpList_thenSortedListinAscOrder() { + Collections.sort(employees, Collections.reverseOrder()); + + assertEquals(employees, employeesSortedByDateDesc); + } + + @Test + public void givenEmpList_SortEmpList_thenCheckSortedListAsc() { + + Collections.sort(employees, new Comparator() { + public int compare(Employee emp1, Employee emp2) { + return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); + } + }); + + assertEquals(employees, employeesSortedByDateDesc); + } + + @Test + public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() { + + Collections.sort(employees, new Comparator() { + public int compare(Employee emp1, Employee emp2) { + if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null) + return 0; + return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); + } + }); + + assertEquals(employees, employeesSortedByDateDesc); + } + + @Test + public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() { + + Collections.sort(employees, + (emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate())); + + assertEquals(employees, employeesSortedByDateDesc); + } + + @Test + public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() { + + Collections.sort(employees, (emp1, emp2) -> { + if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null) + return 0; + return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); + }); + + assertEquals(employees, employeesSortedByDateDesc); + } + + @Test + public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() { + Collections.sort(employees, + Comparator.comparing(Employee::getJoiningDate)); + assertEquals(employees, employeesSortedByDateAsc); + } + +} diff --git a/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java b/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java index 61f54aaea8..4553bf398c 100644 --- a/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java +++ b/persistence-modules/hibernate-queries/src/test/java/com/baeldung/hibernate/criteria/EmployeeCriteriaIntegrationTest.java @@ -32,5 +32,4 @@ public class EmployeeCriteriaIntegrationTest { session.close(); assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray()); } - } From 6de9954c286432753edf96163af781aeb79fac92 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 05:47:26 +0500 Subject: [PATCH 35/94] Updated README.md added link back to the article: https://www.baeldung.com/spring-injecting-all-annotated-beans --- spring-di-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-di-3/README.md b/spring-di-3/README.md index 4246069616..3ddd720af5 100644 --- a/spring-di-3/README.md +++ b/spring-di-3/README.md @@ -6,4 +6,5 @@ This module contains articles about dependency injection with Spring - [@Lookup Annotation in Spring](https://www.baeldung.com/spring-lookup) - [Spring @Autowired Field Null – Common Causes and Solutions](https://www.baeldung.com/spring-autowired-field-null) +- [Finding All Beans with a Custom Annotation](https://www.baeldung.com/spring-injecting-all-annotated-beans) - More articles: [[<-- prev]](../spring-di-2) From 17106eb651c263c205096bc5708c90f0068821ab Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 05:54:15 +0500 Subject: [PATCH 36/94] Updated README.md added link back to the article: https://www.baeldung.com/spark-dataframes --- apache-spark/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/apache-spark/README.md b/apache-spark/README.md index 3a2d2f4e15..862626988b 100644 --- a/apache-spark/README.md +++ b/apache-spark/README.md @@ -9,3 +9,4 @@ This module contains articles about Apache Spark - [Machine Learning with Spark MLlib](https://www.baeldung.com/spark-mlib-machine-learning) - [Introduction to Spark Graph Processing with GraphFrames](https://www.baeldung.com/spark-graph-graphframes) - [Apache Spark: Differences between Dataframes, Datasets and RDDs](https://www.baeldung.com/java-spark-dataframe-dataset-rdd) +- [Spark DataFrame](https://www.baeldung.com/spark-dataframes) From e4271c648b2583eb138ba70ecef398339d9373fe Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:00:05 +0500 Subject: [PATCH 37/94] Updated README.md added link back to the article: https://www.baeldung.com/ops/docker-push-image-to-private-repository --- docker/docker-push-to-private-repo/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docker/docker-push-to-private-repo/README.md b/docker/docker-push-to-private-repo/README.md index e320af31b4..61fe00e016 100644 --- a/docker/docker-push-to-private-repo/README.md +++ b/docker/docker-push-to-private-repo/README.md @@ -1 +1,3 @@ -### Relevant Articles: \ No newline at end of file +### Relevant Articles: + +- [Pushing a Docker Image to a Private Repository](https://www.baeldung.com/ops/docker-push-image-to-private-repository) From 05772eb51928f2b0b48523add5b4b704d4865111 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:12:49 +0500 Subject: [PATCH 38/94] Updated README.md added a link back to the article: https://www.baeldung.com/java-equals-method-operator-difference --- core-java-modules/core-java-lang-5/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core-java-modules/core-java-lang-5/README.md b/core-java-modules/core-java-lang-5/README.md index 012f4edc51..8920e9c231 100644 --- a/core-java-modules/core-java-lang-5/README.md +++ b/core-java-modules/core-java-lang-5/README.md @@ -2,4 +2,6 @@ This module contains articles about core features in the Java language -## TODO ## +### Relevant Articles: + +- [Difference Between == and equals() in Java](https://www.baeldung.com/java-equals-method-operator-difference) From 0fce3b833a9069273ed6851db913908bdf4ec28b Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:19:26 +0500 Subject: [PATCH 39/94] Updated README.md added link back to the article: https://www.baeldung.com/java-swap-two-variables --- core-java-modules/core-java-lang-math-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-lang-math-3/README.md b/core-java-modules/core-java-lang-math-3/README.md index 1dd3a3c7e0..3ddaddae39 100644 --- a/core-java-modules/core-java-lang-math-3/README.md +++ b/core-java-modules/core-java-lang-math-3/README.md @@ -5,4 +5,5 @@ ### Relevant articles: - [Evaluating a Math Expression in Java](https://www.baeldung.com/java-evaluate-math-expression-string) +- [Swap Two Variables in Java](https://www.baeldung.com/java-swap-two-variables) - More articles: [[<-- Prev]](/core-java-modules/core-java-lang-math-2) From fd426982798ee88f7c35a0296522dc22c2dfcc02 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:27:02 +0500 Subject: [PATCH 40/94] Created/Updated README.md added link back to the article: https://www.baeldung.com/spring-security-authorization-opa --- spring-security-modules/spring-security-opa/README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 spring-security-modules/spring-security-opa/README.md diff --git a/spring-security-modules/spring-security-opa/README.md b/spring-security-modules/spring-security-opa/README.md new file mode 100644 index 0000000000..d2c1652edb --- /dev/null +++ b/spring-security-modules/spring-security-opa/README.md @@ -0,0 +1,4 @@ + +### Relevant Articles: + +- [Spring Security Authorization with OPA](https://www.baeldung.com/spring-security-authorization-opa) From 1e2b926b8e623f07da543bae32cce54bf225fa0f Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:36:05 +0500 Subject: [PATCH 41/94] Updated README.md added link back to the article: https://www.baeldung.com/postman-add-headers-pre-request --- spring-boot-modules/spring-boot-mvc-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-boot-modules/spring-boot-mvc-2/README.md b/spring-boot-modules/spring-boot-mvc-2/README.md index 0d0e05daf0..30e6d71a30 100644 --- a/spring-boot-modules/spring-boot-mvc-2/README.md +++ b/spring-boot-modules/spring-boot-mvc-2/README.md @@ -11,4 +11,5 @@ This module contains articles about Spring Web MVC in Spring Boot projects. - [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections) - [Spring Boot Consuming and Producing JSON](https://www.baeldung.com/spring-boot-json) - [Serve Static Resources with Spring](https://www.baeldung.com/spring-mvc-static-resources) +- [Add Header to Every Request in Postman](https://www.baeldung.com/postman-add-headers-pre-request) - More articles: [[prev -->]](/spring-boot-modules/spring-boot-mvc) From 4a8826ae78c4e07b0ec784dc7157eb3a1eeda0ad Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:40:56 +0500 Subject: [PATCH 42/94] Updated README.md added link back to the article: https://www.baeldung.com/java-atomic-set-vs-lazyset --- core-java-modules/core-java-concurrency-advanced-4/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-concurrency-advanced-4/README.md b/core-java-modules/core-java-concurrency-advanced-4/README.md index 808db89b12..d9207644b3 100644 --- a/core-java-modules/core-java-concurrency-advanced-4/README.md +++ b/core-java-modules/core-java-concurrency-advanced-4/README.md @@ -6,3 +6,4 @@ - [Volatile Variables and Thread Safety](https://www.baeldung.com/java-volatile-variables-thread-safety) - [Producer-Consumer Problem With Example in Java](https://www.baeldung.com/java-producer-consumer-problem) - [Acquire a Lock by a Key in Java](https://www.baeldung.com/java-acquire-lock-by-key) +- [Differences Between set() and lazySet() in Java Atomic Variables](https://www.baeldung.com/java-atomic-set-vs-lazyset) From 3ba1e439cfabc5802bae54259da73a3ec29b6386 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:49:34 +0500 Subject: [PATCH 43/94] Updated README.md added link back to the article: https://www.baeldung.com/jpql-hql-criteria-query --- persistence-modules/spring-data-jpa-query-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/spring-data-jpa-query-3/README.md b/persistence-modules/spring-data-jpa-query-3/README.md index 246b6ebf3d..920ec40965 100644 --- a/persistence-modules/spring-data-jpa-query-3/README.md +++ b/persistence-modules/spring-data-jpa-query-3/README.md @@ -4,6 +4,7 @@ This module contains articles about querying data using Spring Data JPA. ### Relevant Articles: - [Query Entities by Dates and Times with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-query-by-date) +- [JPA and Hibernate – Criteria vs. JPQL vs. HQL Query](https://www.baeldung.com/jpql-hql-criteria-query) - More articles: [[<-- prev]](../spring-data-jpa-query-2) ### Eclipse Config From b05960761dbe50ea7dba1e2c81871451ff7a5550 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:49:43 +0500 Subject: [PATCH 44/94] Updated README.md added link back to the article: https://www.baeldung.com/jpql-hql-criteria-query --- persistence-modules/hibernate-queries/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/hibernate-queries/README.md b/persistence-modules/hibernate-queries/README.md index ac52e73abf..f5cba1aa6f 100644 --- a/persistence-modules/hibernate-queries/README.md +++ b/persistence-modules/hibernate-queries/README.md @@ -11,3 +11,4 @@ This module contains articles about use of Queries in Hibernate. - [Hibernate Query Plan Cache](https://www.baeldung.com/hibernate-query-plan-cache) - [Hibernate’s addScalar() Method](https://www.baeldung.com/hibernate-addscalar) - [Distinct Queries in HQL](https://www.baeldung.com/java-hql-distinct) +- [JPA and Hibernate – Criteria vs. JPQL vs. HQL Query](https://www.baeldung.com/jpql-hql-criteria-query) From d33c2ea16fa93d32fa1d0a9ac34f96944741ce3d Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:53:47 +0500 Subject: [PATCH 45/94] Updated README.md added link back to the article: https://www.baeldung.com/java-httpclient-ssl --- core-java-modules/core-java-11-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-11-2/README.md b/core-java-modules/core-java-11-2/README.md index c42b3f0e18..ebd857886d 100644 --- a/core-java-modules/core-java-11-2/README.md +++ b/core-java-modules/core-java-11-2/README.md @@ -12,3 +12,4 @@ This module contains articles about Java 11 core features - [Java HTTPS Client Certificate Authentication](https://www.baeldung.com/java-https-client-certificate-authentication) - [Call Methods at Runtime Using Java Reflection](https://www.baeldung.com/java-method-reflection) - [Java HttpClient Basic Authentication](https://www.baeldung.com/java-httpclient-basic-auth) +- [Java HttpClient With SSL](https://www.baeldung.com/java-httpclient-ssl) From 35ba3dcd90b015fd092cc0f1847eb8a996ba8c0c Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 06:57:31 +0500 Subject: [PATCH 46/94] Updated README.md added link back to the article: https://www.baeldung.com/java-iterator-vs-iterable --- core-java-modules/core-java-collections-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-collections-2/README.md b/core-java-modules/core-java-collections-2/README.md index e5f6126811..d482ed7773 100644 --- a/core-java-modules/core-java-collections-2/README.md +++ b/core-java-modules/core-java-collections-2/README.md @@ -12,3 +12,4 @@ - [Sorting in Java](https://www.baeldung.com/java-sorting) - [Getting the Size of an Iterable in Java](https://www.baeldung.com/java-iterable-size) - [Java Null-Safe Streams from Collections](https://www.baeldung.com/java-null-safe-streams-from-collections) +- [Differences Between Iterator and Iterable and How to Use Them?](https://www.baeldung.com/java-iterator-vs-iterable) From f1008e4fe54c4c3850b0ab498a3bb58a5f8dbc5a Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 10 May 2022 07:01:26 +0500 Subject: [PATCH 47/94] Updated README.md added link back to the article: https://www.baeldung.com/spring-boot-docker-start-with-profile --- docker/docker-spring-boot/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docker/docker-spring-boot/README.md b/docker/docker-spring-boot/README.md index 4af9378290..0fc9d2b6b5 100644 --- a/docker/docker-spring-boot/README.md +++ b/docker/docker-spring-boot/README.md @@ -1,3 +1,4 @@ ### Relevant Articles: - [Creating Docker Images with Spring Boot](https://www.baeldung.com/spring-boot-docker-images) +- [Starting Spring Boot Application in Docker With Profile](https://www.baeldung.com/spring-boot-docker-start-with-profile) From 635f834aeaab755db99339042cdf0e582d011d06 Mon Sep 17 00:00:00 2001 From: panagiotiskakos Date: Tue, 10 May 2022 10:45:49 +0300 Subject: [PATCH 48/94] [JAVA-10597] Reverted + Enhanced test cases --- .../introduction/CountQueryIntegrationTest.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java index 361569554c..9765a26a2b 100644 --- a/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java +++ b/persistence-modules/spring-jooq/src/test/java/com/baeldung/jooq/introduction/CountQueryIntegrationTest.java @@ -2,6 +2,8 @@ package com.baeldung.jooq.introduction; import static com.baeldung.jooq.introduction.db.public_.tables.Author.AUTHOR; +import java.util.ArrayList; +import java.util.List; import org.jooq.Condition; import org.jooq.DSLContext; @@ -66,10 +68,22 @@ public class CountQueryIntegrationTest { @Test public void givenValidData_whenFetchCountWithMultipleConditions_thenSucceed() { + Condition firstCond = AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan"); + Condition secondCond = AUTHOR.ID.notEqual(1); + List conditions = new ArrayList<>(); + conditions.add(firstCond); + conditions.add(secondCond); + int count = dsl.fetchCount(AUTHOR, conditions); + Assert.assertEquals(1, count); + } + + + @Test + public void givenValidData_whenFetchCountWithMultipleConditionsUsingAndOperator_thenSucceed() { int count = dsl.fetchCount(AUTHOR, AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan").and(AUTHOR.ID.notEqual(1))); Assert.assertEquals(1, count); } - + @Test public void givenValidData_whenFetchCountWithConditionsInVarargs_thenSucceed() { Condition firstCond = AUTHOR.FIRST_NAME.equalIgnoreCase("Bryan"); From 8e3fcaaa39b1571129629b246cc686b74e1aa710 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Tue, 10 May 2022 11:57:13 +0300 Subject: [PATCH 49/94] remove unnecesary dependency --- docker/docker-spring-boot/mvnw | 310 ----------------------------- docker/docker-spring-boot/mvnw.cmd | 182 ----------------- docker/docker-spring-boot/pom.xml | 5 - 3 files changed, 497 deletions(-) delete mode 100755 docker/docker-spring-boot/mvnw delete mode 100644 docker/docker-spring-boot/mvnw.cmd diff --git a/docker/docker-spring-boot/mvnw b/docker/docker-spring-boot/mvnw deleted file mode 100755 index a16b5431b4..0000000000 --- a/docker/docker-spring-boot/mvnw +++ /dev/null @@ -1,310 +0,0 @@ -#!/bin/sh -# ---------------------------------------------------------------------------- -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# ---------------------------------------------------------------------------- - -# ---------------------------------------------------------------------------- -# Maven Start Up Batch script -# -# Required ENV vars: -# ------------------ -# JAVA_HOME - location of a JDK home dir -# -# Optional ENV vars -# ----------------- -# M2_HOME - location of maven2's installed home dir -# MAVEN_OPTS - parameters passed to the Java VM when running Maven -# e.g. to debug Maven itself, use -# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -# MAVEN_SKIP_RC - flag to disable loading of mavenrc files -# ---------------------------------------------------------------------------- - -if [ -z "$MAVEN_SKIP_RC" ] ; then - - if [ -f /etc/mavenrc ] ; then - . /etc/mavenrc - fi - - if [ -f "$HOME/.mavenrc" ] ; then - . "$HOME/.mavenrc" - fi - -fi - -# OS specific support. $var _must_ be set to either true or false. -cygwin=false; -darwin=false; -mingw=false -case "`uname`" in - CYGWIN*) cygwin=true ;; - MINGW*) mingw=true;; - Darwin*) darwin=true - # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home - # See https://developer.apple.com/library/mac/qa/qa1170/_index.html - if [ -z "$JAVA_HOME" ]; then - if [ -x "/usr/libexec/java_home" ]; then - export JAVA_HOME="`/usr/libexec/java_home`" - else - export JAVA_HOME="/Library/Java/Home" - fi - fi - ;; -esac - -if [ -z "$JAVA_HOME" ] ; then - if [ -r /etc/gentoo-release ] ; then - JAVA_HOME=`java-config --jre-home` - fi -fi - -if [ -z "$M2_HOME" ] ; then - ## resolve links - $0 may be a link to maven's home - PRG="$0" - - # need this for relative symlinks - while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG="`dirname "$PRG"`/$link" - fi - done - - saveddir=`pwd` - - M2_HOME=`dirname "$PRG"`/.. - - # make it fully qualified - M2_HOME=`cd "$M2_HOME" && pwd` - - cd "$saveddir" - # echo Using m2 at $M2_HOME -fi - -# For Cygwin, ensure paths are in UNIX format before anything is touched -if $cygwin ; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --unix "$M2_HOME"` - [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --unix "$JAVA_HOME"` - [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --unix "$CLASSPATH"` -fi - -# For Mingw, ensure paths are in UNIX format before anything is touched -if $mingw ; then - [ -n "$M2_HOME" ] && - M2_HOME="`(cd "$M2_HOME"; pwd)`" - [ -n "$JAVA_HOME" ] && - JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" -fi - -if [ -z "$JAVA_HOME" ]; then - javaExecutable="`which javac`" - if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then - # readlink(1) is not available as standard on Solaris 10. - readLink=`which readlink` - if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then - if $darwin ; then - javaHome="`dirname \"$javaExecutable\"`" - javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" - else - javaExecutable="`readlink -f \"$javaExecutable\"`" - fi - javaHome="`dirname \"$javaExecutable\"`" - javaHome=`expr "$javaHome" : '\(.*\)/bin'` - JAVA_HOME="$javaHome" - export JAVA_HOME - fi - fi -fi - -if [ -z "$JAVACMD" ] ; then - if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - else - JAVACMD="`which java`" - fi -fi - -if [ ! -x "$JAVACMD" ] ; then - echo "Error: JAVA_HOME is not defined correctly." >&2 - echo " We cannot execute $JAVACMD" >&2 - exit 1 -fi - -if [ -z "$JAVA_HOME" ] ; then - echo "Warning: JAVA_HOME environment variable is not set." -fi - -CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher - -# traverses directory structure from process work directory to filesystem root -# first directory with .mvn subdirectory is considered project base directory -find_maven_basedir() { - - if [ -z "$1" ] - then - echo "Path not specified to find_maven_basedir" - return 1 - fi - - basedir="$1" - wdir="$1" - while [ "$wdir" != '/' ] ; do - if [ -d "$wdir"/.mvn ] ; then - basedir=$wdir - break - fi - # workaround for JBEAP-8937 (on Solaris 10/Sparc) - if [ -d "${wdir}" ]; then - wdir=`cd "$wdir/.."; pwd` - fi - # end of workaround - done - echo "${basedir}" -} - -# concatenates all lines of a file -concat_lines() { - if [ -f "$1" ]; then - echo "$(tr -s '\n' ' ' < "$1")" - fi -} - -BASE_DIR=`find_maven_basedir "$(pwd)"` -if [ -z "$BASE_DIR" ]; then - exit 1; -fi - -########################################################################################## -# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central -# This allows using the maven wrapper in projects that prohibit checking in binary data. -########################################################################################## -if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found .mvn/wrapper/maven-wrapper.jar" - fi -else - if [ "$MVNW_VERBOSE" = true ]; then - echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." - fi - if [ -n "$MVNW_REPOURL" ]; then - jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - else - jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - fi - while IFS="=" read key value; do - case "$key" in (wrapperUrl) jarUrl="$value"; break ;; - esac - done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" - if [ "$MVNW_VERBOSE" = true ]; then - echo "Downloading from: $jarUrl" - fi - wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" - if $cygwin; then - wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` - fi - - if command -v wget > /dev/null; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found wget ... using wget" - fi - if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then - wget "$jarUrl" -O "$wrapperJarPath" - else - wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" - fi - elif command -v curl > /dev/null; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found curl ... using curl" - fi - if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then - curl -o "$wrapperJarPath" "$jarUrl" -f - else - curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f - fi - - else - if [ "$MVNW_VERBOSE" = true ]; then - echo "Falling back to using Java to download" - fi - javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" - # For Cygwin, switch paths to Windows format before running javac - if $cygwin; then - javaClass=`cygpath --path --windows "$javaClass"` - fi - if [ -e "$javaClass" ]; then - if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then - if [ "$MVNW_VERBOSE" = true ]; then - echo " - Compiling MavenWrapperDownloader.java ..." - fi - # Compiling the Java class - ("$JAVA_HOME/bin/javac" "$javaClass") - fi - if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then - # Running the downloader - if [ "$MVNW_VERBOSE" = true ]; then - echo " - Running MavenWrapperDownloader.java ..." - fi - ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") - fi - fi - fi -fi -########################################################################################## -# End of extension -########################################################################################## - -export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} -if [ "$MVNW_VERBOSE" = true ]; then - echo $MAVEN_PROJECTBASEDIR -fi -MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" - -# For Cygwin, switch paths to Windows format before running java -if $cygwin; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --path --windows "$M2_HOME"` - [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` - [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --windows "$CLASSPATH"` - [ -n "$MAVEN_PROJECTBASEDIR" ] && - MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` -fi - -# Provide a "standardized" way to retrieve the CLI args that will -# work with both Windows and non-Windows executions. -MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" -export MAVEN_CMD_LINE_ARGS - -WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain - -exec "$JAVACMD" \ - $MAVEN_OPTS \ - -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ - "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ - ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/docker/docker-spring-boot/mvnw.cmd b/docker/docker-spring-boot/mvnw.cmd deleted file mode 100644 index c8d43372c9..0000000000 --- a/docker/docker-spring-boot/mvnw.cmd +++ /dev/null @@ -1,182 +0,0 @@ -@REM ---------------------------------------------------------------------------- -@REM Licensed to the Apache Software Foundation (ASF) under one -@REM or more contributor license agreements. See the NOTICE file -@REM distributed with this work for additional information -@REM regarding copyright ownership. The ASF licenses this file -@REM to you under the Apache License, Version 2.0 (the -@REM "License"); you may not use this file except in compliance -@REM with the License. You may obtain a copy of the License at -@REM -@REM https://www.apache.org/licenses/LICENSE-2.0 -@REM -@REM Unless required by applicable law or agreed to in writing, -@REM software distributed under the License is distributed on an -@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -@REM KIND, either express or implied. See the License for the -@REM specific language governing permissions and limitations -@REM under the License. -@REM ---------------------------------------------------------------------------- - -@REM ---------------------------------------------------------------------------- -@REM Maven Start Up Batch script -@REM -@REM Required ENV vars: -@REM JAVA_HOME - location of a JDK home dir -@REM -@REM Optional ENV vars -@REM M2_HOME - location of maven2's installed home dir -@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands -@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending -@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven -@REM e.g. to debug Maven itself, use -@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files -@REM ---------------------------------------------------------------------------- - -@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' -@echo off -@REM set title of command window -title %0 -@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' -@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% - -@REM set %HOME% to equivalent of $HOME -if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") - -@REM Execute a user defined script before this one -if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre -@REM check for pre script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" -if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" -:skipRcPre - -@setlocal - -set ERROR_CODE=0 - -@REM To isolate internal variables from possible post scripts, we use another setlocal -@setlocal - -@REM ==== START VALIDATION ==== -if not "%JAVA_HOME%" == "" goto OkJHome - -echo. -echo Error: JAVA_HOME not found in your environment. >&2 -echo Please set the JAVA_HOME variable in your environment to match the >&2 -echo location of your Java installation. >&2 -echo. -goto error - -:OkJHome -if exist "%JAVA_HOME%\bin\java.exe" goto init - -echo. -echo Error: JAVA_HOME is set to an invalid directory. >&2 -echo JAVA_HOME = "%JAVA_HOME%" >&2 -echo Please set the JAVA_HOME variable in your environment to match the >&2 -echo location of your Java installation. >&2 -echo. -goto error - -@REM ==== END VALIDATION ==== - -:init - -@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". -@REM Fallback to current working directory if not found. - -set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% -IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir - -set EXEC_DIR=%CD% -set WDIR=%EXEC_DIR% -:findBaseDir -IF EXIST "%WDIR%"\.mvn goto baseDirFound -cd .. -IF "%WDIR%"=="%CD%" goto baseDirNotFound -set WDIR=%CD% -goto findBaseDir - -:baseDirFound -set MAVEN_PROJECTBASEDIR=%WDIR% -cd "%EXEC_DIR%" -goto endDetectBaseDir - -:baseDirNotFound -set MAVEN_PROJECTBASEDIR=%EXEC_DIR% -cd "%EXEC_DIR%" - -:endDetectBaseDir - -IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig - -@setlocal EnableExtensions EnableDelayedExpansion -for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a -@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% - -:endReadAdditionalConfig - -SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" -set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" -set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain - -set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - -FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( - IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B -) - -@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central -@REM This allows using the maven wrapper in projects that prohibit checking in binary data. -if exist %WRAPPER_JAR% ( - if "%MVNW_VERBOSE%" == "true" ( - echo Found %WRAPPER_JAR% - ) -) else ( - if not "%MVNW_REPOURL%" == "" ( - SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - ) - if "%MVNW_VERBOSE%" == "true" ( - echo Couldn't find %WRAPPER_JAR%, downloading it ... - echo Downloading from: %DOWNLOAD_URL% - ) - - powershell -Command "&{"^ - "$webclient = new-object System.Net.WebClient;"^ - "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ - "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ - "}"^ - "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ - "}" - if "%MVNW_VERBOSE%" == "true" ( - echo Finished downloading %WRAPPER_JAR% - ) -) -@REM End of extension - -@REM Provide a "standardized" way to retrieve the CLI args that will -@REM work with both Windows and non-Windows executions. -set MAVEN_CMD_LINE_ARGS=%* - -%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* -if ERRORLEVEL 1 goto error -goto end - -:error -set ERROR_CODE=1 - -:end -@endlocal & set ERROR_CODE=%ERROR_CODE% - -if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost -@REM check for post script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" -if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" -:skipRcPost - -@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' -if "%MAVEN_BATCH_PAUSE%" == "on" pause - -if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% - -exit /B %ERROR_CODE% diff --git a/docker/docker-spring-boot/pom.xml b/docker/docker-spring-boot/pom.xml index fbc891ca2c..c42f7602b4 100644 --- a/docker/docker-spring-boot/pom.xml +++ b/docker/docker-spring-boot/pom.xml @@ -18,11 +18,6 @@ org.springframework.boot spring-boot-starter-web - - com.baeldung.docker - docker-internal-dto - 0.0.1 - org.springframework.boot spring-boot-starter-test From 32f4f7d81dc1ef4ba015cbb556f051bc0ea36291 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 10 May 2022 20:13:31 +0530 Subject: [PATCH 50/94] Update README.md (#12194) --- reactive-systems/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reactive-systems/README.md b/reactive-systems/README.md index b23f4e4dc4..65d4b0a919 100644 --- a/reactive-systems/README.md +++ b/reactive-systems/README.md @@ -1,6 +1,6 @@ ## Reactive Systems in Java -This module contains services for article about reactive systems in Java. Please note that these secrives comprise parts of a full stack application to demonstrate the capabilities of a reactive system. Unless there is an article which extends on this concept, this is probably not a suitable module to add other code. +This module contains services for article about reactive systems in Java. Please note that these services comprise parts of a full stack application to demonstrate the capabilities of a reactive system. Unless there is an article which extends on this concept, this is probably not a suitable module to add other code. ### Relevant Articles From 40214dbc4d513db321d692fb904c646890774918 Mon Sep 17 00:00:00 2001 From: panos-kakos <102670093+panos-kakos@users.noreply.github.com> Date: Tue, 10 May 2022 15:44:57 +0100 Subject: [PATCH 51/94] [JAVA-10581] Changed PII data to placeholders (#12174) * [JAVA-10581] Changed PII data to placeholders * [JAVA-10581] More intuitive properties * [JAVA-10581] More intuitive properties Co-authored-by: panagiotiskakos --- maven-modules/host-maven-repo-example/pom.xml | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/maven-modules/host-maven-repo-example/pom.xml b/maven-modules/host-maven-repo-example/pom.xml index bd58dddeda..ee6929de91 100644 --- a/maven-modules/host-maven-repo-example/pom.xml +++ b/maven-modules/host-maven-repo-example/pom.xml @@ -6,11 +6,11 @@ com.baeldung.maven.plugin host-maven-repo-example 1.0-SNAPSHOT - https://github.com/sgrverma23/host-maven-repo-example.git + https://github.com/${repository-owner}/${repository-name}.git - https://github.com/sgrverma23/host-maven-repo-example.git - scm:git:git@github.com:sgrverma23/host-maven-repo-example.git - scm:git:git@github.com:sgrverma23/host-maven-repo-example.git + https://github.com/${repository-owner}/${repository-name}.git + scm:git:git@github.com:${repository-owner}/${repository-name}.git + scm:git:git@github.com:${repository-owner}/${repository-name}.git @@ -43,13 +43,13 @@ Maven artifacts for ${project.version} true ${project.build.directory} - refs/heads/main + refs/heads/${branch-name} **/* true - host-maven-repo-example - sgrverma23 + ${repository-name} + ${repository-owner} github @@ -89,7 +89,7 @@ PROJECT-REPO-URL - https://github.com/sgrverma23/host-maven-repo-example/main + https://github.com/{repository-owner}/${repository-name}/${branch-name} true always @@ -98,6 +98,10 @@ + + Put-repo-owner + Put-repository-name + Put-branch-name github 8 8 From fe96f9747f884a2b60e0152fcf519cda08adbf4f Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 10 May 2022 22:50:02 +0530 Subject: [PATCH 52/94] Update pom.xml (#12196) --- maven-modules/host-maven-repo-example/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maven-modules/host-maven-repo-example/pom.xml b/maven-modules/host-maven-repo-example/pom.xml index ee6929de91..20528853cd 100644 --- a/maven-modules/host-maven-repo-example/pom.xml +++ b/maven-modules/host-maven-repo-example/pom.xml @@ -89,7 +89,7 @@ PROJECT-REPO-URL - https://github.com/{repository-owner}/${repository-name}/${branch-name} + https://github.com/${repository-owner}/${repository-name}/${branch-name} true always @@ -107,4 +107,4 @@ 8 - \ No newline at end of file + From f24b9af096aa74ca2a6bc816400f90f33174ad6a Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Wed, 11 May 2022 09:57:02 +0200 Subject: [PATCH 53/94] Bael 5481 java httpclient post (#12118) * BAEL-5481: Create new module * BAEL-5481: Sync and async example * BAEL-5481: Concurrent example * BAEL-5481: Concurrent example * BAEL-5481: JSON body example * BAEL-5481: Form data example * BAEL-5481: File upload example * BAEL-5481: PR comments + Jenkins * BAEL-5481: Update aftifact ID * BAEL-5481: Spaces --- .../core-java-httpclient/README.md | 6 + .../core-java-httpclient/pom.xml | 58 +++++++ .../baeldung/httpclient/HttpClientPost.java | 162 ++++++++++++++++++ .../httpclient/HttpClientPostUnitTest.java | 99 +++++++++++ .../httpclient/PostRequestMockServer.java | 61 +++++++ pom.xml | 1 + 6 files changed, 387 insertions(+) create mode 100644 core-java-modules/core-java-httpclient/README.md create mode 100644 core-java-modules/core-java-httpclient/pom.xml create mode 100644 core-java-modules/core-java-httpclient/src/main/java/com/baeldung/httpclient/HttpClientPost.java create mode 100644 core-java-modules/core-java-httpclient/src/test/java/com/baeldung/httpclient/HttpClientPostUnitTest.java create mode 100644 core-java-modules/core-java-httpclient/src/test/java/com/baeldung/httpclient/PostRequestMockServer.java diff --git a/core-java-modules/core-java-httpclient/README.md b/core-java-modules/core-java-httpclient/README.md new file mode 100644 index 0000000000..24ff7d9941 --- /dev/null +++ b/core-java-modules/core-java-httpclient/README.md @@ -0,0 +1,6 @@ +## Java HttpClient + +This module contains articles about Java HttpClient + +### Relevant articles +- TODO diff --git a/core-java-modules/core-java-httpclient/pom.xml b/core-java-modules/core-java-httpclient/pom.xml new file mode 100644 index 0000000000..57b23e96c1 --- /dev/null +++ b/core-java-modules/core-java-httpclient/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + core-java-httpclient + 0.1.0-SNAPSHOT + core-java-httpclient + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../pom.xml + + + + + org.mock-server + mockserver-netty + ${mockserver.version} + + + org.mock-server + mockserver-client-java + ${mockserver.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source.version} + ${maven.compiler.target.version} + + + + + + + 11 + 11 + 3.22.0 + 5.11.2 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-httpclient/src/main/java/com/baeldung/httpclient/HttpClientPost.java b/core-java-modules/core-java-httpclient/src/main/java/com/baeldung/httpclient/HttpClientPost.java new file mode 100644 index 0000000000..d08a7bf183 --- /dev/null +++ b/core-java-modules/core-java-httpclient/src/main/java/com/baeldung/httpclient/HttpClientPost.java @@ -0,0 +1,162 @@ +package com.baeldung.httpclient; + +import java.io.IOException; +import java.net.Authenticator; +import java.net.PasswordAuthentication; +import java.net.URI; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.Base64; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; + +public class HttpClientPost { + + public static HttpResponse sendSynchronousPost(String serviceUrl) throws IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(serviceUrl)) + .POST(HttpRequest.BodyPublishers.noBody()) + .build(); + + HttpResponse response = client + .send(request, HttpResponse.BodyHandlers.ofString()); + + return response; + } + + public static CompletableFuture> sendAsynchronousPost(String serviceUrl) { + HttpClient client = HttpClient.newHttpClient(); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(serviceUrl)) + .POST(HttpRequest.BodyPublishers.noBody()) + .build(); + + CompletableFuture> futureResponse = client + .sendAsync(request, HttpResponse.BodyHandlers.ofString()); + + return futureResponse; + } + + public static List>> sendConcurrentPost(List serviceUrls) { + HttpClient client = HttpClient.newHttpClient(); + + List>> completableFutures = serviceUrls.stream() + .map(URI::create) + .map(HttpRequest::newBuilder) + .map(builder -> builder.POST(HttpRequest.BodyPublishers.noBody())) + .map(HttpRequest.Builder::build) + .map(request -> client.sendAsync(request, HttpResponse.BodyHandlers.ofString())) + .collect(Collectors.toList()); + + return completableFutures; + } + + public static HttpResponse sendPostWithAuthHeader(String serviceUrl) throws IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(serviceUrl)) + .POST(HttpRequest.BodyPublishers.noBody()) + .header("Authorization", "Basic " + Base64.getEncoder() + .encodeToString(("baeldung:123456").getBytes())) + .build(); + + HttpResponse response = client + .send(request, HttpResponse.BodyHandlers.ofString()); + + return response; + } + + public static HttpResponse sendPostWithAuthClient(String serviceUrl) throws IOException, InterruptedException { + HttpClient client = HttpClient.newBuilder() + .authenticator(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication( + "baeldung", + "123456".toCharArray()); + } + }) + .build(); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(serviceUrl)) + .POST(HttpRequest.BodyPublishers.noBody()) + .build(); + + HttpResponse response = client + .send(request, HttpResponse.BodyHandlers.ofString()); + + return response; + } + + public static HttpResponse sendPostWithJsonBody(String serviceUrl) throws IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(serviceUrl)) + .POST(HttpRequest.BodyPublishers.ofString("{\"action\":\"hello\"}")) + .build(); + + HttpResponse response = client + .send(request, HttpResponse.BodyHandlers.ofString()); + + return response; + } + + public static HttpResponse sendPostWithFormData(String serviceUrl) throws IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); + + Map formData = new HashMap<>(); + formData.put("username", "baeldung"); + formData.put("message", "hello"); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(serviceUrl)) + .POST(HttpRequest.BodyPublishers.ofString(getFormDataAsString(formData))) + .build(); + + HttpResponse response = client + .send(request, HttpResponse.BodyHandlers.ofString()); + + return response; + } + + public static HttpResponse sendPostWithFileData(String serviceUrl, Path file) throws IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); + + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(serviceUrl)) + .POST(HttpRequest.BodyPublishers.ofFile(file)) + .build(); + + HttpResponse response = client + .send(request, HttpResponse.BodyHandlers.ofString()); + + return response; + } + + private static String getFormDataAsString(Map formData) { + StringBuilder formBodyBuilder = new StringBuilder(); + for (Map.Entry singleEntry : formData.entrySet()) { + if (formBodyBuilder.length() > 0) { + formBodyBuilder.append("&"); + } + formBodyBuilder.append(URLEncoder.encode(singleEntry.getKey(), StandardCharsets.UTF_8)); + formBodyBuilder.append("="); + formBodyBuilder.append(URLEncoder.encode(singleEntry.getValue(), StandardCharsets.UTF_8)); + } + return formBodyBuilder.toString(); + } + +} diff --git a/core-java-modules/core-java-httpclient/src/test/java/com/baeldung/httpclient/HttpClientPostUnitTest.java b/core-java-modules/core-java-httpclient/src/test/java/com/baeldung/httpclient/HttpClientPostUnitTest.java new file mode 100644 index 0000000000..b43cf08649 --- /dev/null +++ b/core-java-modules/core-java-httpclient/src/test/java/com/baeldung/httpclient/HttpClientPostUnitTest.java @@ -0,0 +1,99 @@ +package com.baeldung.httpclient; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.*; + +class HttpClientPostUnitTest extends PostRequestMockServer { + + @Test + void givenSyncPostRequest_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException { + HttpResponse response = HttpClientPost.sendSynchronousPost(serviceUrl); + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}"); + } + + @Test + void givenAsyncPostRequest_whenServerIsAvailable_thenOkStatusIsReceived() throws ExecutionException, InterruptedException { + CompletableFuture> futureResponse = HttpClientPost.sendAsynchronousPost(serviceUrl); + HttpResponse response = futureResponse.get(); + + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}"); + } + + @Test + void givenConcurrentPostRequests_whenServerIsAvailable_thenOkStatusIsReceived() throws ExecutionException, InterruptedException { + List>> completableFutures = HttpClientPost + .sendConcurrentPost(List.of(serviceUrl, serviceUrl)); + + CompletableFuture>> combinedFutures = CompletableFuture + .allOf(completableFutures.toArray(new CompletableFuture[0])) + .thenApply(future -> + completableFutures.stream() + .map(CompletableFuture::join) + .collect(Collectors.toList())); + + List> responses = combinedFutures.get(); + responses.forEach((response) -> { + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}"); + }); + } + + @Test + void givenPostRequestWithAuthClient_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException { + HttpResponse response = HttpClientPost.sendPostWithAuthClient(serviceUrl); + + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}"); + } + + @Test + void givenPostRequestWithAuthHeader_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException { + HttpResponse response = HttpClientPost.sendPostWithAuthHeader(serviceUrl); + + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}"); + } + + @Test + void givenPostRequestWithJsonBody_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException { + HttpResponse response = HttpClientPost.sendPostWithJsonBody(serviceUrl); + + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}"); + } + + @Test + void givenPostRequestWithFormData_whenServerIsAvailable_thenOkStatusIsReceived() throws IOException, InterruptedException { + HttpResponse response = HttpClientPost.sendPostWithFormData(serviceUrl); + + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}"); + } + + @Test + void givenPostRequestWithFileData_whenServerIsAvailable_thenOkStatusIsReceived(@TempDir Path tempDir) throws IOException, InterruptedException { + Path file = tempDir.resolve("temp.txt"); + List lines = Arrays.asList("1", "2", "3"); + Files.write(file, lines); + + HttpResponse response = HttpClientPost.sendPostWithFileData(serviceUrl, file); + + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.body()).isEqualTo("{\"message\":\"ok\"}"); + } + +} diff --git a/core-java-modules/core-java-httpclient/src/test/java/com/baeldung/httpclient/PostRequestMockServer.java b/core-java-modules/core-java-httpclient/src/test/java/com/baeldung/httpclient/PostRequestMockServer.java new file mode 100644 index 0000000000..fa594897a3 --- /dev/null +++ b/core-java-modules/core-java-httpclient/src/test/java/com/baeldung/httpclient/PostRequestMockServer.java @@ -0,0 +1,61 @@ +package com.baeldung.httpclient; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.mockserver.client.MockServerClient; +import org.mockserver.integration.ClientAndServer; +import org.mockserver.model.HttpStatusCode; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.URISyntaxException; + +import static org.mockserver.integration.ClientAndServer.startClientAndServer; +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +public abstract class PostRequestMockServer { + + public static ClientAndServer mockServer; + public static String serviceUrl; + + private static int serverPort; + + public static final String SERVER_ADDRESS = "127.0.0.1"; + public static final String PATH = "/test1"; + public static final String METHOD = "POST"; + + @BeforeAll + static void startServer() throws IOException, URISyntaxException { + serverPort = getFreePort(); + serviceUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH; + mockServer = startClientAndServer(serverPort); + mockBasicPostRequest(); + } + + @AfterAll + static void stopServer() { + mockServer.stop(); + } + + private static void mockBasicPostRequest() { + new MockServerClient(SERVER_ADDRESS, serverPort) + .when( + request() + .withPath(PATH) + .withMethod(METHOD) + ) + .respond( + response() + .withStatusCode(HttpStatusCode.OK_200.code()) + .withBody("{\"message\":\"ok\"}") + ); + } + + private static int getFreePort () throws IOException { + try (ServerSocket serverSocket = new ServerSocket(0)) { + return serverSocket.getLocalPort(); + } + } + +} diff --git a/pom.xml b/pom.xml index f06c75b3be..5b64baac7f 100644 --- a/pom.xml +++ b/pom.xml @@ -1318,6 +1318,7 @@ core-java-modules/core-java-networking-3 core-java-modules/multimodulemavenproject core-java-modules/core-java-strings + core-java-modules/core-java-httpclient ddd-modules docker apache-httpclient-2 From f5380f98f6f73caaeb616c620d36ba72ebdcfdda Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 11 May 2022 12:52:41 +0300 Subject: [PATCH 54/94] BAEL-5420 fix equals method --- .../main/java/com/baeldung/employee/EmployeeVO.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/employee/EmployeeVO.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/employee/EmployeeVO.java index 7a1775f79d..3d2c61aa3d 100644 --- a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/employee/EmployeeVO.java +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/employee/EmployeeVO.java @@ -28,9 +28,15 @@ public class EmployeeVO { @Override public boolean equals(Object obj) { - return Objects.equals(firstName, this.firstName) - && Objects.equals(lastName, this.lastName) - && Objects.equals(startDate, this.startDate); + + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + + EmployeeVO emp = (EmployeeVO) obj; + + return Objects.equals(firstName, emp.firstName) + && Objects.equals(lastName, emp.lastName) + && Objects.equals(startDate, emp.startDate); } @Override From 1b1a411770cf0a899e42d7094f120045834a9e4e Mon Sep 17 00:00:00 2001 From: panagiotiskakos Date: Wed, 11 May 2022 13:53:42 +0300 Subject: [PATCH 55/94] [JAVA-8688] Added tomcat dependecy --- spring-boot-modules/spring-boot-deployment/pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spring-boot-modules/spring-boot-deployment/pom.xml b/spring-boot-modules/spring-boot-deployment/pom.xml index 7a9c2096f1..47b1a0f0d5 100644 --- a/spring-boot-modules/spring-boot-deployment/pom.xml +++ b/spring-boot-modules/spring-boot-deployment/pom.xml @@ -41,6 +41,11 @@ org.springframework.boot spring-boot-starter-actuator + + org.springframework.boot + spring-boot-starter-tomcat + provided + com.h2database h2 From 39e01903c24b3005e88bdd22c7133120dbd1c6c1 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Wed, 11 May 2022 12:28:14 +0100 Subject: [PATCH 56/94] [JAVA-11122] Logging clean up --- .../differences/rdd/ActionsUnitTest.java | 25 ++++++---- .../differences/rdd/DataFrameUnitTest.java | 22 +++++---- .../differences/rdd/DatasetUnitTest.java | 46 +++++++++++++------ .../baeldung/graphql/GraphQLMockServer.java | 19 ++++---- 4 files changed, 72 insertions(+), 40 deletions(-) diff --git a/apache-spark/src/test/java/com/baeldung/differences/rdd/ActionsUnitTest.java b/apache-spark/src/test/java/com/baeldung/differences/rdd/ActionsUnitTest.java index a3e1811e6f..6d6a1394c5 100644 --- a/apache-spark/src/test/java/com/baeldung/differences/rdd/ActionsUnitTest.java +++ b/apache-spark/src/test/java/com/baeldung/differences/rdd/ActionsUnitTest.java @@ -12,13 +12,18 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import scala.Tuple2; public class ActionsUnitTest { + + public static final Logger LOG = LoggerFactory.getLogger(ActionsUnitTest.class); + private static JavaRDD tourists; private static JavaSparkContext sc; public static final String COMMA_DELIMITER = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"; - + @BeforeClass public static void init() { SparkConf conf = new SparkConf().setAppName("reduce") @@ -26,7 +31,7 @@ public class ActionsUnitTest { sc = new JavaSparkContext(conf); tourists = sc.textFile("data/Tourist.csv").filter(line -> !line.startsWith("Region")); } - + @AfterClass public static void cleanup() { sc.close(); @@ -40,11 +45,11 @@ public class ActionsUnitTest { }) .distinct(); Long numberOfCountries = countries.count(); - System.out.println("Count: " + numberOfCountries); - + LOG.debug("Count: {}", numberOfCountries); + assertEquals(Long.valueOf(220), numberOfCountries); } - + @Test public void whenReduceByKeySum_thenTotalValuePerKey() { JavaRDD touristsExpenditure = tourists.filter(line -> line.split(COMMA_DELIMITER)[3].contains("expenditure")); @@ -53,10 +58,12 @@ public class ActionsUnitTest { String[] columns = line.split(COMMA_DELIMITER); return new Tuple2<>(columns[1], Double.valueOf(columns[6])); }); - List> totalByCountry = expenditurePairRdd.reduceByKey((x, y) -> x + y) - .collect(); - System.out.println("Total per Country: " + totalByCountry); - + List> totalByCountry = expenditurePairRdd + .reduceByKey(Double::sum) + .collect(); + + LOG.debug("Total per Country: {}", totalByCountry); + for(Tuple2 tuple : totalByCountry) { if (tuple._1.equals("Mexico")) { assertEquals(Double.valueOf(99164), tuple._2); diff --git a/apache-spark/src/test/java/com/baeldung/differences/rdd/DataFrameUnitTest.java b/apache-spark/src/test/java/com/baeldung/differences/rdd/DataFrameUnitTest.java index f294e5bc66..621e589fb6 100644 --- a/apache-spark/src/test/java/com/baeldung/differences/rdd/DataFrameUnitTest.java +++ b/apache-spark/src/test/java/com/baeldung/differences/rdd/DataFrameUnitTest.java @@ -39,8 +39,10 @@ public class DataFrameUnitTest { @Test public void whenSelectSpecificColumns_thenColumnsFiltered() { Dataset selectedData = data.select(col("country"), col("year"), col("value")); - selectedData.show(); - + + // uncomment to see table + // selectedData.show(); + List resultList = Arrays.asList(selectedData.columns()); assertTrue(resultList.contains("country")); assertTrue(resultList.contains("year")); @@ -52,22 +54,26 @@ public class DataFrameUnitTest { @Test public void whenFilteringByCountry_thenCountryRecordsSelected() { Dataset filteredData = data.filter(col("country").equalTo("Mexico")); - filteredData.show(); - + + // uncomment to see table + // filteredData.show(); + filteredData.foreach(record -> { assertEquals("Mexico", record.get(1)); }); - + } @Test public void whenGroupCountByCountry_thenContryTotalRecords() { Dataset recordsPerCountry = data.groupBy(col("country")) .count(); - recordsPerCountry.show(); - + + // uncomment to see table + // recordsPerCountry.show(); + Dataset filteredData = recordsPerCountry.filter(col("country").equalTo("Sweden")); - assertEquals(new Long(12), filteredData.first() + assertEquals(12L, filteredData.first() .get(1)); } diff --git a/apache-spark/src/test/java/com/baeldung/differences/rdd/DatasetUnitTest.java b/apache-spark/src/test/java/com/baeldung/differences/rdd/DatasetUnitTest.java index 1d83505812..4fde933a3b 100644 --- a/apache-spark/src/test/java/com/baeldung/differences/rdd/DatasetUnitTest.java +++ b/apache-spark/src/test/java/com/baeldung/differences/rdd/DatasetUnitTest.java @@ -3,6 +3,7 @@ package com.baeldung.differences.rdd; import static org.apache.spark.sql.functions.col; import static org.apache.spark.sql.functions.sum; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import org.apache.spark.api.java.function.FilterFunction; import org.apache.spark.sql.DataFrameReader; @@ -29,8 +30,8 @@ public class DatasetUnitTest { DataFrameReader dataFrameReader = session.read(); Dataset data = dataFrameReader.option("header", "true") .csv("data/Tourist.csv"); - Dataset responseWithSelectedColumns = data.select(col("region"), - col("country"), col("year"), col("series"), col("value").cast("double"), + Dataset responseWithSelectedColumns = data.select(col("region"), + col("country"), col("year"), col("series"), col("value").cast("double"), col("footnotes"), col("source")); typedDataset = responseWithSelectedColumns.as(Encoders.bean(TouristData.class)); } @@ -45,7 +46,9 @@ public class DatasetUnitTest { Dataset selectedData = typedDataset .filter((FilterFunction) record -> record.getCountry() .equals("Norway")); - selectedData.show(); + + // uncomment to see output + // selectedData.show(); selectedData.foreach(record -> { assertEquals("Norway", record.getCountry()); @@ -56,28 +59,41 @@ public class DatasetUnitTest { public void whenGroupCountByCountry_thenContryTotalRecords() { Dataset countriesCount = typedDataset.groupBy(typedDataset.col("country")) .count(); - countriesCount.show(); - assertEquals(Long.valueOf(220), Long.valueOf(countriesCount.count())); + // uncomment to see output + // countriesCount.show(); + + assertEquals(220, countriesCount.count()); } @Test public void whenFilteredByPropertyRange_thenRetreiveValidRecords() { // Filter records with existing data for years between 2010 and 2017 - typedDataset.filter((FilterFunction) record -> record.getYear() != null - && (Long.valueOf(record.getYear()) > 2010 && Long.valueOf(record.getYear()) < 2017)) - .show(); + Dataset filteredData = typedDataset.filter( + (FilterFunction) record -> record.getYear() != null + && (Long.parseLong(record.getYear()) > 2010 && Long.parseLong(record.getYear()) < 2017)); + + // uncomment to see output + // filteredData.show(); + + assertEquals(394, filteredData.count()); + filteredData.foreach(record -> { + assertTrue(Integer.parseInt(record.getYear()) > 2010 && Integer.parseInt(record.getYear()) < 2017); + }); } - + @Test public void whenSumValue_thenRetreiveTotalValue() { // Total tourist expenditure by country - typedDataset.filter((FilterFunction) record -> record.getValue() != null - && record.getSeries() - .contains("expenditure")) - .groupBy("country") - .agg(sum("value")) - .show(); + Dataset filteredData = typedDataset.filter((FilterFunction) record -> record.getValue() != null + && record.getSeries().contains("expenditure")) + .groupBy("country") + .agg(sum("value")); + + // uncomment to see output + // filteredData.show(); + + assertEquals(212, filteredData.count()); } } diff --git a/graphql/graphql-java/src/test/java/com/baeldung/graphql/GraphQLMockServer.java b/graphql/graphql-java/src/test/java/com/baeldung/graphql/GraphQLMockServer.java index fb5a789428..e72f6a38b9 100644 --- a/graphql/graphql-java/src/test/java/com/baeldung/graphql/GraphQLMockServer.java +++ b/graphql/graphql-java/src/test/java/com/baeldung/graphql/GraphQLMockServer.java @@ -3,12 +3,13 @@ package com.baeldung.graphql; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.mockserver.client.MockServerClient; +import org.mockserver.configuration.Configuration; import org.mockserver.integration.ClientAndServer; import org.mockserver.model.HttpStatusCode; +import org.slf4j.event.Level; import java.io.IOException; import java.net.ServerSocket; -import java.net.URISyntaxException; import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.matchers.Times.exactly; @@ -17,20 +18,22 @@ import static org.mockserver.model.HttpResponse.response; public class GraphQLMockServer { - public static ClientAndServer mockServer; + private static final String SERVER_ADDRESS = "127.0.0.1"; + private static final String PATH = "/graphql"; + public static String serviceUrl; + private static ClientAndServer mockServer; private static int serverPort; - public static final String SERVER_ADDRESS = "127.0.0.1"; - public static final String HTTP_GET_POST = "GET"; - public static final String PATH = "/graphql"; - @BeforeAll - static void startServer() throws IOException, URISyntaxException { + static void startServer() throws IOException { serverPort = getFreePort(); serviceUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH; - mockServer = startClientAndServer(serverPort); + + Configuration config = Configuration.configuration().logLevel(Level.WARN); + mockServer = startClientAndServer(config, serverPort); + mockAllBooksTitleRequest(); mockAllBooksTitleAuthorRequest(); } From 3aacf5a5a807c071260ab52c436d688bf8b4fb89 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Wed, 11 May 2022 12:59:11 +0100 Subject: [PATCH 57/94] [JAVA-11122] Allow multiple contexts to prevent failing tests --- .../java/com/baeldung/differences/rdd/ActionsUnitTest.java | 7 +++++-- .../baeldung/differences/rdd/TransformationsUnitTest.java | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apache-spark/src/test/java/com/baeldung/differences/rdd/ActionsUnitTest.java b/apache-spark/src/test/java/com/baeldung/differences/rdd/ActionsUnitTest.java index 6d6a1394c5..b1083021a9 100644 --- a/apache-spark/src/test/java/com/baeldung/differences/rdd/ActionsUnitTest.java +++ b/apache-spark/src/test/java/com/baeldung/differences/rdd/ActionsUnitTest.java @@ -26,8 +26,11 @@ public class ActionsUnitTest { @BeforeClass public static void init() { - SparkConf conf = new SparkConf().setAppName("reduce") - .setMaster("local[*]"); + SparkConf conf = new SparkConf() + .setAppName("reduce") + .setMaster("local[*]") + .set("spark.driver.allowMultipleContexts", "true"); + sc = new JavaSparkContext(conf); tourists = sc.textFile("data/Tourist.csv").filter(line -> !line.startsWith("Region")); } diff --git a/apache-spark/src/test/java/com/baeldung/differences/rdd/TransformationsUnitTest.java b/apache-spark/src/test/java/com/baeldung/differences/rdd/TransformationsUnitTest.java index 01e7d3adfc..6de407f3b4 100644 --- a/apache-spark/src/test/java/com/baeldung/differences/rdd/TransformationsUnitTest.java +++ b/apache-spark/src/test/java/com/baeldung/differences/rdd/TransformationsUnitTest.java @@ -23,8 +23,11 @@ public class TransformationsUnitTest { @BeforeClass public static void init() { - SparkConf conf = new SparkConf().setAppName("uppercaseCountries") - .setMaster("local[*]"); + SparkConf conf = new SparkConf() + .setAppName("uppercaseCountries") + .setMaster("local[*]") + .set("spark.driver.allowMultipleContexts", "true"); + sc = new JavaSparkContext(conf); tourists = sc.textFile("data/Tourist.csv") .filter(line -> !line.startsWith("Region")); //filter header row From 9727e555ce3f24466aaccd90fa135f34b350552d Mon Sep 17 00:00:00 2001 From: Timothy Kruger Date: Thu, 12 May 2022 13:52:54 +0200 Subject: [PATCH 58/94] BAEL-5563 Reversing a number (#12153) * BAEL-5563 Reverse a number initial commit * BAEL-5563 Final improvements before PR * BAEL-5563 Reverse a number - Draft improvements * BAEL-5563 Reverse a number - Recursive method improvements * BAEL-5563 Reverse a number - Recursive method improvements * BAEL-5563 Reverse a number - Remove unused imports Co-authored-by: Timothy Kruger - Lenny Co-authored-by: claud <180181@virtualwindow.co.za> --- .../baeldung/reversenumber/ReverseNumber.java | 45 +++++++++++++++++++ .../reversenumber/ReverseNumberUnitTest.java | 34 ++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 java-numbers-4/src/main/java/com/baeldung/reversenumber/ReverseNumber.java create mode 100644 java-numbers-4/src/test/java/com/baeldung/reversenumber/ReverseNumberUnitTest.java diff --git a/java-numbers-4/src/main/java/com/baeldung/reversenumber/ReverseNumber.java b/java-numbers-4/src/main/java/com/baeldung/reversenumber/ReverseNumber.java new file mode 100644 index 0000000000..04865a8d52 --- /dev/null +++ b/java-numbers-4/src/main/java/com/baeldung/reversenumber/ReverseNumber.java @@ -0,0 +1,45 @@ +package com.baeldung.reversenumber; + +public class ReverseNumber { + + public static int reverseNumberWhileLoop(int number) { + int reversedNumber = 0; + int numberToReverse = Math.abs(number); + + while (numberToReverse > 0) { + int mod = numberToReverse % 10; + reversedNumber = reversedNumber * 10 + mod; + numberToReverse /= 10; + } + + return number < 0 ? reversedNumber * -1 : reversedNumber; + } + + public static int reverseNumberForLoop(int number) { + int reversedNumber = 0; + int numberToReverse = Math.abs(number); + + for (; numberToReverse > 0; numberToReverse /= 10) { + int mod = numberToReverse % 10; + reversedNumber = reversedNumber * 10 + mod; + } + + return number < 0 ? reversedNumber * -1 : reversedNumber; + } + + public static int reverseNumberRecWrapper(int number) { + int output = reverseNumberRec(Math.abs(number), 0); + return number < 0 ? output * -1 : output; + } + private static int reverseNumberRec(int numberToReverse, int recursiveReversedNumber) { + + if (numberToReverse > 0) { + int mod = numberToReverse % 10; + recursiveReversedNumber = recursiveReversedNumber * 10 + mod; + numberToReverse /= 10; + return reverseNumberRec(numberToReverse, recursiveReversedNumber); + } + + return recursiveReversedNumber; + } +} diff --git a/java-numbers-4/src/test/java/com/baeldung/reversenumber/ReverseNumberUnitTest.java b/java-numbers-4/src/test/java/com/baeldung/reversenumber/ReverseNumberUnitTest.java new file mode 100644 index 0000000000..2c55bcc539 --- /dev/null +++ b/java-numbers-4/src/test/java/com/baeldung/reversenumber/ReverseNumberUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.reversenumber; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ReverseNumberUnitTest { + + private static final int ORIGINAL_NUMBER = 123456789; + private static final int REVERSED_NUMBER = 987654321; + + @Test + void whenReverseNumberWhileLoop_thenOriginalEqualToReverse() { + Assertions.assertThat(ReverseNumber.reverseNumberWhileLoop(ORIGINAL_NUMBER)).isEqualTo(REVERSED_NUMBER); + } + + @Test + void whenReverseNumberForLoop_thenOriginalEqualToReverse() { + Assertions.assertThat(ReverseNumber.reverseNumberForLoop(ORIGINAL_NUMBER)).isEqualTo(REVERSED_NUMBER); + } + + @Test + void whenReverseNumberRec_thenOriginalEqualToReverse() { + Assertions.assertThat(ReverseNumber.reverseNumberRecWrapper(ORIGINAL_NUMBER)).isEqualTo(REVERSED_NUMBER); + } + + @Test + void whenReverseNegativeNumber_thenNumberShouldReverse() { + Assertions.assertThat(ReverseNumber.reverseNumberWhileLoop(ORIGINAL_NUMBER * -1)).isEqualTo(REVERSED_NUMBER * -1); + Assertions.assertThat(ReverseNumber.reverseNumberForLoop(ORIGINAL_NUMBER * -1)).isEqualTo(REVERSED_NUMBER * -1); + Assertions.assertThat(ReverseNumber.reverseNumberRecWrapper(ORIGINAL_NUMBER * -1)).isEqualTo(REVERSED_NUMBER * -1); + } +} \ No newline at end of file From 870dc43c9daaec2ab38ac5976a5f6e11914046db Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Thu, 12 May 2022 18:59:40 +0100 Subject: [PATCH 59/94] [JAVA-11876] Split spring-boot-mongodb module --- persistence-modules/pom.xml | 1 + .../.gitignore | 2 ++ .../README.md | 4 +++ .../spring-boot-persistence-mongodb-2/pom.xml | 35 +++++++++++++++++++ .../SpringBootPersistenceApplication.java | 13 +++++++ .../java/com/baeldung/logging/model/Book.java | 0 .../src/main/resources/application.properties | 1 + .../com/baeldung/logging/GroupByAuthor.java | 0 .../com/baeldung/logging/LoggingUnitTest.java | 4 +-- .../src/test/resources/application.properties | 1 + .../src/test/resources/logback-test.xml | 12 +++++++ .../spring-boot-persistence-mongodb/README.md | 2 +- 12 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/.gitignore create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/README.md create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/pom.xml create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/SpringBootPersistenceApplication.java rename persistence-modules/{spring-boot-persistence-mongodb => spring-boot-persistence-mongodb-2}/src/main/java/com/baeldung/logging/model/Book.java (100%) create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/application.properties rename persistence-modules/{spring-boot-persistence-mongodb => spring-boot-persistence-mongodb-2}/src/test/java/com/baeldung/logging/GroupByAuthor.java (100%) rename persistence-modules/{spring-boot-persistence-mongodb => spring-boot-persistence-mongodb-2}/src/test/java/com/baeldung/logging/LoggingUnitTest.java (97%) create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/test/resources/application.properties create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/test/resources/logback-test.xml diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 988bb46575..7be71ad215 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -60,6 +60,7 @@ spring-boot-persistence spring-boot-persistence-h2 spring-boot-persistence-mongodb + spring-boot-persistence-mongodb-2 spring-data-arangodb spring-data-cassandra spring-data-cassandra-test diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/.gitignore b/persistence-modules/spring-boot-persistence-mongodb-2/.gitignore new file mode 100644 index 0000000000..2d513a0101 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/.gitignore @@ -0,0 +1,2 @@ +/.idea/ +/target/ diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/README.md b/persistence-modules/spring-boot-persistence-mongodb-2/README.md new file mode 100644 index 0000000000..9169e09813 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/README.md @@ -0,0 +1,4 @@ +# Relevant Articles + +- [Logging MongoDB Queries with Spring Boot](https://www.baeldung.com/spring-boot-mongodb-logging) +- More articles: [[<--prev]](../spring-boot-persistence-mongodb) \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml b/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml new file mode 100644 index 0000000000..a6ac4987a1 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + spring-boot-persistence-mongodb-2 + spring-boot-persistence-mongodb-2 + war + This is simple boot application for Spring boot persistence mongodb test + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + ${embed.mongo.version} + test + + + + + 3.2.6 + + + diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/SpringBootPersistenceApplication.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/SpringBootPersistenceApplication.java new file mode 100644 index 0000000000..2dff3f37df --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/SpringBootPersistenceApplication.java @@ -0,0 +1,13 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootPersistenceApplication { + + public static void main(String ... args) { + SpringApplication.run(SpringBootPersistenceApplication.class, args); + } + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/logging/model/Book.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/logging/model/Book.java similarity index 100% rename from persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/logging/model/Book.java rename to persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/logging/model/Book.java diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/application.properties b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/application.properties new file mode 100644 index 0000000000..9dbc261896 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=spring-boot-persistence-mongodb-2 diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/GroupByAuthor.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/logging/GroupByAuthor.java similarity index 100% rename from persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/GroupByAuthor.java rename to persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/logging/GroupByAuthor.java diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/LoggingUnitTest.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/logging/LoggingUnitTest.java similarity index 97% rename from persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/LoggingUnitTest.java rename to persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/logging/LoggingUnitTest.java index 1c59dcb5ac..00def53566 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/logging/LoggingUnitTest.java +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/logging/LoggingUnitTest.java @@ -35,7 +35,7 @@ import de.flapdoodle.embed.mongo.distribution.Version; import de.flapdoodle.embed.process.runtime.Network; @SpringBootTest -@TestPropertySource(properties = { "logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG" }) +@TestPropertySource(properties = { "logging.level.org.springframework.data.mongodb.core.MongoTemplate=INFO" }) public class LoggingUnitTest { private static final String CONNECTION_STRING = "mongodb://%s:%d"; @@ -51,7 +51,7 @@ public class LoggingUnitTest { @BeforeEach void setup() throws Exception { String ip = "localhost"; - int port = SocketUtils.findAvailableTcpPort(); + int port = Network.freeServerPort(Network.getLocalHost()); ImmutableMongodConfig mongodConfig = MongodConfig.builder() .version(Version.Main.PRODUCTION) diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/test/resources/application.properties b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/resources/application.properties new file mode 100644 index 0000000000..a5b5fb9804 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/resources/application.properties @@ -0,0 +1 @@ +spring.mongodb.embedded.version=4.4.9 \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/test/resources/logback-test.xml b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..8d4771e308 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/resources/logback-test.xml @@ -0,0 +1,12 @@ + + + + + [%d{ISO8601}]-[%thread] %-5level %logger - %msg%n + + + + + + + \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-mongodb/README.md b/persistence-modules/spring-boot-persistence-mongodb/README.md index 91dd8718e1..8e9399f076 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/README.md +++ b/persistence-modules/spring-boot-persistence-mongodb/README.md @@ -7,4 +7,4 @@ - [ZonedDateTime with Spring Data MongoDB](https://www.baeldung.com/spring-data-mongodb-zoneddatetime) - [A Guide to @DBRef in MongoDB](https://www.baeldung.com/spring-mongodb-dbref-annotation) - [Import Data to MongoDB From JSON File Using Java](https://www.baeldung.com/java-import-json-mongodb) -- [Logging MongoDB Queries with Spring Boot](https://www.baeldung.com/spring-boot-mongodb-logging) +- More articles: [[next-->]](../spring-boot-persistence-mongodb-2) From efd9a398a13c04dd496f0bef00021304c846e7de Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Thu, 12 May 2022 19:36:01 +0100 Subject: [PATCH 60/94] [JAVA-11979] Rename Keycloak server Integration test to Live test --- ...rationTest.java => KeycloakSoapLiveTest.java} | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) rename spring-boot-modules/spring-boot-keycloak/src/test/java/com/baeldung/keycloaksoap/{KeycloakSoapIntegrationTest.java => KeycloakSoapLiveTest.java} (96%) diff --git a/spring-boot-modules/spring-boot-keycloak/src/test/java/com/baeldung/keycloaksoap/KeycloakSoapIntegrationTest.java b/spring-boot-modules/spring-boot-keycloak/src/test/java/com/baeldung/keycloaksoap/KeycloakSoapLiveTest.java similarity index 96% rename from spring-boot-modules/spring-boot-keycloak/src/test/java/com/baeldung/keycloaksoap/KeycloakSoapIntegrationTest.java rename to spring-boot-modules/spring-boot-keycloak/src/test/java/com/baeldung/keycloaksoap/KeycloakSoapLiveTest.java index e0de897044..0327915399 100644 --- a/spring-boot-modules/spring-boot-keycloak/src/test/java/com/baeldung/keycloaksoap/KeycloakSoapIntegrationTest.java +++ b/spring-boot-modules/spring-boot-keycloak/src/test/java/com/baeldung/keycloaksoap/KeycloakSoapLiveTest.java @@ -26,29 +26,35 @@ import java.util.Objects; import static org.assertj.core.api.Assertions.assertThat; /** - * The class contains Live/Integration tests. + * The class contains Live tests. * These tests expect that the Keycloak server is up and running on port 8080. - * The tests may fail without a Keycloak server. */ -@DisplayName("Keycloak SOAP Webservice Unit Tests") +@DisplayName("Keycloak SOAP Webservice Live Tests") @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") @AutoConfigureMockMvc -class KeycloakSoapIntegrationTest { +class KeycloakSoapLiveTest { + + private static final Logger logger = LoggerFactory.getLogger(KeycloakSoapLiveTest.class); - private static final Logger logger = LoggerFactory.getLogger(KeycloakSoapIntegrationTest.class); @LocalServerPort private int port; + @Autowired private TestRestTemplate restTemplate; + @Autowired private ObjectMapper objectMapper; + @Value("${grant.type}") private String grantType; + @Value("${client.id}") private String clientId; + @Value("${client.secret}") private String clientSecret; + @Value("${url}") private String keycloakUrl; From 557dba376b6af21318c429375dd3a16b9656d51d Mon Sep 17 00:00:00 2001 From: freelansam <79205526+freelansam@users.noreply.github.com> Date: Fri, 13 May 2022 16:14:49 +0530 Subject: [PATCH 61/94] JAVA-11765: Dissolve spring-boot-rest-2 and distribute its articles (#12195) * JAVA-11765: Dissolve spring-boot-rest-2 and distribute its articles spring-boot-modules * JAVA-11765: removed module from main pom --- pom.xml | 2 - spring-boot-rest-2/README.md | 5 -- spring-boot-rest-2/pom.xml | 57 ------------------- .../src/main/resources/application.properties | 2 - .../spring-rest-http-2/README.md | 3 + .../endpoint/SpringBootRestApplication.java | 0 .../endpoint/controller/HelloController.java | 0 .../AnnotationDrivenEndpointsListener.java | 0 .../endpoint/listener/EndpointsListener.java | 0 .../endpoint/swagger/SpringFoxConfig.java | 0 .../java/com/baeldung/putvspost/Address.java | 0 .../baeldung/putvspost/AddressController.java | 0 .../baeldung/putvspost/AddressRepository.java | 0 .../putvspost/PutVsPostApplication.java | 0 .../UnsupportedMediaTypeApplication.java | 0 .../baeldung/unsupportedmediatype/User.java | 0 .../unsupportedmediatype/UserController.java | 0 .../src/main/resources/application.properties | 3 +- .../ApplicationUnitTest.java | 0 .../src/test/resources/application.properties | 3 +- 20 files changed, 7 insertions(+), 68 deletions(-) delete mode 100644 spring-boot-rest-2/README.md delete mode 100644 spring-boot-rest-2/pom.xml delete mode 100644 spring-boot-rest-2/src/main/resources/application.properties rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/endpoint/SpringBootRestApplication.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/endpoint/controller/HelloController.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/endpoint/listener/AnnotationDrivenEndpointsListener.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/endpoint/listener/EndpointsListener.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/endpoint/swagger/SpringFoxConfig.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/putvspost/Address.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/putvspost/AddressController.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/putvspost/AddressRepository.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/putvspost/PutVsPostApplication.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/unsupportedmediatype/User.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/main/java/com/baeldung/unsupportedmediatype/UserController.java (100%) rename {spring-boot-rest-2 => spring-web-modules/spring-rest-http-2}/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java (100%) diff --git a/pom.xml b/pom.xml index 5b64baac7f..106bb7516a 100644 --- a/pom.xml +++ b/pom.xml @@ -620,7 +620,6 @@ spring-bom spring-boot-modules spring-boot-rest - spring-boot-rest-2 spring-caching spring-caching-2 @@ -1085,7 +1084,6 @@ spring-bom spring-boot-modules spring-boot-rest - spring-boot-rest-2 spring-caching spring-caching-2 diff --git a/spring-boot-rest-2/README.md b/spring-boot-rest-2/README.md deleted file mode 100644 index 985aa97a86..0000000000 --- a/spring-boot-rest-2/README.md +++ /dev/null @@ -1,5 +0,0 @@ -### Relevant Article: - -- [Get All Endpoints in Spring Boot](https://www.baeldung.com/spring-boot-get-all-endpoints) -- [HTTP PUT vs. POST in REST API](https://www.baeldung.com/rest-http-put-vs-post) -- [415 Unsupported MediaType in Spring Application](https://www.baeldung.com/spring-415-unsupported-mediatype) diff --git a/spring-boot-rest-2/pom.xml b/spring-boot-rest-2/pom.xml deleted file mode 100644 index b75e93577a..0000000000 --- a/spring-boot-rest-2/pom.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - 4.0.0 - com.baeldung.web - spring-boot-rest-2 - spring-boot-rest-2 - war - Spring Boot Rest Module - - - com.baeldung - parent-boot-2 - 0.0.1-SNAPSHOT - ../parent-boot-2 - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-actuator - - - io.springfox - springfox-boot-starter - ${springfox.version} - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.h2database - h2 - runtime - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - 3.0.0 - - - \ No newline at end of file diff --git a/spring-boot-rest-2/src/main/resources/application.properties b/spring-boot-rest-2/src/main/resources/application.properties deleted file mode 100644 index 5046c9660f..0000000000 --- a/spring-boot-rest-2/src/main/resources/application.properties +++ /dev/null @@ -1,2 +0,0 @@ - -management.endpoints.web.exposure.include=mappings diff --git a/spring-web-modules/spring-rest-http-2/README.md b/spring-web-modules/spring-rest-http-2/README.md index bb9175db8c..2c1b1f76f7 100644 --- a/spring-web-modules/spring-rest-http-2/README.md +++ b/spring-web-modules/spring-rest-http-2/README.md @@ -11,4 +11,7 @@ The "REST With Spring 2" Classes: http://bit.ly/restwithspring - [Setting a Request Timeout for a Spring REST API](https://www.baeldung.com/spring-rest-timeout) - [Long Polling in Spring MVC](https://www.baeldung.com/spring-mvc-long-polling) - [Guide to UriComponentsBuilder in Spring](https://www.baeldung.com/spring-uricomponentsbuilder) +- [Get All Endpoints in Spring Boot](https://www.baeldung.com/spring-boot-get-all-endpoints) +- [HTTP PUT vs. POST in REST API](https://www.baeldung.com/rest-http-put-vs-post) +- [415 Unsupported MediaType in Spring Application](https://www.baeldung.com/spring-415-unsupported-mediatype) - More articles: [[<-- prev]](../spring-rest-http) diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/endpoint/SpringBootRestApplication.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/SpringBootRestApplication.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/endpoint/SpringBootRestApplication.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/SpringBootRestApplication.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/endpoint/controller/HelloController.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/controller/HelloController.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/endpoint/controller/HelloController.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/controller/HelloController.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/endpoint/listener/AnnotationDrivenEndpointsListener.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/listener/AnnotationDrivenEndpointsListener.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/endpoint/listener/AnnotationDrivenEndpointsListener.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/listener/AnnotationDrivenEndpointsListener.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/endpoint/listener/EndpointsListener.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/listener/EndpointsListener.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/endpoint/listener/EndpointsListener.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/listener/EndpointsListener.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/endpoint/swagger/SpringFoxConfig.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/swagger/SpringFoxConfig.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/endpoint/swagger/SpringFoxConfig.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/endpoint/swagger/SpringFoxConfig.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/Address.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/putvspost/Address.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/putvspost/Address.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/putvspost/Address.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressController.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/putvspost/AddressController.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressController.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/putvspost/AddressController.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressRepository.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/putvspost/AddressRepository.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/putvspost/AddressRepository.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/putvspost/AddressRepository.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/putvspost/PutVsPostApplication.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/putvspost/PutVsPostApplication.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/putvspost/PutVsPostApplication.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/putvspost/PutVsPostApplication.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/unsupportedmediatype/UnsupportedMediaTypeApplication.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/unsupportedmediatype/User.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/User.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/unsupportedmediatype/User.java diff --git a/spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UserController.java b/spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/unsupportedmediatype/UserController.java similarity index 100% rename from spring-boot-rest-2/src/main/java/com/baeldung/unsupportedmediatype/UserController.java rename to spring-web-modules/spring-rest-http-2/src/main/java/com/baeldung/unsupportedmediatype/UserController.java diff --git a/spring-web-modules/spring-rest-http-2/src/main/resources/application.properties b/spring-web-modules/spring-rest-http-2/src/main/resources/application.properties index ff4af943ec..3119ad188a 100644 --- a/spring-web-modules/spring-rest-http-2/src/main/resources/application.properties +++ b/spring-web-modules/spring-rest-http-2/src/main/resources/application.properties @@ -1 +1,2 @@ -spring.mvc.async.request-timeout=750 \ No newline at end of file +spring.mvc.async.request-timeout=750 +management.endpoints.web.exposure.include=mappings \ No newline at end of file diff --git a/spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java b/spring-web-modules/spring-rest-http-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java similarity index 100% rename from spring-boot-rest-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java rename to spring-web-modules/spring-rest-http-2/src/test/java/com/baeldung/unsupportedmediatype/ApplicationUnitTest.java diff --git a/spring-web-modules/spring-rest-http-2/src/test/resources/application.properties b/spring-web-modules/spring-rest-http-2/src/test/resources/application.properties index ff4af943ec..10ac1ab5fa 100644 --- a/spring-web-modules/spring-rest-http-2/src/test/resources/application.properties +++ b/spring-web-modules/spring-rest-http-2/src/test/resources/application.properties @@ -1 +1,2 @@ -spring.mvc.async.request-timeout=750 \ No newline at end of file +spring.mvc.async.request-timeout=750 +spring.main.allow-bean-definition-overriding=true From 538c116302454fe251b66773e3741a808fcb9185 Mon Sep 17 00:00:00 2001 From: chrisjaimes <45322800+chrisjaimes@users.noreply.github.com> Date: Sat, 14 May 2022 05:29:19 -0400 Subject: [PATCH 62/94] BAEL-5521 Convert boolean to int in Java (#12017) * added class for Article examples, created unit tests and modified pom in order to call Apache Commons outside test * changed parameter to prevent NPE Co-authored-by: Christian Jaimes --- java-numbers-4/pom.xml | 1 - .../baeldung/booleantoint/BooleanToInt.java | 42 ++++++++++++++ .../booleantoint/BooleanToIntUnitTest.java | 55 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 java-numbers-4/src/main/java/com/baeldung/booleantoint/BooleanToInt.java create mode 100644 java-numbers-4/src/test/java/com/baeldung/booleantoint/BooleanToIntUnitTest.java diff --git a/java-numbers-4/pom.xml b/java-numbers-4/pom.xml index 40fe17cc0d..4750b58511 100644 --- a/java-numbers-4/pom.xml +++ b/java-numbers-4/pom.xml @@ -23,7 +23,6 @@ org.apache.commons commons-lang3 ${commons-lang3.version} - test com.google.guava diff --git a/java-numbers-4/src/main/java/com/baeldung/booleantoint/BooleanToInt.java b/java-numbers-4/src/main/java/com/baeldung/booleantoint/BooleanToInt.java new file mode 100644 index 0000000000..3cca1592fb --- /dev/null +++ b/java-numbers-4/src/main/java/com/baeldung/booleantoint/BooleanToInt.java @@ -0,0 +1,42 @@ +package com.baeldung.booleantoint; + +import org.apache.commons.lang3.BooleanUtils; + +public class BooleanToInt { + public static int booleanPrimitiveToInt(boolean foo) { + int bar = 0; + if (foo) { + bar = 1; + } + return bar; + } + + public static int booleanPrimitiveToIntTernary(boolean foo) { + return (foo) ? 1 : 0; + } + + public static int booleanObjectToInt(boolean foo) { + return Boolean.compare(foo, false); + } + + public static int booleanObjectToIntInverse(boolean foo) { + return Boolean.compare(foo, true) + 1; + } + + public static int booleanObjectMethodToInt(Boolean foo) { + return foo.compareTo(false); + } + + public static int booleanObjectMethodToIntInverse(Boolean foo) { + return foo.compareTo(true) + 1; + } + + public static int booleanUtilsToInt(Boolean foo) { + return BooleanUtils.toInteger(foo); + } + + public static int bitwiseBooleanToInt(Boolean foo) { + return (Boolean.hashCode(foo) >> 1) & 1; + } +} + diff --git a/java-numbers-4/src/test/java/com/baeldung/booleantoint/BooleanToIntUnitTest.java b/java-numbers-4/src/test/java/com/baeldung/booleantoint/BooleanToIntUnitTest.java new file mode 100644 index 0000000000..032eb1d28c --- /dev/null +++ b/java-numbers-4/src/test/java/com/baeldung/booleantoint/BooleanToIntUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.booleantoint; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BooleanToIntUnitTest { + @Test + void givenBooleanPrimitiveValue_ThenReturnInt() { + assertEquals(1, BooleanToInt.booleanPrimitiveToInt(true)); + assertEquals(0, BooleanToInt.booleanPrimitiveToInt(false)); + } + + @Test + void givenBooleanPrimitiveValue_ThenReturnIntTernary() { + assertEquals(1, BooleanToInt.booleanPrimitiveToIntTernary(true)); + assertEquals(0, BooleanToInt.booleanPrimitiveToIntTernary(false)); + } + + @Test + void givenBooleanObject_ThenReturnInt() { + assertEquals(0, BooleanToInt.booleanObjectToInt(false)); + assertEquals(1, BooleanToInt.booleanObjectToInt(true)); + } + + @Test + void givenBooleanObject_ThenReturnIntInverse() { + assertEquals(0, BooleanToInt.booleanObjectToIntInverse(false)); + assertEquals(1, BooleanToInt.booleanObjectToIntInverse(true)); + } + + @Test + void givenBooleanObject_ThenReturnIntUsingClassMethod() { + assertEquals(0, BooleanToInt.booleanObjectMethodToInt(false)); + assertEquals(1, BooleanToInt.booleanObjectMethodToInt(true)); + } + + @Test + void givenBooleanObject_ThenReturnIntUsingClassMethodInverse() { + assertEquals(0, BooleanToInt.booleanObjectMethodToIntInverse(false)); + assertEquals(1, BooleanToInt.booleanObjectMethodToIntInverse(true)); + } + + @Test + void givenBoolean_ThenReturnIntUsingBooleanUtils() { + assertEquals(0, BooleanToInt.booleanUtilsToInt(false)); + assertEquals(1, BooleanToInt.booleanUtilsToInt(true)); + } + + @Test + void givenBoolean_ThenReturnIntUsingBitwiseOperators() { + assertEquals(0, BooleanToInt.bitwiseBooleanToInt(false)); + assertEquals(1, BooleanToInt.bitwiseBooleanToInt(true)); + } +} From 4f8d67b7c1cecade87c5ea879354372bcf47fbd0 Mon Sep 17 00:00:00 2001 From: Eric Martin Date: Sat, 14 May 2022 11:17:48 -0500 Subject: [PATCH 63/94] BAEL-5577: Moving code to core-java-collections-4 (#12210) Co-authored-by: martine --- .../core-java-collections-list-4/README.md | 7 +++ .../core-java-collections-list-4/pom.xml | 56 +++++++++++++++++++ .../list/listoflists/ListOfListsUnitTest.java | 0 .../test/resources/listoflists/example.csv | 0 core-java-modules/pom.xml | 1 + 5 files changed, 64 insertions(+) create mode 100644 core-java-modules/core-java-collections-list-4/README.md create mode 100644 core-java-modules/core-java-collections-list-4/pom.xml rename core-java-modules/{core-java-collections-list-3 => core-java-collections-list-4}/src/test/java/com/baeldung/list/listoflists/ListOfListsUnitTest.java (100%) rename core-java-modules/{core-java-collections-list-3 => core-java-collections-list-4}/src/test/resources/listoflists/example.csv (100%) diff --git a/core-java-modules/core-java-collections-list-4/README.md b/core-java-modules/core-java-collections-list-4/README.md new file mode 100644 index 0000000000..3a90b3d813 --- /dev/null +++ b/core-java-modules/core-java-collections-list-4/README.md @@ -0,0 +1,7 @@ +## Core Java Collections List (Part 4) + +This module contains articles about the Java List collection + +### Relevant Articles: +- [Working With a List of Lists in Java](https://www.baeldung.com/java-list-of-lists) +- [[<-- Prev]](/core-java-modules/core-java-collections-list-3) diff --git a/core-java-modules/core-java-collections-list-4/pom.xml b/core-java-modules/core-java-collections-list-4/pom.xml new file mode 100644 index 0000000000..8c1d3a881e --- /dev/null +++ b/core-java-modules/core-java-collections-list-4/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + core-java-collections-list-4 + 0.1.0-SNAPSHOT + core-java-collections-list-4 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + net.sf.trove4j + trove4j + ${trove4j.version} + + + it.unimi.dsi + fastutil + ${fastutil.version} + + + colt + colt + ${colt.version} + + + org.openjdk.jmh + jmh-core + ${jmh-core.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-generator.version} + + + + + 3.0.2 + 8.1.0 + 1.2.0 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listoflists/ListOfListsUnitTest.java b/core-java-modules/core-java-collections-list-4/src/test/java/com/baeldung/list/listoflists/ListOfListsUnitTest.java similarity index 100% rename from core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listoflists/ListOfListsUnitTest.java rename to core-java-modules/core-java-collections-list-4/src/test/java/com/baeldung/list/listoflists/ListOfListsUnitTest.java diff --git a/core-java-modules/core-java-collections-list-3/src/test/resources/listoflists/example.csv b/core-java-modules/core-java-collections-list-4/src/test/resources/listoflists/example.csv similarity index 100% rename from core-java-modules/core-java-collections-list-3/src/test/resources/listoflists/example.csv rename to core-java-modules/core-java-collections-list-4/src/test/resources/listoflists/example.csv diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 04b46e3740..15347d0fe9 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -35,6 +35,7 @@ core-java-collections-list core-java-collections-list-2 core-java-collections-list-3 + core-java-collections-list-4 core-java-collections-maps core-java-collections-maps-2 core-java-collections-maps-3 From 8eadd920467ffea5e7e2975bbb0cb7c789bf8ccf Mon Sep 17 00:00:00 2001 From: Luis Javier Peris Morillo Date: Sat, 14 May 2022 18:33:51 +0200 Subject: [PATCH 64/94] refactor: split spring security module (#12211) Refs #BAEL-5339 --- spring-security-modules/pom.xml | 1 + .../spring-security-core-2/.gitignore | 1 + .../spring-security-core-2/README.md | 10 ++ .../spring-security-core-2/pom.xml | 103 ++++++++++++++++++ .../exceptionhandler/AppInitializer.java | 0 .../controller/AccessDeniedController.java | 0 .../controller/CustomErrorController.java | 0 .../controller/HomeController.java | 0 .../controller/SecuredResourceController.java | 0 .../security/CustomAccessDeniedHandler.java | 0 .../CustomAuthenticationFailureHandler.java | 0 .../CustomAuthenticationSuccessHandler.java | 0 .../security/SecurityConfig.java | 0 .../src/main/resources/application.properties | 0 .../src/main/resources/templates/admin.html | 0 .../src/main/resources/templates/denied.html | 0 .../src/main/resources/templates/error.html | 0 .../src/main/resources/templates/index.html | 0 .../SecurityConfigUnitTest.java | 0 19 files changed, 115 insertions(+) create mode 100644 spring-security-modules/spring-security-core-2/.gitignore create mode 100644 spring-security-modules/spring-security-core-2/README.md create mode 100644 spring-security-modules/spring-security-core-2/pom.xml rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/java/com/baeldung/exceptionhandler/AppInitializer.java (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/java/com/baeldung/exceptionhandler/controller/AccessDeniedController.java (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/java/com/baeldung/exceptionhandler/controller/CustomErrorController.java (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/java/com/baeldung/exceptionhandler/controller/HomeController.java (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/java/com/baeldung/exceptionhandler/controller/SecuredResourceController.java (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/java/com/baeldung/exceptionhandler/security/CustomAccessDeniedHandler.java (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationFailureHandler.java (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationSuccessHandler.java (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/java/com/baeldung/exceptionhandler/security/SecurityConfig.java (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/resources/application.properties (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/resources/templates/admin.html (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/resources/templates/denied.html (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/resources/templates/error.html (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/main/resources/templates/index.html (100%) rename spring-security-modules/{spring-security-core => spring-security-core-2}/src/test/java/com/baeldung/exceptionhandler/SecurityConfigUnitTest.java (100%) diff --git a/spring-security-modules/pom.xml b/spring-security-modules/pom.xml index bb36909c79..eb643c78c7 100644 --- a/spring-security-modules/pom.xml +++ b/spring-security-modules/pom.xml @@ -22,6 +22,7 @@ spring-security-acl spring-security-auth0 spring-security-core + spring-security-core-2 spring-security-ldap spring-security-legacy-oidc spring-security-oauth2-sso diff --git a/spring-security-modules/spring-security-core-2/.gitignore b/spring-security-modules/spring-security-core-2/.gitignore new file mode 100644 index 0000000000..9f970225ad --- /dev/null +++ b/spring-security-modules/spring-security-core-2/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/spring-security-modules/spring-security-core-2/README.md b/spring-security-modules/spring-security-core-2/README.md new file mode 100644 index 0000000000..9ce12af8ef --- /dev/null +++ b/spring-security-modules/spring-security-core-2/README.md @@ -0,0 +1,10 @@ +## Spring Security Core + +This module contains articles about core Spring Security + +### Relevant Articles: +- [Handle Spring Security Exceptions](https://www.baeldung.com/spring-security-exceptions) + +### Build the Project + +`mvn clean install` diff --git a/spring-security-modules/spring-security-core-2/pom.xml b/spring-security-modules/spring-security-core-2/pom.xml new file mode 100644 index 0000000000..cf150bfd42 --- /dev/null +++ b/spring-security-modules/spring-security-core-2/pom.xml @@ -0,0 +1,103 @@ + + + 4.0.0 + spring-security-core-2 + 0.1-SNAPSHOT + spring-security-core-2 + war + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-devtools + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + com.h2database + h2 + runtime + + + org.springframework.security + spring-security-test + + + + + spring-security-core + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-war-plugin + + + + + + + live + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + none + + + **/*LiveTest.java + + + cargo + + + + + + + + + + + \ No newline at end of file diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/AppInitializer.java b/spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/AppInitializer.java similarity index 100% rename from spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/AppInitializer.java rename to spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/AppInitializer.java diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/controller/AccessDeniedController.java b/spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/controller/AccessDeniedController.java similarity index 100% rename from spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/controller/AccessDeniedController.java rename to spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/controller/AccessDeniedController.java diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/controller/CustomErrorController.java b/spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/controller/CustomErrorController.java similarity index 100% rename from spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/controller/CustomErrorController.java rename to spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/controller/CustomErrorController.java diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/controller/HomeController.java b/spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/controller/HomeController.java similarity index 100% rename from spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/controller/HomeController.java rename to spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/controller/HomeController.java diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/controller/SecuredResourceController.java b/spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/controller/SecuredResourceController.java similarity index 100% rename from spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/controller/SecuredResourceController.java rename to spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/controller/SecuredResourceController.java diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/security/CustomAccessDeniedHandler.java b/spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/security/CustomAccessDeniedHandler.java similarity index 100% rename from spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/security/CustomAccessDeniedHandler.java rename to spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/security/CustomAccessDeniedHandler.java diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationFailureHandler.java b/spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationFailureHandler.java similarity index 100% rename from spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationFailureHandler.java rename to spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationFailureHandler.java diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationSuccessHandler.java b/spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationSuccessHandler.java similarity index 100% rename from spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationSuccessHandler.java rename to spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/security/CustomAuthenticationSuccessHandler.java diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/security/SecurityConfig.java b/spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/security/SecurityConfig.java similarity index 100% rename from spring-security-modules/spring-security-core/src/main/java/com/baeldung/exceptionhandler/security/SecurityConfig.java rename to spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/exceptionhandler/security/SecurityConfig.java diff --git a/spring-security-modules/spring-security-core/src/main/resources/application.properties b/spring-security-modules/spring-security-core-2/src/main/resources/application.properties similarity index 100% rename from spring-security-modules/spring-security-core/src/main/resources/application.properties rename to spring-security-modules/spring-security-core-2/src/main/resources/application.properties diff --git a/spring-security-modules/spring-security-core/src/main/resources/templates/admin.html b/spring-security-modules/spring-security-core-2/src/main/resources/templates/admin.html similarity index 100% rename from spring-security-modules/spring-security-core/src/main/resources/templates/admin.html rename to spring-security-modules/spring-security-core-2/src/main/resources/templates/admin.html diff --git a/spring-security-modules/spring-security-core/src/main/resources/templates/denied.html b/spring-security-modules/spring-security-core-2/src/main/resources/templates/denied.html similarity index 100% rename from spring-security-modules/spring-security-core/src/main/resources/templates/denied.html rename to spring-security-modules/spring-security-core-2/src/main/resources/templates/denied.html diff --git a/spring-security-modules/spring-security-core/src/main/resources/templates/error.html b/spring-security-modules/spring-security-core-2/src/main/resources/templates/error.html similarity index 100% rename from spring-security-modules/spring-security-core/src/main/resources/templates/error.html rename to spring-security-modules/spring-security-core-2/src/main/resources/templates/error.html diff --git a/spring-security-modules/spring-security-core/src/main/resources/templates/index.html b/spring-security-modules/spring-security-core-2/src/main/resources/templates/index.html similarity index 100% rename from spring-security-modules/spring-security-core/src/main/resources/templates/index.html rename to spring-security-modules/spring-security-core-2/src/main/resources/templates/index.html diff --git a/spring-security-modules/spring-security-core/src/test/java/com/baeldung/exceptionhandler/SecurityConfigUnitTest.java b/spring-security-modules/spring-security-core-2/src/test/java/com/baeldung/exceptionhandler/SecurityConfigUnitTest.java similarity index 100% rename from spring-security-modules/spring-security-core/src/test/java/com/baeldung/exceptionhandler/SecurityConfigUnitTest.java rename to spring-security-modules/spring-security-core-2/src/test/java/com/baeldung/exceptionhandler/SecurityConfigUnitTest.java From f16942b0c172f099aa7c54c197c26fe52d5753f8 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Sun, 15 May 2022 00:04:42 +0100 Subject: [PATCH 65/94] [JAVA-8150] Update webclient to use retrieve() method instead of deprecated API --- .../WebClientRequestsUnitTest.java | 176 ---------------- ...bClientRequestsWithParametersUnitTest.java | 194 ++++++++++++++++++ 2 files changed, 194 insertions(+), 176 deletions(-) delete mode 100644 spring-reactive/src/test/java/com/baeldung/reactive/webclientrequests/WebClientRequestsUnitTest.java create mode 100644 spring-reactive/src/test/java/com/baeldung/reactive/webclientrequests/WebClientRequestsWithParametersUnitTest.java diff --git a/spring-reactive/src/test/java/com/baeldung/reactive/webclientrequests/WebClientRequestsUnitTest.java b/spring-reactive/src/test/java/com/baeldung/reactive/webclientrequests/WebClientRequestsUnitTest.java deleted file mode 100644 index ff59f12391..0000000000 --- a/spring-reactive/src/test/java/com/baeldung/reactive/webclientrequests/WebClientRequestsUnitTest.java +++ /dev/null @@ -1,176 +0,0 @@ -package com.baeldung.reactive.webclientrequests; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.web.reactive.function.client.ClientRequest; -import org.springframework.web.reactive.function.client.ClientResponse; -import org.springframework.web.reactive.function.client.ExchangeFunction; -import org.springframework.web.reactive.function.client.WebClient; -import org.springframework.web.util.DefaultUriBuilderFactory; -import reactor.core.publisher.Mono; - -import java.time.Duration; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -@RunWith(SpringRunner.class) -@WebFluxTest -public class WebClientRequestsUnitTest { - - private static final String BASE_URL = "https://example.com"; - - private WebClient webClient; - - @Captor - private ArgumentCaptor argumentCaptor; - - private ExchangeFunction exchangeFunction; - - @Before - public void init() { - MockitoAnnotations.initMocks(this); - this.exchangeFunction = mock(ExchangeFunction.class); - ClientResponse mockResponse = mock(ClientResponse.class); - when(this.exchangeFunction.exchange(this.argumentCaptor.capture())).thenReturn(Mono.just(mockResponse)); - this.webClient = WebClient - .builder() - .baseUrl(BASE_URL) - .exchangeFunction(exchangeFunction) - .build(); - } - - @Test - public void whenCallSimpleURI_thenURIMatched() { - this.webClient.get() - .uri("/products") - .exchange() - .block(Duration.ofSeconds(1)); - verifyCalledUrl("/products"); - } - - @Test - public void whenCallSinglePathSegmentUri_thenURIMatched() { - this.webClient.get() - .uri(uriBuilder -> uriBuilder - .path("/products/{id}") - .build(2)) - .exchange() - .block(Duration.ofSeconds(1)); - verifyCalledUrl("/products/2"); - } - - @Test - public void whenCallMultiplePathSegmentsUri_thenURIMatched() { - this.webClient.get() - .uri(uriBuilder -> uriBuilder - .path("/products/{id}/attributes/{attributeId}") - .build(2, 13)) - .exchange() - .block(Duration.ofSeconds(1)); - verifyCalledUrl("/products/2/attributes/13"); - } - - @Test - public void whenCallSingleQueryParams_thenURIMatched() { - this.webClient.get() - .uri(uriBuilder -> uriBuilder - .path("/products/") - .queryParam("name", "AndroidPhone") - .queryParam("color", "black") - .queryParam("deliveryDate", "13/04/2019") - .build()) - .exchange() - .block(Duration.ofSeconds(1)); - verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019"); - } - - @Test - public void whenCallSingleQueryParamsPlaceholders_thenURIMatched() { - this.webClient.get() - .uri(uriBuilder -> uriBuilder - .path("/products/") - .queryParam("name", "{title}") - .queryParam("color", "{authorId}") - .queryParam("deliveryDate", "{date}") - .build("AndroidPhone", "black", "13/04/2019")) - .exchange() - .block(Duration.ofSeconds(1)); - verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13%2F04%2F2019"); - } - - @Test - public void whenCallArrayQueryParamsBrackets_thenURIMatched() { - this.webClient.get() - .uri(uriBuilder -> uriBuilder - .path("/products/") - .queryParam("tag[]", "Snapdragon", "NFC") - .build()) - .exchange() - .block(Duration.ofSeconds(1)); - verifyCalledUrl("/products/?tag%5B%5D=Snapdragon&tag%5B%5D=NFC"); - } - - - @Test - public void whenCallArrayQueryParams_thenURIMatched() { - this.webClient.get() - .uri(uriBuilder -> uriBuilder - .path("/products/") - .queryParam("category", "Phones", "Tablets") - .build()) - .exchange() - .block(Duration.ofSeconds(1)); - verifyCalledUrl("/products/?category=Phones&category=Tablets"); - } - - @Test - public void whenCallArrayQueryParamsComma_thenURIMatched() { - this.webClient.get() - .uri(uriBuilder -> uriBuilder - .path("/products/") - .queryParam("category", String.join(",", "Phones", "Tablets")) - .build()) - .exchange() - .block(Duration.ofSeconds(1)); - verifyCalledUrl("/products/?category=Phones,Tablets"); - } - - @Test - public void whenUriComponentEncoding_thenQueryParamsNotEscaped() { - DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(BASE_URL); - factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.URI_COMPONENT); - this.webClient = WebClient - .builder() - .uriBuilderFactory(factory) - .baseUrl(BASE_URL) - .exchangeFunction(exchangeFunction) - .build(); - this.webClient.get() - .uri(uriBuilder -> uriBuilder - .path("/products/") - .queryParam("name", "AndroidPhone") - .queryParam("color", "black") - .queryParam("deliveryDate", "13/04/2019") - .build()) - .exchange() - .block(Duration.ofSeconds(1)); - verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019"); - } - - private void verifyCalledUrl(String relativeUrl) { - ClientRequest request = this.argumentCaptor.getValue(); - Assert.assertEquals(String.format("%s%s", BASE_URL, relativeUrl), request.url().toString()); - Mockito.verify(this.exchangeFunction).exchange(request); - verifyNoMoreInteractions(this.exchangeFunction); - } -} diff --git a/spring-reactive/src/test/java/com/baeldung/reactive/webclientrequests/WebClientRequestsWithParametersUnitTest.java b/spring-reactive/src/test/java/com/baeldung/reactive/webclientrequests/WebClientRequestsWithParametersUnitTest.java new file mode 100644 index 0000000000..eefde078e1 --- /dev/null +++ b/spring-reactive/src/test/java/com/baeldung/reactive/webclientrequests/WebClientRequestsWithParametersUnitTest.java @@ -0,0 +1,194 @@ +package com.baeldung.reactive.webclientrequests; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.reactive.function.client.ClientRequest; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.ExchangeFunction; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.DefaultUriBuilderFactory; +import reactor.core.publisher.Mono; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +@RunWith(SpringRunner.class) +@WebFluxTest +public class WebClientRequestsWithParametersUnitTest { + + private static final String BASE_URL = "https://example.com"; + + private WebClient webClient; + + @Captor + private ArgumentCaptor argumentCaptor; + + @Mock + private ExchangeFunction exchangeFunction; + + @Before + public void init() { + ClientResponse mockResponse = mock(ClientResponse.class); + when(mockResponse.bodyToMono(String.class)).thenReturn(Mono.just("test")); + when(exchangeFunction.exchange(argumentCaptor.capture())).thenReturn(Mono.just(mockResponse)); + + webClient = WebClient + .builder() + .baseUrl(BASE_URL) + .exchangeFunction(exchangeFunction) + .build(); + } + + @Test + public void whenCallSimpleURI_thenURIMatched() { + webClient.get() + .uri("/products") + .retrieve() + .bodyToMono(String.class) + .block(); + + verifyCalledUrl("/products"); + } + + @Test + public void whenCallSinglePathSegmentUri_thenURIMatched() { + webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/{id}") + .build(2)) + .retrieve() + .bodyToMono(String.class) + .block(); + + verifyCalledUrl("/products/2"); + } + + @Test + public void whenCallMultiplePathSegmentsUri_thenURIMatched() { + webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/{id}/attributes/{attributeId}") + .build(2, 13)) + .retrieve() + .bodyToMono(String.class) + .block(); + + verifyCalledUrl("/products/2/attributes/13"); + } + + @Test + public void whenCallSingleQueryParams_thenURIMatched() { + webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("name", "AndroidPhone") + .queryParam("color", "black") + .queryParam("deliveryDate", "13/04/2019") + .build()) + .retrieve() + .bodyToMono(String.class) + .block(); + + verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019"); + } + + @Test + public void whenCallSingleQueryParamsPlaceholders_thenURIMatched() { + webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("name", "{title}") + .queryParam("color", "{authorId}") + .queryParam("deliveryDate", "{date}") + .build("AndroidPhone", "black", "13/04/2019")) + .retrieve() + .bodyToMono(String.class) + .block(); + + verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13%2F04%2F2019"); + } + + @Test + public void whenCallArrayQueryParamsBrackets_thenURIMatched() { + webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("tag[]", "Snapdragon", "NFC") + .build()) + .retrieve() + .bodyToMono(String.class) + .block(); + + verifyCalledUrl("/products/?tag%5B%5D=Snapdragon&tag%5B%5D=NFC"); + } + + @Test + public void whenCallArrayQueryParams_thenURIMatched() { + webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("category", "Phones", "Tablets") + .build()) + .retrieve() + .bodyToMono(String.class) + .block(); + + verifyCalledUrl("/products/?category=Phones&category=Tablets"); + } + + @Test + public void whenCallArrayQueryParamsComma_thenURIMatched() { + webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("category", String.join(",", "Phones", "Tablets")) + .build()) + .retrieve() + .bodyToMono(String.class) + .block(); + + verifyCalledUrl("/products/?category=Phones,Tablets"); + } + + @Test + public void whenUriComponentEncoding_thenQueryParamsNotEscaped() { + DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(BASE_URL); + factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.URI_COMPONENT); + webClient = WebClient + .builder() + .uriBuilderFactory(factory) + .baseUrl(BASE_URL) + .exchangeFunction(exchangeFunction) + .build(); + + webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/products/") + .queryParam("name", "AndroidPhone") + .queryParam("color", "black") + .queryParam("deliveryDate", "13/04/2019") + .build()) + .retrieve() + .bodyToMono(String.class) + .block(); + + verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019"); + } + + private void verifyCalledUrl(String relativeUrl) { + ClientRequest request = argumentCaptor.getValue(); + assertEquals(String.format("%s%s", BASE_URL, relativeUrl), request.url().toString()); + + verify(exchangeFunction).exchange(request); + verifyNoMoreInteractions(exchangeFunction); + } +} From be944bc3010042e28d5aa8ecf650a32c13634500 Mon Sep 17 00:00:00 2001 From: ACHRAF TAITAI <43656331+achraftt@users.noreply.github.com> Date: Sun, 15 May 2022 07:12:48 +0200 Subject: [PATCH 66/94] Upgrade JCache article (#12213) --- libraries-data/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries-data/pom.xml b/libraries-data/pom.xml index dd48453a8c..c33be5b192 100644 --- a/libraries-data/pom.xml +++ b/libraries-data/pom.xml @@ -172,9 +172,9 @@ 1.0.0 2.4.0 2.8.2 - 1.1.0 + 1.1.1 1.5.0 - 3.8.4 + 5.1.1 0.15.0 2.2.0 1.6.0.1 From 8e7dc50c0e4ba9df54189f2be798134ff961ee42 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Sun, 15 May 2022 08:53:05 +0200 Subject: [PATCH 67/94] Java 11788 (#12204) * JAVA-11788: Cleanup pom properties in core-java-modules * JAVA-11788: Cleanup pom properties --- core-java-modules/core-java-collections-4/pom.xml | 6 +++++- core-java-modules/core-java/pom.xml | 3 ++- .../java-collections-conversions-2/pom.xml | 6 +++++- core-java-modules/java-collections-maps-3/pom.xml | 2 +- docker/docker-caching/multi-module-caching/pom.xml | 3 ++- docker/docker-caching/single-module-caching/pom.xml | 3 ++- grpc/pom.xml | 3 ++- json-2/pom.xml | 12 ++++++++---- kubernetes/k8s-intro/pom.xml | 6 +++++- 9 files changed, 32 insertions(+), 12 deletions(-) diff --git a/core-java-modules/core-java-collections-4/pom.xml b/core-java-modules/core-java-collections-4/pom.xml index 68d7df66d0..aaf63a2b2a 100644 --- a/core-java-modules/core-java-collections-4/pom.xml +++ b/core-java-modules/core-java-collections-4/pom.xml @@ -18,8 +18,12 @@ commons-lang commons-lang - 2.2 + ${commons-lang.version} + + 2.2 + + \ No newline at end of file diff --git a/core-java-modules/core-java/pom.xml b/core-java-modules/core-java/pom.xml index 786ee91192..87abe6c007 100644 --- a/core-java-modules/core-java/pom.xml +++ b/core-java-modules/core-java/pom.xml @@ -67,7 +67,7 @@ com.google.gdata core - 1.47.1 + ${gdata.version} @@ -193,6 +193,7 @@ 1.8 1.8 4.3.20.RELEASE + 1.47.1 \ No newline at end of file diff --git a/core-java-modules/java-collections-conversions-2/pom.xml b/core-java-modules/java-collections-conversions-2/pom.xml index 0f8e80fdbf..510921c35e 100644 --- a/core-java-modules/java-collections-conversions-2/pom.xml +++ b/core-java-modules/java-collections-conversions-2/pom.xml @@ -28,7 +28,7 @@ io.vavr vavr - 0.10.3 + ${vavr.version} @@ -42,4 +42,8 @@ + + 0.10.3 + + \ No newline at end of file diff --git a/core-java-modules/java-collections-maps-3/pom.xml b/core-java-modules/java-collections-maps-3/pom.xml index db56550d10..ab80a9e2fd 100644 --- a/core-java-modules/java-collections-maps-3/pom.xml +++ b/core-java-modules/java-collections-maps-3/pom.xml @@ -19,7 +19,7 @@ org.junit.jupiter junit-jupiter-api - 5.8.1 + ${junit-jupiter.version} org.springframework diff --git a/docker/docker-caching/multi-module-caching/pom.xml b/docker/docker-caching/multi-module-caching/pom.xml index e02ced2dca..7e279dc334 100644 --- a/docker/docker-caching/multi-module-caching/pom.xml +++ b/docker/docker-caching/multi-module-caching/pom.xml @@ -19,13 +19,14 @@ com.google.guava guava - 31.0.1-jre + ${guava.version} 1.8 + 31.0.1-jre \ No newline at end of file diff --git a/docker/docker-caching/single-module-caching/pom.xml b/docker/docker-caching/single-module-caching/pom.xml index 74bb477fb2..d7f96e1e7e 100644 --- a/docker/docker-caching/single-module-caching/pom.xml +++ b/docker/docker-caching/single-module-caching/pom.xml @@ -11,7 +11,7 @@ com.google.guava guava - 31.0.1-jre + ${guava.version} @@ -48,6 +48,7 @@ 8 8 + 31.0.1-jre \ No newline at end of file diff --git a/grpc/pom.xml b/grpc/pom.xml index 40284c90fe..40d35183dc 100644 --- a/grpc/pom.xml +++ b/grpc/pom.xml @@ -40,7 +40,7 @@ javax.annotation javax.annotation-api - 1.2 + ${annotation-api.version} @@ -79,6 +79,7 @@ 3.17.2 1.6.2 0.6.1 + 1.2 \ No newline at end of file diff --git a/json-2/pom.xml b/json-2/pom.xml index 3e12fccc29..6fbdebc953 100644 --- a/json-2/pom.xml +++ b/json-2/pom.xml @@ -27,7 +27,7 @@ org.jsonschema2pojo jsonschema2pojo-core - 1.1.1 + ${jsonschema2pojo-core.version} com.jsoniter @@ -62,7 +62,7 @@ com.io-informatics.oss jackson-jsonld - 0.1.1 + ${jackson-jsonld.version} jackson-databind @@ -85,7 +85,7 @@ de.escalon.hypermedia hydra-jsonld - 0.4.2 + ${hydra-jsonld.version} jackson-databind @@ -96,7 +96,7 @@ com.github.jsonld-java jsonld-java - 0.13.0 + ${jsonld-java.version} jackson-core @@ -154,6 +154,10 @@ 1.9.2 1.2.21 20211205 + 1.1.1 + 0.1.1 + 0.4.2 + 0.13.0 \ No newline at end of file diff --git a/kubernetes/k8s-intro/pom.xml b/kubernetes/k8s-intro/pom.xml index 6d1cec9971..067700bdad 100644 --- a/kubernetes/k8s-intro/pom.xml +++ b/kubernetes/k8s-intro/pom.xml @@ -15,7 +15,7 @@ io.kubernetes client-java - 11.0.0 + ${client-java.version} ch.qos.logback @@ -39,4 +39,8 @@ + + 11.0.0 + + \ No newline at end of file From 93b3264964f888c211933a0dc9934636011c9148 Mon Sep 17 00:00:00 2001 From: etrandafir93 <75391049+etrandafir93@users.noreply.github.com> Date: Sun, 15 May 2022 17:35:01 +0300 Subject: [PATCH 68/94] BAEL-5547: added code for specifications join (#12212) * BAEL-5547: added code for specifications join * BAEL-5547: renamed test class and formatted the code --- .../jpa/query/specifications/join/Author.java | 57 +++++++++++++ .../join/AuthorSpecifications.java | 24 ++++++ .../join/AuthorsRepository.java | 9 +++ .../jpa/query/specifications/join/Book.java | 37 +++++++++ .../SpecificationsJoinIntegrationTest.java | 80 +++++++++++++++++++ 5 files changed, 207 insertions(+) create mode 100644 persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/Author.java create mode 100644 persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/AuthorSpecifications.java create mode 100644 persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/AuthorsRepository.java create mode 100644 persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/Book.java create mode 100644 persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/specifications/join/SpecificationsJoinIntegrationTest.java diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/Author.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/Author.java new file mode 100644 index 0000000000..70e699ebeb --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/Author.java @@ -0,0 +1,57 @@ +package com.baeldung.spring.data.jpa.query.specifications.join; + +import javax.persistence.*; + +import java.util.List; + +@Entity +public class Author { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String firstName; + + private String lastName; + + @OneToMany(cascade = CascadeType.ALL) + private List books; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public List getBooks() { + return books; + } + + public void setBooks(List books) { + this.books = books; + } + + @Override + public String toString() { + return "Author{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", books=" + books + '}'; + } +} diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/AuthorSpecifications.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/AuthorSpecifications.java new file mode 100644 index 0000000000..73d0cd6c01 --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/AuthorSpecifications.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.data.jpa.query.specifications.join; + +import org.springframework.data.jpa.domain.Specification; + +import javax.persistence.criteria.*; + +public class AuthorSpecifications { + + public static Specification hasFirstNameLike(String name) { + return (root, query, criteriaBuilder) -> criteriaBuilder.like(root.get("firstName"), "%" + name + "%"); + } + + public static Specification hasLastName(String name) { + return (root, query, cb) -> cb.equal(root.get("lastName"), name); + } + + public static Specification hasBookWithTitle(String bookTitle) { + return (root, query, criteriaBuilder) -> { + Join authorsBook = root.join("books"); + return criteriaBuilder.equal(authorsBook.get("title"), bookTitle); + }; + } + +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/AuthorsRepository.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/AuthorsRepository.java new file mode 100644 index 0000000000..67fe86b8b3 --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/AuthorsRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.spring.data.jpa.query.specifications.join; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Repository; + +@Repository +public interface AuthorsRepository extends JpaRepository, JpaSpecificationExecutor { +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/Book.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/Book.java new file mode 100644 index 0000000000..3d658ca107 --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/specifications/join/Book.java @@ -0,0 +1,37 @@ +package com.baeldung.spring.data.jpa.query.specifications.join; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String title; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + @Override + public String toString() { + return "Book{" + "id=" + id + ", title='" + title + '\'' + '}'; + } +} diff --git a/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/specifications/join/SpecificationsJoinIntegrationTest.java b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/specifications/join/SpecificationsJoinIntegrationTest.java new file mode 100644 index 0000000000..27db09d11c --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/specifications/join/SpecificationsJoinIntegrationTest.java @@ -0,0 +1,80 @@ +package com.baeldung.spring.data.jpa.query.specifications.join; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.data.jpa.domain.Specification; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Arrays; +import java.util.List; + +import static com.baeldung.spring.data.jpa.query.specifications.join.AuthorSpecifications.*; +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@DataJpaTest +public class SpecificationsJoinIntegrationTest { + + @Autowired + private AuthorsRepository repository; + + @Before + public void beforeEach() { + saveTestData(); + } + + @Test + public void whenSearchingByLastName_thenOneAuthorIsReturned() { + + List authors = repository.findAll(hasLastName("Martin")); + + assertThat(authors).hasSize(1); + } + + @Test + public void whenSearchingByLastNameAndFirstNameLike_thenOneAuthorIsReturned() { + + Specification specification = hasLastName("Martin").and(hasFirstNameLike("Robert")); + + List authors = repository.findAll(specification); + + assertThat(authors).hasSize(1); + } + + @Test + public void whenSearchingByBookTitle_thenOneAuthorIsReturned() { + + Specification specification = hasBookWithTitle("Clean Code"); + + List authors = repository.findAll(specification); + + assertThat(authors).hasSize(1); + } + + @Test + public void whenSearchingByBookTitleAndAuthorName_thenOneAuthorIsReturned() { + + Specification specification = hasLastName("Martin").and(hasBookWithTitle("Clean Code")); + + List authors = repository.findAll(specification); + + assertThat(authors).hasSize(1); + } + + private void saveTestData() { + Author uncleBob = new Author(); + uncleBob.setFirstName("Robert"); + uncleBob.setLastName("Martin"); + + Book book1 = new Book(); + book1.setTitle("Clean Code"); + Book book2 = new Book(); + book2.setTitle("Clean Architecture"); + + uncleBob.setBooks(Arrays.asList(book1, book2)); + repository.save(uncleBob); + } +} From 386707c24f7edfeca9bf9c2bf32051cc2f3495dd Mon Sep 17 00:00:00 2001 From: panagiotiskakos Date: Sun, 15 May 2022 18:28:33 +0300 Subject: [PATCH 69/94] [JAVA-10577] workaround for circular dependencies --- .../src/main/resources/application.properties | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-security-modules/spring-security-saml/src/main/resources/application.properties b/spring-security-modules/spring-security-saml/src/main/resources/application.properties index 1d93a12737..fd7798dda9 100644 --- a/spring-security-modules/spring-security-saml/src/main/resources/application.properties +++ b/spring-security-modules/spring-security-saml/src/main/resources/application.properties @@ -1,8 +1,9 @@ -saml.keystore.location=classpath:/saml/samlKeystore.jks +saml.keystore.location=classpath:/saml/saml-keystore # Password for Java keystore and item therein saml.keystore.password= saml.keystore.alias= # SAML Entity ID extracted from top of SAML metadata file saml.idp= -saml.sp=http://localhost:8080/saml/metadata \ No newline at end of file +saml.sp=http://localhost:8080/saml/metadata +spring.main.allow-circular-references=true \ No newline at end of file From 0e501405a4a1f1589c918450f95cab7be90e2a30 Mon Sep 17 00:00:00 2001 From: Azhwani Date: Sun, 15 May 2022 19:20:04 +0200 Subject: [PATCH 70/94] init commit --- .../componentscan/springapp/SpringComponentScanApp.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/SpringComponentScanApp.java b/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/SpringComponentScanApp.java index 8873f1214c..5d3cbe35cb 100644 --- a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/SpringComponentScanApp.java +++ b/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/SpringComponentScanApp.java @@ -13,6 +13,10 @@ import org.springframework.context.annotation.Configuration; // @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class)) // @ComponentScan(basePackages = "com.baeldung.componentscan.springapp") // @ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals") +// @ComponentScan(basePackages = {"com.baeldung.componentscan.springapp.animals","com.baeldung.componentscan.springapp.flowers"}) +// @ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals;com.baeldung.componentscan.springapp.flowers") +// @ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals,com.baeldung.componentscan.springapp.flowers") +// @ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals com.baeldung.componentscan.springapp.flowers") // @ComponentScan (excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springapp\\.flowers\\..*")) public class SpringComponentScanApp { From 8fc971657a6a7017fb0f78a4efe4b762acb979bd Mon Sep 17 00:00:00 2001 From: Thibault Faure Date: Fri, 29 Apr 2022 22:13:19 +0200 Subject: [PATCH 71/94] BAEL-5383 code for the use delimiters article --- .../java9/delimiters/DelimiterDemo.java | 36 ++++++++++ .../delimiters/DelimiterDemoUnitTest.java | 67 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 core-java-modules/core-java-9/src/main/java/com/baeldung/java9/delimiters/DelimiterDemo.java create mode 100644 core-java-modules/core-java-9/src/test/java/com/baeldung/java9/delimiters/DelimiterDemoUnitTest.java diff --git a/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/delimiters/DelimiterDemo.java b/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/delimiters/DelimiterDemo.java new file mode 100644 index 0000000000..edc0b91d02 --- /dev/null +++ b/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/delimiters/DelimiterDemo.java @@ -0,0 +1,36 @@ +package com.baeldung.java9.delimiters; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.regex.Pattern; + +public class DelimiterDemo { + + public static List scannerWithDelimiter(String input, String delimiter) { + try (Scanner scan = new Scanner(input)) { + scan.useDelimiter(delimiter); + List result = new ArrayList(); + scan.forEachRemaining(result::add); + return result; + } + } + + public static List scannerWithDelimiterUsingPattern(String input, Pattern delimiter) { + try (Scanner scan = new Scanner(input)) { + scan.useDelimiter(delimiter); + List result = new ArrayList(); + scan.forEachRemaining(result::add); + return result; + } + } + + public static List baseScanner(String input) { + try (Scanner scan = new Scanner(input)) { + List result = new ArrayList(); + scan.forEachRemaining(result::add); + return result; + } + } + +} diff --git a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/delimiters/DelimiterDemoUnitTest.java b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/delimiters/DelimiterDemoUnitTest.java new file mode 100644 index 0000000000..1c1fffe362 --- /dev/null +++ b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/delimiters/DelimiterDemoUnitTest.java @@ -0,0 +1,67 @@ +package com.baeldung.java9.delimiters; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; +import java.util.List; +import java.util.function.BiFunction; +import java.util.regex.Pattern; + +import org.junit.jupiter.api.Test; + +class DelimiterDemoUnitTest { + + @Test + void givenSimpleCharacterDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() { + checkOutput(DelimiterDemo::scannerWithDelimiter, "Welcome to Baeldung", "\\s", Arrays.asList("Welcome", "to", "Baeldung")); + } + + @Test + void givenStringDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() { + checkOutput(DelimiterDemo::scannerWithDelimiter, "HelloBaeldungHelloWorld", "Hello", Arrays.asList("Baeldung", "World")); + } + + @Test + void givenVariousPossibleDelimiters_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() { + checkOutput(DelimiterDemo::scannerWithDelimiter, "Welcome to Baeldung.\nThank you for reading.\nThe team", "\n|\\s", Arrays.asList("Welcome", "to", "Baeldung.", "Thank", "you", "for", "reading.", "The", "team")); + } + + @Test + void givenWildcardRegexDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() { + checkOutput(DelimiterDemo::scannerWithDelimiter, "1aaaaaaa2aa3aaa4", "a+", Arrays.asList("1", "2", "3", "4")); + } + + @Test + void givenSimpleCharacterDelimiter_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() { + checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "Welcome to Baeldung", Pattern.compile("\\s"), Arrays.asList("Welcome", "to", "Baeldung")); + } + + @Test + void givenStringDelimiter_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() { + checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "HelloBaeldungHelloWorld", Pattern.compile("Hello"), Arrays.asList("Baeldung", "World")); + } + + @Test + void givenVariousPossibleDelimiters_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() { + checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "Welcome to Baeldung.\nThank you for reading.\nThe team", Pattern.compile("\n|\\s"), Arrays.asList("Welcome", "to", "Baeldung.", "Thank", "you", "for", "reading.", "The", "team")); + } + + @Test + void givenWildcardRegexDelimiters_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() { + checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "1aaaaaaa2aa3aaa4", Pattern.compile("a*"), Arrays.asList("1", "2", "3", "4")); + } + + void checkOutput(BiFunction> function, String input, String delimiter, List expectedOutput) { + assertEquals(expectedOutput, function.apply(input, delimiter)); + } + + void checkOutput(BiFunction> function, String input, Pattern delimiter, List expectedOutput) { + assertEquals(expectedOutput, function.apply(input, delimiter)); + } + + @Test + void whenBaseScanner_ThenWhitespacesAreUsedAsDelimiters() { + assertEquals(List.of("Welcome", "at", "Baeldung"), DelimiterDemo.baseScanner("Welcome at Baeldung")); + } + +} From f5f6459f566eb58c6fcf66f43623d217648fd733 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Mon, 16 May 2022 15:04:10 +0300 Subject: [PATCH 72/94] BAEL-5342 fix live test --- resteasy/pom.xml | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/resteasy/pom.xml b/resteasy/pom.xml index e4ab6d84b2..a8c1f2815c 100644 --- a/resteasy/pom.xml +++ b/resteasy/pom.xml @@ -20,12 +20,6 @@ org.jboss.resteasy resteasy-servlet-initializer ${resteasy.version} - - - commons-logging - commons-logging - - org.jboss.resteasy @@ -44,10 +38,11 @@ ${resteasy.version} - commons-io - commons-io - ${commons-io.version} + javax.servlet + javax.servlet-api + 4.0.1 + From 365f029e3a3fa5b22fd89c47d8b87254e0ccaf8a Mon Sep 17 00:00:00 2001 From: Alireza Ghasemi Date: Mon, 16 May 2022 17:34:55 +0200 Subject: [PATCH 73/94] Add tutorial files for ResultSet2JSON Article (#11912) * Add tutorial files * Move script to new folder * Use Maven/H2 instead of JBang/DuckDB * Use Java 8 * Usen an older versio of JOOQ * Format according to Beldung Intellij guide * Remove dangling commit * Use 2-space indentation * Apply formatting from Eclipse * Add unit test * Add assertion * Change test names * Change method names Co-authored-by: root Co-authored-by: Alireza Ghasemi --- .../core-java-persistence-2/example.csv | 4 + .../core-java-persistence-2/pom.xml | 18 ++- .../resultset2json/ResultSet2JSON.java | 137 ++++++++++++++++++ .../ResultSet2JSONUnitTest.java | 75 ++++++++++ 4 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 persistence-modules/core-java-persistence-2/example.csv create mode 100755 persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/resultset2json/ResultSet2JSON.java create mode 100644 persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/resultset2json/ResultSet2JSONUnitTest.java diff --git a/persistence-modules/core-java-persistence-2/example.csv b/persistence-modules/core-java-persistence-2/example.csv new file mode 100644 index 0000000000..d7715864d1 --- /dev/null +++ b/persistence-modules/core-java-persistence-2/example.csv @@ -0,0 +1,4 @@ +Username,Id,First name,Last name +doe1,7173,John,Doe +smith3,3722,Dana,Smith +john22,5490,John,Wang \ No newline at end of file diff --git a/persistence-modules/core-java-persistence-2/pom.xml b/persistence-modules/core-java-persistence-2/pom.xml index 780c1fcfca..c7547e1c46 100644 --- a/persistence-modules/core-java-persistence-2/pom.xml +++ b/persistence-modules/core-java-persistence-2/pom.xml @@ -1,7 +1,5 @@ - + 4.0.0 com.baeldung.core-java-persistence-2 core-java-persistence-2 @@ -41,6 +39,20 @@ mssql-jdbc ${mssql.driver.version} + + + + org.jooq + jooq + 3.11.11 + + + + org.json + json + 20220320 + + diff --git a/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/resultset2json/ResultSet2JSON.java b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/resultset2json/ResultSet2JSON.java new file mode 100755 index 0000000000..bbe3ccf9a0 --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/resultset2json/ResultSet2JSON.java @@ -0,0 +1,137 @@ +package com.baeldung.resultset2json; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.jooq.Record; +import org.jooq.RecordMapper; +import org.jooq.impl.DSL; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONArray; + +public class ResultSet2JSON { + + public static void main(String... args) throws ClassNotFoundException, SQLException { + + ResultSet2JSON testClass = new ResultSet2JSON(); + testClass.convertWithoutJOOQ(); + } + + public void convertWithoutJOOQ() throws ClassNotFoundException, SQLException { + Class.forName("org.h2.Driver"); + Connection dbConnection = DriverManager.getConnection("jdbc:h2:mem:rs2jdbc", "user", "password"); + + // Create a table + Statement stmt = dbConnection.createStatement(); + stmt.execute("CREATE TABLE words AS SELECT * FROM CSVREAD('./example.csv')"); + ResultSet resultSet = stmt.executeQuery("SELECT * FROM words"); + + JSONArray result1 = resultSet2JdbcWithoutJOOQ(resultSet); + System.out.println(result1); + + resultSet.close(); + } + + public void convertUsingJOOQDefaultApproach() throws ClassNotFoundException, SQLException { + Class.forName("org.h2.Driver"); + Connection dbConnection = DriverManager.getConnection("jdbc:h2:mem:rs2jdbc", "user", "password"); + // Create a table + Statement stmt = dbConnection.createStatement(); + stmt.execute("CREATE TABLE words AS SELECT * FROM CSVREAD('./example.csv')"); + ResultSet resultSet = stmt.executeQuery("SELECT * FROM words"); + + JSONObject result1 = resultSet2JdbcUsingJOOQDefaultApproach(resultSet, dbConnection); + System.out.println(result1); + + resultSet.close(); + } + + public void convertUsingCustomisedJOOQ() throws ClassNotFoundException, SQLException { + Class.forName("org.h2.Driver"); + Connection dbConnection = DriverManager.getConnection("jdbc:h2:mem:rs2jdbc", "user", "password"); + // Create a table + Statement stmt = dbConnection.createStatement(); + stmt.execute("CREATE TABLE words AS SELECT * FROM CSVREAD('./example.csv')"); + ResultSet resultSet = stmt.executeQuery("SELECT * FROM words"); + + JSONArray result1 = resultSet2JdbcUsingCustomisedJOOQ(resultSet, dbConnection); + System.out.println(result1); + + resultSet.close(); + } + + public static JSONArray resultSet2JdbcWithoutJOOQ(ResultSet resultSet) throws SQLException { + ResultSetMetaData md = resultSet.getMetaData(); + int numCols = md.getColumnCount(); + List colNames = IntStream.range(0, numCols) + .mapToObj(i -> { + try { + return md.getColumnName(i + 1); + } catch (SQLException e) { + + e.printStackTrace(); + return "?"; + } + }) + .collect(Collectors.toList()); + + JSONArray result = new JSONArray(); + while (resultSet.next()) { + JSONObject row = new JSONObject(); + colNames.forEach(cn -> { + try { + row.put(cn, resultSet.getObject(cn)); + } catch (JSONException | SQLException e) { + + e.printStackTrace(); + } + }); + result.put(row); + } + return result; + } + + public static JSONObject resultSet2JdbcUsingJOOQDefaultApproach(ResultSet resultSet, Connection dbConnection) throws SQLException { + JSONObject result = new JSONObject(DSL.using(dbConnection) + .fetch(resultSet) + .formatJSON()); + return result; + } + + public static JSONArray resultSet2JdbcUsingCustomisedJOOQ(ResultSet resultSet, Connection dbConnection) throws SQLException { + ResultSetMetaData md = resultSet.getMetaData(); + int numCols = md.getColumnCount(); + List colNames = IntStream.range(0, numCols) + .mapToObj(i -> { + try { + return md.getColumnName(i + 1); + } catch (SQLException e) { + + e.printStackTrace(); + return "?"; + } + }) + .collect(Collectors.toList()); + + List json = DSL.using(dbConnection) + .fetch(resultSet) + .map(new RecordMapper() { + + @Override + public JSONObject map(Record r) { + JSONObject obj = new JSONObject(); + colNames.forEach(cn -> obj.put(cn, r.get(cn))); + return obj; + } + }); + return new JSONArray(json); + } +} diff --git a/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/resultset2json/ResultSet2JSONUnitTest.java b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/resultset2json/ResultSet2JSONUnitTest.java new file mode 100644 index 0000000000..f3dd8350fa --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/resultset2json/ResultSet2JSONUnitTest.java @@ -0,0 +1,75 @@ +package com.baeldung.resultset2json; + +import static com.baeldung.resultset2json.ResultSet2JSON.resultSet2JdbcWithoutJOOQ; +import static com.baeldung.resultset2json.ResultSet2JSON.resultSet2JdbcUsingJOOQDefaultApproach; +import static com.baeldung.resultset2json.ResultSet2JSON.resultSet2JdbcUsingCustomisedJOOQ; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ResultSet2JSONUnitTest { + JSONObject object = new JSONObject( + "{\"records\":[[\"doe1\",\"7173\",\"John\",\"Doe\"],[\"smith3\",\"3722\",\"Dana\",\"Smith\"],[\"john22\",\"5490\",\"John\",\"Wang\"]],\"fields\":[{\"schema\":\"PUBLIC\",\"name\":\"USERNAME\",\"type\":\"VARCHAR\",\"table\":\"WORDS\"},{\"schema\":\"PUBLIC\",\"name\":\"ID\",\"type\":\"VARCHAR\",\"table\":\"WORDS\"},{\"schema\":\"PUBLIC\",\"name\":\"First name\",\"type\":\"VARCHAR\",\"table\":\"WORDS\"},{\"schema\":\"PUBLIC\",\"name\":\"Last name\",\"type\":\"VARCHAR\",\"table\":\"WORDS\"}]}"); + + JSONArray array = new JSONArray( + "[{\"USERNAME\":\"doe1\",\"First name\":\"John\",\"ID\":\"7173\",\"Last name\":\"Doe\"},{\"USERNAME\":\"smith3\",\"First name\":\"Dana\",\"ID\":\"3722\",\"Last name\":\"Smith\"},{\"USERNAME\":\"john22\",\"First name\":\"John\",\"ID\":\"5490\",\"Last name\":\"Wang\"}]"); + + @Test + void whenResultSetConvertedWithoutJOOQ_shouldMatchJSON() throws SQLException, ClassNotFoundException { + Class.forName("org.h2.Driver"); + Connection dbConnection = DriverManager.getConnection("jdbc:h2:mem:rs2jdbc1", "user", "password"); + + // Create a table + Statement stmt = dbConnection.createStatement(); + stmt.execute("CREATE TABLE words AS SELECT * FROM CSVREAD('./example.csv')"); + ResultSet resultSet = stmt.executeQuery("SELECT * FROM words"); + + JSONArray result1 = resultSet2JdbcWithoutJOOQ(resultSet); + + resultSet.close(); + + assertTrue(array.similar(result1)); + } + + @Test + void whenResultSetConvertedUsingJOOQDefaultApproach_shouldMatchJSON() throws SQLException, ClassNotFoundException { + Class.forName("org.h2.Driver"); + Connection dbConnection = DriverManager.getConnection("jdbc:h2:mem:rs2jdbc2", "user", "password"); + // Create a table + Statement stmt = dbConnection.createStatement(); + stmt.execute("CREATE TABLE words AS SELECT * FROM CSVREAD('./example.csv')"); + ResultSet resultSet = stmt.executeQuery("SELECT * FROM words"); + + JSONObject result2 = resultSet2JdbcUsingJOOQDefaultApproach(resultSet, dbConnection); + + resultSet.close(); + + assertTrue(object.similar(result2)); + } + + @Test + void whenResultSetConvertedUsingCustomisedJOOQ_shouldMatchJSON() throws SQLException, ClassNotFoundException { + Class.forName("org.h2.Driver"); + Connection dbConnection = DriverManager.getConnection("jdbc:h2:mem:rs2jdbc3", "user", "password"); + // Create a table + Statement stmt = dbConnection.createStatement(); + stmt.execute("CREATE TABLE words AS SELECT * FROM CSVREAD('./example.csv')"); + ResultSet resultSet = stmt.executeQuery("SELECT * FROM words"); + + JSONArray result3 = resultSet2JdbcUsingCustomisedJOOQ(resultSet, dbConnection); + + resultSet.close(); + + assertTrue(array.similar(result3)); + } + +} From 2065e6b624b9ed69868a2a8df321c252dadc7a90 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Mon, 16 May 2022 22:25:29 +0530 Subject: [PATCH 74/94] JAVA-11790: Fix references to parents --- apache-tapestry/pom.xml | 7 ++++++- .../aws-lambda/shipping-tracker/ShippingFunction/pom.xml | 7 +++++++ libraries-primitive/pom.xml | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/apache-tapestry/pom.xml b/apache-tapestry/pom.xml index 7a4c2b53b5..201b807d9f 100644 --- a/apache-tapestry/pom.xml +++ b/apache-tapestry/pom.xml @@ -3,12 +3,17 @@ xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - com.baeldung apache-tapestry 0.0.1-SNAPSHOT apache-tapestry war + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + From 18237d20979d94f3868e0053c8348366373cd562 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Mon, 16 May 2022 23:23:26 -0300 Subject: [PATCH 75/94] removing from the old module --- .../baeldung/boot/collection/name/Naming.java | 35 -------- .../SpringBootCollectionNameApplication.java | 19 ----- .../name/dao/CompilationRepository.java | 9 --- .../name/dao/MusicAlbumRepository.java | 9 --- .../name/dao/MusicTrackRepository.java | 9 --- .../collection/name/dao/StoreRepository.java | 9 --- .../collection/name/data/Compilation.java | 36 --------- .../boot/collection/name/data/MusicAlbum.java | 48 ----------- .../boot/collection/name/data/MusicTrack.java | 50 ------------ .../boot/collection/name/data/Store.java | 36 --------- .../name/service/MusicStoreService.java | 62 -------------- .../name/web/CollectionController.java | 24 ------ .../name/web/MusicStoreController.java | 63 --------------- .../boot.collection.name/app.properties | 1 - .../MusicStoreServiceIntegrationTest.java | 81 ------------------- 15 files changed, 491 deletions(-) delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/Naming.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Store.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/resources/boot.collection.name/app.properties delete mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/Naming.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/Naming.java deleted file mode 100644 index 9808ecccb6..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/Naming.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.baeldung.boot.collection.name; - -import java.util.ArrayList; -import java.util.List; - -import org.springframework.data.util.ParsingUtils; -import org.springframework.util.StringUtils; - -public class Naming { - public static void main(String[] args) { - String r = new Naming().fix(args[0]); - System.out.println(r); - } - - public String fix(String name) { - List parts = ParsingUtils.splitCamelCaseToLower(name); - List result = new ArrayList<>(); - - for (String part : parts) { - if (StringUtils.hasText(part)) { - result.add(part); - } - } - - return StringUtils.collectionToDelimitedString(result, "_"); - } - - public String convert(Class type) { - return fix(type.getSimpleName()); - } - - public String convert(Object instance) { - return convert(instance.getClass()); - } -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java deleted file mode 100644 index 0a5c36db29..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.boot.collection.name; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.PropertySource; - -@SpringBootApplication -@PropertySource("classpath:boot.collection.name/app.properties") -public class SpringBootCollectionNameApplication { - public static void main(String... args) { - SpringApplication.run(SpringBootCollectionNameApplication.class, args); - } - - @Bean - public Naming naming() { - return new Naming(); - } -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java deleted file mode 100644 index 3f83ad4548..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.boot.collection.name.dao; - -import org.springframework.data.mongodb.repository.MongoRepository; - -import com.baeldung.boot.collection.name.data.Compilation; - -public interface CompilationRepository extends MongoRepository { - -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java deleted file mode 100644 index 98709361d7..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.boot.collection.name.dao; - -import org.springframework.data.mongodb.repository.MongoRepository; - -import com.baeldung.boot.collection.name.data.MusicAlbum; - -public interface MusicAlbumRepository extends MongoRepository { - -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java deleted file mode 100644 index 0964a8de00..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.boot.collection.name.dao; - -import org.springframework.data.mongodb.repository.MongoRepository; - -import com.baeldung.boot.collection.name.data.MusicTrack; - -public interface MusicTrackRepository extends MongoRepository { - -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java deleted file mode 100644 index b446a7d98d..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.boot.collection.name.dao; - -import org.springframework.data.mongodb.repository.MongoRepository; - -import com.baeldung.boot.collection.name.data.Store; - -public interface StoreRepository extends MongoRepository { - -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java deleted file mode 100644 index ce081acf25..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.boot.collection.name.data; - -import org.springframework.data.annotation.Id; -import org.springframework.data.mongodb.core.mapping.Document; - -@Document -public class Compilation { - @Id - private String id; - - private String name; - - public Compilation() { - } - - public Compilation(String name) { - super(); - this.name = name; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java deleted file mode 100644 index ce2e084504..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.boot.collection.name.data; - -import org.springframework.data.annotation.Id; -import org.springframework.data.mongodb.core.mapping.Document; - -@Document("albums") -public class MusicAlbum { - @Id - private String id; - - private String name; - - private String artist; - - public MusicAlbum() { - - } - - public MusicAlbum(String name, String artist) { - super(); - this.name = name; - this.artist = artist; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getArtist() { - return artist; - } - - public void setArtist(String artist) { - this.artist = artist; - } -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java deleted file mode 100644 index 39ce3994bb..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.boot.collection.name.data; - -import org.springframework.data.annotation.Id; -import org.springframework.data.mongodb.core.mapping.Document; - -@Document("#{@naming.fix('MusicTrack')}") -public class MusicTrack { - @Id - private String id; - - private String name; - - private String artist; - - public MusicTrack() { - } - - public MusicTrack(String name, String artist) { - this.name = name; - this.artist = artist; - } - - public MusicTrack(String name) { - this.name = name; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getArtist() { - return artist; - } - - public void setArtist(String artist) { - this.artist = artist; - } -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Store.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Store.java deleted file mode 100644 index 83f5017a13..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/data/Store.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.boot.collection.name.data; - -import org.springframework.data.annotation.Id; -import org.springframework.data.mongodb.core.mapping.Document; - -@Document("store-#{@environment.getProperty('collection.suffix')}") -public class Store { - @Id - private String id; - - private String name; - - public Store() { - } - - public Store(String name) { - super(); - this.name = name; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java deleted file mode 100644 index 6083e3d0c3..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.baeldung.boot.collection.name.service; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.baeldung.boot.collection.name.dao.CompilationRepository; -import com.baeldung.boot.collection.name.dao.MusicAlbumRepository; -import com.baeldung.boot.collection.name.dao.MusicTrackRepository; -import com.baeldung.boot.collection.name.dao.StoreRepository; -import com.baeldung.boot.collection.name.data.Compilation; -import com.baeldung.boot.collection.name.data.MusicAlbum; -import com.baeldung.boot.collection.name.data.MusicTrack; -import com.baeldung.boot.collection.name.data.Store; - -@Service -public class MusicStoreService { - @Autowired - private MusicAlbumRepository albumRepository; - - @Autowired - private CompilationRepository compilationRepository; - - @Autowired - private StoreRepository storeRepository; - - @Autowired - private MusicTrackRepository trackRepository; - - public MusicAlbum add(MusicAlbum item) { - return albumRepository.save(item); - } - - public List getAlbumList() { - return albumRepository.findAll(); - } - - public Compilation add(Compilation item) { - return compilationRepository.save(item); - } - - public List getCompilationList() { - return compilationRepository.findAll(); - } - - public Store add(Store item) { - return storeRepository.save(item); - } - - public List getStoreList() { - return storeRepository.findAll(); - } - - public MusicTrack add(MusicTrack item) { - return trackRepository.save(item); - } - - public List getTrackList() { - return trackRepository.findAll(); - } -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java deleted file mode 100644 index 2efca361b9..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.boot.collection.name.web; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.mongodb.DBObject; - -@RestController -@RequestMapping("/collection") -public class CollectionController { - @Autowired - private MongoTemplate mongo; - - @GetMapping("/{name}") - public List get(@PathVariable String name) { - return mongo.findAll(DBObject.class, name); - } -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java deleted file mode 100644 index 8c510121c2..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.boot.collection.name.web; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.baeldung.boot.collection.name.data.Compilation; -import com.baeldung.boot.collection.name.data.MusicAlbum; -import com.baeldung.boot.collection.name.data.MusicTrack; -import com.baeldung.boot.collection.name.data.Store; -import com.baeldung.boot.collection.name.service.MusicStoreService; - -@RestController -@RequestMapping("/music") -public class MusicStoreController { - @Autowired - private MusicStoreService service; - - @PostMapping("/album") - public MusicAlbum post(@RequestBody MusicAlbum item) { - return service.add(item); - } - - @GetMapping("/album") - public List getAlbumList() { - return service.getAlbumList(); - } - - @PostMapping("/compilation") - public Compilation post(@RequestBody Compilation item) { - return service.add(item); - } - - @GetMapping("/compilation") - public List getCompilationList() { - return service.getCompilationList(); - } - - @PostMapping("/store") - public Store post(@RequestBody Store item) { - return service.add(item); - } - - @GetMapping("/store") - public List getStoreList() { - return service.getStoreList(); - } - - @PostMapping("/track") - public MusicTrack post(@RequestBody MusicTrack item) { - return service.add(item); - } - - @GetMapping("/track") - public List getTrackList() { - return service.getTrackList(); - } -} diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/boot.collection.name/app.properties b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/boot.collection.name/app.properties deleted file mode 100644 index 98945a76e1..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/boot.collection.name/app.properties +++ /dev/null @@ -1 +0,0 @@ -collection.suffix=db diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java deleted file mode 100644 index eda8b8aafb..0000000000 --- a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.baeldung.boot.collection.name.service; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.List; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringRunner; - -import com.baeldung.boot.collection.name.data.Compilation; -import com.baeldung.boot.collection.name.data.MusicAlbum; -import com.baeldung.boot.collection.name.data.MusicTrack; -import com.baeldung.boot.collection.name.data.Store; - -@SpringBootTest -@DirtiesContext -@RunWith(SpringRunner.class) -public class MusicStoreServiceIntegrationTest { - @Autowired - private MusicStoreService service; - - @Autowired - private MongoTemplate mongoDb; - - @Test - public void givenAnnotation_whenSearchingByCollectionName_thenFound() { - List list = service.getCompilationList(); - int sizeBefore = list.size(); - - service.add(new Compilation("Spring Hits")); - - list = mongoDb.findAll(Compilation.class, "compilation"); - int sizeAfter = list.size(); - - assertThat(sizeAfter - sizeBefore).isEqualTo(1); - } - - @Test - public void givenAnnotationWithValue_whenSearchingByCollectionName_thenFound() { - List list = service.getAlbumList(); - int sizeBefore = list.size(); - - service.add(new MusicAlbum("Album 1", "Artist A")); - - list = mongoDb.findAll(MusicAlbum.class, "albums"); - int sizeAfter = list.size(); - - assertThat(sizeAfter - sizeBefore).isEqualTo(1); - } - - @Test - public void givenAnnotationWithSpELEnvironment_whenSearchingByCollectionName_thenFound() { - List list = service.getStoreList(); - int sizeBefore = list.size(); - - service.add(new Store("Store A")); - - list = mongoDb.findAll(Store.class, "store-db"); - int sizeAfter = list.size(); - - assertThat(sizeAfter - sizeBefore).isEqualTo(1); - } - - @Test - public void givenAnnotationWithSpELBean_whenSearchingByCollectionName_thenFound() { - List list = service.getTrackList(); - int sizeBefore = list.size(); - - service.add(new MusicTrack("Track 1")); - - list = mongoDb.findAll(MusicTrack.class, "music_track"); - int sizeAfter = list.size(); - - assertThat(sizeAfter - sizeBefore).isEqualTo(1); - } -} From 16bb4772ec608cd91845274dac5eb1671f89de18 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Mon, 16 May 2022 23:24:46 -0300 Subject: [PATCH 76/94] moved code to spring-boot-persistence-mongodb-2 --- .../spring-boot-persistence-mongodb-2/pom.xml | 4 + .../baeldung/boot/collection/name/Naming.java | 35 ++++++++ .../SpringBootCollectionNameApplication.java | 19 +++++ .../name/dao/CompilationRepository.java | 9 +++ .../name/dao/MusicAlbumRepository.java | 9 +++ .../name/dao/MusicTrackRepository.java | 9 +++ .../collection/name/dao/StoreRepository.java | 9 +++ .../collection/name/data/Compilation.java | 36 +++++++++ .../boot/collection/name/data/MusicAlbum.java | 48 +++++++++++ .../boot/collection/name/data/MusicTrack.java | 50 ++++++++++++ .../boot/collection/name/data/Store.java | 36 +++++++++ .../name/service/MusicStoreService.java | 62 ++++++++++++++ .../name/web/CollectionController.java | 24 ++++++ .../name/web/MusicStoreController.java | 63 +++++++++++++++ .../boot.collection.name/app.properties | 1 + .../MusicStoreServiceIntegrationTest.java | 81 +++++++++++++++++++ 16 files changed, 495 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/Naming.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Store.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/boot.collection.name/app.properties create mode 100644 persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml b/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml index a6ac4987a1..a172c28a80 100644 --- a/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml +++ b/persistence-modules/spring-boot-persistence-mongodb-2/pom.xml @@ -16,6 +16,10 @@ + + org.springframework.boot + spring-boot-starter-web + org.springframework.boot spring-boot-starter-data-mongodb diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/Naming.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/Naming.java new file mode 100644 index 0000000000..9808ecccb6 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/Naming.java @@ -0,0 +1,35 @@ +package com.baeldung.boot.collection.name; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.data.util.ParsingUtils; +import org.springframework.util.StringUtils; + +public class Naming { + public static void main(String[] args) { + String r = new Naming().fix(args[0]); + System.out.println(r); + } + + public String fix(String name) { + List parts = ParsingUtils.splitCamelCaseToLower(name); + List result = new ArrayList<>(); + + for (String part : parts) { + if (StringUtils.hasText(part)) { + result.add(part); + } + } + + return StringUtils.collectionToDelimitedString(result, "_"); + } + + public String convert(Class type) { + return fix(type.getSimpleName()); + } + + public String convert(Object instance) { + return convert(instance.getClass()); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java new file mode 100644 index 0000000000..0a5c36db29 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java @@ -0,0 +1,19 @@ +package com.baeldung.boot.collection.name; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; + +@SpringBootApplication +@PropertySource("classpath:boot.collection.name/app.properties") +public class SpringBootCollectionNameApplication { + public static void main(String... args) { + SpringApplication.run(SpringBootCollectionNameApplication.class, args); + } + + @Bean + public Naming naming() { + return new Naming(); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java new file mode 100644 index 0000000000..3f83ad4548 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/CompilationRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.Compilation; + +public interface CompilationRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java new file mode 100644 index 0000000000..98709361d7 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicAlbumRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.MusicAlbum; + +public interface MusicAlbumRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java new file mode 100644 index 0000000000..0964a8de00 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/MusicTrackRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.MusicTrack; + +public interface MusicTrackRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java new file mode 100644 index 0000000000..b446a7d98d --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/dao/StoreRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.boot.collection.name.dao; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import com.baeldung.boot.collection.name.data.Store; + +public interface StoreRepository extends MongoRepository { + +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java new file mode 100644 index 0000000000..ce081acf25 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Compilation.java @@ -0,0 +1,36 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class Compilation { + @Id + private String id; + + private String name; + + public Compilation() { + } + + public Compilation(String name) { + super(); + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java new file mode 100644 index 0000000000..ce2e084504 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicAlbum.java @@ -0,0 +1,48 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("albums") +public class MusicAlbum { + @Id + private String id; + + private String name; + + private String artist; + + public MusicAlbum() { + + } + + public MusicAlbum(String name, String artist) { + super(); + this.name = name; + this.artist = artist; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getArtist() { + return artist; + } + + public void setArtist(String artist) { + this.artist = artist; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java new file mode 100644 index 0000000000..39ce3994bb --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/MusicTrack.java @@ -0,0 +1,50 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("#{@naming.fix('MusicTrack')}") +public class MusicTrack { + @Id + private String id; + + private String name; + + private String artist; + + public MusicTrack() { + } + + public MusicTrack(String name, String artist) { + this.name = name; + this.artist = artist; + } + + public MusicTrack(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getArtist() { + return artist; + } + + public void setArtist(String artist) { + this.artist = artist; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Store.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Store.java new file mode 100644 index 0000000000..83f5017a13 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/data/Store.java @@ -0,0 +1,36 @@ +package com.baeldung.boot.collection.name.data; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document("store-#{@environment.getProperty('collection.suffix')}") +public class Store { + @Id + private String id; + + private String name; + + public Store() { + } + + public Store(String name) { + super(); + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java new file mode 100644 index 0000000000..6083e3d0c3 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/service/MusicStoreService.java @@ -0,0 +1,62 @@ +package com.baeldung.boot.collection.name.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.boot.collection.name.dao.CompilationRepository; +import com.baeldung.boot.collection.name.dao.MusicAlbumRepository; +import com.baeldung.boot.collection.name.dao.MusicTrackRepository; +import com.baeldung.boot.collection.name.dao.StoreRepository; +import com.baeldung.boot.collection.name.data.Compilation; +import com.baeldung.boot.collection.name.data.MusicAlbum; +import com.baeldung.boot.collection.name.data.MusicTrack; +import com.baeldung.boot.collection.name.data.Store; + +@Service +public class MusicStoreService { + @Autowired + private MusicAlbumRepository albumRepository; + + @Autowired + private CompilationRepository compilationRepository; + + @Autowired + private StoreRepository storeRepository; + + @Autowired + private MusicTrackRepository trackRepository; + + public MusicAlbum add(MusicAlbum item) { + return albumRepository.save(item); + } + + public List getAlbumList() { + return albumRepository.findAll(); + } + + public Compilation add(Compilation item) { + return compilationRepository.save(item); + } + + public List getCompilationList() { + return compilationRepository.findAll(); + } + + public Store add(Store item) { + return storeRepository.save(item); + } + + public List getStoreList() { + return storeRepository.findAll(); + } + + public MusicTrack add(MusicTrack item) { + return trackRepository.save(item); + } + + public List getTrackList() { + return trackRepository.findAll(); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java new file mode 100644 index 0000000000..2efca361b9 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/CollectionController.java @@ -0,0 +1,24 @@ +package com.baeldung.boot.collection.name.web; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.mongodb.DBObject; + +@RestController +@RequestMapping("/collection") +public class CollectionController { + @Autowired + private MongoTemplate mongo; + + @GetMapping("/{name}") + public List get(@PathVariable String name) { + return mongo.findAll(DBObject.class, name); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java new file mode 100644 index 0000000000..8c510121c2 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/web/MusicStoreController.java @@ -0,0 +1,63 @@ +package com.baeldung.boot.collection.name.web; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.boot.collection.name.data.Compilation; +import com.baeldung.boot.collection.name.data.MusicAlbum; +import com.baeldung.boot.collection.name.data.MusicTrack; +import com.baeldung.boot.collection.name.data.Store; +import com.baeldung.boot.collection.name.service.MusicStoreService; + +@RestController +@RequestMapping("/music") +public class MusicStoreController { + @Autowired + private MusicStoreService service; + + @PostMapping("/album") + public MusicAlbum post(@RequestBody MusicAlbum item) { + return service.add(item); + } + + @GetMapping("/album") + public List getAlbumList() { + return service.getAlbumList(); + } + + @PostMapping("/compilation") + public Compilation post(@RequestBody Compilation item) { + return service.add(item); + } + + @GetMapping("/compilation") + public List getCompilationList() { + return service.getCompilationList(); + } + + @PostMapping("/store") + public Store post(@RequestBody Store item) { + return service.add(item); + } + + @GetMapping("/store") + public List getStoreList() { + return service.getStoreList(); + } + + @PostMapping("/track") + public MusicTrack post(@RequestBody MusicTrack item) { + return service.add(item); + } + + @GetMapping("/track") + public List getTrackList() { + return service.getTrackList(); + } +} diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/boot.collection.name/app.properties b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/boot.collection.name/app.properties new file mode 100644 index 0000000000..98945a76e1 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/resources/boot.collection.name/app.properties @@ -0,0 +1 @@ +collection.suffix=db diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java new file mode 100644 index 0000000000..eda8b8aafb --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/test/java/com/baeldung/boot/collection/name/service/MusicStoreServiceIntegrationTest.java @@ -0,0 +1,81 @@ +package com.baeldung.boot.collection.name.service; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.boot.collection.name.data.Compilation; +import com.baeldung.boot.collection.name.data.MusicAlbum; +import com.baeldung.boot.collection.name.data.MusicTrack; +import com.baeldung.boot.collection.name.data.Store; + +@SpringBootTest +@DirtiesContext +@RunWith(SpringRunner.class) +public class MusicStoreServiceIntegrationTest { + @Autowired + private MusicStoreService service; + + @Autowired + private MongoTemplate mongoDb; + + @Test + public void givenAnnotation_whenSearchingByCollectionName_thenFound() { + List list = service.getCompilationList(); + int sizeBefore = list.size(); + + service.add(new Compilation("Spring Hits")); + + list = mongoDb.findAll(Compilation.class, "compilation"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } + + @Test + public void givenAnnotationWithValue_whenSearchingByCollectionName_thenFound() { + List list = service.getAlbumList(); + int sizeBefore = list.size(); + + service.add(new MusicAlbum("Album 1", "Artist A")); + + list = mongoDb.findAll(MusicAlbum.class, "albums"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } + + @Test + public void givenAnnotationWithSpELEnvironment_whenSearchingByCollectionName_thenFound() { + List list = service.getStoreList(); + int sizeBefore = list.size(); + + service.add(new Store("Store A")); + + list = mongoDb.findAll(Store.class, "store-db"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } + + @Test + public void givenAnnotationWithSpELBean_whenSearchingByCollectionName_thenFound() { + List list = service.getTrackList(); + int sizeBefore = list.size(); + + service.add(new MusicTrack("Track 1")); + + list = mongoDb.findAll(MusicTrack.class, "music_track"); + int sizeAfter = list.size(); + + assertThat(sizeAfter - sizeBefore).isEqualTo(1); + } +} From d848af386a0cc984ed2fe596b590301fd252e364 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 17 May 2022 09:44:35 +0500 Subject: [PATCH 77/94] Updated README.md added link back to the article: https://www.baeldung.com/java-httpclient-post --- core-java-modules/core-java-httpclient/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-httpclient/README.md b/core-java-modules/core-java-httpclient/README.md index 24ff7d9941..712328a123 100644 --- a/core-java-modules/core-java-httpclient/README.md +++ b/core-java-modules/core-java-httpclient/README.md @@ -3,4 +3,4 @@ This module contains articles about Java HttpClient ### Relevant articles -- TODO +- [Posting with Java HttpClient](https://www.baeldung.com/java-httpclient-post) From bb7edc30e4f1644dc412c6a4aa493b8f200f7f14 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Tue, 17 May 2022 09:58:36 +0500 Subject: [PATCH 78/94] Updated README.md added link back to the article: https://www.baeldung.com/java-reverse-number --- java-numbers-4/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/java-numbers-4/README.md b/java-numbers-4/README.md index c18a5ebe6f..2a77992c8f 100644 --- a/java-numbers-4/README.md +++ b/java-numbers-4/README.md @@ -8,3 +8,4 @@ - [Convert Byte Size Into a Human-Readable Format in Java](https://www.baeldung.com/java-human-readable-byte-size) - [Convert boolean to int in Java](https://www.baeldung.com/java-boolean-to-int) - [Generate a Random Value From an Enum](https://www.baeldung.com/java-enum-random-value) +- [Reverse a Number in Java](https://www.baeldung.com/java-reverse-number) From 177f912393c0e0110a9b9b01862d7a5ff053508a Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Tue, 17 May 2022 16:07:21 +0100 Subject: [PATCH 79/94] Source code for Lightrun articles --- lightrun/README.md | 29 +++++++ lightrun/api-service/.gitignore | 33 +++++++ lightrun/api-service/pom.xml | 45 ++++++++++ .../apiservice/ApiServiceApplication.java | 13 +++ .../apiservice/RequestIdGenerator.java | 33 +++++++ .../apiservice/RestTemplateConfig.java | 20 +++++ .../com/baeldung/apiservice/WebConfig.java | 17 ++++ .../adapters/http/TaskResponse.java | 11 +++ .../adapters/http/TasksController.java | 54 ++++++++++++ .../adapters/http/UnknownTaskException.java | 4 + .../adapters/http/UserResponse.java | 4 + .../apiservice/adapters/tasks/Task.java | 11 +++ .../adapters/tasks/TaskRepository.java | 30 +++++++ .../apiservice/adapters/users/User.java | 4 + .../adapters/users/UserRepository.java | 31 +++++++ .../src/main/resources/application.properties | 2 + lightrun/pom.xml | 17 ++++ lightrun/tasks-service/.gitignore | 33 +++++++ lightrun/tasks-service/pom.xml | 67 +++++++++++++++ .../baeldung/tasksservice/ArtemisConfig.java | 23 +++++ .../tasksservice/TasksServiceApplication.java | 13 +++ .../adapters/http/CreateTaskRequest.java | 15 ++++ .../adapters/http/PatchTaskRequest.java | 17 ++++ .../adapters/http/TaskResponse.java | 22 +++++ .../adapters/http/TasksController.java | 86 +++++++++++++++++++ .../adapters/http/http-client.env.json | 5 ++ .../tasksservice/adapters/http/tasks.http | 35 ++++++++ .../adapters/jms/JmsConsumer.java | 18 ++++ .../adapters/repository/TaskRecord.java | 80 +++++++++++++++++ .../adapters/repository/TasksRepository.java | 28 ++++++ .../service/DeletedUserService.java | 27 ++++++ .../tasksservice/service/TasksService.java | 72 ++++++++++++++++ .../service/UnknownTaskException.java | 24 ++++++ .../src/main/resources/application.properties | 13 +++ .../migration/V1_0_0__create-tasks-table.sql | 13 +++ lightrun/users-service/.gitignore | 33 +++++++ lightrun/users-service/README.md | 29 +++++++ lightrun/users-service/pom.xml | 63 ++++++++++++++ .../com/baeldung/usersservice/JmsConfig.java | 29 +++++++ .../usersservice/UsersServiceApplication.java | 13 +++ .../adapters/http/CreateUserRequest.java | 15 ++++ .../adapters/http/PatchUserRequest.java | 17 ++++ .../adapters/http/UserResponse.java | 16 ++++ .../adapters/http/UsersController.java | 69 +++++++++++++++ .../adapters/http/http-client.env.json | 5 ++ .../usersservice/adapters/http/users.http | 25 ++++++ .../usersservice/adapters/jms/JmsSender.java | 26 ++++++ .../adapters/repository/UserRecord.java | 47 ++++++++++ .../adapters/repository/UsersRepository.java | 19 ++++ .../service/UnknownUserException.java | 24 ++++++ .../usersservice/service/UsersService.java | 61 +++++++++++++ .../src/main/resources/application.properties | 10 +++ .../migration/V1_0_0__create-users-table.sql | 8 ++ pom.xml | 2 + 54 files changed, 1460 insertions(+) create mode 100644 lightrun/README.md create mode 100644 lightrun/api-service/.gitignore create mode 100644 lightrun/api-service/pom.xml create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/WebConfig.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/UnknownTaskException.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/UserResponse.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/User.java create mode 100644 lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java create mode 100644 lightrun/api-service/src/main/resources/application.properties create mode 100644 lightrun/pom.xml create mode 100644 lightrun/tasks-service/.gitignore create mode 100644 lightrun/tasks-service/pom.xml create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/ArtemisConfig.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/CreateTaskRequest.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/PatchTaskRequest.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/http-client.env.json create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/tasks.http create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TasksRepository.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java create mode 100644 lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/UnknownTaskException.java create mode 100644 lightrun/tasks-service/src/main/resources/application.properties create mode 100644 lightrun/tasks-service/src/main/resources/db/migration/V1_0_0__create-tasks-table.sql create mode 100644 lightrun/users-service/.gitignore create mode 100644 lightrun/users-service/README.md create mode 100644 lightrun/users-service/pom.xml create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/JmsConfig.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/CreateUserRequest.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/PatchUserRequest.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/http-client.env.json create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/users.http create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/jms/JmsSender.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/repository/UserRecord.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/repository/UsersRepository.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UnknownUserException.java create mode 100644 lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java create mode 100644 lightrun/users-service/src/main/resources/application.properties create mode 100644 lightrun/users-service/src/main/resources/db/migration/V1_0_0__create-users-table.sql diff --git a/lightrun/README.md b/lightrun/README.md new file mode 100644 index 0000000000..732d9b03cd --- /dev/null +++ b/lightrun/README.md @@ -0,0 +1,29 @@ +# Lightrun Example Application - Tasks Management + +This application exists as an example for the Lightrun series of articles. + +## Building +This application requires [Apache Maven](https://maven.apache.org/) and [Java 17+](https://www.oracle.com/java/technologies/downloads/). + +Building the code is done by executing: +``` +$ mvn install +``` +from the top level. + +## Running +The application consists of three services: +* Tasks +* Users +* API + +These are all Spring Boot applications. + +The Tasks and Users services exist as microservices for managing one facet of data. Each uses a database, and utilise a JMS queue between them as well. For convenience this infrastructure is all embedded in the applications. + +This does mean that the startup order is important. The JMS queue exists within the Tasks service and is connected to from the Users service. As such, the Tasks service must be started before the others. + +Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for example: +``` +$ java -jar ./target/tasks-service-0.0.1-SNAPSHOT.jar +``` diff --git a/lightrun/api-service/.gitignore b/lightrun/api-service/.gitignore new file mode 100644 index 0000000000..549e00a2a9 --- /dev/null +++ b/lightrun/api-service/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/lightrun/api-service/pom.xml b/lightrun/api-service/pom.xml new file mode 100644 index 0000000000..c72d66748c --- /dev/null +++ b/lightrun/api-service/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.baeldung + api-service + 0.0.1-SNAPSHOT + api-service + Aggregator Service for LightRun Article + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java new file mode 100644 index 0000000000..4d7e2f3ff7 --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.apiservice; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ApiServiceApplication { + + public static void main(String[] args) { + SpringApplication.run(ApiServiceApplication.class, args); + } + +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java new file mode 100644 index 0000000000..3ad137ca4d --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java @@ -0,0 +1,33 @@ +package com.baeldung.apiservice; + +import org.slf4j.MDC; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.UUID; + +@Component +public class RequestIdGenerator implements HandlerInterceptor { + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String requestId = UUID.randomUUID().toString(); + + MDC.put(RequestIdGenerator.class.getCanonicalName(), requestId); + response.addHeader("X-Request-Id", requestId); + + return true; + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + MDC.remove(RequestIdGenerator.class.getCanonicalName()); + } + + public static String getRequestId() { + return MDC.get(RequestIdGenerator.class.getCanonicalName()); + } +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java new file mode 100644 index 0000000000..278e1520c4 --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java @@ -0,0 +1,20 @@ +package com.baeldung.apiservice; + +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateConfig { + @Bean + public RestTemplate restTemplate(RestTemplateBuilder builder) { + return builder + .additionalInterceptors((request, body, execution) -> { + request.getHeaders().add("X-Request-Id", RequestIdGenerator.getRequestId()); + + return execution.execute(request, body); + }) + .build(); + } +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/WebConfig.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/WebConfig.java new file mode 100644 index 0000000000..9edfcff6f6 --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/WebConfig.java @@ -0,0 +1,17 @@ +package com.baeldung.apiservice; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + @Autowired + private RequestIdGenerator requestIdGenerator; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(requestIdGenerator); + } +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java new file mode 100644 index 0000000000..cec3105c04 --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java @@ -0,0 +1,11 @@ +package com.baeldung.apiservice.adapters.http; + +import java.time.Instant; + +public record TaskResponse(String id, + String title, + Instant created, + UserResponse createdBy, + UserResponse assignedTo, + String status) { +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java new file mode 100644 index 0000000000..e11eaac35f --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java @@ -0,0 +1,54 @@ +package com.baeldung.apiservice.adapters.http; + +import com.baeldung.apiservice.adapters.tasks.Task; +import com.baeldung.apiservice.adapters.tasks.TaskRepository; +import com.baeldung.apiservice.adapters.users.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +@RequestMapping("/") +@RestController +public class TasksController { + @Autowired + private TaskRepository taskRepository; + @Autowired + private UserRepository userRepository; + + @GetMapping("/{id}") + public TaskResponse getTaskById(@PathVariable("id") String id) { + Task task = taskRepository.getTaskById(id); + + if (task == null) { + throw new UnknownTaskException(); + } + + return buildResponse(task); + } + + private TaskResponse buildResponse(Task task) { + return new TaskResponse(task.id(), + task.title(), + task.created(), + getUser(task.createdBy()), + getUser(task.assignedTo()), + task.status()); + } + + private UserResponse getUser(String userId) { + if (userId == null) { + return null; + } + + var user = userRepository.getUserById(userId); + if (user == null) { + return null; + } + + return new UserResponse(user.id(), user.name()); + } + @ExceptionHandler(UnknownTaskException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public void handleUnknownTask() { + } +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/UnknownTaskException.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/UnknownTaskException.java new file mode 100644 index 0000000000..1635ca8796 --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/UnknownTaskException.java @@ -0,0 +1,4 @@ +package com.baeldung.apiservice.adapters.http; + +public class UnknownTaskException extends RuntimeException { +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/UserResponse.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/UserResponse.java new file mode 100644 index 0000000000..f311b895a8 --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/UserResponse.java @@ -0,0 +1,4 @@ +package com.baeldung.apiservice.adapters.http; + +public record UserResponse(String id, String name) { +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java new file mode 100644 index 0000000000..a83192f188 --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java @@ -0,0 +1,11 @@ +package com.baeldung.apiservice.adapters.tasks; + +import java.time.Instant; + +public record Task(String id, + String title, + Instant created, + String createdBy, + String assignedTo, + String status) { +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java new file mode 100644 index 0000000000..49ffa51818 --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java @@ -0,0 +1,30 @@ +package com.baeldung.apiservice.adapters.tasks; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Repository; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +@Repository +public class TaskRepository { + @Autowired + private RestTemplate restTemplate; + + @Value("${tasks-service.url}") + private String tasksService; + + public Task getTaskById(String id) { + var uri = UriComponentsBuilder.fromUriString(tasksService) + .path(id) + .build() + .toUri(); + + try { + return restTemplate.getForObject(uri, Task.class); + } catch (HttpClientErrorException.NotFound e) { + return null; + } + } +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/User.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/User.java new file mode 100644 index 0000000000..a3a53c0805 --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/User.java @@ -0,0 +1,4 @@ +package com.baeldung.apiservice.adapters.users; + +public record User(String id, String name) { +} diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java new file mode 100644 index 0000000000..5a0e4eb64f --- /dev/null +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java @@ -0,0 +1,31 @@ +package com.baeldung.apiservice.adapters.users; + +import com.baeldung.apiservice.adapters.users.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Repository; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +@Repository +public class UserRepository { + @Autowired + private RestTemplate restTemplate; + + @Value("${users-service.url}") + private String usersService; + + public User getUserById(String id) { + var uri = UriComponentsBuilder.fromUriString(usersService) + .path(id) + .build() + .toUri(); + + try { + return restTemplate.getForObject(uri, User.class); + } catch (HttpClientErrorException.NotFound e) { + return null; + } + } +} diff --git a/lightrun/api-service/src/main/resources/application.properties b/lightrun/api-service/src/main/resources/application.properties new file mode 100644 index 0000000000..f3f227f46f --- /dev/null +++ b/lightrun/api-service/src/main/resources/application.properties @@ -0,0 +1,2 @@ +users-service.url=http://localhost:8081 +tasks-service.url=http://localhost:8082 \ No newline at end of file diff --git a/lightrun/pom.xml b/lightrun/pom.xml new file mode 100644 index 0000000000..cfe275848f --- /dev/null +++ b/lightrun/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + com.baelduung + lightrun-demo + 0.0.1-SNAPSHOT + pom + lightrun + Services for LightRun Article + + + tasks-service + users-service + api-service + + diff --git a/lightrun/tasks-service/.gitignore b/lightrun/tasks-service/.gitignore new file mode 100644 index 0000000000..549e00a2a9 --- /dev/null +++ b/lightrun/tasks-service/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/lightrun/tasks-service/pom.xml b/lightrun/tasks-service/pom.xml new file mode 100644 index 0000000000..e56a3a9edf --- /dev/null +++ b/lightrun/tasks-service/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.baeldung + tasks-service + 0.0.1-SNAPSHOT + tasks-service + Tasks Service for LightRun Article + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-artemis + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.flywaydb + flyway-core + + + + com.h2database + h2 + runtime + + + org.apache.activemq + artemis-jms-server + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/ArtemisConfig.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/ArtemisConfig.java new file mode 100644 index 0000000000..56ee3bbc93 --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/ArtemisConfig.java @@ -0,0 +1,23 @@ +package com.baeldung.tasksservice; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.jms.artemis.ArtemisConfigurationCustomizer; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ArtemisConfig implements ArtemisConfigurationCustomizer { + @Value("${spring.artemis.host}") + private String hostname; + + @Value("${spring.artemis.port}") + private int port; + + @Override + public void customize(org.apache.activemq.artemis.core.config.Configuration configuration) { + try { + configuration.addAcceptorConfiguration("remote", "tcp://" + hostname + ":" + port); + } catch (Exception e) { + throw new RuntimeException("Failed to configure Artemis listener", e); + } + } +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java new file mode 100644 index 0000000000..f8e0d64c0c --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.tasksservice; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class TasksServiceApplication { + + public static void main(String[] args) { + SpringApplication.run(TasksServiceApplication.class, args); + } + +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/CreateTaskRequest.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/CreateTaskRequest.java new file mode 100644 index 0000000000..64acea8b1b --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/CreateTaskRequest.java @@ -0,0 +1,15 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.tasksservice.adapters.http; + +public record CreateTaskRequest(String title, String createdBy) { +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/PatchTaskRequest.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/PatchTaskRequest.java new file mode 100644 index 0000000000..20974b1c1d --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/PatchTaskRequest.java @@ -0,0 +1,17 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.tasksservice.adapters.http; + +import java.util.Optional; + +public record PatchTaskRequest(Optional status, Optional assignedTo) { +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java new file mode 100644 index 0000000000..ca13b2ee3d --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java @@ -0,0 +1,22 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.tasksservice.adapters.http; + +import java.time.Instant; + +public record TaskResponse(String id, + String title, + Instant created, + String createdBy, + String assignedTo, + String status) { +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java new file mode 100644 index 0000000000..6502e43883 --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java @@ -0,0 +1,86 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.tasksservice.adapters.http; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import com.baeldung.tasksservice.adapters.repository.TaskRecord; +import com.baeldung.tasksservice.service.TasksService; +import com.baeldung.tasksservice.service.UnknownTaskException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/") +class TasksController { + @Autowired + private TasksService tasksService; + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public TaskResponse createTask(@RequestBody CreateTaskRequest body) { + var task = tasksService.createTask(body.title(), body.createdBy()); + return buildResponse(task); + } + + @GetMapping + public List searchTasks(@RequestParam("status") Optional status, + @RequestParam("createdBy") Optional createdBy) { + var tasks = tasksService.search(status, createdBy); + + return tasks.stream() + .map(this::buildResponse) + .collect(Collectors.toList()); + } + + @GetMapping("/{id}") + public TaskResponse getTask(@PathVariable("id") String id) { + var task = tasksService.getTaskById(id); + return buildResponse(task); + } + + @DeleteMapping("/{id}") + public void deleteTask(@PathVariable("id") String id) { + tasksService.deleteTaskById(id); + } + + @PatchMapping("/{id}") + public TaskResponse patchTask(@PathVariable("id") String id, + @RequestBody PatchTaskRequest body) { + var task = tasksService.updateTask(id, body.status(), body.assignedTo()); + + return buildResponse(task); + } + + private TaskResponse buildResponse(final TaskRecord task) { + return new TaskResponse(task.getId(), task.getTitle(), task.getCreated(), task.getCreatedBy(), + task.getAssignedTo(), task.getStatus()); + } + + @ExceptionHandler(UnknownTaskException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public void handleUnknownTask() { + } +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/http-client.env.json b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/http-client.env.json new file mode 100644 index 0000000000..f27fc4325b --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/http-client.env.json @@ -0,0 +1,5 @@ +{ + "local-tasks": { + "host": "localhost:8082" + } +} \ No newline at end of file diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/tasks.http b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/tasks.http new file mode 100644 index 0000000000..eb7d578ac9 --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/tasks.http @@ -0,0 +1,35 @@ +GET http://{{host}}/createdemoapplication1 HTTP/1.1 + +### +GET http://{{host}}/unknown HTTP/1.1 + +### +GET http://{{host}}?status=PENDING + +### +GET http://{{host}}?createdBy=baeldung + +### +GET http://{{host}}?createdBy=baeldung&status=COMPLETE + +### +DELETE http://{{host}}/createdemoapplication1 HTTP/1.1 + +### +DELETE http://{{host}}/unknown HTTP/1.1 + +### +POST http://{{host}} HTTP/1.1 +Content-Type: application/json + +{ + "title": "My Task", + "createdBy": "graham" +} +### +PATCH http://{{host}}/createdemoapplication1 HTTP/1.1 +Content-Type: application/json + +{ + "status": "COMPLETE" +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java new file mode 100644 index 0000000000..d5a705f56f --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java @@ -0,0 +1,18 @@ +package com.baeldung.tasksservice.adapters.jms; + +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.annotation.JmsListener; + +import com.baeldung.tasksservice.service.DeletedUserService; + +@Service +public class JmsConsumer { + @Autowired + private DeletedUserService deletedUserService; + + @JmsListener(destination = "deleted_user") + public void receive(String user) { + deletedUserService.handleDeletedUser(user); + } +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java new file mode 100644 index 0000000000..6646258c22 --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java @@ -0,0 +1,80 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.tasksservice.adapters.repository; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.time.Instant; + +@Entity +@Table(name = "tasks") +public class TaskRecord { + @Id + @Column(name = "task_id") + private String id; + private String title; + @Column(name = "created_at") + private Instant created; + @Column(name = "created_by") + private String createdBy; + @Column(name = "assigned_to") + private String assignedTo; + private String status; + + public TaskRecord(final String id, final String title, final Instant created, final String createdBy, + final String assignedTo, final String status) { + this.id = id; + this.title = title; + this.created = created; + this.createdBy = createdBy; + this.assignedTo = assignedTo; + this.status = status; + } + + private TaskRecord() { + // Needed for JPA + } + + public String getId() { + return id; + } + + public String getTitle() { + return title; + } + + public Instant getCreated() { + return created; + } + + public String getCreatedBy() { + return createdBy; + } + + public String getAssignedTo() { + return assignedTo; + } + + public String getStatus() { + return status; + } + + public void setAssignedTo(final String assignedTo) { + this.assignedTo = assignedTo; + } + + public void setStatus(final String status) { + this.status = status; + } +} \ No newline at end of file diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TasksRepository.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TasksRepository.java new file mode 100644 index 0000000000..9b6f041fd2 --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TasksRepository.java @@ -0,0 +1,28 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.tasksservice.adapters.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface TasksRepository extends JpaRepository { + List findByStatus(String status); + + List findByCreatedBy(String createdBy); + + List findByStatusAndCreatedBy(String status, String createdBy); + + List findByAssignedTo(String assignedTo); +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java new file mode 100644 index 0000000000..50d35d3f93 --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java @@ -0,0 +1,27 @@ +package com.baeldung.tasksservice.service; + +import javax.transaction.Transactional; + +import com.baeldung.tasksservice.adapters.repository.TaskRecord; +import com.baeldung.tasksservice.adapters.repository.TasksRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class DeletedUserService { + @Autowired + private TasksRepository tasksRepository; + + @Transactional + public void handleDeletedUser(String user) { + var ownedByUser = tasksRepository.findByCreatedBy(user); + tasksRepository.deleteAll(ownedByUser); + + var assignedToUser = tasksRepository.findByAssignedTo(user); + for (TaskRecord record : assignedToUser) { + record.setAssignedTo(null); + record.setStatus("PENDING"); + } + } +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java new file mode 100644 index 0000000000..e9ba1a9f70 --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java @@ -0,0 +1,72 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.tasksservice.service; + +import javax.transaction.Transactional; +import java.time.Instant; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import com.baeldung.tasksservice.adapters.repository.TaskRecord; +import com.baeldung.tasksservice.adapters.repository.TasksRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class TasksService { + @Autowired + private TasksRepository tasksRepository; + + public TaskRecord getTaskById(String id) { + return tasksRepository.findById(id).orElseThrow(() -> new UnknownTaskException(id)); + } + + @Transactional + public void deleteTaskById(String id) { + var task = tasksRepository.findById(id).orElseThrow(() -> new UnknownTaskException(id)); + tasksRepository.delete(task); + } + + public List search(Optional createdBy, Optional status) { + if (createdBy.isPresent() && status.isPresent()) { + return tasksRepository.findByStatusAndCreatedBy(status.get(), createdBy.get()); + } else if (createdBy.isPresent()) { + return tasksRepository.findByCreatedBy(createdBy.get()); + } else if (status.isPresent()) { + return tasksRepository.findByStatus(status.get()); + } else { + return tasksRepository.findAll(); + } + } + + @Transactional + public TaskRecord updateTask(String id, Optional newStatus, Optional newAssignedTo) { + var task = tasksRepository.findById(id).orElseThrow(() -> new UnknownTaskException(id)); + + newStatus.ifPresent(task::setStatus); + newAssignedTo.ifPresent(task::setAssignedTo); + + return task; + } + + public TaskRecord createTask(String title, String createdBy) { + var task = new TaskRecord(UUID.randomUUID().toString(), + title, + Instant.now(), + createdBy, + null, + "PENDING"); + tasksRepository.save(task); + return task; + } +} diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/UnknownTaskException.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/UnknownTaskException.java new file mode 100644 index 0000000000..fd9fce4d16 --- /dev/null +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/UnknownTaskException.java @@ -0,0 +1,24 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.tasksservice.service; + +public class UnknownTaskException extends RuntimeException { + private final String id; + + public UnknownTaskException(final String id) { + this.id = id; + } + + public String getId() { + return id; + } +} diff --git a/lightrun/tasks-service/src/main/resources/application.properties b/lightrun/tasks-service/src/main/resources/application.properties new file mode 100644 index 0000000000..83bbf1a1b8 --- /dev/null +++ b/lightrun/tasks-service/src/main/resources/application.properties @@ -0,0 +1,13 @@ +server.port=8082 + +spring.artemis.mode=EMBEDDED +spring.artemis.host=localhost +spring.artemis.port=61616 + +spring.artemis.embedded.enabled=true + +spring.jms.template.default-destination=my-queue-1 + +logging.level.org.apache.activemq.audit.base=WARN +logging.level.org.apache.activemq.audit.message=WARN + diff --git a/lightrun/tasks-service/src/main/resources/db/migration/V1_0_0__create-tasks-table.sql b/lightrun/tasks-service/src/main/resources/db/migration/V1_0_0__create-tasks-table.sql new file mode 100644 index 0000000000..f2365c2687 --- /dev/null +++ b/lightrun/tasks-service/src/main/resources/db/migration/V1_0_0__create-tasks-table.sql @@ -0,0 +1,13 @@ +CREATE TABLE tasks ( + task_id VARCHAR(36) PRIMARY KEY, + title VARCHAR(100) NOT NULL, + created_at DATETIME NOT NULL, + created_by VARCHAR(36) NOT NULL, + assigned_to VARCHAR(36) NULL, + status VARCHAR(20) NOT NULL +); + +INSERT INTO tasks(task_id, title, created_at, created_by, assigned_to, status) VALUES + ('createdemoapplication1', 'Create demo applications - Tasks', '2022-05-05 12:34:56', 'baeldung', 'coxg', 'IN_PROGRESS'), + ('createdemoapplication2', 'Create demo applications - Users', '2022-05-05 12:34:56', 'baeldung', NULL, 'PENDING'), + ('createdemoapplication3', 'Create demo applications - API', '2022-05-05 12:34:56', 'baeldung', NULL, 'PENDING'); \ No newline at end of file diff --git a/lightrun/users-service/.gitignore b/lightrun/users-service/.gitignore new file mode 100644 index 0000000000..549e00a2a9 --- /dev/null +++ b/lightrun/users-service/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/lightrun/users-service/README.md b/lightrun/users-service/README.md new file mode 100644 index 0000000000..7c713e6638 --- /dev/null +++ b/lightrun/users-service/README.md @@ -0,0 +1,29 @@ +# Lightrun Example Application - Tasks Management + +This application exists as an example for the Lightrun series of articles. + +## Building +This application requires [Apache Maven](https://maven.apache.org/) and [Java 17+](https://www.oracle.com/java/technologies/downloads/). It does use the Maven Wrapper, so it can be built with only Java available on the path. + +As such, building the code is done by executing: +``` +$ ./mvnw install +``` +from the top level. + +## Running +The application consists of three services: +* Tasks +* Users +* API + +These are all Spring Boot applications. + +The Tasks and Users services exist as microservices for managing one facet of data. Each uses a database, and utilise a JMS queue between them as well. For convenience this infrastructure is all embedded in the applications. + +This does mean that the startup order is important. The JMS queue exists within the Tasks service and is connected to from the Users service. As such, the Tasks service must be started before the others. + +Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for example: +``` +$ java -jar ./target/tasks-service-0.0.1-SNAPSHOT.jar +``` diff --git a/lightrun/users-service/pom.xml b/lightrun/users-service/pom.xml new file mode 100644 index 0000000000..a961e4093b --- /dev/null +++ b/lightrun/users-service/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.baeldung + users-service + 0.0.1-SNAPSHOT + users-service + Users Service for LightRun Article + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-artemis + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.flywaydb + flyway-core + + + + com.h2database + h2 + runtime + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/JmsConfig.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/JmsConfig.java new file mode 100644 index 0000000000..c803c9af13 --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/JmsConfig.java @@ -0,0 +1,29 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jms.support.converter.MappingJackson2MessageConverter; +import org.springframework.jms.support.converter.MessageConverter; +import org.springframework.jms.support.converter.MessageType; + +@Configuration +public class JmsConfig { + @Bean + public MessageConverter jacksonJmsMessageConverter() { + MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); + converter.setTargetType(MessageType.TEXT); + converter.setTypeIdPropertyName("_type"); + return converter; + } +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java new file mode 100644 index 0000000000..487e8de45b --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.usersservice; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class UsersServiceApplication { + + public static void main(String[] args) { + SpringApplication.run(UsersServiceApplication.class, args); + } + +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/CreateUserRequest.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/CreateUserRequest.java new file mode 100644 index 0000000000..c3dfc8d068 --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/CreateUserRequest.java @@ -0,0 +1,15 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice.adapters.http; + +public record CreateUserRequest(String name) { +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/PatchUserRequest.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/PatchUserRequest.java new file mode 100644 index 0000000000..acaf7e635f --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/PatchUserRequest.java @@ -0,0 +1,17 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice.adapters.http; + +import java.util.Optional; + +public record PatchUserRequest(Optional name) { +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java new file mode 100644 index 0000000000..b9a20c415b --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java @@ -0,0 +1,16 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice.adapters.http; + +public record UserResponse(String id, + String name) { +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java new file mode 100644 index 0000000000..a5138e31d6 --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java @@ -0,0 +1,69 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice.adapters.http; + +import com.baeldung.usersservice.adapters.repository.UserRecord; +import com.baeldung.usersservice.service.UsersService; +import com.baeldung.usersservice.service.UnknownUserException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/") +class UsersController { + @Autowired + private UsersService usersService; + + @GetMapping("/{id}") + public UserResponse getUser(@PathVariable("id") String id) { + var user = usersService.getUserById(id); + return buildResponse(user); + } + + @DeleteMapping("/{id}") + public void deleteUser(@PathVariable("id") String id) { + usersService.deleteUserById(id); + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public UserResponse createUser(@RequestBody CreateUserRequest body) { + var user = usersService.createUser(body.name()); + return buildResponse(user); + } + + @PatchMapping("/{id}") + public UserResponse patchUser(@PathVariable("id") String id, + @RequestBody PatchUserRequest body) { + var user = usersService.updateUser(id, body.name()); + + return buildResponse(user); + } + private UserResponse buildResponse(final UserRecord user) { + return new UserResponse(user.getId(), user.getName()); + } + + @ExceptionHandler(UnknownUserException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public void handleUnknownUser() { + } +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/http-client.env.json b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/http-client.env.json new file mode 100644 index 0000000000..3be2f710ff --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/http-client.env.json @@ -0,0 +1,5 @@ +{ + "local-users": { + "host": "localhost:8081" + } +} \ No newline at end of file diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/users.http b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/users.http new file mode 100644 index 0000000000..904c5f1cf1 --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/users.http @@ -0,0 +1,25 @@ +GET http://{{host}}/baeldung HTTP/1.1 + +### +GET http://{{host}}/unknown HTTP/1.1 + +### +DELETE http://{{host}}/baeldung HTTP/1.1 + +### +DELETE http://{{host}}/unknown HTTP/1.1 + +### +POST http://{{host}} HTTP/1.1 +Content-Type: application/json + +{ + "name": "Testing" +} +### +PATCH http://{{host}}/coxg HTTP/1.1 +Content-Type: application/json + +{ + "name": "Test Name" +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/jms/JmsSender.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/jms/JmsSender.java new file mode 100644 index 0000000000..f5d5d7900f --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/jms/JmsSender.java @@ -0,0 +1,26 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice.adapters.jms; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.core.JmsTemplate; +import org.springframework.stereotype.Repository; + +@Repository +public class JmsSender { + @Autowired + private JmsTemplate jmsTemplate; + + public void sendDeleteUserMessage(String userId) { + jmsTemplate.convertAndSend("deleted_user", userId); + } +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/repository/UserRecord.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/repository/UserRecord.java new file mode 100644 index 0000000000..fe87ac3ffe --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/repository/UserRecord.java @@ -0,0 +1,47 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice.adapters.repository; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +public class UserRecord { + @Id + @Column(name = "user_id") + private String id; + private String name; + + public UserRecord(final String id, final String name) { + this.id = id; + this.name = name; + } + + private UserRecord() { + // Needed for JPA + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/repository/UsersRepository.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/repository/UsersRepository.java new file mode 100644 index 0000000000..ed193a4955 --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/repository/UsersRepository.java @@ -0,0 +1,19 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice.adapters.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UsersRepository extends JpaRepository { +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UnknownUserException.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UnknownUserException.java new file mode 100644 index 0000000000..d0ca79850c --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UnknownUserException.java @@ -0,0 +1,24 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice.service; + +public class UnknownUserException extends RuntimeException { + private final String id; + + public UnknownUserException(final String id) { + this.id = id; + } + + public String getId() { + return id; + } +} diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java new file mode 100644 index 0000000000..46954b1ee0 --- /dev/null +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java @@ -0,0 +1,61 @@ +/**************************************************************************************************************** + * + * Copyright (c) 2022 OCLC, Inc. All Rights Reserved. + * + * OCLC proprietary information: the enclosed materials contain + * proprietary information of OCLC, Inc. and shall not be disclosed in whole or in + * any part to any third party or used by any person for any purpose, without written + * consent of OCLC, Inc. Duplication of any portion of these materials shall include this notice. + * + ******************************************************************************************************************/ + +package com.baeldung.usersservice.service; + +import javax.transaction.Transactional; + +import java.time.Instant; +import java.util.Optional; +import java.util.UUID; + +import com.baeldung.usersservice.adapters.jms.JmsSender; +import com.baeldung.usersservice.adapters.repository.UserRecord; +import com.baeldung.usersservice.adapters.repository.UsersRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.core.JmsTemplate; +import org.springframework.stereotype.Service; + +@Service +public class UsersService { + @Autowired + private UsersRepository usersRepository; + + @Autowired + private JmsSender jmsSender; + + public UserRecord getUserById(String id) { + return usersRepository.findById(id).orElseThrow(() -> new UnknownUserException(id)); + } + + @Transactional + public void deleteUserById(String id) { + var user = usersRepository.findById(id).orElseThrow(() -> new UnknownUserException(id)); + usersRepository.delete(user); + + jmsSender.sendDeleteUserMessage(id); + } + + @Transactional + public UserRecord updateUser(String id, Optional newName) { + var user = usersRepository.findById(id).orElseThrow(() -> new UnknownUserException(id)); + + newName.ifPresent(user::setName); + + return user; + } + + public UserRecord createUser(String name) { + var user = new UserRecord(UUID.randomUUID().toString(), name); + usersRepository.save(user); + return user; + } +} diff --git a/lightrun/users-service/src/main/resources/application.properties b/lightrun/users-service/src/main/resources/application.properties new file mode 100644 index 0000000000..8cc8f67d92 --- /dev/null +++ b/lightrun/users-service/src/main/resources/application.properties @@ -0,0 +1,10 @@ +server.port=8081 + +spring.artemis.host=localhost +spring.artemis.port=61616 + +spring.jms.template.default-destination=my-queue-1 + +logging.level.org.apache.activemq.audit.base=WARN +logging.level.org.apache.activemq.audit.message=WARN + diff --git a/lightrun/users-service/src/main/resources/db/migration/V1_0_0__create-users-table.sql b/lightrun/users-service/src/main/resources/db/migration/V1_0_0__create-users-table.sql new file mode 100644 index 0000000000..d1ec9387e6 --- /dev/null +++ b/lightrun/users-service/src/main/resources/db/migration/V1_0_0__create-users-table.sql @@ -0,0 +1,8 @@ +CREATE TABLE users ( + user_id VARCHAR(36) PRIMARY KEY, + name VARCHAR(100) NOT NULL +); + +INSERT INTO users(user_id, name) VALUES + ('baeldung', 'Baeldung'), + ('coxg', 'Graham'); \ No newline at end of file diff --git a/pom.xml b/pom.xml index 106bb7516a..963267fee5 100644 --- a/pom.xml +++ b/pom.xml @@ -1329,6 +1329,7 @@ spring-boot-modules/spring-boot-camel testing-modules/testing-assertions persistence-modules/fauna + lightrun @@ -1396,6 +1397,7 @@ spring-boot-modules/spring-boot-camel testing-modules/testing-assertions persistence-modules/fauna + lightrun From e1c046fcab423aad3cffce764728fc4f53b620df Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 17 May 2022 21:53:59 +0530 Subject: [PATCH 80/94] JAVA-11489 Disabled sharing of Restx logs to Restx stats collector which is not working as of now --- restx/src/main/resources/restx/demo/settings.properties | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/restx/src/main/resources/restx/demo/settings.properties b/restx/src/main/resources/restx/demo/settings.properties index a03c2eea97..3b2b591922 100644 --- a/restx/src/main/resources/restx/demo/settings.properties +++ b/restx/src/main/resources/restx/demo/settings.properties @@ -1 +1,2 @@ -app.name=restx-demo \ No newline at end of file +app.name=restx-demo +restx.stats.share.enable=false \ No newline at end of file From 851c7d933e5375f241ff6ea9cffc2bc3d72fdae6 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 17 May 2022 22:04:30 +0530 Subject: [PATCH 81/94] JAVA-11489 Removed a test that was designed to run for 50 seconds to a new Manual Test class --- .../flogger/FloggerIntegrationTest.java | 8 ------ .../baeldung/flogger/FloggerManualTest.java | 25 +++++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerManualTest.java diff --git a/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerIntegrationTest.java b/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerIntegrationTest.java index 80fa0edd96..d3b73637a4 100644 --- a/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerIntegrationTest.java +++ b/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerIntegrationTest.java @@ -5,7 +5,6 @@ import com.google.common.flogger.LoggerConfig; import com.google.common.flogger.StackSize; import org.junit.Test; -import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.stream.IntStream; @@ -25,13 +24,6 @@ public class FloggerIntegrationTest { }); } - @Test - public void givenATimeInterval_shouldLogAfterEveryTimeInterval() { - IntStream.range(0, 1_000_0000).forEach(value -> { - logger.atInfo().atMostEvery(10, TimeUnit.SECONDS).log("This log shows [every 10 seconds] => %d", value); - }); - } - @Test public void givenAnObject_shouldLogTheObject() { User user = new User(); diff --git a/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerManualTest.java b/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerManualTest.java new file mode 100644 index 0000000000..a3444e596e --- /dev/null +++ b/logging-modules/flogger/src/test/java/com/baeldung/flogger/FloggerManualTest.java @@ -0,0 +1,25 @@ +package com.baeldung.flogger; + +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +import org.junit.Test; + +import com.google.common.flogger.FluentLogger; + +public class FloggerManualTest { + static { +// System.setProperty("flogger.backend_factory", "com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"); + System.setProperty("flogger.backend_factory", + "com.google.common.flogger.backend.slf4j.Slf4jBackendFactory#getInstance"); + } + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); + + @Test + public void givenATimeInterval_shouldLogAfterEveryTimeInterval() { + IntStream.range(0, 1_000_0000).forEach(value -> { + logger.atInfo().atMostEvery(10, TimeUnit.SECONDS).log("This log shows [every 10 seconds] => %d", value); + }); + } + +} From 633b5849bb8a65c69ea08443faefd686d01a2819 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 17 May 2022 22:28:39 +0530 Subject: [PATCH 82/94] JAVA-11489 Renamed time taking and unused integration tests to *ManualTests --- ...le1IntegrationTest.java => Example1ManualTest.java} | 2 +- ...le2IntegrationTest.java => Example2ManualTest.java} | 2 +- ...lelIntegrationTest.java => ParallelManualTest.java} | 6 +++--- ...nTest.java => Spring5JUnit5ParallelManualTest.java} | 10 +++++----- 4 files changed, 10 insertions(+), 10 deletions(-) rename spring-5/src/test/java/com/baeldung/{Example1IntegrationTest.java => Example1ManualTest.java} (93%) rename spring-5/src/test/java/com/baeldung/{Example2IntegrationTest.java => Example2ManualTest.java} (93%) rename spring-5/src/test/java/com/baeldung/{ParallelIntegrationTest.java => ParallelManualTest.java} (62%) rename spring-5/src/test/java/com/baeldung/jupiter/{Spring5JUnit5ParallelIntegrationTest.java => Spring5JUnit5ParallelManualTest.java} (60%) diff --git a/spring-5/src/test/java/com/baeldung/Example1IntegrationTest.java b/spring-5/src/test/java/com/baeldung/Example1ManualTest.java similarity index 93% rename from spring-5/src/test/java/com/baeldung/Example1IntegrationTest.java rename to spring-5/src/test/java/com/baeldung/Example1ManualTest.java index 8b9e66213f..c3330b4213 100644 --- a/spring-5/src/test/java/com/baeldung/Example1IntegrationTest.java +++ b/spring-5/src/test/java/com/baeldung/Example1ManualTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class Example1IntegrationTest { +public class Example1ManualTest { @Test public void test1a() { diff --git a/spring-5/src/test/java/com/baeldung/Example2IntegrationTest.java b/spring-5/src/test/java/com/baeldung/Example2ManualTest.java similarity index 93% rename from spring-5/src/test/java/com/baeldung/Example2IntegrationTest.java rename to spring-5/src/test/java/com/baeldung/Example2ManualTest.java index 6ed53ca4e9..9c47b17fdb 100644 --- a/spring-5/src/test/java/com/baeldung/Example2IntegrationTest.java +++ b/spring-5/src/test/java/com/baeldung/Example2ManualTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class Example2IntegrationTest { +public class Example2ManualTest { @Test public void test1a() { diff --git a/spring-5/src/test/java/com/baeldung/ParallelIntegrationTest.java b/spring-5/src/test/java/com/baeldung/ParallelManualTest.java similarity index 62% rename from spring-5/src/test/java/com/baeldung/ParallelIntegrationTest.java rename to spring-5/src/test/java/com/baeldung/ParallelManualTest.java index 1ce96de4ef..5c3a111c62 100644 --- a/spring-5/src/test/java/com/baeldung/ParallelIntegrationTest.java +++ b/spring-5/src/test/java/com/baeldung/ParallelManualTest.java @@ -5,18 +5,18 @@ import org.junit.experimental.ParallelComputer; import org.junit.runner.Computer; import org.junit.runner.JUnitCore; -public class ParallelIntegrationTest { +public class ParallelManualTest { @Test public void runTests() { - final Class[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class }; + final Class[] classes = { Example1ManualTest.class, Example2ManualTest.class }; JUnitCore.runClasses(new Computer(), classes); } @Test public void runTestsInParallel() { - final Class[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class }; + final Class[] classes = { Example1ManualTest.class, Example2ManualTest.class }; JUnitCore.runClasses(new ParallelComputer(true, true), classes); } diff --git a/spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelIntegrationTest.java b/spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelManualTest.java similarity index 60% rename from spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelIntegrationTest.java rename to spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelManualTest.java index 55b0fcf267..b9ed87ed73 100644 --- a/spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelIntegrationTest.java +++ b/spring-5/src/test/java/com/baeldung/jupiter/Spring5JUnit5ParallelManualTest.java @@ -1,24 +1,24 @@ package com.baeldung.jupiter; -import com.baeldung.Example1IntegrationTest; -import com.baeldung.Example2IntegrationTest; +import com.baeldung.Example1ManualTest; +import com.baeldung.Example2ManualTest; import org.junit.experimental.ParallelComputer; import org.junit.jupiter.api.Test; import org.junit.runner.Computer; import org.junit.runner.JUnitCore; -class Spring5JUnit5ParallelIntegrationTest { +class Spring5JUnit5ParallelManualTest { @Test void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingParallel() { - final Class[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class }; + final Class[] classes = { Example1ManualTest.class, Example2ManualTest.class }; JUnitCore.runClasses(new ParallelComputer(true, true), classes); } @Test void givenTwoTestClasses_whenJUnitRunParallel_thenTheTestsExecutingLinear() { - final Class[] classes = { Example1IntegrationTest.class, Example2IntegrationTest.class }; + final Class[] classes = { Example1ManualTest.class, Example2ManualTest.class }; JUnitCore.runClasses(new Computer(), classes); } From f05171163b381e998fffed390cfb9f9cad78bf79 Mon Sep 17 00:00:00 2001 From: 515882294 <515882294@qq.com> Date: Wed, 18 May 2022 01:51:16 +0800 Subject: [PATCH 83/94] BAEL-4463: change method name --- .../java/com/baeldung/reflection/proxy/AdvancedOperation.java | 2 +- .../main/java/com/baeldung/reflection/proxy/BasicOperation.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/AdvancedOperation.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/AdvancedOperation.java index 3262b255f0..b3f0dc8ec0 100644 --- a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/AdvancedOperation.java +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/AdvancedOperation.java @@ -1,7 +1,7 @@ package com.baeldung.reflection.proxy; public interface AdvancedOperation { - int multiple(int a, int b); + int multiply(int a, int b); int divide(int a, int b); } \ No newline at end of file diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/BasicOperation.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/BasicOperation.java index b39eb0118e..168367b690 100644 --- a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/BasicOperation.java +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/proxy/BasicOperation.java @@ -3,5 +3,5 @@ package com.baeldung.reflection.proxy; public interface BasicOperation { int add(int a, int b); - int sub(int a, int b); + int subtract(int a, int b); } \ No newline at end of file From f57604bef02552b57fc4ea0bf6ed6e7a1047b941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Aragon=C3=A9s=20L=C3=B3pez?= Date: Tue, 17 May 2022 21:02:03 +0200 Subject: [PATCH 84/94] Request Mapping value in properties file (#12226) * Exception Handler implemented for Spring Security Resource Server * Renamed test class name to solve PMD Failure * Code formatting * Using property parameter as Request Mapping value * Renamed test class name to solve PMD Failure --- .../WelcomeController.java | 15 +++++++++++ .../src/main/resources/application.properties | 1 + .../WelcomeControllerUnitTest.java | 25 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/requestmappingvalue/WelcomeController.java create mode 100644 spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/requestmappingvalue/WelcomeControllerUnitTest.java diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/requestmappingvalue/WelcomeController.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/requestmappingvalue/WelcomeController.java new file mode 100644 index 0000000000..bbc978ccd4 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/requestmappingvalue/WelcomeController.java @@ -0,0 +1,15 @@ +package com.baeldung.requestmappingvalue; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/${request.value}") +public class WelcomeController { + + @GetMapping + public String getWelcomeMessage() { + return "Welcome to Baeldung!"; + } +} diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/resources/application.properties b/spring-web-modules/spring-mvc-basics-5/src/main/resources/application.properties index 935f91554b..61a0755b93 100644 --- a/spring-web-modules/spring-mvc-basics-5/src/main/resources/application.properties +++ b/spring-web-modules/spring-mvc-basics-5/src/main/resources/application.properties @@ -1 +1,2 @@ server.servlet.context-path=/spring-mvc-basics +request.value=welcome diff --git a/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/requestmappingvalue/WelcomeControllerUnitTest.java b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/requestmappingvalue/WelcomeControllerUnitTest.java new file mode 100644 index 0000000000..ac5140733b --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/requestmappingvalue/WelcomeControllerUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.requestmappingvalue; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +class WelcomeControllerUnitTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void whenUserAccessToWelcome_thenReturnOK() throws Exception { + this.mockMvc.perform(get("/welcome")) + .andExpect(status().isOk()); + } + +} \ No newline at end of file From 7e3cddafaad586287c99f2f23426ddee554fc93d Mon Sep 17 00:00:00 2001 From: Ashley Frieze Date: Wed, 18 May 2022 08:54:10 +0100 Subject: [PATCH 85/94] BAEL-5562 Check if character is vowel (#12221) --- .../core-java-string-operations-4/pom.xml | 6 +++ .../com/baeldung/checkvowels/CheckVowels.java | 38 ++++++++++++++ .../checkvowels/CheckVowelsUnitTest.java | 51 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/checkvowels/CheckVowels.java create mode 100644 core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/checkvowels/CheckVowelsUnitTest.java diff --git a/core-java-modules/core-java-string-operations-4/pom.xml b/core-java-modules/core-java-string-operations-4/pom.xml index 7f71ea8da5..0f1e377d18 100644 --- a/core-java-modules/core-java-string-operations-4/pom.xml +++ b/core-java-modules/core-java-string-operations-4/pom.xml @@ -30,6 +30,12 @@ commons-lang3 ${apache-commons-lang3.version} + + org.assertj + assertj-core + ${assertj.version} + test + diff --git a/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/checkvowels/CheckVowels.java b/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/checkvowels/CheckVowels.java new file mode 100644 index 0000000000..fd60427a4e --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/checkvowels/CheckVowels.java @@ -0,0 +1,38 @@ +package com.baeldung.checkvowels; + +import java.util.regex.Pattern; + +public class CheckVowels { + private static final String VOWELS = "aeiouAEIOU"; + private static final Pattern VOWELS_PATTERN = Pattern.compile("[aeiou]", Pattern.CASE_INSENSITIVE); + + public static boolean isInVowelsString(char c) { + return VOWELS.indexOf(c) != -1; + } + + public static boolean isInVowelsString(String c) { + return VOWELS.contains(c); + } + + public static boolean isVowelBySwitch(char c) { + switch (c) { + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': + case 'A': + case 'E': + case 'I': + case 'O': + case 'U': + return true; + default: + return false; + } + } + + public static boolean isVowelByRegex(String c) { + return VOWELS_PATTERN.matcher(c).matches(); + } +} diff --git a/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/checkvowels/CheckVowelsUnitTest.java b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/checkvowels/CheckVowelsUnitTest.java new file mode 100644 index 0000000000..52b0e55692 --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/checkvowels/CheckVowelsUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.checkvowels; + +import org.junit.jupiter.api.Test; + +import static com.baeldung.checkvowels.CheckVowels.*; +import static org.assertj.core.api.Assertions.*; + +class CheckVowelsUnitTest { + + @Test + void givenAVowelCharacter_thenInVowelString() { + assertThat(isInVowelsString('e')).isTrue(); + } + + @Test + void givenAConsonantCharacter_thenNotInVowelString() { + assertThat(isInVowelsString('z')).isFalse(); + } + + @Test + void givenAVowelString_thenInVowelString() { + assertThat(isInVowelsString("e")).isTrue(); + } + + @Test + void givenAConsonantString_thenNotInVowelString() { + assertThat(isInVowelsString("z")).isFalse(); + } + + @Test + void givenAVowelCharacter_thenInVowelSwitch() { + assertThat(isVowelBySwitch('e')).isTrue(); + } + + @Test + void givenAConsonantCharacter_thenNotInVowelSwitch() { + assertThat(isVowelBySwitch('z')).isFalse(); + } + + @Test + void givenAVowelString_thenInVowelPattern() { + assertThat(isVowelByRegex("e")).isTrue(); + assertThat(isVowelByRegex("E")).isTrue(); + } + + @Test + void givenAVowelCharacter_thenInVowelPattern() { + assertThat(isVowelByRegex(Character.toString('e'))).isTrue(); + assertThat(isVowelByRegex("E")).isTrue(); + } +} From 228c92dc67651662cb31bb6ae49e6b108b9fcb4a Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Wed, 18 May 2022 23:38:44 +0530 Subject: [PATCH 86/94] JAVA-12081 Reverting hazelcast back to previous version - Fixes JAVA-12081 --- libraries-data/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries-data/pom.xml b/libraries-data/pom.xml index c33be5b192..f0f5338560 100644 --- a/libraries-data/pom.xml +++ b/libraries-data/pom.xml @@ -174,7 +174,7 @@ 2.8.2 1.1.1 1.5.0 - 5.1.1 + 3.8.4 0.15.0 2.2.0 1.6.0.1 From b1c0a40b0eb7f087a60f9a1e0a6e59878870528c Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Wed, 18 May 2022 23:59:18 +0530 Subject: [PATCH 87/94] JAVA-11489 Renamed PropertiesReloadIntegrationTest to *ManualTest --- ...loadIntegrationTest.java => PropertiesReloadManualTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/reloading/{PropertiesReloadIntegrationTest.java => PropertiesReloadManualTest.java} (99%) diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/reloading/PropertiesReloadIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/reloading/PropertiesReloadManualTest.java similarity index 99% rename from spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/reloading/PropertiesReloadIntegrationTest.java rename to spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/reloading/PropertiesReloadManualTest.java index 0c28cb085b..88e22af4ba 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/reloading/PropertiesReloadIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/reloading/PropertiesReloadManualTest.java @@ -24,7 +24,7 @@ import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = SpringBootPropertiesTestApplication.class) -public class PropertiesReloadIntegrationTest { +public class PropertiesReloadManualTest { protected MockMvc mvc; From 75a751df3a5695b605472ff86a56b84861c11d4c Mon Sep 17 00:00:00 2001 From: Harry9656 Date: Thu, 19 May 2022 01:34:23 +0200 Subject: [PATCH 88/94] JAVA-5484: New Java Http Client Timeout (#12223) Co-authored-by: Harpal Singh --- .../baeldung/http/JavaHttpClientTimeout.java | 12 +++++ .../JavaHttpClientTimeoutIntegrationTest.java | 54 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 core-java-modules/core-java-networking-3/src/main/java/com/baeldung/http/JavaHttpClientTimeout.java create mode 100644 core-java-modules/core-java-networking-3/src/test/java/com/baeldung/http/JavaHttpClientTimeoutIntegrationTest.java diff --git a/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/http/JavaHttpClientTimeout.java b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/http/JavaHttpClientTimeout.java new file mode 100644 index 0000000000..d7ab002e77 --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/http/JavaHttpClientTimeout.java @@ -0,0 +1,12 @@ +package com.baeldung.http; + +import java.net.http.HttpClient; +import java.time.Duration; + +public class JavaHttpClientTimeout { + static HttpClient getHttpClientWithTimeout(int seconds) { + return HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(seconds)) + .build(); + } +} diff --git a/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/http/JavaHttpClientTimeoutIntegrationTest.java b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/http/JavaHttpClientTimeoutIntegrationTest.java new file mode 100644 index 0000000000..df635621ad --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/http/JavaHttpClientTimeoutIntegrationTest.java @@ -0,0 +1,54 @@ +package com.baeldung.http; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpConnectTimeoutException; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static com.baeldung.http.JavaHttpClientTimeout.getHttpClientWithTimeout; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class JavaHttpClientTimeoutIntegrationTest { + + private HttpClient httpClient; + private HttpRequest httpRequest; + + @BeforeEach + public void setUp() { + httpClient = getHttpClientWithTimeout(3); + httpClient.connectTimeout().map(Duration::toSeconds) + .ifPresent(sec -> System.out.println("Timeout in seconds: " + sec)); + + httpRequest = HttpRequest.newBuilder().uri(URI.create("http://10.255.255.1")).GET().build(); + } + + @Test + void shouldThrowExceptionWhenMakingSyncCall() { + HttpConnectTimeoutException thrown = assertThrows( + HttpConnectTimeoutException.class, + () -> httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()), + "Expected doThing() to throw, but it didn't" + ); + assertTrue(thrown.getMessage().contains("timed out")); + } + + @Test + void shouldThrowExceptionWhenMakingASyncCall() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture completableFuture = + httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .exceptionally(Throwable::getMessage); + String response = completableFuture.get(5, TimeUnit.SECONDS); + assertTrue(response.contains("timed out")); + } +} \ No newline at end of file From f8ce24823675e0e32b3e965755812b27b3cfce2b Mon Sep 17 00:00:00 2001 From: opokharel <66694687+opokharel@users.noreply.github.com> Date: Wed, 18 May 2022 18:36:27 -0600 Subject: [PATCH 89/94] [BAEL-5554] Find files that match a wildcard string in Java by @opokharel (#12190) * BAEL-5554 by @opokharel * deletedToMoveToSrcFolder * movedToSrcFolder * redoingForJenkins * newPR for [BAEL-5554] Find files that match a wildcard string in Java by @opokharel * @opokharel * [BAEL-5554] @opokharel * [BAEL-5554] Find files that match a wildcard string in Java by @opokharel * Update SearchFileByWildcardTest.java * Update SearchFileByWildcard.java * Update SearchFileByWildcard.java * Create SearchFileByWildcardUnitTest.java * Delete SearchFileByWildcardTest.java * [BAEL-5554] Find files that match a wildcard string in Java by @opokharel * Update SearchFileByWildcardUnitTest.java * Update SearchFileByWildcardUnitTest.java * [BAEL-5554] UnitTestFiles by @opokharel * Update core-java-modules/core-java-nio-2/src/test/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcardUnitTest.java Co-authored-by: KevinGilmore * updated curly braces and assertions Co-authored-by: KevinGilmore --- .../SearchFileByWildcard.java | 37 ++++++++++++++++++ .../SearchFileByWildcardUnitTest.java | 30 ++++++++++++++ .../src/test/resources/sfbw/test/five.csv | 0 .../src/test/resources/sfbw/test/four.xlsx | Bin 0 -> 6169 bytes .../src/test/resources/sfbw/test/one.txt | 0 .../test/resources/sfbw/test/test2/six.txt | 0 .../src/test/resources/sfbw/test/three.txt | 0 .../src/test/resources/sfbw/test/two.docx | 0 8 files changed, 67 insertions(+) create mode 100644 core-java-modules/core-java-nio-2/src/main/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcard.java create mode 100644 core-java-modules/core-java-nio-2/src/test/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcardUnitTest.java create mode 100644 core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/five.csv create mode 100644 core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/four.xlsx create mode 100644 core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/one.txt create mode 100644 core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/test2/six.txt create mode 100644 core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/three.txt create mode 100644 core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/two.docx diff --git a/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcard.java b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcard.java new file mode 100644 index 0000000000..2deaa60ec6 --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcard.java @@ -0,0 +1,37 @@ +package com.baeldung.searchfilesbywildcards; + +import java.io.IOException; + +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; + +import java.util.ArrayList; +import java.util.List; + +public class SearchFileByWildcard { + public static List matchesList = new ArrayList(); + public List searchWithWc(Path rootDir, String pattern) throws IOException { + matchesList.clear(); + FileVisitor matcherVisitor = new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attribs) throws IOException { + FileSystem fs = FileSystems.getDefault(); + PathMatcher matcher = fs.getPathMatcher(pattern); + Path name = file.getFileName(); + if (matcher.matches(name)) { + matchesList.add(name.toString()); + } + return FileVisitResult.CONTINUE; + } + }; + Files.walkFileTree(rootDir, matcherVisitor); + return matchesList; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcardUnitTest.java b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcardUnitTest.java new file mode 100644 index 0000000000..71cee036ec --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcardUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.searchfilesbywildcards; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; + +import java.nio.file.Paths; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +import org.junit.jupiter.api.Test; + +public class SearchFileByWildcardUnitTest { + @Test + public void whenFourFilenameMatch_thenListOfFour() throws IOException { + SearchFileByWildcard sfbw = new SearchFileByWildcard(); + List actual = sfbw.searchWithWc(Paths.get("src/test/resources/sfbw"), "glob:*.{txt,docx}"); + + assertEquals(new HashSet<>(Arrays.asList("six.txt", "three.txt", "two.docx", "one.txt")), new HashSet<>(actual)); + } + @Test + public void whenOneFilenameMatch_thenListOfOne() throws IOException { + SearchFileByWildcard sfbw = new SearchFileByWildcard(); + List actual = sfbw.searchWithWc(Paths.get("src/test/resources/sfbw"), "glob:????.{csv}"); + + assertEquals(new HashSet<>(Arrays.asList("five.csv")), new HashSet<>(actual)); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/five.csv b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/five.csv new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/four.xlsx b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/four.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..1091dafe75e41a1e45f2c0b8df3dfe6d7f6f9af6 GIT binary patch literal 6169 zcmeHLcQoAFw;yfv5H(5&Awh^Xdan_^_Yxx_7}0yp=!{_W1R+X@8odn?o#-_f1c``V zM+tf3=H2UFzx&==@BepZ%{epYeD|JNpWV*>XsTdgQviSfJOBW|1jyCt7*xjq0PwH@ z0CMz>iJapzH|u9^7P{U})~;svy&NFL34Q8qJfsRJrJr)kTv5mhX*_`qXct)n8{?zJ zhv0E;@7a-a33AkX>1;|nq4(gySyu`E%uj+76!sAXh@z>~SVN4`L*>RA@ayQ>-FV8> zT%8dwT*?{DiqHFpPb#K>tsGQ;8 z0))zfcA5{dU`CVlZG$_MWpvYXB9V{cCUD{x*2Z4xypS?3k@t7aoOoxl0;%G7ovD-DCC1EKi&hEGzcU+Pcor}M; zU-uY47(YyVpzO`&fr#ZOsw>D=>6yQ+IJzVMo^Hk$@#0qQV}Am>mzBD-L7Dp&!v9d5T=7;T0NboBDUq=OczmR zZ;Qu});;-GE#zl@TqVDY;Xf$KN9NGi_ z6CBHj_@t5Olj~Rj04W-r$BtH-E{;yF+?GyG7mk?@mvNloAqw5XI$fpetkoMgH2~o zeVF7mg)|GG#DU{i@9;{qz6e@WwD8k@MNj_NB?#vMcCoxlqQ3SUhiv}3M>uE3&m*m_$+IIF-B`O}TO>C+RJ4!xz zTPXsD6dVPVFC?W$N3GU8vZ%<(xu29?YiA!5E`Yn%>4nWY5B$OKsk|!2nWp-O?Raor zDxq7TmxIBZBL`Aoy7K-VG4hD_z?m^vwT6mIsOx}`d78$_UEiCHk3YtT>KwF%+W09o zdfDTdPkKx0&P+TmIL1+5lO!IKaO68>dW?Ko9)2>u*TN%yzO>(VuB={Mx6o_Or=C^f9?NOo%!e zR8{JF_NZaM#InTKbWe@tW+w-;-m$anZakX@A@V#OV{ePp2T3Q}Dp6}DMRG-kwWPB? zXze@dm)L=4hU%ZtJDW$_U+a*#+3YXw^nj(PtD-|#gNjD{ba|Kh()}(g+Utllhh#J+ z*gyb)_%|-DZr%`U*9$yybxrJNcqjrG*QM|fjTJ~?73f9Mc;97 zRDo$E`J&5qQCr$;FQCXR8dtnqiT&Gi^-mNcxKOIvzS(s73mPRcxz}_nZ`bg`^d~If z&~{7VyKt9K-;gBEU%2wxdtOhW+I?Z7&0nF7R^g%XoCdXGdBjM9tZ7Al<2QmYUwOfX z(#)~rn?Tb)rp*9s!|wemhwwExCqCDp65v!cmyOAC_HO3eYW;yX+Kh?8yI2i(5k9&n zjT}F@9oG@NW9QNux{B?SgE__=zZlTUw_bW zDM%I~8?+MUG+mTUH|)p++}Ncc*i6;JVS5_K_<}gZfQL>(bp=JTm66@WrhbQ7iJC{F zc3PJwdc_BpFHTwPAw9N@oO`VB%+W7EUu~b*(Hi%A!@7y719KqSs$FiWhP5RRp~Jzi3RmlhR1` zc{b||&vrd3X{_WicX|{2h34G5^1OU@mcqFlZ!-FkM-Ti_TCtD}U!%FBU=}B>GeBI zYq`tLkl2!*7a&FcxfuNCaPfmYO(hiz0{=BRc4E-bP|A5~Xo%q1bqRiWN!o$%tG@gy zR(r$TVb1|S*MVUnYO^Lc?)-u4>LQj8XG+Wq>aMAGLS*4aGYA3t#FwRQILdb-DzIyj ztlu{Ckg-w?5gzg(R_j6jA3Zf^VnR~468rp!`XekJxu&;Pva49JEC^;K-DMXAw(xRI zawKKZ--g$zL%~kI{-Uf_>qL1fo}OQuG`NJ(S<`IC5r5i9d9ae~$(=l`{!G4u%B25U*11dL>?0RL9sm5w;Rc<-Jw#UJ zVHrRdvUm~`FhnwOP63GN;u(QOZ;o!&G)PEw*=4PdEH+MlA1?x$?^(<*%I=GkYeaQsaEhP^Npma+C%JRZ+V22_mJ4#NzhmCcBAx%Nk%Qcbb zT7QzUwPs;k;PKvAQWW0w2c^p7rXc+Jr*J=dN%bDnDx}7Tq9U10DPND>?bV((3k?Pd zQST$yxmp+NDKnWK)mq=DM!no=ncpMgy|tSs9`9m5$n?a1| zHpO5``a`nSDUBnhL^hXaJeI**{CmXls5sn2h#JZP8sqkw3%gd@)LmKOdH3DuF?e5J zYTDKkT>ZK}2iCNsl8-8!|KKdK;`A4+Vn*PE9)#RHG8=13AX^i2|C$))*mj1S)EG|H zLrlco_wCbxOq~>7?P&}3ro-0mai4qk(5<-+W3!Ukr4m9Ed-lFGC;Qxz-Nt~W`AecK zEeDRV0T)F4s|QYLj^sqMoC>|257Uld5#Mjgx*9GRj0?JTx=!+MexeC?Z?V&-1N-&X zPD^ku`V00P7LC7~ef4DVLpP%XNFFh0I0<@ZzkIffGvZt7xNo$R3PQ3UoE_)1n6YUK&U~b6I*__}V+T7)plNOQ_e%XV6 zm|=sgd}~J|q6Uf<8P%i|v7hePKU3SKEdrAX7+Q~%)DSlfHB2b4WoedFw$oH@+N#&varlJr~pYsz{@Kib02(cZTS z_1a&c@RGqXC=B5yVTUSMc)UpO0X8wCk?%Bk{YkXT4J)6qEh36@cZlyFe7_bYR zsUyaA&U~u34DKPGZD6Q!GDj5!91dFpaJAgh0)4bL=w4eajvLK{@<#Wm`kS-gTl}%7 zQeAVl7cD34wuxIUQ#AJjn5U26XY)ops~jT;epW~w5;K=YebPU%@O+J)y?nAT+Em$3Erlbc`A z)hz=orW8k&zuJ@AG;hm>j0tQIB_FZ1Af(5=j%1ZAG@NAyNxIv@rea^z_LA00qu$6n zGkW9^X;Oi@@(`_37?Xmp^f^A=F-q=CEcsQn_~Cjx(1}D{($%?*D=|@XM?Vy+#+ox? zM?5+qS8S$wTkB)TuMRgA7k_wuRSI-5xDJtOak8DjDQ7kWQgi)8@_z7^hP$-KOPk7T z{pRJTrB*QkEcu?N2w(hy^4JYAxx~7>_Q;tOgs>n3xhcNeB=K3NxEaf6DbTcyn(-vQ zN}X!o2HwHAxu`(Z(YVUl`z;#u(qKo?#R8Pz}dAmR9IY3h}+dc zBQ-2kAUJE~h$~k>&HY5Tzf)&z{vY0ejV76JwZADUI$;p{p}ip!eJ#In=?$(=t*zZ$ zx&K)HxYoc%`k$d2dccD3YuO=0@@UB#Z96^?`-2Y~4`gk#N_9q-WjHQ1-@E4Kp^|Ut zum?HO_dFq;e97$xfq}n_9L%8xgQ*ochN}u%cA>-COJIZ5x33%5292%5rh?u^BuQFM zygC^Fp=A5oS6&~p-E=k6+mJTFAr`|$221gHRGXlcEPr5`i3v2Cx6rR<6u^gJI#yY( zy2V@IkkP^QS+2F4A3lVoa$N5zCu#qoIh-)=F#OB3_KlE&3{St8L;QOcM|h5T3y?Re zT+}Y3p9ByNTXVB?<;FOTe&3l{3lKz3WCth8g?dZ5k3X40|_} z7r#pLj!6yWSVJD07{}NbL>^il2Gb{%h18p-!HlNV`i_!ygSqI%1K%zgxS0*gB9*(W z%KE-lbwI4^pp-(jzf!7j!sZbQar2AasMrThv{JCGH8pLqAhkEZLz>x&y`BE0{irwrK& zZ4MqrCMU&#@MfOZERPLI>lpixk96|B4~QER^*-(OSQL#@iJeY6^YGBB5$_c}!C2!c zagm2pC}h=9C2_d!H=A`3Qiu#DKC0G_%OMx8;@sTgd&V|NC8$qV-i%3&19wS$MZbX% zqOj^h-AYq|%HcS_EqN>{?hr}5gsYF4nY&7_vaQN9=tKzXmzKB5UBDXgn`ywpdyx&q z?-f%egZ^q_D0UN~6SZDWOp;0~9~Ie5F9HSVvkEE^1m8>J5{xz(@|Dl>d%IkS6Ueo*`Q1S(E)PVFqoY!2|>T>GW@6OHKae2Ia{ z1-Q(ZSBY3O=zkmkLd$BZ{8PbID)CY}5N*i65sOy^uhMLng6Zi0`j_SZn{@jv{Xerk zm(n_DO8F}HbM?2ck{FlL4LJWBrEyiy)u#Va&j`AcLRX(l3gD{f)pqMrGy(r#o3E=1 zuGYFs1qNvP>q_POQ}`k^UXW#1Q{$zuHQ8Un|B)#F)bb~be>Ge$wNO+1&4hmjucitP T8WjM50R8eq+c=!^;@f`#F)1b5 literal 0 HcmV?d00001 diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/one.txt b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/one.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/test2/six.txt b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/test2/six.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/three.txt b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/three.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/two.docx b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/two.docx new file mode 100644 index 0000000000..e69de29bb2 From 762d5f0f56f66972f94d1dc1c19a29842338462a Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Thu, 19 May 2022 10:14:05 +0100 Subject: [PATCH 90/94] Reformatted code --- lightrun/README.md | 9 +- lightrun/api-service/pom.xml | 80 ++++++------ .../apiservice/ApiServiceApplication.java | 6 +- .../apiservice/RequestIdGenerator.java | 14 +-- .../apiservice/RestTemplateConfig.java | 12 +- .../adapters/http/TaskResponse.java | 7 +- .../adapters/http/TasksController.java | 22 ++-- .../apiservice/adapters/tasks/Task.java | 7 +- .../adapters/tasks/TaskRepository.java | 6 +- .../adapters/users/UserRepository.java | 7 +- lightrun/pom.xml | 8 +- lightrun/tasks-service/pom.xml | 114 +++++++++--------- .../tasksservice/TasksServiceApplication.java | 6 +- .../adapters/http/TaskResponse.java | 7 +- .../adapters/http/TasksController.java | 20 ++- .../adapters/jms/JmsConsumer.java | 2 +- .../adapters/repository/TaskRecord.java | 6 +- .../service/DeletedUserService.java | 28 ++--- .../tasksservice/service/TasksService.java | 25 ++-- .../src/main/resources/application.properties | 4 - lightrun/users-service/README.md | 9 +- lightrun/users-service/pom.xml | 114 +++++++++--------- .../usersservice/UsersServiceApplication.java | 6 +- .../adapters/http/UserResponse.java | 3 +- .../adapters/http/UsersController.java | 13 +- .../usersservice/service/UsersService.java | 23 ++-- .../src/main/resources/application.properties | 3 - 27 files changed, 278 insertions(+), 283 deletions(-) diff --git a/lightrun/README.md b/lightrun/README.md index 732d9b03cd..18d4ccc12f 100644 --- a/lightrun/README.md +++ b/lightrun/README.md @@ -3,16 +3,21 @@ This application exists as an example for the Lightrun series of articles. ## Building + This application requires [Apache Maven](https://maven.apache.org/) and [Java 17+](https://www.oracle.com/java/technologies/downloads/). Building the code is done by executing: + ``` $ mvn install ``` + from the top level. ## Running + The application consists of three services: + * Tasks * Users * API @@ -23,7 +28,9 @@ The Tasks and Users services exist as microservices for managing one facet of da This does mean that the startup order is important. The JMS queue exists within the Tasks service and is connected to from the Users service. As such, the Tasks service must be started before the others. -Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for example: +Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for +example: + ``` $ java -jar ./target/tasks-service-0.0.1-SNAPSHOT.jar ``` diff --git a/lightrun/api-service/pom.xml b/lightrun/api-service/pom.xml index c72d66748c..3423c490f1 100644 --- a/lightrun/api-service/pom.xml +++ b/lightrun/api-service/pom.xml @@ -1,45 +1,45 @@ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.6.7 - - - com.baeldung - api-service - 0.0.1-SNAPSHOT - api-service - Aggregator Service for LightRun Article - - 17 - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-web - + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.baeldung + api-service + 0.0.1-SNAPSHOT + api-service + Aggregator Service for LightRun Article + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + - - org.springframework.boot - spring-boot-starter-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java index 4d7e2f3ff7..a9b29cbd4b 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/ApiServiceApplication.java @@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ApiServiceApplication { - public static void main(String[] args) { - SpringApplication.run(ApiServiceApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(ApiServiceApplication.class, args); + } } diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java index 3ad137ca4d..f15738c1e6 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RequestIdGenerator.java @@ -1,20 +1,20 @@ package com.baeldung.apiservice; -import org.slf4j.MDC; -import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.ModelAndView; +import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.UUID; + +import org.slf4j.MDC; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; @Component public class RequestIdGenerator implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String requestId = UUID.randomUUID().toString(); + String requestId = UUID.randomUUID() + .toString(); MDC.put(RequestIdGenerator.class.getCanonicalName(), requestId); response.addHeader("X-Request-Id", requestId); diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java index 278e1520c4..1582ba5953 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/RestTemplateConfig.java @@ -9,12 +9,12 @@ import org.springframework.web.client.RestTemplate; public class RestTemplateConfig { @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { - return builder - .additionalInterceptors((request, body, execution) -> { - request.getHeaders().add("X-Request-Id", RequestIdGenerator.getRequestId()); + return builder.additionalInterceptors((request, body, execution) -> { + request.getHeaders() + .add("X-Request-Id", RequestIdGenerator.getRequestId()); - return execution.execute(request, body); - }) - .build(); + return execution.execute(request, body); + }) + .build(); } } diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java index cec3105c04..875390fdcd 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TaskResponse.java @@ -2,10 +2,5 @@ package com.baeldung.apiservice.adapters.http; import java.time.Instant; -public record TaskResponse(String id, - String title, - Instant created, - UserResponse createdBy, - UserResponse assignedTo, - String status) { +public record TaskResponse(String id, String title, Instant created, UserResponse createdBy, UserResponse assignedTo, String status) { } diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java index e11eaac35f..55b449f249 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/http/TasksController.java @@ -1,11 +1,17 @@ package com.baeldung.apiservice.adapters.http; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + import com.baeldung.apiservice.adapters.tasks.Task; import com.baeldung.apiservice.adapters.tasks.TaskRepository; import com.baeldung.apiservice.adapters.users.UserRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.*; @RequestMapping("/") @RestController @@ -27,15 +33,10 @@ public class TasksController { } private TaskResponse buildResponse(Task task) { - return new TaskResponse(task.id(), - task.title(), - task.created(), - getUser(task.createdBy()), - getUser(task.assignedTo()), - task.status()); + return new TaskResponse(task.id(), task.title(), task.created(), getUser(task.createdBy()), getUser(task.assignedTo()), task.status()); } - private UserResponse getUser(String userId) { + private UserResponse getUser(String userId) { if (userId == null) { return null; } @@ -47,6 +48,7 @@ public class TasksController { return new UserResponse(user.id(), user.name()); } + @ExceptionHandler(UnknownTaskException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public void handleUnknownTask() { diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java index a83192f188..188d3e951c 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/Task.java @@ -2,10 +2,5 @@ package com.baeldung.apiservice.adapters.tasks; import java.time.Instant; -public record Task(String id, - String title, - Instant created, - String createdBy, - String assignedTo, - String status) { +public record Task(String id, String title, Instant created, String createdBy, String assignedTo, String status) { } diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java index 49ffa51818..9260a125af 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/tasks/TaskRepository.java @@ -17,9 +17,9 @@ public class TaskRepository { public Task getTaskById(String id) { var uri = UriComponentsBuilder.fromUriString(tasksService) - .path(id) - .build() - .toUri(); + .path(id) + .build() + .toUri(); try { return restTemplate.getForObject(uri, Task.class); diff --git a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java index 5a0e4eb64f..d5625f6efc 100644 --- a/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java +++ b/lightrun/api-service/src/main/java/com/baeldung/apiservice/adapters/users/UserRepository.java @@ -1,6 +1,5 @@ package com.baeldung.apiservice.adapters.users; -import com.baeldung.apiservice.adapters.users.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Repository; @@ -18,9 +17,9 @@ public class UserRepository { public User getUserById(String id) { var uri = UriComponentsBuilder.fromUriString(usersService) - .path(id) - .build() - .toUri(); + .path(id) + .build() + .toUri(); try { return restTemplate.getForObject(uri, User.class); diff --git a/lightrun/pom.xml b/lightrun/pom.xml index cfe275848f..4d046347ab 100644 --- a/lightrun/pom.xml +++ b/lightrun/pom.xml @@ -1,13 +1,13 @@ - + 4.0.0 com.baelduung lightrun-demo 0.0.1-SNAPSHOT pom - lightrun - Services for LightRun Article + lightrun + Services for LightRun Article tasks-service diff --git a/lightrun/tasks-service/pom.xml b/lightrun/tasks-service/pom.xml index e56a3a9edf..441b4d3713 100644 --- a/lightrun/tasks-service/pom.xml +++ b/lightrun/tasks-service/pom.xml @@ -1,67 +1,67 @@ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.6.7 - - - com.baeldung - tasks-service - 0.0.1-SNAPSHOT - tasks-service - Tasks Service for LightRun Article - - 17 - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-artemis - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - - - org.flywaydb - flyway-core - + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.baeldung + tasks-service + 0.0.1-SNAPSHOT + tasks-service + Tasks Service for LightRun Article + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-artemis + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.flywaydb + flyway-core + - - com.h2database - h2 - runtime - + + com.h2database + h2 + runtime + org.apache.activemq artemis-jms-server - - org.springframework.boot - spring-boot-starter-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java index f8e0d64c0c..dfd9859674 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/TasksServiceApplication.java @@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TasksServiceApplication { - public static void main(String[] args) { - SpringApplication.run(TasksServiceApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(TasksServiceApplication.class, args); + } } diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java index ca13b2ee3d..4e01a649b1 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TaskResponse.java @@ -13,10 +13,5 @@ package com.baeldung.tasksservice.adapters.http; import java.time.Instant; -public record TaskResponse(String id, - String title, - Instant created, - String createdBy, - String assignedTo, - String status) { +public record TaskResponse(String id, String title, Instant created, String createdBy, String assignedTo, String status) { } diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java index 6502e43883..a4145a2243 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/http/TasksController.java @@ -15,9 +15,6 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -import com.baeldung.tasksservice.adapters.repository.TaskRecord; -import com.baeldung.tasksservice.service.TasksService; -import com.baeldung.tasksservice.service.UnknownTaskException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; @@ -32,6 +29,10 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +import com.baeldung.tasksservice.adapters.repository.TaskRecord; +import com.baeldung.tasksservice.service.TasksService; +import com.baeldung.tasksservice.service.UnknownTaskException; + @RestController @RequestMapping("/") class TasksController { @@ -46,13 +47,12 @@ class TasksController { } @GetMapping - public List searchTasks(@RequestParam("status") Optional status, - @RequestParam("createdBy") Optional createdBy) { + public List searchTasks(@RequestParam("status") Optional status, @RequestParam("createdBy") Optional createdBy) { var tasks = tasksService.search(status, createdBy); return tasks.stream() - .map(this::buildResponse) - .collect(Collectors.toList()); + .map(this::buildResponse) + .collect(Collectors.toList()); } @GetMapping("/{id}") @@ -67,16 +67,14 @@ class TasksController { } @PatchMapping("/{id}") - public TaskResponse patchTask(@PathVariable("id") String id, - @RequestBody PatchTaskRequest body) { + public TaskResponse patchTask(@PathVariable("id") String id, @RequestBody PatchTaskRequest body) { var task = tasksService.updateTask(id, body.status(), body.assignedTo()); return buildResponse(task); } private TaskResponse buildResponse(final TaskRecord task) { - return new TaskResponse(task.getId(), task.getTitle(), task.getCreated(), task.getCreatedBy(), - task.getAssignedTo(), task.getStatus()); + return new TaskResponse(task.getId(), task.getTitle(), task.getCreated(), task.getCreatedBy(), task.getAssignedTo(), task.getStatus()); } @ExceptionHandler(UnknownTaskException.class) diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java index d5a705f56f..c380a16acc 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/jms/JmsConsumer.java @@ -1,8 +1,8 @@ package com.baeldung.tasksservice.adapters.jms; -import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; +import org.springframework.stereotype.Service; import com.baeldung.tasksservice.service.DeletedUserService; diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java index 6646258c22..dee3017a59 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/adapters/repository/TaskRecord.java @@ -11,11 +11,12 @@ package com.baeldung.tasksservice.adapters.repository; +import java.time.Instant; + import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; -import java.time.Instant; @Entity @Table(name = "tasks") @@ -32,8 +33,7 @@ public class TaskRecord { private String assignedTo; private String status; - public TaskRecord(final String id, final String title, final Instant created, final String createdBy, - final String assignedTo, final String status) { + public TaskRecord(final String id, final String title, final Instant created, final String createdBy, final String assignedTo, final String status) { this.id = id; this.title = title; this.created = created; diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java index 50d35d3f93..fa0c3572fb 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/DeletedUserService.java @@ -2,26 +2,26 @@ package com.baeldung.tasksservice.service; import javax.transaction.Transactional; -import com.baeldung.tasksservice.adapters.repository.TaskRecord; -import com.baeldung.tasksservice.adapters.repository.TasksRepository; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.baeldung.tasksservice.adapters.repository.TaskRecord; +import com.baeldung.tasksservice.adapters.repository.TasksRepository; + @Service public class DeletedUserService { - @Autowired - private TasksRepository tasksRepository; + @Autowired + private TasksRepository tasksRepository; - @Transactional - public void handleDeletedUser(String user) { - var ownedByUser = tasksRepository.findByCreatedBy(user); - tasksRepository.deleteAll(ownedByUser); + @Transactional + public void handleDeletedUser(String user) { + var ownedByUser = tasksRepository.findByCreatedBy(user); + tasksRepository.deleteAll(ownedByUser); - var assignedToUser = tasksRepository.findByAssignedTo(user); - for (TaskRecord record : assignedToUser) { - record.setAssignedTo(null); - record.setStatus("PENDING"); + var assignedToUser = tasksRepository.findByAssignedTo(user); + for (TaskRecord record : assignedToUser) { + record.setAssignedTo(null); + record.setStatus("PENDING"); + } } - } } diff --git a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java index e9ba1a9f70..3539dbbc3c 100644 --- a/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java +++ b/lightrun/tasks-service/src/main/java/com/baeldung/tasksservice/service/TasksService.java @@ -11,29 +11,33 @@ package com.baeldung.tasksservice.service; -import javax.transaction.Transactional; import java.time.Instant; import java.util.List; import java.util.Optional; import java.util.UUID; -import com.baeldung.tasksservice.adapters.repository.TaskRecord; -import com.baeldung.tasksservice.adapters.repository.TasksRepository; +import javax.transaction.Transactional; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.baeldung.tasksservice.adapters.repository.TaskRecord; +import com.baeldung.tasksservice.adapters.repository.TasksRepository; + @Service public class TasksService { @Autowired private TasksRepository tasksRepository; public TaskRecord getTaskById(String id) { - return tasksRepository.findById(id).orElseThrow(() -> new UnknownTaskException(id)); + return tasksRepository.findById(id) + .orElseThrow(() -> new UnknownTaskException(id)); } @Transactional public void deleteTaskById(String id) { - var task = tasksRepository.findById(id).orElseThrow(() -> new UnknownTaskException(id)); + var task = tasksRepository.findById(id) + .orElseThrow(() -> new UnknownTaskException(id)); tasksRepository.delete(task); } @@ -51,7 +55,8 @@ public class TasksService { @Transactional public TaskRecord updateTask(String id, Optional newStatus, Optional newAssignedTo) { - var task = tasksRepository.findById(id).orElseThrow(() -> new UnknownTaskException(id)); + var task = tasksRepository.findById(id) + .orElseThrow(() -> new UnknownTaskException(id)); newStatus.ifPresent(task::setStatus); newAssignedTo.ifPresent(task::setAssignedTo); @@ -60,12 +65,8 @@ public class TasksService { } public TaskRecord createTask(String title, String createdBy) { - var task = new TaskRecord(UUID.randomUUID().toString(), - title, - Instant.now(), - createdBy, - null, - "PENDING"); + var task = new TaskRecord(UUID.randomUUID() + .toString(), title, Instant.now(), createdBy, null, "PENDING"); tasksRepository.save(task); return task; } diff --git a/lightrun/tasks-service/src/main/resources/application.properties b/lightrun/tasks-service/src/main/resources/application.properties index 83bbf1a1b8..88326ed10e 100644 --- a/lightrun/tasks-service/src/main/resources/application.properties +++ b/lightrun/tasks-service/src/main/resources/application.properties @@ -1,13 +1,9 @@ server.port=8082 - spring.artemis.mode=EMBEDDED spring.artemis.host=localhost spring.artemis.port=61616 - spring.artemis.embedded.enabled=true - spring.jms.template.default-destination=my-queue-1 - logging.level.org.apache.activemq.audit.base=WARN logging.level.org.apache.activemq.audit.message=WARN diff --git a/lightrun/users-service/README.md b/lightrun/users-service/README.md index 7c713e6638..e7faae858f 100644 --- a/lightrun/users-service/README.md +++ b/lightrun/users-service/README.md @@ -3,16 +3,21 @@ This application exists as an example for the Lightrun series of articles. ## Building + This application requires [Apache Maven](https://maven.apache.org/) and [Java 17+](https://www.oracle.com/java/technologies/downloads/). It does use the Maven Wrapper, so it can be built with only Java available on the path. As such, building the code is done by executing: + ``` $ ./mvnw install ``` + from the top level. ## Running + The application consists of three services: + * Tasks * Users * API @@ -23,7 +28,9 @@ The Tasks and Users services exist as microservices for managing one facet of da This does mean that the startup order is important. The JMS queue exists within the Tasks service and is connected to from the Users service. As such, the Tasks service must be started before the others. -Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for example: +Each service can be started either by executing `mvn spring-boot:run` from within the appropriate directory. Alternatively, as Spring Boot applications, the build will produce an executable JAR file within the `target` directory that can be executed as, for +example: + ``` $ java -jar ./target/tasks-service-0.0.1-SNAPSHOT.jar ``` diff --git a/lightrun/users-service/pom.xml b/lightrun/users-service/pom.xml index a961e4093b..63596ed67b 100644 --- a/lightrun/users-service/pom.xml +++ b/lightrun/users-service/pom.xml @@ -1,63 +1,63 @@ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.6.7 - - - com.baeldung - users-service - 0.0.1-SNAPSHOT - users-service - Users Service for LightRun Article - - 17 - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-artemis - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - - - org.flywaydb - flyway-core - + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + com.baeldung + users-service + 0.0.1-SNAPSHOT + users-service + Users Service for LightRun Article + + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-artemis + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + org.flywaydb + flyway-core + - - com.h2database - h2 - runtime - + + com.h2database + h2 + runtime + - - org.springframework.boot - spring-boot-starter-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java index 487e8de45b..3910960282 100644 --- a/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/UsersServiceApplication.java @@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class UsersServiceApplication { - public static void main(String[] args) { - SpringApplication.run(UsersServiceApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(UsersServiceApplication.class, args); + } } diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java index b9a20c415b..e74ede65d5 100644 --- a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UserResponse.java @@ -11,6 +11,5 @@ package com.baeldung.usersservice.adapters.http; -public record UserResponse(String id, - String name) { +public record UserResponse(String id, String name) { } diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java index a5138e31d6..795d240fe9 100644 --- a/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/adapters/http/UsersController.java @@ -11,9 +11,6 @@ package com.baeldung.usersservice.adapters.http; -import com.baeldung.usersservice.adapters.repository.UserRecord; -import com.baeldung.usersservice.service.UsersService; -import com.baeldung.usersservice.service.UnknownUserException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; @@ -27,6 +24,10 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +import com.baeldung.usersservice.adapters.repository.UserRecord; +import com.baeldung.usersservice.service.UnknownUserException; +import com.baeldung.usersservice.service.UsersService; + @RestController @RequestMapping("/") class UsersController { @@ -50,14 +51,14 @@ class UsersController { var user = usersService.createUser(body.name()); return buildResponse(user); } - + @PatchMapping("/{id}") - public UserResponse patchUser(@PathVariable("id") String id, - @RequestBody PatchUserRequest body) { + public UserResponse patchUser(@PathVariable("id") String id, @RequestBody PatchUserRequest body) { var user = usersService.updateUser(id, body.name()); return buildResponse(user); } + private UserResponse buildResponse(final UserRecord user) { return new UserResponse(user.getId(), user.getName()); } diff --git a/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java b/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java index 46954b1ee0..5115a5a77b 100644 --- a/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java +++ b/lightrun/users-service/src/main/java/com/baeldung/usersservice/service/UsersService.java @@ -11,18 +11,17 @@ package com.baeldung.usersservice.service; -import javax.transaction.Transactional; - -import java.time.Instant; import java.util.Optional; import java.util.UUID; +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + import com.baeldung.usersservice.adapters.jms.JmsSender; import com.baeldung.usersservice.adapters.repository.UserRecord; import com.baeldung.usersservice.adapters.repository.UsersRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jms.core.JmsTemplate; -import org.springframework.stereotype.Service; @Service public class UsersService { @@ -33,12 +32,14 @@ public class UsersService { private JmsSender jmsSender; public UserRecord getUserById(String id) { - return usersRepository.findById(id).orElseThrow(() -> new UnknownUserException(id)); + return usersRepository.findById(id) + .orElseThrow(() -> new UnknownUserException(id)); } @Transactional public void deleteUserById(String id) { - var user = usersRepository.findById(id).orElseThrow(() -> new UnknownUserException(id)); + var user = usersRepository.findById(id) + .orElseThrow(() -> new UnknownUserException(id)); usersRepository.delete(user); jmsSender.sendDeleteUserMessage(id); @@ -46,7 +47,8 @@ public class UsersService { @Transactional public UserRecord updateUser(String id, Optional newName) { - var user = usersRepository.findById(id).orElseThrow(() -> new UnknownUserException(id)); + var user = usersRepository.findById(id) + .orElseThrow(() -> new UnknownUserException(id)); newName.ifPresent(user::setName); @@ -54,7 +56,8 @@ public class UsersService { } public UserRecord createUser(String name) { - var user = new UserRecord(UUID.randomUUID().toString(), name); + var user = new UserRecord(UUID.randomUUID() + .toString(), name); usersRepository.save(user); return user; } diff --git a/lightrun/users-service/src/main/resources/application.properties b/lightrun/users-service/src/main/resources/application.properties index 8cc8f67d92..616131c42e 100644 --- a/lightrun/users-service/src/main/resources/application.properties +++ b/lightrun/users-service/src/main/resources/application.properties @@ -1,10 +1,7 @@ server.port=8081 - spring.artemis.host=localhost spring.artemis.port=61616 - spring.jms.template.default-destination=my-queue-1 - logging.level.org.apache.activemq.audit.base=WARN logging.level.org.apache.activemq.audit.message=WARN From 9ada6d69ae5d425bd3352d8bf057df81b1063f88 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Thu, 19 May 2022 20:46:52 +0530 Subject: [PATCH 91/94] JAVA-11849 Disabled NPM and Frontend Tasks from Integration builds, added jhipster-5 to integration-heavy profile --- jhipster-5/bookstore-monolith/pom.xml | 60 +++++++++++++++++++++++++++ pom.xml | 9 ++-- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/jhipster-5/bookstore-monolith/pom.xml b/jhipster-5/bookstore-monolith/pom.xml index 411de0e712..ccf7a3c85e 100644 --- a/jhipster-5/bookstore-monolith/pom.xml +++ b/jhipster-5/bookstore-monolith/pom.xml @@ -1090,6 +1090,66 @@ + + integration-lite-first + + + + com.github.eirslett + frontend-maven-plugin + + + + install node and npm + none + + + npm install + none + + + webpack build dev + none + + + webpack build test + none + + + + + + + + integration-lite-second + + + + com.github.eirslett + frontend-maven-plugin + + + + install node and npm + none + + + npm install + none + + + webpack build dev + none + + + webpack build test + none + + + + + + diff --git a/pom.xml b/pom.xml index 106bb7516a..37cc9a3ea1 100644 --- a/pom.xml +++ b/pom.xml @@ -366,7 +366,7 @@ atomix aws-modules - + axon azure @@ -836,7 +836,7 @@ atomix aws-modules - + axon azure @@ -895,7 +895,7 @@ helidon apache-httpclient httpclient-simple - hystrix + hystrix jackson-modules jackson-simple @@ -1209,6 +1209,7 @@ jenkins/plugins jhipster + jhipster-5 jws libraries @@ -1326,7 +1327,7 @@ quarkus-vs-springboot quarkus-jandex spring-boot-modules/spring-boot-cassandre - spring-boot-modules/spring-boot-camel + spring-boot-modules/spring-boot-camel testing-modules/testing-assertions persistence-modules/fauna From 2e8c7dc728cff6fdac37762049f2a65b884260e5 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Fri, 20 May 2022 10:32:38 +0530 Subject: [PATCH 92/94] JAVA-12015: Align module names, folder names and artifact id --- docker/docker-push-to-private-repo/pom.xml | 24 +++++++++++++--------- lightrun/pom.xml | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/docker/docker-push-to-private-repo/pom.xml b/docker/docker-push-to-private-repo/pom.xml index 59a909ff07..19be098794 100644 --- a/docker/docker-push-to-private-repo/pom.xml +++ b/docker/docker-push-to-private-repo/pom.xml @@ -1,19 +1,19 @@ - + 4.0.0 + docker-push-to-private-repo + 0.0.1-SNAPSHOT + docker-push-to-private-repo + Example application to showcase how to push a docker image to a private repository + com.baeldung.docker docker 0.0.1 - push-to-private-repo - 0.0.1-SNAPSHOT - push-to-private-repo - Example application to showcase how to push a docker image to a private repository - - 11 - + org.springframework.boot @@ -40,4 +40,8 @@ - + + 11 + + + \ No newline at end of file diff --git a/lightrun/pom.xml b/lightrun/pom.xml index cfe275848f..ddb720b023 100644 --- a/lightrun/pom.xml +++ b/lightrun/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baelduung - lightrun-demo + lightrun 0.0.1-SNAPSHOT pom lightrun From 22f01e2188427bd4cbe5612c2e194dbe9124485b Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Fri, 20 May 2022 14:09:36 -0300 Subject: [PATCH 93/94] isolating application repositories --- .../collection/name/SpringBootCollectionNameApplication.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java index 0a5c36db29..e4157b6a53 100644 --- a/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java +++ b/persistence-modules/spring-boot-persistence-mongodb-2/src/main/java/com/baeldung/boot/collection/name/SpringBootCollectionNameApplication.java @@ -4,9 +4,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; @SpringBootApplication @PropertySource("classpath:boot.collection.name/app.properties") +@EnableMongoRepositories(basePackages = { "com.baeldung.boot.collection.name" }) public class SpringBootCollectionNameApplication { public static void main(String... args) { SpringApplication.run(SpringBootCollectionNameApplication.class, args); From 296b77b90951c8324d9593e99492f51b771df6f7 Mon Sep 17 00:00:00 2001 From: etrandafir93 <75391049+etrandafir93@users.noreply.github.com> Date: Sat, 21 May 2022 21:47:49 +0200 Subject: [PATCH 94/94] BAEL-4605: code and tests for composing constraints (#12180) * BAEL-4605: code and tests for composing constraints * BAEL-4605: small fix and formatted the code * BAEL-4605: code review --- .../constraint/composition/Account.java | 37 +++++++++ .../composition/AccountService.java | 19 +++++ .../composition/AlphanumericReturnValue.java | 36 +++++++++ .../ConstraintCompositionConfig.java | 21 +++++ .../composition/ValidAlphanumeric.java | 37 +++++++++ .../ValidAlphanumericWithSingleViolation.java | 38 +++++++++ .../ValidLengthOrNumericCharacter.java | 34 ++++++++ .../ConstraintCompositionUnitTest.java | 80 +++++++++++++++++++ 8 files changed, 302 insertions(+) create mode 100644 javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/Account.java create mode 100644 javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/AccountService.java create mode 100644 javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/AlphanumericReturnValue.java create mode 100644 javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ConstraintCompositionConfig.java create mode 100644 javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidAlphanumeric.java create mode 100644 javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidAlphanumericWithSingleViolation.java create mode 100644 javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidLengthOrNumericCharacter.java create mode 100644 javaxval/src/test/java/com/baeldung/javaxval/constraint/composition/ConstraintCompositionUnitTest.java diff --git a/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/Account.java b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/Account.java new file mode 100644 index 0000000000..b47a47ec81 --- /dev/null +++ b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/Account.java @@ -0,0 +1,37 @@ +package com.baeldung.javaxval.constraint.composition; + +public class Account { + + @ValidAlphanumeric + private String username; + + @ValidAlphanumericWithSingleViolation + private String password; + + @ValidLengthOrNumericCharacter + private String nickname; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getNickname() { + return nickname; + } + + public void setNickname(String nickname) { + this.nickname = nickname; + } +} diff --git a/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/AccountService.java b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/AccountService.java new file mode 100644 index 0000000000..f76fed8f04 --- /dev/null +++ b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/AccountService.java @@ -0,0 +1,19 @@ +package com.baeldung.javaxval.constraint.composition; + +import org.springframework.stereotype.Component; +import org.springframework.validation.annotation.Validated; + +@Component +@Validated +public class AccountService { + + @AlphanumericReturnValue + public String getAnInvalidAlphanumericValue() { + return "john"; + } + + @AlphanumericReturnValue + public String getValidAlphanumericValue() { + return "johnDoe1234"; + } +} diff --git a/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/AlphanumericReturnValue.java b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/AlphanumericReturnValue.java new file mode 100644 index 0000000000..6e3408712b --- /dev/null +++ b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/AlphanumericReturnValue.java @@ -0,0 +1,36 @@ +package com.baeldung.javaxval.constraint.composition; + +import org.hibernate.validator.constraints.Length; + +import javax.validation.Constraint; +import javax.validation.Payload; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraintvalidation.SupportedValidationTarget; +import javax.validation.constraintvalidation.ValidationTarget; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@NotNull +@Pattern(regexp = ".*\\d.*", message = "must contain at least one numeric character") +@Length(min = 6, max = 32, message = "must have between 6 and 32 characters") +@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) +@Retention(RUNTIME) +@Documented +@Constraint(validatedBy = {}) +@SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT) +public @interface AlphanumericReturnValue { + + String message() default "method return value should have a valid length and contain numeric character(s)."; + + Class[] groups() default {}; + + Class[] payload() default {}; + +} + diff --git a/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ConstraintCompositionConfig.java b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ConstraintCompositionConfig.java new file mode 100644 index 0000000000..ac0ec81ab2 --- /dev/null +++ b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ConstraintCompositionConfig.java @@ -0,0 +1,21 @@ +package com.baeldung.javaxval.constraint.composition; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; + +@Configuration +@ComponentScan({ "com.baeldung.javaxval.constraint.composition" }) +public class ConstraintCompositionConfig { + + @Bean + public MethodValidationPostProcessor methodValidationPostProcessor() { + return new MethodValidationPostProcessor(); + } + + @Bean + public AccountService accountService() { + return new AccountService(); + } +} \ No newline at end of file diff --git a/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidAlphanumeric.java b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidAlphanumeric.java new file mode 100644 index 0000000000..916b4e36a4 --- /dev/null +++ b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidAlphanumeric.java @@ -0,0 +1,37 @@ +package com.baeldung.javaxval.constraint.composition; + +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.validation.Constraint; +import javax.validation.Payload; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; + +import org.hibernate.validator.constraints.Length; + +@NotNull +@Pattern(regexp = ".*\\d.*", message = "must contain at least one numeric character") +@Length(min = 6, max = 32, message = "must have between 6 and 32 characters") +@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) +@Retention(RUNTIME) +@Documented +@Constraint(validatedBy = {}) +public @interface ValidAlphanumeric { + + String message() default "field should have a valid length and contain numeric character(s)."; + + Class[] groups() default {}; + + Class[] payload() default {}; + +} + diff --git a/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidAlphanumericWithSingleViolation.java b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidAlphanumericWithSingleViolation.java new file mode 100644 index 0000000000..edc5b6af3e --- /dev/null +++ b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidAlphanumericWithSingleViolation.java @@ -0,0 +1,38 @@ +package com.baeldung.javaxval.constraint.composition; + +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import javax.validation.Constraint; +import javax.validation.Payload; +import javax.validation.ReportAsSingleViolation; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; + +import org.hibernate.validator.constraints.Length; + +@NotNull +@Pattern(regexp = ".*\\d.*", message = "must contain at least one numeric character") +@Length(min = 6, max = 32, message = "must have between 6 and 32 characters") +@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) +@Retention(RUNTIME) +@Documented +@Constraint(validatedBy = {}) +@ReportAsSingleViolation +public @interface ValidAlphanumericWithSingleViolation { + + String message() default "field should have a valid length and contain numeric character(s)."; + + Class[] groups() default {}; + + Class[] payload() default {}; + +} \ No newline at end of file diff --git a/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidLengthOrNumericCharacter.java b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidLengthOrNumericCharacter.java new file mode 100644 index 0000000000..444cb4a63a --- /dev/null +++ b/javaxval/src/main/java/com/baeldung/javaxval/constraint/composition/ValidLengthOrNumericCharacter.java @@ -0,0 +1,34 @@ +package com.baeldung.javaxval.constraint.composition; + +import org.hibernate.validator.constraints.CompositionType; +import org.hibernate.validator.constraints.ConstraintComposition; +import org.hibernate.validator.constraints.Length; + +import javax.validation.Constraint; +import javax.validation.Payload; +import javax.validation.constraints.Pattern; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Pattern(regexp = ".*\\d.*", message = "must contain at least one numeric character") +@Length(min = 6, max = 32, message = "must have between 6 and 32 characters") +@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) +@Retention(RUNTIME) +@Documented +@Constraint(validatedBy = {}) +@ConstraintComposition(CompositionType.OR) +public @interface ValidLengthOrNumericCharacter { + + String message() default "field should have a valid length or contain numeric character(s)."; + + Class[] groups() default {}; + + Class[] payload() default {}; + +} + diff --git a/javaxval/src/test/java/com/baeldung/javaxval/constraint/composition/ConstraintCompositionUnitTest.java b/javaxval/src/test/java/com/baeldung/javaxval/constraint/composition/ConstraintCompositionUnitTest.java new file mode 100644 index 0000000000..6c2b8f801c --- /dev/null +++ b/javaxval/src/test/java/com/baeldung/javaxval/constraint/composition/ConstraintCompositionUnitTest.java @@ -0,0 +1,80 @@ +package com.baeldung.javaxval.constraint.composition; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { ConstraintCompositionConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ConstraintCompositionUnitTest { + + @Autowired + private AccountService accountService; + + private Validator validator; + + @Before + public void setup() { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + validator = factory.getValidator(); + } + + @Test + public void whenUsernameIsInvalid_validationShouldReturnTwoViolations() { + Account account = new Account(); + account.setNickname("valid_nickname123"); + account.setPassword("valid_password123"); + account.setUsername("john"); + + Set> violations = validator.validate(account); + + assertThat(violations).hasSize(2); + } + + @Test + public void whenPasswordIsInvalid_validationShouldReturnSingleViolation() { + Account account = new Account(); + account.setUsername("valid_username123"); + account.setNickname("valid_nickname123"); + account.setPassword("john"); + + Set> violations = validator.validate(account); + + assertThat(violations).hasSize(1); + } + + @Test + public void whenNicknameIsTooShortButContainsNumericCharacter_validationShouldPass() { + Account account = new Account(); + account.setUsername("valid_username123"); + account.setPassword("valid_password123"); + account.setNickname("doe1"); + + Set> violations = validator.validate(account); + + assertThat(violations).isEmpty(); + } + + @Test + public void whenMethodReturnValuesIsInvalid_validationShouldFail() { + assertThatThrownBy(() -> accountService.getAnInvalidAlphanumericValue()).isInstanceOf(ConstraintViolationException.class) + .hasMessageContaining("must contain at least one numeric character") + .hasMessageContaining("must have between 6 and 32 characters"); + } + +} \ No newline at end of file