BAEL-6692 - converting json to map and comparing (#14630)
* BAEL-6692 - converting json to map and comparing * BAEL-6692 - converting json to map and comparing * BAEL-6692 - converting json to map and comparing
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.jsontomap;
|
||||
|
||||
import com.google.common.collect.MapDifference;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JSONComparisonUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoJsonFiles_whenCompared_thenTheyAreDifferent() throws IOException {
|
||||
Map<String, Object> firstMap = JsonUtils.jsonFileToMap("src/test/resources/first.json");
|
||||
Map<String, Object> secondMap = JsonUtils.jsonFileToMap("src/test/resources/second.json");
|
||||
|
||||
MapDifference<String, Object> difference = Maps.difference(firstMap, secondMap);
|
||||
difference.entriesDiffering().forEach((key, value) -> {
|
||||
System.out.println(key + ": " + value.leftValue() + " - " + value.rightValue());
|
||||
});
|
||||
assertThat(difference.areEqual()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoJsonFiles_whenFlattenedAndCompared_thenTheyAreDifferent() throws IOException {
|
||||
Map<String, Object> firstFlatMap = FlattenUtils.flatten(JsonUtils.jsonFileToMap("src/test/resources/first.json"));
|
||||
Map<String, Object> secondFlatMap = FlattenUtils.flatten(JsonUtils.jsonFileToMap("src/test/resources/second.json"));
|
||||
|
||||
MapDifference<String, Object> difference = Maps.difference(firstFlatMap, secondFlatMap);
|
||||
difference.entriesDiffering().forEach((key, value) -> {
|
||||
System.out.println(key + ": " + value.leftValue() + " - " + value.rightValue());
|
||||
});
|
||||
assertThat(difference.areEqual()).isFalse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "John",
|
||||
"age": 30,
|
||||
"cars": [
|
||||
"Ford",
|
||||
"BMW"
|
||||
],
|
||||
"address": {
|
||||
"street": "Second Street",
|
||||
"city": "New York"
|
||||
},
|
||||
"children": [
|
||||
{
|
||||
"name": "Sara",
|
||||
"age": 5
|
||||
},
|
||||
{
|
||||
"name": "Alex",
|
||||
"age": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "John",
|
||||
"age": 30,
|
||||
"cars": [
|
||||
"Ford",
|
||||
"Audi"
|
||||
],
|
||||
"address": {
|
||||
"street": "Main Street",
|
||||
"city": "New York"
|
||||
},
|
||||
"children": [
|
||||
{
|
||||
"name": "Peter",
|
||||
"age": 5
|
||||
},
|
||||
{
|
||||
"name": "Cathy",
|
||||
"age": 10
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user