1 package com.networknt.schema; 2 3 import static org.junit.jupiter.api.Assertions.assertTrue; 4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 6 import com.fasterxml.jackson.databind.JsonNode; 7 import com.fasterxml.jackson.databind.ObjectMapper; 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.util.List; 11 import java.util.Set; 12 import org.junit.jupiter.api.BeforeEach; 13 import org.junit.jupiter.api.Test; 14 15 public class Issue366FailFastTest { 16 17 @BeforeEach setup()18 public void setup() throws IOException { 19 setupSchema(); 20 } 21 22 JsonSchema jsonSchema; 23 ObjectMapper objectMapper = new ObjectMapper(); 24 setupSchema()25 private void setupSchema() throws IOException { 26 27 SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig(); 28 schemaValidatorsConfig.setFailFast(true); 29 JsonSchemaFactory schemaFactory = JsonSchemaFactory 30 .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).jsonMapper(objectMapper).build(); 31 32 schemaValidatorsConfig.setTypeLoose(false); 33 34 SchemaLocation uri = getSchema(); 35 36 InputStream in = getClass().getResourceAsStream("/schema/issue366_schema.json"); 37 JsonNode testCases = objectMapper.readValue(in, JsonNode.class); 38 this.jsonSchema = schemaFactory.getSchema(uri, testCases, schemaValidatorsConfig); 39 } 40 getJsonNodeFromStreamContent(InputStream content)41 protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception { 42 ObjectMapper mapper = new ObjectMapper(); 43 JsonNode node = mapper.readTree(content); 44 return node; 45 } 46 47 @Test firstOneValid()48 public void firstOneValid() throws Exception { 49 String dataPath = "/data/issue366.json"; 50 51 InputStream dataInputStream = getClass().getResourceAsStream(dataPath); 52 JsonNode node = getJsonNodeFromStreamContent(dataInputStream); 53 List<JsonNode> testNodes = node.findValues("tests"); 54 JsonNode testNode = testNodes.get(0).get(0); 55 JsonNode dataNode = testNode.get("data"); 56 Set<ValidationMessage> errors = jsonSchema.validate(dataNode); 57 assertTrue(errors.isEmpty()); 58 } 59 60 @Test secondOneValid()61 public void secondOneValid() throws Exception { 62 String dataPath = "/data/issue366.json"; 63 64 InputStream dataInputStream = getClass().getResourceAsStream(dataPath); 65 JsonNode node = getJsonNodeFromStreamContent(dataInputStream); 66 List<JsonNode> testNodes = node.findValues("tests"); 67 JsonNode testNode = testNodes.get(0).get(1); 68 JsonNode dataNode = testNode.get("data"); 69 Set<ValidationMessage> errors = jsonSchema.validate(dataNode); 70 assertTrue(errors.isEmpty()); 71 } 72 73 @Test bothValid()74 public void bothValid() throws Exception { 75 String dataPath = "/data/issue366.json"; 76 77 InputStream dataInputStream = getClass().getResourceAsStream(dataPath); 78 JsonNode node = getJsonNodeFromStreamContent(dataInputStream); 79 List<JsonNode> testNodes = node.findValues("tests"); 80 JsonNode testNode = testNodes.get(0).get(2); 81 JsonNode dataNode = testNode.get("data"); 82 assertEquals(1, jsonSchema.validate(dataNode).size()); 83 } 84 85 @Test neitherValid()86 public void neitherValid() throws Exception { 87 String dataPath = "/data/issue366.json"; 88 89 InputStream dataInputStream = getClass().getResourceAsStream(dataPath); 90 JsonNode node = getJsonNodeFromStreamContent(dataInputStream); 91 List<JsonNode> testNodes = node.findValues("tests"); 92 JsonNode testNode = testNodes.get(0).get(3); 93 JsonNode dataNode = testNode.get("data"); 94 assertEquals(1, jsonSchema.validate(dataNode).size()); 95 } 96 getSchema()97 private SchemaLocation getSchema() { 98 return SchemaLocation.of("classpath:" + "/draft7/issue366_schema.json"); 99 } 100 } 101