diff options
author | Peyton Puckett <peyton.puckett@att.com> | 2019-12-04 15:55:30 -0600 |
---|---|---|
committer | Peyton Puckett <peyton.puckett@att.com> | 2019-12-05 16:01:01 -0600 |
commit | 583245286bf67cd11cc5b16c90bbf9a06c6b2609 (patch) | |
tree | 420068aa3a7ad3e91dfc23cc48a1c6db814bd973 /POLICY-SDK-APP/src | |
parent | 2d523ee0d6929bd812bd9387c596ed8a653c714e (diff) |
Add and Modify jUnits for code coverage (model, admin)
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>
Diffstat (limited to 'POLICY-SDK-APP/src')
3 files changed, 184 insertions, 6 deletions
diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java index 244901a3f..e943883f8 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyManagerServletTest.java @@ -22,23 +22,28 @@ 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)); + } } diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyUserInfoControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyUserInfoControllerTest.java index bf4b67683..55e4b5a93 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyUserInfoControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/admin/PolicyUserInfoControllerTest.java @@ -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(); + } } diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/model/PDPGroupContainerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/model/PDPGroupContainerTest.java index f184bde1b..5204fc414 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/model/PDPGroupContainerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/model/PDPGroupContainerTest.java @@ -21,8 +21,12 @@ 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)); + } +} |