From d0799660ee9714a33247d3be2d7841b1cedf75b8 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Tue, 30 Jun 2020 16:27:05 +0100 Subject: 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 --- .../core/infrastructure/xml/XPathReaderTest.java | 74 ++++++++++++++++++++++ .../src/test/resources/xmlTestFile.xml | 20 ++++++ 2 files changed, 94 insertions(+) create mode 100644 core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/xml/XPathReaderTest.java create mode 100644 core/core-infrastructure/src/test/resources/xmlTestFile.xml (limited to 'core/core-infrastructure/src') 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 @@ + + + + This is my name + -- cgit 1.2.3-korg