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

View File

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