Skip to content

Commit 7ec1623

Browse files
committed
added json format and test fot this format
1 parent 80ae655 commit 7ec1623

File tree

4 files changed

+140
-65
lines changed

4 files changed

+140
-65
lines changed

app/src/main/java/hexlet/code/formatters/Formatter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static String format(List<Map<String, Object>> diff, String format) {
99
return switch (format) {
1010
case "plain" -> Plain.format(diff);
1111
case "stylish" -> Stylish.format(diff);
12+
case "json" -> Json.format(diff);
1213
default -> throw new IllegalArgumentException("Unsupported format: " + format);
1314
};
1415
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package hexlet.code.formatters;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class Json {
9+
public static String format(List<Map<String, Object>> diff) {
10+
ObjectMapper objectMapper = new ObjectMapper();
11+
try {
12+
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(diff);
13+
} catch (JsonProcessingException e) {
14+
throw new RuntimeException("Ошибка при формировании JSON: " + e.getMessage(), e);
15+
}
16+
}
17+
}

app/src/main/java/hexlet/code/formatters/Plain.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class Plain {
77

88
public static String format(List<Map<String, Object>> diff) {
9-
StringBuilder result = new StringBuilder("\n");
9+
StringBuilder result = new StringBuilder();
1010

1111
for (Map<String, Object> entry : diff) {
1212
String key = (String) entry.get("key");
@@ -23,8 +23,7 @@ public static String format(List<Map<String, Object>> diff) {
2323
default -> {} // Игнорируем "unchanged" (оставленные без изменений)
2424
}
2525
}
26-
27-
return result.toString().trim();
26+
return result.toString();
2827
}
2928

3029
private static String formatValue(Object value) {

app/src/test/java/hexlet/code/DifferTest.java

Lines changed: 120 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,37 @@
55
import static org.junit.jupiter.api.Assertions.assertEquals;
66

77
class DifferTest {
8-
private static final String EXPECTED_DIFF = """
8+
9+
private static final String EXPECTED_JSON_STYLISH = """
10+
{
11+
chars1: [a, b, c]
12+
- chars2: [d, e, f]
13+
+ chars2: false
14+
- checked: false
15+
+ checked: true
16+
- default: null
17+
+ default: [value1, value2]
18+
- id: 45
19+
+ id: null
20+
- key1: value1
21+
+ key2: value2
22+
numbers1: [1, 2, 3, 4]
23+
- numbers2: [2, 3, 4, 5]
24+
+ numbers2: [22, 33, 44, 55]
25+
- numbers3: [3, 4, 5]
26+
+ numbers4: [4, 5, 6]
27+
+ obj1: {nestedKey=value, isNested=true}
28+
- setting1: Some value
29+
+ setting1: Another value
30+
- setting2: 200
31+
+ setting2: 300
32+
- setting3: true
33+
+ setting3: none
34+
}
35+
""";
36+
37+
38+
private static final String EXPECTED_DIFF_PLAIN = """
939
Property 'chars2' was updated. From [complex value] to false
1040
Property 'checked' was updated. From false to true
1141
Property 'default' was updated. From null to [complex value]
@@ -21,78 +51,106 @@ class DifferTest {
2151
Property 'setting3' was updated. From true to 'none'
2252
""";
2353

54+
55+
private static final String EXPECTED_JSON = """
56+
[ {
57+
"type" : "unchanged",
58+
"value" : [ "a", "b", "c" ],
59+
"key" : "chars1"
60+
}, {
61+
"newValue" : false,
62+
"oldValue" : [ "d", "e", "f" ],
63+
"type" : "updated",
64+
"key" : "chars2"
65+
}, {
66+
"newValue" : true,
67+
"oldValue" : false,
68+
"type" : "updated",
69+
"key" : "checked"
70+
}, {
71+
"newValue" : [ "value1", "value2" ],
72+
"oldValue" : null,
73+
"type" : "updated",
74+
"key" : "default"
75+
}, {
76+
"newValue" : null,
77+
"oldValue" : 45,
78+
"type" : "updated",
79+
"key" : "id"
80+
}, {
81+
"oldValue" : "value1",
82+
"type" : "removed",
83+
"key" : "key1"
84+
}, {
85+
"newValue" : "value2",
86+
"type" : "added",
87+
"key" : "key2"
88+
}, {
89+
"type" : "unchanged",
90+
"value" : [ 1, 2, 3, 4 ],
91+
"key" : "numbers1"
92+
}, {
93+
"newValue" : [ 22, 33, 44, 55 ],
94+
"oldValue" : [ 2, 3, 4, 5 ],
95+
"type" : "updated",
96+
"key" : "numbers2"
97+
}, {
98+
"oldValue" : [ 3, 4, 5 ],
99+
"type" : "removed",
100+
"key" : "numbers3"
101+
}, {
102+
"newValue" : [ 4, 5, 6 ],
103+
"type" : "added",
104+
"key" : "numbers4"
105+
}, {
106+
"newValue" : {
107+
"nestedKey" : "value",
108+
"isNested" : true
109+
},
110+
"type" : "added",
111+
"key" : "obj1"
112+
}, {
113+
"newValue" : "Another value",
114+
"oldValue" : "Some value",
115+
"type" : "updated",
116+
"key" : "setting1"
117+
}, {
118+
"newValue" : 300,
119+
"oldValue" : 200,
120+
"type" : "updated",
121+
"key" : "setting2"
122+
}, {
123+
"newValue" : "none",
124+
"oldValue" : true,
125+
"type" : "updated",
126+
"key" : "setting3"
127+
} ]
128+
""";
129+
24130
@Test
25-
void testGenerateDiffForJson() throws IOException {
131+
void testGenerateDiffForJsonStylish() throws IOException {
26132
String filePath1 = "file1.json";
27133
String filePath2 = "file2.json";
28-
String actualDiff = Differ.generate(filePath1, filePath2, "plain");
29-
assertEquals(EXPECTED_DIFF.trim(), actualDiff.trim());
134+
String actualDiff = Differ.generate(filePath1, filePath2, "stylish");
135+
assertEquals(EXPECTED_JSON_STYLISH.trim(), actualDiff.trim());
30136
System.out.println("ВСЕ РАБОТАЕТ!");
31137
}
32138

33139
@Test
34-
void testGenerateDiffForYaml() throws IOException {
140+
void testGenerateDiffForJsonPlain() throws IOException {
35141
String filePath1 = "file1.yml";
36142
String filePath2 = "file2.yml";
37143
String actualDiff = Differ.generate(filePath1, filePath2, "plain");
38-
assertEquals(EXPECTED_DIFF.trim(), actualDiff.trim());
144+
assertEquals(EXPECTED_DIFF_PLAIN.trim(), actualDiff.trim());
39145
System.out.println("ВСЕ РАБОТАЕТ! лала!");
40146
}
41147

148+
@Test
149+
void testGenerateDiffForJsonJson() throws IOException {
150+
String filePath1 = "file1.json";
151+
String filePath2 = "file2.json";
152+
String actualJson = Differ.generate(filePath1, filePath2, "json");
153+
assertEquals(EXPECTED_JSON.trim(), actualJson.trim());
154+
System.out.println("ВСЕ РАБОТАЕТ! аддада");
155+
}
42156
}
43-
44-
45-
46-
47-
//
48-
//@Test
49-
//void testGenerateDiffJson() throws IOException {
50-
// // Указываем пути к фикстурам
51-
// String filePath1 = "file1.json";
52-
// String filePath2 = "file2.json";
53-
//
54-
// // Ожидаемый результат сравнения JSON-файлов
55-
// String expectedDiff = """
56-
// {
57-
// - follow: false
58-
// host: hexlet.io
59-
// - proxy: 123.234.53.22
60-
// - timeout: 50
61-
// + timeout: 20
62-
// + verbose: true
63-
// }
64-
// """;
65-
//
66-
// // Запускаем метод сравнения
67-
// String actualDiff = Differ.generate(filePath1, filePath2);
68-
//
69-
// // Проверяем, что результат совпадает с ожидаемым
70-
// assertEquals(expectedDiff.trim(), actualDiff.trim());
71-
// System.out.println("ВСЕ РАБОТАЕТ!!");
72-
//}
73-
//
74-
//@Test
75-
//void testGenerateDiffYml() throws IOException {
76-
// // Указываем пути к фикстурам
77-
// String filePath1 = "file1.yml";
78-
// String filePath2 = "file2.yml";
79-
//
80-
// // Ожидаемый результат сравнения YML-файлов
81-
// String expectedDiff = """
82-
// {
83-
// - follow: false
84-
// host: hexlet.io
85-
// - proxy: 123.234.53.22
86-
// - timeout: 50
87-
// + timeout: 20
88-
// + verbose: true
89-
// }
90-
// """;
91-
//
92-
// // Запускаем метод сравнения
93-
// String actualDiff = Differ.generate(filePath1, filePath2);
94-
//
95-
// // Проверяем, что результат совпадает с ожидаемым
96-
// assertEquals(expectedDiff.trim(), actualDiff.trim());
97-
// System.out.println("ВСЕ РАБОТАЕТ! лала!");
98-
//}

0 commit comments

Comments
 (0)