Add tests for XPathReader 95/109695/2
authorToineSiebelink <toine.siebelink@est.tech>
Tue, 30 Jun 2020 15:27:05 +0000 (16:27 +0100)
committerToineSiebelink <toine.siebelink@est.tech>
Tue, 30 Jun 2020 15:56:45 +0000 (16:56 +0100)
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>
core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/xml/XPathReaderTest.java [new file with mode: 0644]
core/core-infrastructure/src/test/resources/xmlTestFile.xml [new file with mode: 0644]

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 (file)
index 0000000..a3395ae
--- /dev/null
@@ -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 (file)
index 0000000..73359a1
--- /dev/null
@@ -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>