X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontroller%2FAutoPushControllerTest.java;fp=POLICY-SDK-APP%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontroller%2FAutoPushControllerTest.java;h=5e390da534d131f5732bc1932992b98f5ba1d99f;hb=aef55c6a2f2ca43547291640e92b4a6c54209d84;hp=64c3b04ff1baecca040ef8e14439090fda145d31;hpb=151ae967bf99589b4f17d0b57ee4385be82d91c0;p=policy%2Fengine.git diff --git a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java index 64c3b04ff..5e390da53 100644 --- a/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java +++ b/POLICY-SDK-APP/src/test/java/org/onap/policy/controller/AutoPushControllerTest.java @@ -24,20 +24,33 @@ package org.onap.policy.controller; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import com.att.research.xacml.api.pap.PAPException; import com.mockrunner.mock.web.MockHttpServletRequest; import com.mockrunner.mock.web.MockHttpServletResponse; - import java.io.IOException; - +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - +import javax.servlet.http.HttpSession; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.onap.policy.model.Roles; +import org.onap.policy.xacml.api.pap.OnapPDPGroup; +import org.onap.policy.xacml.api.pap.PAPPolicyEngine; import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.web.support.AppUtils; import org.onap.portalsdk.core.web.support.UserUtils; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -48,12 +61,27 @@ public class AutoPushControllerTest { private PolicyController controller = new PolicyController(); private AutoPushController apController = new AutoPushController(); + private static HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); + private static HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class); + private static PolicyController mockPolicyController = Mockito.mock(PolicyController.class); + private static HttpSession mockSession = Mockito.mock(HttpSession.class); + private static User mockUser = Mockito.mock(User.class); + private static List rolesList = new ArrayList<>(); + @Rule public ExpectedException thrown = ExpectedException.none(); + @BeforeClass + public static void setupMocks() { + when(mockRequest.getSession(false)).thenReturn(mockSession); + when(AppUtils.getSession(mockRequest)).thenReturn(mockSession); + when(UserUtils.getUserSession(mockRequest)).thenReturn(mockUser); + when(mockUser.getOrgUserId()).thenReturn(""); + when(mockPolicyController.getRoles(any(String.class))).thenReturn(rolesList); + } + @Test public void testAutoPushSetGet() throws IOException { - // Get and set tests apController.setPolicyController(controller); assertEquals(apController.getPolicyController(), controller); } @@ -80,7 +108,7 @@ public class AutoPushControllerTest { } @Test(expected = NullPointerException.class) - public void testRefresh() throws IOException { + public void testRefreshGroupsNull() throws IOException { apController.refreshGroups(); } @@ -111,4 +139,87 @@ public class AutoPushControllerTest { apController.removePDPGroup(request, response); assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); } + + @Test + public void testRefreshGroupsSuccess() throws PAPException { + PolicyController mockPolicyController = Mockito.mock(PolicyController.class); + PAPPolicyEngine mockPAPPolicyEngine = Mockito.mock(PAPPolicyEngine.class); + Set onapPDPGroups = new HashSet<>(); + when(mockPolicyController.getPapEngine()).thenReturn(mockPAPPolicyEngine); + when(mockPAPPolicyEngine.getOnapPDPGroups()).thenReturn(onapPDPGroups); + apController.setPolicyController(mockPolicyController); + apController.refreshGroups(); + verify(mockPolicyController).getPapEngine(); + verify(mockPAPPolicyEngine).getOnapPDPGroups(); + } + + @SuppressWarnings("unchecked") + @Test + public void testRefreshGroups() throws PAPException { + PolicyController mockPolicyController = Mockito.mock(PolicyController.class); + PAPPolicyEngine mockPAPPolicyEngine = Mockito.mock(PAPPolicyEngine.class); + when(mockPolicyController.getPapEngine()).thenReturn(mockPAPPolicyEngine); + when(mockPAPPolicyEngine.getOnapPDPGroups()).thenThrow(PAPException.class); + apController.setPolicyController(mockPolicyController); + apController.refreshGroups(); + verify(mockPolicyController).getPapEngine(); + verify(mockPAPPolicyEngine).getOnapPDPGroups(); + } + + @Test + public void testGetPolicyGroupContainerData() throws Exception { + Roles superAdmin = new Roles(); + Roles superEditor = new Roles(); + Roles superGuest = new Roles(); + rolesList.add(superAdmin); + rolesList.add(superEditor); + rolesList.add(superGuest); + + apController.setPolicyController(mockPolicyController); + apController.getPolicyGroupContainerData(mockRequest, mockResponse); + + verify(mockRequest, atLeast(1)).getSession(false); + verify(mockUser, atLeast(1)).getOrgUserId(); + verify(mockPolicyController, atLeast(1)).getRoles(any(String.class)); + } + + @Test + public void testGetPolicyGroupContainerDataWithScope() throws Exception { + Roles superAdmin = new Roles(); + superAdmin.setScope("super-admin-scope"); + Roles superEditor = new Roles(); + superEditor.setScope("super-editor-scope"); + Roles superGuest = new Roles(); + superGuest.setScope("super-guest-scope"); + rolesList.add(superAdmin); + rolesList.add(superEditor); + rolesList.add(superGuest); + + apController.setPolicyController(mockPolicyController); + apController.getPolicyGroupContainerData(mockRequest, mockResponse); + + verify(mockRequest, atLeast(1)).getSession(false); + verify(mockUser, atLeast(1)).getOrgUserId(); + verify(mockPolicyController, atLeast(1)).getRoles(any(String.class)); + } + + @Test + public void testGetPolicyGroupContainerDataWithRole() throws Exception { + Roles superAdmin = new Roles(); + superAdmin.setRole("super-admin"); + Roles superEditor = new Roles(); + superEditor.setRole("super-editor"); + Roles superGuest = new Roles(); + superGuest.setRole("super-guest"); + rolesList.add(superAdmin); + rolesList.add(superEditor); + rolesList.add(superGuest); + + apController.setPolicyController(mockPolicyController); + apController.getPolicyGroupContainerData(mockRequest, mockResponse); + + verify(mockRequest, atLeast(1)).getSession(false); + verify(mockUser, atLeast(1)).getOrgUserId(); + verify(mockPolicyController, atLeast(1)).getRoles(any(String.class)); + } }