41367a16ee0e6af09d92da65c99b668054afbb84
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.IOException;
44 import java.io.PrintWriter;
45 import java.io.StringWriter;
46 import java.util.ArrayList;
47 import java.util.List;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51
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;
72
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({ SystemProperties.class, AppUtils.class ,UserUtils.class})
75 public class ProfileControllerTest {
76
77         @InjectMocks
78         ProfileController profileController = new ProfileController();
79                 
80         @Mock
81         UserProfileService service;
82         
83         @Mock
84         UserService userService;
85         
86         @Mock
87         RoleService roleService;
88         
89         @Mock
90         private SharedContextRestClient sharedContextRestClient;
91                  
92         @Before
93         public void setup() {
94                 MockitoAnnotations.initMocks(this);
95         }
96         
97         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
98         
99         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
100         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
101         
102         NullPointerException nullPointerException = new NullPointerException();
103         
104         User user = new User();
105         
106         @Mock
107         UserUtils userUtils = new UserUtils();
108         
109         @Test
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());
124         }
125         
126         @Test
127         public void profileRequestURITest() throws IOException{
128                 ModelAndView actualModelAndView = new ModelAndView("profile");
129                 User user = new User();
130                 user.setOrgUserId("test");
131                 int profileId =  1;
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());
142         }
143         
144         @Test
145         public void profileExceptionTest() throws IOException{
146                 ModelAndView actualModelAndView = new ModelAndView("profile");
147                 User profile = null;
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());
155         }
156         
157         @Test
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());
166         }
167         
168         @Test
169         public void selfProfileExceptionTest() throws Exception{
170                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
171                 profileController.selfProfile(mockedRequest);
172         }
173
174         @SuppressWarnings("rawtypes")
175         @Test
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());
182         }
183         
184         @Test
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);
194         }
195         
196         @Test
197         public void getSelfProfileExceptionTest(){
198                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
199                 profileController.getSelfProfile(mockedRequest, mockedResponse);
200         }
201                 
202         @Test
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);
218         }
219         
220         @Test
221         public void getUserExceptionTest(){
222                 profileController.getUser(mockedRequest, mockedResponse);
223         }
224         
225         /*@Test
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));
233         }*/
234         
235         @Test
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));
241         }
242         
243         /*@SuppressWarnings("unchecked")
244         @Test
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);
250         }*/
251         
252         /*@Test
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);
258         }*/
259         
260         @Test
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);
266         }
267         
268         /*@SuppressWarnings("unchecked")
269         @Test
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);
275         }*/
276         
277         /*@Test
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);
283         }*/
284         
285         @Test
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);
291         }
292         
293         @Test
294         public void getViewNameTest(){
295                 String actualResult = null;
296                 profileController.setViewName(null);
297                 String expectedResult = profileController.getViewName();
298                 assertEquals(actualResult, expectedResult);
299         }
300         
301         @SuppressWarnings({ "rawtypes", "null", "unchecked" })
302         @Test
303         public void getAvailableRolesTest() throws IOException{ 
304                 List actualList = null;         
305                 List list = null;
306                 Mockito.when(roleService.getAvailableRoles(null)).thenReturn(list);
307                 List expectedList = profileController.getAvailableRoles(null);
308                 assertEquals(actualList, expectedList);
309         }
310 }