Skip to content

Commit

Permalink
refactor test for all keyword defined in corevocab
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 9, 2024
1 parent b2f1f20 commit bc9b0f1
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 259 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
* <b>$vocabulary</b> : <i>Object&lt;URI, Boolean&gt;</i><br/>
* This keyword is used in meta-schemas to identify the required and optional vocabularies available for use in<br/>
* schemas described by that meta-schema.<br/>
* <br/>
* <ul>
* <li>identifier</li>
* </ul>
*
* 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 extends Media<T>> 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<KeywordCategory> categories() {
//is a identifier after spec ... but how to implement it as it?
return List.of();
}

@Override
public Stream<VocabularyDefinition> definitions() {
return vocabularies
.entrySet()
.stream()
.map(entry -> new VocabularyDefinition(URI.create(entry.getKey()), entry.getValue() == JsonValue.TRUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -64,53 +54,4 @@ public VocabularyKeyword createKeyword(final JsonSchema schema) {
}
return result;
}

/**
* <b>$vocabulary</b> : <i>Object&lt;URI, Boolean&gt;</i><br/>
* This keyword is used in meta-schemas to identify the required and optional vocabularies available for use in<br/>
* schemas described by that meta-schema.<br/>
* <br/>
* <ul>
* <li>identifier</li>
* </ul>
*
* 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 extends Media<T>> 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<KeywordCategory> categories() {
//is a identifier after spec ... but how to implement it as it?
return List.of();
}

@Override
public Stream<VocabularyDefinition> definitions() {
return vocabularies
.entrySet()
.stream()
.map(entry -> new VocabularyDefinition(URI.create(entry.getKey()), entry.getValue() == JsonValue.TRUE));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,21 @@
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));
}

@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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,17 @@
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;

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));
Expand All @@ -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)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -51,32 +46,16 @@ 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"))
);
}

@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)
);
}
}
Loading

0 comments on commit bc9b0f1

Please sign in to comment.