aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'datarouter-prov/src/test/java')
-rw-r--r--datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/DrServletTestBase.java4
-rw-r--r--[-rwxr-xr-x]datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java41
-rw-r--r--datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/GroupTest.java120
-rw-r--r--datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecordTest.java50
4 files changed, 158 insertions, 57 deletions
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 414fc185..c7f639ed 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
@@ -38,8 +38,8 @@ public class DrServletTestBase {
public void setUp() throws Exception {
Properties props = new Properties();
props.setProperty("org.onap.dmaap.datarouter.provserver.isaddressauthenabled", "false");
- props.setProperty("org.onap.dmaap.datarouter.provserver.accesslog.dir", "datarouter-prov/unit-test-logs");
- props.setProperty("org.onap.dmaap.datarouter.provserver.spooldir", "resources/spooldir");
+ 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");
FieldUtils.writeDeclaredStaticField(DB.class, "props", props, true);
FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsgFlag", false, true);
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java
index 97900d4d..5f6b7ae3 100755..100644
--- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java
+++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
+import java.io.File;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
@@ -154,6 +155,19 @@ public class InternalServletTest extends DrServletTestBase {
}
@Test
+ public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_And_File_Exists_Then_Request_Returns_Ok()
+ throws Exception {
+ when(request.getPathInfo()).thenReturn("/logs/testFile.txt");
+ File testFile = new File("unit-test-logs/testFile.txt");
+ testFile.createNewFile();
+ testFile.deleteOnExit();
+ ServletOutputStream outStream = mock(ServletOutputStream.class);
+ when(response.getOutputStream()).thenReturn(outStream);
+ internalServlet.doGet(request, response);
+ verify(response).setStatus(eq(HttpServletResponse.SC_OK));
+ }
+
+ @Test
public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
when(request.getPathInfo()).thenReturn("/api/Key");
setParametersToNotContactDb(false);
@@ -167,10 +181,7 @@ public class InternalServletTest extends DrServletTestBase {
public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Request_Succeeds()
throws Exception {
when(request.getPathInfo()).thenReturn("/drlogs/");
- PowerMockito.mockStatic(LogfileLoader.class);
- LogfileLoader logfileLoader = mock(LogfileLoader.class);
- when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet());
- PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader);
+ mockLogfileLoader();
ServletOutputStream outStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outStream);
internalServlet.doGet(request, response);
@@ -332,6 +343,21 @@ public class InternalServletTest extends DrServletTestBase {
}
@Test
+ public void Given_Request_Is_HTTP_POST_To_Logs_Then_Request_Succeeds()
+ throws Exception {
+ when(request.getHeader("Content-Encoding")).thenReturn("gzip");
+ when(request.getPathInfo()).thenReturn("/logs/");
+ ServletInputStream inStream = mock(ServletInputStream.class);
+ when(request.getInputStream()).thenReturn(inStream);
+ File testDir = new File("unit-test-logs/spool");
+ testDir.mkdirs();
+ testDir.deleteOnExit();
+ mockLogfileLoader();
+ internalServlet.doPost(request, response);
+ verify(response).setStatus(eq(HttpServletResponse.SC_CREATED));
+ }
+
+ @Test
public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated()
throws Exception {
when(request.getHeader("Content-Type")).thenReturn("stub_contentType");
@@ -452,4 +478,11 @@ public class InternalServletTest extends DrServletTestBase {
Map<String, Integer> map = new HashMap<>();
FieldUtils.writeDeclaredStaticField(NodeClass.class, "map", map, true);
}
+
+ private void mockLogfileLoader() {
+ PowerMockito.mockStatic(LogfileLoader.class);
+ LogfileLoader logfileLoader = mock(LogfileLoader.class);
+ when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet());
+ PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader);
+ }
}
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/GroupTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/GroupTest.java
index 098765cf..91d72af7 100644
--- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/GroupTest.java
+++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/GroupTest.java
@@ -22,62 +22,80 @@
******************************************************************************/
package org.onap.dmaap.datarouter.provisioning.beans;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
-import org.powermock.modules.junit4.PowerMockRunner;
+import org.junit.*;
+import org.onap.dmaap.datarouter.provisioning.utils.DB;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import java.util.Collection;
import java.util.Date;
+import java.util.List;
-
-@RunWith(PowerMockRunner.class)
-@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Group"})
public class GroupTest {
- private Group group;
+ private static EntityManagerFactory emf;
+ private static EntityManager em;
+ private Group group;
+ 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();
+ group = new Group("GroupTest", "", "");
+ group.doInsert(db.getConnection());
+ }
+
+ @Test
+ public void Given_Group_Exists_In_Db_GetAllGroups_Returns_Correct_Group() {
+ Collection<Group> groups = Group.getAllgroups();
+ Assert.assertEquals("Group1", ((List<Group>) groups).get(0).getName());
+ }
+
+ @Test
+ public void Given_Group_Inserted_Into_Db_GetGroupMatching_Returns_Created_Group() {
+ Assert.assertEquals(group, Group.getGroupMatching(group));
+ }
+
+ @Test
+ public void Given_Group_Inserted_With_Same_Name_GetGroupMatching_With_Id_Returns_Correct_Group()
+ throws Exception {
+ Group sameGroupName = new Group("GroupTest", "This group has a description", "");
+ sameGroupName.doInsert(db.getConnection());
+ Assert.assertEquals(
+ "This group has a description", Group.getGroupMatching(group, 2).getDescription());
+ sameGroupName.doDelete(db.getConnection());
+ }
- @Test
- public void Validate_Group_Created_With_Default_Contructor() {
- group = new Group();
- Assert.assertEquals(group.getGroupid(), -1);
- Assert.assertEquals(group.getName(), "");
- }
+ @Test
+ public void Given_Group_Inserted_GetGroupById_Returns_Correct_Group() {
+ Assert.assertEquals(group, Group.getGroupById(group.getGroupid()));
+ }
- @Test
- public void Validate_Getters_And_Setters() {
- group = new Group();
- group.setGroupid(1);
- group.setAuthid("Auth");
- group.setClassification("Class");
- group.setDescription("Description");
- Date date = new Date();
- group.setLast_mod(date);
- group.setMembers("Members");
- group.setName("NewName");
- Assert.assertEquals(1, group.getGroupid());
- Assert.assertEquals("Auth", group.getAuthid());
- Assert.assertEquals("Class", group.getClassification());
- Assert.assertEquals("Description", group.getDescription());
- Assert.assertEquals(date, group.getLast_mod());
- Assert.assertEquals("Members", group.getMembers());
- }
+ @Test
+ public void Given_Group_AuthId_Updated_GetGroupByAuthId_Returns_Correct_Group() throws Exception {
+ group.setAuthid("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9");
+ group.doUpdate(db.getConnection());
+ Assert.assertEquals(group, Group.getGroupByAuthId("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9"));
+ }
- @Test
- public void Validate_Equals() {
- group = new Group();
- group.setGroupid(1);
- group.setAuthid("Auth");
- group.setClassification("Class");
- group.setDescription("Description");
- Date date = new Date();
- group.setLast_mod(date);
- group.setMembers("Members");
- group.setName("NewName");
- Group group2 = new Group("NewName", "Description", "Members");
- group2.setGroupid(1);
- group2.setAuthid("Auth");
- group2.setClassification("Class");
- group2.setLast_mod(date);
- Assert.assertEquals(group, group2);
- }
+ @After
+ public void tearDown() throws Exception {
+ group.doDelete(db.getConnection());
+ }
}
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecordTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecordTest.java
new file mode 100644
index 00000000..dd798ab6
--- /dev/null
+++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecordTest.java
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * ============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.beans;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import java.text.ParseException;
+
+@RunWith(PowerMockRunner.class)
+public class PubFailRecordTest {
+
+ private PubFailRecord pubFailRecord;
+
+
+ @Test
+ public void Validate_PubFailRecord_Created_With_Default_Constructor() throws ParseException {
+ String[] args = {"2018-08-29-10-10-10-543.", "PBF", "238465493.fileName",
+ "1", "/publish/1/fileName", "PUT", "application/octet-stream", "285", "200",
+ "172.100.0.3", "user1", "403"};
+ pubFailRecord = new PubFailRecord(args);
+
+ Assert.assertEquals("user1", pubFailRecord.getUser());
+ Assert.assertEquals("172.100.0.3", pubFailRecord.getSourceIP());
+ Assert.assertEquals("403", pubFailRecord.getError());
+ Assert.assertEquals(200, pubFailRecord.getContentLengthReceived());
+ }
+}