Compare commits

..

No commits in common. "07b5e4686bc954e1d4cb9050f6ac93c939475add" and "c3f3efaf18fd82d21a6d450e8b565eb63d09c485" have entirely different histories.

12 changed files with 49 additions and 88 deletions

View File

@ -95,6 +95,3 @@ if [ ! -f .environment ]; then
cp .tc-environment .environment
fi
source .environment
alias scenario-reports-upload='./gradlew convertMarkdownToHtml && ssh hsh03-hsngdev@h50.hostsharing.net "rm doms/hsngdev.hs-example.de/htdocs-ssl/scenarios/office/*.html" && scp doc/scenarios/*.html hsh03-hsngdev@h50.hostsharing.net:doms/hsngdev.hs-example.de/htdocs-ssl/scenarios/office'
alias scenario-reports-online='open https://hsngdev.hs-example.de/scenarios/office'

View File

@ -410,7 +410,7 @@ tasks.register('convertMarkdownToHtml') {
group = 'Conversion'
// Define the template file and input directory
def templateFile = file('doc/scenarios/.template.html')
def templateFile = file('doc/scenarios/template.html')
// Task configuration and execution
doFirst {
@ -425,7 +425,7 @@ tasks.register('convertMarkdownToHtml') {
// Check if the template file exists
if (!templateFile.exists()) {
throw new GradleException("Template file 'doc/scenarios/.template.html' not found.")
throw new GradleException("Template file 'doc/scenarios/template.html' not found.")
}
}

View File

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

View File

@ -343,7 +343,7 @@ class HsOfficeScenarioTests extends ScenarioTest {
}
@Test
@Order(4202)
@Order(4201)
@Requires("Membership: M-3101000 - Test AG")
void shouldRevertCoopSharesSubscription() {
new CreateCoopSharesRevertTransaction(this)

View File

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

View File

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

View File

@ -10,39 +10,29 @@ import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static net.hostsharing.hsadminng.hs.office.scenarios.TemplateResolver.Resolver.DROP_COMMENTS;
public class TemplateResolver {
public enum Resolver {
DROP_COMMENTS, // deletes comments ('#{whatever}' -> '')
KEEP_COMMENTS // deletes comments ('#{whatever}' -> '')
}
private final static Pattern pattern = Pattern.compile(",(\\s*})", Pattern.MULTILINE);
private static final String IF_NOT_FOUND_SYMBOL = "???";
enum PlaceholderPrefix {
RAW('%') {
@Override
String convert(final Object value, final Resolver resolver) {
String convert(final Object value) {
return value != null ? value.toString() : "";
}
},
JSON_QUOTED('$'){
@Override
String convert(final Object value, final Resolver resolver) {
String convert(final Object value) {
return jsonQuoted(value);
}
},
URI_ENCODED('&'){
@Override
String convert(final Object value, final Resolver resolver) {
String convert(final Object value) {
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;
@ -59,17 +49,12 @@ public class TemplateResolver {
return Arrays.stream(values()).filter(p -> p.prefixChar == givenChar).findFirst().orElseThrow();
}
abstract String convert(final Object value, final Resolver resolver); // FIXME: why Object and not String?
abstract String convert(final Object value);
}
private final static Pattern pattern = Pattern.compile(",(\\s*})", Pattern.MULTILINE);
private static final String IF_NOT_FOUND_SYMBOL = "???";
private final String template;
private final Map<String, Object> properties;
private final StringBuilder resolved = new StringBuilder();
private Resolver resolver;
private int position = 0;
public TemplateResolver(final String template, final Map<String, Object> properties) {
@ -77,8 +62,7 @@ public class TemplateResolver {
this.properties = properties;
}
String resolve(final Resolver resolver) {
this.resolver = resolver;
String resolve() {
final var resolved = copy();
final var withoutDroppedLines = dropLinesWithNullProperties(resolved);
final var result = removeDanglingCommas(withoutDroppedLines);
@ -135,10 +119,10 @@ public class TemplateResolver {
placeholder.append(fetchChar());
}
}
final var content = new TemplateResolver(placeholder.toString(), properties).resolve(resolver);
final var value = intro != '#' ? propVal(content) : content;
final var name = new TemplateResolver(placeholder.toString(), properties).resolve();
final var value = propVal(name);
resolved.append(
PlaceholderPrefix.ofPrefixChar(intro).convert(value, resolver)
PlaceholderPrefix.ofPrefixChar(intro).convert(value)
);
skipChar('}');
}

View File

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

View File

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

View File

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