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 d59bfc89..52a45ad4 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 @@ -46,7 +46,6 @@ import org.junit.jupiter.api.TestClassOrder; import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.DirtiesContext; @Tag("scenarioTest") @SpringBootTest( diff --git a/src/test/java/net/hostsharing/hsadminng/test/scenarios/ScenarioTest.java b/src/test/java/net/hostsharing/hsadminng/test/scenarios/ScenarioTest.java index 6944e245..e23fc584 100644 --- a/src/test/java/net/hostsharing/hsadminng/test/scenarios/ScenarioTest.java +++ b/src/test/java/net/hostsharing/hsadminng/test/scenarios/ScenarioTest.java @@ -17,7 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.web.server.LocalServerPort; import org.testcontainers.shaded.org.apache.commons.lang3.ObjectUtils; -import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.HashMap; @@ -26,6 +25,7 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.Stack; import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -34,14 +34,24 @@ import static java.util.Arrays.asList; import static java.util.Arrays.stream; import static java.util.Optional.ofNullable; import static net.hostsharing.hsadminng.test.scenarios.TemplateResolver.Resolver.DROP_COMMENTS; +import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.assertj.core.api.Assertions.assertThat; public abstract class ScenarioTest extends ContextBasedTest { final static String RUN_AS_USER = "superuser-alex@hostsharing.net"; // TODO.test: use global:AGENT when implemented + private final Stack currentTestMethodProduces = new Stack<>(); + protected ScenarioTest scenarioTest = this; + Optional takeProducedAlias() { + if (currentTestMethodProduces.isEmpty()) { + return Optional.empty(); + } + return Optional.of(currentTestMethodProduces.pop()); + } + record Alias>(Class useCase, UUID uuid) { @Override @@ -70,7 +80,10 @@ public abstract class ScenarioTest extends ContextBasedTest { void init(final TestInfo testInfo) { createHostsharingPerson(); try { - testInfo.getTestMethod().ifPresent(this::callRequiredProducers); + testInfo.getTestMethod().ifPresent(currentTestMethod -> { + callRequiredProducers(currentTestMethod); + keepProducesAlias(currentTestMethod); + }); testReport.createTestLogMarkdownFile(testInfo); } catch (Exception exc) { throw exc; @@ -124,12 +137,13 @@ public abstract class ScenarioTest extends ContextBasedTest { // then we recursively produce the pre-requisites of the producer method callRequiredProducers(potentialProducerMethod); + keepProducesAlias(currentTestMethod); // and finally we call the producer method invokeProducerMethod(this, potentialProducerMethod); assertThat(knowVariables().containsKey(testMethodRequires)) - .as("@Producer(\"" + testMethodRequires + "\") did not produce") + .as("@Producer method " + potentialProducerMethod.getName() + " did declare but not produce \"" + testMethodRequires + "\"") .isTrue(); } // @formatter:on @@ -141,6 +155,17 @@ public abstract class ScenarioTest extends ContextBasedTest { } } + private void keepProducesAlias(final Method currentTestMethod) { + final var producesAnnot = currentTestMethod.getAnnotation(Produces.class); + if ( producesAnnot != null ) { + final var producesAlias = isNotBlank(producesAnnot.value()) ? producesAnnot.value() : producesAnnot.explicitly(); + assertThat(producesAlias) + .as(currentTestMethod.getName() + " must define either value or explicit for @Produces") + .isNotNull(); + this.currentTestMethodProduces.push(producesAlias); + } + } + private Method @NotNull [] getPotentialProducerMethods() { final var methodsDeclaredInOuterTestClass = stream(getClass().getDeclaredMethods()) .filter(m -> m.getAnnotation(Produces.class) != null) diff --git a/src/test/java/net/hostsharing/hsadminng/test/scenarios/UseCase.java b/src/test/java/net/hostsharing/hsadminng/test/scenarios/UseCase.java index 8f1eaf1a..1209cfbc 100644 --- a/src/test/java/net/hostsharing/hsadminng/test/scenarios/UseCase.java +++ b/src/test/java/net/hostsharing/hsadminng/test/scenarios/UseCase.java @@ -96,6 +96,7 @@ public abstract class UseCase> { ); final var response = run(); verify(response); + keepInProduceAlias(response); resetProperties(); @@ -244,6 +245,13 @@ public abstract class UseCase> { } + private void keepInProduceAlias(final HttpResponse response) { + final var producedAlias = testSuite.takeProducedAlias(); + if (response != null) { + producedAlias.ifPresent(response::keepAs); + } + } + private static Duration seconds(final int secondsIfNoDebuggerAttached) { return isDebuggerAttached() ? Duration.ofHours(1) : Duration.ofSeconds(secondsIfNoDebuggerAttached); }