Skip to content

Commit

Permalink
WIP printables some tests yet :(
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jan 13, 2024
1 parent 37cee6f commit ba2cc0c
Show file tree
Hide file tree
Showing 24 changed files with 264 additions and 119 deletions.
4 changes: 4 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<groupId>io.github.sebastian-toepfer.ddd</groupId>
<artifactId>common</artifactId>
</dependency>
<dependency>
<groupId>io.github.sebastian-toepfer.ddd</groupId>
<artifactId>media-json-api</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
package io.github.sebastiantoepfer.jsonschema.keyword;

import io.github.sebastiantoepfer.ddd.common.Media;
import jakarta.json.JsonNumber;
import jakarta.json.JsonString;
import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import java.util.Objects;

Expand Down Expand Up @@ -57,18 +57,6 @@ public JsonValue valueFor(final JsonValue instance) {

@Override
public <T extends Media<T>> T printOn(final T media) {
final T result;
if (value instanceof JsonString str) {
result = media.withValue(name, str.getString());
} else if (value instanceof JsonNumber nr) {
result = media.withValue(name, nr.bigDecimalValue());
} else if (value == JsonValue.TRUE) {
result = media.withValue(name, true);
} else if (value == JsonValue.FALSE) {
result = media.withValue(name, false);
} else {
result = media;
}
return result;
return new JsonObjectPrintable(Json.createObjectBuilder().add(name, value).build()).printOn(media);
}
}
1 change: 1 addition & 0 deletions api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
exports io.github.sebastiantoepfer.jsonschema.spi;

requires io.github.sebastiantoepfer.ddd.common;
requires io.github.sebastiantoepfer.ddd.media.json;
requires jakarta.json;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;

class StaticAnnotationTest {
Expand Down Expand Up @@ -63,15 +62,15 @@ void should_be_printable_with_null_as_value() {
void should_be_printable_with_decimalnumber_as_value() {
assertThat(
new StaticAnnotation("default", Json.createValue(32)).printOn(new HashMapMedia()),
hasEntry("default", BigDecimal.valueOf(32).stripTrailingZeros())
hasEntry("default", 32L)
);
}

@Test
void should_be_printable_with_decimal_as_value() {
assertThat(
new StaticAnnotation("default", Json.createValue(32.1)).printOn(new HashMapMedia()),
hasEntry("default", BigDecimal.valueOf(32.1))
hasEntry("default", 32.1)
);
}

Expand Down
4 changes: 3 additions & 1 deletion api/src/test/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@

open module io.github.sebastiantoepfer.jsonschema {
requires io.github.sebastiantoepfer.ddd.common;
requires io.github.sebastiantoepfer.ddd.media.core;
requires io.github.sebastiantoepfer.ddd.media.json;

requires jakarta.json;

requires io.github.sebastiantoepfer.ddd.media.core;
requires org.junit.jupiter.api;
requires org.junit.jupiter.params;
requires org.hamcrest;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import io.github.sebastiantoepfer.jsonschema.keyword.Assertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.JsonNumber;
import jakarta.json.JsonValue;
import java.math.BigInteger;
import java.util.Objects;

final class MaxItemsKeywordType implements KeywordType {
Expand All @@ -42,15 +42,15 @@ public String name() {

@Override
public Keyword createKeyword(final JsonSchema schema) {
return new MaxItemsKeyword(schema.asJsonObject().getJsonNumber(name()));
return new MaxItemsKeyword(schema.asJsonObject().getJsonNumber(name()).bigIntegerValueExact());
}

private class MaxItemsKeyword implements Assertion {

private final int maxItems;
private final BigInteger maxItems;

public MaxItemsKeyword(final JsonNumber maxItems) {
this.maxItems = maxItems.intValue();
public MaxItemsKeyword(final BigInteger maxItems) {
this.maxItems = maxItems;
}

@Override
Expand All @@ -60,7 +60,7 @@ public <T extends Media<T>> T printOn(final T media) {

@Override
public boolean isValidFor(final JsonValue instance) {
return !InstanceType.ARRAY.isInstance(instance) || instance.asJsonArray().size() <= maxItems;
return !InstanceType.ARRAY.isInstance(instance) || instance.asJsonArray().size() <= maxItems.intValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import jakarta.json.JsonNumber;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import java.math.BigInteger;
import java.util.Objects;

class MaxLengthKeywordType implements KeywordType {
Expand All @@ -45,23 +46,23 @@ public String name() {
public Keyword createKeyword(final JsonSchema schema) {
final JsonValue value = schema.asJsonObject().get(name());
if (InstanceType.INTEGER.isInstance(value)) {
return new MaxLengthKeyword((JsonNumber) value);
return new MaxLengthKeyword(((JsonNumber) value).bigIntegerValueExact());
} else {
throw new IllegalArgumentException("value must be a positiv integer!");
}
}

private class MaxLengthKeyword implements Assertion {

private final JsonNumber value;
private final BigInteger value;

public MaxLengthKeyword(final JsonNumber value) {
public MaxLengthKeyword(final BigInteger value) {
this.value = value;
}

@Override
public <T extends Media<T>> T printOn(final T media) {
return media.withValue(name(), value.longValue());
return media.withValue(name(), value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import io.github.sebastiantoepfer.jsonschema.keyword.Assertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.JsonNumber;
import jakarta.json.JsonValue;
import java.math.BigInteger;
import java.util.Objects;

final class MinItemsKeywordType implements KeywordType {
Expand All @@ -42,15 +42,15 @@ public String name() {

@Override
public Keyword createKeyword(final JsonSchema schema) {
return new MinItemsKeyword(schema.asJsonObject().getJsonNumber(name()));
return new MinItemsKeyword(schema.asJsonObject().getJsonNumber(name()).bigIntegerValueExact());
}

private class MinItemsKeyword implements Assertion {

private final int minItems;
private final BigInteger minItems;

public MinItemsKeyword(final JsonNumber minItems) {
this.minItems = minItems.intValue();
public MinItemsKeyword(final BigInteger minItems) {
this.minItems = minItems;
}

@Override
Expand All @@ -60,7 +60,7 @@ public <T extends Media<T>> T printOn(final T media) {

@Override
public boolean isValidFor(final JsonValue instance) {
return !InstanceType.ARRAY.isInstance(instance) || instance.asJsonArray().size() >= minItems;
return !InstanceType.ARRAY.isInstance(instance) || instance.asJsonArray().size() >= minItems.intValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public MinLengthKeyword(final JsonNumber value) {

@Override
public <T extends Media<T>> T printOn(final T media) {
return media.withValue(name(), value.longValue());
return media.withValue(name(), value.bigIntegerValueExact());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
package io.github.sebastiantoepfer.jsonschema.core.vocab.validation;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasEntry;
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.keyword.Keyword;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;

class EnumKeywordTypeTest {
Expand Down Expand Up @@ -97,4 +101,22 @@ void should_be_valid_for_decimal_without_scale_if_number_is_valid() {
is(true)
);
}

@Test
void should_be_printable() {
assertThat(
new EnumKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory()
.create(
Json
.createObjectBuilder()
.add("enum", Json.createArrayBuilder().add("TEST").add("VALID"))
.build()
)
)
.printOn(new HashMapMedia()),
(Matcher) hasEntry(is("enum"), containsInAnyOrder("TEST", "VALID"))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
package io.github.sebastiantoepfer.jsonschema.core.vocab.validation;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import java.math.BigDecimal;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -111,4 +115,17 @@ void shhould_be_valid_for_smaller_numbers() {
is(true)
);
}

@Test
void should_be_printable() {
assertThat(
new ExclusiveMaximumKeywordType()
.createKeyword(
new DefaultJsonSchemaFactory()
.create(Json.createObjectBuilder().add("exclusiveMaximum", Json.createValue(10)).build())
)
.printOn(new HashMapMedia()),
(Matcher) hasEntry(is("exclusiveMaximum"), is(BigDecimal.valueOf(10)))
);
}
}
Loading

0 comments on commit ba2cc0c

Please sign in to comment.