From ce3ac3a029613c66d7e54706f9fabdcb0311a558 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 containskeyword --- .../vocab/applicator/ContainsKeywordTest.java | 59 +++++-------------- 1 file changed, 15 insertions(+), 44 deletions(-) diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeywordTest.java index 02c8c5f8..f94a4c35 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeywordTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeywordTest.java @@ -31,11 +31,8 @@ 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.SubSchemaKeywordType; 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.hamcrest.Matchers; @@ -46,9 +43,9 @@ class ContainsKeywordTest { @Test void should_be_printable() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() - ).printOn(new HashMapMedia()), + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build())).printOn( + new HashMapMedia() + ), (Matcher) hasEntry(is("contains"), hasEntry(is("type"), is("number"))) ); } @@ -64,8 +61,8 @@ void should_know_his_name() { @Test void should_be_an_applicator_and_annotation() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() + new ContainsKeyword( + JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build()) ).categories(), Matchers.containsInAnyOrder(Keyword.KeywordCategory.ANNOTATION, Keyword.KeywordCategory.APPLICATOR) ); @@ -74,9 +71,7 @@ void should_be_an_applicator_and_annotation() { @Test void should_apply_for_non_array() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build())) .asApplicator() .applyTo(JsonValue.EMPTY_JSON_OBJECT), is(true) @@ -86,9 +81,7 @@ void should_apply_for_non_array() { @Test void should_not_apply_to_empty_array_if_non_min_andor_max_is_provided() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build())) .asApplicator() .applyTo(JsonValue.EMPTY_JSON_ARRAY), is(false) @@ -98,9 +91,7 @@ void should_not_apply_to_empty_array_if_non_min_andor_max_is_provided() { @Test void should_apply_if_one_item_applies() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build())) .asApplicator() .applyTo( Json.createArrayBuilder() @@ -118,9 +109,7 @@ void should_apply_if_one_item_applies() { @Test void should_apply_if_all_item_applies() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "string")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "string").build())) .asApplicator() .applyTo(Json.createArrayBuilder().add("foo").add("bar").add("baz").build()), is(true) @@ -130,9 +119,7 @@ void should_apply_if_all_item_applies() { @Test void should_not_apply_if_non_item_applies() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build())) .asApplicator() .applyTo( Json.createArrayBuilder().add("foo").add(false).add(Json.createArrayBuilder().add("bar")).build() @@ -144,9 +131,7 @@ void should_not_apply_if_non_item_applies() { @Test void should_return_false_for_non_array() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build())) .asAnnotation() .valueFor(JsonValue.EMPTY_JSON_OBJECT), is(JsonValue.FALSE) @@ -156,9 +141,7 @@ void should_return_false_for_non_array() { @Test void should_return_empty_array_if_no_item_applies() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build())) .asAnnotation() .valueFor(Json.createArrayBuilder().add("foo").build()) .asJsonArray(), @@ -169,9 +152,7 @@ void should_return_empty_array_if_no_item_applies() { @Test void should_return_empty_array_for_empty_array() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build())) .asAnnotation() .valueFor(JsonValue.EMPTY_JSON_ARRAY) .asJsonArray(), @@ -182,9 +163,7 @@ void should_return_empty_array_for_empty_array() { @Test void should_return_matching_items() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "number").build())) .asAnnotation() .valueFor( Json.createArrayBuilder() @@ -203,18 +182,10 @@ void should_return_matching_items() { @Test void should_return_true_if_all_item_applies() { assertThat( - createKeywordFrom( - Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "string")).build() - ) + new ContainsKeyword(JsonSchemas.load(Json.createObjectBuilder().add("type", "string").build())) .asAnnotation() .valueFor(Json.createArrayBuilder().add("foo").add("bar").add("baz").build()), is(JsonValue.TRUE) ); } - - private static Keyword createKeywordFrom(final JsonObject json) { - return new SubSchemaKeywordType("contains", ContainsKeyword::new).createKeyword( - new DefaultJsonSchemaFactory().create(json) - ); - } }