• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.networknt.schema;
17 
18 import static org.junit.jupiter.api.Assertions.*;
19 
20 import org.junit.jupiter.api.Test;
21 
22 class SchemaLocationTest {
23 
24     @Test
ofAbsoluteIri()25     void ofAbsoluteIri() {
26         SchemaLocation schemaLocation = SchemaLocation.of("https://json-schema.org/draft/2020-12/schema");
27         assertEquals("https://json-schema.org/draft/2020-12/schema", schemaLocation.getAbsoluteIri().toString());
28         assertEquals(0, schemaLocation.getFragment().getNameCount());
29         assertEquals("https://json-schema.org/draft/2020-12/schema#", schemaLocation.toString());
30     }
31 
32     @Test
ofAbsoluteIriWithJsonPointer()33     void ofAbsoluteIriWithJsonPointer() {
34         SchemaLocation schemaLocation = SchemaLocation.of("https://json-schema.org/draft/2020-12/schema#/properties/0");
35         assertEquals("https://json-schema.org/draft/2020-12/schema", schemaLocation.getAbsoluteIri().toString());
36         assertEquals(2, schemaLocation.getFragment().getNameCount());
37         assertEquals("https://json-schema.org/draft/2020-12/schema#/properties/0", schemaLocation.toString());
38         assertEquals("/properties/0", schemaLocation.getFragment().toString());
39     }
40 
41     @Test
ofAbsoluteIriWithAnchor()42     void ofAbsoluteIriWithAnchor() {
43         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address#street_address");
44         assertEquals("https://example.com/schemas/address", schemaLocation.getAbsoluteIri().toString());
45         assertEquals(1, schemaLocation.getFragment().getNameCount());
46         assertEquals("https://example.com/schemas/address#street_address", schemaLocation.toString());
47         assertEquals("street_address", schemaLocation.getFragment().toString());
48     }
49 
50     @Test
ofDocument()51     void ofDocument() {
52         assertEquals(SchemaLocation.DOCUMENT, SchemaLocation.of("#"));
53     }
54 
55     @Test
document()56     void document() {
57         assertEquals(SchemaLocation.DOCUMENT.toString(), "#");
58     }
59 
60     @Test
schemaLocationResolveDocument()61     void schemaLocationResolveDocument() {
62         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address#street_address");
63         assertEquals("https://example.com/schemas/address#", SchemaLocation.resolve(schemaLocation, "#"));
64     }
65 
66     @Test
schemaLocationResolveDocumentPointer()67     void schemaLocationResolveDocumentPointer() {
68         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address#street_address");
69         assertEquals("https://example.com/schemas/address#/allOf/12/properties",
70                 SchemaLocation.resolve(schemaLocation, "#/allOf/12/properties"));
71     }
72 
73     @Test
schemaLocationResolveEmptyString()74     void schemaLocationResolveEmptyString() {
75         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address#street_address");
76         assertEquals("https://example.com/schemas/address#", SchemaLocation.resolve(schemaLocation, ""));
77     }
78 
79     @Test
schemaLocationResolveRelative()80     void schemaLocationResolveRelative() {
81         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address#street_address");
82         assertEquals("https://example.com/schemas/test#", SchemaLocation.resolve(schemaLocation, "test"));
83     }
84 
85     @Test
schemaLocationResolveRelativeIndex()86     void schemaLocationResolveRelativeIndex() {
87         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address/#street_address");
88         assertEquals("https://example.com/schemas/address/test#", SchemaLocation.resolve(schemaLocation, "test"));
89     }
90 
91     @Test
resolveDocument()92     void resolveDocument() {
93         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address#street_address");
94         assertEquals("https://example.com/schemas/address#", schemaLocation.resolve("#").toString());
95     }
96 
97     @Test
resolveDocumentPointer()98     void resolveDocumentPointer() {
99         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address#street_address");
100         assertEquals("https://example.com/schemas/address#/allOf/10/properties",
101                 schemaLocation.resolve("#/allOf/10/properties").toString());
102     }
103 
104     @Test
resolveEmptyString()105     void resolveEmptyString() {
106         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address#street_address");
107         assertEquals("https://example.com/schemas/address#", schemaLocation.resolve("").toString());
108     }
109 
110     @Test
resolveRelative()111     void resolveRelative() {
112         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address#street_address");
113         assertEquals("https://example.com/schemas/test#", schemaLocation.resolve("test").toString());
114     }
115 
116     @Test
resolveRelativeIndex()117     void resolveRelativeIndex() {
118         SchemaLocation schemaLocation = SchemaLocation.of("https://example.com/schemas/address/#street_address");
119         assertEquals("https://example.com/schemas/address/test#", schemaLocation.resolve("test").toString());
120     }
121 
122     @Test
resolveNull()123     void resolveNull() {
124         SchemaLocation schemaLocation = new SchemaLocation(null);
125         assertEquals("test#", schemaLocation.resolve("test").toString());
126     }
127 
128     @Test
build()129     void build() {
130         SchemaLocation schemaLocation = SchemaLocation.builder().absoluteIri("https://example.com/schemas/address/")
131                 .fragment("/allOf/10/properties").build();
132         assertEquals("https://example.com/schemas/address/#/allOf/10/properties", schemaLocation.toString());
133         assertEquals("https://example.com/schemas/address/", schemaLocation.getAbsoluteIri().toString());
134         assertEquals("/allOf/10/properties", schemaLocation.getFragment().toString());
135     }
136 
137     @Test
append()138     void append() {
139         SchemaLocation schemaLocation = SchemaLocation.builder().absoluteIri("https://example.com/schemas/address/")
140                 .build().append("allOf").append(10).append("properties");
141         assertEquals("https://example.com/schemas/address/#/allOf/10/properties", schemaLocation.toString());
142         assertEquals("https://example.com/schemas/address/", schemaLocation.getAbsoluteIri().toString());
143         assertEquals("/allOf/10/properties", schemaLocation.getFragment().toString());
144     }
145 
146     @Test
anchorFragment()147     void anchorFragment() {
148         assertTrue(SchemaLocation.Fragment.isAnchorFragment("#test"));
149         assertFalse(SchemaLocation.Fragment.isAnchorFragment("#"));
150         assertFalse(SchemaLocation.Fragment.isAnchorFragment("#/allOf/10/properties"));
151         assertFalse(SchemaLocation.Fragment.isAnchorFragment(""));
152     }
153 
154     @Test
jsonPointerFragment()155     void jsonPointerFragment() {
156         assertTrue(SchemaLocation.Fragment.isJsonPointerFragment("#/allOf/10/properties"));
157         assertFalse(SchemaLocation.Fragment.isJsonPointerFragment("#"));
158         assertFalse(SchemaLocation.Fragment.isJsonPointerFragment("#test"));
159     }
160 
161     @Test
fragment()162     void fragment() {
163         assertTrue(SchemaLocation.Fragment.isFragment("#/allOf/10/properties"));
164         assertTrue(SchemaLocation.Fragment.isFragment("#test"));
165         assertFalse(SchemaLocation.Fragment.isFragment("test"));
166     }
167 
168     @Test
documentFragment()169     void documentFragment() {
170         assertFalse(SchemaLocation.Fragment.isDocumentFragment("#/allOf/10/properties"));
171         assertFalse(SchemaLocation.Fragment.isDocumentFragment("#test"));
172         assertFalse(SchemaLocation.Fragment.isDocumentFragment("test"));
173         assertTrue(SchemaLocation.Fragment.isDocumentFragment("#"));
174     }
175 
176     @Test
ofNull()177     void ofNull() {
178         assertNull(SchemaLocation.of(null));
179     }
180 
181     @Test
ofEmptyString()182     void ofEmptyString() {
183         SchemaLocation schemaLocation = SchemaLocation.of("");
184         assertEquals("", schemaLocation.getAbsoluteIri().toString());
185         assertEquals("#", schemaLocation.toString());
186     }
187 
188     @Test
newNull()189     void newNull() {
190         SchemaLocation schemaLocation = new SchemaLocation(null);
191         assertEquals("#", schemaLocation.toString());
192     }
193 
194     @Test
equalsEquals()195     void equalsEquals() {
196         assertEquals(SchemaLocation.of("https://example.com/schemas/address/#street_address"),
197                 SchemaLocation.of("https://example.com/schemas/address/#street_address"));
198     }
199 
200     @Test
hashCodeEquals()201     void hashCodeEquals() {
202         assertEquals(SchemaLocation.of("https://example.com/schemas/address/#street_address").hashCode(),
203                 SchemaLocation.of("https://example.com/schemas/address/#street_address").hashCode());
204     }
205 
206 }
207