Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions src/test/java/com/networknt/schema/BaseSuiteJsonSchemaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2020 Network New Technologies Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import io.undertow.Undertow;
import io.undertow.server.handlers.resource.FileResourceManager;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import static io.undertow.Handlers.resource;
import static org.junit.jupiter.api.Assertions.assertEquals;

public abstract class BaseSuiteJsonSchemaTest {
protected ObjectMapper mapper = new ObjectMapper();
protected JsonSchemaFactory validatorFactory;
protected static Undertow server = null;

protected BaseSuiteJsonSchemaTest(SpecVersion.VersionFlag version) {
validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(version)).objectMapper(mapper).build();
}

@BeforeAll
public static void setUp() {
if (server == null) {
server = Undertow.builder()
.addHttpListener(1234, "localhost")
.setHandler(resource(new FileResourceManager(
new File("./src/test/resources/remotes"), 100)))
.build();
server.start();
}
}

@AfterAll
public static void tearDown() throws Exception {
if (server != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
server.stop();
server = null;
}
}

protected void runTestFile(String testCaseFile) throws Exception {
final URI testCaseFileUri = URI.create("classpath:" + testCaseFile);
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(testCaseFile);
ArrayNode testCases = mapper.readValue(in, ArrayNode.class);

for (int j = 0; j < testCases.size(); j++) {
try {
JsonNode testCase = testCases.get(j);
SchemaValidatorsConfig config = new SchemaValidatorsConfig();

ArrayNode testNodes = (ArrayNode) testCase.get("tests");
for (int i = 0; i < testNodes.size(); i++) {
JsonNode test = testNodes.get(i);
JsonNode node = test.get("data");
JsonNode typeLooseNode = test.get("isTypeLoose");
// Configure the schemaValidator to set typeLoose's value based on the test file,
// if test file do not contains typeLoose flag, use default value: true.
config.setTypeLoose((typeLooseNode == null) ? false : typeLooseNode.asBoolean());
JsonSchema schema = validatorFactory.getSchema(testCaseFileUri, testCase.get("schema"), config);
List<ValidationMessage> errors = new ArrayList<ValidationMessage>();

errors.addAll(schema.validate(node));

if (test.get("valid").asBoolean()) {
if (!errors.isEmpty()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema.toString());
System.out.println("data: " + test.get("data"));
}
assertEquals(0, errors.size());
} else {
if (errors.isEmpty()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema);
System.out.println("data: " + test.get("data"));
} else {
JsonNode errorCount = test.get("errorCount");
if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema);
System.out.println("data: " + test.get("data"));
System.out.println("errors: " + errors);
assertEquals(errorCount.asInt(), errors.size(), "expected error count");
}
}
assertEquals(false, errors.isEmpty());
}
}
} catch (JsonSchemaException e) {
throw new IllegalStateException(String.format("Current schema should not be invalid: %s", testCaseFile), e);
}
}
}
}
102 changes: 2 additions & 100 deletions src/test/java/com/networknt/schema/V201909JsonSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,110 +15,12 @@
*/
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import io.undertow.Undertow;
import io.undertow.server.handlers.resource.FileResourceManager;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import static io.undertow.Handlers.resource;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class V201909JsonSchemaTest {
protected ObjectMapper mapper = new ObjectMapper();
protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).objectMapper(mapper).build();
protected static Undertow server = null;

public class V201909JsonSchemaTest extends BaseSuiteJsonSchemaTest {
public V201909JsonSchemaTest() {
}

@BeforeAll
public static void setUp() {
if (server == null) {
server = Undertow.builder()
.addHttpListener(1234, "localhost")
.setHandler(resource(new FileResourceManager(
new File("./src/test/resources/remotes"), 100)))
.build();
server.start();
}
}

@AfterAll
public static void tearDown() throws Exception {
if (server != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
server.stop();
}
}

private void runTestFile(String testCaseFile) throws Exception {
final URI testCaseFileUri = URI.create("classpath:" + testCaseFile);
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(testCaseFile);
ArrayNode testCases = mapper.readValue(in, ArrayNode.class);

for (int j = 0; j < testCases.size(); j++) {
try {
JsonNode testCase = testCases.get(j);
SchemaValidatorsConfig config = new SchemaValidatorsConfig();

ArrayNode testNodes = (ArrayNode) testCase.get("tests");
for (int i = 0; i < testNodes.size(); i++) {
JsonNode test = testNodes.get(i);
JsonNode node = test.get("data");
JsonNode typeLooseNode = test.get("isTypeLoose");
// Configure the schemaValidator to set typeLoose's value based on the test file,
// if test file do not contains typeLoose flag, use default value: true.
config.setTypeLoose((typeLooseNode == null) ? false : typeLooseNode.asBoolean());
JsonSchema schema = validatorFactory.getSchema(testCaseFileUri, testCase.get("schema"), config);
List<ValidationMessage> errors = new ArrayList<ValidationMessage>();

errors.addAll(schema.validate(node));

if (test.get("valid").asBoolean()) {
if (!errors.isEmpty()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema.toString());
System.out.println("data: " + test.get("data"));
}
assertEquals(0, errors.size());
} else {
if (errors.isEmpty()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema);
System.out.println("data: " + test.get("data"));
} else {
JsonNode errorCount = test.get("errorCount");
if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema);
System.out.println("data: " + test.get("data"));
System.out.println("errors: " + errors);
assertEquals(errorCount.asInt(), errors.size(), "expected error count");
}
}
assertEquals(false, errors.isEmpty());
}
}
} catch (JsonSchemaException e) {
throw new IllegalStateException(String.format("Current schema should not be invalid: %s", testCaseFile), e);
}
}
super(SpecVersion.VersionFlag.V201909);
}

@Test
Expand Down
97 changes: 2 additions & 95 deletions src/test/java/com/networknt/schema/V4JsonSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,110 +18,17 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import io.undertow.Undertow;
import io.undertow.server.handlers.resource.FileResourceManager;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static io.undertow.Handlers.resource;
import static org.junit.jupiter.api.Assertions.*;

public class V4JsonSchemaTest {
protected ObjectMapper mapper = new ObjectMapper();
protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).objectMapper(mapper).build();
protected static Undertow server = null;

public class V4JsonSchemaTest extends BaseSuiteJsonSchemaTest {
public V4JsonSchemaTest() {
}

@BeforeAll
public static void setUp() {
if (server == null) {
server = Undertow.builder()
.addHttpListener(1234, "localhost")
.setHandler(resource(new FileResourceManager(
new File("./src/test/resources/remotes"), 100)))
.build();
server.start();
}
}

@AfterAll
public static void tearDown() throws Exception {
if (server != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
server.stop();
}
}

private void runTestFile(String testCaseFile) throws Exception {
final URI testCaseFileUri = URI.create("classpath:" + testCaseFile);
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(testCaseFile);
ArrayNode testCases = mapper.readValue(in, ArrayNode.class);

for (int j = 0; j < testCases.size(); j++) {
try {
JsonNode testCase = testCases.get(j);
SchemaValidatorsConfig config = new SchemaValidatorsConfig();

ArrayNode testNodes = (ArrayNode) testCase.get("tests");
for (int i = 0; i < testNodes.size(); i++) {
JsonNode test = testNodes.get(i);
JsonNode node = test.get("data");
JsonNode typeLooseNode = test.get("isTypeLoose");
// Configure the schemaValidator to set typeLoose's value based on the test file,
// if test file do not contains typeLoose flag, use default value: true.
config.setTypeLoose((typeLooseNode == null) ? false : typeLooseNode.asBoolean());
JsonSchema schema = validatorFactory.getSchema(testCaseFileUri, testCase.get("schema"), config);
List<ValidationMessage> errors = new ArrayList<ValidationMessage>();

errors.addAll(schema.validate(node));

if (test.get("valid").asBoolean()) {
if (!errors.isEmpty()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema.toString());
System.out.println("data: " + test.get("data"));
}
assertEquals(0, errors.size());
} else {
if (errors.isEmpty()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema);
System.out.println("data: " + test.get("data"));
} else {
JsonNode errorCount = test.get("errorCount");
if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema);
System.out.println("data: " + test.get("data"));
System.out.println("errors: " + errors);
assertEquals(errorCount.asInt(), errors.size(), "expected error count");
}
}
assertEquals(false, errors.isEmpty());
}
}
} catch (JsonSchemaException e) {
throw new IllegalStateException(String.format("Current schema should not be invalid: %s", testCaseFile), e);
}
}
super(SpecVersion.VersionFlag.V4);
}

@Test(/*expected = java.lang.StackOverflowError.class*/)
Expand Down
Loading