016f9bef4b4c47fe76c9a48c1938356d33d27563
[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.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;
64
65 import com.fasterxml.jackson.databind.ObjectMapper;
66
67 public class RoleFunctionListControllerTest {
68
69         @InjectMocks
70         RoleFunctionListController roleFunctionListController = new RoleFunctionListController();
71
72         @Mock
73         RoleService roleService;
74
75         @Before
76         public void setup() {
77                 MockitoAnnotations.initMocks(this);
78         }
79
80         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
81
82         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
83         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
84         NullPointerException nullPointerException = new NullPointerException();
85
86         @Mock
87         UserUtils userUtils = new UserUtils();
88
89         @Mock
90         ServletRequestUtils servletRequestUtils;
91
92         @Mock
93         ObjectMapper mapper = new ObjectMapper();
94
95         @Test
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");
105         }
106
107         @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());
115         }
116
117         @Test
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);
128         }
129
130         @Test
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);
137         }
138
139         @Test
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);
151         }
152
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);
161         }
162
163         @Test
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);
179         }
180
181         @Test
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);
197         }
198
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);
215         }
216
217         @Test
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);
231
232         }
233
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);
248         }
249 }