pretty JSON in test report

This commit is contained in:
Michael Hoennig 2024-12-07 07:27:01 +01:00
parent 886b621fce
commit bfa2c21013
2 changed files with 20 additions and 5 deletions

View File

@ -1,5 +1,7 @@
package net.hostsharing.hsadminng.hs.scenarios;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import net.hostsharing.hsadminng.system.SystemProcess;
import org.jetbrains.annotations.NotNull;
@ -23,9 +25,11 @@ import static org.assertj.core.api.Assertions.assertThat;
public class TestReport {
public static final File BUILD_DOC_SCENARIOS = new File("build/doc/scenarios");
private final static File markdownLogFile = new File(BUILD_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 static final File markdownLogFile = new File(BUILD_DOC_SCENARIOS, ".last-debug-log.md");
private static final ObjectMapper objectMapper = new ObjectMapper();
private final Map<String, ?> aliases;
private final PrintWriter markdownLog; // records everything for debugging purposes
private File markdownReportFile;
@ -76,6 +80,11 @@ public class TestReport {
printLine("\n" +output + "\n");
}
@SneakyThrows
public void printJson(final String json) {
printLine(prettyJson(json));
}
void silent(final Runnable code) {
silent++;
code.run();
@ -100,6 +109,14 @@ public class TestReport {
return convertedTestMethodName.replaceAll(": should ", ": ");
}
private static String prettyJson(final String json) throws JsonProcessingException {
if (json == null) {
return "";
}
final var jsonNode = objectMapper.readTree(json);
return "// pretty json\n" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
}
private String asClickableLink(final File file) {
return file.toURI().toString().replace("file:/", "file:///");
}

View File

@ -379,14 +379,12 @@ public abstract class UseCase<T extends UseCase<?>> {
// the request
testReport.printLine("```");
testReport.printLine(httpMethod.name() + " " + uri);
testReport.printLine((requestBody != null ? requestBody.trim() : ""));
testReport.printJson(requestBody);
// the response
testReport.printLine("=> status: " + status + " " + (locationUuid != null ? locationUuid : ""));
if (httpMethod == HttpMethod.GET || status.isError()) {
final var jsonNode = objectMapper.readTree(response.body());
final var prettyJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
testReport.printLine(prettyJson);
testReport.printJson(response.body());
}
testReport.printLine("```");
testReport.printLine("");