encapsulate aliases+properties

This commit is contained in:
Michael Hoennig 2024-10-18 10:48:33 +02:00
parent 4d29c4643b
commit 887ec588b6
2 changed files with 24 additions and 16 deletions

View File

@ -1,17 +1,13 @@
package net.hostsharing.hsadminng.hs.office.usecases; package net.hostsharing.hsadminng.hs.office.usecases;
import com.tngtech.archunit.thirdparty.com.google.common.collect.Streams;
import io.restassured.RestAssured; import io.restassured.RestAssured;
import io.restassured.http.ContentType; import io.restassured.http.ContentType;
import io.restassured.response.Response; import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse; import io.restassured.response.ValidatableResponse;
import lombok.SneakyThrows;
import org.apache.commons.collections4.map.LinkedMap; import org.apache.commons.collections4.map.LinkedMap;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -20,11 +16,7 @@ import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
@ -50,16 +42,16 @@ public abstract class UseCase<T extends UseCase<?>> {
} }
public final void requires(final String alias, final Function<String, UseCase<?>> useCaseFactory) { public final void requires(final String alias, final Function<String, UseCase<?>> useCaseFactory) {
if ( !UseCaseTest.aliases.containsKey(alias) ) { if ( !UseCaseTest.containsAlias(alias) ) {
requirements.put(alias, useCaseFactory); requirements.put(alias, useCaseFactory);
} }
} }
public void requires(final String alias) { public void requires(final String alias) {
assumeThat(UseCaseTest.aliases.containsKey(alias)) assumeThat(UseCaseTest.containsAlias(alias))
.as("skipping because alias '" + alias + "' not found, maybe the other test failed?") .as("skipping because alias '" + alias + "' not found, maybe the other test failed?")
.isTrue(); .isTrue();
log("depends on ["+alias+"]("+UseCaseTest.aliases.get(alias).useCase().getSimpleName()+".md)"); log("depends on ["+alias+"]("+UseCaseTest.getAlias(alias).useCase().getSimpleName()+".md)");
} }
public final HttpResponse doRun() { public final HttpResponse doRun() {
@ -70,7 +62,7 @@ public abstract class UseCase<T extends UseCase<?>> {
protected abstract HttpResponse run(); protected abstract HttpResponse run();
public final UseCase<T> given(final String propName, final Object propValue) { public final UseCase<T> given(final String propName, final Object propValue) {
UseCaseTest.properties.put(propName, propValue); UseCaseTest.putProperty(propName, propValue);
return this; return this;
} }
@ -105,7 +97,7 @@ public abstract class UseCase<T extends UseCase<?>> {
} }
public final UUID uuid(final String alias) { public final UUID uuid(final String alias) {
return UseCaseTest.aliases.get(alias).uuid(); return UseCaseTest.getAlias(alias).uuid();
} }
static class JsonTemplate { static class JsonTemplate {
@ -196,7 +188,7 @@ public abstract class UseCase<T extends UseCase<?>> {
} }
public void keep() { public void keep() {
UseCaseTest.aliases.put( UseCaseTest.putAlias(
nextTitle != null ? nextTitle : resultAlias, nextTitle != null ? nextTitle : resultAlias,
new UseCaseTest.Alias<>(UseCase.this.getClass(), locationUuid)); new UseCaseTest.Alias<>(UseCase.this.getClass(), locationUuid));
} }

View File

@ -34,8 +34,8 @@ public abstract class UseCaseTest extends ContextBasedTest {
record Alias<T extends UseCase<T>>(Class<T> useCase, UUID uuid) {} record Alias<T extends UseCase<T>>(Class<T> useCase, UUID uuid) {}
final static Map<String, Alias<?>> aliases = new HashMap<>(); private final static Map<String, Alias<?>> aliases = new HashMap<>();
final static Map<String, Object> properties = new HashMap<>(); private final static Map<String, Object> properties = new HashMap<>();
@LocalServerPort @LocalServerPort
Integer port; Integer port;
@ -81,6 +81,22 @@ public abstract class UseCaseTest extends ContextBasedTest {
markdownFile.println(output); markdownFile.println(output);
} }
static boolean containsAlias(final String alias) {
return aliases.containsKey(alias);
}
static Alias<?> getAlias(final String alias) {
return aliases.get(alias);
}
static void putAlias(final String name, final Alias<?> value) {
aliases.put(name, value);
}
static void putProperty(final String name, final Object value) {
properties.put(name, value);
}
static Stream<Map.Entry<String,?>> knowVariables() { static Stream<Map.Entry<String,?>> knowVariables() {
return Streams.concat( return Streams.concat(
UseCaseTest.aliases.entrySet().stream().map(e -> Map.entry(e.getKey(), e.getValue().uuid())), UseCaseTest.aliases.entrySet().stream().map(e -> Map.entry(e.getKey(), e.getValue().uuid())),