diff options
author | 2018-04-10 15:33:46 -0400 | |
---|---|---|
committer | 2018-04-10 16:02:37 -0400 | |
commit | fd3287187e487d8b0789484def296074e985b7cb (patch) | |
tree | db5e50544ac9fa515be5a647a8489815131ab711 /javatoscachecker/kwalify/src/test/java | |
parent | dc3ecc22915249d0fb542ab23b400abd6b5ef620 (diff) |
Add more unit testing
Unit testing for kwalify parser and validator and for domain model representation.
Fix oparent reference in pom in datse format in docker image tag.
Change-Id: Icca11ae7fc773cae3de910acb10fcacd51b909a4
Signed-off-by: Serban Jora <sj2381@att.com>
Issue-ID: MODELING-53
Signed-off-by: Serban Jora <sj2381@att.com>
Diffstat (limited to 'javatoscachecker/kwalify/src/test/java')
-rw-r--r-- | javatoscachecker/kwalify/src/test/java/kwalify/test/ParserTest.java | 300 | ||||
-rw-r--r-- | javatoscachecker/kwalify/src/test/java/kwalify/test/ValidatorTest.java | 150 |
2 files changed, 450 insertions, 0 deletions
diff --git a/javatoscachecker/kwalify/src/test/java/kwalify/test/ParserTest.java b/javatoscachecker/kwalify/src/test/java/kwalify/test/ParserTest.java new file mode 100644 index 0000000..1983619 --- /dev/null +++ b/javatoscachecker/kwalify/src/test/java/kwalify/test/ParserTest.java @@ -0,0 +1,300 @@ +/* + * Copyright (c) 2017 <AT&T>. All rights reserved. + * =================================================================== + * 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 kwalify.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.FixMethodOrder; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Set; +import java.util.Map; +import java.util.List; +import java.util.Arrays; +import java.io.InputStream; +import java.io.IOException; + +import kwalify.PlainYamlParser; +import kwalify.SyntaxException; +import kwalify.Util; + + +/* + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ParserTest { + + @BeforeClass + public static void initialize() throws Exception { + } + + private Object parse(String theResource) throws SyntaxException { + Object output = null; + InputStream source = getClass().getClassLoader().getResourceAsStream(theResource); + if (source == null) { + fail("Cannot find test resource " + theResource); + } + + try { + output = new PlainYamlParser( + Util.readInputStream(source)) + .parse(); + } + catch (IOException iox) { + fail("Failed to load test resource " + theResource + ": " + iox); + } + return output; + } + + @Test + public void testParserSuccess() { + try { + assertTrue( + parse("parserSuccess.yml") instanceof Map); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx); + } + } + + + + @Test + public void testParserFailOnInvalidAnchor() { + try { + parse("parserFailInvalidAnchor.yml"); + } + catch (SyntaxException sx) { + assertTrue(sx.getMessage(), sx.getMessage().matches("anchor '.*' not found.")); + assertTrue("" + sx.getLineNumber(), sx.getLineNumber() == 25); + } + } + + @Test + public void testParserFailIndentation() { + try { + parse("parserFailIndentation.yml"); + } + catch (SyntaxException sx) { + assertTrue(sx.getMessage(), sx.getMessage().matches("invalid indent of mapping.")); + assertTrue("" + sx.getLineNumber(), sx.getLineNumber() == 28); + } + } + + @Test + public void testParserSeqOfScalar() { + try { + assertTrue( + parse("parserSuccessSeqOfScalar.yml") instanceof List); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + } + + @Test + public void testParserSeqOfMap() { + Object res = null; + try { + res = parse("parserSuccessSeqOfMap.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue(res instanceof List); + for (Object entry: (List)res) + assertTrue(entry instanceof Map); + } + + //@Test + public void testParserSeqOfSeq() { + Object res = null; + try { + res = parse("parserSuccessSeqOfSeq.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue(res instanceof List); + for (Object entry: (List)res) + assertTrue(entry instanceof List); + } + + @Test + public void testParserMapScalarToScalar() { + Object res = null; + try { + res = parse("parserSuccessMapScalarToScalar.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue(res instanceof Map); + } + + @Test + public void testParserMapScalarToSeq() { + Object res = null; + try { + res = parse("parserSuccessMapScalarToSeq.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue(res instanceof Map); + for (Object value: ((Map)res).values()) + assertTrue(value instanceof List); + } + + @Test + public void testParserMapOfMap() { + Object res = null; + try { + res = parse("parserSuccessMapOfMap.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue("result not a map: " + res.getClass() + ", " + res, res instanceof Map); + for (Object value: ((Map)res).values()) + assertTrue("value not a map, " + value.getClass() + ", " + value, value instanceof Map); + } + + //@Test + //not supported + public void testParserMapOfSeqToSeq() { + Object res = null; + try { + res = parse("parserSuccessMapOfSeqToSeq.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue("result not a map: " + res.getClass() + ", " + res, res instanceof Map); + for (Map.Entry entry: (Set<Map.Entry>)((Map)res).entrySet()) { + assertTrue(entry.getKey() instanceof List); + assertTrue(entry.getValue() instanceof List); + } + } + + //@Test + //not supported + public void testParserUnorderedSet() { + Object res = null; + try { + res = parse("parserSuccessUnorderedSet.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue("result not a set: " + res.getClass() + ", " + res, res instanceof Set); + } + + @Test + public void testParserOrderedMap() { + Object res = null; + try { + res = parse("parserSuccessOrderedMap.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue("result not a list: " + res.getClass() + ", " + res, res instanceof List); + } + + @Test + public void testParserFloats() { + Object res = null; + try { + res = parse("parserSuccessFloats.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue("result not a map: " + res.getClass() + ", " + res, res instanceof Map); + + for (Object value: ((Map)res).values()) + assertTrue("value not encoded as a Double: " + value.getClass() + ", " + value, value instanceof Double); + } + + @Test + public void testParserInts() { + Object res = null; + try { + res = parse("parserSuccessInts.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue("result not a map: " + res.getClass() + ", " + res, res instanceof Map); + + for (Object value: ((Map)res).values()) + assertTrue("value not encoded as a Integer: " + value.getClass() + ", " + value, value instanceof Integer); + } + + @Test + public void testParserQuotedStrings() { + Object res = null; + try { + res = parse("parserSuccessQuotedStrings.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue("result not a map: " + res.getClass() + ", " + res, res instanceof Map); + + for (Object value: ((Map)res).values()) + assertTrue("value not encoded as a String: " + value.getClass() + ", " + value, value instanceof String); + } + + @Test + public void testParserTimestamps() { + Object res = null; + try { + res = parse("parserSuccessTimestamps.yml"); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx + " at line " + sx.getLineNumber()); + } + assertTrue("result not a map: " + res.getClass() + ", " + res, res instanceof Map); + + for (Object value: ((Map)res).values()) + assertTrue("value not encoded as a Date: " + value.getClass() + ", " + value, value instanceof java.util.Date); + } + + @Test + public void testMultipleDocs() throws SyntaxException { + Object[] output = null; + InputStream source = getClass().getClassLoader().getResourceAsStream("parserSuccessMultipleDocs.yml"); + if (source == null) { + fail("Cannot find test resource parserSuccessMultipleDocs.yml"); + } + + try { + output = new PlainYamlParser( + Util.readInputStream(source)) + .parseAll(); + } + catch (IOException iox) { + fail("Failed to load test resource parserSuccessMultipleDocs.yml: " + iox); + } + assertTrue("Unexpected output " + output == null ? "null" : Arrays.toString(output), output != null && output.length == 2); + } +} diff --git a/javatoscachecker/kwalify/src/test/java/kwalify/test/ValidatorTest.java b/javatoscachecker/kwalify/src/test/java/kwalify/test/ValidatorTest.java new file mode 100644 index 0000000..e5c2735 --- /dev/null +++ b/javatoscachecker/kwalify/src/test/java/kwalify/test/ValidatorTest.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2017 <AT&T>. All rights reserved. + * =================================================================== + * 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 kwalify.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.FixMethodOrder; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Map; +import java.util.List; +import java.io.InputStream; +import java.io.IOException; + +import kwalify.PlainYamlParser; +import kwalify.SyntaxException; +import kwalify.SchemaException; +import kwalify.Util; +import kwalify.Validator; + + +/* + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ValidatorTest { + + @BeforeClass + public static void initialize() throws Exception { + } + + protected Object[] prepareValidateTest(String theName) { + Object[] res = new Object[3]; + InputStream source = null; + + source = getClass().getClassLoader().getResourceAsStream(theName + "Schema.yml"); + if (source == null) { + fail("Cannot find test resource " + theName + "Schema.yml"); + } + + try { + res[0] = new PlainYamlParser( + Util.readInputStream(source)) + .parse(); + } + catch (IOException iox) { + fail("Failed to load test resource " + iox); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx); + } + + source = getClass().getClassLoader().getResourceAsStream(theName + ".yml"); + if (source == null) { + fail("Cannot find test resource " + theName + ".yml"); + } + try { + res[1] = new PlainYamlParser( + Util.readInputStream(source)) + .parse(); + } + catch (IOException iox) { + fail("Failed to load test resource " + iox); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx); + } + + source = getClass().getClassLoader().getResourceAsStream("in" + theName + ".yml"); + if (source == null) { + fail("Cannot find test resource in" + theName + ".yml"); + } + try { + res[2] = new PlainYamlParser( + Util.readInputStream(source)) + .parse(); + } + catch (IOException iox) { + fail("Failed to load test resource " + iox); + } + catch (SyntaxException sx) { + fail("Unexpected syntax error " + sx); + } + + return res; + } + + protected void runValidateTest(String theTestName) { + Object[] docs = prepareValidateTest(theTestName); + List errors = null; + try { + errors = new Validator(docs[0]).validate(docs[1]); + } + catch (SchemaException sx) { + fail("Invalid schema " + sx); + } + + assertTrue(errors.toString(), errors != null && errors.size() == 0); + + try { + errors = new Validator(docs[0]).validate(docs[2]); + } + catch (SchemaException sx) { + fail("Invalid schema " + sx); + } + + assertTrue(errors.toString(), errors != null && errors.size() > 0); + } + + @Test + public void testMap() { + runValidateTest("validateMap"); + } + + @Test + public void testSeq() { + runValidateTest("validateSeq"); + } + + @Test + public void testMapOfSeq() { + runValidateTest("validateMapOfSeq"); + } + + @Test + public void testSeqOfMap() { + runValidateTest("validateSeqOfMap"); + } + + @Test + public void testRule() { + runValidateTest("validateRule"); + } +} |