aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'datarouter-prov/src/test')
-rw-r--r--datarouter-prov/src/test/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthTest.java134
-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.java3
-rwxr-xr-xdatarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java13
-rwxr-xr-xdatarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java44
-rwxr-xr-xdatarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscriptionServletTest.java15
-rwxr-xr-xdatarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java203
-rw-r--r--datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/SubscriptionTest.java52
-rw-r--r--datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoaderTest.java91
-rwxr-xr-xdatarouter-prov/src/test/resources/create.sql3
-rwxr-xr-xdatarouter-prov/src/test/resources/h2Database.properties11
-rw-r--r--datarouter-prov/src/test/resources/prov_data.json129
12 files changed, 680 insertions, 108 deletions
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthTest.java
new file mode 100644
index 00000000..7de4ea91
--- /dev/null
+++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/authz/impl/ProvAuthTest.java
@@ -0,0 +1,134 @@
+/*-
+ * ============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.authz.impl;
+
+import static org.mockito.Mockito.when;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import javax.servlet.http.HttpServletRequest;
+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.mockito.Mock;
+import org.mockito.Mockito;
+import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
+import org.onap.dmaap.datarouter.authz.impl.ProvAuthorizer;
+import org.onap.dmaap.datarouter.provisioning.StatisticsServlet;
+import org.onap.dmaap.datarouter.provisioning.utils.DB;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+public class ProvAuthTest {
+
+ @Mock
+ private HttpServletRequest request;
+
+ @Mock
+ private StatisticsServlet statisticsServlet;
+
+ private ProvAuthorizer provAuthorizer;
+
+ 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();
+ provAuthorizer = new ProvAuthorizer(statisticsServlet);
+ }
+
+ @Test
+ public void Validate_Prov_Auth_Check_Feed_Access() {
+ when(statisticsServlet.getFeedOwner(Mockito.anyString())).thenReturn("dr-admin");
+ when(statisticsServlet.getGroupByFeedGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
+ when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
+ when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
+ when(request.getMethod()).thenReturn("PUT");
+ when(request.getRequestURI()).thenReturn("http://the-request-uri:443/feed/1?1");
+ AuthorizationResponse authResp;
+ authResp = provAuthorizer.decide(request);
+ Assert.assertTrue(authResp.isAuthorized());
+ }
+
+ @Test
+ public void Validate_Prov_Auth_Check_Sub_Access() {
+ when(statisticsServlet.getSubscriptionOwner(Mockito.anyString())).thenReturn("dr-admin");
+ when(statisticsServlet.getGroupBySubGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
+ when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
+ when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
+ when(request.getMethod()).thenReturn("PUT");
+ when(request.getRequestURI()).thenReturn("http://the-request-uri:443/subs/1?1");
+ AuthorizationResponse authResp;
+ authResp = provAuthorizer.decide(request);
+ Assert.assertTrue(authResp.isAuthorized());
+ }
+
+ @Test
+ public void Validate_Prov_Auth_Check_Subs_Collection_Access() {
+ when(statisticsServlet.getSubscriptionOwner(Mockito.anyString())).thenReturn("dr-admin");
+ when(statisticsServlet.getGroupBySubGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
+ when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
+ when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
+ when(request.getMethod()).thenReturn("POST");
+ when(request.getRequestURI()).thenReturn("http://the-request-uri:443/subscribe/1?1");
+ AuthorizationResponse authResp;
+ authResp = provAuthorizer.decide(request);
+ Assert.assertTrue(authResp.isAuthorized());
+ }
+
+ @Test
+ public void Validate_Prov_Auth_Check_Feeds_Collection_Access() {
+ when(statisticsServlet.getFeedOwner(Mockito.anyString())).thenReturn("dr-admin");
+ when(statisticsServlet.getGroupByFeedGroupId(Mockito.anyString(), Mockito.anyString())).thenReturn("stub_auth_id");
+ when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF")).thenReturn("dr-admin");
+ when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_auth_id");
+ when(request.getMethod()).thenReturn("POST");
+ when(request.getRequestURI()).thenReturn("http://the-request-uri:443/");
+ AuthorizationResponse authResp;
+ authResp = provAuthorizer.decide(request);
+ Assert.assertTrue(authResp.isAuthorized());
+ Assert.assertNull(authResp.getAdvice());
+ Assert.assertNull(authResp.getObligations());
+ }
+
+}
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 bad6e2cb..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,10 +47,11 @@ 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);
- when(synchronizerTask.getState()).thenReturn(SynchronizerTask.UNKNOWN);
+ when(synchronizerTask.getPodState()).thenReturn(SynchronizerTask.UNKNOWN_POD);
FieldUtils.writeDeclaredStaticField(BaseServlet.class, "synctask", synchronizerTask, true);
}
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java
index f4eac05f..a1f714bb 100755
--- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java
+++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java
@@ -109,7 +109,6 @@ public class FeedServletTest extends DrServletTestBase {
verifyEnteringExitCalled(listAppender);
}
-
@Test
public void Given_Request_Is_HTTP_DELETE_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated()
throws Exception {
@@ -118,7 +117,6 @@ public class FeedServletTest extends DrServletTestBase {
verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_DELETE_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception {
when(request.getPathInfo()).thenReturn(null);
@@ -126,7 +124,6 @@ public class FeedServletTest extends DrServletTestBase {
verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_DELETE_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
when(request.getPathInfo()).thenReturn("/123");
@@ -134,7 +131,6 @@ public class FeedServletTest extends DrServletTestBase {
verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_DELETE_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
setAuthoriserToReturnRequestNotAuthorized();
@@ -158,7 +154,6 @@ public class FeedServletTest extends DrServletTestBase {
verifyEnteringExitCalled(listAppender);
}
-
@Test
public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Fails_An_Internal_Server_Error_Is_Reported()
throws Exception {
@@ -172,7 +167,6 @@ public class FeedServletTest extends DrServletTestBase {
.sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Succeeds_A_NO_CONTENT_Response_Is_Generated() throws Exception {
feedServlet.doDelete(request, response);
@@ -198,7 +192,6 @@ public class FeedServletTest extends DrServletTestBase {
verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_GET_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated()
throws Exception {
@@ -207,7 +200,6 @@ public class FeedServletTest extends DrServletTestBase {
verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_GET_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated()
throws Exception {
@@ -216,7 +208,6 @@ public class FeedServletTest extends DrServletTestBase {
verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_GET_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
throws Exception {
@@ -226,7 +217,6 @@ public class FeedServletTest extends DrServletTestBase {
verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_GET_And_Request_Succeeds() throws Exception {
ServletOutputStream outStream = mock(ServletOutputStream.class);
@@ -237,7 +227,6 @@ public class FeedServletTest extends DrServletTestBase {
verifyEnteringExitCalled(listAppender);
}
-
@Test
public void Given_Request_Is_HTTP_PUT_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
throws Exception {
@@ -255,7 +244,6 @@ public class FeedServletTest extends DrServletTestBase {
verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_PUT_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated()
throws Exception {
@@ -264,7 +252,6 @@ public class FeedServletTest extends DrServletTestBase {
verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
}
-
@Test
public void Given_Request_Is_HTTP_PUT_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated()
throws Exception {
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java
index b867c672..57007489 100755
--- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java
+++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SubscribeServletTest.java
@@ -22,8 +22,25 @@
******************************************************************************/
package org.onap.dmaap.datarouter.provisioning;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.argThat;
+import static org.mockito.Mockito.contains;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
+
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
+import java.util.HashSet;
+import java.util.Set;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
@@ -36,30 +53,11 @@ import org.mockito.Mock;
import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
import org.onap.dmaap.datarouter.authz.Authorizer;
import org.onap.dmaap.datarouter.provisioning.beans.Insertable;
-import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.Persistence;
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-import static org.hamcrest.Matchers.notNullValue;
-import static org.mockito.Mockito.*;
-import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
-
@RunWith(PowerMockRunner.class)
-@PrepareForTest(Subscription.class)
public class SubscribeServletTest extends DrServletTestBase {
private static SubscribeServlet subscribeServlet;
private static EntityManagerFactory emf;
@@ -144,10 +142,6 @@ public class SubscribeServletTest extends DrServletTestBase {
ServletOutputStream outStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outStream);
when(request.getPathInfo()).thenReturn("/1");
- PowerMockito.mockStatic(Subscription.class);
- List<String> 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/SynchronizerTaskTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java
new file mode 100755
index 00000000..8c48d705
--- /dev/null
+++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTaskTest.java
@@ -0,0 +1,203 @@
+/*******************************************************************************
+ * ============LICENSE_START==================================================
+ * * org.onap.dmaap
+ * * ===========================================================================
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * * ===========================================================================
+ * * 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.
+ * * ============LICENSE_END====================================================
+ * *
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * *
+ ******************************************************************************/
+
+package org.onap.dmaap.datarouter.provisioning;
+
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.powermock.api.mockito.PowerMockito.when;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.StatusLine;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.impl.client.AbstractHttpClient;
+import org.apache.http.message.BasicHeader;
+import org.junit.After;
+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.mockito.Mock;
+import org.mockito.Mockito;
+import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
+import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PowerMockIgnore("javax.net.ssl.*")
+@PrepareForTest({BaseServlet.class, URLUtilities.class})
+public class SynchronizerTaskTest {
+
+ @Mock
+ private AbstractHttpClient httpClient;
+
+ @Mock
+ private HttpEntity httpEntity;
+
+ @Mock
+ private StatusLine statusLine;
+
+ @Mock
+ private CloseableHttpResponse response;
+
+ private SynchronizerTask synchronizerTask;
+
+ private static EntityManagerFactory emf;
+ private static EntityManager em;
+
+ @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 IllegalAccessException, UnknownHostException {
+ SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
+ doNothing().when(sslSocketFactory).setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+
+ PowerMockito.mockStatic(BaseServlet.class);
+ PowerMockito.mockStatic(URLUtilities.class);
+ when(BaseServlet.getPods()).thenReturn(new String[] {InetAddress.getLocalHost().getHostName(), "stand-by-prov"});
+ when(URLUtilities.generatePeerProvURL()).thenReturn("https://stand-by-prov/internal/prov");
+ when(URLUtilities.generatePeerLogsURL()).thenReturn("https://stand-by-prov/internal/drlogs");
+
+ synchronizerTask = Mockito.spy(SynchronizerTask.getSynchronizer());
+ doReturn(2).when(synchronizerTask).lookupState();
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ @Test
+ public void Given_Synch_Task_readRemoteLoglist_Called_And_Valid_BitSet_Returned_Success()
+ throws IOException, IllegalAccessException {
+ mockHttpClientForGetRequest();
+ Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
+ Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
+ Mockito.when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("1-55251".getBytes()));
+ RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
+ Assert.assertNotNull(rleBitSet);
+ }
+
+ @Test
+ public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Resonse_Code_Failure()
+ throws IOException, IllegalAccessException {
+ mockHttpClientForGetRequest();
+ Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
+ RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
+ Assert.assertNotNull(rleBitSet);
+ }
+
+ @Test
+ public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Content_Type_Failure()
+ throws IOException, IllegalAccessException {
+ mockHttpClientForGetRequest();
+ Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
+ Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
+ RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
+ Assert.assertNotNull(rleBitSet);
+ }
+
+ @Test
+ public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Valid_BitSet_Returned_Success()
+ throws IOException, IllegalAccessException {
+ mockHttpClientForGetRequest();
+ Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
+ Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
+ RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
+ synchronizerTask.replicateDataRouterLogs(rleBitSet);
+ }
+
+ @Test
+ public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Content_Type_Failure()
+ throws IOException, IllegalAccessException {
+ mockHttpClientForGetRequest();
+ Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
+ Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
+ RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
+ synchronizerTask.replicateDataRouterLogs(rleBitSet);
+ }
+
+ @Test
+ public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Resonse_Code_Failure()
+ throws IOException, IllegalAccessException {
+ mockHttpClientForGetRequest();
+ Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
+ RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
+ synchronizerTask.replicateDataRouterLogs(rleBitSet);
+ }
+
+ @Test
+ public void Given_Synch_Task_Is_Started_And_LogFileLoader_Is_Idle_Then_Standby_Pod_Synch_Is_Successful()
+ throws IOException, IllegalAccessException {
+ mockHttpClientForGetRequest();
+ 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();
+ }
+
+
+ private void mockHttpClientForGetRequest() throws IllegalAccessException, IOException {
+ FieldUtils.writeField(synchronizerTask, "httpclient", httpClient, true);
+ Mockito.when(httpClient.execute(anyObject())).thenReturn(response);
+ Mockito.when(response.getEntity()).thenReturn(httpEntity);
+ Mockito.when(response.getStatusLine()).thenReturn(statusLine);
+
+ }
+
+ private void mockResponseFromGet() throws IOException {
+ InputStream in = getClass().getClassLoader().getResourceAsStream("prov_data.json");
+ Mockito.when(httpEntity.getContent()).thenReturn(in);
+ }
+}
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
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/create.sql b/datarouter-prov/src/test/resources/create.sql
index 7c106723..a811847c 100755
--- a/datarouter-prov/src/test/resources/create.sql
+++ b/datarouter-prov/src/test/resources/create.sql
@@ -186,6 +186,9 @@ insert into INGRESS_ROUTES(SEQUENCE, FEEDID , USERID, SUBNET, NODESET)
VALUES (2,1,'user',null,2);
insert into NODESETS(SETID, NODEID)
+VALUES (1,1);
+
+insert into NODESETS(SETID, NODEID)
VALUES (2,2);
insert into LOG_RECORDS(RECORD_ID,TYPE,EVENT_TIME,PUBLISH_ID,FEEDID,REQURI,METHOD,CONTENT_TYPE,CONTENT_LENGTH,FEED_FILEID,REMOTE_ADDR,USER,STATUS,DELIVERY_SUBID,DELIVERY_FILEID,RESULT,ATTEMPTS,REASON,FILENAME)
diff --git a/datarouter-prov/src/test/resources/h2Database.properties b/datarouter-prov/src/test/resources/h2Database.properties
index fee9c688..cb472419 100755
--- a/datarouter-prov/src/test/resources/h2Database.properties
+++ b/datarouter-prov/src/test/resources/h2Database.properties
@@ -27,6 +27,13 @@ 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# \ No newline at end of file
+org.onap.dmaap.datarouter.provserver.passwordencryption = PasswordEncryptionKey#@$%^&1234#
+
+org.onap.dmaap.datarouter.provserver.keystore.type = jks
+org.onap.dmaap.datarouter.provserver.keymanager.password = FZNkU,B%NJzcT1v7;^v]M#ZX
+org.onap.dmaap.datarouter.provserver.keystore.path = aaf_certs/org.onap.dmaap-dr.jks
+org.onap.dmaap.datarouter.provserver.keystore.password = FZNkU,B%NJzcT1v7;^v]M#ZX
+org.onap.dmaap.datarouter.provserver.truststore.path = aaf_certs/org.onap.dmaap-dr.trust.jks
+org.onap.dmaap.datarouter.provserver.truststore.password = +mzf@J.D^;3!![*Xr.z$c#?b \ No newline at end of file
diff --git a/datarouter-prov/src/test/resources/prov_data.json b/datarouter-prov/src/test/resources/prov_data.json
new file mode 100644
index 00000000..32536316
--- /dev/null
+++ b/datarouter-prov/src/test/resources/prov_data.json
@@ -0,0 +1,129 @@
+{
+ "feeds": [
+ {
+ "suspend": false,
+ "groupid": 0,
+ "description": "Default feed provisioned for PM File collector",
+ "version": "m1.0",
+ "authorization": {
+ "endpoint_addrs": [
+
+ ],
+ "classification": "unclassified",
+ "endpoint_ids": [
+ {
+ "password": "dradmin",
+ "id": "dradmin"
+ }
+ ]
+ },
+ "last_mod": 1560871903000,
+ "deleted": false,
+ "feedid": 1,
+ "name": "Default PM Feed",
+ "business_description": "Default Feed",
+ "aaf_instance": "legacy",
+ "publisher": "dradmin",
+ "links": {
+ "subscribe": "https://dmaap-dr-prov/subscribe/1",
+ "log": "https://dmaap-dr-prov/feedlog/1",
+ "publish": "https://dmaap-dr-prov/publish/1",
+ "self": "https://dmaap-dr-prov/feed/1"
+ },
+ "created_date": 1560871903000
+ }
+ ],
+ "groups": [
+ {
+ "authid": "GROUP-0000-c2754bb7-92ef-4869-9c6b-1bc1283be4c0",
+ "name": "Test Group",
+ "description": "Test Description of Group .",
+ "classification": "publisher/subscriber",
+ "members": "{id=attuid, name=User1}, {id=attuid, name=User 2]"
+ }
+ ],
+ "subscriptions": [
+ {
+ "suspend": false,
+ "delivery": {
+ "use100": true,
+ "password": "PASSWORD",
+ "user": "LOGIN",
+ "url": "https://dcae-pm-mapper:8443/delivery"
+ },
+ "subscriber": "dradmin",
+ "groupid": 0,
+ "metadataOnly": false,
+ "privilegedSubscriber": true,
+ "subid": 1,
+ "last_mod": 1560872889000,
+ "feedid": 1,
+ "follow_redirect": false,
+ "decompress": true,
+ "aaf_instance": "legacy",
+ "links": {
+ "feed": "https://dmaap-dr-prov/feed/1",
+ "log": "https://dmaap-dr-prov/sublog/1",
+ "self": "https://dmaap-dr-prov/subs/1"
+ },
+ "created_date": 1560872889000
+ }
+ ],
+ "parameters": {
+ "ACTIVE_POD": "dmaap-dr-prov",
+ "DELIVERY_FILE_PROCESS_INTERVAL": 10,
+ "DELIVERY_INIT_RETRY_INTERVAL": 10,
+ "DELIVERY_MAX_AGE": 86400,
+ "DELIVERY_MAX_RETRY_INTERVAL": 3600,
+ "DELIVERY_RETRY_RATIO": 2,
+ "LOGROLL_INTERVAL": 30,
+ "NODES": [
+ "dmaap-dr-node"
+ ],
+ "PROV_ACTIVE_NAME": "dmaap-dr-prov",
+ "PROV_AUTH_ADDRESSES": [
+ "dmaap-dr-prov",
+ "dmaap-dr-node"
+ ],
+ "PROV_AUTH_SUBJECTS": [
+ ""
+ ],
+ "PROV_DOMAIN": "",
+ "PROV_MAXFEED_COUNT": 10000,
+ "PROV_MAXSUB_COUNT": 100000,
+ "PROV_NAME": "dmaap-dr-prov",
+ "PROV_REQUIRE_CERT": "false",
+ "PROV_REQUIRE_SECURE": "true",
+ "STANDBY_POD": "",
+ "_INT_VALUES": [
+ "LOGROLL_INTERVAL",
+ "PROV_MAXFEED_COUNT",
+ "PROV_MAXSUB_COUNT",
+ "DELIVERY_INIT_RETRY_INTERVAL",
+ "DELIVERY_MAX_RETRY_INTERVAL",
+ "DELIVERY_RETRY_RATIO",
+ "DELIVERY_MAX_AGE",
+ "DELIVERY_FILE_PROCESS_INTERVAL"
+ ]
+ },
+ "ingress": [
+ {
+ "feedid": 1,
+ "subnet": "",
+ "user": "",
+ "node": [
+ "stub_from."
+ ]
+ }
+ ],
+ "egress": {
+ "1": "stub_to."
+ },
+ "routing": [
+ {
+ "from": 1,
+ "to": 3,
+ "via": 2
+ }
+ ]
+} \ No newline at end of file