Skip to content

Commit 20d89d8

Browse files
committed
adding appropriate tests to ReferenceLookup
1 parent 2c1537c commit 20d89d8

File tree

5 files changed

+85
-34
lines changed

5 files changed

+85
-34
lines changed

core/src/main/java/org/everit/json/schema/loader/JsonPointerEvaluator.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ static final JsonPointerEvaluator forURL(SchemaClient schemaClient, String url,
142142
*/
143143
public QueryResult query() {
144144
JsonObject document = documentProvider.get();
145-
System.out.println(document);
146145
if (fragment.isEmpty()) {
147146
return new QueryResult(document, document);
148147
}
@@ -175,12 +174,10 @@ private String unescape(String token) {
175174

176175
private JsonValue queryFrom(JsonValue document, LinkedList<String> tokens) {
177176
String key = unescape(tokens.poll());
178-
System.out.println(key + " , " + document);
179177
JsonValue next = document.canBeMappedTo(JsonObject.class, obj -> obj.childFor(key))
180178
.orMappedTo(JsonArray.class, arr -> arr.at(Integer.parseInt(key)))
181179
.requireAny();
182180

183-
System.out.println("next: " + next);
184181
if (tokens.isEmpty()) {
185182
return next;
186183
}

core/src/main/java/org/everit/json/schema/loader/LoadingState.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ SchemaLoader.SchemaLoaderBuilder initChildLoader() {
8686
}
8787

8888
private Object getRawChildOfObject(JsonObject obj, String key) {
89-
if (!((Map<String, Object>) obj.unwrap()).containsKey(key)) {
89+
Map<String, Object> rawMap = (Map<String, Object>) obj.unwrap();
90+
if (!rawMap.containsKey(key)) {
9091
throw createSchemaException(format("key [%s] not found", key));
9192
}
92-
return ((Map<String, Object>) obj.unwrap()).get(key);
93+
return rawMap.get(key);
9394
}
9495

9596
private Object getRawElemOfArray(JsonArray array, String rawIndex) {

core/src/main/java/org/everit/json/schema/loader/ReferenceLookup.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ Map<String, Object> withoutRef(JsonObject original) {
9191
private JsonObject lookupObjById(JsonValue val, String idAttrVal) {
9292
if (val instanceof JsonObject) {
9393
JsonObject obj = (JsonObject) val;
94-
if (obj.containsKey("$id") && obj.require("$id").requireString().equals(idAttrVal)) {
94+
if (obj.containsKey("$id") && obj.require("$id").requireString()
95+
.equals(idAttrVal)) {
9596
return obj;
9697
}
9798
for (String key : obj.keySet()) {
@@ -117,7 +118,6 @@ private JsonObject lookupObjById(JsonValue val, String idAttrVal) {
117118
* Returns a schema builder instance after looking up the JSON pointer.
118119
*/
119120
Schema.Builder<?> lookup(String relPointerString, JsonObject ctx) {
120-
System.out.println(ls.rootSchemaJson());
121121
if (isSameDocumentRef(relPointerString)) {
122122
if (ls.pointerSchemas.containsKey(relPointerString)) {
123123
return ls.pointerSchemas.get(relPointerString);
@@ -133,7 +133,6 @@ Schema.Builder<?> lookup(String relPointerString, JsonObject ctx) {
133133
ReferenceSchema.Builder refBuilder = ReferenceSchema.builder()
134134
.refValue(relPointerString);
135135
ls.pointerSchemas.put(relPointerString, refBuilder);
136-
System.out.println(rawInternalReferenced);
137136
Schema referredSchema = new SchemaLoader(rawInternalReferenced.ls).load().build();
138137
refBuilder.build().setReferredSchema(referredSchema);
139138
return refBuilder;
@@ -142,16 +141,12 @@ Schema.Builder<?> lookup(String relPointerString, JsonObject ctx) {
142141
if (ls.pointerSchemas.containsKey(absPointerString)) {
143142
return ls.pointerSchemas.get(absPointerString);
144143
}
145-
146144
JsonValue rawInternalRefereced = lookupObjById(ls.rootSchemaJson, absPointerString);
147145
if (rawInternalRefereced != null) {
148146
ReferenceSchema.Builder refBuilder = ReferenceSchema.builder()
149147
.refValue(relPointerString);
150148
ls.pointerSchemas.put(absPointerString, refBuilder);
151-
Schema referredSchema = ls.initChildLoader()
152-
.pointerToCurrentObj(rawInternalRefereced.ls.pointerToCurrentObj)
153-
.schemaJson(rawInternalRefereced)
154-
.build().load().build();
149+
Schema referredSchema = new SchemaLoader(rawInternalRefereced.ls).load().build();
155150
refBuilder.build().setReferredSchema(referredSchema);
156151
return refBuilder;
157152
}
Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package org.everit.json.schema.loader;
22

3+
import static java.util.Collections.emptyList;
34
import static java.util.Collections.emptyMap;
5+
import static org.everit.json.schema.TestSupport.asStream;
6+
import static org.junit.Assert.assertEquals;
47
import static org.mockito.Mockito.mock;
8+
import static org.mockito.Mockito.when;
59

6-
import java.net.URI;
7-
import java.util.ArrayList;
810
import java.util.HashMap;
911
import java.util.Map;
1012

13+
import org.everit.json.schema.ReferenceSchema;
1114
import org.everit.json.schema.ResourceLoader;
1215
import org.everit.json.schema.Schema;
1316
import org.junit.Before;
14-
import org.junit.Ignore;
1517
import org.junit.Test;
1618

1719
public class ReferenceLookupTest {
1820

1921
private static Map<String, Object> rootSchemaJson;
2022

2123
{
22-
rootSchemaJson = ResourceLoader.DEFAULT.readObj("testschemas.json").getJSONObject("refPointerDerivatedFromPointer").toMap();
24+
rootSchemaJson = ResourceLoader.DEFAULT.readObj("ref-lookup-tests.json").toMap();
2325
}
2426

2527
private SchemaClient httpClient;
@@ -29,26 +31,50 @@ public void before() {
2931
httpClient = mock(SchemaClient.class);
3032
}
3133

32-
/**
33-
* Creates a LoadingState instance for which the schemaJson is an object with a single key, a $ref containing the ref param
34-
*
35-
* @param ref
36-
* @return
37-
*/
38-
private LoadingState createLoadingState(String ref) {
39-
LoaderConfig config = new LoaderConfig(httpClient, emptyMap(), SpecificationVersion.DRAFT_4);
40-
URI parentScopeId = null;
41-
Object rootSchemaJson = this.rootSchemaJson;
42-
HashMap<String, Object> schemaJson = new HashMap<>();
43-
schemaJson.put("$ref", ref);
44-
return new LoadingState(config, new HashMap<>(), rootSchemaJson, schemaJson, parentScopeId, new ArrayList<>());
34+
private Schema performLookup(String pointerToRef) {
35+
JsonObject jsonValue = query(pointerToRef).requireObject();
36+
ReferenceLookup subject = new ReferenceLookup(jsonValue.ls);
37+
String refPointer = jsonValue.require("$ref").requireString();
38+
Schema.Builder<?> actual = subject.lookup(refPointer, jsonValue);
39+
ReferenceSchema ref = (ReferenceSchema) actual.build();
40+
return ref.getReferredSchema();
4541
}
4642

47-
@Test @Ignore
43+
@Test
4844
public void sameDocumentLookup() {
49-
JsonValue jsonValue = createLoadingState("#/definitions").schemaJson;
50-
ReferenceLookup subject = new ReferenceLookup(jsonValue.ls);
51-
Schema.Builder<?> actual = subject.lookup("#/definitions", jsonValue.requireObject());
45+
Schema actual = performLookup("#/properties/sameDocPointer");
46+
assertEquals("dummy schema at #/definitions/Bar", actual.getDescription());
47+
}
48+
49+
private JsonValue query(String pointer) {
50+
LoadingState rootLs = new LoadingState(new LoaderConfig(httpClient, emptyMap(), SpecificationVersion.DRAFT_6),
51+
new HashMap<>(),
52+
rootSchemaJson,
53+
rootSchemaJson,
54+
null,
55+
emptyList()
56+
);
57+
return JsonPointerEvaluator.forDocument(rootLs.rootSchemaJson(), pointer).query().getQueryResult();
58+
}
59+
60+
@Test
61+
public void sameDocumentLookupById() {
62+
Schema actual = performLookup("#/properties/lookupByDocLocalIdent");
63+
assertEquals("it has document-local identifier", actual.getDescription());
64+
}
65+
66+
@Test
67+
public void absoluteRef() {
68+
when(httpClient.get("http://localhost/schema.json")).thenReturn(asStream("{\"description\":\"ok\"}"));
69+
Schema actual = performLookup("#/properties/absoluteRef");
70+
assertEquals("ok", actual.getDescription());
71+
}
72+
73+
@Test
74+
public void withParentScope() {
75+
when(httpClient.get("http://localhost/child-ref")).thenReturn(asStream("{\"description\":\"ok\"}"));
76+
Schema actual = performLookup("#/properties/parent/child");
77+
assertEquals("ok", actual.getDescription());
5278
}
5379

5480
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"properties": {
3+
"sameDocPointer": {
4+
"$ref": "#/definitions/Bar"
5+
},
6+
"lookupByDocLocalIdent": {
7+
"$ref": "has-id"
8+
},
9+
"absoluteRef": {
10+
"$ref": "http://localhost/schema.json"
11+
},
12+
"parent": {
13+
"$id": "http://localhost/",
14+
"child": {
15+
"$ref": "child-ref"
16+
}
17+
}
18+
},
19+
"definitions": {
20+
"$id": "folder/",
21+
"Foo": {
22+
"$ref": "folderInteger.json"
23+
},
24+
"Bar": {
25+
"description": "dummy schema at #/definitions/Bar"
26+
},
27+
"HasId": {
28+
"$id": "has-id",
29+
"description": "it has document-local identifier"
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)