Skip to content

Commit

Permalink
replace own testengine with parameterized test
Browse files Browse the repository at this point in the history
let the code their -> for the case we unterstand how java modules,
pitest and junit-engines work together.
currently we load the test in a parameterized test with a copy
of the resource loading code from the engine (bad i know).
  • Loading branch information
sebastian-toepfer committed Sep 10, 2023
1 parent 8c7de2c commit d10d43d
Show file tree
Hide file tree
Showing 18 changed files with 783 additions and 28 deletions.
31 changes: 27 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>json-schema-testsuite-junit-platform-engine</artifactId>
<version>${project.version}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>

Expand All @@ -46,6 +45,12 @@
<artifactId>jakarta.json-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -104,6 +109,24 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>run-jsonschema-testsuite</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/JsonTestSuiteIT.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core;

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

import jakarta.json.Json;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class InstanceTypeTest {

@Test
void should_be_true_if_instancetype_is_correct() {
assertThat(InstanceType.NULL.isInstance(JsonValue.NULL), is(true));
assertThat(InstanceType.BOOLEAN.isInstance(JsonValue.FALSE), is(true));
assertThat(InstanceType.BOOLEAN.isInstance(JsonValue.TRUE), is(true));
assertThat(InstanceType.OBJECT.isInstance(JsonValue.EMPTY_JSON_OBJECT), is(true));
assertThat(InstanceType.ARRAY.isInstance(JsonValue.EMPTY_JSON_ARRAY), is(true));
assertThat(InstanceType.NUMBER.isInstance(Json.createValue(23L)), is(true));
assertThat(InstanceType.INTEGER.isInstance(Json.createValue(23L)), is(true));
assertThat(InstanceType.INTEGER.isInstance(Json.createValue(0.0)), is(true));
}

@Test
void should_be_false_for_non_integers() {
assertThat(InstanceType.INTEGER.isInstance(Json.createValue(23.2)), is(false));
}

@Test
void should_be_false_for_nonnumbers() {
assertThat(InstanceType.INTEGER.isInstance(Json.createValue("test")), is(false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;

import org.junit.jupiter.api.Test;

class JsonSchemasTest {

@Test
void should_return_a_schema() {
assertThat(JsonSchemas.load("{}"), is(not(nullValue())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import com.github.npathai.hamcrestopt.OptionalMatchers;
import io.github.sebastiantoepfer.jsonschema.core.keyword.KeywordType;
import jakarta.json.JsonValue;
import java.net.URI;
import org.junit.jupiter.api.Test;

Expand All @@ -52,8 +53,11 @@ void should_return_the_type_keywordtype() {
@Test
void should_return_the_custom_annotation_for_unknow_keyword() {
assertThat(
new BasicVocabulary().findKeywordTypeByName("unknow").map(KeywordType::name),
OptionalMatchers.isPresentAndIs("unknow")
new BasicVocabulary()
.findKeywordTypeByName("unknow")
.map(keywordType -> keywordType.createKeyword(JsonValue.FALSE))
.map(keyword -> keyword.hasName("unknow")),
OptionalMatchers.isPresentAndIs(true)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.jsonschema.core.keyword.Assertion;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class TypeTest {
Expand All @@ -40,4 +42,27 @@ void should_know_his_name() {
void should_know_other_names() {
assertThat(new Type(Json.createValue("string")).hasName("id"), is(false));
}

@Test
void should_use_stringvalue_to_validate_type() {
final Assertion typeAssertion = new Type(Json.createValue("string")).asAssertion();

assertThat(typeAssertion.isValidFor(Json.createValue("value")), is(true));
assertThat(typeAssertion.isValidFor(JsonValue.EMPTY_JSON_OBJECT), is(false));
assertThat(typeAssertion.isValidFor(Json.createValue(1L)), is(false));
assertThat(typeAssertion.isValidFor(JsonValue.EMPTY_JSON_ARRAY), is(false));
}

@Test
void should_use_arrayvalue_to_validate_type() {
final Assertion typeAssertion = new Type(
Json.createArrayBuilder().add(Json.createValue("string")).add(Json.createValue("object")).build()
)
.asAssertion();

assertThat(typeAssertion.isValidFor(Json.createValue("value")), is(true));
assertThat(typeAssertion.isValidFor(JsonValue.EMPTY_JSON_OBJECT), is(true));
assertThat(typeAssertion.isValidFor(Json.createValue(1L)), is(false));
assertThat(typeAssertion.isValidFor(JsonValue.EMPTY_JSON_ARRAY), is(false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.impl.spi;

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

import jakarta.json.Json;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class DefaultJsonSchemaFactoryTest {

@Test
void should_return_truejsonschema_for_true() {
assertThat(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), is(instanceOf(TrueJsonSchema.class)));
}

@Test
void should_return_truejsonschema_for_false() {
assertThat(new DefaultJsonSchemaFactory().create(JsonValue.FALSE), is(instanceOf(FalseJsonSchema.class)));
}

@Test
void should_return_emptyjsonschema_for_emptyobject() {
assertThat(
new DefaultJsonSchemaFactory().create(JsonValue.EMPTY_JSON_OBJECT),
is(instanceOf(EmptyJsonSchema.class))
);
}

@Test
void should_return_defaultjsonschema_for_everything_else() {
assertThat(
new DefaultJsonSchemaFactory().create(Json.createObjectBuilder().add("type", "string").build()),
is(instanceOf(DefaultJsonSchema.class))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.impl.spi;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import jakarta.json.Json;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

class DefaultJsonSchemaTest {

private final DefaultJsonSchema schema = new DefaultJsonSchema(
Json.createObjectBuilder().add("type", "string").build()
);

@Test
void should_be_valid_for_string() {
assertThat(schema.validator().validate(Json.createValue("test")), is(empty()));
}

@Test
void should_be_invalid_for_object() {
assertThat(schema.validator().validate(JsonValue.EMPTY_JSON_OBJECT), is(not(empty())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.impl.spi;

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

import jakarta.json.JsonValue;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

class EmptyJsonSchemaTest {

@ParameterizedTest
@ArgumentsSource(JsonValuesArguments.class)
void should_be_valid_for_everything(final JsonValue value) {
assertThat(new EmptyJsonSchema().validator().validate(value), is(empty()));
}
}
Loading

0 comments on commit d10d43d

Please sign in to comment.