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