diff options
author | Jon Marius Venstad <venstad@gmail.com> | 2021-11-17 13:35:01 +0100 |
---|---|---|
committer | Jon Marius Venstad <venstad@gmail.com> | 2021-11-17 13:35:01 +0100 |
commit | b46f7e06091420fb73ad127e2ebd153825c758a3 (patch) | |
tree | b18bc4f1ff40ae303c5d9380c46aa11040a4215e /vespa-osgi-testrunner | |
parent | 97fd93ea00be2bfba7028c7d9b96ee8c91b1c3fa (diff) |
Move serialization to handler
Diffstat (limited to 'vespa-osgi-testrunner')
5 files changed, 54 insertions, 55 deletions
diff --git a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java index 6d296615c9c..5cc4d8336f2 100644 --- a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java +++ b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/JunitRunner.java @@ -242,10 +242,4 @@ public class JunitRunner extends AbstractComponent implements TestRunner { } } - @Override - public String getReportAsJson() { - return Optional.ofNullable(getReport()) - .map(TestReport::toJson) - .orElse(""); - } } diff --git a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestReport.java b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestReport.java index 07b4b7b72e8..b3ca47e2480 100644 --- a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestReport.java +++ b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestReport.java @@ -1,13 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.testrunner; -import com.yahoo.exception.ExceptionUtils; -import com.yahoo.slime.Cursor; -import com.yahoo.slime.Slime; -import com.yahoo.slime.SlimeUtils; -import com.yahoo.yolean.Exceptions; - -import java.nio.charset.StandardCharsets; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.logging.LogRecord; @@ -16,15 +10,16 @@ import java.util.logging.LogRecord; * @author mortent */ public class TestReport { - private final long totalCount; - private final long successCount; - private final long failedCount; - private final long ignoredCount; - private final long abortedCount; - private final List<Failure> failures; - private final List<LogRecord> logLines; - - public TestReport(long totalCount, long successCount, long failedCount, long ignoredCount, long abortedCount, List<Failure> failures, List<LogRecord> logLines) { + + final long totalCount; + final long successCount; + final long failedCount; + final long ignoredCount; + final long abortedCount; + final List<Failure> failures; + final List<LogRecord> logLines; + + private TestReport(long totalCount, long successCount, long failedCount, long ignoredCount, long abortedCount, List<Failure> failures, List<LogRecord> logLines) { this.totalCount = totalCount; this.successCount = successCount; this.failedCount = failedCount; @@ -34,31 +29,6 @@ public class TestReport { this.logLines = logLines; } - private void serializeFailure(Failure failure, Cursor slime) { - var testIdentifier = failure.testId(); - slime.setString("testName", failure.testId()); - slime.setString("testError",failure.exception().getMessage()); - slime.setString("exception", ExceptionUtils.getStackTraceAsString(failure.exception())); - } - - public String toJson() { - var slime = new Slime(); - var root = slime.setObject(); - var summary = root.setObject("summary"); - summary.setLong("total", totalCount); - summary.setLong("success", successCount); - summary.setLong("failed", failedCount); - summary.setLong("ignored", ignoredCount); - summary.setLong("aborted", abortedCount); - var failureRoot = summary.setArray("failures"); - this.failures.forEach(failure -> serializeFailure(failure, failureRoot.addObject())); - - var output = root.setArray("output"); - logLines.forEach(lr -> output.addString(lr.getMessage())); - - return Exceptions.uncheck(() -> new String(SlimeUtils.toJsonBytes(slime), StandardCharsets.UTF_8)); - } - public List<LogRecord> logLines() { return logLines; } @@ -71,7 +41,9 @@ public class TestReport { return new Builder(); } + public static class Builder { + private long totalCount; private long successCount; private long failedCount; @@ -88,18 +60,22 @@ public class TestReport { this.totalCount = totalCount; return this; } + public Builder withSuccessCount(long successCount) { this.successCount = successCount; return this; } + public Builder withFailedCount(long failedCount) { this.failedCount = failedCount; return this; } + public Builder withIgnoredCount(long ignoredCount) { this.ignoredCount = ignoredCount; return this; } + public Builder withAbortedCount(long abortedCount) { this.abortedCount = abortedCount; return this; @@ -114,8 +90,10 @@ public class TestReport { this.logLines = List.copyOf(logRecords); return this; } + } + public static class Failure { private final String testId; private final Throwable exception; @@ -132,5 +110,7 @@ public class TestReport { public Throwable exception() { return exception; } + } + } diff --git a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestRunner.java b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestRunner.java index 619fe77cf3a..5ac6a87a89c 100644 --- a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestRunner.java +++ b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestRunner.java @@ -17,5 +17,4 @@ public interface TestRunner { TestReport getReport(); - String getReportAsJson(); -} +}
\ No newline at end of file diff --git a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestRunnerHandler.java b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestRunnerHandler.java index 116f9ac311b..227b2fde44e 100644 --- a/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestRunnerHandler.java +++ b/vespa-osgi-testrunner/src/main/java/com/yahoo/vespa/testrunner/TestRunnerHandler.java @@ -9,6 +9,7 @@ import com.yahoo.container.jdisc.EmptyResponse; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.container.jdisc.LoggingRequestHandler; +import com.yahoo.exception.ExceptionUtils; import com.yahoo.slime.Cursor; import com.yahoo.slime.JsonFormat; import com.yahoo.slime.Slime; @@ -21,6 +22,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -96,8 +98,7 @@ public class TestRunnerHandler extends LoggingRequestHandler { } } else if (path.equals("/tester/v1/report")) { if (useOsgiMode) { - String report = junitRunner.getReportAsJson(); - return new SlimeJsonResponse(SlimeUtils.jsonToSlime(report)); + return new SlimeJsonResponse(toSlime(junitRunner.getReport())); } else { return new EmptyResponse(200); } @@ -218,4 +219,33 @@ public class TestRunnerHandler extends LoggingRequestHandler { return CONTENT_TYPE_APPLICATION_JSON; } } + + private static Slime toSlime(TestReport testReport) { + var slime = new Slime(); + var root = slime.setObject(); + if (testReport == null) + return slime; + + var summary = root.setObject("summary"); + summary.setLong("total", testReport.totalCount); + summary.setLong("success", testReport.successCount); + summary.setLong("failed", testReport.failedCount); + summary.setLong("ignored", testReport.ignoredCount); + summary.setLong("aborted", testReport.abortedCount); + var failureRoot = summary.setArray("failures"); + testReport.failures.forEach(failure -> serializeFailure(failure, failureRoot.addObject())); + + var output = root.setArray("output"); + testReport.logLines.forEach(lr -> output.addString(lr.getMessage())); + + return slime; + } + + private static void serializeFailure(TestReport.Failure failure, Cursor slime) { + var testIdentifier = failure.testId(); + slime.setString("testName", failure.testId()); + slime.setString("testError",failure.exception().getMessage()); + slime.setString("exception", ExceptionUtils.getStackTraceAsString(failure.exception())); + } + } diff --git a/vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java b/vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java index 0caac95ed34..4324ffd6a62 100644 --- a/vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java +++ b/vespa-osgi-testrunner/src/test/java/com/yahoo/vespa/testrunner/TestRunnerHandlerTest.java @@ -145,9 +145,5 @@ public class TestRunnerHandlerTest { return testReport; } - @Override - public String getReportAsJson() { - return getReport().toJson(); - } } } |