• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.networknt.schema;
2 
3 import java.io.InputStream;
4 import java.util.Set;
5 import org.junit.jupiter.api.Assertions;
6 import org.junit.jupiter.api.Test;
7 import com.fasterxml.jackson.annotation.JsonInclude;
8 import com.fasterxml.jackson.databind.DeserializationFeature;
9 import com.fasterxml.jackson.databind.JsonNode;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.SerializationFeature;
12 import com.fasterxml.jackson.databind.node.BinaryNode;
13 
14 /**
15  *
16  * created at 07.02.2023
17  *
18  * @author k-oliver
19  * @since 1.0.77
20  */
21 public class Issue650Test {
22 
23     /**
24      * Test using a Java model with a byte[] property which jackson converts to a BASE64 encoded string automatically. Then convert into
25      * a jackson tree. The resulting node is of type {@link BinaryNode}. This test checks if validation handles the {@link BinaryNode} as string
26      * when validating.
27      *
28      * @throws Exception
29      * @since 1.0.77
30      */
31     @Test
testBinaryNode()32     public void testBinaryNode() throws Exception {
33         final ObjectMapper mapper = new ObjectMapper();
34         mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
35         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
36         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
37 
38         // schema with data property of type string:
39         InputStream schemaInputStream = getClass().getResourceAsStream("/draft7/issue650.json");
40         JsonSchema schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaInputStream);
41 
42         // create model first:
43         Issue650Test.Model model = new Issue650Test.Model();
44         model.setData("content".getBytes("UTF-8"));
45         // now convert to tree. The resulting type of the data property is BinaryNode now:
46         JsonNode node = mapper.valueToTree(model);
47 
48         // validate:
49         Set<ValidationMessage> errors = schema.validate(node);
50 
51         // check result:
52         Assertions.assertTrue(errors.isEmpty());
53     }
54 
55     /**
56      * created at 07.02.2023
57      *
58      * @author Oliver Kelling
59      * @since 1.0.77
60      */
61     private static class Model {
62         private byte[] data;
63 
64 
65         /**
66          * @return the data
67          * @since 1.0.77
68          */
getData()69         public byte[] getData() {
70             return this.data;
71         }
72 
73 
74         /**
75          * @param data the data to set
76          * @since 1.0.77
77          */
setData(byte[] data)78         public void setData(byte[] data) {
79             this.data = data;
80         }
81 
82     }
83 }
84