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 b6823b09..63cbe8a5 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 @@ -1,6 +1,7 @@ package io.zenwave360.sdk.zdl; import io.zenwave360.sdk.utils.JSONPath; +import org.apache.commons.lang3.ObjectUtils; import java.util.ArrayList; import java.util.HashSet; @@ -65,22 +66,24 @@ public static String findServiceName(String entityName, Map mode var entity = (Map) JSONPath.get(model, "$.allEntitiesAndEnums." + entityName, Map.of()); var allServices = JSONPath.get(model, "$.services[*]", List.of()); if ("entities".equals(entity.get("type"))) { - return allServices.stream() - .filter(service -> JSONPath.get(service, "$.aggregates", List.of()).contains(entityName)) - .map(service -> (String) service.get("name")).findFirst().orElse(null); + var aggregateService = _findServiceName(allServices, entityName, "$.aggregates"); + var parameterService = _findServiceName(allServices, entityName, "$.methods[*].parameter"); + return ObjectUtils.firstNonNull(aggregateService, parameterService); } if ("inputs".equals(entity.get("type"))) { - return allServices.stream() - .filter(service -> JSONPath.get(service, "$.methods[*].parameter", List.of()).contains(entityName)) - .map(service -> (String) service.get("name")).findFirst().orElse(null); + return _findServiceName(allServices, entityName, "$.methods[*].parameter"); } if ("outputs".equals(entity.get("type"))) { - return allServices.stream().filter(service -> JSONPath.get(service, "$.methods[*].returnType", List.of()).contains(entityName)) - .map(service -> (String) service.get("name")).findFirst().orElse(null); + return _findServiceName(allServices, entityName, "$.methods[*].returnType"); } return null; } + private static String _findServiceName(List services, String entityName, String jsonPath) { + return services.stream().filter(service -> JSONPath.get(service, jsonPath, List.of()).contains(entityName)) + .map(service -> (String) service.get("name")).findFirst().orElse(null); + } + public static Map findServiceMethod(String operationId, Map model) { var methods = JSONPath.get(model, "$.services[*].methods[*]", List.of()); return methods.stream() 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 5d50c3c0..f422df03 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 @@ -66,4 +66,24 @@ public void testFindDependentEntitiesRelational() throws Exception { var entities = ZDLFindUtils.findDependentEntities((Map) model.get("zdl"), "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")); + Assertions.assertEquals("AttachmentService", serviceName); + + serviceName = ZDLFindUtils.findServiceName("OrderBusinessId", (Map) model.get("zdl")); + Assertions.assertEquals("AttachmentService", serviceName); + + serviceName = ZDLFindUtils.findServiceName("AttachmentFileId", (Map) model.get("zdl")); + 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")); + Assertions.assertEquals("AttachmentService", method.get("serviceName")); + } } 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 5cbfa63c..ae0d2da6 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 @@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test; import java.io.IOException; +import java.util.List; import java.util.Map; public class ZDLJavaSignatureUtilsTest { @@ -34,6 +35,23 @@ void methodReturnType() throws IOException { Assertions.assertEquals("PurchaseOrder", returnType); } + @Test + void methodReturnTypeArray() throws IOException { + var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); + var method = JSONPath.get(model, "$.services.AttachmentService.methods.listAttachmentFiles", Map.of()); + var returnType = ZDLJavaSignatureUtils.methodReturnType(method); + Assertions.assertEquals("List", returnType); + } + + @Test + void methodReturnTypeOptional() throws IOException { + var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); + var method = JSONPath.get(model, "$.services.AttachmentService.methods.updatePurchaseOrder", Map.of()); + var returnType = ZDLJavaSignatureUtils.methodReturnType(method); + Assertions.assertEquals("Optional", returnType); + } + + @Test void fieldType() throws IOException { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); @@ -60,6 +78,16 @@ void populateField_String() throws IOException { Assertions.assertEquals("\"\"", fieldTypeInitializer); } + @Test + void populateField_all_types() throws IOException { + var model = loadZDL("classpath:io/zenwave360/sdk/zdl/populate-fields.zdl"); + var fields = JSONPath.get(model, "$.entities.Entity.fields[*]", List.of()); + for (var field : fields) { + var fieldTypeInitializer = ZDLJavaSignatureUtils.populateField(field); + Assertions.assertEquals(((String)field.get("javadoc")).trim(), fieldTypeInitializer); + } + } + @Test void populateField_enum() throws IOException { var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl"); diff --git a/zenwave-sdk-cli/src/test/resources/io/zenwave360/sdk/zdl/populate-fields.zdl b/zenwave-sdk-cli/src/test/resources/io/zenwave360/sdk/zdl/populate-fields.zdl new file mode 100644 index 00000000..a465757b --- /dev/null +++ b/zenwave-sdk-cli/src/test/resources/io/zenwave360/sdk/zdl/populate-fields.zdl @@ -0,0 +1,15 @@ + +entity Entity { + localDate LocalDate /** LocalDate.now() */ + zoneDateTime ZonedDateTime /** ZonedDateTime.now() */ + instant Instant /** Instant.now() */ + duration Duration /** Duration.ofSeconds(0) */ + integer Integer /** 0 */ + long Long /** 0L */ + float Float /** 0.0F */ + double Double /** 0.0 */ + bigDecimal BigDecimal /** BigDecimal.valueOf(0) */ + boolean Boolean /** false */ + uuid UUID /** UUID.randomUUID() */ + blob Blob /** null */ +}