From bc9b0f180578b3c41eec23ac55173dd44396f436 Mon Sep 17 00:00:00 2001
From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com>
Date: Thu, 6 Jun 2024 20:17:08 +0200
Subject: [PATCH] refactor test for all keyword defined in corevocab
---
.../core/vocab/core/VocabularyKeyword.java | 86 +++++++++++++++++++
.../vocab/core/VocabularyKeywordType.java | 63 +-------------
.../core/vocab/core/CommentKeywordTest.java | 22 +----
.../core/vocab/core/DefsKeywordTest.java | 21 +----
.../vocab/core/DynamicRefKeywordTest.java | 35 +++-----
.../core/vocab/core/IdKeywordTest.java | 27 +-----
.../core/vocab/core/RefKeywordTest.java | 81 ++++++++---------
.../core/vocab/core/RefKeywordTypeTest.java | 28 ++++++
.../core/vocab/core/SchemaKeywordTest.java | 31 +------
.../vocab/core/VocabularyKeywordTypeTest.java | 50 +++--------
.../vocabulary/spi/VocabularyDefinitions.java | 3 +-
11 files changed, 188 insertions(+), 259 deletions(-)
create mode 100644 core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeyword.java
diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeyword.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeyword.java
new file mode 100644
index 00000000..7dbb20ce
--- /dev/null
+++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeyword.java
@@ -0,0 +1,86 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2024 sebastian.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package io.github.sebastiantoepfer.jsonschema.core.vocab.core;
+
+import io.github.sebastiantoepfer.ddd.common.Media;
+import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable;
+import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition;
+import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinitions;
+import jakarta.json.JsonObject;
+import jakarta.json.JsonValue;
+import java.net.URI;
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+
+/**
+ * $vocabulary : Object<URI, Boolean>
+ * This keyword is used in meta-schemas to identify the required and optional vocabularies available for use in
+ * schemas described by that meta-schema.
+ *
+ *
+ *
+ * source: https://www.learnjsonschema.com/2020-12/core/vocabulary/
+ * spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-8.1.2
+ */
+final class VocabularyKeyword implements VocabularyDefinitions {
+
+ static final String NAME = "$vocabulary";
+ private final JsonObject vocabularies;
+
+ VocabularyKeyword(final JsonValue vocabularies) {
+ this(vocabularies.asJsonObject());
+ }
+
+ VocabularyKeyword(final JsonObject vocabularies) {
+ this.vocabularies = Objects.requireNonNull(vocabularies);
+ }
+
+ @Override
+ public > T printOn(final T media) {
+ return media.withValue(NAME, new JsonObjectPrintable(vocabularies));
+ }
+
+ @Override
+ public boolean hasName(final String name) {
+ return Objects.equals(NAME, name);
+ }
+
+ @Override
+ public Collection categories() {
+ //is a identifier after spec ... but how to implement it as it?
+ return List.of();
+ }
+
+ @Override
+ public Stream definitions() {
+ return vocabularies
+ .entrySet()
+ .stream()
+ .map(entry -> new VocabularyDefinition(URI.create(entry.getKey()), entry.getValue() == JsonValue.TRUE));
+ }
+}
diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordType.java
index 1bbac9ae..e099babb 100644
--- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordType.java
+++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordType.java
@@ -23,21 +23,11 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;
-import io.github.sebastiantoepfer.ddd.common.Media;
-import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
-import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
-import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinitions;
-import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
-import java.net.URI;
-import java.util.Collection;
-import java.util.List;
-import java.util.Objects;
-import java.util.stream.Stream;
/**
* see: https://json-schema.org/draft/2020-12/json-schema-core.html#name-the-vocabulary-keyword
@@ -46,11 +36,11 @@ public final class VocabularyKeywordType implements KeywordType {
@Override
public String name() {
- return "$vocabulary";
+ return VocabularyKeyword.NAME;
}
@Override
- public VocabularyKeyword createKeyword(final JsonSchema schema) {
+ public VocabularyDefinitions createKeyword(final JsonSchema schema) {
final JsonValue value = schema.asJsonObject().get((name()));
final VocabularyKeyword result;
if (InstanceType.OBJECT.isInstance(value)) {
@@ -64,53 +54,4 @@ public VocabularyKeyword createKeyword(final JsonSchema schema) {
}
return result;
}
-
- /**
- * $vocabulary : Object<URI, Boolean>
- * This keyword is used in meta-schemas to identify the required and optional vocabularies available for use in
- * schemas described by that meta-schema.
- *
- *
- *
- * source: https://www.learnjsonschema.com/2020-12/core/vocabulary/
- * spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-8.1.2
- */
- public final class VocabularyKeyword implements Keyword, VocabularyDefinitions {
-
- private final JsonObject vocabularies;
-
- VocabularyKeyword(final JsonValue vocabularies) {
- this(vocabularies.asJsonObject());
- }
-
- VocabularyKeyword(final JsonObject vocabularies) {
- this.vocabularies = Objects.requireNonNull(vocabularies);
- }
-
- @Override
- public > T printOn(final T media) {
- return media.withValue(name(), new JsonObjectPrintable(vocabularies));
- }
-
- @Override
- public boolean hasName(final String name) {
- return Objects.equals(name(), name);
- }
-
- @Override
- public Collection categories() {
- //is a identifier after spec ... but how to implement it as it?
- return List.of();
- }
-
- @Override
- public Stream definitions() {
- return vocabularies
- .entrySet()
- .stream()
- .map(entry -> new VocabularyDefinition(URI.create(entry.getKey()), entry.getValue() == JsonValue.TRUE));
- }
- }
}
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CommentKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CommentKeywordTest.java
index a853cda2..625e81cf 100644
--- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CommentKeywordTest.java
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CommentKeywordTest.java
@@ -28,21 +28,14 @@
import static org.hamcrest.Matchers.is;
import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
-import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
-import io.github.sebastiantoepfer.jsonschema.core.keyword.type.StringKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
-import jakarta.json.Json;
-import jakarta.json.JsonObject;
-import jakarta.json.spi.JsonProvider;
import org.junit.jupiter.api.Test;
class CommentKeywordTest {
@Test
void should_know_her_name() {
- final Keyword keyword = createKeywordFrom(
- Json.createObjectBuilder().add("$comment", Json.createValue("comment")).build()
- );
+ final Keyword keyword = new CommentKeyword("comment");
assertThat(keyword.hasName("$comment"), is(true));
assertThat(keyword.hasName("test"), is(false));
@@ -50,17 +43,6 @@ void should_know_her_name() {
@Test
void should_be_printable() {
- assertThat(
- createKeywordFrom(Json.createObjectBuilder().add("$comment", Json.createValue("comment")).build()).printOn(
- new HashMapMedia()
- ),
- hasEntry("$comment", "comment")
- );
- }
-
- private static Keyword createKeywordFrom(final JsonObject json) {
- return new StringKeywordType(JsonProvider.provider(), "$comment", CommentKeyword::new).createKeyword(
- new DefaultJsonSchemaFactory().create(json)
- );
+ assertThat(new CommentKeyword("comment").printOn(new HashMapMedia()), hasEntry("$comment", "comment"));
}
}
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTest.java
index 4dd22189..878f9d38 100644
--- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTest.java
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTest.java
@@ -29,12 +29,9 @@
import static org.hamcrest.Matchers.is;
import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
-import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
-import io.github.sebastiantoepfer.jsonschema.core.keyword.type.NamedJsonSchemaKeywordType;
+import io.github.sebastiantoepfer.jsonschema.core.keyword.type.NamedJsonSchemas;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
-import jakarta.json.Json;
-import jakarta.json.JsonObject;
-import jakarta.json.JsonValue;
+import java.util.Map;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
@@ -42,9 +39,7 @@ class DefsKeywordTest {
@Test
void should_know_his_name() {
- final Keyword defs = createKeywordFrom(
- Json.createObjectBuilder().add("$defs", JsonValue.EMPTY_JSON_OBJECT).build()
- );
+ final Keyword defs = new DefsKeyword(new NamedJsonSchemas(Map.of()));
assertThat(defs.hasName("$defs"), is(true));
assertThat(defs.hasName("test"), is(false));
@@ -53,16 +48,8 @@ void should_know_his_name() {
@Test
void should_be_printable() {
assertThat(
- createKeywordFrom(Json.createObjectBuilder().add("$defs", JsonValue.EMPTY_JSON_OBJECT).build()).printOn(
- new HashMapMedia()
- ),
+ new DefsKeyword(new NamedJsonSchemas(Map.of())).printOn(new HashMapMedia()),
(Matcher) hasEntry(is("$defs"), anEmptyMap())
);
}
-
- private static Keyword createKeywordFrom(final JsonObject json) {
- return new NamedJsonSchemaKeywordType(DefsKeyword.NAME, DefsKeyword::new).createKeyword(
- new DefaultJsonSchemaFactory().create(json)
- );
- }
}
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTest.java
index d909a1a9..5c708491 100644
--- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTest.java
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTest.java
@@ -28,13 +28,8 @@
import static org.hamcrest.Matchers.is;
import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
-import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
-import io.github.sebastiantoepfer.jsonschema.core.keyword.type.StringKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
-import jakarta.json.Json;
-import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
-import jakarta.json.spi.JsonProvider;
import java.net.URI;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
@@ -43,37 +38,27 @@ class DynamicRefKeywordTest {
@Test
void should_create_keyword_with_name() {
- assertThat(
- createKeywordFrom(Json.createObjectBuilder().add("$dynamicRef", "test").build()).hasName("$dynamicRef"),
- is(true)
- );
- }
-
- @Test
- void notFinischedYet() {
- final Keyword keyword = createKeywordFrom(Json.createObjectBuilder().add("$dynamicRef", "test").build());
+ final Keyword keyword = new DynamicRefKeyword(URI.create("test"));
assertThat(keyword.hasName("$dynamicRef"), is(true));
assertThat(keyword.hasName("$id"), is(false));
-
- assertThat(keyword.asApplicator().applyTo(JsonValue.TRUE), is(true));
}
@Test
void should_be_printable() {
assertThat(
- createKeywordFrom(Json.createObjectBuilder().add("$dynamicRef", "test").build()).printOn(
- new HashMapMedia()
- ),
+ new DynamicRefKeyword(URI.create("test")).printOn(new HashMapMedia()),
(Matcher) hasEntry(is("$dynamicRef"), is("test"))
);
}
- private static Keyword createKeywordFrom(final JsonObject json) {
- return new StringKeywordType(
- JsonProvider.provider(),
- DynamicRefKeyword.NAME,
- s -> new DynamicRefKeyword(URI.create(s))
- ).createKeyword(new DefaultJsonSchemaFactory().create(json));
+ @Test
+ void notFinischedYet() {
+ final Keyword keyword = new DynamicRefKeyword(URI.create("test"));
+
+ assertThat(keyword.hasName("$dynamicRef"), is(true));
+ assertThat(keyword.hasName("$id"), is(false));
+
+ assertThat(keyword.asApplicator().applyTo(JsonValue.TRUE), is(true));
}
}
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTest.java
index 0e23f507..3d9cb8fc 100644
--- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTest.java
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTest.java
@@ -28,12 +28,7 @@
import static org.hamcrest.Matchers.is;
import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
-import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
-import io.github.sebastiantoepfer.jsonschema.core.keyword.type.StringKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
-import jakarta.json.Json;
-import jakarta.json.JsonObject;
-import jakarta.json.spi.JsonProvider;
import java.net.URI;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
@@ -42,7 +37,7 @@ class IdKeywordTest {
@Test
void should_know_his_name() {
- final Keyword id = createKeywordFrom(Json.createObjectBuilder().add("$id", Json.createValue("/test")).build());
+ final Keyword id = new IdKeyword(URI.create("/test"));
assertThat(id.hasName("$id"), is(true));
assertThat(id.hasName("test"), is(false));
@@ -51,13 +46,7 @@ void should_know_his_name() {
@Test
void should_retun_his_uri() {
assertThat(
- createKeywordFrom(
- Json.createObjectBuilder()
- .add("$id", Json.createValue("https://json-schema.org/draft/2020-12/schema"))
- .build()
- )
- .asIdentifier()
- .asUri(),
+ new IdKeyword(URI.create("https://json-schema.org/draft/2020-12/schema")).asIdentifier().asUri(),
is(URI.create("https://json-schema.org/draft/2020-12/schema"))
);
}
@@ -65,18 +54,8 @@ void should_retun_his_uri() {
@Test
void should_be_printable() {
assertThat(
- createKeywordFrom(
- Json.createObjectBuilder()
- .add("$id", Json.createValue("https://json-schema.org/draft/2020-12/schema"))
- .build()
- ).printOn(new HashMapMedia()),
+ new IdKeyword(URI.create("https://json-schema.org/draft/2020-12/schema")).printOn(new HashMapMedia()),
(Matcher) hasEntry(is("$id"), is("https://json-schema.org/draft/2020-12/schema"))
);
}
-
- private static Keyword createKeywordFrom(final JsonObject json) {
- return new StringKeywordType(JsonProvider.provider(), "$id", s -> new IdKeyword(URI.create(s))).createKeyword(
- new DefaultJsonSchemaFactory().create(json)
- );
- }
}
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTest.java
index 489e2f0c..a1b5be96 100644
--- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTest.java
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTest.java
@@ -26,52 +26,41 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
-import static org.junit.jupiter.api.Assertions.assertThrows;
import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
-import io.github.sebastiantoepfer.jsonschema.JsonSchema;
-import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
+import io.github.sebastiantoepfer.jsonschema.JsonSchemas;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.Json;
-import jakarta.json.JsonValue;
-import jakarta.json.spi.JsonProvider;
+import java.net.URI;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
class RefKeywordTest {
- @Test
- void should_be_not_createable_from_non_string() {
- final RefKeywordType keywordType = new RefKeywordType(JsonProvider.provider());
- final JsonSchema schema = new DefaultJsonSchemaFactory()
- .create(Json.createObjectBuilder().add("$ref", JsonValue.TRUE).build());
- assertThrows(IllegalArgumentException.class, () -> keywordType.createKeyword(schema));
- }
-
@Test
void should_know_his_name() {
- final Keyword ref = new RefKeywordType(JsonProvider.provider()).createKeyword(
- new DefaultJsonSchemaFactory().create(Json.createObjectBuilder().add("$ref", Json.createValue("#")).build())
+ final Keyword ref = new RefKeyword(
+ JsonSchemas.load(Json.createObjectBuilder().add("$ref", Json.createValue("#")).build()),
+ URI.create("#")
);
-
assertThat(ref.hasName("$ref"), is(true));
assertThat(ref.hasName("test"), is(false));
}
@Test
void should_use_local_referenced_schema_for_validation() {
- final Keyword keyword = new RefKeywordType(JsonProvider.provider()).createKeyword(
- new DefaultJsonSchemaFactory()
- .create(
- Json.createObjectBuilder()
- .add(
- "$defs",
- Json.createObjectBuilder()
- .add("positiveInteger", Json.createObjectBuilder().add("type", "integer"))
- )
- .add("$ref", Json.createValue("#/$defs/positiveInteger"))
- .build()
- )
+ final Keyword keyword = new RefKeyword(
+ JsonSchemas.load(
+ Json.createObjectBuilder()
+ .add(
+ "$defs",
+ Json.createObjectBuilder()
+ .add("positiveInteger", Json.createObjectBuilder().add("type", "integer"))
+ )
+ .add("$ref", Json.createValue("#/$defs/positiveInteger"))
+ .build()
+ ),
+ URI.create("#/$defs/positiveInteger")
);
assertThat(keyword.asApplicator().applyTo(Json.createValue(1L)), is(true));
@@ -80,32 +69,30 @@ void should_use_local_referenced_schema_for_validation() {
@Test
void should_use_remote_referenced_schema_for_validation() {
- final Keyword keyword = new RefKeywordType(JsonProvider.provider()).createKeyword(
- new DefaultJsonSchemaFactory()
- .create(
- Json.createObjectBuilder()
- .add(
- "$defs",
- Json.createObjectBuilder()
- .add("positiveInteger", Json.createObjectBuilder().add("type", "integer"))
- )
- .add("$ref", Json.createValue("#/$defs/positiveInteger"))
- .build()
- )
+ final Keyword keyword = new RefKeyword(
+ JsonSchemas.load(
+ Json.createObjectBuilder()
+ .add(
+ "$defs",
+ Json.createObjectBuilder()
+ .add("positiveInteger", Json.createObjectBuilder().add("type", "integer"))
+ )
+ .add("$ref", Json.createValue("#/$defs/positiveInteger"))
+ .build()
+ ),
+ URI.create("#/$defs/positiveInteger")
);
-
assertThat(keyword.asApplicator().applyTo(Json.createValue(1L)), is(true));
+ assertThat(keyword.asApplicator().applyTo(Json.createValue("invalid")), is(false));
}
@Test
void should_be_printable() {
assertThat(
- new RefKeywordType(JsonProvider.provider())
- .createKeyword(
- new DefaultJsonSchemaFactory()
- .create(Json.createObjectBuilder().add("$ref", Json.createValue("#")).build())
- )
- .printOn(new HashMapMedia()),
+ new RefKeyword(
+ JsonSchemas.load(Json.createObjectBuilder().add("$ref", Json.createValue("#")).build()),
+ URI.create("#")
+ ).printOn(new HashMapMedia()),
(Matcher) hasEntry(is("$ref"), is("#"))
);
}
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTypeTest.java
index ab7ed341..5750e79d 100644
--- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTypeTest.java
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/RefKeywordTypeTest.java
@@ -25,8 +25,16 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import io.github.sebastiantoepfer.jsonschema.JsonSchema;
+import io.github.sebastiantoepfer.jsonschema.JsonSchemas;
+import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
+import jakarta.json.Json;
+import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import org.junit.jupiter.api.Test;
@@ -40,4 +48,24 @@ void should_know_the_keywords_name() {
assertThat(refKeywordType.hasName("ref"), is(false));
assertThat(refKeywordType.hasName("test"), is(false));
}
+
+ @Test
+ void should_be_not_createable_from_non_string() {
+ final RefKeywordType keywordType = new RefKeywordType(JsonProvider.provider());
+ final JsonSchema schema = new DefaultJsonSchemaFactory()
+ .create(Json.createObjectBuilder().add("$ref", JsonValue.TRUE).build());
+ assertThrows(IllegalArgumentException.class, () -> keywordType.createKeyword(schema));
+ }
+
+ @Test
+ void should_create_refkeyword() {
+ assertThat(
+ new RefKeywordType(JsonProvider.provider()).createKeyword(
+ JsonSchemas.load(
+ Json.createObjectBuilder().add("$ref", Json.createValue("#/$defs/positiveInteger")).build()
+ )
+ ),
+ is(not(nullValue()))
+ );
+ }
}
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTest.java
index e2ff6c7f..cc3eb4b5 100644
--- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTest.java
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTest.java
@@ -28,12 +28,7 @@
import static org.hamcrest.Matchers.is;
import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
-import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
-import io.github.sebastiantoepfer.jsonschema.core.keyword.type.StringKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
-import jakarta.json.Json;
-import jakarta.json.JsonObject;
-import jakarta.json.spi.JsonProvider;
import java.net.URI;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
@@ -42,9 +37,7 @@ class SchemaKeywordTest {
@Test
void should_know_his_name() {
- final Keyword schema = createKeywordFrom(
- Json.createObjectBuilder().add("$schema", Json.createValue("/test")).build()
- );
+ final Keyword schema = new SchemaKeyword(URI.create("/test"));
assertThat(schema.hasName("$schema"), is(true));
assertThat(schema.hasName("test"), is(false));
@@ -53,13 +46,7 @@ void should_know_his_name() {
@Test
void should_retun_his_uri() {
assertThat(
- createKeywordFrom(
- Json.createObjectBuilder()
- .add("$schema", Json.createValue("https://json-schema.org/draft/2020-12/schema"))
- .build()
- )
- .asIdentifier()
- .asUri(),
+ new SchemaKeyword(URI.create("https://json-schema.org/draft/2020-12/schema")).asIdentifier().asUri(),
is(URI.create("https://json-schema.org/draft/2020-12/schema"))
);
}
@@ -67,20 +54,8 @@ void should_retun_his_uri() {
@Test
void should_be_printable() {
assertThat(
- createKeywordFrom(
- Json.createObjectBuilder()
- .add("$schema", Json.createValue("https://json-schema.org/draft/2020-12/schema"))
- .build()
- ).printOn(new HashMapMedia()),
+ new SchemaKeyword(URI.create("https://json-schema.org/draft/2020-12/schema")).printOn(new HashMapMedia()),
(Matcher) hasEntry(is("$schema"), is("https://json-schema.org/draft/2020-12/schema"))
);
}
-
- private static Keyword createKeywordFrom(final JsonObject json) {
- return new StringKeywordType(
- JsonProvider.provider(),
- "$schema",
- s -> new SchemaKeyword(URI.create(s))
- ).createKeyword(new DefaultJsonSchemaFactory().create(json));
- }
}
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordTypeTest.java
index 3ee97e25..5e7dbac9 100644
--- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordTypeTest.java
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/VocabularyKeywordTypeTest.java
@@ -30,10 +30,8 @@
import static org.hamcrest.Matchers.is;
import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
-import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition;
-import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinitions;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import java.net.URI;
@@ -44,11 +42,7 @@ class VocabularyKeywordTypeTest {
@Test
void should_created_keyword_should_know_his_name() {
- final Keyword vocabulary = new VocabularyKeywordType()
- .createKeyword(
- new DefaultJsonSchemaFactory()
- .create(Json.createObjectBuilder().add("$vocabulary", JsonValue.EMPTY_JSON_OBJECT).build())
- );
+ final Keyword vocabulary = new VocabularyKeyword(JsonValue.EMPTY_JSON_OBJECT);
assertThat(vocabulary.hasName("$vocabulary"), is(true));
assertThat(vocabulary.hasName("$id"), is(false));
@@ -57,20 +51,13 @@ void should_created_keyword_should_know_his_name() {
@Test
void should_create_definitions() {
assertThat(
- ((VocabularyDefinitions) new VocabularyKeywordType()
- .createKeyword(
- new DefaultJsonSchemaFactory()
- .create(
- Json.createObjectBuilder()
- .add(
- "$vocabulary",
- Json.createObjectBuilder()
- .add("https://json-schema.org/draft/2020-12/vocab/core", true)
- .add("http://openapi.org/test", false)
- )
- .build()
- )
- )).definitions()
+ new VocabularyKeyword(
+ Json.createObjectBuilder()
+ .add("https://json-schema.org/draft/2020-12/vocab/core", true)
+ .add("http://openapi.org/test", false)
+ .build()
+ )
+ .definitions()
.toList(),
containsInAnyOrder(
new VocabularyDefinition(URI.create("https://json-schema.org/draft/2020-12/vocab/core"), true),
@@ -82,21 +69,12 @@ void should_create_definitions() {
@Test
void should_be_printable() {
assertThat(
- new VocabularyKeywordType()
- .createKeyword(
- new DefaultJsonSchemaFactory()
- .create(
- Json.createObjectBuilder()
- .add(
- "$vocabulary",
- Json.createObjectBuilder()
- .add("https://json-schema.org/draft/2020-12/vocab/core", true)
- .add("http://openapi.org/test", false)
- )
- .build()
- )
- )
- .printOn(new HashMapMedia()),
+ new VocabularyKeyword(
+ Json.createObjectBuilder()
+ .add("https://json-schema.org/draft/2020-12/vocab/core", true)
+ .add("http://openapi.org/test", false)
+ .build()
+ ).printOn(new HashMapMedia()),
(Matcher) hasEntry(
is("$vocabulary"),
allOf(
diff --git a/vocabulary-spi/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/VocabularyDefinitions.java b/vocabulary-spi/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/VocabularyDefinitions.java
index fe9dd85f..d9822afd 100644
--- a/vocabulary-spi/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/VocabularyDefinitions.java
+++ b/vocabulary-spi/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/VocabularyDefinitions.java
@@ -23,8 +23,9 @@
*/
package io.github.sebastiantoepfer.jsonschema.vocabulary.spi;
+import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import java.util.stream.Stream;
-public interface VocabularyDefinitions {
+public interface VocabularyDefinitions extends Keyword {
Stream definitions();
}