Skip to content

Updates tests from JSON Schema Test Suite #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,48 @@ public class BaseJsonSchemaValidatorTest {

private static final ObjectMapper mapper = new ObjectMapper();

protected JsonNode getJsonNodeFromClasspath(String name) throws IOException {
public static JsonNode getJsonNodeFromClasspath(String name) throws IOException {
InputStream is1 = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(name);
return mapper.readTree(is1);
}

protected JsonNode getJsonNodeFromStringContent(String content) throws IOException {
public static JsonNode getJsonNodeFromStringContent(String content) throws IOException {
return mapper.readTree(content);
}

protected JsonNode getJsonNodeFromUrl(String url) throws IOException {
public static JsonNode getJsonNodeFromUrl(String url) throws IOException {
return mapper.readTree(new URL(url));
}

protected static JsonSchema getJsonSchemaFromClasspath(String name) {
public static JsonSchema getJsonSchemaFromClasspath(String name) {
return getJsonSchemaFromClasspath(name, SpecVersion.VersionFlag.V4);
}

protected static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion) {
public static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(schemaVersion);
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(name);
return factory.getSchema(is);
}

protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
public static JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(schemaContent);
}

protected JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException {
public static JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(new URI(uri));
}

protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) {
public static JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(jsonNode);
}

// Automatically detect version for given JsonNode
protected JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
public static JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNode));
return factory.getSchema(jsonNode);
}
Expand Down
64 changes: 18 additions & 46 deletions src/test/java/com/networknt/schema/Issue619Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import io.undertow.Undertow;
import io.undertow.server.handlers.resource.FileResourceManager;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.net.URI;

import static io.undertow.Handlers.resource;
import static org.junit.jupiter.api.Assertions.*;
import static com.networknt.schema.BaseJsonSchemaValidatorTest.getJsonNodeFromStringContent;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class Issue619Test extends BaseJsonSchemaValidatorTest {
public class Issue619Test extends HTTPServiceSupport {

private JsonSchemaFactory factory;
private JsonNode one;
Expand Down Expand Up @@ -116,51 +117,22 @@ public void uriThatPointsToNodeThatInTurnReferencesOneShouldOnlyValidateOne_Uri(

@Test
public void uriThatPointsToSchemaWithIdThatHasDifferentUri_Ref() throws Exception {
runLocalServer(() -> {
JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");

JsonSchema schemaWithIdFromRef = factory.getSchema("{ \"$ref\": \"resource:tests/draft4/refRemote.json#/3/schema\" }");
assertTrue(schemaWithIdFromRef.validate(oneArray).isEmpty());
assertFalse(schemaWithIdFromRef.validate(textArray).isEmpty());
});
JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");

JsonSchema schemaWithIdFromRef = factory.getSchema("{ \"$ref\": \"resource:tests/draft4/refRemote.json#/3/schema\" }");
assertTrue(schemaWithIdFromRef.validate(oneArray).isEmpty());
assertFalse(schemaWithIdFromRef.validate(textArray).isEmpty());
}

@Test
public void uriThatPointsToSchemaWithIdThatHasDifferentUri_Uri() throws Exception {
runLocalServer(() -> {
JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");

JsonSchema schemaWithIdFromUri = factory.getSchema(new URI("resource:tests/draft4/refRemote.json#/3/schema"));
assertTrue(schemaWithIdFromUri.validate(oneArray).isEmpty());
assertFalse(schemaWithIdFromUri.validate(textArray).isEmpty());
});
}
JsonNode oneArray = getJsonNodeFromStringContent("[[1]]");
JsonNode textArray = getJsonNodeFromStringContent("[[\"a\"]]");

private interface ThrowingRunnable {
void run() throws Exception;
}

private void runLocalServer(ThrowingRunnable actualTest) throws Exception {
Undertow server = Undertow.builder()
.addHttpListener(1234, "localhost")
.setHandler(resource(new FileResourceManager(
new File("./src/test/resources/remotes"), 100)))
.build();
try {
server.start();

actualTest.run();

} finally {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
server.stop();
}
JsonSchema schemaWithIdFromUri = factory.getSchema(new URI("resource:tests/draft4/refRemote.json#/3/schema"));
assertTrue(schemaWithIdFromUri.validate(oneArray).isEmpty());
assertFalse(schemaWithIdFromUri.validate(textArray).isEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.networknt.schema;

import com.networknt.schema.SpecVersion.VersionFlag;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicNode;
import org.junit.jupiter.api.TestFactory;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

@DisplayName("JSON Schema Test Suite Extras")
class JsonSchemaTestSuiteExtrasTest extends AbstractJsonSchemaTestSuite {

private static final Path excluded = Paths.get("src/test/resources/draft4/relativeRefRemote.json");

@TestFactory
@DisplayName("Draft 2020-12")
Stream<DynamicNode> draft2022012() {
return createTests(VersionFlag.V202012, "src/test/resources/draft2020-12");
}

@TestFactory
@DisplayName("Draft 2019-09")
Stream<DynamicNode> draft201909() {
return createTests(VersionFlag.V201909, "src/test/resources/draft2019-09");
}

@TestFactory
@DisplayName("Draft 7")
Stream<DynamicNode> draft7() {
return createTests(VersionFlag.V7, "src/test/resources/draft7");
}

@TestFactory
@DisplayName("Draft 6")
Stream<DynamicNode> draft6() {
return createTests(VersionFlag.V6, "src/test/resources/draft6");
}

@TestFactory
@DisplayName("Draft 4")
Stream<DynamicNode> draft4() {
return createTests(VersionFlag.V4, "src/test/resources/draft4");
}

@Override
protected boolean enabled(Path path) {
return !excluded.equals(path);
}

}
Loading