diff --git a/src/carts/.mvn/wrapper/maven-wrapper.properties b/src/carts/.mvn/wrapper/maven-wrapper.properties index 642d572c..6b029944 100644 --- a/src/carts/.mvn/wrapper/maven-wrapper.properties +++ b/src/carts/.mvn/wrapper/maven-wrapper.properties @@ -1,2 +1,2 @@ -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip -wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar \ No newline at end of file diff --git a/src/carts/app/pom.xml b/src/carts/app/pom.xml index 485626be..77a70867 100644 --- a/src/carts/app/pom.xml +++ b/src/carts/app/pom.xml @@ -1,65 +1,80 @@ - - - - micronaut.mushop.carts - carts-parent - ${revision} - - 4.0.0 - - carts-app - - ${packaging} - - - jar - mushop.carts.Application - - - - - micronaut.mushop.carts - carts-lib - ${project.version} - - - - io.micronaut.micrometer - micronaut-micrometer-registry-prometheus - compile - - - - micronaut.mushop.carts - carts-tck - ${project.version} - test - - - - - - - io.micronaut.build - micronaut-maven-plugin - - - -H:AdditionalSecurityProviders=com.sun.security.sasl.Provider - - - - - com.google.cloud.tools - jib-maven-plugin - - - phx.ocir.io/oraclelabs/micronaut-showcase/mushop/${project.artifactId}-${docker.image.suffix}:${project.version} - - - - - - - + + + + micronaut.mushop.carts + carts-parent + 0.1 + + 4.0.0 + + carts-app + + ${packaging} + + + jar + mushop.carts.Application + + + + + micronaut.mushop.carts + carts-lib + ${project.version} + + + + io.micronaut.micrometer + micronaut-micrometer-registry-prometheus + compile + + + org.yaml + snakeyaml + runtime + + + micronaut.mushop.carts + carts-tck + ${project.version} + test + + + + + + + io.micronaut.maven + micronaut-maven-plugin + + + -H:AdditionalSecurityProviders=com.sun.security.sasl.Provider + -H:-UseServiceLoaderFeature + + + + io.micronaut.testresources + micronaut-test-resources-mongodb + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + + + com.google.cloud.tools + jib-maven-plugin + + + phx.ocir.io/oraclelabs/micronaut-showcase/mushop/${project.artifactId}-${docker.image.suffix}:${project.version} + + + + + + + diff --git a/src/carts/app/src/main/resources/application-app.yml b/src/carts/app/src/main/resources/application-app.yml index 0b2e9513..d1039e3c 100644 --- a/src/carts/app/src/main/resources/application-app.yml +++ b/src/carts/app/src/main/resources/application-app.yml @@ -10,7 +10,4 @@ endpoints: prometheus: enabled: true sensitive: false - details-visible: ANONYMOUS - -mongodb: - uri: mongodb://${MONGO_USER:root}:${MONGO_PASS}@${MONGO_HOST:localhost}:${MONGO_PORT:27017}/?authSource=admin + details-visible: ANONYMOUS \ No newline at end of file diff --git a/src/carts/aws/pom.xml b/src/carts/aws/pom.xml index 25abf594..d3c3294d 100644 --- a/src/carts/aws/pom.xml +++ b/src/carts/aws/pom.xml @@ -1,80 +1,93 @@ - - - - micronaut.mushop.carts - carts-parent - ${revision} - - 4.0.0 - - carts-aws - - ${packaging} - - - jar - mushop.carts.AwsApplication - - - - - micronaut.mushop.carts - carts-lib - ${project.version} - - - - software.amazon.awssdk - sts - - - - - io.micronaut.aws - micronaut-aws-secretsmanager - - - io.micronaut.aws - micronaut-aws-parameter-store - - - - - io.micronaut.micrometer - micronaut-micrometer-registry-cloudwatch - - - - micronaut.mushop.carts - carts-tck - ${project.version} - test - - - - - - - io.micronaut.build - micronaut-maven-plugin - - - -H:AdditionalSecurityProviders=com.sun.security.sasl.Provider - - - - - com.google.cloud.tools - jib-maven-plugin - - - phx.ocir.io/oraclelabs/micronaut-showcase/mushop/${project.artifactId}-${docker.image.suffix}:${project.version} - - - - - - - + + + + micronaut.mushop.carts + carts-parent + 0.1 + + 4.0.0 + + carts-aws + + ${packaging} + + + jar + mushop.carts.AwsApplication + + + + + micronaut.mushop.carts + carts-lib + ${project.version} + + + software.amazon.awssdk + sts + + + + + io.micronaut.aws + micronaut-aws-secretsmanager + + + io.micronaut.aws + micronaut-aws-parameter-store + + + + + io.micronaut.micrometer + micronaut-micrometer-registry-cloudwatch + + + micronaut.mushop.carts + carts-tck + ${project.version} + test + + + org.yaml + snakeyaml + runtime + + + + + + + io.micronaut.maven + micronaut-maven-plugin + + + -H:AdditionalSecurityProviders=com.sun.security.sasl.Provider + -H:-UseServiceLoaderFeature + + + + io.micronaut.testresources + micronaut-test-resources-mongodb + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + + + com.google.cloud.tools + jib-maven-plugin + + + phx.ocir.io/oraclelabs/micronaut-showcase/mushop/${project.artifactId}-${docker.image.suffix}:${project.version} + + + + + + diff --git a/src/carts/aws/src/test/resources/application-test.yml b/src/carts/aws/src/test/resources/application-test.yml index 0193c2f9..09829671 100644 --- a/src/carts/aws/src/test/resources/application-test.yml +++ b/src/carts/aws/src/test/resources/application-test.yml @@ -1,5 +1,5 @@ -micronaut: - metrics: - export: - cloudwatch: - enabled: false +micronaut: + metrics: + export: + cloudwatch: + enabled: false \ No newline at end of file diff --git a/src/carts/lib/pom.xml b/src/carts/lib/pom.xml index 3603fdd1..3e92d876 100644 --- a/src/carts/lib/pom.xml +++ b/src/carts/lib/pom.xml @@ -1,98 +1,134 @@ - - - - micronaut.mushop.carts - carts-parent - ${revision} - - 4.0.0 - - carts-lib - - jar - - - - io.micronaut - micronaut-inject - compile - - - io.micronaut - micronaut-validation - compile - - - io.micronaut - micronaut-http-server-netty - compile - - - ch.qos.logback - logback-classic - runtime - - - - io.micronaut - micronaut-http-client - compile - - - io.micronaut - micronaut-runtime - compile - - - javax.annotation - javax.annotation-api - compile - - - - - io.swagger.core.v3 - swagger-annotations - compile - - - - - io.micronaut.data - micronaut-data-mongodb - compile - - - org.mongodb - mongodb-driver-sync - - - - - io.micronaut - micronaut-management - compile - - - io.micronaut.micrometer - micronaut-micrometer-core - compile - - - - - io.micronaut.tracing - micronaut-tracing-core - compile - - - io.micronaut.tracing - micronaut-tracing-zipkin - compile - - - - - + + + + micronaut.mushop.carts + carts-parent + 0.1 + + 4.0.0 + + carts-lib + + jar + + + + io.micronaut + micronaut-inject + compile + + + io.micronaut.validation + micronaut-validation + compile + + + io.micronaut + micronaut-http-server-netty + compile + + + org.yaml + snakeyaml + runtime + + + ch.qos.logback + logback-classic + runtime + + + io.micronaut + micronaut-http-client + test + + + io.micronaut + micronaut-runtime + compile + + + jakarta.validation + jakarta.validation-api + compile + + + + io.swagger.core.v3 + swagger-annotations + compile + + + + + io.micronaut.data + micronaut-data-mongodb + compile + + + io.micronaut.serde + micronaut-serde-jackson + compile + + + io.micronaut.mongodb + micronaut-mongo-reactive + + + io.micronaut.testresources + micronaut-test-resources-client + provided + + + io.micronaut.test + micronaut-test-junit5 + test + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + + io.micronaut + micronaut-management + compile + + + io.micronaut.micrometer + micronaut-micrometer-core + compile + + + + + io.micronaut.tracing + micronaut-tracing-core + compile + + + io.micronaut.tracing + micronaut-tracing-brave-http + compile + + + + + + org.apache.maven.plugins + maven-shade-plugin + + true + + + + + diff --git a/src/carts/lib/src/main/java/mushop/carts/controllers/CartsController.java b/src/carts/lib/src/main/java/mushop/carts/controllers/CartsController.java index 91908f84..bec3be9c 100644 --- a/src/carts/lib/src/main/java/mushop/carts/controllers/CartsController.java +++ b/src/carts/lib/src/main/java/mushop/carts/controllers/CartsController.java @@ -113,7 +113,7 @@ HttpResponse postCart(@PathVariable String cartId, LOG.info("Received {} with cart {}", cartId, newCart); Optional cartHolder = cartRepository.findById(cartId); if (cartHolder.isEmpty()) { - newCart.setId(cartId); + newCart = newCart.withId(cartId); cartRepository.save(newCart); LOG.info("Cart created: {}", newCart); return HttpResponse.created(newCart); @@ -136,9 +136,8 @@ Cart updateCartItem(@PathVariable String cartId, @Body Item qItem) { ); for (Item item : cart.getItems()) { - if (item.getItemId().equals(qItem.getItemId())) { - item.setQuantity(qItem.getQuantity()); - + if (item.itemId().equals(qItem.itemId())) { + cart.updateItem(item.withQuantity(qItem.quantity())); cartRepository.update(cart); LOG.info("Cart item updated: {}", cart); return cart; diff --git a/src/carts/lib/src/main/java/mushop/carts/entities/Cart.java b/src/carts/lib/src/main/java/mushop/carts/entities/Cart.java index e2b7cc14..773ae203 100644 --- a/src/carts/lib/src/main/java/mushop/carts/entities/Cart.java +++ b/src/carts/lib/src/main/java/mushop/carts/entities/Cart.java @@ -1,82 +1,58 @@ -package mushop.carts.entities; - -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; -import javax.validation.constraints.NotNull; - -import io.micronaut.core.annotation.Creator; -import io.micronaut.data.annotation.Id; -import io.micronaut.data.annotation.MappedEntity; - -@MappedEntity -public class Cart { - - @Id - private String id; - - private String customerId; - - private List items = new ArrayList<>(); - - public Cart() { - id = UUID.randomUUID().toString(); - } - - public Cart(String id) { - this.id = id; - } - - public String getId() { - return id; - } - - public String getCustomerId() { - return customerId; - } - - public List getItems() { - return items; - } - - public void setId(String id) { - this.id = id; - } - - public void setCustomerId(String customerId) { - this.customerId = customerId; - } - - public void setItems(List items) { - this.items = items; - } - - public boolean removeItem(String itemId) { - return items.removeIf(item -> itemId.equalsIgnoreCase(item.getItemId())); - } - - public void merge(Cart cart) { - customerId = cart.getCustomerId(); - for (Item item : cart.items) { - mergeItem(item); - } - } - - private void mergeItem(Item item) { - for (Item existing : items) { - if (existing.getItemId().equals(item.getItemId())) { - existing.setQuantity(existing.getQuantity() + item.getQuantity()); - return; - } - } - items.add(item); - } - - @Override - public String toString() { - return "Cart [customerId=" + customerId + - ", id=" + id + - ", items=" + items + - "]"; - } -} +package mushop.carts.entities; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Function; +import java.util.stream.Collectors; + +import io.micronaut.data.annotation.Id; +import io.micronaut.data.annotation.MappedEntity; + +@MappedEntity +public record Cart(@Id String id, String customerId, Map items) { + + public static Cart of(String customerId, List items) { + return new Cart(UUID.randomUUID().toString(), customerId, items); + } + + public Cart(@Id String id, String customerId, List itemList) { + this(id, customerId, itemList.stream().collect(Collectors.toMap(Item::itemId, Function.identity()))); + } + + public boolean removeItem(String itemId) { + Item removed = items.remove(itemId); + return removed != null; + } + + public void updateItem(Item item) { + items.put(item.itemId(), item); + } + + public Cart merge(Cart cart) { + return new Cart(id, cart.customerId(), mergeItems(cart.items)); + } + + private Map mergeItems(Map newItems) { + Map result = new HashMap<>(items); + for (Item newItem : newItems.values()) { + result.computeIfPresent(newItem.itemId(), (itemId, existing) -> new Item(existing.id(), itemId, existing.quantity() + newItem.quantity(), existing.unitPrice())); + result.putIfAbsent(newItem.itemId(), newItem); + } + return result; + } + + public Cart withId(String cartId) { + return new Cart(cartId, customerId, items); + } + + public List getItems() { + return Optional.ofNullable(items) + .map(Map::values) + .map(ArrayList::new) + .orElseGet(ArrayList::new); + } +} diff --git a/src/carts/lib/src/main/java/mushop/carts/entities/Item.java b/src/carts/lib/src/main/java/mushop/carts/entities/Item.java index 497d19f2..4f406119 100644 --- a/src/carts/lib/src/main/java/mushop/carts/entities/Item.java +++ b/src/carts/lib/src/main/java/mushop/carts/entities/Item.java @@ -1,67 +1,20 @@ -package mushop.carts.entities; - -import java.math.BigDecimal; -import java.util.UUID; -import javax.validation.constraints.NotNull; - -import io.micronaut.data.annotation.Id; -import io.micronaut.data.annotation.MappedEntity; - -@MappedEntity -public class Item { - - @Id - private String id; - - @NotNull - private String itemId; - - private int quantity; - private BigDecimal unitPrice; - - public Item() { - id = UUID.randomUUID().toString(); - quantity = 1; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getItemId() { - return itemId; - } - - public void setItemId(String itemId) { - this.itemId = itemId; - } - - public int getQuantity() { - return quantity; - } - - public void setQuantity(int quantity) { - this.quantity = quantity; - } - - public BigDecimal getUnitPrice() { - return unitPrice; - } - - public void setUnitPrice(BigDecimal unitPrice) { - this.unitPrice = unitPrice; - } - - @Override - public String toString() { - return "Item [id=" + id + - ", itemId=" + itemId + - ", quantity=" + quantity + - ", unitPrice=" + unitPrice + - "]"; - } -} +package mushop.carts.entities; + +import java.math.BigDecimal; +import java.util.UUID; + +import io.micronaut.data.annotation.Id; +import io.micronaut.data.annotation.MappedEntity; + +@MappedEntity +public record Item(@Id String id, String itemId, int quantity, BigDecimal unitPrice) { + + public static Item of(String itemId, int quantity, BigDecimal unitPrice) { + return new Item(UUID.randomUUID().toString(), itemId, quantity, unitPrice); + + } + + public Item withQuantity(int newQuantity) { + return new Item(id, itemId, newQuantity, unitPrice); + } +} diff --git a/src/carts/lib/src/main/resources/logback.xml b/src/carts/lib/src/main/resources/logback.xml index 37634848..bf96140c 100644 --- a/src/carts/lib/src/main/resources/logback.xml +++ b/src/carts/lib/src/main/resources/logback.xml @@ -1,15 +1,14 @@ - - - - true - - - %cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n - - - - - - + + + + + + %cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n + + + + + + \ No newline at end of file diff --git a/src/carts/lib/src/test/java/mushop/carts/entities/CartTest.java b/src/carts/lib/src/test/java/mushop/carts/entities/CartTest.java new file mode 100644 index 00000000..3365c37c --- /dev/null +++ b/src/carts/lib/src/test/java/mushop/carts/entities/CartTest.java @@ -0,0 +1,83 @@ +package mushop.carts.entities; + +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class CartTest { + + @Test + void testRemoveItem() { + String itemId = "itemId"; + + Item item = Item.of(itemId, 0, new BigDecimal(0)); + Cart cart = new Cart(null, null, List.of(item)); + + assertEquals(1, cart.items().size()); + assertTrue(cart.removeItem(itemId)); + assertEquals(0, cart.items().size()); + assertFalse(cart.removeItem(itemId)); + } + + @Test + void testMergeCartsSameItems() { + String itemId = "itemId"; + + Item cart1Item = Item.of(itemId, 1, new BigDecimal(10)); + Cart cart1 = new Cart(null, null, List.of(cart1Item)); + + Item cart2Item = Item.of(itemId, 3, new BigDecimal(20)); + Cart cart2 = new Cart(null, null, List.of(cart2Item)); + + Cart merged = cart1.merge(cart2); + assertEquals(1, merged.items().size()); + + Item first = merged.getItems().getFirst(); + assertEquals(4, first.quantity()); + assertEquals(10, first.unitPrice().intValue()); + } + + @Test + void testMergeCartsMoreItems() { + Item cart1item1 = Item.of("1", 1, new BigDecimal(1)); + Item cart2item1 = Item.of("1", 2, new BigDecimal(1)); + + Item item2 = Item.of("2", 2, new BigDecimal(2)); + + Cart cart1 = new Cart(null, null, List.of(cart1item1)); + Cart cart2 = new Cart(null, null, List.of(cart2item1, item2)); + + Cart merged = cart1.merge(cart2); + assertEquals(2, merged.items().size()); + + Item mergedItem1 = merged.getItems().stream().filter(item -> item.itemId().equals("1")).findFirst().get(); + assertEquals(3, mergedItem1.quantity()); + + Item mergedItem2 = merged.getItems().stream().filter(item -> item.itemId().equals("2")).findFirst().get(); + assertEquals(2, mergedItem2.quantity()); + } + + @Test + void testMergeCartsLessItems() { + Item cart1item1 = Item.of("1", 1, new BigDecimal(1)); + Item cart2item1 = Item.of("1", 2, new BigDecimal(1)); + + Item item2 = Item.of("2", 2, new BigDecimal(2)); + + Cart cart1 = new Cart(null, null, List.of(cart1item1, item2)); + Cart cart2 = new Cart(null, null, List.of(cart2item1)); + + Cart merged = cart1.merge(cart2); + assertEquals(2, merged.items().size()); + + Item mergedItem1 = merged.getItems().stream().filter(item -> item.itemId().equals("1")).findFirst().get(); + assertEquals(3, mergedItem1.quantity()); + + Item mergedItem2 = merged.getItems().stream().filter(item -> item.itemId().equals("2")).findFirst().get(); + assertEquals(2, mergedItem2.quantity()); + } +} \ No newline at end of file diff --git a/src/carts/oci/pom.xml b/src/carts/oci/pom.xml index f4307aa4..c6116cbb 100644 --- a/src/carts/oci/pom.xml +++ b/src/carts/oci/pom.xml @@ -1,71 +1,84 @@ - - - - micronaut.mushop.carts - carts-parent - ${revision} - - 4.0.0 - - carts-oci - - ${packaging} - - - jar - mushop.carts.OciApplication - - - - - micronaut.mushop.carts - carts-lib - ${project.version} - - - - - com.oracle.oci.sdk - oci-java-sdk-monitoring - compile - - - io.micronaut.oraclecloud - micronaut-oraclecloud-micrometer - compile - - - - micronaut.mushop.carts - carts-tck - ${project.version} - test - - - - - - - io.micronaut.build - micronaut-maven-plugin - - - -H:AdditionalSecurityProviders=com.sun.security.sasl.Provider - - - - - com.google.cloud.tools - jib-maven-plugin - - - phx.ocir.io/oraclelabs/micronaut-showcase/mushop/${project.artifactId}-${docker.image.suffix}:${project.version} - - - - - - - + + + + micronaut.mushop.carts + carts-parent + 0.1 + + 4.0.0 + + carts-oci + + ${packaging} + + + jar + mushop.carts.OciApplication + + + + + micronaut.mushop.carts + carts-lib + ${project.version} + + + + + com.oracle.oci.sdk + oci-java-sdk-monitoring + compile + + + io.micronaut.oraclecloud + micronaut-oraclecloud-micrometer + compile + + + org.yaml + snakeyaml + runtime + + + micronaut.mushop.carts + carts-tck + ${project.version} + test + + + + + + io.micronaut.maven + micronaut-maven-plugin + + + -H:AdditionalSecurityProviders=com.sun.security.sasl.Provider + -H:-UseServiceLoaderFeature + + + + io.micronaut.testresources + micronaut-test-resources-mongodb + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + + + com.google.cloud.tools + jib-maven-plugin + + + phx.ocir.io/oraclelabs/micronaut-showcase/mushop/${project.artifactId}-${docker.image.suffix}:${project.version} + + + + + + diff --git a/src/carts/pom.xml b/src/carts/pom.xml index cad412fb..f4640acf 100644 --- a/src/carts/pom.xml +++ b/src/carts/pom.xml @@ -1,131 +1,161 @@ - - - - 4.0.0 - - - app - aws - oci - lib - tck - - - - io.micronaut - micronaut-parent - 3.7.3 - - - micronaut.mushop.carts - carts-parent - ${revision} - - carts - Cart service - - pom - - - 2.0.0-SNAPSHOT - 11 - 11 - netty - 3.7.3 - graalvm - - - - - central - https://repo.maven.apache.org/maven2 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - - - - - io.micronaut - micronaut-inject-java - ${micronaut.version} - - - io.micronaut.data - micronaut-data-processor - ${micronaut.data.version} - - - io.micronaut - micronaut-graal - ${micronaut.version} - - - io.micronaut - micronaut-http-validation - ${micronaut.version} - - - io.micronaut - micronaut-validation - ${micronaut.version} - - - io.micronaut.data - micronaut-data-document-processor - ${micronaut.data.version} - - - io.micronaut.openapi - micronaut-openapi - ${micronaut.openapi.version} - - - io.micronaut.micrometer - micronaut-micrometer-annotation - ${micronaut.micrometer.version} - - - - -Amicronaut.processing.group=mushop - -Amicronaut.processing.module=carts - - - - - org.codehaus.mojo - flatten-maven-plugin - 1.1.0 - - true - resolveCiFriendliesOnly - - - - flatten - process-resources - - flatten - - - - flatten.clean - clean - - clean - - - - - - - + + + + 4.0.0 + + + app + aws + oci + lib + tck + + + + io.micronaut.platform + micronaut-parent + 4.3.4 + + micronaut.mushop.carts + carts-parent + 0.1 + + carts + Cart service + + pom + + + 21 + 21 + 4.3.4 + netty + false + mushop.carts.Application + 4.5.2 + true + 2.4.0 + mushop.micronaut.aot.generated + graalvm + + + + + central + https://repo.maven.apache.org/maven2 + + + + + + + + io.micronaut.maven + micronaut-maven-plugin + + aot-${packaging}.properties + + + + org.apache.maven.plugins + maven-enforcer-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + + + + + 21 + 21 + + + + io.micronaut + micronaut-inject-java + ${micronaut.core.version} + + + io.micronaut.data + micronaut-data-processor + ${micronaut.data.version} + + + io.micronaut + micronaut-graal + ${micronaut.core.version} + + + io.micronaut + micronaut-http-validation + ${micronaut.core.version} + + + io.micronaut.serde + micronaut-serde-processor + ${micronaut.serialization.version} + + + io.micronaut.data + micronaut-data-document-processor + ${micronaut.data.version} + + + io.micronaut + micronaut-inject + + + + + io.micronaut.openapi + micronaut-openapi + ${micronaut.openapi.version} + + + io.micronaut.micrometer + micronaut-micrometer-annotation + ${micronaut.micrometer.version} + + + io.micronaut.validation + micronaut-validation-processor + ${micronaut.validation.version} + + + + -Amicronaut.processing.group=mushop + -Amicronaut.processing.module=carts + + + + + org.codehaus.mojo + flatten-maven-plugin + 1.1.0 + + true + resolveCiFriendliesOnly + + + + flatten + process-resources + + flatten + + + + flatten.clean + clean + + clean + + + + + + + diff --git a/src/carts/tck/pom.xml b/src/carts/tck/pom.xml index 842fb44d..2a676744 100644 --- a/src/carts/tck/pom.xml +++ b/src/carts/tck/pom.xml @@ -1,48 +1,62 @@ - - - - micronaut.mushop.carts - carts-parent - ${revision} - - 4.0.0 - - carts-tck - - jar - - - - micronaut.mushop.carts - carts-lib - ${project.version} - - - org.junit.jupiter - junit-jupiter-api - - - org.junit.jupiter - junit-jupiter-engine - - - org.testcontainers - junit-jupiter - - - io.micronaut.test - micronaut-test-junit5 - - - org.hamcrest - hamcrest - - - org.testcontainers - mongodb - - - - + + + + micronaut.mushop.carts + carts-parent + 0.1 + + 4.0.0 + + carts-tck + + jar + + + micronaut.mushop.carts + carts-lib + ${project.version} + + + org.junit.jupiter + junit-jupiter-api + + + org.junit.jupiter + junit-jupiter-engine + + + org.testcontainers + junit-jupiter + + + io.micronaut.test + micronaut-test-junit5 + + + org.hamcrest + hamcrest + + + io.micronaut.testresources + micronaut-test-resources-client + + + org.yaml + snakeyaml + runtime + + + + + + org.apache.maven.plugins + maven-shade-plugin + + true + + + + + diff --git a/src/carts/tck/src/main/java/mushop/carts/AbstractCartRepositoryMongoTest.java b/src/carts/tck/src/main/java/mushop/carts/AbstractCartRepositoryMongoTest.java index d4f938c0..4f9b4ffd 100644 --- a/src/carts/tck/src/main/java/mushop/carts/AbstractCartRepositoryMongoTest.java +++ b/src/carts/tck/src/main/java/mushop/carts/AbstractCartRepositoryMongoTest.java @@ -1,86 +1,59 @@ -/* - * Copyright 2021 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package mushop.carts; - -import io.micronaut.test.support.TestPropertyProvider; -import mushop.carts.entities.Cart; -import mushop.carts.entities.Item; -import mushop.carts.repositories.CartRepository; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -import org.testcontainers.containers.MongoDBContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; - -import jakarta.inject.Inject; -import java.math.BigDecimal; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -@Testcontainers -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -abstract class AbstractCartRepositoryMongoTest implements TestPropertyProvider { - - @Container - final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10")); - - @Inject - CartRepository cartRepository; - - @Override - public Map getProperties() { - mongoDBContainer.start(); - return Map.of( - "mongodb.uri", mongoDBContainer.getReplicaSetUrl("mushop"), - "mongodb.package-names", "mushop.carts", - "micronaut.data.mongodb.create-collections", "true", - "carts.database", "mushop" - ); - } - - @Test - void testCartRepositoryResolution(){ - assertNotNull(cartRepository); - } - - @Test - void testCartRepository() { - Cart cart = new Cart("1234"); - cart.setCustomerId("abcd"); - Item item = new Item(); - item.setItemId("item id"); - item.setId("ab"); - item.setQuantity(3); - item.setUnitPrice(new BigDecimal(101)); - cart.getItems().add(item); - cartRepository.save(cart); - - Optional acart = cartRepository.findById("1234"); - assertTrue(acart.isPresent()); - assertEquals(cart.getCustomerId(), acart.get().getCustomerId()); - - List customerCart = cartRepository.getByCustomerId("abcd"); - assertNotNull(customerCart); - assertEquals(1, customerCart.size()); - assertEquals(cart.getCustomerId(), customerCart.get(0).getCustomerId()); - } -} +/* + * Copyright 2021 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package mushop.carts; + +import java.math.BigDecimal; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +import jakarta.inject.Inject; +import mushop.carts.entities.Cart; +import mushop.carts.entities.Item; +import mushop.carts.repositories.CartRepository; + +abstract class AbstractCartRepositoryMongoTest { + + @Inject + CartRepository cartRepository; + + + @Test + void testCartRepositoryResolution(){ + assertNotNull(cartRepository); + } + + @Test + void testCartRepository() { + Item item = new Item("ab", "item id", 3, new BigDecimal(101)); + Cart cart = new Cart("1234", "abcd", List.of(item)); + cart.getItems().add(item); + cartRepository.save(cart); + + Optional acart = cartRepository.findById("1234"); + assertTrue(acart.isPresent()); + assertEquals(cart.customerId(), acart.get().customerId()); + + List customerCart = cartRepository.getByCustomerId("abcd"); + assertNotNull(customerCart); + assertEquals(1, customerCart.size()); + assertEquals(cart.customerId(), customerCart.get(0).customerId()); + } +} diff --git a/src/carts/tck/src/main/java/mushop/carts/AbstractTestCartService.java b/src/carts/tck/src/main/java/mushop/carts/AbstractTestCartService.java index 2dbdd9e5..8bfc98ee 100644 --- a/src/carts/tck/src/main/java/mushop/carts/AbstractTestCartService.java +++ b/src/carts/tck/src/main/java/mushop/carts/AbstractTestCartService.java @@ -1,95 +1,60 @@ -package mushop.carts; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.math.BigDecimal; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import jakarta.inject.Inject; - -import com.fasterxml.jackson.databind.JsonNode; -import io.micronaut.core.annotation.NonNull; -import io.micronaut.core.type.Argument; -import io.micronaut.http.HttpRequest; -import io.micronaut.http.HttpResponse; -import io.micronaut.http.HttpStatus; -import io.micronaut.http.client.HttpClient; -import io.micronaut.http.client.annotation.Client; -import io.micronaut.test.support.TestPropertyProvider; -import mushop.carts.entities.Cart; -import mushop.carts.entities.Item; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -import org.testcontainers.containers.MongoDBContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; - -@Testcontainers -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -abstract class AbstractTestCartService implements TestPropertyProvider { - - @Container - final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10")); - - @Inject - @Client("/") - HttpClient httpClient; - - @NonNull - @Override - public Map getProperties() { - mongoDBContainer.start(); - return Map.of( - "mongodb.uri", mongoDBContainer.getReplicaSetUrl(), - "mongodb.package-names", "mushop.carts" - ); - } - - @Test - public void testMetricsJson() { - JsonNode result = httpClient.toBlocking().retrieve("/metrics", JsonNode.class); - assertThat(result.get("names").size(), greaterThan(0)); - } - - @Test - public void testHealthCheck(){ - Arrays.asList("/health", "/health/liveness", "/health/readiness").forEach(uri -> { - HttpResponse result = httpClient.toBlocking().exchange(uri, JsonNode.class); - assertThat(result.status(), equalTo(HttpStatus.OK)); - assertThat(result.body().get("status").asText(), - Matchers.either(Matchers.is("UP")).or(Matchers.is("UNKNOWN"))); - }); - } - - @Test - public void testStoreCart() { - Item i = new Item(); - i.setUnitPrice(BigDecimal.valueOf(123)); - i.setQuantity(47); - i.setItemId("I123"); - - Cart c = new Cart(); - c.setCustomerId("c1"); - c.getItems().add(i); - - HttpResponse created = httpClient.toBlocking().exchange(HttpRequest.POST("/carts/" + c.getId(), c), Cart.class); - assertEquals(HttpStatus.CREATED, created.getStatus()); - assertEquals(c.getId(), created.body().getId()); - - HttpResponse> items = httpClient.toBlocking().exchange(HttpRequest.GET("/carts/" + c.getId() + "/items"), Argument.listOf(Item.class)); - assertEquals(1, items.body().size()); - assertEquals(i.getId(), items.body().get(0).getId()); - - HttpResponse deleteItem = httpClient.toBlocking().exchange(HttpRequest.DELETE("/carts/" + c.getId() + "/items/" + i.getItemId()), Cart.class); - assertEquals(HttpStatus.OK, deleteItem.getStatus()); - - items = httpClient.toBlocking().exchange(HttpRequest.GET("/carts/" + c.getId() + "/items"), Argument.listOf(Item.class)); - assertEquals(0, items.body().size()); - } -} +package mushop.carts; + +import java.math.BigDecimal; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +import io.micronaut.core.type.Argument; +import io.micronaut.http.HttpRequest; +import io.micronaut.http.HttpResponse; +import io.micronaut.http.HttpStatus; +import io.micronaut.http.client.HttpClient; +import io.micronaut.http.client.annotation.Client; +import io.micronaut.json.tree.JsonNode; +import jakarta.inject.Inject; +import mushop.carts.entities.Cart; +import mushop.carts.entities.Item; +abstract class AbstractTestCartService { + + + @Inject + @Client("/") + HttpClient client; + + + @Test + public void testMetricsJson() { + JsonNode result = client.toBlocking().retrieve("/metrics", JsonNode.class); + assertThat(result.get("names").size(), greaterThan(0)); + } + + + + @Test + public void testStoreCart() { + Item i = Item.of("I123", 47, BigDecimal.valueOf(123)); + Cart c = Cart.of("c1", List.of(i)); + + HttpResponse created = client.toBlocking().exchange(HttpRequest.POST("/carts/" + c.id(), c), Cart.class); + assertEquals(HttpStatus.CREATED, created.getStatus()); + assertEquals(c.id(), created.body().id()); + + HttpResponse> items = client.toBlocking().exchange(HttpRequest.GET("/carts/" + c.id() + "/items"), Argument.listOf(Item.class)); + assertEquals(1, items.body().size()); + assertEquals(i.id(), items.body().get(0).id()); + + HttpResponse updateItem = client.toBlocking().exchange(HttpRequest.PUT("/carts/" + c.id() + "/items", i.withQuantity(23)), Cart.class); + assertEquals(HttpStatus.OK, updateItem.getStatus()); + assertEquals(23, updateItem.body().getItems().getFirst().quantity()); + + HttpResponse deleteItem = client.toBlocking().exchange(HttpRequest.DELETE("/carts/" + c.id() + "/items/" + i.itemId()), Cart.class); + assertEquals(HttpStatus.OK, deleteItem.getStatus()); + + items = client.toBlocking().exchange(HttpRequest.GET("/carts/" + c.id() + "/items"), Argument.listOf(Item.class)); + assertEquals(0, items.body().size()); + } +}