• 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 org.junit.jupiter.api.Assertions;
6 import org.junit.jupiter.api.Test;
7 
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Set;
13 
14 public class Issue832Test {
15     private class NoMatchFormat implements Format {
16         @Override
matches(ExecutionContext executionContext, String value)17         public boolean matches(ExecutionContext executionContext, String value) {
18             return false;
19         }
20 
21         @Override
getName()22         public String getName() {
23             return "no_match";
24         }
25 
26         @Override
getErrorMessageDescription()27         public String getErrorMessageDescription() {
28             return "always fail match";
29         }
30     }
31 
buildV7PlusNoFormatSchemaFactory()32     private JsonSchemaFactory buildV7PlusNoFormatSchemaFactory() {
33         List<Format> formats;
34         formats = new ArrayList<>();
35         formats.add(new NoMatchFormat());
36 
37         JsonMetaSchema jsonMetaSchema = JsonMetaSchema.builder(
38                 JsonMetaSchema.getV7().getIri(),
39                 JsonMetaSchema.getV7())
40                 .formats(formats)
41                 .build();
42         return new JsonSchemaFactory.Builder().defaultMetaSchemaIri(jsonMetaSchema.getIri()).metaSchema(jsonMetaSchema).build();
43     }
44 
getJsonNodeFromStreamContent(InputStream content)45     protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws IOException {
46         ObjectMapper mapper = new ObjectMapper();
47         return mapper.readTree(content);
48     }
49 
50     @Test
testV7WithNonMatchingCustomFormat()51     public void testV7WithNonMatchingCustomFormat() throws IOException {
52         String schemaPath = "/schema/issue832-v7.json";
53         String dataPath = "/data/issue832.json";
54         InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
55         JsonSchemaFactory factory = buildV7PlusNoFormatSchemaFactory();
56         JsonSchema schema = factory.getSchema(schemaInputStream);
57         InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
58         JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
59         Set<ValidationMessage> errors = schema.validate(node);
60         // Both the custom no_match format and the standard email format should fail.
61         // This ensures that both the standard and custom formatters have been invoked.
62         Assertions.assertEquals(2, errors.size());
63     }
64 }
65