Skip to content

Commit

Permalink
refactor test for properties keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 8, 2024
1 parent ea8d883 commit 4ba8073
Showing 1 changed file with 13 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
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.JsonSchemas;
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;
Expand All @@ -44,9 +43,7 @@ class PropertiesKeywordTest {

@Test
void should_be_know_his_name() {
final Keyword keyword = createKeywordFrom(
Json.createObjectBuilder().add("properties", JsonValue.EMPTY_JSON_OBJECT).build()
);
final Keyword keyword = new PropertiesKeyword(new NamedJsonSchemas(Map.of()));

assertThat(keyword.hasName("properties"), is(true));
assertThat(keyword.hasName("test"), is(false));
Expand All @@ -55,31 +52,23 @@ void should_be_know_his_name() {
@Test
void should_be_an_applicator_and_annotation() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder().add("properties", JsonValue.EMPTY_JSON_OBJECT).build()
).categories(),
new PropertiesKeyword(new NamedJsonSchemas(Map.of())).categories(),
containsInAnyOrder(Keyword.KeywordCategory.APPLICATOR, Keyword.KeywordCategory.ANNOTATION)
);
}

@Test
void should_be_valid_for_non_objects() {
assertThat(
createKeywordFrom(Json.createObjectBuilder().add("properties", JsonValue.EMPTY_JSON_OBJECT).build())
.asApplicator()
.applyTo(JsonValue.EMPTY_JSON_ARRAY),
new PropertiesKeyword(new NamedJsonSchemas(Map.of())).asApplicator().applyTo(JsonValue.EMPTY_JSON_ARRAY),
is(true)
);
}

@Test
void should_be_valid_if_properties_applies_to_his_schema() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE))
.build()
)
new PropertiesKeyword(new NamedJsonSchemas(Map.of("test", JsonSchemas.load(JsonValue.TRUE))))
.asApplicator()
.applyTo(Json.createObjectBuilder().add("test", 1).build()),
is(true)
Expand All @@ -89,11 +78,7 @@ void should_be_valid_if_properties_applies_to_his_schema() {
@Test
void should_be_invalid_if_properties_not_applies_to_his_schema() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", JsonValue.FALSE))
.build()
)
new PropertiesKeyword(new NamedJsonSchemas(Map.of("test", JsonSchemas.load(JsonValue.FALSE))))
.asApplicator()
.applyTo(Json.createObjectBuilder().add("test", 1).build()),
is(false)
Expand All @@ -103,11 +88,7 @@ void should_be_invalid_if_properties_not_applies_to_his_schema() {
@Test
void should_be_valid_for_empty_objects() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", JsonValue.FALSE))
.build()
)
new PropertiesKeyword(new NamedJsonSchemas(Map.of("test", JsonSchemas.load(JsonValue.FALSE))))
.asApplicator()
.applyTo(JsonValue.EMPTY_JSON_OBJECT),
is(true)
Expand All @@ -117,10 +98,10 @@ void should_be_valid_for_empty_objects() {
@Test
void should_return_all_matched_propertynames() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", true).add("foo", true))
.build()
new PropertiesKeyword(
new NamedJsonSchemas(
Map.of("test", JsonSchemas.load(JsonValue.TRUE), "foo", JsonSchemas.load(JsonValue.TRUE))
)
)
.asAnnotation()
.valueFor(Json.createObjectBuilder().add("foo", 1).build())
Expand All @@ -134,20 +115,10 @@ void should_return_all_matched_propertynames() {
@Test
void should_be_printable() {
assertThat(
((Map) createKeywordFrom(
Json.createObjectBuilder()
.add("properties", Json.createObjectBuilder().add("test", JsonValue.TRUE))
.build()
)
((Map) new PropertiesKeyword(new NamedJsonSchemas(Map.of("test", JsonSchemas.load(JsonValue.TRUE))))
.printOn(new HashMapMedia())
.get("properties")).get("test"),
(Matcher) anEmptyMap()
);
}

private static Keyword createKeywordFrom(final JsonObject json) {
return new NamedJsonSchemaKeywordType("properties", PropertiesKeyword::new).createKeyword(
new DefaultJsonSchemaFactory().create(json)
);
}
}

0 comments on commit 4ba8073

Please sign in to comment.