• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.networknt.schema;
2 
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.fasterxml.jackson.databind.node.ArrayNode;
6 import org.junit.jupiter.api.Test;
7 
8 import java.io.InputStream;
9 import java.util.ArrayList;
10 import java.util.List;
11 
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertFalse;
14 
15 public class Issue428Test extends HTTPServiceSupport {
16     protected ObjectMapper mapper = new ObjectMapper();
17     protected JsonSchemaFactory validatorFactory = JsonSchemaFactory
18             .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).jsonMapper(mapper).build();
19 
runTestFile(String testCaseFile)20     private void runTestFile(String testCaseFile) throws Exception {
21         final SchemaLocation testCaseFileUri = SchemaLocation.of("classpath:" + testCaseFile);
22         InputStream in = Thread.currentThread().getContextClassLoader()
23                 .getResourceAsStream(testCaseFile);
24         ArrayNode testCases = mapper.readValue(in, ArrayNode.class);
25 
26         for (int j = 0; j < testCases.size(); j++) {
27             try {
28                 JsonNode testCase = testCases.get(j);
29                 SchemaValidatorsConfig config = new SchemaValidatorsConfig();
30 
31                 ArrayNode testNodes = (ArrayNode) testCase.get("tests");
32                 for (int i = 0; i < testNodes.size(); i++) {
33                     JsonNode test = testNodes.get(i);
34                     System.out.println("=== " + test.get("description"));
35                     JsonNode node = test.get("data");
36                     JsonNode typeLooseNode = test.get("isTypeLoose");
37                     // Configure the schemaValidator to set typeLoose's value based on the test file,
38                     // if test file do not contains typeLoose flag, use default value: true.
39                     config.setTypeLoose(typeLooseNode != null && typeLooseNode.asBoolean());
40                     config.setOpenAPI3StyleDiscriminators(false);
41                     JsonSchema schema = validatorFactory.getSchema(testCaseFileUri, testCase.get("schema"), config);
42 
43                     List<ValidationMessage> errors = new ArrayList<ValidationMessage>(schema.validate(node));
44 
45                     if (test.get("valid").asBoolean()) {
46                         if (!errors.isEmpty()) {
47                             System.out.println("---- test case failed ----");
48                             System.out.println("schema: " + schema.toString());
49                             System.out.println("data: " + test.get("data"));
50                             System.out.println("errors:");
51                             for (ValidationMessage error : errors) {
52                                 System.out.println(error);
53                             }
54                         }
55                         //assertEquals(2, errors.size());
56                     } else {
57                         if (errors.isEmpty()) {
58                             System.out.println("---- test case failed ----");
59                             System.out.println("schema: " + schema);
60                             System.out.println("data: " + test.get("data"));
61                         } else {
62                             JsonNode errorCount = test.get("errorCount");
63                             if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) {
64                                 System.out.println("---- test case failed ----");
65                                 System.out.println("schema: " + schema);
66                                 System.out.println("data: " + test.get("data"));
67                                 System.out.println("errors: " + errors);
68                                 for (ValidationMessage error : errors) {
69                                     System.out.println(error);
70                                 }
71                                 assertEquals(errorCount.asInt(), errors.size(), "expected error count");
72                             }
73                         }
74                         assertFalse(errors.isEmpty());
75                     }
76 
77                 }
78 
79 
80             } catch (JsonSchemaException e) {
81                 throw new IllegalStateException(String.format("Current schema should not be invalid: %s", testCaseFile), e);
82             }
83         }
84     }
85 
86     @Test
testNullableOneOf()87     public void testNullableOneOf() throws Exception {
88         runTestFile("data/issue428.json");
89     }
90 }
91