From 091454b6abaad788219cc75ce0f9cf960b7329b4 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 18 Dec 2024 10:22:30 +0100 Subject: [PATCH] amendments according to code review --- ...ceDeceasedPartnerWithCommunityOfHeirs.java | 20 ++--------- .../hsadminng/hs/scenarios/UseCase.java | 34 +++++++++++++++---- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/partner/ReplaceDeceasedPartnerWithCommunityOfHeirs.java b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/partner/ReplaceDeceasedPartnerWithCommunityOfHeirs.java index 8d386abe..8aa535ca 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/partner/ReplaceDeceasedPartnerWithCommunityOfHeirs.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/scenarios/partner/ReplaceDeceasedPartnerWithCommunityOfHeirs.java @@ -1,6 +1,5 @@ package net.hostsharing.hsadminng.hs.office.scenarios.partner; -import io.restassured.http.ContentType; import net.hostsharing.hsadminng.hs.scenarios.ScenarioTest; import net.hostsharing.hsadminng.hs.scenarios.UseCase; import org.springframework.http.HttpStatus; @@ -28,7 +27,7 @@ public class ReplaceDeceasedPartnerWithCommunityOfHeirs extends UseCase httpGet("/api/hs/office/partners/%{partnerNumber}") - .expecting(OK).expecting(JSON), + .reportWithResponse().expecting(OK).expecting(JSON), response -> response.getFromBody("uuid"), "Even in production data we expect this query to return just a single result." // TODO.impl: add constraint? ).extractValue("partnerRel.holder.uuid", "Person: %{nameOfDeceasedPerson}"); @@ -41,8 +40,6 @@ public class ReplaceDeceasedPartnerWithCommunityOfHeirs extends UseCase - httpPost("/api/hs/office/relations", usingJsonBody(""" - { - "type": "PARTNER", - "anchor.uuid": ${Person: Hostsharing eG}, - "holder.uuid": ${Person: Erbengemeinschaft %{nameOfDeceasedPerson}}, - "contact.uuid": ${Contact: Erbengemeinschaft %{nameOfDeceasedPerson}} - } - """)) - .expecting(HttpStatus.CREATED).expecting(ContentType.JSON) - ); - obtain("Partner: Erbengemeinschaft %{nameOfDeceasedPerson}", () -> httpPatch("/api/hs/office/partners/%{Partner: %{partnerNumber}}", usingJsonBody(""" { diff --git a/src/test/java/net/hostsharing/hsadminng/hs/scenarios/UseCase.java b/src/test/java/net/hostsharing/hsadminng/hs/scenarios/UseCase.java index 095a4ff5..01c5dede 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/scenarios/UseCase.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/scenarios/UseCase.java @@ -129,22 +129,26 @@ public abstract class UseCase> { final String... extraInfo) { return withTitle(title, () -> { final var response = http.get().keep(extractor); + response.optionallyReportRequestAndResponse(); Arrays.stream(extraInfo).forEach(testReport::printPara); return response; }); } - public final HttpResponse obtain(final String alias, final Supplier http, final String... extraInfo) { + public final HttpResponse obtain(final String alias, final Supplier httpCall, final String... extraInfo) { return withTitle(alias, () -> { - final var response = http.get().keep(); + final var response = httpCall.get().keep(); + response.optionallyReportRequestAndResponse(); Arrays.stream(extraInfo).forEach(testReport::printPara); return response; }); } - public HttpResponse withTitle(final String resolvableTitle, final Supplier code) { + public HttpResponse withTitle(final String resolvableTitle, final Supplier httpCall, final String... extraInfo) { this.nextTitle = resolvableTitle; - final var response = code.get(); + final var response = httpCall.get(); + response.optionallyReportRequestAndResponse(); + Arrays.stream(extraInfo).forEach(testReport::printPara); this.nextTitle = null; return response; } @@ -274,7 +278,8 @@ public abstract class UseCase> { @Getter private UUID locationUuid; - private boolean reported = false; + private boolean reportGenerated = false; + private boolean reportGeneratedWithResponse = false; @SneakyThrows public HttpResponse( @@ -385,7 +390,7 @@ public abstract class UseCase> { @SneakyThrows private HttpResponse reportRequestAndResponse(final boolean unconditionallyWithResponse) { - if (reported) { + if (reportGenerated) { throw new IllegalStateException("request report already generated"); } @@ -409,19 +414,30 @@ public abstract class UseCase> { testReport.printLine("=> status: " + status + " " + (locationUuid != null ? locationUuid : "")); if (unconditionallyWithResponse || httpMethod == HttpMethod.GET || status.isError()) { testReport.printJson(response.body()); + this.reportGeneratedWithResponse = true; } testReport.printLine("```"); testReport.printLine(""); + this.reportGenerated = true; return this; } @SneakyThrows private void optionallyReportRequestAndResponse() { - if (!reported) { + if (!reportGenerated) { reportRequestAndResponse(false); } } + private void verifyResponseReported(final String action) { + if (!reportGenerated) { + throw new IllegalStateException("report not generated yet, but expected for `" + action + "`"); + } + if (!reportGeneratedWithResponse) { + throw new IllegalStateException("report without response, but response report required for `" + action + "`"); + } + } + private String nonNullAlias(final String alias) { // This marker tag should not appear in the source-code, as here is nothing to fix. // But if it appears in generated Markdown files, it should show up when that marker tag is searched. @@ -430,6 +446,8 @@ public abstract class UseCase> { } public HttpResponse extractUuidAlias(final String jsonPath, final String resolvableName) { + verifyResponseReported("extractUuidAlias"); + final var resolvedName = ScenarioTest.resolve(resolvableName, DROP_COMMENTS); final var resolvedJsonPath = getFromBodyAsOptional(jsonPath).givenUUID(); ScenarioTest.putAlias(resolvedName, resolvedJsonPath); @@ -437,6 +455,8 @@ public abstract class UseCase> { } public HttpResponse extractValue(final String jsonPath, final String resolvableName) { + verifyResponseReported("extractValue"); + final var resolvedName = ScenarioTest.resolve(resolvableName, DROP_COMMENTS); final var resolvedJsonPath = getFromBodyAsOptional(jsonPath).givenAsString(); ScenarioTest.putProperty(resolvedName, resolvedJsonPath);