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.IOException;
44 import java.io.PrintWriter;
45 import java.io.StringWriter;
46 import java.util.ArrayList;
47 import java.util.List;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.mockito.InjectMocks;
56 import org.mockito.Mock;
57 import org.mockito.Mockito;
58 import org.mockito.MockitoAnnotations;
59 import org.onap.portalapp.framework.MockitoTestSuite;
60 import org.onap.portalsdk.core.domain.User;
61 import org.onap.portalsdk.core.restful.client.SharedContextRestClient;
62 import org.onap.portalsdk.core.service.RoleService;
63 import org.onap.portalsdk.core.service.UserProfileService;
64 import org.onap.portalsdk.core.service.UserService;
65 import org.onap.portalsdk.core.util.SystemProperties;
66 import org.onap.portalsdk.core.web.support.AppUtils;
67 import org.onap.portalsdk.core.web.support.UserUtils;
68 import org.powermock.api.mockito.PowerMockito;
69 import org.powermock.core.classloader.annotations.PrepareForTest;
70 import org.powermock.modules.junit4.PowerMockRunner;
71 import org.springframework.web.servlet.ModelAndView;
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({ SystemProperties.class, AppUtils.class ,UserUtils.class})
75 public class ProfileControllerTest {
78 ProfileController profileController = new ProfileController();
81 UserProfileService service;
84 UserService userService;
87 RoleService roleService;
90 private SharedContextRestClient sharedContextRestClient;
94 MockitoAnnotations.initMocks(this);
97 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
99 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
100 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
102 NullPointerException nullPointerException = new NullPointerException();
104 User user = new User();
107 UserUtils userUtils = new UserUtils();
110 public void profileTest() throws IOException{
111 ModelAndView actualModelAndView = new ModelAndView("profile");
112 User user = new User();
113 user.setOrgUserId("test");
114 Long profileId = null;
115 PowerMockito.mockStatic(AppUtils.class);
116 PowerMockito.mockStatic(UserUtils.class);
117 Mockito.when(mockedRequest.getRequestURI()).thenReturn("self_profile.htm");
118 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("test");
119 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
120 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD")).thenReturn(new ArrayList<>());
121 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(user);
122 ModelAndView expectedModelAndView = profileController.profile(mockedRequest);
123 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
127 public void profileRequestURITest() throws IOException{
128 ModelAndView actualModelAndView = new ModelAndView("profile");
129 User user = new User();
130 user.setOrgUserId("test");
132 PowerMockito.mockStatic(AppUtils.class);
133 PowerMockito.mockStatic(UserUtils.class);
134 Mockito.when(mockedRequest.getRequestURI()).thenReturn("test");
135 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("1");
136 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
137 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(user);
138 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD")).thenReturn(new ArrayList<>());
139 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(user);
140 ModelAndView expectedModelAndView = profileController.profile(mockedRequest);
141 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
145 public void profileExceptionTest() throws IOException{
146 ModelAndView actualModelAndView = new ModelAndView("profile");
148 Long profileId = null;
149 Mockito.when(mockedRequest.getRequestURI()).thenReturn("self_profile.htm");
150 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("test");
151 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
152 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(profile);
153 ModelAndView expectedModelAndView = profileController.profile(mockedRequest);
154 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
158 public void selfProfileTest() throws Exception{
159 ModelAndView actualModelAndView = new ModelAndView("profile");
160 PowerMockito.mockStatic(AppUtils.class);
161 PowerMockito.mockStatic(UserUtils.class);
162 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD")).thenReturn(new ArrayList<>());
163 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
164 ModelAndView expectedModelAndView = profileController.selfProfile(mockedRequest);
165 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
169 public void selfProfileExceptionTest() throws Exception{
170 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
171 profileController.selfProfile(mockedRequest);
174 @SuppressWarnings("rawtypes")
176 public void getStatesTest(){
177 List actualList = new ArrayList();
178 PowerMockito.mockStatic(AppUtils.class);
179 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD")).thenReturn(new ArrayList<>());
180 List expectedlist =profileController.getStates();
181 assertEquals(actualList.size(), expectedlist.size());
185 public void getSelfProfileTest() throws IOException{
186 PowerMockito.mockStatic(AppUtils.class);
187 PowerMockito.mockStatic(UserUtils.class);
188 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
189 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD")).thenReturn(new ArrayList<>());
190 StringWriter sw = new StringWriter();
191 PrintWriter writer = new PrintWriter(sw);
192 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
193 profileController.getSelfProfile(mockedRequest, mockedResponse);
197 public void getSelfProfileExceptionTest(){
198 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
199 profileController.getSelfProfile(mockedRequest, mockedResponse);
203 public void getUserTest() throws IOException{
204 User user = new User();
205 user.setOrgUserId("test");
206 Long profileId = null;
207 PowerMockito.mockStatic(AppUtils.class);
208 PowerMockito.mockStatic(UserUtils.class);
209 Mockito.when(mockedRequest.getRequestURI()).thenReturn("self_profile.htm");
210 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("test");
211 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
212 Mockito.when(AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD")).thenReturn(new ArrayList<>());
213 Mockito.when(userService.getUser(String.valueOf(profileId))).thenReturn(user);
214 StringWriter sw = new StringWriter();
215 PrintWriter writer = new PrintWriter(sw);
216 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
217 profileController.getUser(mockedRequest, mockedResponse);
221 public void getUserExceptionTest(){
222 profileController.getUser(mockedRequest, mockedResponse);
226 public void saveProfileTest() throws IOException{
227 String json = "{\"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\":[]}";
228 Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
229 StringWriter sw = new StringWriter();
230 PrintWriter writer = new PrintWriter(sw);
231 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
232 assertNull(profileController.saveProfile(mockedRequest, mockedResponse));
236 public void saveProfilePrintWriterExceptionTest() throws IOException{
237 StringWriter sw = new StringWriter();
238 PrintWriter writer = new PrintWriter(sw);
239 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
240 assertNull(profileController.saveProfile(mockedRequest, mockedResponse));
243 /*@SuppressWarnings("unchecked")
245 public void saveProfileExceptionTest() throws IOException{
246 StringWriter sw = new StringWriter();
247 PrintWriter writer = new PrintWriter(sw);
248 Mockito.when(mockedResponse.getWriter()).thenThrow(IOException.class);
249 profileController.saveProfile(mockedRequest, mockedResponse);
253 public void removeRoleTest() throws IOException{
254 StringWriter sw = new StringWriter();
255 PrintWriter writer = new PrintWriter(sw);
256 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
257 profileController.removeRole(mockedRequest, mockedResponse);
261 public void removeRolePrintWriterExceptionTest() throws IOException{
262 StringWriter sw = new StringWriter();
263 PrintWriter writer = new PrintWriter(sw);
264 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
265 profileController.removeRole(mockedRequest, mockedResponse);
268 /*@SuppressWarnings("unchecked")
270 public void removeRoleExceptionTest() throws IOException{
271 StringWriter sw = new StringWriter();
272 PrintWriter writer = new PrintWriter(sw);
273 Mockito.when(mockedResponse.getWriter()).thenThrow(IOException.class);
274 profileController.removeRole(mockedRequest, mockedResponse);
278 public void addNewRoleTest() throws IOException{
279 StringWriter sw = new StringWriter();
280 PrintWriter writer = new PrintWriter(sw);
281 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
282 profileController.addNewRole(mockedRequest, mockedResponse);
286 public void addNewRoleExceptionTest() throws IOException{
287 StringWriter sw = new StringWriter();
288 PrintWriter writer = new PrintWriter(sw);
289 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
290 profileController.addNewRole(mockedRequest, mockedResponse);
294 public void getViewNameTest(){
295 String actualResult = null;
296 profileController.setViewName(null);
297 String expectedResult = profileController.getViewName();
298 assertEquals(actualResult, expectedResult);
301 @SuppressWarnings({ "rawtypes", "null", "unchecked" })
303 public void getAvailableRolesTest() throws IOException{
304 List actualList = null;
306 Mockito.when(roleService.getAvailableRoles(null)).thenReturn(list);
307 List expectedList = profileController.getAvailableRoles(null);
308 assertEquals(actualList, expectedList);