Add and Modify jUnits for code coverage (model, admin) 36/99236/1
authorPeyton Puckett <peyton.puckett@att.com>
Wed, 4 Dec 2019 21:55:30 +0000 (15:55 -0600)
committerPeyton Puckett <peyton.puckett@att.com>
Thu, 5 Dec 2019 22:01:01 +0000 (16:01 -0600)
Add tests to model after viewing sonarcloud coverage
Start adding tests for admin

Issue-ID: POLICY-2133
Change-Id: I690c612357fe3390c7904c6ba4c941dcefcc7640
Signed-off-by: Peyton Puckett <peyton.puckett@att.com>
POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java
POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyUserInfoControllerTest.java
POLICY-SDK-APP/src/test/java/org/onap/policy/model/PDPGroupContainerTest.java

index 244901a..e943883 100644 (file)
 
 package org.onap.policy.admin;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-
+import javax.json.JsonArray;
 import javax.servlet.ServletConfig;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
-
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.commons.io.IOUtils;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mockito;
 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 import org.onap.policy.common.logging.flexlogger.Logger;
@@ -55,8 +60,12 @@ import org.onap.policy.rest.jpa.PolicyVersion;
 import org.onap.policy.rest.jpa.UserInfo;
 import org.onap.portalsdk.core.domain.User;
 import org.onap.portalsdk.core.util.SystemProperties;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
 import org.springframework.mock.web.MockHttpServletResponse;
 
+@RunWith(PowerMockRunner.class)
 public class PolicyManagerServletTest extends Mockito {
 
     private static Logger logger = FlexLogger.getLogger(PolicyManagerServletTest.class);
@@ -746,4 +755,63 @@ public class PolicyManagerServletTest extends Mockito {
             }
         }
     }
+
+    @Test
+    public void testSetPolicyNames() {
+        JsonArray mockJsonArray = Mockito.mock(JsonArray.class);
+        PolicyManagerServlet.setPolicyNames(mockJsonArray);
+        assertEquals(mockJsonArray, PolicyManagerServlet.getPolicyNames());
+    }
+
+    @Test
+    public void testDoPostSetErrorException() throws IOException {
+        PolicyManagerServlet servlet = new PolicyManagerServlet();
+        HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
+        HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
+        doThrow(IOException.class).when(mockRequest).getReader();
+        doThrow(IOException.class).when(mockResponse).sendError(any(Integer.class), any(String.class));
+        servlet.doPost(mockRequest, mockResponse);
+        verify(mockRequest).getReader();
+    }
+
+    @Test
+    public void testDoPostException() throws IOException {
+        PolicyManagerServlet servlet = new PolicyManagerServlet();
+        HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
+        HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
+
+        doThrow(IOException.class).when(mockRequest).getReader();
+        doThrow(IOException.class).when(mockResponse).sendError(any(Integer.class), any(String.class));
+        doThrow(IOException.class).when(mockResponse).getWriter();
+
+        servlet.doPost(mockRequest, mockResponse);
+        verify(mockRequest).getReader();
+        verify(mockResponse).getWriter();
+    }
+
+    @Test
+    public void testDoPostSuccess() throws IOException {
+        PolicyManagerServlet servlet = new PolicyManagerServlet();
+        HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
+        HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
+        PrintWriter mockPrintWriter = Mockito.mock(PrintWriter.class);
+
+        doThrow(IOException.class).when(mockRequest).getReader();
+        when(mockResponse.getWriter()).thenReturn(mockPrintWriter);
+
+        servlet.doPost(null, mockResponse);
+        verify(mockResponse).getWriter();
+    }
+
+    @PrepareForTest(ServletFileUpload.class)
+    @Test
+    public void testDoPostUploadFileException() {
+        PolicyManagerServlet servlet = new PolicyManagerServlet();
+        HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
+        HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
+        PowerMockito.mockStatic(ServletFileUpload.class);
+        when(ServletFileUpload.isMultipartContent(mockRequest)).thenReturn(true);
+        servlet.doPost(mockRequest, mockResponse);
+        PowerMockito.verifyStatic(ServletFileUpload.class, Mockito.times(1));
+    }
 }
index bf4b676..55e4b5a 100644 (file)
@@ -24,11 +24,15 @@ package org.onap.policy.admin;
 
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.mockito.Mockito.atLeast;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+import java.io.IOException;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -62,4 +66,14 @@ public class PolicyUserInfoControllerTest {
             fail();
         }
     }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testGetPolicyUserInfoException() throws IOException {
+        HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
+        when(mockResponse.getWriter()).thenThrow(IOException.class);
+        PolicyUserInfoController controller = new PolicyUserInfoController();
+        controller.getPolicyUserInfo(request, mockResponse);
+        verify(mockResponse, atLeast(1)).getWriter();
+    }
 }
index f184bde..5204fc4 100644 (file)
 package org.onap.policy.model;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.when;
 
 import com.att.research.xacml.api.pap.PAPException;
 import java.awt.Checkbox;
@@ -106,7 +110,7 @@ public class PDPGroupContainerTest {
     }
 
     @Test(expected = IndexOutOfBoundsException.class)
-    public void testGetIdByIndex() {
+    public void testGetIdByIndexException() {
         container.getIdByIndex(0);
     }
 
@@ -116,7 +120,7 @@ public class PDPGroupContainerTest {
     }
 
     @Test(expected = IllegalArgumentException.class)
-    public void testGetItemIds() {
+    public void testGetItemIdsException() {
         container.getItemIds(0, 1);
     }
 
@@ -165,4 +169,96 @@ public class PDPGroupContainerTest {
         doThrow(PAPException.class).when(engine).removePDP(pdp);
         container.removePDP(pdp, group);
     }
-}
\ No newline at end of file
+
+    @Test(expected = UnsupportedOperationException.class)
+    public void testRemoveDefaultGroup() throws PAPException {
+        when(group.isDefaultGroup()).thenReturn(true);
+        container.removeGroup(group, newGroup);
+    }
+
+    @Test
+    public void testGetItemIds() {
+        assertNotNull(container.getItemIds(0, 0));
+    }
+
+    @Test
+    public void testContainsId() {
+        assertFalse(container.containsId(group));
+    }
+
+    @Test
+    public void testGroupMethods() {
+        container.groups.add(group);
+
+        Object retObj = container.getIdByIndex(0);
+        assertTrue(retObj instanceof OnapPDPGroup);
+        assertNotNull(retObj);
+
+        Object retFirstItemId = container.firstItemId();
+        assertTrue(retFirstItemId instanceof OnapPDPGroup);
+        assertNotNull(retFirstItemId);
+
+        Object retLastItemId = container.lastItemId();
+        assertTrue(retLastItemId instanceof OnapPDPGroup);
+        assertNotNull(retLastItemId);
+
+        assertTrue(container.isFirstId(group));
+
+        assertTrue(container.isLastId(group));
+    }
+
+    @Test
+    public void testNextItemId() {
+        OnapPDPGroup groupNotInList = Mockito.mock(OnapPDPGroup.class);
+        Object retObj = null;
+
+        container.groups.add(group);
+        container.groups.add(newGroup);
+
+        assertNull(container.nextItemId(groupNotInList));
+        assertNull(container.nextItemId(newGroup));
+
+        retObj = container.nextItemId(group);
+        assertNotNull(retObj);
+        assertTrue(retObj instanceof OnapPDPGroup);
+    }
+
+    @Test
+    public void testPrevItemId() {
+        OnapPDPGroup groupNotInList = Mockito.mock(OnapPDPGroup.class);
+        Object retObj = null;
+
+        container.groups.add(group);
+        container.groups.add(newGroup);
+
+        assertNull(container.prevItemId(groupNotInList));
+        assertNull(container.prevItemId(group));
+
+        retObj = container.prevItemId(newGroup);
+        assertNotNull(retObj);
+        assertTrue(retObj instanceof OnapPDPGroup);
+    }
+
+    @Test
+    public void testRemoveNullItem() {
+        OnapPDPGroup nullGroup = null;
+        assertFalse(container.removeItem(nullGroup));
+    }
+
+    @Test(expected = UnsupportedOperationException.class)
+    public void testRemoveDefaultItem() {
+        when(group.getId()).thenReturn("Default");
+        container.removeItem(group);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testRemoveItem() throws PAPException {
+        assertTrue(container.removeItem(group));
+
+        PAPPolicyEngine mockPAPPolicyEngine = Mockito.mock(PAPPolicyEngine.class);
+        PDPGroupContainer groupContainer = new PDPGroupContainer(mockPAPPolicyEngine);
+        when(mockPAPPolicyEngine.getDefaultGroup()).thenThrow(PAPException.class);
+        assertFalse(groupContainer.removeItem(group));
+    }
+}

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use