2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.controller;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.atLeast;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import com.att.research.xacml.api.pap.PAPException;
33 import com.mockrunner.mock.web.MockHttpServletRequest;
34 import com.mockrunner.mock.web.MockHttpServletResponse;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashSet;
38 import java.util.List;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import javax.servlet.http.HttpSession;
43 import org.junit.BeforeClass;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.junit.rules.ExpectedException;
47 import org.junit.runner.RunWith;
48 import org.mockito.Mockito;
49 import org.onap.policy.model.Roles;
50 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
51 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
52 import org.onap.portalsdk.core.domain.User;
53 import org.onap.portalsdk.core.web.support.AppUtils;
54 import org.onap.portalsdk.core.web.support.UserUtils;
55 import org.powermock.api.mockito.PowerMockito;
56 import org.powermock.core.classloader.annotations.PowerMockIgnore;
57 import org.powermock.core.classloader.annotations.PrepareForTest;
58 import org.powermock.modules.junit4.PowerMockRunner;
60 @RunWith(PowerMockRunner.class)
61 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
62 public class AutoPushControllerTest {
63 private PolicyController controller = new PolicyController();
64 private AutoPushController apController = new AutoPushController();
66 private static HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
67 private static HttpServletResponse mockResponse = Mockito.mock(HttpServletResponse.class);
68 private static PolicyController mockPolicyController = Mockito.mock(PolicyController.class);
69 private static HttpSession mockSession = Mockito.mock(HttpSession.class);
70 private static User mockUser = Mockito.mock(User.class);
71 private static List<Object> rolesList = new ArrayList<>();
74 public ExpectedException thrown = ExpectedException.none();
77 public static void setupMocks() {
78 when(mockRequest.getSession(false)).thenReturn(mockSession);
79 when(AppUtils.getSession(mockRequest)).thenReturn(mockSession);
80 when(UserUtils.getUserSession(mockRequest)).thenReturn(mockUser);
81 when(mockUser.getOrgUserId()).thenReturn("");
82 when(mockPolicyController.getRoles(any(String.class))).thenReturn(rolesList);
86 public void testAutoPushSetGet() throws IOException {
87 apController.setPolicyController(controller);
88 assertEquals(apController.getPolicyController(), controller);
92 public void testNegativeCase1() {
94 apController.getPolicyGroupContainerData(null, null);
95 } catch (Exception ex) {
96 fail("No exceptions expected, received: " + ex);
101 public void testNegativeCase2() throws IOException {
102 thrown.expect(NullPointerException.class);
103 apController.pushPolicyToPDPGroup(null, null);
107 public void testNegativeCase3() throws IOException {
108 thrown.expect(NullPointerException.class);
109 apController.removePDPGroup(null, null);
112 @Test(expected = NullPointerException.class)
113 public void testRefreshGroupsNull() throws IOException {
114 apController.refreshGroups();
117 @PrepareForTest({UserUtils.class})
119 public void testRequests() throws Exception {
120 // Mock user utilities
121 PowerMockito.mockStatic(UserUtils.class);
122 User user = new User();
123 Mockito.when(UserUtils.getUserSession(Mockito.any())).thenReturn(user);
125 // Mock policy controller
126 PolicyController policyController = Mockito.mock(PolicyController.class);
127 PowerMockito.whenNew(PolicyController.class).withNoArguments().thenReturn(policyController);
128 Mockito.when(policyController.getRoles(Mockito.any())).thenReturn(null);
130 // Test group container
131 MockHttpServletRequest request = new MockHttpServletRequest();
132 MockHttpServletResponse response = new MockHttpServletResponse();
133 apController.getPolicyGroupContainerData(request, response);
134 assertEquals(HttpServletResponse.SC_OK, response.getStatusCode());
137 apController.pushPolicyToPDPGroup(request, response);
138 assertEquals(HttpServletResponse.SC_OK, response.getStatusCode());
141 apController.removePDPGroup(request, response);
142 assertEquals(HttpServletResponse.SC_OK, response.getStatusCode());
146 public void testRefreshGroupsSuccess() throws PAPException {
147 PolicyController mockPolicyController = Mockito.mock(PolicyController.class);
148 PAPPolicyEngine mockPAPPolicyEngine = Mockito.mock(PAPPolicyEngine.class);
149 Set<OnapPDPGroup> onapPDPGroups = new HashSet<>();
150 when(mockPolicyController.getPapEngine()).thenReturn(mockPAPPolicyEngine);
151 when(mockPAPPolicyEngine.getOnapPDPGroups()).thenReturn(onapPDPGroups);
152 apController.setPolicyController(mockPolicyController);
153 apController.refreshGroups();
154 verify(mockPolicyController).getPapEngine();
155 verify(mockPAPPolicyEngine).getOnapPDPGroups();
158 @SuppressWarnings("unchecked")
160 public void testRefreshGroups() throws PAPException {
161 PolicyController mockPolicyController = Mockito.mock(PolicyController.class);
162 PAPPolicyEngine mockPAPPolicyEngine = Mockito.mock(PAPPolicyEngine.class);
163 when(mockPolicyController.getPapEngine()).thenReturn(mockPAPPolicyEngine);
164 when(mockPAPPolicyEngine.getOnapPDPGroups()).thenThrow(PAPException.class);
165 apController.setPolicyController(mockPolicyController);
166 apController.refreshGroups();
167 verify(mockPolicyController).getPapEngine();
168 verify(mockPAPPolicyEngine).getOnapPDPGroups();
172 public void testGetPolicyGroupContainerData() throws Exception {
173 Roles superAdmin = new Roles();
174 Roles superEditor = new Roles();
175 Roles superGuest = new Roles();
176 rolesList.add(superAdmin);
177 rolesList.add(superEditor);
178 rolesList.add(superGuest);
180 apController.setPolicyController(mockPolicyController);
181 apController.getPolicyGroupContainerData(mockRequest, mockResponse);
183 verify(mockRequest, atLeast(1)).getSession(false);
184 verify(mockUser, atLeast(1)).getOrgUserId();
185 verify(mockPolicyController, atLeast(1)).getRoles(any(String.class));
189 public void testGetPolicyGroupContainerDataWithScope() throws Exception {
190 Roles superAdmin = new Roles();
191 superAdmin.setScope("super-admin-scope");
192 Roles superEditor = new Roles();
193 superEditor.setScope("super-editor-scope");
194 Roles superGuest = new Roles();
195 superGuest.setScope("super-guest-scope");
196 rolesList.add(superAdmin);
197 rolesList.add(superEditor);
198 rolesList.add(superGuest);
200 apController.setPolicyController(mockPolicyController);
201 apController.getPolicyGroupContainerData(mockRequest, mockResponse);
203 verify(mockRequest, atLeast(1)).getSession(false);
204 verify(mockUser, atLeast(1)).getOrgUserId();
205 verify(mockPolicyController, atLeast(1)).getRoles(any(String.class));
209 public void testGetPolicyGroupContainerDataWithRole() throws Exception {
210 Roles superAdmin = new Roles();
211 superAdmin.setRole("super-admin");
212 Roles superEditor = new Roles();
213 superEditor.setRole("super-editor");
214 Roles superGuest = new Roles();
215 superGuest.setRole("super-guest");
216 rolesList.add(superAdmin);
217 rolesList.add(superEditor);
218 rolesList.add(superGuest);
220 apController.setPolicyController(mockPolicyController);
221 apController.getPolicyGroupContainerData(mockRequest, mockResponse);
223 verify(mockRequest, atLeast(1)).getSession(false);
224 verify(mockUser, atLeast(1)).getOrgUserId();
225 verify(mockPolicyController, atLeast(1)).getRoles(any(String.class));