From 98572b78fcce9ff28fa7429c9265812bd1e78bf2 Mon Sep 17 00:00:00 2001 From: efiacor Date: Wed, 10 Jul 2019 15:02:29 +0000 Subject: [PATCH] Adding more DR-Node unit tests Change-Id: I57b1c7aa678188136ecf84be53e0811908091f1a Issue-ID: DMAAP-1226 Signed-off-by: efiacor --- .../org/onap/dmaap/datarouter/node/PathFinder.java | 10 +-- .../onap/dmaap/datarouter/node/RedirManager.java | 14 ++-- .../onap/dmaap/datarouter/node/NodeConfigTest.java | 3 +- .../onap/dmaap/datarouter/node/NodeUtilsTest.java | 34 ++-------- .../onap/dmaap/datarouter/node/PathFinderTest.java | 75 +++++++++++++++++++++ .../dmaap/datarouter/node/RedirManagerTest.java | 73 ++++++++++++++++++++ .../test/resources/org.onap.dmaap-dr-test-cert.jks | Bin 0 -> 3647 bytes datarouter-node/src/test/resources/redir_file | 2 + .../provisioning/beans/Subscription.java | 9 ++- .../provisioning/SubscribeServletTest.java | 44 +++++------- .../provisioning/SubscriptionServletTest.java | 15 +++++ .../provisioning/beans/SubscriptionTest.java | 52 ++++++++++++-- 12 files changed, 252 insertions(+), 79 deletions(-) create mode 100644 datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/PathFinderTest.java create mode 100644 datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/RedirManagerTest.java create mode 100644 datarouter-node/src/test/resources/org.onap.dmaap-dr-test-cert.jks create mode 100644 datarouter-node/src/test/resources/redir_file 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 errors = new ArrayList<>(); private HashMap 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 known = new HashSet<>(); HashMap> 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 sid2primary = new HashMap<>(); private HashMap 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 new file mode 100644 index 0000000000000000000000000000000000000000..2320dc9fe6b64cfcf7e3a9476ef868ed6198f059 GIT binary patch literal 3647 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 u+iMKT3AsAjR^x}C>Kf0=1qu?I=b0#_r! list = new ArrayList<>(); - list.add("{}"); - PowerMockito.when(Subscription.getSubscriptionUrlList(anyInt())).thenReturn(list); subscribeServlet.doGet(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); verifyEnteringExitCalled(listAppender); @@ -294,8 +288,6 @@ public class SubscribeServletTest extends DrServletTestBase { when(response.getOutputStream()).thenReturn(outStream); when(request.getPathInfo()).thenReturn("/2"); when(request.isUserInRole("org.onap.dmaap-dr.feed|*|approveSub")).thenReturn(true); - PowerMockito.mockStatic(Subscription.class); - PowerMockito.when(Subscription.getSubscriptionMatching(new Subscription())).thenReturn(null); JSONObject JSObject = buildRequestJsonObject(); SubscribeServlet subscribeServlet = new SubscribeServlet() { protected JSONObject getJSONfromInput(HttpServletRequest req) { @@ -353,8 +345,6 @@ public class SubscribeServletTest extends DrServletTestBase { @Test public void Given_Request_Is_HTTP_POST_And_POST_Fails_Bad_Request_Response_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/2"); - PowerMockito.mockStatic(Subscription.class); - PowerMockito.when(Subscription.getSubscriptionMatching(new Subscription())).thenReturn(null); JSONObject JSObject = buildRequestJsonObject(); SubscribeServlet subscribeServlet = new SubscribeServlet() { protected JSONObject getJSONfromInput(HttpServletRequest req) { diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServletTest.java index a17e23e0..4a410ddd 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServletTest.java @@ -323,6 +323,7 @@ public class SubscriptionServletTest extends DrServletTestBase { subscriptionServlet.doPut(request, response); verify(response).setStatus(eq(HttpServletResponse.SC_OK)); resetAafSubscriptionInDB(); + addNewSubscriptionInDB(); verifyEnteringExitCalled(listAppender); } @@ -627,4 +628,18 @@ public class SubscriptionServletTest extends DrServletTestBase { subscription.setPrivilegedSubscriber(false); subscription.doUpdate(db.getConnection()); } + + private void addNewSubscriptionInDB() throws SQLException { + Subscription subscription = new Subscription("https://172.100.0.6:8080", "user3", "password3"); + subscription.setSubid(3); + subscription.setSubscriber("user3"); + subscription.setFeedid(1); + SubDelivery subDelivery = new SubDelivery(URL, USER, PASSWORD, true); + subscription.setDelivery(subDelivery); + subscription.setGroupid(1); + subscription.setMetadataOnly(false); + subscription.setSuspended(false); + subscription.setDecompress(false); + subscription.doInsert(db.getConnection()); + } } \ No newline at end of file diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/SubscriptionTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/SubscriptionTest.java index d859e082..214cc6e7 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/SubscriptionTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/SubscriptionTest.java @@ -23,22 +23,50 @@ package org.onap.dmaap.datarouter.provisioning.beans; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.onap.dmaap.datarouter.provisioning.utils.DB; import org.powermock.modules.junit4.PowerMockRunner; - @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Subscription"}) public class SubscriptionTest { private Subscription subscription; + private static EntityManagerFactory emf; + private static EntityManager em; + private DB db; + + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + @Before + public void setUp() throws Exception { + db = new DB(); + subscription = new Subscription(); + } + @Test public void validate_Subscription_Created_With_Default_Constructor() { - subscription = new Subscription(); Assert.assertEquals(subscription.getSubid(), -1); Assert.assertEquals(subscription.getGroupid(), -1); Assert.assertEquals(subscription.getSubscriber(), ""); @@ -56,13 +84,13 @@ public class SubscriptionTest { subLinks.setLog("log"); subLinks.setSelf("self"); - subscription = new Subscription(); subscription.setGroupid(2); subscription.setDelivery(subDelivery); subscription.setMetadataOnly(false); subscription.setSubscriber(subscriber); subscription.setSuspended(false); subscription.setPrivilegedSubscriber(false); + subscription.setFollowRedirect(true); subscription.setLinks(subLinks); subscription.setDecompress(false); @@ -73,5 +101,19 @@ public class SubscriptionTest { Assert.assertFalse(subscription.isSuspended()); Assert.assertFalse(subscription.isPrivilegedSubscriber()); Assert.assertFalse(subscription.isDecompress()); + + Subscription sub2 = new Subscription(); + sub2.setGroupid(2); + sub2.setDelivery(subDelivery); + sub2.setMetadataOnly(false); + sub2.setSubscriber(subscriber); + sub2.setSuspended(false); + sub2.setPrivilegedSubscriber(false); + sub2.setFollowRedirect(true); + sub2.setLinks(subLinks); + sub2.setDecompress(false); + Assert.assertTrue(subscription.equals(sub2)); + Assert.assertNotNull(sub2.toString()); + sub2.hashCode(); } } \ No newline at end of file -- 2.16.6