From 82900d7a808241259455bcdc6956f6fec27d80b0 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Fri, 1 Nov 2024 14:23:47 +0100 Subject: [PATCH] introduce nullable properties (by ?) and drop-line (by ??) --- .../scenarios/HsOfficeScenarioTests.java | 34 +++++++++++++++++- .../hs/office/scenarios/TemplateResolver.java | 35 +++++++++++++++++-- .../scenarios/UseCaseNotImplementedYet.java | 16 +++++++++ .../scenarios/contact/AmendContactData.java | 19 ++++++++++ .../scenarios/partner/CreatePartner.java | 12 ++++--- 5 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/UseCaseNotImplementedYet.java create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/contact/AmendContactData.java diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/HsOfficeScenarioTests.java b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/HsOfficeScenarioTests.java index 4178b65a..0e55ba52 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/HsOfficeScenarioTests.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/HsOfficeScenarioTests.java @@ -1,6 +1,7 @@ package net.hostsharing.hsadminng.hs.office.scenarios; import net.hostsharing.hsadminng.HsadminNgApplication; +import net.hostsharing.hsadminng.hs.office.scenarios.contact.AmendContactData; import net.hostsharing.hsadminng.hs.office.scenarios.debitor.CreateExternalDebitorForPartner; import net.hostsharing.hsadminng.hs.office.scenarios.debitor.CreateSelfDebitorForPartner; import net.hostsharing.hsadminng.hs.office.scenarios.debitor.CreateSepaMandateForDebitor; @@ -44,7 +45,7 @@ class HsOfficeScenarioTests extends ScenarioTest { @Test @Order(1010) @Produces(explicitly = "Partner: Test AG", implicitly = {"Person: Test AG", "Contact: Test AG - Board of Directors"}) - void shouldCreatePartner() { + void shouldCreateLegalPersonAsPartner() { new CreatePartner(this) .given("partnerNumber", 31010) .given("personType", "LEGAL_PERSON") @@ -55,6 +56,21 @@ class HsOfficeScenarioTests extends ScenarioTest { .keep(); } + @Test + @Order(1011) + @Produces(explicitly = "Partner: Michelle Matthieu", implicitly = {"Person: Michelle Matthieu", "Contact: Michelle Matthieu"}) + void shouldCreateNaturalPersonAsPartner() { + new CreatePartner(this) + .given("partnerNumber", 31011) + .given("personType", "NATURAL_PERSON") + .given("givenName", "Michelle") + .given("familyName", "Matthieu") + .given("contactCaption", "Michelle Matthieu") + .given("emailAddress", "michelle.matthieu@example.org") + .doRun() + .keep(); + } + @Test @Order(1020) @Requires("Person: Test AG") @@ -106,6 +122,22 @@ class HsOfficeScenarioTests extends ScenarioTest { .doRun(); } + @Test + @Order(1100) + void shouldAmendContactData() { + new AmendContactData(this) + .given("partnerNumber", 31020) + .doRun(); + } + + @Test + @Order(1101) + void shouldReplaceContactData() { + new UseCaseNotImplementedYet(this) + .given("partnerNumber", 31020) + .doRun(); + } + @Test @Order(2010) @Requires("Partner: Test AG") diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/TemplateResolver.java b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/TemplateResolver.java index 41507f36..13e01324 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/TemplateResolver.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/TemplateResolver.java @@ -4,8 +4,11 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Map; +import java.util.regex.Pattern; +import java.util.stream.Collectors; public class TemplateResolver { + private final static Pattern pattern = Pattern.compile(",\\s*([}\\]])", Pattern.MULTILINE); enum PlaceholderPrefix { RAW('%') { @@ -55,11 +58,23 @@ public class TemplateResolver { } String resolve() { - copy(); - return resolved.toString(); + final var resolved = copy(); + final var withoutDroppedLines = removeDroppedLinkes(resolved); + final var result = removeSpareCommas(withoutDroppedLines); + return result; } - private void copy() { + private static String removeSpareCommas(final String withoutDroppedLines) { + return pattern.matcher(withoutDroppedLines).replaceAll("$1"); + } + + private String removeDroppedLinkes(final String text) { + return Arrays.stream(text.split("\n")) + .filter(line -> !line.contains("<<>")) + .collect(Collectors.joining("\n")); + } + + private String copy() { while (hasMoreChars()) { if (PlaceholderPrefix.contains(currentChar()) && nextChar() == '{') { startPlaceholder(currentChar()); @@ -67,6 +82,7 @@ public class TemplateResolver { resolved.append(fetchChar()); } } + return resolved.toString(); } private boolean hasMoreChars() { @@ -97,6 +113,18 @@ public class TemplateResolver { } private Object propVal(final String name) { + if (name.endsWith("??")) { + final String pureName = name.substring(0, name.length() - "??".length()); + final var val = properties.get(pureName); + if (val == null) { + return "<<>"; + } + return val; + } + if (name.endsWith("?")) { + final String pureName = name.substring(0, name.length() - "?".length()); + return properties.get(pureName); + } final var val = properties.get(name); if (val == null) { throw new IllegalStateException("Missing required property: " + name); @@ -144,6 +172,7 @@ public class TemplateResolver { private static String jsonQuoted(final Object value) { return switch (value) { + case null -> null; case Boolean bool -> bool.toString(); case Number number -> number.toString(); case String string -> "\"" + string.replace("\n", "\\n") + "\""; diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/UseCaseNotImplementedYet.java b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/UseCaseNotImplementedYet.java new file mode 100644 index 00000000..d21d6413 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/UseCaseNotImplementedYet.java @@ -0,0 +1,16 @@ +package net.hostsharing.hsadminng.hs.office.scenarios; + +import static org.assertj.core.api.Assumptions.assumeThat; + +public class UseCaseNotImplementedYet extends UseCase { + + public UseCaseNotImplementedYet(final ScenarioTest testSuite) { + super(testSuite); + } + + @Override + protected HttpResponse run() { + assumeThat(false).isTrue(); // makes the test gray + return null; + } +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/contact/AmendContactData.java b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/contact/AmendContactData.java new file mode 100644 index 00000000..360f0756 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/contact/AmendContactData.java @@ -0,0 +1,19 @@ +package net.hostsharing.hsadminng.hs.office.scenarios.contact; + +import net.hostsharing.hsadminng.hs.office.scenarios.ScenarioTest; +import net.hostsharing.hsadminng.hs.office.scenarios.UseCase; + +import static org.assertj.core.api.Assumptions.assumeThat; + +public class AmendContactData extends UseCase { + + public AmendContactData(final ScenarioTest testSuite) { + super(testSuite); + } + + @Override + protected HttpResponse run() { + assumeThat(false).isTrue(); // makes the test gray + return null; + } +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/partner/CreatePartner.java b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/partner/CreatePartner.java index c96acbdf..0aa485ad 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/partner/CreatePartner.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/partner/CreatePartner.java @@ -28,17 +28,19 @@ public class CreatePartner extends UseCase { "Even in production data we expect this query to return just a single result." // TODO.impl: add constraint? ); - obtain("Person: %{tradeName}", () -> + obtain("Partner-Person", () -> httpPost("/api/hs/office/persons", usingJsonBody(""" { "personType": ${personType}, - "tradeName": ${tradeName} + "tradeName": ${tradeName??}, + "givenName": ${givenName??}, + "familyName": ${familyName??} } """)) .expecting(HttpStatus.CREATED).expecting(ContentType.JSON) ); - obtain("Contact: %{tradeName} - Board of Directors", () -> + obtain("Contact: %{contactCaption}", () -> httpPost("/api/hs/office/contacts", usingJsonBody(""" { "caption": ${contactCaption}, @@ -55,8 +57,8 @@ public class CreatePartner extends UseCase { "partnerNumber": ${partnerNumber}, "partnerRel": { "anchorUuid": ${Person: Hostsharing eG}, - "holderUuid": ${Person: %{tradeName}}, - "contactUuid": ${Contact: %{tradeName} - Board of Directors} + "holderUuid": ${Partner-Person}, + "contactUuid": ${Contact: %{contactCaption}} }, "details": { "registrationOffice": "Registergericht Hamburg",