diff --git a/java-reporter-core/src/main/java/io/testomat/core/batch/BatchResultManager.java b/java-reporter-core/src/main/java/io/testomat/core/batch/BatchResultManager.java index 5b29c80..d0af38e 100644 --- a/java-reporter-core/src/main/java/io/testomat/core/batch/BatchResultManager.java +++ b/java-reporter-core/src/main/java/io/testomat/core/batch/BatchResultManager.java @@ -135,6 +135,8 @@ public synchronized void flushPendingResults() { /** * Sends batch of test results with retry mechanism. * Uses exponential backoff for retries and moves failed results to failed list. + * Runtime exceptions are handled the same way as I/O errors: an exception that escapes + * the periodic flush task would cancel all its further executions. * * @param results list of test results to send * @param attempt current attempt number (1-based) @@ -148,7 +150,7 @@ private void sendBatch(List results, int attempt) { apiClient.reportTests(runUid, results); log.debug("Reported batch of {} tests", results.size()); } - } catch (IOException e) { + } catch (IOException | RuntimeException e) { log.error("Failed to report batch (attempt {}/{}): {}", attempt, MAX_RETRY_ATTEMPTS, e.getMessage()); diff --git a/java-reporter-core/src/main/java/io/testomat/core/client/request/NativeRequestBodyBuilder.java b/java-reporter-core/src/main/java/io/testomat/core/client/request/NativeRequestBodyBuilder.java index 59d3822..40ffc88 100644 --- a/java-reporter-core/src/main/java/io/testomat/core/client/request/NativeRequestBodyBuilder.java +++ b/java-reporter-core/src/main/java/io/testomat/core/client/request/NativeRequestBodyBuilder.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -173,7 +174,7 @@ private Map buildTestResultMap(TestResult result) { } if (result.getExample() != null) { - body.put("example", result.getExample()); + body.put("example", toSerializableExample(result.getExample())); } if (result.getSteps() != null && !result.getSteps().isEmpty()) { @@ -215,6 +216,32 @@ private Map buildTestResultMap(TestResult result) { return body; } + /** + * Makes parameterized test data safe for JSON serialization. + * Test parameters are arbitrary user objects, so a value that cannot be serialized + * (for example an object graph with a cycle) is reported as its string representation + * instead of failing the whole request. + */ + private Object toSerializableExample(Object example) { + if (!(example instanceof Map)) { + return toSerializableValue(example); + } + Map serializable = new LinkedHashMap<>(); + ((Map) example).forEach((name, value) -> + serializable.put(name, toSerializableValue(value))); + return serializable; + } + + private Object toSerializableValue(Object value) { + try { + return objectMapper.valueToTree(value); + } catch (IllegalArgumentException e) { + log.debug("Test parameter of type {} is not serializable, reported as a string: {}", + value.getClass().getName(), e.getMessage()); + return String.valueOf(value); + } + } + /** * Converts a list of TestStep objects to a list of maps for JSON serialization. * Each step is converted to match the API format with category, title, duration, diff --git a/java-reporter-core/src/test/java/io/testomat/core/batch/BatchResultManagerTest.java b/java-reporter-core/src/test/java/io/testomat/core/batch/BatchResultManagerTest.java index 85931ce..11f572e 100644 --- a/java-reporter-core/src/test/java/io/testomat/core/batch/BatchResultManagerTest.java +++ b/java-reporter-core/src/test/java/io/testomat/core/batch/BatchResultManagerTest.java @@ -11,6 +11,7 @@ import io.testomat.core.client.ApiInterface; import io.testomat.core.constants.PropertyValuesConstants; +import io.testomat.core.exception.ReportingFailedException; import io.testomat.core.facade.methods.artifact.model.AddTestsBatchRequest; import io.testomat.core.facade.methods.artifact.model.TestItem; import io.testomat.core.model.TestResult; @@ -176,6 +177,25 @@ void sendBatch_WithIOException_ShouldHandleGracefully() throws IOException, Inte Thread.sleep(1000); } + @Test + @DisplayName("Should keep reporting after a runtime exception from API client") + void sendBatch_WithRuntimeException_ShouldKeepReporting() throws IOException { + TestResult failingResult = createTestResult("Test 1", "passed"); + TestResult nextResult = createTestResult("Test 2", "passed"); + doThrow(new ReportingFailedException("Failed to report test")) + .when(mockApiClient).reportTest(testRunUid, failingResult); + + batchManager.addResult(failingResult); + + // The periodic flush task is cancelled by any exception that escapes it + assertDoesNotThrow(() -> batchManager.flushPendingResults()); + + batchManager.addResult(nextResult); + batchManager.flushPendingResults(); + + verify(mockApiClient, times(1)).reportTest(testRunUid, nextResult); + } + @Test @DisplayName("Should shutdown gracefully and flush remaining results") diff --git a/java-reporter-core/src/test/java/io/testomat/core/client/request/NativeRequestBodyBuilderTest.java b/java-reporter-core/src/test/java/io/testomat/core/client/request/NativeRequestBodyBuilderTest.java index 08183f4..cb9525d 100644 --- a/java-reporter-core/src/test/java/io/testomat/core/client/request/NativeRequestBodyBuilderTest.java +++ b/java-reporter-core/src/test/java/io/testomat/core/client/request/NativeRequestBodyBuilderTest.java @@ -26,7 +26,9 @@ import io.testomat.core.step.TestStep; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import org.junit.jupiter.api.BeforeEach; @@ -353,6 +355,44 @@ void buildBatchTestReportBodyMultipleResultsShouldIncludeAllResults() throws Exc assertEquals(2, jsonNode.get("tests").size()); } + @Test + @DisplayName("Should report non-serializable test parameter as string and keep the batch") + void buildBatchTestReportBodyNonSerializableParameterShouldFallBackToString() throws Exception { + + Map example = new HashMap<>(); + example.put("param0", new SelfReferencingParameter("product-1")); + example.put("param1", Collections.singletonMap("currency", "EUR")); + + TestResult parameterized = new TestResult.Builder() + .withTitle("Test 1") + .withTestId("test-1") + .withSuiteTitle("Suite 1") + .withFile("Test1.java") + .withStatus("passed") + .withExample(example) + .build(); + + TestResult regular = new TestResult.Builder() + .withTitle("Test 2") + .withTestId("test-2") + .withSuiteTitle("Suite 1") + .withFile("Test1.java") + .withStatus("passed") + .build(); + + String result = requestBodyBuilder.buildBatchTestReportBody( + Arrays.asList(parameterized, regular), + "test-api-key" + ); + + JsonNode tests = objectMapper.readTree(result).get("tests"); + JsonNode reportedExample = tests.get(0).get("example"); + + assertEquals(2, tests.size()); + assertEquals("product-1", reportedExample.get("param0").asText()); + assertEquals("EUR", reportedExample.get("param1").get("currency").asText()); + } + @Test @DisplayName("Should build finish run body with duration") void buildFinishRunBodyWithDurationShouldIncludeStatusAndDuration() throws Exception { @@ -479,4 +519,22 @@ void shouldHandleMissingStepData() { requestBodyBuilder.buildSingleTestReportBody(result) ); } + + private static class SelfReferencingParameter { + + private final String name; + + SelfReferencingParameter(String name) { + this.name = name; + } + + public SelfReferencingParameter getSelf() { + return this; + } + + @Override + public String toString() { + return name; + } + } } \ No newline at end of file