also keep aliases from @Produces annotations

This commit is contained in:
Michael Hoennig 2024-11-29 09:44:14 +01:00
parent b45639878e
commit ad9ae3d766
3 changed files with 36 additions and 4 deletions

View File

@ -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(

View File

@ -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<String> currentTestMethodProduces = new Stack<>();
protected ScenarioTest scenarioTest = this;
Optional<String> takeProducedAlias() {
if (currentTestMethodProduces.isEmpty()) {
return Optional.empty();
}
return Optional.of(currentTestMethodProduces.pop());
}
record Alias<T extends UseCase<T>>(Class<T> 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)

View File

@ -96,6 +96,7 @@ public abstract class UseCase<T extends UseCase<?>> {
);
final var response = run();
verify(response);
keepInProduceAlias(response);
resetProperties();
@ -244,6 +245,13 @@ public abstract class UseCase<T extends 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);
}