Skip to content

Commit

Permalink
improves test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Jan 16, 2023
1 parent 7c22f48 commit 0ab117c
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public static List<String> findDependentEntities(Map<String, Object> model, List
}

public static List<String> findMethodParameterAndReturnTypes(Map<String, Object> model) {
var entities = JSONPath.get(model, "$.services[*].methods[*]['parameter','returnType']");
return new ArrayList(new HashSet(JSONPath.get(entities, "$[*][*]")));
var entities = new ArrayList<String>();
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<String, Object> model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(", "));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,94 @@
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;

public class ZDLFindUtilsTest {

private Map<String, Object> loadZDL(String resource) throws IOException {
Map<String, Object> model = new ZDLParser().withSpecFile(resource).parse();
return new ZDLProcessor().process(model);
return (Map<String, Object>) 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 {
Expand All @@ -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");
Expand All @@ -81,6 +113,15 @@ void methodReturnTypeArray() throws IOException {
Assertions.assertEquals("List<AttachmentFile>", 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<AttachmentFile>", returnType);
}


@Test
void methodReturnTypeOptional() 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
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.zenwave360.sdk.jpa2jdl;

public enum PostType {
POST, PAGE
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.zenwave360.sdk.mongodb2jdl;

public enum PostType {
POST, PAGE
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0ab117c

Please sign in to comment.