|
1 | 1 | package source; |
2 | 2 |
|
3 | 3 | import hexlet.code.source.Differ; |
| 4 | +import org.junit.jupiter.api.BeforeAll; |
4 | 5 | import org.junit.jupiter.api.Test; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertEquals; |
6 | 7 | import static org.junit.jupiter.api.Assertions.assertThrows; |
7 | 8 |
|
8 | 9 | import java.io.FileNotFoundException; |
9 | 10 | import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
10 | 13 |
|
11 | 14 | public class TestJsonDiffer { |
12 | 15 |
|
13 | | - String differsStylish = "{\n" |
14 | | - + " chars1: [a, b, c]\n" |
15 | | - + "- chars2: [d, e, f]\n" |
16 | | - + "+ chars2: false\n" |
17 | | - + "- checked: false\n" |
18 | | - + "+ checked: true\n" |
19 | | - + "- default: null\n" |
20 | | - + "+ default: [value1, value2]\n" |
21 | | - + "- id: 45\n" |
22 | | - + "+ id: null\n" |
23 | | - + "- key1: value1\n" |
24 | | - + "+ key2: value2\n" |
25 | | - + " numbers1: [1, 2, 3, 4]\n" |
26 | | - + "- numbers2: [2, 3, 4, 5]\n" |
27 | | - + "+ numbers2: [22, 33, 44, 55]\n" |
28 | | - + "- numbers3: [3, 4, 5]\n" |
29 | | - + "+ numbers4: [4, 5, 6]\n" |
30 | | - + "+ obj1: {nestedKey=value, isNested=true}\n" |
31 | | - + "- setting1: Some value\n" |
32 | | - + "+ setting1: Another value\n" |
33 | | - + "- setting2: 200\n" |
34 | | - + "+ setting2: 300\n" |
35 | | - + "- setting3: true\n" |
36 | | - + "+ setting3: none\n" |
37 | | - + "}"; |
38 | | - |
39 | | - String differsPlain = "Property 'chars2' was updated. From [complex value] to false\n" |
40 | | - + "Property 'checked' was updated. From false to true\n" |
41 | | - + "Property 'default' was updated. From null to [complex value]\n" |
42 | | - + "Property 'id' was updated. From 45 to null\n" |
43 | | - + "Property 'key1' was removed\n" |
44 | | - + "Property 'key2' was added with value: 'value2'\n" |
45 | | - + "Property 'numbers2' was updated. From [complex value] to [complex value]\n" |
46 | | - + "Property 'numbers3' was removed\n" |
47 | | - + "Property 'numbers4' was added with value: [complex value]\n" |
48 | | - + "Property 'obj1' was added with value: [complex value]\n" |
49 | | - + "Property 'setting1' was updated. From 'Some value' to 'Another value'\n" |
50 | | - + "Property 'setting2' was updated. From 200 to 300\n" |
51 | | - + "Property 'setting3' was updated. From true to 'none'"; |
52 | | - |
53 | | - String differsJson = "[{\"key\":\"chars1\",\"oldValue\":[\"a\",\"b\",\"c\"],\"newValue\":[\"a\",\"b\",\"c\"]," |
54 | | - + "\"state\":\"UNC\"}," |
55 | | - + "{\"key\":\"chars2\",\"oldValue\":[\"d\",\"e\",\"f\"],\"newValue\":false,\"state\":\"CHN\"}," |
56 | | - + "{\"key\":\"checked\",\"oldValue\":false,\"newValue\":true,\"state\":\"CHN\"}," |
57 | | - + "{\"key\":\"default\",\"oldValue\":\"null\",\"newValue\":[\"value1\",\"value2\"],\"state\":\"CHN\"}," |
58 | | - + "{\"key\":\"id\",\"oldValue\":45,\"newValue\":\"null\",\"state\":\"CHN\"}," |
59 | | - + "{\"key\":\"key1\",\"oldValue\":\"value1\",\"newValue\":null,\"state\":\"DEL\"}," |
60 | | - + "{\"key\":\"key2\",\"oldValue\":null,\"newValue\":\"value2\",\"state\":\"ADD\"}," |
61 | | - + "{\"key\":\"numbers1\",\"oldValue\":[1,2,3,4],\"newValue\":[1,2,3,4],\"state\":\"UNC\"}," |
62 | | - + "{\"key\":\"numbers2\",\"oldValue\":[2,3,4,5],\"newValue\":[22,33,44,55],\"state\":\"CHN\"}," |
63 | | - + "{\"key\":\"numbers3\",\"oldValue\":[3,4,5],\"newValue\":null,\"state\":\"DEL\"}," |
64 | | - + "{\"key\":\"numbers4\",\"oldValue\":null,\"newValue\":[4,5,6],\"state\":\"ADD\"}," |
65 | | - + "{\"key\":\"obj1\",\"oldValue\":null,\"newValue\":{\"nestedKey\":\"value\",\"isNested\":true}," |
66 | | - + "\"state\":\"ADD\"}," |
67 | | - + "{\"key\":\"setting1\",\"oldValue\":\"Some value\",\"newValue\":\"Another value\",\"state\":\"CHN\"}," |
68 | | - + "{\"key\":\"setting2\",\"oldValue\":200,\"newValue\":300,\"state\":\"CHN\"}," |
69 | | - + "{\"key\":\"setting3\",\"oldValue\":true,\"newValue\":\"none\",\"state\":\"CHN\"}]"; |
| 16 | + static final String STYLISH_REPORT_PATH = "src/test/java/fixtures/Stylish"; |
| 17 | + static final String PLAIN_REPORT_PATH = "src/test/java/fixtures/Plain"; |
| 18 | + static final String JSON_REPORT_PATH = "src/test/java/fixtures/Json"; |
| 19 | + |
| 20 | + static String differsStylish; |
| 21 | + static String differsPlain; |
| 22 | + static String differsJson; |
| 23 | + |
| 24 | + |
| 25 | + @BeforeAll |
| 26 | + public static void readFixtures() throws IOException { |
| 27 | + differsStylish = readReroptAsString(Path.of(STYLISH_REPORT_PATH)); |
| 28 | + differsPlain = readReroptAsString(Path.of(PLAIN_REPORT_PATH)); |
| 29 | + differsJson = readReroptAsString(Path.of(JSON_REPORT_PATH)); |
| 30 | + |
| 31 | + } |
| 32 | + |
70 | 33 |
|
71 | 34 | @Test |
72 | 35 | public void testGenerateStylish() { |
@@ -127,4 +90,24 @@ public void testWrongPathFile() { |
127 | 90 | assertEquals("Файл для чтения не найден", thrownSecondArg.getMessage()); |
128 | 91 | } |
129 | 92 |
|
| 93 | + |
| 94 | + @Test |
| 95 | + public void testGenerateWithoutFormat() { |
| 96 | + var file1 = "src/test/java/resources/File1.json"; |
| 97 | + var file2 = "src/test/java/resources/File2.json"; |
| 98 | + var formant = "stylish"; |
| 99 | + String result = ""; |
| 100 | + try { |
| 101 | + result = Differ.generate(file1, file2, formant); |
| 102 | + } catch (IOException e) { |
| 103 | + throw new RuntimeException(e); |
| 104 | + } |
| 105 | + assertEquals(differsStylish, result); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + public static String readReroptAsString(Path path) throws IOException { |
| 110 | + var normalizePath = path.normalize().toAbsolutePath(); |
| 111 | + return Files.readString(normalizePath); |
| 112 | + } |
130 | 113 | } |
0 commit comments