Skip to content

Commit

Permalink
refactor test for anyofkeyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 8, 2024
1 parent 184e763 commit 52f5bc9
Showing 1 changed file with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@
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 java.util.List;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;

class AnyOfKeywordTest {

@Test
void should_know_his_name() {
final Keyword items = createKeywordFrom(
Json.createObjectBuilder().add("anyOf", Json.createArrayBuilder().add(JsonValue.TRUE)).build()
);
final Keyword items = new AnyOfKeyword(List.of(JsonSchemas.load(JsonValue.TRUE)));

assertThat(items.hasName("anyOf"), is(true));
assertThat(items.hasName("test"), is(false));
Expand All @@ -54,20 +51,17 @@ void should_know_his_name() {
@Test
void should_be_printable() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add(
"anyOf",
Json.createArrayBuilder()
new AnyOfKeyword(
List.of(
JsonSchemas.load(
Json.createObjectBuilder()
.add(
Json.createObjectBuilder()
.add(
"allOf",
Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "number"))
)
"allOf",
Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "number"))
)
.build()
)
.build()
)
).printOn(new HashMapMedia()),
(Matcher) hasEntry(is("anyOf"), hasItem((hasKey("allOf"))))
);
Expand All @@ -76,13 +70,15 @@ void should_be_printable() {
@Test
void should_be_valid_if_any_schemas_applies() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add(
"anyOf",
Json.createArrayBuilder().add(JsonValue.FALSE).add(JsonValue.TRUE).add(JsonValue.FALSE)
)
new AnyOfKeyword(
Json.createArrayBuilder()
.add(JsonValue.FALSE)
.add(JsonValue.TRUE)
.add(JsonValue.FALSE)
.build()
.stream()
.map(JsonSchemas::load)
.toList()
)
.asApplicator()
.applyTo(Json.createValue(25)),
Expand All @@ -93,20 +89,18 @@ void should_be_valid_if_any_schemas_applies() {
@Test
void should_be_invalid_if_no_schemas_apply() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("anyOf", Json.createArrayBuilder().add(JsonValue.FALSE).add(JsonValue.FALSE))
new AnyOfKeyword(
Json.createArrayBuilder()
.add(JsonValue.FALSE)
.add(JsonValue.FALSE)
.build()
.stream()
.map(JsonSchemas::load)
.toList()
)
.asApplicator()
.applyTo(Json.createValue(10)),
is(false)
);
}

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

0 comments on commit 52f5bc9

Please sign in to comment.