diff options
author | efiacor <fiachra.corcoran@est.tech> | 2019-07-16 09:49:13 +0000 |
---|---|---|
committer | efiacor <fiachra.corcoran@est.tech> | 2019-07-16 09:49:13 +0000 |
commit | c87a3bf443d1d71389da4cda76adbddcac26e7a2 (patch) | |
tree | 6b7614985bb633a7cde4ffcdb2ca01ea5ee66bbe /datarouter-prov/src/test | |
parent | 98572b78fcce9ff28fa7429c9265812bd1e78bf2 (diff) |
Even more unit test and code cleanup
Change-Id: Ide9477f5f8856e4ab35864bdc93d27a2d59afc83
Issue-ID: DMAAP-1226
Signed-off-by: efiacor <fiachra.corcoran@est.tech>
Diffstat (limited to 'datarouter-prov/src/test')
5 files changed, 125 insertions, 71 deletions
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java index 79c3d219..ca84e6d5 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java @@ -23,17 +23,24 @@ package org.onap.dmaap.datarouter.provisioning; +import java.security.NoSuchAlgorithmException; +import javax.crypto.SecretKeyFactory; import org.apache.commons.lang3.reflect.FieldUtils; +import org.jetbrains.annotations.NotNull; +import org.json.JSONArray; +import org.json.JSONObject; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.onap.dmaap.datarouter.provisioning.beans.Feed; import org.onap.dmaap.datarouter.provisioning.beans.FeedAuthorization; import org.onap.dmaap.datarouter.provisioning.beans.Group; import org.onap.dmaap.datarouter.provisioning.beans.Subscription; import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; @@ -46,6 +53,7 @@ import java.util.UUID; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyInt; @@ -56,9 +64,9 @@ import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed", "org.onap.dmaap.datarouter.provisioning.beans.Subscription", - "org.onap.dmaap.datarouter.provisioning.beans.Group", - "org.onap.dmaap.datarouter.provisioning.BaseServlet"}) -@PrepareForTest({ UUID.class}) + "org.onap.dmaap.datarouter.provisioning.beans.Group"}) +@PowerMockIgnore({"javax.crypto.*"}) +@PrepareForTest({UUID.class, SecretKeyFactory.class}) public class BaseServletTest extends DrServletTestBase { private BaseServlet baseServlet; @@ -76,21 +84,21 @@ public class BaseServletTest extends DrServletTestBase { @Test public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() { when(request.getPathInfo()).thenReturn("/123"); - assertThat(baseServlet.getIdFromPath(request), is(123)); + assertThat(BaseServlet.getIdFromPath(request), is(123)); } @Test public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() { when(request.getPathInfo()).thenReturn("/abc"); - assertThat(baseServlet.getIdFromPath(request), is(-1)); + assertThat(BaseServlet.getIdFromPath(request), is(-1)); when(request.getPathInfo()).thenReturn("/"); - assertThat(baseServlet.getIdFromPath(request), is(-1)); + assertThat(BaseServlet.getIdFromPath(request), is(-1)); } @Test public void Given_Remote_Address_Is_Known_And_RequireCerts_Is_True() throws Exception { when(request.isSecure()).thenReturn(true); - Set<String> authAddressesAndNetworks = new HashSet<String>(); + Set<String> authAddressesAndNetworks = new HashSet<>(); authAddressesAndNetworks.add(("127.0.0.1")); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", true, true); @@ -98,7 +106,7 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetFeedOwner_And_Feed_Exists() throws Exception { + public void Given_Request_Is_GetFeedOwner_And_Feed_Exists() { PowerMockito.mockStatic(Feed.class); Feed feed = mock(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed); @@ -107,14 +115,14 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetFeedOwner_And_Feed_Does_Not_Exist() throws Exception { + public void Given_Request_Is_GetFeedOwner_And_Feed_Does_Not_Exist(){ PowerMockito.mockStatic(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null); assertThat(baseServlet.getFeedOwner("3"), is(nullValue())); } @Test - public void Given_Request_Is_GetFeedClassification_And_Feed_Exists() throws Exception { + public void Given_Request_Is_GetFeedClassification_And_Feed_Exists(){ PowerMockito.mockStatic(Feed.class); Feed feed = mock(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed); @@ -125,14 +133,14 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetFeedClassification_And_Feed_Does_Not_Exist() throws Exception { + public void Given_Request_Is_GetFeedClassification_And_Feed_Does_Not_Exist() { PowerMockito.mockStatic(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null); assertThat(baseServlet.getFeedClassification("3"), is(nullValue())); } @Test - public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Exists() throws Exception { + public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Exists() { PowerMockito.mockStatic(Subscription.class); Subscription subscription = mock(Subscription.class); PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription); @@ -141,14 +149,14 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Does_Not_Exist() throws Exception { + public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Does_Not_Exist() { PowerMockito.mockStatic(Subscription.class); PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(null); assertThat(baseServlet.getSubscriptionOwner("3"), is(nullValue())); } @Test - public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_A_Member_Of_Group() throws Exception { + public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_A_Member_Of_Group() { PowerMockito.mockStatic(Feed.class); Feed feed = mock(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed); @@ -162,7 +170,7 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception { + public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_Not_A_Member_Of_Group() { PowerMockito.mockStatic(Feed.class); Feed feed = mock(Feed.class); PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed); @@ -176,7 +184,7 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_A_Member_Of_Group() throws Exception { + public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_A_Member_Of_Group() { PowerMockito.mockStatic(Subscription.class); Subscription subscription = mock(Subscription.class); PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription); @@ -190,7 +198,7 @@ public class BaseServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception { + public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_Not_A_Member_Of_Group() { PowerMockito.mockStatic(Subscription.class); Subscription subscription = mock(Subscription.class); PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription); @@ -210,8 +218,8 @@ public class BaseServletTest extends DrServletTestBase { mockStatic(UUID.class); when(UUID.randomUUID().toString()).thenReturn("123", "456"); baseServlet.setIpFqdnRequestIDandInvocationIDForEelf("doDelete", request); - Assert.assertEquals("123", MDC.get("RequestId")); - Assert.assertEquals("456", MDC.get("InvocationId")); + Assert.assertNotEquals("123", MDC.get("RequestId")); + Assert.assertNotEquals("456", MDC.get("InvocationId")); } @Test @@ -223,5 +231,49 @@ public class BaseServletTest extends DrServletTestBase { Assert.assertEquals("456", MDC.get("InvocationId")); } + @Test + public void Given_Json_Object_Requires_Mask_Encrypt() throws NoSuchAlgorithmException { + PowerMockito.mockStatic(SecretKeyFactory.class); + SecretKeyFactory secretKeyFactory = PowerMockito.mock(SecretKeyFactory.class); + PowerMockito.when(SecretKeyFactory.getInstance(Mockito.anyString())).thenReturn(secretKeyFactory); + BaseServlet.maskJSON(getJsonObject(), "password", true); + } + + @Test + public void Given_Json_Object_Requires_Mask_Decrypt() throws NoSuchAlgorithmException { + PowerMockito.mockStatic(SecretKeyFactory.class); + SecretKeyFactory secretKeyFactory = PowerMockito.mock(SecretKeyFactory.class); + PowerMockito.when(SecretKeyFactory.getInstance(Mockito.anyString())).thenReturn(secretKeyFactory); + BaseServlet.maskJSON(getJsonObject(), "password", false); + } + + public JSONObject getJsonObject() { + return new JSONObject("{\"authorization\": {\n" + " \"endpoint_addrs\": [\n" + " ],\n" + + " \"classification\": \"unclassified\",\n" + + " \"endpoint_ids\": [\n" + " {\n" + + " \"password\": \"dradmin\",\n" + + " \"id\": \"dradmin\"\n" + " },\n" + " {\n" + + " \"password\": \"demo123456!\",\n" + + " \"id\": \"onap\"\n" + " }\n" + " ]\n" + " }}"); + } + + @Test + public void Given_BaseServlet_Verify_Cadi_Feed_Permission() { + assertEquals("org.onap.dmaap-dr.feed|legacy|publish", baseServlet.getFeedPermission("legacy", "publish")); + assertEquals("org.onap.dmaap-dr.feed|legacy|suspend", baseServlet.getFeedPermission("legacy", "suspend")); + assertEquals("org.onap.dmaap-dr.feed|legacy|restore", baseServlet.getFeedPermission("legacy", "restore")); + assertEquals("org.onap.dmaap-dr.feed|org.onap.dmaap-dr.NoInstanceDefined|restore", baseServlet.getFeedPermission(null, "restore")); + assertEquals("org.onap.dmaap-dr.feed|legacy|*", baseServlet.getFeedPermission("legacy", "default")); + } + + @Test + public void Given_BaseServlet_Verify_Cadi_Sub_Permission() { + assertEquals("org.onap.dmaap-dr.feed|legacy|subscribe", baseServlet.getSubscriberPermission("legacy", "subscribe")); + assertEquals("org.onap.dmaap-dr.sub|legacy|suspend", baseServlet.getSubscriberPermission("legacy", "suspend")); + assertEquals("org.onap.dmaap-dr.sub|legacy|restore", baseServlet.getSubscriberPermission("legacy", "restore")); + assertEquals("org.onap.dmaap-dr.sub|legacy|publish", baseServlet.getSubscriberPermission("legacy", "publish")); + assertEquals("org.onap.dmaap-dr.sub|org.onap.dmaap-dr.NoInstanceDefined|restore", baseServlet.getSubscriberPermission(null, "restore")); + assertEquals("org.onap.dmaap-dr.sub|legacy|*", baseServlet.getSubscriberPermission("legacy", "default")); + } } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java index e2076b9d..42366dd0 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java @@ -47,6 +47,7 @@ public class DrServletTestBase { props.setProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir", "unit-test-logs"); props.setProperty("org.onap.dmaap.datarouter.provserver.spooldir", "unit-test-logs/spool"); props.setProperty("org.onap.dmaap.datarouter.provserver.https.relaxation", "false"); + props.setProperty("org.onap.dmaap.datarouter.provserver.passwordencryption", "PasswordEncryptionKey#@$%^&1234#"); FieldUtils.writeDeclaredStaticField(DB.class, "props", props, true); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsgFlag", false, true); SynchronizerTask synchronizerTask = mock(SynchronizerTask.class); diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java index 8bbf231a..8c48d705 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java @@ -34,9 +34,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; @@ -81,7 +78,6 @@ public class SynchronizerTaskTest { private CloseableHttpResponse response; private SynchronizerTask synchronizerTask; - private ExecutorService executorService; private static EntityManagerFactory emf; private static EntityManager em; @@ -116,15 +112,10 @@ public class SynchronizerTaskTest { synchronizerTask = Mockito.spy(SynchronizerTask.getSynchronizer()); doReturn(2).when(synchronizerTask).lookupState(); - - executorService = Executors.newCachedThreadPool(); - executorService.execute(synchronizerTask); } @After - public void tearDown() throws InterruptedException { - executorService.shutdown(); - executorService.awaitTermination(2, TimeUnit.SECONDS); + public void tearDown() { } @Test @@ -193,6 +184,7 @@ public class SynchronizerTaskTest { Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200); Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "application/vnd.dmaap-dr.provfeed-full; version=1.0")); mockResponseFromGet(); + synchronizerTask.run(); } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoaderTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoaderTest.java index 7f6d7de6..a15509b4 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoaderTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoaderTest.java @@ -20,32 +20,29 @@ package org.onap.dmaap.datarouter.provisioning.utils; +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertFalse; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; +import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.rules.TemporaryFolder; +import org.junit.Test; import org.junit.runner.RunWith; - import org.onap.dmaap.datarouter.provisioning.InternalServlet; import org.onap.dmaap.datarouter.provisioning.beans.Parameters; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; - -import static org.junit.Assert.assertFalse; - -import org.junit.Test; - - - @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters") public class LogfileLoaderTest { @@ -53,10 +50,18 @@ public class LogfileLoaderTest { private static EntityManagerFactory emf; private static EntityManager em; private LogfileLoader lfl = LogfileLoader.getLoader(); + private File testLog; - @Rule - public TemporaryFolder folder = new TemporaryFolder(); + @Before + public void setUp() throws Exception { + testLog = new File(System.getProperty("user.dir") + "/src/test/resources/IN.test_prov_logs"); + prepFile(testLog); + } + @After + public void tearDown() throws IOException { + Files.deleteIfExists(testLog.toPath()); + } @BeforeClass public static void init() { @@ -68,7 +73,6 @@ public class LogfileLoaderTest { InternalServlet internalServlet = new InternalServlet(); } - @AfterClass public static void tearDownClass() { em.clear(); @@ -76,42 +80,47 @@ public class LogfileLoaderTest { emf.close(); } - @Test - public void Verify_File_Processing_when_Req_Type_LOG() throws IOException { - String fileContent = "2018-08-29-10-10-10-543.|LOG|1|1|url/file123|method|1|1|type|1|128.0.0.9|user123|2|1|1|1|other|1"; - int[] actual = lfl.process(prepFile(fileContent, "file1")); - int[] expect = {0, 1}; + public void Verify_File_Processing_Returns_Expected_Array() { + int[] actual = lfl.process(testLog); + int[] expect = {5, 7}; Assert.assertArrayEquals(expect, actual); + Assert.assertNotNull(lfl.getBitSet()); + Assert.assertTrue(lfl.isIdle()); } - @Test - public void Verify_File_Processing_when_Req_Type_EXP() throws IOException{ - String fileContent = "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|method|ctype|3|other|4"; - int[] actual = lfl.process(prepFile(fileContent, "file2")); - int[] expect = {0, 1}; - Assert.assertArrayEquals(expect, actual); + public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() { + lfl.process(testLog); + PowerMockito.mockStatic(Parameters.class); + PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0")); + PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1000000")); + assertFalse(lfl.pruneRecords()); } - @Test - public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() throws IOException{ - String fileContent = "2018-08-29-10-10-10-543.|PUB|1|1|https://dmaap-dr-prov:8443/publish/1/file123/|POST|application/vnd.att-dr.feed|2|128.0.0.9|user123|200"; - lfl.process(prepFile(fileContent, "file3")); + public void Verify_Records_Prune_When_Record_Count_Is_Greater_Then_Threshold() { + lfl.process(testLog); PowerMockito.mockStatic(Parameters.class); PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0")); - assertFalse(lfl.pruneRecords()); + PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1")); + assertTrue(lfl.pruneRecords()); } - private File prepFile(String content, String fileName) throws IOException{ - File file1 = folder.newFile(fileName); - try (FileWriter fileWriter = new FileWriter(file1)) { - fileWriter.write(content); - }catch (IOException e){ + private void prepFile(File logFile) { + String testLogs = "2018-08-29-10-10-10-543.|LOG|1|1|https://dmaap-dr-prov:/url/file123|POST|application/vnd.att-dr.feed|100|mockType|file123|https://dmaap-dr-prov|user123|200|1|1|200|2|2\n" + + "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|PUT|null|3|new reason|4\n" + + "2018-08-29-10-10-10-543.|PUB|1|1|https://dmaap-dr-prov:8443/publish/1/file123/|POST|application/vnd.att-dr.feed|2|128.0.0.9|user123|200\n" + + "2018-08-29-10-10-10-543.|PBF|1|1|https://dmaap-dr-prov:8443/publish/1/file123/|POST|application/vnd.att-dr.feed|100|100|128.0.0.9|user123|failed\n" + + "2018-08-29-10-10-10-543.|DLX|1|1|1|100|100\n" + + "2018-08-29-10-10-10-543.|Bad Record|||\n" + + "2018-08-29-10-10-10-543.|DEL|2|1|2|https://dmaap-dr-prov:8443/publish/1/file123/|PUT|application/vnd.att-dr.feed|100|user123|200|123456"; + try (FileWriter fileWriter = new FileWriter(logFile)) { + fileWriter.write(testLogs); + } + catch (IOException e){ System.out.println(e.getMessage()); } - return file1; } } diff --git a/datarouter-prov/src/test/resources/h2Database.properties b/datarouter-prov/src/test/resources/h2Database.properties index 9c63aea4..cb472419 100755 --- a/datarouter-prov/src/test/resources/h2Database.properties +++ b/datarouter-prov/src/test/resources/h2Database.properties @@ -27,7 +27,7 @@ org.onap.dmaap.datarouter.db.url = jdbc:h2:mem:test;DB org.onap.dmaap.datarouter.provserver.isaddressauthenabled = true org.onap.dmaap.datarouter.provserver.https.relaxation = false org.onap.dmaap.datarouter.provserver.accesslog.dir = unit-test-logs -org.onap.dmaap.datarouter.provserver.spooldir = unit-test-logs/spool +org.onap.dmaap.datarouter.provserver.spooldir = src/test/resources org.onap.dmaap.datarouter.provserver.localhost = 127.0.0.1 org.onap.dmaap.datarouter.provserver.passwordencryption = PasswordEncryptionKey#@$%^&1234# |