• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Network New Technologies Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.networknt.schema;
18 
19 import com.fasterxml.jackson.databind.JsonNode;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.networknt.schema.serialization.JsonMapperFactory;
22 
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 
28 /**
29  * Created by steve on 22/10/16.
30  */
31 public class BaseJsonSchemaValidatorTest {
32 
33     private static final ObjectMapper mapper = JsonMapperFactory.getInstance();
34 
getJsonNodeFromClasspath(String name)35     public static JsonNode getJsonNodeFromClasspath(String name) throws IOException {
36         InputStream is1 = Thread.currentThread().getContextClassLoader()
37             .getResourceAsStream(name);
38         return mapper.readTree(is1);
39     }
40 
getJsonNodeFromStringContent(String content)41     public static JsonNode getJsonNodeFromStringContent(String content) throws IOException {
42         return mapper.readTree(content);
43     }
44 
getJsonNodeFromUrl(String url)45     public static JsonNode getJsonNodeFromUrl(String url) throws IOException {
46         return mapper.readTree(new URL(url));
47     }
48 
getJsonSchemaFromClasspath(String name)49     public static JsonSchema getJsonSchemaFromClasspath(String name) {
50         return getJsonSchemaFromClasspath(name, SpecVersion.VersionFlag.V4, null);
51     }
52 
getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion)53     public static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion) {
54         return getJsonSchemaFromClasspath(name, schemaVersion, null);
55     }
56 
getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion, SchemaValidatorsConfig config)57     public static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion, SchemaValidatorsConfig config) {
58         JsonSchemaFactory factory = JsonSchemaFactory.getInstance(schemaVersion);
59         InputStream is = Thread.currentThread().getContextClassLoader()
60             .getResourceAsStream(name);
61         if (config == null) {
62             return factory.getSchema(is);
63         }
64         return factory.getSchema(is, config);
65     }
66 
getJsonSchemaFromStringContent(String schemaContent)67     public static JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
68         JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
69         return factory.getSchema(schemaContent);
70     }
71 
getJsonSchemaFromUrl(String uri)72     public static JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException {
73         JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
74         return factory.getSchema(SchemaLocation.of(uri));
75     }
76 
getJsonSchemaFromJsonNode(JsonNode jsonNode)77     public static JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) {
78         JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
79         return factory.getSchema(jsonNode);
80     }
81 
82     // Automatically detect version for given JsonNode
getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode)83     public static JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
84         JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNode));
85         return factory.getSchema(jsonNode);
86     }
87 
88 }
89