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.Locale; 11 import java.util.Map; 12 import java.util.Set; 13 import java.util.stream.Collectors; 14 15 class Issue471Test { 16 private final String DATA_PATH = "/data/issue471.json"; 17 private final String SCHEMA_PATH = "/schema/issue471-2019-09.json"; 18 19 // Only one test method is allowed at a time as the ResourceBundle is statically initialized 20 21 @Test 22 @Disabled shouldFailV201909_with_enUS()23 void shouldFailV201909_with_enUS() throws Exception { 24 Locale.setDefault(Locale.US); 25 Map<String, String> errorsMap = validate(); 26 Assertions.assertEquals("$.title: may only be 10 characters long", errorsMap.get("$.title")); 27 Assertions.assertEquals("$.pictures: there must be a maximum of 2 items in the array", errorsMap.get("$.pictures")); 28 } 29 30 @Test 31 @Disabled shouldFailV201909_with_zhCN()32 void shouldFailV201909_with_zhCN() throws Exception { 33 Locale.setDefault(Locale.CHINA); 34 Map<String, String> errorsMap = validate(); 35 Assertions.assertEquals("$.title:可能只有 10 个字符长", errorsMap.get("$.title")); 36 Assertions.assertEquals("$.pictures:数组中最多必须有 2 个项目", errorsMap.get("$.pictures")); 37 } 38 39 @Test 40 @Disabled shouldFailV201909_with_deDE()41 void shouldFailV201909_with_deDE() throws Exception { 42 Locale.setDefault(Locale.GERMANY); 43 Map<String, String> errorsMap = validate(); 44 Assertions.assertEquals("$.title darf höchstens 10 Zeichen lang sein", errorsMap.get("$.title")); 45 Assertions.assertEquals("$.pictures: Es dürfen höchstens 2 Elemente in diesem Array sein", errorsMap.get("$.pictures")); 46 } 47 48 @Test 49 @Disabled shouldFailV201909_with_frFR()50 void shouldFailV201909_with_frFR() throws Exception { 51 Locale.setDefault(Locale.FRANCE); 52 Map<String, String> errorsMap = validate(); 53 Assertions.assertEquals("$.title: ne doit pas dépasser 10 caractères", errorsMap.get("$.title")); 54 Assertions.assertEquals("$.pictures: doit avoir un maximum de 2 éléments dans le tableau", errorsMap.get("$.pictures")); 55 } 56 57 @Test 58 @Disabled shouldFailV201909_with_frIT()59 void shouldFailV201909_with_frIT() throws Exception { 60 Locale.setDefault(Locale.ITALIAN); 61 Map<String, String> errorsMap = validate(); 62 Assertions.assertEquals("$.title: può avere lunghezza massima di 10", errorsMap.get("$.title")); 63 Assertions.assertEquals("$.pictures: deve esserci un numero massimo di 2 elementi nell'array", errorsMap.get("$.pictures")); 64 } 65 validate()66 private Map<String, String> validate() throws Exception { 67 InputStream schemaInputStream = Issue471Test.class.getResourceAsStream(SCHEMA_PATH); 68 JsonSchema schema = getJsonSchemaFromStreamContentV201909(schemaInputStream); 69 InputStream dataInputStream = Issue471Test.class.getResourceAsStream(DATA_PATH); 70 JsonNode node = getJsonNodeFromStreamContent(dataInputStream); 71 72 Set<ValidationMessage> validationMessages = schema.validate(node); 73 return convertValidationMessagesToMap(validationMessages); 74 } 75 convertValidationMessagesToMap(Set<ValidationMessage> validationMessages)76 private Map<String, String> convertValidationMessagesToMap(Set<ValidationMessage> validationMessages) { 77 return validationMessages.stream().collect(Collectors.toMap(m -> m.getInstanceLocation().toString(), ValidationMessage::getMessage)); 78 } 79 getJsonSchemaFromStreamContentV201909(InputStream schemaContent)80 private JsonSchema getJsonSchemaFromStreamContentV201909(InputStream schemaContent) { 81 JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909); 82 return factory.getSchema(schemaContent); 83 } 84 getJsonNodeFromStreamContent(InputStream content)85 private JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception { 86 ObjectMapper mapper = new ObjectMapper(); 87 return mapper.readTree(content); 88 } 89 90 } 91