From 891e044e4ae63524456165a52efc7db5c2407cd5 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Sun, 10 Apr 2022 17:39:01 -0300 Subject: [PATCH 1/7] 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 2/7] 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 3/7] 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 c69f4d207f19190213f6fd857c977ebfb9f4a593 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Sat, 7 May 2022 17:24:56 -0300 Subject: [PATCH 4/7] 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 18237d20979d94f3868e0053c8348366373cd562 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Mon, 16 May 2022 23:23:26 -0300 Subject: [PATCH 5/7] 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 6/7] 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 22f01e2188427bd4cbe5612c2e194dbe9124485b Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Fri, 20 May 2022 14:09:36 -0300 Subject: [PATCH 7/7] 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);