aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/test
diff options
context:
space:
mode:
authorefiacor <fiachra.corcoran@est.tech>2019-07-16 11:15:29 +0000
committerefiacor <fiachra.corcoran@est.tech>2019-07-16 11:15:29 +0000
commit343f8d2a0e05b783a17d0056bd2c483ca210970a (patch)
treeb04985ea78cce84d7848fed9e3c0891ef960127e /datarouter-prov/src/test
parent98572b78fcce9ff28fa7429c9265812bd1e78bf2 (diff)
More DR unit tests and code cleanup
Change-Id: Ia9b2b1c10cb43fd428baf1afd99c792ea4f96e9e Issue-ID: DMAAP-1226 Signed-off-by: efiacor <fiachra.corcoran@est.tech>
Diffstat (limited to 'datarouter-prov/src/test')
-rwxr-xr-xdatarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java90
-rw-r--r--datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java1
-rwxr-xr-xdatarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java12
3 files changed, 74 insertions, 29 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();
}