diff options
author | efiacor <fiachra.corcoran@est.tech> | 2019-07-10 15:02:29 +0000 |
---|---|---|
committer | efiacor <fiachra.corcoran@est.tech> | 2019-07-10 15:02:29 +0000 |
commit | 98572b78fcce9ff28fa7429c9265812bd1e78bf2 (patch) | |
tree | e5f14bf7b083f543435634be6fee442882024827 /datarouter-node/src | |
parent | cec9a9227c805ff5415d6b9fd913fa64adafdd3a (diff) |
Adding more DR-Node unit tests
Change-Id: I57b1c7aa678188136ecf84be53e0811908091f1a
Issue-ID: DMAAP-1226
Signed-off-by: efiacor <fiachra.corcoran@est.tech>
Diffstat (limited to 'datarouter-node/src')
8 files changed, 169 insertions, 42 deletions
diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/PathFinder.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/PathFinder.java index d86b1e4d..fe3fdb6e 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/PathFinder.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/PathFinder.java @@ -35,7 +35,7 @@ import org.onap.dmaap.datarouter.node.NodeConfig.ProvHop; * get from this node to any other node. */ -public class PathFinder { +class PathFinder { private ArrayList<String> errors = new ArrayList<>(); private HashMap<String, String> routes = new HashMap<>(); @@ -47,7 +47,7 @@ public class PathFinder { * @param nodes where we can go * @param hops detours along the way */ - public PathFinder(String origin, String[] nodes, NodeConfig.ProvHop[] hops) { + PathFinder(String origin, String[] nodes, NodeConfig.ProvHop[] hops) { HashSet<String> known = new HashSet<>(); HashMap<String, HashMap<String, Hop>> ht = new HashMap<>(); for (String n : nodes) { @@ -77,8 +77,8 @@ public class PathFinder { * * @return array of error descriptions */ - public String[] getErrors() { - return (errors.toArray(new String[errors.size()])); + String[] getErrors() { + return (errors.toArray(new String[0])); } /** @@ -87,7 +87,7 @@ public class PathFinder { * @param destination node * @return list of node names separated by and ending with "/" */ - public String getPath(String destination) { + String getPath(String destination) { String ret = routes.get(destination); if (ret == null) { return (""); diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/RedirManager.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/RedirManager.java index f501583a..b4a3f0a7 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/RedirManager.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/RedirManager.java @@ -37,10 +37,10 @@ import java.util.Timer; /** * Track redirections of subscriptions. */ -public class RedirManager { +class RedirManager { private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(RedirManager.class); - RateLimitedOperation op; + private RateLimitedOperation op; private HashMap<String, String> sid2primary = new HashMap<>(); private HashMap<String, String> sid2secondary = new HashMap<>(); private String redirfile; @@ -52,7 +52,7 @@ public class RedirManager { * @param mininterval The minimum number of milliseconds between writes to the redirection information file. * @param timer The timer thread used to run delayed file writes. */ - public RedirManager(String redirfile, long mininterval, Timer timer) { + RedirManager(String redirfile, long mininterval, Timer timer) { this.redirfile = redirfile; op = new RateLimitedOperation(mininterval, timer) { public void run() { @@ -92,7 +92,7 @@ public class RedirManager { * @param primary The URL associated with that subscription ID * @param secondary The replacement URL to use instead */ - public synchronized void redirect(String sid, String primary, String secondary) { + synchronized void redirect(String sid, String primary, String secondary) { sid2primary.put(sid, primary); sid2secondary.put(sid, secondary); op.request(); @@ -103,7 +103,7 @@ public class RedirManager { * * @param sid The subscription ID to remove from the table. */ - public synchronized void forget(String sid) { + synchronized void forget(String sid) { sid2primary.remove(sid); sid2secondary.remove(sid); op.request(); @@ -117,7 +117,7 @@ public class RedirManager { * @param primary The configured primary URL. * @return The destination URL to really use. */ - public synchronized String lookup(String sid, String primary) { + synchronized String lookup(String sid, String primary) { String oprim = sid2primary.get(sid); if (primary.equals(oprim)) { return (sid2secondary.get(sid)); @@ -130,7 +130,7 @@ public class RedirManager { /** * Is a subscription redirected. */ - public synchronized boolean isRedirected(String sid) { + synchronized boolean isRedirected(String sid) { return (sid != null && sid2secondary.get(sid) != null); } 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 |