OfficeScenarioTests CoopShares+Assets #121

Merged
hsh-michaelhoennig merged 39 commits from feature/use-case-acceptance-tests-4 into master 2024-11-15 11:54:19 +01:00
8 changed files with 81 additions and 45 deletions
Showing only changes of commit 07b5e4686b - Show all commits

View File

@ -14,6 +14,7 @@ public class SystemProcess {
@Getter @Getter
private String stdOut; private String stdOut;
@Getter @Getter
private String stdErr; private String stdErr;
@ -21,7 +22,6 @@ public class SystemProcess {
this.processBuilder = new ProcessBuilder(command); this.processBuilder = new ProcessBuilder(command);
} }
public String getCommand() { public String getCommand() {
return processBuilder.command().toString(); return processBuilder.command().toString();
} }

View File

@ -4,6 +4,7 @@ import net.hostsharing.hsadminng.hs.office.scenarios.UseCase.HttpResponse;
import java.util.function.Consumer; import java.util.function.Consumer;
import static net.hostsharing.hsadminng.hs.office.scenarios.TemplateResolver.Resolver.DROP_COMMENTS;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
public class PathAssertion { public class PathAssertion {
@ -18,7 +19,7 @@ public class PathAssertion {
public Consumer<UseCase.HttpResponse> contains(final String resolvableValue) { public Consumer<UseCase.HttpResponse> contains(final String resolvableValue) {
return response -> { return response -> {
try { try {
response.path(path).map(Object::toString).contains(ScenarioTest.resolve(resolvableValue)); response.path(path).map(Object::toString).contains(ScenarioTest.resolve(resolvableValue, DROP_COMMENTS));
} catch (final AssertionError e) { } catch (final AssertionError e) {
// without this, the error message is often lacking important context // without this, the error message is often lacking important context
fail(e.getMessage() + " in `path(\"" + path + "\").contains(\"" + resolvableValue + "\")`" ); fail(e.getMessage() + " in `path(\"" + path + "\").contains(\"" + resolvableValue + "\")`" );

View File

@ -3,6 +3,7 @@ package net.hostsharing.hsadminng.hs.office.scenarios;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity; import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository; import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository;
import net.hostsharing.hsadminng.hs.office.scenarios.TemplateResolver.Resolver;
import net.hostsharing.hsadminng.lambda.Reducer; import net.hostsharing.hsadminng.lambda.Reducer;
import net.hostsharing.hsadminng.rbac.context.ContextBasedTest; import net.hostsharing.hsadminng.rbac.context.ContextBasedTest;
import net.hostsharing.hsadminng.rbac.test.JpaAttempt; import net.hostsharing.hsadminng.rbac.test.JpaAttempt;
@ -26,6 +27,8 @@ import java.util.stream.Collectors;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Optional.ofNullable; import static java.util.Optional.ofNullable;
import static net.hostsharing.hsadminng.hs.office.scenarios.TemplateResolver.Resolver.DROP_COMMENTS;
import static net.hostsharing.hsadminng.hs.office.scenarios.TemplateResolver.Resolver.DROP_COMMENTS;
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 {
@ -38,11 +41,11 @@ public abstract class ScenarioTest extends ContextBasedTest {
public String toString() { public String toString() {
return ObjectUtils.toString(uuid); return ObjectUtils.toString(uuid);
} }
} }
private final static Map<String, Alias<?>> aliases = new HashMap<>(); private final static Map<String, Alias<?>> aliases = new HashMap<>();
private final static Map<String, Object> properties = new HashMap<>();
private final static Map<String, Object> properties = new HashMap<>();
public final TestReport testReport = new TestReport(aliases); public final TestReport testReport = new TestReport(aliases);
@LocalServerPort @LocalServerPort
@ -139,9 +142,9 @@ public abstract class ScenarioTest extends ContextBasedTest {
} }
static UUID uuid(final String nameWithPlaceholders) { static UUID uuid(final String nameWithPlaceholders) {
final var resoledName = resolve(nameWithPlaceholders); final var resolvedName = resolve(nameWithPlaceholders, DROP_COMMENTS);
final UUID alias = ofNullable(knowVariables().get(resoledName)).filter(v -> v instanceof UUID).map(UUID.class::cast).orElse(null); final UUID alias = ofNullable(knowVariables().get(resolvedName)).filter(v -> v instanceof UUID).map(UUID.class::cast).orElse(null);
assertThat(alias).as("alias '" + resoledName + "' not found in aliases nor in properties [" + assertThat(alias).as("alias '" + resolvedName + "' not found in aliases nor in properties [" +
knowVariables().keySet().stream().map(v -> "'" + v + "'").collect(Collectors.joining(", ")) + "]" knowVariables().keySet().stream().map(v -> "'" + v + "'").collect(Collectors.joining(", ")) + "]"
).isNotNull(); ).isNotNull();
return alias; return alias;
@ -162,13 +165,13 @@ public abstract class ScenarioTest extends ContextBasedTest {
return map; return map;
} }
public static String resolve(final String text) { public static String resolve(final String text, final Resolver resolver) {
final var resolved = new TemplateResolver(text, ScenarioTest.knowVariables()).resolve(); final var resolved = new TemplateResolver(text, ScenarioTest.knowVariables()).resolve(resolver);
return resolved; return resolved;
} }
public static Object resolveTyped(final String text) { public static Object resolveTyped(final String text) {
final var resolved = resolve(text); final var resolved = resolve(text, DROP_COMMENTS);
try { try {
return UUID.fromString(resolved); return UUID.fromString(resolved);
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {

View File

@ -10,29 +10,39 @@ import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static net.hostsharing.hsadminng.hs.office.scenarios.TemplateResolver.Resolver.DROP_COMMENTS;
public class TemplateResolver { public class TemplateResolver {
private final static Pattern pattern = Pattern.compile(",(\\s*})", Pattern.MULTILINE); public enum Resolver {
private static final String IF_NOT_FOUND_SYMBOL = "???"; DROP_COMMENTS, // deletes comments ('#{whatever}' -> '')
KEEP_COMMENTS // deletes comments ('#{whatever}' -> '')
hsh-michaelhoennig marked this conversation as resolved Outdated

keep

keep
}
enum PlaceholderPrefix { enum PlaceholderPrefix {
RAW('%') { RAW('%') {
@Override @Override
String convert(final Object value) { String convert(final Object value, final Resolver resolver) {
return value != null ? value.toString() : ""; return value != null ? value.toString() : "";
} }
}, },
JSON_QUOTED('$'){ JSON_QUOTED('$'){
@Override @Override
String convert(final Object value) { String convert(final Object value, final Resolver resolver) {
return jsonQuoted(value); return jsonQuoted(value);
} }
}, },
URI_ENCODED('&'){ URI_ENCODED('&'){
@Override @Override
String convert(final Object value) { String convert(final Object value, final Resolver resolver) {
return value != null ? URLEncoder.encode(value.toString(), StandardCharsets.UTF_8) : ""; return value != null ? URLEncoder.encode(value.toString(), StandardCharsets.UTF_8) : "";
} }
},
COMMENT('#'){
@Override
String convert(final Object value, final Resolver resolver) {
return resolver == DROP_COMMENTS ? "" : value.toString();
}
}; };
private final char prefixChar; private final char prefixChar;
@ -49,12 +59,17 @@ public class TemplateResolver {
return Arrays.stream(values()).filter(p -> p.prefixChar == givenChar).findFirst().orElseThrow(); return Arrays.stream(values()).filter(p -> p.prefixChar == givenChar).findFirst().orElseThrow();
} }
abstract String convert(final Object value); abstract String convert(final Object value, final Resolver resolver); // FIXME: why Object and not String?
} }
private final static Pattern pattern = Pattern.compile(",(\\s*})", Pattern.MULTILINE);
hsh-michaelhoennig marked this conversation as resolved Outdated

nur }?

nur }?
private static final String IF_NOT_FOUND_SYMBOL = "???";
private final String template; private final String template;
private final Map<String, Object> properties; private final Map<String, Object> properties;
private final StringBuilder resolved = new StringBuilder(); private final StringBuilder resolved = new StringBuilder();
private Resolver resolver;
private int position = 0; private int position = 0;
public TemplateResolver(final String template, final Map<String, Object> properties) { public TemplateResolver(final String template, final Map<String, Object> properties) {
@ -62,7 +77,8 @@ public class TemplateResolver {
this.properties = properties; this.properties = properties;
} }
String resolve() { String resolve(final Resolver resolver) {
this.resolver = resolver;
final var resolved = copy(); final var resolved = copy();
final var withoutDroppedLines = dropLinesWithNullProperties(resolved); final var withoutDroppedLines = dropLinesWithNullProperties(resolved);
final var result = removeDanglingCommas(withoutDroppedLines); final var result = removeDanglingCommas(withoutDroppedLines);
@ -119,10 +135,10 @@ public class TemplateResolver {
placeholder.append(fetchChar()); placeholder.append(fetchChar());
} }
} }
final var name = new TemplateResolver(placeholder.toString(), properties).resolve(); final var content = new TemplateResolver(placeholder.toString(), properties).resolve(resolver);
final var value = propVal(name); final var value = intro != '#' ? propVal(content) : content;
resolved.append( resolved.append(
PlaceholderPrefix.ofPrefixChar(intro).convert(value) PlaceholderPrefix.ofPrefixChar(intro).convert(value, resolver)
); );
skipChar('}'); skipChar('}');
} }

View File

@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
import java.util.Map; import java.util.Map;
import static net.hostsharing.hsadminng.hs.office.scenarios.TemplateResolver.Resolver.DROP_COMMENTS;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
class TemplateResolverUnitTest { class TemplateResolverUnitTest {
@ -42,7 +43,7 @@ class TemplateResolverUnitTest {
Map.entry("simple placeholder", "einfach"), Map.entry("simple placeholder", "einfach"),
Map.entry("nested placeholder", "verschachtelt"), Map.entry("nested placeholder", "verschachtelt"),
Map.entry("with-special-chars", "3&3 AG") Map.entry("with-special-chars", "3&3 AG")
)).resolve(); )).resolve(DROP_COMMENTS);
assertThat(resolved).isEqualTo(""" assertThat(resolved).isEqualTo("""
with optional JSON quotes: with optional JSON quotes:

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.hs.office.scenarios; package net.hostsharing.hsadminng.hs.office.scenarios;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import net.hostsharing.hsadminng.system.SystemProcess;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.TestInfo;
@ -10,6 +11,8 @@ import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -17,6 +20,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class TestReport { public class TestReport {
private final static File markdownLogFile = new File("doc/scenarios/.last-debug-log.md"); private final static File markdownLogFile = new File("doc/scenarios/.last-debug-log.md");
public static final SimpleDateFormat MM_DD_YYYY_HH_MM_SS = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
private final Map<String, ?> aliases; private final Map<String, ?> aliases;
private final PrintWriter markdownLog; // records everything for debugging purposes private final PrintWriter markdownLog; // records everything for debugging purposes
@ -61,8 +65,16 @@ public class TestReport {
printLine("\n" +output + "\n"); printLine("\n" +output + "\n");
} }
void silent(final Runnable code) {
silent++;
code.run();
silent--;
}
public void close() { public void close() {
if (markdownReport != null) { if (markdownReport != null) {
printPara("---");
printPara("generated on " + MM_DD_YYYY_HH_MM_SS.format(new Date()) + " for branch " + currentGitBranch());
markdownReport.close(); markdownReport.close();
System.out.println("SCENARIO REPORT: " + asClickableLink(markdownReportFile)); System.out.println("SCENARIO REPORT: " + asClickableLink(markdownReportFile));
} }
@ -102,9 +114,10 @@ public class TestReport {
return result.toString(); return result.toString();
} }
void silent(final Runnable code) { @SneakyThrows
silent++; private String currentGitBranch() {
code.run(); final var gitRevParse = new SystemProcess("git", "rev-parse", "--abbrev-ref", "HEAD");
silent--; gitRevParse.execute();
return gitRevParse.getStdOut().split("\\R", 2)[0];
} }
} }

View File

@ -34,6 +34,8 @@ import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import static java.net.URLEncoder.encode; import static java.net.URLEncoder.encode;
import static net.hostsharing.hsadminng.hs.office.scenarios.TemplateResolver.Resolver.DROP_COMMENTS;
import static net.hostsharing.hsadminng.hs.office.scenarios.TemplateResolver.Resolver.KEEP_COMMENTS;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.platform.commons.util.StringUtils.isBlank; import static org.junit.platform.commons.util.StringUtils.isBlank;
@ -116,11 +118,11 @@ public abstract class UseCase<T extends UseCase<?>> {
} }
public final void obtain( public final void obtain(
final String alias, final String title,
final Supplier<HttpResponse> http, final Supplier<HttpResponse> http,
final Function<HttpResponse, String> extractor, final Function<HttpResponse, String> extractor,
final String... extraInfo) { final String... extraInfo) {
withTitle(ScenarioTest.resolve(alias), () -> { withTitle(title, () -> {
final var response = http.get().keep(extractor); final var response = http.get().keep(extractor);
Arrays.stream(extraInfo).forEach(testReport::printPara); Arrays.stream(extraInfo).forEach(testReport::printPara);
return response; return response;
@ -128,15 +130,15 @@ public abstract class UseCase<T extends UseCase<?>> {
} }
public final void obtain(final String alias, final Supplier<HttpResponse> http, final String... extraInfo) { public final void obtain(final String alias, final Supplier<HttpResponse> http, final String... extraInfo) {
withTitle(ScenarioTest.resolve(alias), () -> { withTitle(alias, () -> {
final var response = http.get().keep(); final var response = http.get().keep();
Arrays.stream(extraInfo).forEach(testReport::printPara); Arrays.stream(extraInfo).forEach(testReport::printPara);
return response; return response;
}); });
} }
public HttpResponse withTitle(final String title, final Supplier<HttpResponse> code) { public HttpResponse withTitle(final String resolvableTitle, final Supplier<HttpResponse> code) {
this.nextTitle = ScenarioTest.resolve(title); this.nextTitle = resolvableTitle;
final var response = code.get(); final var response = code.get();
this.nextTitle = null; this.nextTitle = null;
return response; return response;
@ -144,7 +146,7 @@ public abstract class UseCase<T extends UseCase<?>> {
@SneakyThrows @SneakyThrows
public final HttpResponse httpGet(final String uriPathWithPlaceholders) { public final HttpResponse httpGet(final String uriPathWithPlaceholders) {
final var uriPath = ScenarioTest.resolve(uriPathWithPlaceholders); final var uriPath = ScenarioTest.resolve(uriPathWithPlaceholders, DROP_COMMENTS);
final var request = HttpRequest.newBuilder() final var request = HttpRequest.newBuilder()
.GET() .GET()
.uri(new URI("http://localhost:" + testSuite.port + uriPath)) .uri(new URI("http://localhost:" + testSuite.port + uriPath))
@ -157,7 +159,7 @@ public abstract class UseCase<T extends UseCase<?>> {
@SneakyThrows @SneakyThrows
public final HttpResponse httpPost(final String uriPathWithPlaceholders, final JsonTemplate bodyJsonTemplate) { public final HttpResponse httpPost(final String uriPathWithPlaceholders, final JsonTemplate bodyJsonTemplate) {
final var uriPath = ScenarioTest.resolve(uriPathWithPlaceholders); final var uriPath = ScenarioTest.resolve(uriPathWithPlaceholders, DROP_COMMENTS);
final var requestBody = bodyJsonTemplate.resolvePlaceholders(); final var requestBody = bodyJsonTemplate.resolvePlaceholders();
final var request = HttpRequest.newBuilder() final var request = HttpRequest.newBuilder()
.POST(BodyPublishers.ofString(requestBody)) .POST(BodyPublishers.ofString(requestBody))
@ -172,7 +174,7 @@ public abstract class UseCase<T extends UseCase<?>> {
@SneakyThrows @SneakyThrows
public final HttpResponse httpPatch(final String uriPathWithPlaceholders, final JsonTemplate bodyJsonTemplate) { public final HttpResponse httpPatch(final String uriPathWithPlaceholders, final JsonTemplate bodyJsonTemplate) {
final var uriPath = ScenarioTest.resolve(uriPathWithPlaceholders); final var uriPath = ScenarioTest.resolve(uriPathWithPlaceholders, DROP_COMMENTS);
final var requestBody = bodyJsonTemplate.resolvePlaceholders(); final var requestBody = bodyJsonTemplate.resolvePlaceholders();
final var request = HttpRequest.newBuilder() final var request = HttpRequest.newBuilder()
.method(HttpMethod.PATCH.toString(), BodyPublishers.ofString(requestBody)) .method(HttpMethod.PATCH.toString(), BodyPublishers.ofString(requestBody))
@ -187,7 +189,7 @@ public abstract class UseCase<T extends UseCase<?>> {
@SneakyThrows @SneakyThrows
public final HttpResponse httpDelete(final String uriPathWithPlaceholders) { public final HttpResponse httpDelete(final String uriPathWithPlaceholders) {
final var uriPath = ScenarioTest.resolve(uriPathWithPlaceholders); final var uriPath = ScenarioTest.resolve(uriPathWithPlaceholders, DROP_COMMENTS);
final var request = HttpRequest.newBuilder() final var request = HttpRequest.newBuilder()
.DELETE() .DELETE()
.uri(new URI("http://localhost:" + testSuite.port + uriPath)) .uri(new URI("http://localhost:" + testSuite.port + uriPath))
@ -207,7 +209,7 @@ public abstract class UseCase<T extends UseCase<?>> {
final String title, final String title,
final Supplier<UseCase.HttpResponse> http, final Supplier<UseCase.HttpResponse> http,
final Consumer<UseCase.HttpResponse>... assertions) { final Consumer<UseCase.HttpResponse>... assertions) {
withTitle(ScenarioTest.resolve(title), () -> { withTitle(title, () -> {
final var response = http.get(); final var response = http.get();
Arrays.stream(assertions).forEach(assertion -> assertion.accept(response)); Arrays.stream(assertions).forEach(assertion -> assertion.accept(response));
return response; return response;
@ -219,7 +221,7 @@ public abstract class UseCase<T extends UseCase<?>> {
} }
public String uriEncoded(final String text) { public String uriEncoded(final String text) {
return encode(ScenarioTest.resolve(text), StandardCharsets.UTF_8); return encode(ScenarioTest.resolve(text, DROP_COMMENTS), StandardCharsets.UTF_8);
} }
public static class JsonTemplate { public static class JsonTemplate {
@ -231,7 +233,7 @@ public abstract class UseCase<T extends UseCase<?>> {
} }
String resolvePlaceholders() { String resolvePlaceholders() {
return ScenarioTest.resolve(template); return ScenarioTest.resolve(template, DROP_COMMENTS);
} }
} }
@ -276,7 +278,7 @@ public abstract class UseCase<T extends UseCase<?>> {
} }
public HttpResponse keep(final Function<HttpResponse, String> extractor) { public HttpResponse keep(final Function<HttpResponse, String> extractor) {
final var alias = nextTitle != null ? nextTitle : resultAlias; final var alias = nextTitle != null ? ScenarioTest.resolve(nextTitle, DROP_COMMENTS) : resultAlias;
assertThat(alias).as("cannot keep result, no alias found").isNotNull(); assertThat(alias).as("cannot keep result, no alias found").isNotNull();
final var value = extractor.apply(this); final var value = extractor.apply(this);
@ -294,7 +296,7 @@ public abstract class UseCase<T extends UseCase<?>> {
} }
public HttpResponse keep() { public HttpResponse keep() {
final var alias = nextTitle != null ? nextTitle : resultAlias; final var alias = nextTitle != null ? ScenarioTest.resolve(nextTitle, DROP_COMMENTS) : resultAlias;
assertThat(alias).as("cannot keep result, no title or alias found for locationUuid: " + locationUuid).isNotNull(); assertThat(alias).as("cannot keep result, no title or alias found for locationUuid: " + locationUuid).isNotNull();
return keepAs(alias); return keepAs(alias);
@ -313,13 +315,13 @@ public abstract class UseCase<T extends UseCase<?>> {
@SneakyThrows @SneakyThrows
public String getFromBody(final String path) { public String getFromBody(final String path) {
return JsonPath.parse(response.body()).read(ScenarioTest.resolve(path)); return JsonPath.parse(response.body()).read(ScenarioTest.resolve(path, DROP_COMMENTS));
} }
@SneakyThrows @SneakyThrows
public <T> Optional<T> getFromBodyAsOptional(final String path) { public <T> Optional<T> getFromBodyAsOptional(final String path) {
try { try {
return Optional.ofNullable(JsonPath.parse(response.body()).read(ScenarioTest.resolve(path))); return Optional.ofNullable(JsonPath.parse(response.body()).read(ScenarioTest.resolve(path, DROP_COMMENTS)));
} catch (final PathNotFoundException e) { } catch (final PathNotFoundException e) {
return null; // means the property did not exist at all, not that it was there with value null return null; // means the property did not exist at all, not that it was there with value null
} }
@ -335,9 +337,9 @@ public abstract class UseCase<T extends UseCase<?>> {
// the title // the title
if (nextTitle != null) { if (nextTitle != null) {
testReport.printLine("\n### " + nextTitle + "\n"); testReport.printLine("\n### " + ScenarioTest.resolve(nextTitle, KEEP_COMMENTS) + "\n");
} else if (resultAlias != null) { } else if (resultAlias != null) {
testReport.printLine("\n### " + resultAlias + "\n"); testReport.printLine("\n### Create " + resultAlias + "\n");
} else { } else {
fail("please wrap the http...-call in the UseCase using `withTitle(...)`"); fail("please wrap the http...-call in the UseCase using `withTitle(...)`");
} }

View File

@ -17,7 +17,7 @@ public abstract class CreateCoopSharesTransaction extends UseCase<CreateCoopShar
@Override @Override
protected HttpResponse run() { protected HttpResponse run() {
obtain("membershipUuid", () -> obtain("#{Find }membershipUuid", () ->
httpGet("/api/hs/office/memberships?memberNumber=&{memberNumber}") httpGet("/api/hs/office/memberships?memberNumber=&{memberNumber}")
.expecting(OK).expecting(JSON).expectArrayElements(1), .expecting(OK).expecting(JSON).expectArrayElements(1),
response -> response.getFromBody("$[0].uuid") response -> response.getFromBody("$[0].uuid")