2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software 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.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller.core;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
43 import java.io.BufferedReader;
44 import java.io.IOException;
45 import java.io.PrintWriter;
46 import java.io.StringReader;
47 import java.io.StringWriter;
48 import java.util.ArrayList;
49 import java.util.List;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.InjectMocks;
58 import org.mockito.Mock;
59 import org.mockito.Mockito;
60 import org.mockito.MockitoAnnotations;
61 import org.onap.portalapp.framework.MockitoTestSuite;
62 import org.onap.portalsdk.core.domain.User;
63 import org.onap.portalsdk.core.restful.client.SharedContextRestClient;
64 import org.onap.portalsdk.core.service.RoleService;
65 import org.onap.portalsdk.core.service.UserProfileService;
66 import org.onap.portalsdk.core.service.UserService;
67 import org.onap.portalsdk.core.util.SystemProperties;
68 import org.onap.portalsdk.core.web.support.AppUtils;
69 import org.onap.portalsdk.core.web.support.UserUtils;
70 import org.powermock.api.mockito.PowerMockito;
71 import org.powermock.core.classloader.annotations.PrepareForTest;
72 import org.powermock.modules.junit4.PowerMockRunner;
73 import org.springframework.web.servlet.ModelAndView;
75 @RunWith(PowerMockRunner.class)
76 @PrepareForTest({ SystemProperties.class, AppUtils.class, UserUtils.class })
77 public class ProfileControllerTest {
80 ProfileController profileController = new ProfileController();
83 UserProfileService service;
86 UserService userService;
89 RoleService roleService;
92 private SharedContextRestClient sharedContextRestClient;
96 MockitoAnnotations.initMocks(this);
99 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
101 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
102 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
104 NullPointerException nullPointerException = new NullPointerException();
106 User user = new User();
109 UserUtils userUtils = new UserUtils();
112 public void profileTest() throws IOException {
113 ModelAndView actualModelAndView = new ModelAndView("profile");
114 User user = new User();
115 user.setOrgUserId("test");
116 Long profileId = null;
117 PowerMockito.mockStatic(AppUtils.class);
118 PowerMockito.mockStatic(UserUtils.class);
119 Mockito.when(mockedRequest.getRequestURI()).thenReturn("self_profile.htm");
120 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("test");
121 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
122 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD"))
123 .thenReturn(new ArrayList<>());
124 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(user);
125 ModelAndView expectedModelAndView = profileController.profile(mockedRequest);
126 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
130 public void profileRequestURITest() throws IOException {
131 ModelAndView actualModelAndView = new ModelAndView("profile");
132 User user = new User();
133 user.setOrgUserId("test");
135 PowerMockito.mockStatic(AppUtils.class);
136 PowerMockito.mockStatic(UserUtils.class);
137 Mockito.when(mockedRequest.getRequestURI()).thenReturn("test");
138 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("1");
139 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
140 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(user);
141 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD"))
142 .thenReturn(new ArrayList<>());
143 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(user);
144 ModelAndView expectedModelAndView = profileController.profile(mockedRequest);
145 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
149 public void profileExceptionTest() throws IOException {
150 ModelAndView actualModelAndView = new ModelAndView("profile");
152 Long profileId = null;
153 Mockito.when(mockedRequest.getRequestURI()).thenReturn("self_profile.htm");
154 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("test");
155 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
156 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(profile);
157 ModelAndView expectedModelAndView = profileController.profile(mockedRequest);
158 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
162 public void selfProfileTest() throws Exception {
163 ModelAndView actualModelAndView = new ModelAndView("profile");
164 PowerMockito.mockStatic(AppUtils.class);
165 PowerMockito.mockStatic(UserUtils.class);
166 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD"))
167 .thenReturn(new ArrayList<>());
168 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
169 ModelAndView expectedModelAndView = profileController.selfProfile(mockedRequest);
170 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
174 public void selfProfileExceptionTest() throws Exception {
175 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
176 profileController.selfProfile(mockedRequest);
179 @SuppressWarnings("rawtypes")
181 public void getStatesTest() {
182 List actualList = new ArrayList();
183 PowerMockito.mockStatic(AppUtils.class);
184 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD"))
185 .thenReturn(new ArrayList<>());
186 List expectedlist = profileController.getStates();
187 assertEquals(actualList.size(), expectedlist.size());
191 public void getSelfProfileTest() throws IOException {
192 PowerMockito.mockStatic(AppUtils.class);
193 PowerMockito.mockStatic(UserUtils.class);
194 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
195 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD"))
196 .thenReturn(new ArrayList<>());
197 StringWriter sw = new StringWriter();
198 PrintWriter writer = new PrintWriter(sw);
199 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
200 profileController.getSelfProfile(mockedRequest, mockedResponse);
204 public void getSelfProfileExceptionTest() {
205 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
206 profileController.getSelfProfile(mockedRequest, mockedResponse);
210 public void getUserTest() throws IOException {
211 User user = new User();
212 user.setOrgUserId("test");
213 Long profileId = null;
214 PowerMockito.mockStatic(AppUtils.class);
215 PowerMockito.mockStatic(UserUtils.class);
216 Mockito.when(mockedRequest.getRequestURI()).thenReturn("self_profile.htm");
217 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("test");
218 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
219 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD"))
220 .thenReturn(new ArrayList<>());
221 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(user);
222 StringWriter sw = new StringWriter();
223 PrintWriter writer = new PrintWriter(sw);
224 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
225 profileController.getUser(mockedRequest, mockedResponse);
229 public void getUserExceptionTest() {
230 profileController.getUser(mockedRequest, mockedResponse);
234 public void saveProfileTest() throws IOException {
235 String json = "{ \"profile\": {\"firstName\": \"Test\" }, \"selectedCountry\" : \"USA\", \"selectedState\" : \"DC\", \"selectedTimeZone\" : \"12345678920\", \"role\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}";
236 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
237 StringWriter sw = new StringWriter();
238 PrintWriter writer = new PrintWriter(sw);
239 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
240 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("123");
241 Mockito.when(userService.getUser(String.valueOf("123"))).thenReturn(new User());
242 assertNull(profileController.saveProfile(mockedRequest, mockedResponse));
246 public void saveProfilePrintWriterExceptionTest() throws IOException {
247 StringWriter sw = new StringWriter();
248 PrintWriter writer = new PrintWriter(sw);
249 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
250 assertNull(profileController.saveProfile(mockedRequest, mockedResponse));
254 public void removeRoleTest() throws IOException {
255 String json = "{ \"profile\": {\"firstName\": \"Test\" }, \"selectedCountry\" : \"USA\", \"selectedState\" : \"DC\", \"selectedTimeZone\" : \"12345678920\", \"role\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}";
256 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
257 StringWriter sw = new StringWriter();
258 PrintWriter writer = new PrintWriter(sw);
259 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
260 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("123");
261 Mockito.when(userService.getUser(String.valueOf("123"))).thenReturn(new User());
262 PowerMockito.mockStatic(SystemProperties.class);
263 Mockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("123");
264 assertNull(profileController.removeRole(mockedRequest, mockedResponse));
268 public void removeRolePrintWriterExceptionTest() throws IOException {
269 StringWriter sw = new StringWriter();
270 PrintWriter writer = new PrintWriter(sw);
271 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
272 profileController.removeRole(mockedRequest, mockedResponse);
276 public void addNewRoleTest() throws IOException {
278 String json = "{ \"profile\": {\"firstName\": \"Test\" }, \"selectedCountry\" : \"USA\", \"selectedState\" : \"DC\", \"selectedTimeZone\" : \"12345678920\", \"role\":{\"id\":1,\"created\":null,\"modified\":null,\"createdId\":null,\"modifiedId\":null,\"rowNum\":null,\"auditUserId\":null,\"auditTrail\":null,\"name\":\"test1\",\"active\":false,\"priority\":\"1\",\"roleFunctions\":[],\"childRoles\":[],\"editUrl\":\"/role.htm?role_id=1\",\"toggleActiveImage\":\"/static/fusion/images/inactive.png\",\"toggleActiveAltText\":\"Click to Activate Role\"},\"childRoles\":[],\"roleFunctions\":[]}";
279 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
281 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("123");
282 Mockito.when(userService.getUser(String.valueOf("123"))).thenReturn(new User());
284 PowerMockito.mockStatic(SystemProperties.class);
285 Mockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("123");
287 StringWriter sw = new StringWriter();
288 PrintWriter writer = new PrintWriter(sw);
289 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
290 assertNull(profileController.addNewRole(mockedRequest, mockedResponse));
294 public void addNewRoleExceptionTest() throws IOException {
295 StringWriter sw = new StringWriter();
296 PrintWriter writer = new PrintWriter(sw);
297 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
298 profileController.addNewRole(mockedRequest, mockedResponse);
302 public void getViewNameTest() {
303 String actualResult = null;
304 profileController.setViewName(null);
305 String expectedResult = profileController.getViewName();
306 assertEquals(actualResult, expectedResult);
309 @SuppressWarnings({ "rawtypes", "null", "unchecked" })
311 public void getAvailableRolesTest() throws IOException {
312 List actualList = null;
314 Mockito.when(roleService.getAvailableRoles(null)).thenReturn(list);
315 List expectedList = profileController.getAvailableRoles(null);
316 assertEquals(actualList, expectedList);