diff options
Diffstat (limited to 'datarouter-node/src/test')
6 files changed, 157 insertions, 30 deletions
diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeConfigTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeConfigTest.java index 79719243..b03407bf 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeConfigTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeConfigTest.java @@ -35,8 +35,7 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFo import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.node.ProvData", - "org.onap.dmaap.datarouter.node.NodeUtils"}) +@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.node.ProvData"}) public class NodeConfigTest { private static NodeConfig nodeConfig; diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java index 88e57432..2d87b8b9 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeUtilsTest.java @@ -22,27 +22,19 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.node; -import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN; -import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS; import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -import java.io.IOException; -import java.net.InetAddress; -import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.modules.junit4.PowerMockRunner; import org.slf4j.MDC; @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeUtils") -@PrepareForTest({UUID.class, InetAddress.class}) +@PowerMockIgnore({"java.net.ssl", "javax.security.auth.x500.X500Principal"}) public class NodeUtilsTest { @Mock @@ -52,6 +44,7 @@ public class NodeUtilsTest { public void Given_Uri_With_Params_Then_Get_Feed_And_File_Id_Returns_Correct_Values() { String uri = "prov.datarouternew.com:8443/feed/12/fileName"; String[] uriParams = NodeUtils.getFeedAndFileID(uri); + assert uriParams != null; Assert.assertEquals("12", uriParams[0]); Assert.assertEquals("fileName", uriParams[1]); } @@ -85,23 +78,8 @@ public class NodeUtilsTest { } @Test - public void Given_setIpAndFqdnForEelf_Called_Set_MDC_Values() throws IOException { - mockStatic(InetAddress.class); - when(InetAddress.getLocalHost().getHostName()).thenReturn("testHostName"); - when(InetAddress.getLocalHost().getHostAddress()).thenReturn("testHostAddress"); - NodeUtils.setIpAndFqdnForEelf("doGet"); - Assert.assertEquals("testHostName", MDC.get(MDC_SERVER_FQDN)); - Assert.assertEquals("testHostAddress", MDC.get(MDC_SERVER_IP_ADDRESS)); - } - - @Test - public void Given_Request_Has_Empty_RequestId_And_InvocationId_Headers_Generate_MDC_Values() { - when(request.getHeader("X-ONAP-RequestID")).thenReturn(""); - when(request.getHeader("X-InvocationID")).thenReturn(""); - mockStatic(UUID.class); - when(UUID.randomUUID().toString()).thenReturn("123", "456"); - NodeUtils.setRequestIdAndInvocationId(request); - Assert.assertEquals("123", MDC.get("RequestId")); - Assert.assertEquals("456", MDC.get("InvocationId")); + public void Given_Get_CanonicalName_Called_Valid_CN_Returned() { + String canonicalName = NodeUtils.getCanonicalName("jks", "src/test/resources/org.onap.dmaap-dr-test-cert.jks", "WGxd2P6MDo*Bi4+UdzWs{?$8"); + Assert.assertEquals("dmaap-dr-node", canonicalName); } } diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/PathFinderTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/PathFinderTest.java new file mode 100644 index 00000000..25edd0c0 --- /dev/null +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/PathFinderTest.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.dmaap.datarouter.node; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class PathFinderTest { + + @Test + public void Given_Unknown_From_Node_Returns_Null() { + new PathFinder("dr-node-1", new String[]{"dr-node-1", "dr-node-2", "dr-node-3"}, + new NodeConfig.ProvHop[]{new NodeConfig.ProvHop("dr-node-4", "dr-node-3", "dr-node-2")}); + } + + @Test + public void Given_Unknown_Destination_Node_Returns_Null() { + new PathFinder("dr-node-1", new String[]{"dr-node-1", "dr-node-2", "dr-node-3"}, + new NodeConfig.ProvHop[]{new NodeConfig.ProvHop("dr-node-1", "dr-node-5", "dr-node-2")}); + } + + @Test + public void Given_Duplicate_Next_Hop_Returns_Null() { + PathFinder p = new PathFinder("dr-node-1", new String[]{"dr-node-1", "dr-node-2", "dr-node-3"}, + new NodeConfig.ProvHop[]{new NodeConfig.ProvHop("dr-node-1", "dr-node-3", "dr-node-2"), + new NodeConfig.ProvHop("dr-node-1", "dr-node-3", "dr-node-2")}); + assertThat(p.getErrors().length, is(1)); + assertNotNull(p.getPath("dr-node-3")); + assertThat(p.getPath("dr-node-5").length(), is(0)); + } + + @Test + public void Given_Unknown_Via_Node_Returns_Null() { + new PathFinder("dr-node-1", new String[]{"dr-node-1", "dr-node-2", "dr-node-3"}, + new NodeConfig.ProvHop[]{new NodeConfig.ProvHop("dr-node-1", "dr-node-3", "dr-node-4")}); + } + + @Test + public void Given_Dest_Equals_Via_Bad_Hop_Defined() { + new PathFinder("dr-node-1", new String[]{"dr-node-1", "dr-node-2", "dr-node-3"}, + new NodeConfig.ProvHop[]{new NodeConfig.ProvHop("dr-node-1", "dr-node-2", "dr-node-2")}); + } + + @Test + public void Given_Valid_Path_Defined_Success() { + new PathFinder("dr-node-1", new String[]{"dr-node-1", "dr-node-2", "dr-node-3"}, + new NodeConfig.ProvHop[]{new NodeConfig.ProvHop("dr-node-1", "dr-node-3+", "dr-node-2")}); + } + + +} diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/RedirManagerTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/RedirManagerTest.java new file mode 100644 index 00000000..2c8a0e52 --- /dev/null +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/RedirManagerTest.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.dmaap.datarouter.node; + +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.Timer; +import org.hamcrest.core.Is; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class RedirManagerTest { + + private RedirManager redirManager; + private String redirFilePath = System.getProperty("user.dir") + "/src/test/resources/redir_file"; + + @Before + public void setUp() { + Timer timer = new Timer("Node Configuration Timer", true); + redirManager = new RedirManager(redirFilePath, 10000L, timer); + } + + @Test + public void Given_Lookup_On_Valid_Redirect_Returns_Target_URL() { + assertThat(redirManager.lookup("1", "http://destination:8443/path/to"), Is.is("http://redirect:8443/path/to")); + } + + @Test + public void Given_IsRedirected_Called_On_Valid_Sub_Id_Then_Returns_True() { + assertThat(redirManager.isRedirected("1"), Is.is(true)); + } + + @Test + public void Given_Redirect_Called_On_Valid_Redirect_New_Redirect_Added() throws IOException { + long origFileLenght = new File(redirFilePath).length(); + redirManager.redirect("3", "http://destination3:8443/path/to", "http://redirect3:8443/path/to"); + assertThat(redirManager.lookup("3", "http://destination3:8443/path/to"), Is.is("http://redirect3:8443/path/to")); + new RandomAccessFile(redirFilePath, "rw").setLength(origFileLenght); + } + + @Test + public void Given_Lookup_On_Invalid_Redirect_Returns_Primary_Target_URL_And_Is_Forgotten() throws IOException { + assertThat(redirManager.lookup("2", "http://invalid:8443/path/to"), Is.is("http://invalid:8443/path/to")); + Files.write(Paths.get(redirFilePath), "2 http://destination2:8443/path/to http://redirect2:8443/path/to".getBytes(), StandardOpenOption.APPEND); + } +} diff --git a/datarouter-node/src/test/resources/org.onap.dmaap-dr-test-cert.jks b/datarouter-node/src/test/resources/org.onap.dmaap-dr-test-cert.jks Binary files differnew file mode 100644 index 00000000..2320dc9f --- /dev/null +++ b/datarouter-node/src/test/resources/org.onap.dmaap-dr-test-cert.jks diff --git a/datarouter-node/src/test/resources/redir_file b/datarouter-node/src/test/resources/redir_file new file mode 100644 index 00000000..0c72ebe9 --- /dev/null +++ b/datarouter-node/src/test/resources/redir_file @@ -0,0 +1,2 @@ +1 http://destination:8443/path/to http://redirect:8443/path/to +2 http://destination2:8443/path/to http://redirect2:8443/path/to
\ No newline at end of file |