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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller.core;
40 import static org.junit.Assert.*;
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.domain.RoleFunction;
59 import org.onap.portalsdk.core.domain.User;
60 import org.onap.portalsdk.core.service.RoleService;
61 import org.onap.portalsdk.core.web.support.UserUtils;
62 import org.springframework.web.bind.ServletRequestUtils;
63 import org.springframework.web.servlet.ModelAndView;
65 import com.fasterxml.jackson.databind.ObjectMapper;
67 public class RoleFunctionListControllerTest {
70 RoleFunctionListController roleFunctionListController = new RoleFunctionListController();
73 RoleService roleService;
77 MockitoAnnotations.initMocks(this);
80 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
82 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
83 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
84 NullPointerException nullPointerException = new NullPointerException();
87 UserUtils userUtils = new UserUtils();
90 ServletRequestUtils servletRequestUtils;
93 ObjectMapper mapper = new ObjectMapper();
96 public void welcomeTest() throws IOException {
97 roleFunctionListController.setViewName("Test");
98 User user = new User();
99 user.setOrgUserId("test12");
100 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
101 List<RoleFunction> roleFunctionList = new ArrayList<RoleFunction>();
102 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunctionList);
103 ModelAndView expectedResult = roleFunctionListController.welcome(mockedRequest);
104 assertEquals(expectedResult.getViewName(), "Test");
108 public void welcomeExceptionTest() throws IOException {
109 User user = new User();
110 user.setOrgUserId("test12");
111 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
112 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException);
113 ModelAndView expectedResult = roleFunctionListController.welcome(mockedRequest);
114 assertNull(expectedResult.getViewName());
118 public void getRoleFunctionListTest() throws IOException {
119 User user = new User();
120 user.setOrgUserId("test12");
121 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
122 List<RoleFunction> roleFunctionList = new ArrayList<RoleFunction>();
123 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunctionList);
124 StringWriter sw = new StringWriter();
125 PrintWriter writer = new PrintWriter(sw);
126 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
127 roleFunctionListController.getRoleFunctionList(mockedRequest, mockedResponse);
131 public void getRoleFunctionListExceptionTest() throws IOException {
132 User user = new User();
133 user.setOrgUserId("test12");
134 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
135 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException);
136 roleFunctionListController.getRoleFunctionList(mockedRequest, mockedResponse);
140 public void saveRoleFunctionTest() throws IOException {
141 User user = new User();
142 user.setOrgUserId("test12");
143 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
144 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
145 RoleFunction roleFunction = new RoleFunction();
146 Mockito.when(roleService.getRoleFunction(user.getOrgUserId(), "Test")).thenReturn(roleFunction);
147 StringWriter sw = new StringWriter();
148 PrintWriter writer = new PrintWriter(sw);
149 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
150 roleFunctionListController.saveRoleFunction(mockedRequest, mockedResponse, roleFun);
153 @Test(expected = java.io.IOException.class)
154 public void saveRoleFunctionExceptionTest() throws IOException {
155 User user = new User();
156 user.setOrgUserId("test12");
157 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
158 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
159 Mockito.when(roleService.getRoleFunction(user.getOrgUserId(), "Test")).thenThrow(nullPointerException);
160 roleFunctionListController.saveRoleFunction(mockedRequest, mockedResponse, roleFun);
164 public void addRoleFunctionTest() throws IOException {
165 User user = new User();
166 user.setOrgUserId("test12");
167 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
168 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
169 List<RoleFunction> roleFunList = new ArrayList<>();
170 RoleFunction roleFun1 = new RoleFunction();
171 roleFun1.setName("TestRoleFun1");
172 roleFun1.setCode("TestRoleFunCode1");
173 roleFunList.add(roleFun1);
174 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunList);
175 StringWriter sw = new StringWriter();
176 PrintWriter writer = new PrintWriter(sw);
177 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
178 roleFunctionListController.addRoleFunction(mockedRequest, mockedResponse, roleFun);
182 public void addRoleFunctionExsistsTest() throws IOException {
183 User user = new User();
184 user.setOrgUserId("test12");
185 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
186 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
187 List<RoleFunction> roleFunList = new ArrayList<>();
188 RoleFunction roleFun1 = new RoleFunction();
189 roleFun1.setName("Test");
190 roleFun1.setCode("Test");
191 roleFunList.add(roleFun1);
192 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunList);
193 StringWriter sw = new StringWriter();
194 PrintWriter writer = new PrintWriter(sw);
195 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
196 roleFunctionListController.addRoleFunction(mockedRequest, mockedResponse, roleFun);
199 @Test(expected = java.io.IOException.class)
200 public void addRoleFunctionExceptionTest() throws IOException {
201 User user = new User();
202 user.setOrgUserId("test12");
203 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
204 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
205 List<RoleFunction> roleFunList = new ArrayList<>();
206 RoleFunction roleFun1 = new RoleFunction();
207 roleFun1.setName("Test");
208 roleFun1.setCode("Test");
209 roleFunList.add(roleFun1);
210 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException);
211 StringWriter sw = new StringWriter();
212 PrintWriter writer = new PrintWriter(sw);
213 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
214 roleFunctionListController.addRoleFunction(mockedRequest, mockedResponse, roleFun);
218 public void removeRoleFunctionTest() throws IOException {
219 User user = new User();
220 user.setOrgUserId("test12");
221 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
222 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
223 RoleFunction roleFun1 = new RoleFunction();
224 roleFun1.setName("Test");
225 roleFun1.setCode("Test");
226 Mockito.when((roleService.getRoleFunction(user.getOrgUserId(), "Test"))).thenReturn(roleFun1);
227 StringWriter sw = new StringWriter();
228 PrintWriter writer = new PrintWriter(sw);
229 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
230 roleFunctionListController.removeRoleFunction(mockedRequest, mockedResponse, roleFun);
234 @Test(expected = java.io.IOException.class)
235 public void removeRoleFunctionExceptionTest() throws IOException {
236 User user = new User();
237 user.setOrgUserId("test12");
238 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
239 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
240 RoleFunction roleFun1 = new RoleFunction();
241 roleFun1.setName("Test");
242 roleFun1.setCode("Test");
243 Mockito.when((roleService.getRoleFunction(user.getOrgUserId(), "Test"))).thenThrow(nullPointerException);
244 StringWriter sw = new StringWriter();
245 PrintWriter writer = new PrintWriter(sw);
246 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
247 roleFunctionListController.removeRoleFunction(mockedRequest, mockedResponse, roleFun);