fc0871a998b1df3fd4a045985fb51db36364a6e6
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37 */
38 package org.onap.portalapp.controller.core;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
42
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;
50
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
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;
74
75 @RunWith(PowerMockRunner.class)
76 @PrepareForTest({ SystemProperties.class, AppUtils.class, UserUtils.class })
77 public class ProfileControllerTest {
78
79         @InjectMocks
80         ProfileController profileController = new ProfileController();
81
82         @Mock
83         UserProfileService service;
84
85         @Mock
86         UserService userService;
87
88         @Mock
89         RoleService roleService;
90
91         @Mock
92         private SharedContextRestClient sharedContextRestClient;
93
94         @Before
95         public void setup() {
96                 MockitoAnnotations.initMocks(this);
97         }
98
99         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
100
101         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
102         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
103
104         NullPointerException nullPointerException = new NullPointerException();
105
106         User user = new User();
107
108         @Mock
109         UserUtils userUtils = new UserUtils();
110
111         @Test
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());
127         }
128
129         @Test
130         public void profileRequestURITest() throws IOException {
131                 ModelAndView actualModelAndView = new ModelAndView("profile");
132                 User user = new User();
133                 user.setOrgUserId("test");
134                 int profileId = 1;
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());
146         }
147
148         @Test
149         public void profileExceptionTest() throws IOException {
150                 ModelAndView actualModelAndView = new ModelAndView("profile");
151                 User profile = null;
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());
159         }
160
161         @Test
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());
171         }
172
173         @Test
174         public void selfProfileExceptionTest() throws Exception {
175                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
176                 profileController.selfProfile(mockedRequest);
177         }
178
179         @SuppressWarnings("rawtypes")
180         @Test
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());
188         }
189
190         @Test
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);
201         }
202
203         @Test
204         public void getSelfProfileExceptionTest() {
205                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
206                 profileController.getSelfProfile(mockedRequest, mockedResponse);
207         }
208
209         @Test
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);
226         }
227
228         @Test
229         public void getUserExceptionTest() {
230                 profileController.getUser(mockedRequest, mockedResponse);
231         }
232
233         @Test
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));
243         }
244
245         @Test
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));
251         }
252
253         @Test
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));
265         }
266
267         @Test
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);
273         }
274
275         @Test
276         public void addNewRoleTest() throws IOException {
277                 
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)));
280
281                 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("123");
282                 Mockito.when(userService.getUser(String.valueOf("123"))).thenReturn(new User());
283
284                 PowerMockito.mockStatic(SystemProperties.class);
285                 Mockito.when(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)).thenReturn("123");
286                 
287                 StringWriter sw = new StringWriter();
288                 PrintWriter writer = new PrintWriter(sw);
289                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
290                 assertNull(profileController.addNewRole(mockedRequest, mockedResponse));
291         }
292
293         @Test
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);
299         }
300
301         @Test
302         public void getViewNameTest() {
303                 String actualResult = null;
304                 profileController.setViewName(null);
305                 String expectedResult = profileController.getViewName();
306                 assertEquals(actualResult, expectedResult);
307         }
308
309         @SuppressWarnings({ "rawtypes", "null", "unchecked" })
310         @Test
311         public void getAvailableRolesTest() throws IOException {
312                 List actualList = null;
313                 List list = null;
314                 Mockito.when(roleService.getAvailableRoles(null)).thenReturn(list);
315                 List expectedList = profileController.getAvailableRoles(null);
316                 assertEquals(actualList, expectedList);
317         }
318 }