From e0b4c9f0c12188b3be85bac2b86d105c35928b20 Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Thu, 5 Oct 2023 10:26:07 +0200 Subject: [PATCH] add posibility to retrieve root-schema --- .../jsonschema/JsonSchema.java | 6 +- .../jsonschema/JsonSubSchema.java | 5 ++ .../jsonschema/FakeJsonSchemaFactory.java | 31 +++++---- .../jsonschema/JsonSchemaTest.java | 40 +++++++++++ .../jsonschema/JsonSubSchemaTest.java | 66 +++++++++++++++++++ 5 files changed, 133 insertions(+), 15 deletions(-) create mode 100644 api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSchemaTest.java create mode 100644 api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchemaTest.java diff --git a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java index 5d559418..2e2da397 100644 --- a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java +++ b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java @@ -28,7 +28,11 @@ import java.util.Optional; public interface JsonSchema extends JsonValue { - public Validator validator(); + Validator validator(); Optional keywordByName(String name); + + default JsonSchema rootSchema() { + return this; + } } diff --git a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchema.java b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchema.java index 0c4ca96b..d6ef9cbb 100644 --- a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchema.java +++ b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchema.java @@ -29,4 +29,9 @@ */ public interface JsonSubSchema extends JsonSchema { JsonSchema owner(); + + @Override + default JsonSchema rootSchema() { + return owner().rootSchema(); + } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java index f909b7ae..16ae4822 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java @@ -32,21 +32,24 @@ public final class FakeJsonSchemaFactory implements JsonSchemaFactory { @Override public JsonSchema create(final JsonValue schema) { - return new JsonSchema() { - @Override - public Validator validator() { - throw new UnsupportedOperationException("Not supported yet."); - } + return new FakeJsonSchema(); + } + + static class FakeJsonSchema implements JsonSchema { + + @Override + public Validator validator() { + throw new UnsupportedOperationException("Not supported yet."); + } - @Override - public JsonValue.ValueType getValueType() { - throw new UnsupportedOperationException("Not supported yet."); - } + @Override + public Optional keywordByName(String name) { + throw new UnsupportedOperationException("Not supported yet."); + } - @Override - public Optional keywordByName(String name) { - throw new UnsupportedOperationException("Not supported yet."); - } - }; + @Override + public JsonValue.ValueType getValueType() { + throw new UnsupportedOperationException("Not supported yet."); + } } } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSchemaTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSchemaTest.java new file mode 100644 index 00000000..9f14dda8 --- /dev/null +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSchemaTest.java @@ -0,0 +1,40 @@ +/* + * 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; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.sameInstance; + +import io.github.sebastiantoepfer.jsonschema.FakeJsonSchemaFactory.FakeJsonSchema; +import org.junit.jupiter.api.Test; + +class JsonSchemaTest { + + @Test + void should_return_itself_as_root_schema() { + final JsonSchema schema = new FakeJsonSchema(); + assertThat(schema.rootSchema(), is(sameInstance(schema))); + } +} diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchemaTest.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchemaTest.java new file mode 100644 index 00000000..f5ac1fa4 --- /dev/null +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/JsonSubSchemaTest.java @@ -0,0 +1,66 @@ +/* + * 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; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.sameInstance; + +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; +import jakarta.json.JsonValue; +import java.util.Optional; +import org.junit.jupiter.api.Test; + +class JsonSubSchemaTest { + + @Test + void should_return_its_owner_as_root() { + final JsonSchema root = new FakeJsonSchemaFactory.FakeJsonSchema(); + assertThat( + new JsonSubSchema() { + @Override + public JsonSchema owner() { + return root; + } + + @Override + public Validator validator() { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Optional keywordByName(String name) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public JsonValue.ValueType getValueType() { + throw new UnsupportedOperationException("Not supported yet."); + } + } + .rootSchema(), + is(sameInstance(root)) + ); + } +}