diff options
author | ToineSiebelink <toine.siebelink@est.tech> | 2020-06-30 16:27:05 +0100 |
---|---|---|
committer | ToineSiebelink <toine.siebelink@est.tech> | 2020-06-30 16:56:45 +0100 |
commit | d0799660ee9714a33247d3be2d7841b1cedf75b8 (patch) | |
tree | e293d471d88837eb066752452ffb43c969cfaaef | |
parent | b3dd71b1d2f4fffe35b4c23d408d836dbcf3114a (diff) |
Add tests for XPathReader
XPathReader had some SQ vulnerabilities whihch have been udpated in previous commit
This is just making sure same code is now covred by testware
Issue-ID: POLICY-2654
Change-Id: Ie40ee015b517b0cc3c5986a67513543653f53a61
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
-rw-r--r-- | core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/xml/XPathReaderTest.java | 74 | ||||
-rw-r--r-- | core/core-infrastructure/src/test/resources/xmlTestFile.xml | 20 |
2 files changed, 94 insertions, 0 deletions
diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/xml/XPathReaderTest.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/xml/XPathReaderTest.java new file mode 100644 index 000000000..a3395ae1f --- /dev/null +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/xml/XPathReaderTest.java @@ -0,0 +1,74 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.infrastructure.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.io.IOException; +import java.net.URL; +import javax.xml.xpath.XPathConstants; +import org.junit.Test; + +public class XPathReaderTest { + + final URL testResource = getClass().getClassLoader().getResource("xmlTestFile.xml"); + + @Test + public void canConstructWithFromFile() { + final XPathReader objectUnderTest = new XPathReader(testResource.getFile()); + assertNotNull(objectUnderTest); + } + + @Test + public void canConstructWithStream() throws IOException { + final XPathReader objectUnderTest = new XPathReader(testResource.openStream()); + assertNotNull(objectUnderTest); + } + + @Test + public void canConstructWithNull() { + final XPathReader objectUnderTest = new XPathReader((String) null); + assertNotNull(objectUnderTest); + } + + @Test + public void canConstructWithInvalidFileName() { + final XPathReader objectUnderTest = new XPathReader(""); + assertNotNull(objectUnderTest); + } + + @Test + public void readReturnsCorrectValueWhenUsingCorrectXpath() throws IOException { + final XPathReader objectUnderTest = new XPathReader(testResource.openStream()); + final String validXPathExpression = "/example/name"; + final Object result = objectUnderTest.read(validXPathExpression, XPathConstants.STRING); + assertEquals("This is my name", result); + } + + @Test + public void readReturnsNullWhenUsingInvalidXpath() throws IOException { + final XPathReader objectUnderTest = new XPathReader(testResource.openStream()); + final String invalidXPathExpression = ""; + assertNull(objectUnderTest.read(invalidXPathExpression, XPathConstants.STRING)); + } + +} diff --git a/core/core-infrastructure/src/test/resources/xmlTestFile.xml b/core/core-infrastructure/src/test/resources/xmlTestFile.xml new file mode 100644 index 000000000..73359a1d6 --- /dev/null +++ b/core/core-infrastructure/src/test/resources/xmlTestFile.xml @@ -0,0 +1,20 @@ +<?xml version='1.0'?> +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2020 Nordix Foundation. + ================================================================================ + 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. + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= +--> +<example> + <name>This is my name</name> +</example> |