2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.controller.core;
40 import static org.junit.Assert.assertEquals;
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;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
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;
68 public class ProfileSearchControllerTest {
71 ProfileSearchController profileSearchController = new ProfileSearchController();
74 UserProfileService service;
77 UserService userService;
80 RoleService roleService;
83 private SharedContextRestClient sharedContextRestClient;
86 LoginStrategy loginStrategy;
90 MockitoAnnotations.initMocks(this);
93 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
95 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
96 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
98 NullPointerException nullPointerException = new NullPointerException();
100 User user = new User();
103 UserUtils userUtils = new UserUtils();
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());
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);
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);
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);
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);
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);
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);
181 public void toggleProfileActiveExceptionTest() throws IOException{
182 profileSearchController.toggleProfileActive(mockedRequest, mockedResponse);