• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.networknt.schema;
2 
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.networknt.schema.i18n.DefaultMessageSource;
6 import com.networknt.schema.i18n.ResourceBundleMessageSource;
7 
8 import org.junit.jupiter.api.Test;
9 
10 import java.text.MessageFormat;
11 import java.util.Locale;
12 import java.util.ResourceBundle;
13 import java.util.Set;
14 
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 
17 public class Issue686Test {
18 
19     @Test
testDefaults()20     void testDefaults() {
21         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
22         assertEquals(DefaultMessageSource.getInstance(), config.getMessageSource());
23     }
24 
25     @Test
testValidationWithDefaultBundleAndLocale()26     void testValidationWithDefaultBundleAndLocale() throws JsonProcessingException {
27         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
28         ResourceBundle resourceBundle = ResourceBundle.getBundle(DefaultMessageSource.BUNDLE_BASE_NAME, Locale.getDefault());
29         String expectedMessage = new MessageFormat(resourceBundle.getString("type")).format(new String[] {"$.foo", "integer", "string"});
30         verify(config, expectedMessage);
31     }
32 
33     @Test
testValidationWithDefaultBundleAndCustomLocale()34     void testValidationWithDefaultBundleAndCustomLocale() throws JsonProcessingException {
35         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
36         config.setLocale(Locale.ITALIAN);
37         verify(config, "$.foo: integer trovato, string previsto");
38     }
39 
40     @Test
testValidationWithCustomBundle()41     void testValidationWithCustomBundle() throws JsonProcessingException {
42         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
43         config.setMessageSource(new ResourceBundleMessageSource("issue686/translations"));
44         config.setLocale(Locale.FRENCH);
45         verify(config, "$.foo: integer found, string expected (TEST) (FR)");
46     }
47 
48     @Test
testLocaleSwitch()49     void testLocaleSwitch() throws JsonProcessingException {
50         SchemaValidatorsConfig config = new SchemaValidatorsConfig();
51         config.setLocale(Locale.ITALIAN);
52         verify(config, "$.foo: integer trovato, string previsto");
53         config.setLocale(Locale.FRENCH);
54         verify(config, "$.foo: integer trouvé, string attendu");
55     }
56 
getSchema(SchemaValidatorsConfig config)57     private JsonSchema getSchema(SchemaValidatorsConfig config) {
58         JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
59         return factory.getSchema("{ \"$schema\": \"https://json-schema.org/draft/2019-09/schema\", \"$id\": \"https://json-schema.org/draft/2019-09/schema\", \"type\": \"object\", \"properties\": { \"foo\": { \"type\": \"string\" } } } }", config);
60     }
61 
verify(SchemaValidatorsConfig config, String expectedMessage)62     private void verify(SchemaValidatorsConfig config, String expectedMessage) throws JsonProcessingException {
63         Set<ValidationMessage> messages = getSchema(config).validate(new ObjectMapper().readTree(" { \"foo\": 123 } "));
64         assertEquals(1, messages.size());
65         assertEquals(expectedMessage, messages.iterator().next().getMessage());
66     }
67 
68 }
69