diff --git a/src/test/java/com/networknt/schema/BaseSuiteJsonSchemaTest.java b/src/test/java/com/networknt/schema/BaseSuiteJsonSchemaTest.java new file mode 100644 index 000000000..383ff8fd0 --- /dev/null +++ b/src/test/java/com/networknt/schema/BaseSuiteJsonSchemaTest.java @@ -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 errors = new ArrayList(); + + 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); + } + } + } +} diff --git a/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java b/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java index 8d093a45b..817a198e5 100644 --- a/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java +++ b/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java @@ -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 errors = new ArrayList(); - - 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 diff --git a/src/test/java/com/networknt/schema/V4JsonSchemaTest.java b/src/test/java/com/networknt/schema/V4JsonSchemaTest.java index e97187d6a..5acefa36b 100644 --- a/src/test/java/com/networknt/schema/V4JsonSchemaTest.java +++ b/src/test/java/com/networknt/schema/V4JsonSchemaTest.java @@ -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 errors = new ArrayList(); - - 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*/) diff --git a/src/test/java/com/networknt/schema/V6JsonSchemaTest.java b/src/test/java/com/networknt/schema/V6JsonSchemaTest.java index dfb3411be..65dcca390 100644 --- a/src/test/java/com/networknt/schema/V6JsonSchemaTest.java +++ b/src/test/java/com/networknt/schema/V6JsonSchemaTest.java @@ -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 V6JsonSchemaTest { - protected ObjectMapper mapper = new ObjectMapper(); - protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V6)).objectMapper(mapper).build(); - protected static Undertow server = null; - +public class V6JsonSchemaTest extends BaseSuiteJsonSchemaTest { public V6JsonSchemaTest() { - } - - @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 errors = new ArrayList(); - - 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.V6); } @Test diff --git a/src/test/java/com/networknt/schema/V7JsonSchemaTest.java b/src/test/java/com/networknt/schema/V7JsonSchemaTest.java index 3885f584c..8890b27f2 100644 --- a/src/test/java/com/networknt/schema/V7JsonSchemaTest.java +++ b/src/test/java/com/networknt/schema/V7JsonSchemaTest.java @@ -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 V7JsonSchemaTest { - protected ObjectMapper mapper = new ObjectMapper(); - protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build(); - protected static Undertow server = null; - +public class V7JsonSchemaTest extends BaseSuiteJsonSchemaTest { public V7JsonSchemaTest() { - } - - @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 errors = new ArrayList(); - - 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.V7); } @Test