From 0ab117cca4eaea08a4a88f30dc5ad04d78bf818d Mon Sep 17 00:00:00 2001 From: Ivan Garcia Sainz-Aja Date: Mon, 16 Jan 2023 20:18:34 +0100 Subject: [PATCH] improves test coverage. --- .../BackendApplicationDefaultHelpers.java | 2 +- .../io/zenwave360/sdk/zdl/ZDLFindUtils.java | 6 ++- .../sdk/zdl/ZDLJavaSignatureUtils.java | 4 +- .../zenwave360/sdk/zdl/ZDLFindUtilsTest.java | 42 ++++++++++++------- .../sdk/zdl/ZDLJavaSignatureUtilsTest.java | 41 ++++++++++++++++++ .../java/io/zenwave360/sdk/jpa2jdl/Post.java | 16 ++----- .../io/zenwave360/sdk/jpa2jdl/PostType.java | 5 +++ .../io/zenwave360/sdk/mongodb2jdl/Post.java | 3 ++ .../zenwave360/sdk/mongodb2jdl/PostType.java | 5 +++ .../zdl/order-faults-attachments-model.zdl | 8 +++- 10 files changed, 99 insertions(+), 33 deletions(-) create mode 100644 zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/jpa2jdl/PostType.java create mode 100644 zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/mongodb2jdl/PostType.java diff --git a/plugins/backend-application-default/src/main/java/io/zenwave360/sdk/plugins/BackendApplicationDefaultHelpers.java b/plugins/backend-application-default/src/main/java/io/zenwave360/sdk/plugins/BackendApplicationDefaultHelpers.java index 3304f082..7927002f 100644 --- a/plugins/backend-application-default/src/main/java/io/zenwave360/sdk/plugins/BackendApplicationDefaultHelpers.java +++ b/plugins/backend-application-default/src/main/java/io/zenwave360/sdk/plugins/BackendApplicationDefaultHelpers.java @@ -140,7 +140,7 @@ public String mapperInputSignature(String inputType, Options options) { public String mapperInputCallSignature(String inputType, Options options) { var zdl = (Map) options.get("zdl"); - return ZDLJavaSignatureUtils.mapperInputCallSignature(inputType, zdl, generator.inputDTOSuffix); + return ZDLJavaSignatureUtils.mapperInputCallSignature(inputType, zdl); } public String inputFieldInitializer(String inputType, Options options) { diff --git a/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/zdl/ZDLFindUtils.java b/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/zdl/ZDLFindUtils.java index c24fdadd..4cb6ebb4 100644 --- a/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/zdl/ZDLFindUtils.java +++ b/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/zdl/ZDLFindUtils.java @@ -59,8 +59,10 @@ public static List findDependentEntities(Map model, List } public static List findMethodParameterAndReturnTypes(Map model) { - var entities = JSONPath.get(model, "$.services[*].methods[*]['parameter','returnType']"); - return new ArrayList(new HashSet(JSONPath.get(entities, "$[*][*]"))); + var entities = new ArrayList(); + entities.addAll(JSONPath.get(model, "$.services[*].methods[*]['parameter']")); + entities.addAll(JSONPath.get(model, "$.services[*].methods[*]['returnType']")); + return entities.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList()); } public static String findServiceName(String entityName, Map model) { diff --git a/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/zdl/ZDLJavaSignatureUtils.java b/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/zdl/ZDLJavaSignatureUtils.java index 02d413a1..693fa21b 100644 --- a/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/zdl/ZDLJavaSignatureUtils.java +++ b/zenwave-sdk-cli/src/main/java/io/zenwave360/sdk/zdl/ZDLJavaSignatureUtils.java @@ -47,8 +47,8 @@ public static String mapperInputSignature(String inputType, Map zdl, String inpu return StringUtils.join(inputSignature(inputType, null, zdl, inputDTOSuffix), ", "); } - public static String mapperInputCallSignature(String inputType, Map zdl, String inputDTOSuffix) { - return inputSignature(inputType, null, zdl, inputDTOSuffix).stream() + public static String mapperInputCallSignature(String inputType, Map zdl) { + return inputSignature(inputType, null, zdl, "notused").stream() .map(p -> p.split(" ")[1]) .collect(Collectors.joining(", ")); } diff --git a/zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/zdl/ZDLFindUtilsTest.java b/zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/zdl/ZDLFindUtilsTest.java index f422df03..c687153e 100644 --- a/zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/zdl/ZDLFindUtilsTest.java +++ b/zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/zdl/ZDLFindUtilsTest.java @@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -15,75 +17,85 @@ public class ZDLFindUtilsTest { private Map loadZDL(String resource) throws IOException { Map model = new ZDLParser().withSpecFile(resource).parse(); - return new ZDLProcessor().process(model); + return (Map) new ZDLProcessor().process(model).get("zdl"); } @Test void testFindAllServiceFacingEntities() throws IOException { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); - var entities = ZDLFindUtils.findAllServiceFacingEntities((Map) model.get("zdl")); - Assertions.assertEquals(List.of("OrderBusinessId", "OrderFaultType", "AttachmentFileId", "AttachmentFileOutput", "AttachmentFile", "PurchaseOrder", "OrderBusinessId", "OrderStatus", "AttachmentFile"), entities); + var entities = ZDLFindUtils.findAllServiceFacingEntities(model); + Collections.sort(entities); + Assertions.assertEquals(List.of("AttachmentFile", "AttachmentFile", "AttachmentFileId", "AttachmentFileOutput", "OrderBusinessId", "OrderBusinessId", "OrderFaultType", "OrderStatus", "PurchaseOrder"), entities); } @Test void testFindAllPaginatedEntities() throws IOException { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); - var entities = ZDLFindUtils.findAllPaginatedEntities((Map) model.get("zdl")); - Assertions.assertEquals(List.of(), entities); + var entities = ZDLFindUtils.findAllPaginatedEntities(model); + Assertions.assertEquals(List.of("AttachmentFile"), entities); } @Test void testFindAllEntitiesReturnedAsList() throws IOException { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); - var entities = ZDLFindUtils.findAllEntitiesReturnedAsList((Map) model.get("zdl")); + var entities = ZDLFindUtils.findAllEntitiesReturnedAsList(model); Assertions.assertEquals(List.of("AttachmentFile"), entities); } @Test void testFindMethodParameterAndReturnTypes() throws IOException { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); - var entities = ZDLFindUtils.findMethodParameterAndReturnTypes((Map) model.get("zdl")); - Assertions.assertEquals(List.of("OrderBusinessId", "AttachmentFileId", "AttachmentFileOutput", "AttachmentFile", "PurchaseOrder"), entities); + var entities = ZDLFindUtils.findMethodParameterAndReturnTypes(model); + Collections.sort(entities); + Assertions.assertEquals(List.of("AttachmentFile", "AttachmentFileId", "AttachmentFileOutput", "OrderBusinessId", "PurchaseOrder"), entities); } @Test public void testFindDependentEntitiesMongodb() throws Exception { var model = loadZDL("classpath:io/zenwave360/sdk/resources/jdl/orders-model.jdl"); - var entities = ZDLFindUtils.findDependentEntities((Map) model.get("zdl"), "CustomerOrder"); + var entities = ZDLFindUtils.findDependentEntities(model, "CustomerOrder"); Assertions.assertEquals(List.of("CustomerOrder", "OrderStatus", "Customer", "OrderedItem", "PaymentDetails", "ShippingDetails"), entities); } @Test public void testFindDependentEntitiesMongodbZdl() throws Exception { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); - var entities = ZDLFindUtils.findDependentEntities((Map) model.get("zdl"), "PurchaseOrder"); + var entities = ZDLFindUtils.findDependentEntities(model, "PurchaseOrder"); Assertions.assertEquals(List.of("PurchaseOrder", "OrderBusinessId", "OrderStatus", "AttachmentFile"), entities); } @Test public void testFindDependentEntitiesRelational() throws Exception { var model = loadZDL("classpath:io/zenwave360/sdk/resources/jdl/orders-model-relational.jdl"); - var entities = ZDLFindUtils.findDependentEntities((Map) model.get("zdl"), "CustomerOrder"); + var entities = ZDLFindUtils.findDependentEntities(model, "CustomerOrder"); Assertions.assertEquals(List.of("CustomerOrder", "OrderStatus", "OrderShippingDetails", "OrderShippingDetails2", "OrderedItem"), entities); } @Test public void testFindServiceName() throws Exception { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); - var serviceName = ZDLFindUtils.findServiceName("PurchaseOrder", (Map) model.get("zdl")); + var serviceName = ZDLFindUtils.findServiceName("PurchaseOrder", model); Assertions.assertEquals("AttachmentService", serviceName); - serviceName = ZDLFindUtils.findServiceName("OrderBusinessId", (Map) model.get("zdl")); + serviceName = ZDLFindUtils.findServiceName("OrderBusinessId", model); Assertions.assertEquals("AttachmentService", serviceName); - serviceName = ZDLFindUtils.findServiceName("AttachmentFileId", (Map) model.get("zdl")); + serviceName = ZDLFindUtils.findServiceName("AttachmentFileId", model); Assertions.assertEquals("AttachmentService", serviceName); } @Test public void testFindServiceMethod() throws Exception { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); - var method = ZDLFindUtils.findServiceMethod("uploadFile", (Map) model.get("zdl")); + var method = ZDLFindUtils.findServiceMethod("uploadFile", model); Assertions.assertEquals("AttachmentService", method.get("serviceName")); } + + @Test + public void methodEventsFlatList() throws Exception { + var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/customer-address.zdl"); + var method = ZDLFindUtils.findServiceMethod("createCustomer", model); + var events = ZDLFindUtils.methodEventsFlatList(method); + Assertions.assertEquals(List.of("CustomerEvent", "CustomerCreated", "CustomerCreatedFailed"), events); + } } diff --git a/zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/zdl/ZDLJavaSignatureUtilsTest.java b/zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/zdl/ZDLJavaSignatureUtilsTest.java index 1fe401ac..78d8812a 100644 --- a/zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/zdl/ZDLJavaSignatureUtilsTest.java +++ b/zenwave-sdk-cli/src/test/java/io/zenwave360/sdk/zdl/ZDLJavaSignatureUtilsTest.java @@ -44,6 +44,21 @@ void methodParametersCallSignature() throws IOException { Assertions.assertEquals("businessUnit, orderId, documentManagerId", signature); } + @Test + void mapperInputCallSignature() throws IOException { + var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); + var signature = ZDLJavaSignatureUtils.mapperInputCallSignature("PurchaseOrder", model); + Assertions.assertEquals("input", signature); + } + + @Test + void mapperInputCallSignatureInline() throws IOException { + var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); + var signature = ZDLJavaSignatureUtils.mapperInputCallSignature("AttachmentFileId", model); + Assertions.assertEquals("businessUnit, orderId, documentManagerId", signature); + } + + @Test void mapperInputSignature() throws IOException { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); @@ -64,6 +79,15 @@ void inputFieldInitializer() throws IOException { """, signature); } + @Test + void methodInputCall() throws IOException { + var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); + var method = JSONPath.get(model, "$.services.AttachmentService.methods.downloadAttachmentFile", Map.of()); + var inputDTOSuffix = "InputDTO"; + var signature = ZDLJavaSignatureUtils.methodInputCall(method, model, inputDTOSuffix); + Assertions.assertEquals(List.of("businessUnit", "orderId", "documentManagerId"), signature); + } + @Test void methodReturnType() throws IOException { @@ -73,6 +97,14 @@ void methodReturnType() throws IOException { Assertions.assertEquals("PurchaseOrder", returnType); } + @Test + void methodReturnTypeVoid() throws IOException { + var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); + var method = JSONPath.get(model, "$.services.AttachmentService.methods.startBackgroundAdminProcess", Map.of()); + var returnType = ZDLJavaSignatureUtils.methodReturnType(method); + Assertions.assertEquals("void", returnType); + } + @Test void methodReturnTypeArray() throws IOException { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); @@ -81,6 +113,15 @@ void methodReturnTypeArray() throws IOException { Assertions.assertEquals("List", returnType); } + @Test + void methodReturnTypeArrayPaginated() throws IOException { + var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); + var method = JSONPath.get(model, "$.services.AttachmentService.methods.listAttachmentFilesPaginated", Map.of()); + var returnType = ZDLJavaSignatureUtils.methodReturnType(method); + Assertions.assertEquals("Page", returnType); + } + + @Test void methodReturnTypeOptional() throws IOException { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); diff --git a/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/jpa2jdl/Post.java b/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/jpa2jdl/Post.java index ee4754de..4e9d6015 100644 --- a/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/jpa2jdl/Post.java +++ b/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/jpa2jdl/Post.java @@ -5,18 +5,7 @@ import java.util.HashSet; import java.util.Set; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.JoinTable; -import javax.persistence.Lob; -import javax.persistence.ManyToMany; -import javax.persistence.ManyToOne; -import javax.persistence.SequenceGenerator; -import javax.persistence.Table; +import javax.persistence.*; import javax.validation.constraints.NotNull; import org.hibernate.annotations.Cache; @@ -52,6 +41,9 @@ public class Post implements Serializable { @Column(name = "date", nullable = false) private Instant date; + @Enumerated + private PostType postType; + @ManyToOne private Blog blog; diff --git a/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/jpa2jdl/PostType.java b/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/jpa2jdl/PostType.java new file mode 100644 index 00000000..2cce03df --- /dev/null +++ b/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/jpa2jdl/PostType.java @@ -0,0 +1,5 @@ +package io.zenwave360.sdk.jpa2jdl; + +public enum PostType { + POST, PAGE +} diff --git a/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/mongodb2jdl/Post.java b/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/mongodb2jdl/Post.java index 65c12466..43719d24 100644 --- a/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/mongodb2jdl/Post.java +++ b/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/mongodb2jdl/Post.java @@ -36,6 +36,9 @@ public class Post implements Serializable { @Field("date") private Instant date; + @Field("postType") + private PostType postType; + @Field("blog") @DBRef private Blog blog; diff --git a/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/mongodb2jdl/PostType.java b/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/mongodb2jdl/PostType.java new file mode 100644 index 00000000..626eeaa6 --- /dev/null +++ b/zenwave-sdk-test-resources/src/main/java/io/zenwave360/sdk/mongodb2jdl/PostType.java @@ -0,0 +1,5 @@ +package io.zenwave360.sdk.mongodb2jdl; + +public enum PostType { + POST, PAGE +} diff --git a/zenwave-sdk-test-resources/src/main/resources/io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl b/zenwave-sdk-test-resources/src/main/resources/io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl index 67eebb16..ee6bdb8c 100644 --- a/zenwave-sdk-test-resources/src/main/resources/io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl +++ b/zenwave-sdk-test-resources/src/main/resources/io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl @@ -41,14 +41,20 @@ service AttachmentService for (PurchaseOrder) { @put("/{businessUnit}/{orderId}") updateFile(id, PurchaseOrder) PurchaseOrder - @put("/{purchaseOrderId}") + @put("/{purchaseOrderId}") updatePurchaseOrder(id, PurchaseOrder) PurchaseOrder? @get("/{businessUnit}/{orderId}") listAttachmentFiles(OrderBusinessId) AttachmentFile[] + @get("/{businessUnit}/{orderId}/paginated") @paginated + listAttachmentFilesPaginated(OrderBusinessId) AttachmentFile[] + + @get("/{businessUnit}/{orderId}/{documentManagerId}") downloadAttachmentFile(AttachmentFileId) AttachmentFileOutput + + startBackgroundAdminProcess() } @inline