io.github.ericdriggs.reportcard.util.JsonCompare Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reportcard-model Show documentation
Show all versions of reportcard-model Show documentation
test report metrics and trend analysis reporting :: reportcard-model
package io.github.ericdriggs.reportcard.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.Option;
import net.javacrumbs.jsonunit.core.internal.Diff;
import org.apache.commons.lang3.ObjectUtils;
import java.util.Map;
import java.util.TreeMap;
import static net.javacrumbs.jsonunit.JsonAssert.when;
public enum JsonCompare {
; //static methods only
private final static ObjectMapper mapper = new ObjectMapper();
public static int compareTo(String expected, String actual) {
if (expected != null && actual != null) {
if (equalsIgnoreArrayOrder(expected, actual)) {
return 0;
}
}
return ObjectUtils.compare(expected, actual);
}
public static boolean equals(String expected, String actual) {
return equalsForConfiguration(expected, actual, when(Option.TREATING_NULL_AS_ABSENT));
}
@SneakyThrows(JsonProcessingException.class)
public static boolean equalsMap(String expected, Map actualMap) {
String actual = mapper.writeValueAsString(actualMap);
return equalsForConfiguration(expected, actual, when(Option.TREATING_NULL_AS_ABSENT));
}
@SneakyThrows(JsonProcessingException.class)
public static boolean containsMap(Map expectedMap, String actualJson) {
if (expectedMap == null) {
return true;
}
@SuppressWarnings("unchecked")
TreeMap actualMap = mapper.readValue(actualJson, TreeMap.class);
return actualMap.entrySet().containsAll(expectedMap.entrySet());
}
public static boolean equalsIgnoreArrayOrder(String expected, String actual) {
return equalsForConfiguration(expected, actual, when(Option.TREATING_NULL_AS_ABSENT, Option.IGNORING_ARRAY_ORDER));
}
protected static boolean equalsForConfiguration(String expected, String actual, Configuration configuration) {
Diff diff = Diff.create(expected,
actual,
"fullJson",
"",
configuration);
return diff.similar();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy