|
| 1 | +package com.networknt.schema; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import org.junit.jupiter.api.Assertions; |
| 6 | +import org.junit.jupiter.api.Disabled; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.io.InputStream; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Locale; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +public class Issue471Test { |
| 16 | + |
| 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 |
| 28 | + |
| 29 | + @Test |
| 30 | + @Disabled |
| 31 | + public void shouldFailV201909_with_enUS() throws Exception { |
| 32 | + 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")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + @Disabled |
| 43 | + public void shouldFailV201909_with_zhCN() throws Exception { |
| 44 | + 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")); |
| 51 | + } |
| 52 | + |
| 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); |
| 57 | + JsonSchema schema = getJsonSchemaFromStreamContentV201909(schemaInputStream); |
| 58 | + InputStream dataInputStream = Issue471Test.class.getResourceAsStream(dataPath); |
| 59 | + JsonNode node = getJsonNodeFromStreamContent(dataInputStream); |
| 60 | + return schema.validate(node); |
| 61 | + } |
| 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; |
| 69 | + } |
| 70 | +} |
0 commit comments