Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -148,7 +150,7 @@ private void sendBatch(List<TestResult> 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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -173,7 +174,7 @@ private Map<String, Object> 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()) {
Expand Down Expand Up @@ -215,6 +216,32 @@ private Map<String, Object> 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<Object, Object> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> 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 {
Expand Down Expand Up @@ -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;
}
}
}