|
19 | 19 | import java.util.Map;
|
20 | 20 | import java.util.function.Function;
|
21 | 21 | import java.util.stream.Collectors;
|
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
22 | 23 |
|
23 | 24 | public class HelpFunctions {
|
24 | 25 |
|
25 |
| - public static void compareEntityWithFile(AbstractEntity entity, String string) throws IOException { |
26 |
| - InputStream inputStream = |
27 |
| - HelpFunctions.class.getResourceAsStream(string); |
28 |
| - JsonNode expectedJson = MyObjectMapper.getMapper().readTree(inputStream); |
29 |
| - JsonNode node = MyObjectMapper.getMapper().convertValue(entity, JsonNode.class); |
30 |
| - compare(expectedJson, node, true); |
31 |
| - } |
32 |
| - |
33 |
| - public static void compare(JsonNode node1, JsonNode node2, Boolean equals) { |
34 |
| - var comparator = new JsonComparator() { |
35 |
| - public boolean compareValues(Object expected, Object actual) { |
36 |
| - return expected.equals(actual); |
37 |
| - } |
38 |
| - |
39 |
| - public boolean compareFields(String expected, String actual) { |
40 |
| - return expected.equals(actual); |
41 |
| - } |
42 |
| - }; |
43 |
| - if (equals) { |
44 |
| - JSONCompare.assertMatches(node1, node2, comparator); |
45 |
| - } else { |
46 |
| - JSONCompare.assertNotMatches(node1, node2, comparator); |
| 26 | + public static void compareEntityWithFile(AbstractEntity entity, String string) throws IOException { |
| 27 | + InputStream inputStream |
| 28 | + = HelpFunctions.class.getResourceAsStream(string); |
| 29 | + JsonNode expectedJson = MyObjectMapper.getMapper().readTree(inputStream); |
| 30 | + JsonNode node = MyObjectMapper.getMapper().convertValue(entity, JsonNode.class); |
| 31 | + //compare the size of the expected and actual node. Both nodes should have the same number of properties. |
| 32 | + assertEquals(expectedJson.size(), node.size()); |
| 33 | + compare(expectedJson, node, true); |
47 | 34 | }
|
48 |
| - } |
49 |
| - |
50 |
| - public static void compareTwoMetadataJsonNotEqual(Crate crate1, Crate crate2) throws JsonProcessingException { |
51 |
| - ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
52 |
| - JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata()); |
53 |
| - JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata()); |
54 |
| - compare(node1, node2, false); |
55 |
| - } |
56 |
| - |
57 |
| - public static void compareTwoMetadataJsonNotEqual(Crate crate1, String jsonFileString) throws IOException { |
58 |
| - InputStream inputStream = HelpFunctions.class.getResourceAsStream( |
59 |
| - jsonFileString); |
60 |
| - ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
61 |
| - JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata()); |
62 |
| - JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream)); |
63 |
| - compare(node1, node2, false); |
64 |
| - } |
65 |
| - |
66 |
| - public static void compareTwoCrateJson(Crate crate1, Crate crate2) throws JsonProcessingException { |
67 |
| - ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
68 |
| - JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata()); |
69 |
| - JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata()); |
70 |
| - compare(node1, node2, true); |
71 |
| - } |
72 |
| - |
73 |
| - public static void compareCrateJsonToFileInResources(File file1, File file2) throws IOException { |
74 |
| - ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
75 |
| - JsonNode node1 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file1)); |
76 |
| - JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file2)); |
77 |
| - compare(node1, node2, true); |
78 |
| - } |
79 |
| - |
80 |
| - public static void compareCrateJsonToFileInResources(Crate crate1, String jsonFileString) throws IOException { |
81 |
| - InputStream inputStream = HelpFunctions.class.getResourceAsStream( |
82 |
| - jsonFileString); |
83 |
| - ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
84 |
| - JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata()); |
85 |
| - JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream)); |
86 |
| - compare(node1, node2, true); |
87 |
| - } |
88 |
| - |
89 |
| - public static boolean compareTwoDir(File dir1, File dir2) throws IOException { |
90 |
| - // compare the content of the two directories |
91 |
| - List<File> a = (List<java.io.File>) FileUtils.listFiles(dir1, null, true); |
92 |
| - Map<String, File> result_map = a.stream() |
93 |
| - .collect(Collectors.toMap(java.io.File::getName, Function.identity())); |
94 |
| - |
95 |
| - List<java.io.File> b = (List<java.io.File>) FileUtils.listFiles(dir2, null, true); |
96 |
| - Map<String, java.io.File> input_map = b.stream() |
97 |
| - .collect(Collectors.toMap(java.io.File::getName, Function.identity())); |
98 |
| - |
99 |
| - if (result_map.size() != input_map.size()) { |
100 |
| - return false; |
| 35 | + |
| 36 | + public static void compare(JsonNode node1, JsonNode node2, Boolean equals) { |
| 37 | + var comparator = new JsonComparator() { |
| 38 | + public boolean compareValues(Object expected, Object actual) { |
| 39 | + |
| 40 | + return expected.equals(actual); |
| 41 | + } |
| 42 | + |
| 43 | + public boolean compareFields(String expected, String actual) { |
| 44 | + return expected.equals(actual); |
| 45 | + } |
| 46 | + }; |
| 47 | + if (equals) { |
| 48 | + JSONCompare.assertMatches(node1, node2, comparator); |
| 49 | + } else { |
| 50 | + JSONCompare.assertNotMatches(node1, node2, comparator); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void compareTwoMetadataJsonNotEqual(Crate crate1, Crate crate2) throws JsonProcessingException { |
| 55 | + ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
| 56 | + JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata()); |
| 57 | + JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata()); |
| 58 | + compare(node1, node2, false); |
| 59 | + } |
| 60 | + |
| 61 | + public static void compareTwoMetadataJsonNotEqual(Crate crate1, String jsonFileString) throws IOException { |
| 62 | + InputStream inputStream = HelpFunctions.class.getResourceAsStream( |
| 63 | + jsonFileString); |
| 64 | + ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
| 65 | + JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata()); |
| 66 | + JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream)); |
| 67 | + compare(node1, node2, false); |
| 68 | + } |
| 69 | + |
| 70 | + public static void compareTwoCrateJson(Crate crate1, Crate crate2) throws JsonProcessingException { |
| 71 | + ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
| 72 | + JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata()); |
| 73 | + JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata()); |
| 74 | + compare(node1, node2, true); |
101 | 75 | }
|
102 |
| - for (String s : input_map.keySet()) { |
103 |
| - // we do that because the ro-crate-metadata.json can be differently formatted, |
104 |
| - // or the order of the entities may be different |
105 |
| - // the same holds for the html file |
106 |
| - if (s.equals("ro-crate-preview.html") || s.equals("ro-crate-metadata.json")) { |
107 |
| - if (!result_map.containsKey(s)) { |
108 |
| - return false; |
| 76 | + |
| 77 | + public static void compareCrateJsonToFileInResources(File file1, File file2) throws IOException { |
| 78 | + ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
| 79 | + JsonNode node1 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file1)); |
| 80 | + JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file2)); |
| 81 | + compare(node1, node2, true); |
| 82 | + } |
| 83 | + |
| 84 | + public static void compareCrateJsonToFileInResources(Crate crate1, String jsonFileString) throws IOException { |
| 85 | + InputStream inputStream = HelpFunctions.class.getResourceAsStream( |
| 86 | + jsonFileString); |
| 87 | + ObjectMapper objectMapper = MyObjectMapper.getMapper(); |
| 88 | + JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata()); |
| 89 | + JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream)); |
| 90 | + compare(node1, node2, true); |
| 91 | + } |
| 92 | + |
| 93 | + public static boolean compareTwoDir(File dir1, File dir2) throws IOException { |
| 94 | + // compare the content of the two directories |
| 95 | + List<File> a = (List<java.io.File>) FileUtils.listFiles(dir1, null, true); |
| 96 | + Map<String, File> result_map = a.stream() |
| 97 | + .collect(Collectors.toMap(java.io.File::getName, Function.identity())); |
| 98 | + |
| 99 | + List<java.io.File> b = (List<java.io.File>) FileUtils.listFiles(dir2, null, true); |
| 100 | + Map<String, java.io.File> input_map = b.stream() |
| 101 | + .collect(Collectors.toMap(java.io.File::getName, Function.identity())); |
| 102 | + |
| 103 | + if (result_map.size() != input_map.size()) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + for (String s : input_map.keySet()) { |
| 107 | + // we do that because the ro-crate-metadata.json can be differently formatted, |
| 108 | + // or the order of the entities may be different |
| 109 | + // the same holds for the html file |
| 110 | + if (s.equals("ro-crate-preview.html") || s.equals("ro-crate-metadata.json")) { |
| 111 | + if (!result_map.containsKey(s)) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + } else if (!FileUtils.contentEqualsIgnoreEOL(input_map.get(s), result_map.get(s), null)) { |
| 115 | + return false; |
| 116 | + } |
109 | 117 | }
|
110 |
| - } else if (!FileUtils.contentEqualsIgnoreEOL(input_map.get(s), result_map.get(s), null)) { |
111 |
| - return false; |
112 |
| - } |
| 118 | + return true; |
113 | 119 | }
|
114 |
| - return true; |
115 |
| - } |
116 | 120 | }
|
0 commit comments