Skip to content

Commit

Permalink
refactor all test of keywords defined by applicator vocab
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 9, 2024
1 parent cfa5be4 commit b2f1f20
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,18 @@ private Stream<JsonValue> itemsForValidation(final JsonArray items) {
}

private int startIndexFor(final JsonArray value) {
return affectedBys
.stream()
.map(anno -> anno.valueFor(value))
.map(v -> new MaxIndexCalculator(value, v))
.mapToInt(MaxIndexCalculator::maxIndex)
.sum();
final int result;
if (affectedBys.isEmpty()) {
result = -1;
} else {
result = affectedBys
.stream()
.map(anno -> anno.valueFor(value))
.map(v -> new MaxIndexCalculator(value, v))
.mapToInt(MaxIndexCalculator::maxIndex)
.sum();
}
return result;
}

private static class MaxIndexCalculator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@

import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
import io.github.sebastiantoepfer.jsonschema.JsonSchemas;
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.Affects;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.AffectsKeywordType;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.SubSchemaKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.StaticAnnotation;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import java.util.List;
import org.hamcrest.Matcher;
Expand All @@ -55,11 +51,9 @@ void should_know_his_name() {
@Test
void should_be_valid_for_non_objects() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE))
.add("additionalProperties", JsonValue.FALSE)
.build()
new AdditionalPropertiesKeyword(
List.of(new StaticAnnotation("properties", JsonValue.EMPTY_JSON_ARRAY)),
JsonSchemas.load(JsonValue.FALSE)
)
.asApplicator()
.applyTo(JsonValue.EMPTY_JSON_ARRAY),
Expand All @@ -70,11 +64,9 @@ void should_be_valid_for_non_objects() {
@Test
void should_not_valid_if_no_additionals_are_allow() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE))
.add("additionalProperties", JsonValue.FALSE)
.build()
new AdditionalPropertiesKeyword(
List.of(new StaticAnnotation("properties", Json.createArrayBuilder().add("test").build())),
JsonSchemas.load(JsonValue.FALSE)
)
.asApplicator()
.applyTo(Json.createObjectBuilder().add("test", 1).add("foo", 1).build()),
Expand All @@ -85,11 +77,9 @@ void should_not_valid_if_no_additionals_are_allow() {
@Test
void should_valid_if_additionals_are_allow() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE))
.add("additionalProperties", JsonValue.TRUE)
.build()
new AdditionalPropertiesKeyword(
List.of(new StaticAnnotation("properties", Json.createArrayBuilder().add("test").build())),
JsonSchemas.load(JsonValue.TRUE)
)
.asApplicator()
.applyTo(Json.createObjectBuilder().add("test", 1).add("foo", 1).build()),
Expand All @@ -100,11 +90,9 @@ void should_valid_if_additionals_are_allow() {
@Test
void should_valid_if_no_additionals_are_allow_and_no_additionals_their() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE))
.add("additionalProperties", JsonValue.FALSE)
.build()
new AdditionalPropertiesKeyword(
List.of(new StaticAnnotation("properties", Json.createArrayBuilder().add("test").build())),
JsonSchemas.load(JsonValue.FALSE)
)
.asApplicator()
.applyTo(Json.createObjectBuilder().add("test", 1).build()),
Expand All @@ -115,8 +103,9 @@ void should_valid_if_no_additionals_are_allow_and_no_additionals_their() {
@Test
void should_be_an_applicator_and_an_annotation() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder().add("additionalProperties", JsonValue.TRUE).build()
new AdditionalPropertiesKeyword(
List.of(new StaticAnnotation("properties", JsonValue.EMPTY_JSON_ARRAY)),
JsonSchemas.load(JsonValue.FALSE)
).categories(),
containsInAnyOrder(Keyword.KeywordCategory.APPLICATOR, Keyword.KeywordCategory.ANNOTATION)
);
Expand All @@ -125,11 +114,9 @@ void should_be_an_applicator_and_an_annotation() {
@Test
void should_return_propertynames_which_will_be_validated() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE))
.add("additionalProperties", JsonValue.TRUE)
.build()
new AdditionalPropertiesKeyword(
List.of(new StaticAnnotation("properties", Json.createArrayBuilder().add("test").build())),
JsonSchemas.load(JsonValue.TRUE)
)
.asAnnotation()
.valueFor(Json.createObjectBuilder().add("test", 1).add("foo", 1).build())
Expand All @@ -141,25 +128,10 @@ void should_return_propertynames_which_will_be_validated() {
@Test
void should_be_printable() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder().add("additionalProperties", JsonValue.EMPTY_JSON_OBJECT).build()
).printOn(new HashMapMedia()),
new AdditionalPropertiesKeyword(List.of(), JsonSchemas.load(JsonValue.EMPTY_JSON_OBJECT)).printOn(
new HashMapMedia()
),
(Matcher) hasEntry(is("additionalProperties"), anEmptyMap())
);
}

private static Keyword createKeywordFrom(final JsonObject json) {
return new AffectsKeywordType(
"additionalProperties",
List.of(
new Affects("properties", JsonValue.EMPTY_JSON_ARRAY),
new Affects("patternProperties", JsonValue.EMPTY_JSON_ARRAY)
),
(affects, schema) ->
new SubSchemaKeywordType(
"additionalProperties",
s -> new AdditionalPropertiesKeyword(affects, s)
).createKeyword(schema)
).createKeyword(new DefaultJsonSchemaFactory().create(json));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasKey;
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.SchemaArrayKeywordType;
import io.github.sebastiantoepfer.jsonschema.JsonSchemas;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
Expand All @@ -43,67 +41,40 @@ class AllOfKeywordTest {

@Test
void should_know_his_name() {
final Keyword items = createKeywordFrom(
Json.createObjectBuilder().add("allOf", Json.createArrayBuilder().add(JsonValue.TRUE)).build()
final Keyword items = new AllOfKeyword(
Json.createArrayBuilder().add(JsonValue.TRUE).build().stream().map(JsonSchemas::load).toList()
);

assertThat(items.hasName("allOf"), is(true));
assertThat(items.hasName("test"), is(false));
}

@Test
void should_be_printable() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder()
.add(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "number"))
)
)
.add(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder().add(Json.createObjectBuilder().add("minimum", 18))
)
)
)
new AllOfKeyword(
Json.createArrayBuilder()
.add(Json.createObjectBuilder().add("type", "number"))
.add(Json.createObjectBuilder().add("minimum", 18))
.build()
.stream()
.map(JsonSchemas::load)
.toList()
).printOn(new HashMapMedia()),
(Matcher) hasEntry(is("allOf"), hasItem((hasKey("allOf"))))
(Matcher) hasEntry(is("allOf"), hasItems(hasKey("type"), hasKey("minimum")))
);
}

@Test
void should_be_valid_if_all_schemas_applies() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder()
.add(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "number"))
)
)
.add(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder().add(Json.createObjectBuilder().add("minimum", 18))
)
)
)
new AllOfKeyword(
Json.createArrayBuilder()
.add(Json.createObjectBuilder().add("type", "number"))
.add(Json.createObjectBuilder().add("minimum", 18))
.build()
.stream()
.map(JsonSchemas::load)
.toList()
)
.asApplicator()
.applyTo(Json.createValue(25)),
Expand All @@ -114,37 +85,18 @@ void should_be_valid_if_all_schemas_applies() {
@Test
void should_be_invalid_if_any_schemas_not_apply() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder()
.add(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "number"))
)
)
.add(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder().add(Json.createObjectBuilder().add("minimum", 18))
)
)
)
new AllOfKeyword(
Json.createArrayBuilder()
.add(Json.createObjectBuilder().add("type", "number"))
.add(Json.createObjectBuilder().add("minimum", 18))
.build()
.stream()
.map(JsonSchemas::load)
.toList()
)
.asApplicator()
.applyTo(Json.createValue(10)),
is(false)
);
}

private static Keyword createKeywordFrom(final JsonObject json) {
return new SchemaArrayKeywordType("allOf", AllOfKeyword::new).createKeyword(
new DefaultJsonSchemaFactory().create(json)
);
}
}
Loading

0 comments on commit b2f1f20

Please sign in to comment.