|
27 | 27 | * Created by steve on 22/10/16.
|
28 | 28 | */
|
29 | 29 | public class BaseJsonSchemaValidatorTest {
|
| 30 | + |
| 31 | + private ObjectMapper mapper = new ObjectMapper(); |
| 32 | + |
30 | 33 | protected JsonNode getJsonNodeFromClasspath(String name) throws Exception {
|
31 | 34 | InputStream is1 = Thread.currentThread().getContextClassLoader()
|
32 | 35 | .getResourceAsStream(name);
|
33 |
| - |
34 |
| - ObjectMapper mapper = new ObjectMapper(); |
35 |
| - JsonNode node = mapper.readTree(is1); |
36 |
| - return node; |
| 36 | + return mapper.readTree(is1); |
37 | 37 | }
|
38 | 38 |
|
39 | 39 | protected JsonNode getJsonNodeFromStringContent(String content) throws Exception {
|
40 |
| - ObjectMapper mapper = new ObjectMapper(); |
41 |
| - JsonNode node = mapper.readTree(content); |
42 |
| - return node; |
| 40 | + return mapper.readTree(content); |
43 | 41 | }
|
44 | 42 |
|
45 | 43 | protected JsonNode getJsonNodeFromUrl(String url) throws Exception {
|
46 |
| - ObjectMapper mapper = new ObjectMapper(); |
47 |
| - JsonNode node = mapper.readTree(new URL(url)); |
48 |
| - return node; |
| 44 | + return mapper.readTree(new URL(url)); |
49 | 45 | }
|
50 | 46 |
|
51 |
| - protected JsonSchema getJsonSchemaFromClasspath(String name) throws Exception { |
| 47 | + protected JsonSchema getJsonSchemaFromClasspath(String name) { |
52 | 48 | JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
|
53 | 49 | InputStream is = Thread.currentThread().getContextClassLoader()
|
54 | 50 | .getResourceAsStream(name);
|
55 |
| - JsonSchema schema = factory.getSchema(is); |
56 |
| - return schema; |
| 51 | + return factory.getSchema(is); |
57 | 52 | }
|
58 | 53 |
|
59 |
| - |
60 |
| - protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) throws Exception { |
| 54 | + protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) { |
61 | 55 | JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
|
62 |
| - JsonSchema schema = factory.getSchema(schemaContent); |
63 |
| - return schema; |
| 56 | + return factory.getSchema(schemaContent); |
64 | 57 | }
|
65 | 58 |
|
66 | 59 | protected JsonSchema getJsonSchemaFromUrl(String uri) throws Exception {
|
67 | 60 | JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
|
68 |
| - JsonSchema schema = factory.getSchema(new URI(uri)); |
69 |
| - return schema; |
| 61 | + return factory.getSchema(new URI(uri)); |
70 | 62 | }
|
71 | 63 |
|
72 |
| - protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) throws Exception { |
| 64 | + protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) { |
73 | 65 | JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
|
74 |
| - JsonSchema schema = factory.getSchema(jsonNode); |
75 |
| - return schema; |
| 66 | + return factory.getSchema(jsonNode); |
76 | 67 | }
|
| 68 | + |
| 69 | + // Automatically detect version for given JsonNode |
| 70 | + protected JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) { |
| 71 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNode)); |
| 72 | + return factory.getSchema(jsonNode); |
| 73 | + } |
| 74 | + |
77 | 75 | }
|
0 commit comments