Skip to content

Commit b2de284

Browse files
committed
Add Schema-based assertion operations
1 parent dc25d09 commit b2de284

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.relogiclabs.json.schema;
2+
3+
import com.relogiclabs.json.schema.message.MessageFormatter;
4+
import com.relogiclabs.json.schema.tree.JsonTree;
5+
import com.relogiclabs.json.schema.tree.RuntimeContext;
6+
import com.relogiclabs.json.schema.tree.SchemaTree;
7+
import com.relogiclabs.json.schema.util.DebugUtils;
8+
import lombok.Getter;
9+
10+
/**
11+
* The class provides assertion functionalities to validate JSON documents against
12+
* a specific JSON Schema.
13+
*/
14+
@Getter
15+
public class JsonAssert {
16+
private final RuntimeContext runtime;
17+
private final SchemaTree schemaTree;
18+
19+
/**
20+
* Initializes a new instance of the {@link JsonAssert} class for the
21+
* specified Schema string.
22+
* @param schema A Schema string for validation or conformation
23+
*/
24+
public JsonAssert(String schema) {
25+
runtime = new RuntimeContext(MessageFormatter.SCHEMA_ASSERTION, true);
26+
schemaTree = new SchemaTree(runtime, schema);
27+
}
28+
29+
public void isValid(String jsonActual) {
30+
runtime.getExceptions().clear();
31+
var jsonTree = new JsonTree(runtime, jsonActual);
32+
DebugUtils.print(schemaTree, jsonTree);
33+
schemaTree.getRoot().match(jsonTree.getRoot());
34+
if(!schemaTree.getRoot().match(jsonTree.getRoot()))
35+
throw new IllegalStateException("Exception not thrown");
36+
}
37+
38+
/**
39+
* Tests whether the specified JSON string conforms to the given Schema string
40+
* and throws an exception if the JSON string does not conform to the Schema.
41+
* @param schemaExpected The expected Schema to conform or validate
42+
* @param jsonActual The actual JSON to conform or validate
43+
*/
44+
public static void isValid(String schemaExpected, String jsonActual) {
45+
var runtime = new RuntimeContext(MessageFormatter.SCHEMA_ASSERTION, true);
46+
var schemaTree = new SchemaTree(runtime, schemaExpected);
47+
var jsonTree = new JsonTree(runtime, jsonActual);
48+
DebugUtils.print(schemaTree, jsonTree);
49+
if(!schemaTree.getRoot().match(jsonTree.getRoot()))
50+
throw new IllegalStateException("Exception not thrown");
51+
}
52+
53+
/**
54+
* Tests if the provided JSON strings are logically equivalent, meaning their structural
55+
* composition and internal data are identical. If the JSON strings are not equivalent,
56+
* an exception is thrown.
57+
* @param jsonExpected The expected JSON to compare
58+
* @param jsonActual The actual JSON to compare
59+
*/
60+
public static void areEqual(String jsonExpected, String jsonActual) {
61+
var runtime = new RuntimeContext(MessageFormatter.JSON_ASSERTION, true);
62+
var expectedTree = new JsonTree(runtime, jsonExpected);
63+
var actualTree = new JsonTree(runtime, jsonActual);
64+
DebugUtils.print(expectedTree, actualTree);
65+
if(!expectedTree.getRoot().match(actualTree.getRoot()))
66+
throw new IllegalStateException("Exception not thrown");
67+
}
68+
}

0 commit comments

Comments
 (0)