From 3a7bc3618a16e9c5cc4a45335ae29a45f0a4a616 Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Mon, 25 Sep 2023 04:54:25 +0200 Subject: [PATCH] create defaultvocabulary --- .../jsonschema/core/Keywords.java | 4 +- ...ocabulary.java => CoreLazyVocabulary.java} | 21 ++++- core/src/main/java/module-info.java | 2 +- ...jsonschema.vocabulary.spi.LazyVocabularies | 2 +- .../jsonschema/core/KeywordsTest.java | 4 +- ...yTest.java => CommentKeywordTypeTest.java} | 26 ++---- ...yTest.java => CoreLazyVocabularyTest.java} | 6 +- .../core/vocab/core/DefsKeywordTypeTest.java | 38 +++++++++ .../vocab/core/DynamicRefKeywordTypeTest.java | 9 +- .../core/vocab/core/IdKeywordTypeTest.java | 38 +++++++++ .../core/vocab/core/RefKeywordTypeTest.java | 6 +- .../vocab/core/SchemaKeywordTypeTest.java | 38 +++++++++ vocabulary-spi/pom.xml | 6 ++ .../vocabulary/spi/DefaultVocabulary.java | 34 ++++---- .../vocabulary/spi/DefaultVocabularyTest.java | 83 +++++++++++++++++++ 15 files changed, 265 insertions(+), 52 deletions(-) rename core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/{LazyCoreVocabulary.java => CoreLazyVocabulary.java} (72%) rename core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/{CoreVocabularyTest.java => CommentKeywordTypeTest.java} (55%) rename core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/{LazyCoreVocabularyTest.java => CoreLazyVocabularyTest.java} (93%) create mode 100644 core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java create mode 100644 core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java create mode 100644 core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTypeTest.java rename core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreVocabulary.java => vocabulary-spi/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/DefaultVocabulary.java (65%) create mode 100644 vocabulary-spi/src/test/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/DefaultVocabularyTest.java diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/Keywords.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/Keywords.java index eb8a6d2d..91f2c14d 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/Keywords.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/Keywords.java @@ -28,7 +28,7 @@ import io.github.sebastiantoepfer.jsonschema.Vocabulary; import io.github.sebastiantoepfer.jsonschema.core.vocab.basic.BasicVocabulary; -import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreVocabulary; +import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreLazyVocabulary; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition; import jakarta.json.JsonValue; @@ -49,7 +49,7 @@ final class Keywords { static { MANDANTORY_VOCABS = List - .of(new BasicVocabulary(), new CoreVocabulary()) + .of(new BasicVocabulary(), new CoreLazyVocabulary().vocab()) .stream() .collect(toMap(Vocabulary::id, Function.identity())); } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/LazyCoreVocabulary.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreLazyVocabulary.java similarity index 72% rename from core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/LazyCoreVocabulary.java rename to core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreLazyVocabulary.java index 82edca2e..9e0ea27c 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/LazyCoreVocabulary.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreLazyVocabulary.java @@ -24,17 +24,32 @@ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; import io.github.sebastiantoepfer.jsonschema.Vocabulary; +import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.DefaultVocabulary; import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.LazyVocabularies; import java.net.URI; import java.util.Objects; import java.util.Optional; -public final class LazyCoreVocabulary implements LazyVocabularies { +public final class CoreLazyVocabulary implements LazyVocabularies { private final Vocabulary vocab; - public LazyCoreVocabulary() { - this.vocab = new CoreVocabulary(); + public CoreLazyVocabulary() { + this.vocab = + new DefaultVocabulary( + URI.create("https://json-schema.org/draft/2020-12/vocab/core"), + new SchemaKeywordType(), + new VocabularyKeywordType(), + new IdKeywordType(), + new RefKeywordType(), + new DynamicRefKeywordType(), + new DefsKeywordType(), + new CommentKeywordType() + ); + } + + public Vocabulary vocab() { + return vocab; } @Override diff --git a/core/src/main/java/module-info.java b/core/src/main/java/module-info.java index 1bcda2e5..8b5dbe51 100644 --- a/core/src/main/java/module-info.java +++ b/core/src/main/java/module-info.java @@ -30,5 +30,5 @@ with io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; provides io.github.sebastiantoepfer.jsonschema.vocabulary.spi.LazyVocabularies - with io.github.sebastiantoepfer.jsonschema.core.vocab.core.LazyCoreVocabulary; + with io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreLazyVocabulary; } diff --git a/core/src/main/resources/META-INF/services/io.github.sebastiantoepfer.jsonschema.vocabulary.spi.LazyVocabularies b/core/src/main/resources/META-INF/services/io.github.sebastiantoepfer.jsonschema.vocabulary.spi.LazyVocabularies index 68cddf8a..7924e0ef 100644 --- a/core/src/main/resources/META-INF/services/io.github.sebastiantoepfer.jsonschema.vocabulary.spi.LazyVocabularies +++ b/core/src/main/resources/META-INF/services/io.github.sebastiantoepfer.jsonschema.vocabulary.spi.LazyVocabularies @@ -1 +1 @@ -io.github.sebastiantoepfer.jsonschema.core.vocab.core.LazyCoreVocabulary +io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreLazyVocabulary diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/KeywordsTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/KeywordsTest.java index 1945fda6..fd6b3c02 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/KeywordsTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/KeywordsTest.java @@ -30,7 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import io.github.sebastiantoepfer.jsonschema.core.vocab.basic.BasicVocabulary; -import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreVocabulary; +import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreLazyVocabulary; import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition; import java.net.URI; import java.util.Collection; @@ -42,7 +42,7 @@ class KeywordsTest { @Test void should_not_be_createbale_without_mandantory_core_vocabulary() { final Collection vocabDefs = List.of( - new VocabularyDefinition(new CoreVocabulary().id(), false) + new VocabularyDefinition(new CoreLazyVocabulary().vocab().id(), false) ); assertThrows(IllegalArgumentException.class, () -> new Keywords(vocabDefs)); } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreVocabularyTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CommentKeywordTypeTest.java similarity index 55% rename from core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreVocabularyTest.java rename to core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CommentKeywordTypeTest.java index 392e842e..1118579e 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreVocabularyTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CommentKeywordTypeTest.java @@ -26,29 +26,13 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import com.github.npathai.hamcrestopt.OptionalMatchers; -import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreVocabulary; import jakarta.json.JsonValue; -import java.net.URI; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; +import org.junit.jupiter.api.Test; -class CoreVocabularyTest { +class CommentKeywordTypeTest { - void should_return_the_uri_of_the_current_core_vocabulary() { - assertThat(new CoreVocabulary().id(), is(URI.create("https://json-schema.org/draft/2020-12/vocab/core"))); - } - - @ParameterizedTest(name = "should know keyword {0}") - @ValueSource(strings = { "$schema", "$vocabulary", "$id", "$ref", "$dynamicRef", "$defs", "$comment" }) - void should_return_keywords_for_name(final String name) { - //keyword creation for easier pitesting :) -> i know it is bad - assertThat( - new CoreVocabulary() - .findKeywordTypeByName(name) - .map(keywordType -> keywordType.createKeyword(JsonValue.EMPTY_JSON_OBJECT)) - .map(keyword -> keyword.hasName(name)), - OptionalMatchers.isPresentAndIs(true) - ); + @Test + void should_create_keyword_with_name() { + assertThat(new CommentKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$comment"), is(true)); } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/LazyCoreVocabularyTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreLazyVocabularyTest.java similarity index 93% rename from core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/LazyCoreVocabularyTest.java rename to core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreLazyVocabularyTest.java index 5f1aed0e..a862b625 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/LazyCoreVocabularyTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreLazyVocabularyTest.java @@ -31,17 +31,17 @@ import java.net.URI; import org.junit.jupiter.api.Test; -class LazyCoreVocabularyTest { +class CoreLazyVocabularyTest { @Test void should_return_empty_for_non_core_id() { - assertThat(new LazyCoreVocabulary().loadVocabularyWithId(URI.create("")), isEmpty()); + assertThat(new CoreLazyVocabulary().loadVocabularyWithId(URI.create("")), isEmpty()); } @Test void should_return_core_vocabulary_for_core_id() { assertThat( - new LazyCoreVocabulary() + new CoreLazyVocabulary() .loadVocabularyWithId(URI.create("https://json-schema.org/draft/2020-12/vocab/core")) .map(Vocabulary::id), isPresentAndIs(URI.create("https://json-schema.org/draft/2020-12/vocab/core")) diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java new file mode 100644 index 00000000..66910b81 --- /dev/null +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java @@ -0,0 +1,38 @@ +/* + * The MIT License + * + * Copyright 2023 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 static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import jakarta.json.JsonValue; +import org.junit.jupiter.api.Test; + +class DefsKeywordTypeTest { + + @Test + void should_create_keyword_with_name() { + assertThat(new DefsKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$defs"), is(true)); + } +} diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTypeTest.java index 4a6dfb94..4a04213b 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DynamicRefKeywordTypeTest.java @@ -26,13 +26,20 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import io.github.sebastiantoepfer.jsonschema.core.vocab.core.DynamicRefKeywordType; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonValue; import org.junit.jupiter.api.Test; class DynamicRefKeywordTypeTest { + @Test + void should_create_keyword_with_name() { + assertThat( + new DynamicRefKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$dynamicRef"), + is(true) + ); + } + @Test void notFinischedYet() { final Keyword keyword = new DynamicRefKeywordType().createKeyword(JsonValue.FALSE); diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java new file mode 100644 index 00000000..3226a1e5 --- /dev/null +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java @@ -0,0 +1,38 @@ +/* + * The MIT License + * + * Copyright 2023 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 static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import jakarta.json.JsonValue; +import org.junit.jupiter.api.Test; + +class IdKeywordTypeTest { + + @Test + void should_create_keyword_with_name() { + assertThat(new IdKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$id"), is(true)); + } +} 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 345da416..5f8a1bdf 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 @@ -26,13 +26,17 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import io.github.sebastiantoepfer.jsonschema.core.vocab.core.RefKeywordType; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonValue; import org.junit.jupiter.api.Test; class RefKeywordTypeTest { + @Test + void should_create_keyword_with_name() { + assertThat(new RefKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$ref"), is(true)); + } + @Test void notFinischedYet() { final Keyword keyword = new RefKeywordType().createKeyword(JsonValue.FALSE); diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTypeTest.java new file mode 100644 index 00000000..60b77e4c --- /dev/null +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/SchemaKeywordTypeTest.java @@ -0,0 +1,38 @@ +/* + * The MIT License + * + * Copyright 2023 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 static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import jakarta.json.JsonValue; +import org.junit.jupiter.api.Test; + +class SchemaKeywordTypeTest { + + @Test + void should_create_keyword_with_name() { + assertThat(new SchemaKeywordType().createKeyword(JsonValue.EMPTY_JSON_OBJECT).hasName("$schema"), is(true)); + } +} diff --git a/vocabulary-spi/pom.xml b/vocabulary-spi/pom.xml index e3227436..afe9e938 100644 --- a/vocabulary-spi/pom.xml +++ b/vocabulary-spi/pom.xml @@ -42,5 +42,11 @@ hamcrest-optional test + + + jakarta.json + jakarta.json-api + provided + diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreVocabulary.java b/vocabulary-spi/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/DefaultVocabulary.java similarity index 65% rename from core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreVocabulary.java rename to vocabulary-spi/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/DefaultVocabulary.java index 33b7477e..2d551ff6 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/CoreVocabulary.java +++ b/vocabulary-spi/src/main/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/DefaultVocabulary.java @@ -21,38 +21,38 @@ * 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; +package io.github.sebastiantoepfer.jsonschema.vocabulary.spi; import io.github.sebastiantoepfer.jsonschema.Vocabulary; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; import java.net.URI; +import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.Optional; -/** - * see: https://json-schema.org/draft/2020-12/json-schema-core.html#name-the-json-schema-core-vocabu - */ -public final class CoreVocabulary implements Vocabulary { +public final class DefaultVocabulary implements Vocabulary { + + private final URI id; + private final List keywords; - private Collection supportedKeywords = List.of( - new SchemaKeywordType(), - new VocabularyKeywordType(), - new IdKeywordType(), - new RefKeywordType(), - new DynamicRefKeywordType(), - new DefsKeywordType(), - new CommentKeywordType() - ); + public DefaultVocabulary(final URI id, final KeywordType... keywortds) { + this(id, Arrays.asList(keywortds)); + } + + public DefaultVocabulary(final URI id, final Collection keywords) { + this.id = Objects.requireNonNull(id); + this.keywords = List.copyOf(keywords); + } @Override public URI id() { - //we need a way to find out which draft we are using! - return URI.create("https://json-schema.org/draft/2020-12/vocab/core"); + return id; } @Override public Optional findKeywordTypeByName(final String name) { - return supportedKeywords.stream().filter(keywordType -> keywordType.hasName(name)).findFirst(); + return keywords.stream().filter(keywordType -> keywordType.hasName(name)).findFirst(); } } diff --git a/vocabulary-spi/src/test/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/DefaultVocabularyTest.java b/vocabulary-spi/src/test/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/DefaultVocabularyTest.java new file mode 100644 index 00000000..f704ff95 --- /dev/null +++ b/vocabulary-spi/src/test/java/io/github/sebastiantoepfer/jsonschema/vocabulary/spi/DefaultVocabularyTest.java @@ -0,0 +1,83 @@ +/* + * The MIT License + * + * Copyright 2023 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.vocabulary.spi; + +import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty; +import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAndIs; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; +import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; +import jakarta.json.JsonValue; +import java.net.URI; +import org.junit.jupiter.api.Test; + +class DefaultVocabularyTest { + + @Test + void should_return_the_id() { + assertThat( + new DefaultVocabulary(URI.create("http::/localhots/vocab")).id(), + is(URI.create("http::/localhots/vocab")) + ); + } + + @Test + void should_return_empty_if_requested_keyword_is_unknown() { + assertThat( + new DefaultVocabulary(URI.create("http://localhost"), new SimpleTestKeywordType("name")) + .findKeywordTypeByName("test"), + isEmpty() + ); + } + + @Test + void should_return_requested_keyword_if_is_it_known() { + final KeywordType testKeywordType = new SimpleTestKeywordType("test"); + assertThat( + new DefaultVocabulary(URI.create("http://localhost"), testKeywordType).findKeywordTypeByName("test"), + isPresentAndIs(testKeywordType) + ); + } + + private static class SimpleTestKeywordType implements KeywordType { + + private final String name; + + public SimpleTestKeywordType(final String name) { + this.name = name; + } + + @Override + public String name() { + return name; + } + + @Override + public Keyword createKeyword(final JsonValue value) { + throw new UnsupportedOperationException("Not supported yet."); + } + } +}