|
| 1 | +package com.networknt.schema; |
| 2 | + |
| 3 | +import java.io.FileNotFoundException; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.MalformedURLException; |
| 6 | +import java.net.URL; |
| 7 | +import java.net.UnknownHostException; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.JsonNode; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import com.networknt.schema.JsonSchemaFactory.Builder; |
| 14 | +import com.networknt.schema.url.URLFactory; |
| 15 | + |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.fail; |
| 20 | + |
| 21 | +public class UrlMappingTest { |
| 22 | + |
| 23 | + private final ObjectMapper mapper = new ObjectMapper(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Validate that a JSON URL Mapping file containing the URL Mapping schema is |
| 27 | + * schema valid. |
| 28 | + * |
| 29 | + * @throws IOException if unable to parse the mapping file |
| 30 | + */ |
| 31 | + @Test |
| 32 | + public void testBuilderUrlMappingUrl() throws IOException { |
| 33 | + URL mappings = URLFactory.toURL("resource:tests/url_mapping/url-mapping.json"); |
| 34 | + JsonMetaSchema draftV4 = JsonMetaSchema.getDraftV4(); |
| 35 | + Builder builder = JsonSchemaFactory.builder() |
| 36 | + .defaultMetaSchemaURI(draftV4.getUri()) |
| 37 | + .addMetaSchema(draftV4) |
| 38 | + .addUrlMappings(getUrlMappingsFromUrl(mappings)); |
| 39 | + JsonSchemaFactory instance = builder.build(); |
| 40 | + JsonSchema schema = instance.getSchema(new URL( |
| 41 | + "https://raw.githubusercontent.com/networknt/json-schema-validator/master/src/test/resources/tests/url_mapping/url-mapping.schema.json")); |
| 42 | + assertEquals(0, schema.validate(mapper.readTree(mappings)).size()); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Validate that local URL is used when attempting to get a schema that is not |
| 47 | + * available publicly. Use the URL http://example.com/invalid/schema/url to use |
| 48 | + * a public URL that returns a 404 Not Found. The locally mapped schema is a |
| 49 | + * valid, but empty schema. |
| 50 | + * |
| 51 | + * @throws IOException if unable to parse the mapping file |
| 52 | + */ |
| 53 | + @Test |
| 54 | + public void testBuilderExampleMappings() throws IOException { |
| 55 | + JsonSchemaFactory instance = JsonSchemaFactory.getInstance(); |
| 56 | + URL example = new URL("http://example.com/invalid/schema/url"); |
| 57 | + // first test that attempting to use example URL throws an error |
| 58 | + try { |
| 59 | + JsonSchema schema = instance.getSchema(example); |
| 60 | + schema.validate(mapper.createObjectNode()); |
| 61 | + fail("Expected exception not thrown"); |
| 62 | + } catch (JsonSchemaException ex) { |
| 63 | + Throwable cause = ex.getCause(); |
| 64 | + if (!(cause instanceof FileNotFoundException || cause instanceof UnknownHostException)) { |
| 65 | + fail("Unexpected cause for JsonSchemaException"); |
| 66 | + } |
| 67 | + // passing, so do nothing |
| 68 | + } catch (Exception ex) { |
| 69 | + fail("Unexpected exception thrown"); |
| 70 | + } |
| 71 | + URL mappings = URLFactory.toURL("resource:tests/url_mapping/invalid-schema-url.json"); |
| 72 | + JsonMetaSchema draftV4 = JsonMetaSchema.getDraftV4(); |
| 73 | + Builder builder = JsonSchemaFactory.builder() |
| 74 | + .defaultMetaSchemaURI(draftV4.getUri()) |
| 75 | + .addMetaSchema(draftV4) |
| 76 | + .addUrlMappings(getUrlMappingsFromUrl(mappings)); |
| 77 | + instance = builder.build(); |
| 78 | + JsonSchema schema = instance.getSchema(example); |
| 79 | + assertEquals(0, schema.validate(mapper.createObjectNode()).size()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Validate that a JSON URL Mapping file containing the URL Mapping schema is |
| 84 | + * schema valid. |
| 85 | + * |
| 86 | + * @throws IOException if unable to parse the mapping file |
| 87 | + */ |
| 88 | + @Test |
| 89 | + public void testValidatorConfigUrlMappingUrl() throws IOException { |
| 90 | + JsonSchemaFactory instance = JsonSchemaFactory.getInstance(); |
| 91 | + URL mappings = URLFactory.toURL("resource:tests/url_mapping/url-mapping.json"); |
| 92 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 93 | + config.setUrlMappings(getUrlMappingsFromUrl(mappings)); |
| 94 | + JsonSchema schema = instance.getSchema(new URL( |
| 95 | + "https://raw.githubusercontent.com/networknt/json-schema-validator/master/src/test/resources/tests/url_mapping/url-mapping.schema.json"), |
| 96 | + config); |
| 97 | + assertEquals(0, schema.validate(mapper.readTree(mappings)).size()); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Validate that local URL is used when attempting to get a schema that is not |
| 102 | + * available publicly. Use the URL http://example.com/invalid/schema/url to use |
| 103 | + * a public URL that returns a 404 Not Found. The locally mapped schema is a |
| 104 | + * valid, but empty schema. |
| 105 | + * |
| 106 | + * @throws IOException if unable to parse the mapping file |
| 107 | + */ |
| 108 | + @Test |
| 109 | + public void testValidatorConfigExampleMappings() throws IOException { |
| 110 | + JsonSchemaFactory instance = JsonSchemaFactory.getInstance(); |
| 111 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 112 | + URL example = new URL("http://example.com/invalid/schema/url"); |
| 113 | + // first test that attempting to use example URL throws an error |
| 114 | + try { |
| 115 | + JsonSchema schema = instance.getSchema(example, config); |
| 116 | + schema.validate(mapper.createObjectNode()); |
| 117 | + fail("Expected exception not thrown"); |
| 118 | + } catch (JsonSchemaException ex) { |
| 119 | + Throwable cause = ex.getCause(); |
| 120 | + if (!(cause instanceof FileNotFoundException || cause instanceof UnknownHostException)) { |
| 121 | + fail("Unexpected cause for JsonSchemaException"); |
| 122 | + } |
| 123 | + // passing, so do nothing |
| 124 | + } catch (Exception ex) { |
| 125 | + fail("Unexpected exception thrown"); |
| 126 | + } |
| 127 | + URL mappings = URLFactory.toURL("resource:tests/url_mapping/invalid-schema-url.json"); |
| 128 | + config.setUrlMappings(getUrlMappingsFromUrl(mappings)); |
| 129 | + JsonSchema schema = instance.getSchema(example, config); |
| 130 | + assertEquals(0, schema.validate(mapper.createObjectNode()).size()); |
| 131 | + } |
| 132 | + |
| 133 | + private Map<URL, URL> getUrlMappingsFromUrl(URL url) throws MalformedURLException, IOException { |
| 134 | + HashMap<URL, URL> map = new HashMap<URL, URL>(); |
| 135 | + for (JsonNode mapping : mapper.readTree(url)) { |
| 136 | + map.put(URLFactory.toURL(mapping.get("publicURL").asText()), |
| 137 | + URLFactory.toURL(mapping.get("localURL").asText())); |
| 138 | + } |
| 139 | + return map; |
| 140 | + } |
| 141 | +} |
0 commit comments