Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds tests for ZDLFindUtils and ZDLJavaSignatureUtils #17

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -65,22 +66,24 @@ public static String findServiceName(String entityName, Map<String, Object> mode
var entity = (Map) JSONPath.get(model, "$.allEntitiesAndEnums." + entityName, Map.of());
var allServices = JSONPath.get(model, "$.services[*]", List.<Map>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<Map> 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<String, Object> findServiceMethod(String operationId, Map<String, Object> model) {
var methods = JSONPath.get(model, "$.services[*].methods[*]", List.<Map>of());
return methods.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<AttachmentFile>", 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<PurchaseOrder>", returnType);
}


@Test
void fieldType() throws IOException {
var model = loadZDL("classpath:io/zenwave360/sdk/resources/zdl/order-faults-attachments-model.zdl");
Expand All @@ -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.<Map>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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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 */
}
Loading