|
| 1 | +package io.zenwave360.zdl.antlr; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import io.zenwave360.zdl.ZflParser; |
| 5 | +import org.antlr.v4.runtime.CharStream; |
| 6 | +import org.antlr.v4.runtime.CharStreams; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import static io.zenwave360.zdl.antlr.JSONPath.get; |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +public class ZflListenerTest { |
| 17 | + |
| 18 | + ObjectMapper mapper = new ObjectMapper(); |
| 19 | + |
| 20 | + @Test |
| 21 | + public void parseZfl_Subscriptions() throws Exception { |
| 22 | + ZflModel model = parseZfl("src/test/resources/flow/subscriptions.zfl"); |
| 23 | + |
| 24 | + // Print the model for debugging |
| 25 | + System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(model)); |
| 26 | + |
| 27 | + // Test imports |
| 28 | + assertEquals(2, get(model, "$.imports", List.of()).size()); |
| 29 | + assertEquals("subscriptions", get(model, "$.imports[0].key")); |
| 30 | + assertEquals("http://localhost:8080/subscription/model.zdl", get(model, "$.imports[0].value")); |
| 31 | + assertEquals("payments", get(model, "$.imports[1].key")); |
| 32 | + assertEquals("com.example.domain:payments:RELEASE", get(model, "$.imports[1].value")); |
| 33 | + |
| 34 | + // Test flow |
| 35 | + assertEquals(1, get(model, "$.flows", Map.of()).size()); |
| 36 | + assertEquals("PaymentsFlow", get(model, "$.flows.PaymentsFlow.name")); |
| 37 | + assertEquals("PaymentsFlow", get(model, "$.flows.PaymentsFlow.className")); |
| 38 | + assertNotNull(get(model, "$.flows.PaymentsFlow.javadoc")); |
| 39 | + |
| 40 | + // Test systems |
| 41 | + assertEquals(3, get(model, "$.flows.PaymentsFlow.systems", Map.of()).size()); |
| 42 | + |
| 43 | + // Test Subscription system |
| 44 | + assertEquals("Subscription", get(model, "$.flows.PaymentsFlow.systems.Subscription.name")); |
| 45 | + assertEquals("subscription/model.zdl", get(model, "$.flows.PaymentsFlow.systems.Subscription.zdl")); |
| 46 | + assertEquals(1, get(model, "$.flows.PaymentsFlow.systems.Subscription.services", Map.of()).size()); |
| 47 | + assertEquals("SubscriptionService", get(model, "$.flows.PaymentsFlow.systems.Subscription.services.SubscriptionService.name")); |
| 48 | + assertEquals(List.of("renewSubscription", "suspendSubscription", "cancelRenewal"), |
| 49 | + get(model, "$.flows.PaymentsFlow.systems.Subscription.services.SubscriptionService.commands")); |
| 50 | + assertEquals(List.of("SubscriptionRenewed", "SubscriptionSuspended", "RenewalCancelled"), |
| 51 | + get(model, "$.flows.PaymentsFlow.systems.Subscription.events")); |
| 52 | + |
| 53 | + // Test Payments system |
| 54 | + assertEquals("Payments", get(model, "$.flows.PaymentsFlow.systems.Payments.name")); |
| 55 | + assertNull(get(model, "$.flows.PaymentsFlow.systems.Payments.zdl")); |
| 56 | + |
| 57 | + // Test Billing system |
| 58 | + assertEquals("Billing", get(model, "$.flows.PaymentsFlow.systems.Billing.name")); |
| 59 | + |
| 60 | + // Test start events |
| 61 | + assertEquals(3, get(model, "$.flows.PaymentsFlow.starts", Map.of()).size()); |
| 62 | + |
| 63 | + // Test CustomerRequestsSubscriptionRenewal start |
| 64 | + assertEquals("CustomerRequestsSubscriptionRenewal", |
| 65 | + get(model, "$.flows.PaymentsFlow.starts.CustomerRequestsSubscriptionRenewal.name")); |
| 66 | + assertEquals("Customer", |
| 67 | + get(model, "$.flows.PaymentsFlow.starts.CustomerRequestsSubscriptionRenewal.options.actor")); |
| 68 | + assertEquals(3, get(model, "$.flows.PaymentsFlow.starts.CustomerRequestsSubscriptionRenewal.fields", Map.of()).size()); |
| 69 | + assertEquals("String", get(model, "$.flows.PaymentsFlow.starts.CustomerRequestsSubscriptionRenewal.fields.subscriptionId.type")); |
| 70 | + assertEquals("String", get(model, "$.flows.PaymentsFlow.starts.CustomerRequestsSubscriptionRenewal.fields.customerId.type")); |
| 71 | + assertEquals("String", get(model, "$.flows.PaymentsFlow.starts.CustomerRequestsSubscriptionRenewal.fields.paymentMethodId.type")); |
| 72 | + |
| 73 | + // Test BillingCycleEnded start |
| 74 | + assertEquals("BillingCycleEnded", get(model, "$.flows.PaymentsFlow.starts.BillingCycleEnded.name")); |
| 75 | + assertEquals("end of month", get(model, "$.flows.PaymentsFlow.starts.BillingCycleEnded.options.time")); |
| 76 | + assertEquals(1, get(model, "$.flows.PaymentsFlow.starts.BillingCycleEnded.fields", Map.of()).size()); |
| 77 | + |
| 78 | + // Test PaymentTimeout start |
| 79 | + assertEquals("PaymentTimeout", get(model, "$.flows.PaymentsFlow.starts.PaymentTimeout.name")); |
| 80 | + assertEquals("5 minutes after SubscriptionRenewed and not PaymentSucceeded or PaymentFailed", |
| 81 | + get(model, "$.flows.PaymentsFlow.starts.PaymentTimeout.options.time")); |
| 82 | + |
| 83 | + // Test when blocks |
| 84 | + List<Map> whens = get(model, "$.flows.PaymentsFlow.whens", List.of()); |
| 85 | + assertEquals(5, whens.size()); |
| 86 | + |
| 87 | + // Test first when block |
| 88 | + assertEquals(List.of("CustomerRequestsSubscriptionRenewal"), get(whens.get(0), "$.triggers")); |
| 89 | + assertEquals(List.of("renewSubscription"), get(whens.get(0), "$.commands")); |
| 90 | + assertEquals(List.of("SubscriptionRenewed"), get(whens.get(0), "$.events")); |
| 91 | + |
| 92 | + // Test second when block |
| 93 | + assertEquals(List.of("SubscriptionRenewed"), get(whens.get(1), "$.triggers")); |
| 94 | + assertEquals(List.of("chargePayment"), get(whens.get(1), "$.commands")); |
| 95 | + assertEquals(List.of("PaymentSucceeded", "PaymentFailed"), get(whens.get(1), "$.events")); |
| 96 | + |
| 97 | + // Test third when block with if/else |
| 98 | + assertEquals(List.of("PaymentFailed"), get(whens.get(2), "$.triggers")); |
| 99 | + List<Map> ifs = get(whens.get(2), "$.ifs", List.of()); |
| 100 | + assertEquals(1, ifs.size()); |
| 101 | + assertEquals("less than 3 attempts", get(ifs.get(0), "$.condition")); |
| 102 | + assertEquals(List.of("retryPayment"), get(ifs.get(0), "$.commands")); |
| 103 | + assertEquals(List.of("PaymentRetryScheduled"), get(ifs.get(0), "$.events")); |
| 104 | + |
| 105 | + Map elseBlock = get(ifs.get(0), "$.else"); |
| 106 | + assertNotNull(elseBlock); |
| 107 | + assertEquals(List.of("Suspend after 3 failed attempts"), get(elseBlock, "$.policies")); |
| 108 | + assertEquals(List.of("suspendSubscription"), get(elseBlock, "$.commands")); |
| 109 | + assertEquals(List.of("SubscriptionSuspended"), get(elseBlock, "$.events")); |
| 110 | + |
| 111 | + // Test fourth when block with AND trigger |
| 112 | + assertEquals(List.of("PaymentSucceeded", "BillingCycleEnded"), get(whens.get(3), "$.triggers")); |
| 113 | + assertEquals(List.of("recordPayment"), get(whens.get(3), "$.commands")); |
| 114 | + assertEquals(List.of("PaymentRecorded"), get(whens.get(3), "$.events")); |
| 115 | + |
| 116 | + // Test fifth when block |
| 117 | + assertEquals(List.of("PaymentTimeout"), get(whens.get(4), "$.triggers")); |
| 118 | + assertEquals(List.of("cancelRenewal"), get(whens.get(4), "$.commands")); |
| 119 | + assertEquals(List.of("RenewalCancelled"), get(whens.get(4), "$.events")); |
| 120 | + |
| 121 | + // Test end block |
| 122 | + Map<String, List<String>> outcomes = get(model, "$.flows.PaymentsFlow.end.outcomes", Map.of()); |
| 123 | + assertEquals(3, outcomes.size()); |
| 124 | + assertEquals("PaymentRecorded", outcomes.get("completed").get(0)); |
| 125 | + assertEquals("SubscriptionSuspended", outcomes.get("suspended").get(0)); |
| 126 | + assertEquals("RenewalCancelled", outcomes.get("cancelled").get(0)); |
| 127 | + } |
| 128 | + |
| 129 | + private static ZflModel parseZfl(String fileName) throws IOException { |
| 130 | + CharStream zfl = CharStreams.fromFileName(fileName); |
| 131 | + return new ZflParser().parseModel(zfl.toString()); |
| 132 | + } |
| 133 | +} |
| 134 | + |
0 commit comments