Skip to content

Commit

Permalink
refactor test for containskeyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 8, 2024
1 parent 52f5bc9 commit ce3ac3a
Showing 1 changed file with 15 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")))
);
}
Expand All @@ -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)
);
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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()
Expand All @@ -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)
);
}
}

0 comments on commit ce3ac3a

Please sign in to comment.