Skip to content
Merged
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
2 changes: 1 addition & 1 deletion java-reporter-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.19.2</version>
<version>0.19.3</version>
<packaging>jar</packaging>

<name>Testomat.io Reporter Core</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.testomat.core.client.serialization.SafeObjectNormalizer;
import io.testomat.core.constants.ApiRequestFields;
import io.testomat.core.exception.FailedToCreateRunBodyException;
import io.testomat.core.facade.methods.artifact.ReportedTestStorage;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class NativeRequestBodyBuilder implements RequestBodyBuilder {
private final String publishParam;

private final ObjectMapper objectMapper;
private final SafeObjectNormalizer safeObjectNormalizer;
private final PropertyProvider provider =
PropertyProviderFactoryImpl.getPropertyProviderFactory().getPropertyProvider();

Expand All @@ -64,6 +66,7 @@ public NativeRequestBodyBuilder() {
this.sharedRunTimeout = getPropertySafely(SHARED_TIMEOUT_PROPERTY_NAME);
this.objectMapper = new ObjectMapper();
this.createParam = getCreateParam();
this.safeObjectNormalizer = new SafeObjectNormalizer(objectMapper);
}

@Override
Expand Down Expand Up @@ -106,7 +109,7 @@ public String buildCreateRunBody(String title) {
@Override
public String buildSingleTestReportBody(TestResult result) throws JsonProcessingException {
Map<String, Object> body = buildTestResultMap(result);
return objectMapper.writeValueAsString(body);
return objectMapper.writeValueAsString(safeObjectNormalizer.normalize(body));
}

@Override
Expand All @@ -121,7 +124,7 @@ public String buildBatchTestReportBody(List<TestResult> results, String apiKey)
requestBody.put(API_KEY_STRING, apiKey);
requestBody.put(TESTS_STRING, testsArray);

return objectMapper.writeValueAsString(requestBody);
return objectMapper.writeValueAsString(safeObjectNormalizer.normalize(requestBody));
}

@Override
Expand All @@ -131,7 +134,7 @@ public String buildBatchReportBodyWithArtifacts(List<Map<String, Object>> testsW
requestBody.put(API_KEY_STRING, apiKey);
requestBody.put(TESTS_STRING, testsWithArtifacts);

return objectMapper.writeValueAsString(requestBody);
return objectMapper.writeValueAsString(safeObjectNormalizer.normalize(requestBody));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package io.testomat.core.client.serialization;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SafeObjectNormalizer {

private static final Logger log = LoggerFactory.getLogger(SafeObjectNormalizer.class);

private final ObjectMapper objectMapper;

public SafeObjectNormalizer(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

public Object normalize(Object value) {
Set<Object> activeObjects = Collections.newSetFromMap(new IdentityHashMap<>());

return normalizeValue(value, activeObjects);
}

private Object normalizeValue(Object value, Set<Object> activeObjects) {
if (value == null) {
return null;
}

if (value instanceof Map<?, ?>) {
if (!activeObjects.add(value)) {
return null;
}

try {
Map<String, Object> normalizedMap = new LinkedHashMap<>();

((Map<?, ?>) value).forEach((key, val) ->
normalizedMap.put(String.valueOf(key), normalizeValue(val, activeObjects)));

return normalizedMap;
} finally {
activeObjects.remove(value);
}
}

if (value instanceof Collection<?>) {
if (!activeObjects.add(value)) {
return null;
}

try {
List<Object> normalizedList = new ArrayList<>();

for (Object item : (Collection<?>) value) {
normalizedList.add(normalizeValue(item, activeObjects));
}

return normalizedList;
} finally {
activeObjects.remove(value);
}
}

if (value.getClass().isArray()) {
if (!activeObjects.add(value)) {
return null;
}

try {
List<Object> normalizedList = new ArrayList<>();
int length = Array.getLength(value);

for (int i = 0; i < length; i++) {
normalizedList.add(normalizeValue(Array.get(value, i), activeObjects));
}

return normalizedList;
} finally {
activeObjects.remove(value);
}
}

try {
objectMapper.writeValueAsString(value);
return value;
} catch (JsonProcessingException e) {
log.warn("Unable to serialize value of type: {}. Converting to string.",
value.getClass().getName(),
e);

return String.valueOf(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.testomat.core.constants;

public class CommonConstants {
public static final String REPORTER_VERSION = "0.19.2";
public static final String REPORTER_VERSION = "0.19.3";

public static final String TESTS_STRING = "tests";
public static final String API_KEY_STRING = "api_key";
Expand Down
4 changes: 2 additions & 2 deletions java-reporter-cucumber/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-cucumber</artifactId>
<version>0.10.2</version>
<version>0.10.3</version>
<packaging>jar</packaging>

<name>Testomat.io Java Reporter Cucumber</name>
Expand Down Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.19.2</version>
<version>0.19.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
4 changes: 2 additions & 2 deletions java-reporter-junit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-junit</artifactId>
<version>0.10.2</version>
<version>0.10.3</version>
<packaging>jar</packaging>

<name>Testomat.io Java Reporter JUnit</name>
Expand Down Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.19.2</version>
<version>0.19.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
4 changes: 2 additions & 2 deletions java-reporter-karate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-karate</artifactId>
<version>0.4.2</version>
<version>0.4.3</version>
<packaging>jar</packaging>

<name>Testomat.io Java Reporter Karate</name>
Expand Down Expand Up @@ -52,7 +52,7 @@
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.19.2</version>
<version>0.19.3</version>
</dependency>
<dependency>
<groupId>io.karatelabs</groupId>
Expand Down
4 changes: 2 additions & 2 deletions java-reporter-testng/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.testomat</groupId>
<artifactId>java-reporter-testng</artifactId>
<version>0.9.2</version>
<version>0.9.3</version>
<packaging>jar</packaging>

<name>Testomat.io Java Reporter TestNG</name>
Expand Down Expand Up @@ -47,7 +47,7 @@
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.19.2</version>
<version>0.19.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
4 changes: 2 additions & 2 deletions testomat-allure-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.testomat</groupId>
<artifactId>testomat-allure-adapter</artifactId>
<version>0.1.10</version>
<version>0.1.11</version>
<packaging>jar</packaging>

<name>Testomat.io Testomat Allure adapter</name>
Expand Down Expand Up @@ -67,7 +67,7 @@
<dependency>
<groupId>io.testomat</groupId>
<artifactId>java-reporter-core</artifactId>
<version>0.19.2</version>
<version>0.19.3</version>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
Expand Down
Loading