refactoring scenario-tests #126
@ -46,7 +46,6 @@ import org.junit.jupiter.api.TestClassOrder;
|
|||||||
import org.junit.jupiter.api.TestMethodOrder;
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
|
||||||
|
|
||||||
@Tag("scenarioTest")
|
@Tag("scenarioTest")
|
||||||
@SpringBootTest(
|
@SpringBootTest(
|
||||||
|
@ -17,7 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
import org.testcontainers.shaded.org.apache.commons.lang3.ObjectUtils;
|
import org.testcontainers.shaded.org.apache.commons.lang3.ObjectUtils;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -26,6 +25,7 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.Stack;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
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.Arrays.stream;
|
||||||
import static java.util.Optional.ofNullable;
|
import static java.util.Optional.ofNullable;
|
||||||
import static net.hostsharing.hsadminng.test.scenarios.TemplateResolver.Resolver.DROP_COMMENTS;
|
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;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public abstract class ScenarioTest extends ContextBasedTest {
|
public abstract class ScenarioTest extends ContextBasedTest {
|
||||||
|
|
||||||
final static String RUN_AS_USER = "superuser-alex@hostsharing.net"; // TODO.test: use global:AGENT when implemented
|
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;
|
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) {
|
record Alias<T extends UseCase<T>>(Class<T> useCase, UUID uuid) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -70,7 +80,10 @@ public abstract class ScenarioTest extends ContextBasedTest {
|
|||||||
void init(final TestInfo testInfo) {
|
void init(final TestInfo testInfo) {
|
||||||
createHostsharingPerson();
|
createHostsharingPerson();
|
||||||
try {
|
try {
|
||||||
testInfo.getTestMethod().ifPresent(this::callRequiredProducers);
|
testInfo.getTestMethod().ifPresent(currentTestMethod -> {
|
||||||
|
callRequiredProducers(currentTestMethod);
|
||||||
|
keepProducesAlias(currentTestMethod);
|
||||||
|
});
|
||||||
testReport.createTestLogMarkdownFile(testInfo);
|
testReport.createTestLogMarkdownFile(testInfo);
|
||||||
} catch (Exception exc) {
|
} catch (Exception exc) {
|
||||||
throw exc;
|
throw exc;
|
||||||
@ -124,12 +137,13 @@ public abstract class ScenarioTest extends ContextBasedTest {
|
|||||||
|
|
||||||
// then we recursively produce the pre-requisites of the producer method
|
// then we recursively produce the pre-requisites of the producer method
|
||||||
callRequiredProducers(potentialProducerMethod);
|
callRequiredProducers(potentialProducerMethod);
|
||||||
|
keepProducesAlias(currentTestMethod);
|
||||||
|
|
||||||
// and finally we call the producer method
|
// and finally we call the producer method
|
||||||
invokeProducerMethod(this, potentialProducerMethod);
|
invokeProducerMethod(this, potentialProducerMethod);
|
||||||
|
|
||||||
assertThat(knowVariables().containsKey(testMethodRequires))
|
assertThat(knowVariables().containsKey(testMethodRequires))
|
||||||
.as("@Producer(\"" + testMethodRequires + "\") did not produce")
|
.as("@Producer method " + potentialProducerMethod.getName() + " did declare but not produce \"" + testMethodRequires + "\"")
|
||||||
.isTrue();
|
.isTrue();
|
||||||
}
|
}
|
||||||
// @formatter:on
|
// @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() {
|
private Method @NotNull [] getPotentialProducerMethods() {
|
||||||
final var methodsDeclaredInOuterTestClass = stream(getClass().getDeclaredMethods())
|
final var methodsDeclaredInOuterTestClass = stream(getClass().getDeclaredMethods())
|
||||||
.filter(m -> m.getAnnotation(Produces.class) != null)
|
.filter(m -> m.getAnnotation(Produces.class) != null)
|
||||||
|
@ -96,6 +96,7 @@ public abstract class UseCase<T extends UseCase<?>> {
|
|||||||
);
|
);
|
||||||
final var response = run();
|
final var response = run();
|
||||||
verify(response);
|
verify(response);
|
||||||
|
keepInProduceAlias(response);
|
||||||
|
|
||||||
resetProperties();
|
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) {
|
private static Duration seconds(final int secondsIfNoDebuggerAttached) {
|
||||||
return isDebuggerAttached() ? Duration.ofHours(1) : Duration.ofSeconds(secondsIfNoDebuggerAttached);
|
return isDebuggerAttached() ? Duration.ofHours(1) : Duration.ofSeconds(secondsIfNoDebuggerAttached);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user