cc672156525e87d5593f85da3f6d3bfcc0aedb5c
[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
42 import java.io.IOException;
43 import java.io.PrintWriter;
44 import java.io.StringWriter;
45 import java.util.ArrayList;
46 import java.util.List;
47
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.onap.portalapp.framework.MockitoTestSuite;
58 import org.onap.portalsdk.core.auth.LoginStrategy;
59 import org.onap.portalsdk.core.domain.User;
60 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
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.web.support.UserUtils;
66 import org.springframework.web.servlet.ModelAndView;
67
68 public class ProfileSearchControllerTest {
69
70         @InjectMocks
71         ProfileSearchController profileSearchController = new ProfileSearchController();
72                 
73         @Mock
74         UserProfileService service;
75         
76         @Mock
77         UserService userService;
78         
79         @Mock
80         RoleService roleService;
81         
82         @Mock
83         private SharedContextRestClient sharedContextRestClient;
84         
85         @Mock
86         LoginStrategy loginStrategy;
87                  
88         @Before
89         public void setup() {
90                 MockitoAnnotations.initMocks(this);
91         }
92         
93         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
94         
95         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
96         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
97         
98         NullPointerException nullPointerException = new NullPointerException();
99         
100         User user = new User();
101         
102         @Mock
103         UserUtils userUtils = new UserUtils();
104         
105         @Test
106         public void profileSearchTest(){
107                 ModelAndView actualModelAndView = new ModelAndView();
108                 List<User> userList = new ArrayList<User>();
109                 Mockito.when(service.findAll()).thenReturn(userList);
110                 ModelAndView expectedModelAndView = profileSearchController.profileSearch(mockedRequest);
111                 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
112         }
113         
114         @Test
115         public void profileSearchExceptionTest(){
116                 ModelAndView actualModelAndView = new ModelAndView();
117                 List<User> userList = new ArrayList<User>();
118                 Mockito.when(service.findAll()).thenThrow(nullPointerException);
119                 profileSearchController.profileSearch(mockedRequest);
120         }
121         
122         @Test
123         public void getUserTest() throws IOException, PortalAPIException{
124                 List<User> profileList = new ArrayList<>();
125                 User user = new User();
126                 user.setOrgUserId("test");
127                 StringWriter sw = new StringWriter();
128                 PrintWriter writer = new PrintWriter(sw);
129                 Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test");
130                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
131                 Mockito.when(service.findAll()).thenReturn(profileList);
132                 profileSearchController.getUser(mockedRequest, mockedResponse);
133         }
134         
135         @Test
136         public void getUserExceptionTest() throws IOException, PortalAPIException{
137                 List<User> profileList = null;
138                 User user = new User();
139                 user.setOrgUserId("test");
140                 StringWriter sw = new StringWriter();
141                 PrintWriter writer = new PrintWriter(sw);
142                 Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test");
143                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
144                 Mockito.when(service.findAll()).thenReturn(profileList);
145                 profileSearchController.getUser(mockedRequest, mockedResponse);
146         }
147         
148         @Test
149         public void getUserPaginationTest() throws IOException{
150                 List<User> profileList = new ArrayList<User>();
151                 Mockito.when(mockedRequest.getParameter("pageNum")).thenReturn("1");
152                 Mockito.when(mockedRequest.getParameter("viewPerPage")).thenReturn("1");
153                 Mockito.when(service.findAll()).thenReturn(profileList);
154                 StringWriter sw = new StringWriter();
155                 PrintWriter writer = new PrintWriter(sw);
156                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
157                 profileSearchController.getUserPagination(mockedRequest, mockedResponse);
158         }
159         
160         @Test
161         public void getUserPaginationExceptionTest(){
162                 List<User> profileList = null;
163                 Mockito.when(mockedRequest.getParameter("pageNum")).thenReturn("1");
164                 Mockito.when(mockedRequest.getParameter("viewPerPage")).thenReturn("1");
165                 Mockito.when(service.findAll()).thenReturn(profileList);
166                 profileSearchController.getUserPagination(mockedRequest, mockedResponse);
167         }
168         
169         @Test
170         public void toggleProfileActiveTest() throws IOException{
171                 User user = new User();
172                 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("1");
173                 Mockito.when(userService.getUser("1")).thenReturn(user);
174                 StringWriter sw = new StringWriter();
175                 PrintWriter writer = new PrintWriter(sw);
176                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
177                 profileSearchController.toggleProfileActive(mockedRequest, mockedResponse);
178         }
179         
180         @Test
181         public void toggleProfileActiveExceptionTest() throws IOException{              
182                 profileSearchController.toggleProfileActive(mockedRequest, mockedResponse);
183         }
184 }