97458f27bbe80957b3daa2156230cddc0699dbb3
[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
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.domain.User;
59 import org.onap.portalsdk.core.restful.client.SharedContextRestClient;
60 import org.onap.portalsdk.core.service.RoleService;
61 import org.onap.portalsdk.core.service.UserProfileService;
62 import org.onap.portalsdk.core.service.UserService;
63 import org.onap.portalsdk.core.web.support.UserUtils;
64 import org.springframework.web.servlet.ModelAndView;
65
66 public class ProfileSearchControllerTest {
67
68         @InjectMocks
69         ProfileSearchController profileSearchController = new ProfileSearchController();
70                 
71         @Mock
72         UserProfileService service;
73         
74         @Mock
75         UserService userService;
76         
77         @Mock
78         RoleService roleService;
79         
80         @Mock
81         private SharedContextRestClient sharedContextRestClient;
82                  
83         @Before
84         public void setup() {
85                 MockitoAnnotations.initMocks(this);
86         }
87         
88         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
89         
90         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
91         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
92         
93         NullPointerException nullPointerException = new NullPointerException();
94         
95         User user = new User();
96         
97         @Mock
98         UserUtils userUtils = new UserUtils();
99         
100         @Test
101         public void profileSearchTest(){
102                 ModelAndView actualModelAndView = new ModelAndView();
103                 List<User> userList = new ArrayList<User>();
104                 Mockito.when(service.findAll()).thenReturn(userList);
105                 ModelAndView expectedModelAndView = profileSearchController.profileSearch(mockedRequest);
106                 assertEquals(actualModelAndView.getViewName(), expectedModelAndView.getViewName());
107         }
108         
109         @Test
110         public void profileSearchExceptionTest(){
111                 ModelAndView actualModelAndView = new ModelAndView();
112                 List<User> userList = new ArrayList<User>();
113                 Mockito.when(service.findAll()).thenThrow(nullPointerException);
114                 profileSearchController.profileSearch(mockedRequest);
115         }
116         
117         @Test
118         public void getUserTest() throws IOException{
119                 List<User> profileList = null;
120                 StringWriter sw = new StringWriter();
121                 PrintWriter writer = new PrintWriter(sw);
122                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
123                 Mockito.when(service.findAll()).thenReturn(profileList);
124                 profileSearchController.getUser(mockedRequest, mockedResponse);
125         }
126         
127         @Test
128         public void getUserExceptionTest(){
129                 List<User> profileList = null;
130                 Mockito.when(service.findAll()).thenReturn(profileList);
131                 profileSearchController.getUser(mockedRequest, mockedResponse);
132         }
133         
134         @Test
135         public void getUserPaginationTest() throws IOException{
136                 List<User> profileList = new ArrayList<User>();
137                 Mockito.when(mockedRequest.getParameter("pageNum")).thenReturn("1");
138                 Mockito.when(mockedRequest.getParameter("viewPerPage")).thenReturn("1");
139                 Mockito.when(service.findAll()).thenReturn(profileList);
140                 StringWriter sw = new StringWriter();
141                 PrintWriter writer = new PrintWriter(sw);
142                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
143                 profileSearchController.getUserPagination(mockedRequest, mockedResponse);
144         }
145         
146         @Test
147         public void getUserPaginationExceptionTest(){
148                 List<User> profileList = null;
149                 Mockito.when(mockedRequest.getParameter("pageNum")).thenReturn("1");
150                 Mockito.when(mockedRequest.getParameter("viewPerPage")).thenReturn("1");
151                 Mockito.when(service.findAll()).thenReturn(profileList);
152                 profileSearchController.getUserPagination(mockedRequest, mockedResponse);
153         }
154         
155         @Test
156         public void toggleProfileActiveTest() throws IOException{
157                 User user = new User();
158                 Mockito.when(mockedRequest.getParameter("profile_id")).thenReturn("1");
159                 Mockito.when(userService.getUser("1")).thenReturn(user);
160                 StringWriter sw = new StringWriter();
161                 PrintWriter writer = new PrintWriter(sw);
162                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
163                 profileSearchController.toggleProfileActive(mockedRequest, mockedResponse);
164         }
165         
166         @Test
167         public void toggleProfileActiveExceptionTest() throws IOException{              
168                 profileSearchController.toggleProfileActive(mockedRequest, mockedResponse);
169         }
170 }