Skip to content

Commit 8eaae67

Browse files
committed
tests(schema): add unit tests for OpenRPC-like fragments to build confidence before IT; INFO prologues per test (#29)
1 parent fb7a7bc commit 8eaae67

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.github.simbo1905.json.schema;
2+
3+
import jdk.sandbox.java.util.json.Json;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static io.github.simbo1905.json.schema.SchemaLogging.LOG;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
/// Unit tests that exercise OpenRPC-like schema fragments using only
10+
/// keywords currently supported by the validator. These build confidence
11+
/// incrementally before the larger IT that validates full documents.
12+
class OpenRPCFragmentsUnitTest extends JsonSchemaLoggingConfig {
13+
14+
@Test
15+
void info_object_minimal_required_fields() {
16+
LOG.info(() -> "TEST: " + getClass().getSimpleName() + "#info_object_minimal_required_fields");
17+
final var schema = JsonSchema.compile(Json.parse(
18+
"{" +
19+
"\"type\":\"object\"," +
20+
"\"required\":[\"title\",\"version\"]," +
21+
"\"properties\":{\"title\":{\"type\":\"string\"},\"version\":{\"type\":\"string\"}}," +
22+
"\"additionalProperties\":true" +
23+
"}"
24+
));
25+
26+
final var good = schema.validate(Json.parse("{\"title\":\"X\",\"version\":\"1.0\"}"));
27+
assertThat(good.valid()).isTrue();
28+
29+
final var bad = schema.validate(Json.parse("{\"title\":\"X\"}"));
30+
assertThat(bad.valid()).isFalse();
31+
}
32+
33+
@Test
34+
void method_object_requires_name_and_params() {
35+
LOG.info(() -> "TEST: " + getClass().getSimpleName() + "#method_object_requires_name_and_params");
36+
final var schema = JsonSchema.compile(Json.parse("""
37+
{
38+
"type":"object",
39+
"required":["name","params"],
40+
"properties":{
41+
"name":{"type":"string","minLength":1},
42+
"params":{"type":"array"}
43+
},
44+
"additionalProperties": true
45+
}
46+
"""));
47+
48+
final var ok = schema.validate(Json.parse("{\"name\":\"op\",\"params\":[]}"));
49+
assertThat(ok.valid()).isTrue();
50+
51+
final var missing = schema.validate(Json.parse("{\"name\":\"op\"}"));
52+
assertThat(missing.valid()).isFalse();
53+
}
54+
55+
@Test
56+
void param_object_requires_name_and_allows_schema_object() {
57+
LOG.info(() -> "TEST: " + getClass().getSimpleName() + "#param_object_requires_name_and_allows_schema_object");
58+
final var schema = JsonSchema.compile(Json.parse(
59+
"{" +
60+
"\"type\":\"object\"," +
61+
"\"required\":[\"name\"]," +
62+
"\"properties\":{\"name\":{\"type\":\"string\",\"minLength\":1},\"schema\":{\"type\":\"object\"}}," +
63+
"\"additionalProperties\":true" +
64+
"}"
65+
));
66+
67+
final var ok = schema.validate(Json.parse("{\"name\":\"n\",\"schema\":{}}"));
68+
assertThat(ok.valid()).isTrue();
69+
70+
final var bad = schema.validate(Json.parse("{\"schema\":{}}"));
71+
assertThat(bad.valid()).isFalse();
72+
}
73+
74+
@Test
75+
void uri_format_example_as_used_by_openrpc_examples() {
76+
LOG.info(() -> "TEST: " + getClass().getSimpleName() + "#uri_format_example_as_used_by_openrpc_examples");
77+
final var schema = JsonSchema.compile(Json.parse("{\"type\":\"string\",\"format\":\"uri\"}"));
78+
79+
assertThat(schema.validate(Json.parse("\"https://open-rpc.org\""))).extracting("valid").isEqualTo(true);
80+
}
81+
}

0 commit comments

Comments
 (0)