|
11 | 11 | import java.util.Locale;
|
12 | 12 | import java.util.Map;
|
13 | 13 | import java.util.Set;
|
| 14 | +import java.util.stream.Collectors; |
14 | 15 |
|
15 | 16 | public class Issue471Test {
|
| 17 | + private final String DATA_PATH = "/data/issue471.json"; |
| 18 | + private final String SCHEMA_PATH = "/schema/issue471-2019-09.json"; |
16 | 19 |
|
17 |
| - private JsonSchema getJsonSchemaFromStreamContentV201909(InputStream schemaContent) { |
18 |
| - JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909); |
19 |
| - return factory.getSchema(schemaContent); |
20 |
| - } |
21 |
| - |
22 |
| - private JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception { |
23 |
| - ObjectMapper mapper = new ObjectMapper(); |
24 |
| - return mapper.readTree(content); |
25 |
| - } |
26 |
| - |
27 |
| - // Only one test method is allowed at a time |
| 20 | + // Only one test method is allowed at a time as the ResourceBundle is statically initialized |
28 | 21 |
|
29 | 22 | @Test
|
30 | 23 | @Disabled
|
31 | 24 | public void shouldFailV201909_with_enUS() throws Exception {
|
32 | 25 | Locale.setDefault(Locale.US);
|
33 |
| - Set<ValidationMessage> errors = validate(); |
34 |
| - Map<String, String> errorMsgMap = transferErrorMsg(errors); |
35 |
| - Assertions.assertTrue(errorMsgMap.containsKey("$.title"), "error message must contains key: $.title"); |
36 |
| - Assertions.assertTrue(errorMsgMap.containsKey("$.pictures"), "error message must contains key: $.pictures"); |
37 |
| - Assertions.assertEquals("$.title: may only be 10 characters long", errorMsgMap.get("$.title")); |
38 |
| - Assertions.assertEquals("$.pictures: there must be a maximum of 2 items in the array", errorMsgMap.get("$.pictures")); |
| 26 | + Map<String, String> errorsMap = validate(); |
| 27 | + Assertions.assertEquals("$.title: may only be 10 characters long", errorsMap.get("$.title")); |
| 28 | + Assertions.assertEquals("$.pictures: there must be a maximum of 2 items in the array", errorsMap.get("$.pictures")); |
39 | 29 | }
|
40 | 30 |
|
41 | 31 | @Test
|
42 | 32 | @Disabled
|
43 | 33 | public void shouldFailV201909_with_zhCN() throws Exception {
|
44 | 34 | Locale.setDefault(Locale.CHINA);
|
45 |
| - Set<ValidationMessage> errors = validate(); |
46 |
| - Map<String, String> errorMsgMap = transferErrorMsg(errors); |
47 |
| - Assertions.assertTrue(errorMsgMap.containsKey("$.title"), "error message must contains key: $.title"); |
48 |
| - Assertions.assertTrue(errorMsgMap.containsKey("$.pictures"), "error message must contains key: $.pictures"); |
49 |
| - Assertions.assertEquals("$.title:可能只有 10 个字符长", errorMsgMap.get("$.title")); |
50 |
| - Assertions.assertEquals("$.pictures:数组中最多必须有 2 个项目", errorMsgMap.get("$.pictures")); |
| 35 | + Map<String, String> errorsMap = validate(); |
| 36 | + Assertions.assertEquals("$.title:可能只有 10 个字符长", errorsMap.get("$.title")); |
| 37 | + Assertions.assertEquals("$.pictures:数组中最多必须有 2 个项目", errorsMap.get("$.pictures")); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + @Disabled |
| 42 | + public void shouldFailV201909_with_deDE() throws Exception { |
| 43 | + Locale.setDefault(Locale.GERMANY); |
| 44 | + Map<String, String> errorsMap = validate(); |
| 45 | + Assertions.assertEquals("$.title darf höchstens 10 Zeichen lang sein", errorsMap.get("$.title")); |
| 46 | + Assertions.assertEquals("$.pictures: Es dürfen höchstens 2 Elemente in diesem Array sein", errorsMap.get("$.pictures")); |
51 | 47 | }
|
52 | 48 |
|
53 |
| - private Set<ValidationMessage> validate() throws Exception { |
54 |
| - String schemaPath = "/schema/issue471-2019-09.json"; |
55 |
| - String dataPath = "/data/issue471.json"; |
56 |
| - InputStream schemaInputStream = Issue471Test.class.getResourceAsStream(schemaPath); |
| 49 | + private Map<String, String> validate() throws Exception { |
| 50 | + InputStream schemaInputStream = Issue471Test.class.getResourceAsStream(SCHEMA_PATH); |
57 | 51 | JsonSchema schema = getJsonSchemaFromStreamContentV201909(schemaInputStream);
|
58 |
| - InputStream dataInputStream = Issue471Test.class.getResourceAsStream(dataPath); |
| 52 | + InputStream dataInputStream = Issue471Test.class.getResourceAsStream(DATA_PATH); |
59 | 53 | JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
|
60 |
| - return schema.validate(node); |
| 54 | + |
| 55 | + Set<ValidationMessage> validationMessages = schema.validate(node); |
| 56 | + return convertValidationMessagesToMap(validationMessages); |
| 57 | + } |
| 58 | + |
| 59 | + private Map<String, String> convertValidationMessagesToMap(Set<ValidationMessage> validationMessages) { |
| 60 | + return validationMessages.stream().collect(Collectors.toMap(ValidationMessage::getPath, ValidationMessage::getMessage)); |
61 | 61 | }
|
62 | 62 |
|
63 |
| - private Map<String, String> transferErrorMsg(Set<ValidationMessage> validationMessages) { |
64 |
| - Map<String, String> pathToMessage = new HashMap<>(); |
65 |
| - validationMessages.forEach(msg -> { |
66 |
| - pathToMessage.put(msg.getPath(), msg.getMessage()); |
67 |
| - }); |
68 |
| - return pathToMessage; |
| 63 | + private JsonSchema getJsonSchemaFromStreamContentV201909(InputStream schemaContent) { |
| 64 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909); |
| 65 | + return factory.getSchema(schemaContent); |
69 | 66 | }
|
| 67 | + |
| 68 | + private JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception { |
| 69 | + ObjectMapper mapper = new ObjectMapper(); |
| 70 | + return mapper.readTree(content); |
| 71 | + } |
| 72 | + |
70 | 73 | }
|
0 commit comments