amendments according to code review

This commit is contained in:
Michael Hoennig 2024-12-18 10:22:30 +01:00
parent 83ab12f3e0
commit 091454b6ab
2 changed files with 29 additions and 25 deletions

View File

@ -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<ReplaceD
obtain("Partner: %{partnerNumber}", () ->
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<ReplaceD
"holder": {
"personType": "UNINCORPORATED_FIRM",
"tradeName": "Erbengemeinschaft %{nameOfDeceasedPerson}",
"givenName": ${givenName???},
"familyName": ${familyName???}
},
"contact": {
"caption": "Erbengemeinschaft %{nameOfDeceasedPerson}",
@ -78,22 +75,9 @@ public class ReplaceDeceasedPartnerWithCommunityOfHeirs extends UseCase<ReplaceD
"contact.uuid": ${Contact: Erbengemeinschaft %{nameOfDeceasedPerson}}
}
"""))
.expecting(CREATED).expecting(JSON),
""
.reportWithResponse().expecting(CREATED).expecting(JSON)
).extractUuidAlias("holder.uuid", "Person: %{representativeGivenName} %{representativeFamilyName}");
obtain("Partner-Relation: Erbengemeinschaft %{nameOfDeceasedPerson}", () ->
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("""
{

View File

@ -129,22 +129,26 @@ public abstract class UseCase<T extends 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<HttpResponse> http, final String... extraInfo) {
public final HttpResponse obtain(final String alias, final Supplier<HttpResponse> 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<HttpResponse> code) {
public HttpResponse withTitle(final String resolvableTitle, final Supplier<HttpResponse> 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<T extends 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<T extends 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<T extends 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<T extends 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<T extends 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);