d073bb64228277dbc4911172759417245362ca27
[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 import static org.junit.Assert.assertNull;
42
43 import java.io.IOException;
44 import java.io.PrintWriter;
45 import java.io.StringWriter;
46 import java.util.ArrayList;
47 import java.util.List;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51
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.RoleFunction;
60 import org.onap.portalsdk.core.domain.User;
61 import org.onap.portalsdk.core.service.RoleService;
62 import org.onap.portalsdk.core.web.support.UserUtils;
63 import org.springframework.web.bind.ServletRequestUtils;
64 import org.springframework.web.servlet.ModelAndView;
65
66 import com.fasterxml.jackson.databind.ObjectMapper;
67
68 public class RoleFunctionListControllerTest {
69
70         @InjectMocks
71         RoleFunctionListController roleFunctionListController = new RoleFunctionListController();
72
73         @Mock
74         RoleService roleService;
75
76         @Before
77         public void setup() {
78                 MockitoAnnotations.initMocks(this);
79         }
80
81         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
82
83         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
84         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
85         NullPointerException nullPointerException = new NullPointerException();
86
87         @Mock
88         UserUtils userUtils = new UserUtils();
89
90         @Mock
91         ServletRequestUtils servletRequestUtils;
92
93         @Mock
94         ObjectMapper mapper = new ObjectMapper();
95
96         @Test
97         public void welcomeTest() throws IOException {
98                 roleFunctionListController.setViewName("Test");
99                 User user = new User();
100                 user.setOrgUserId("test12");
101                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
102                 List<RoleFunction> roleFunctionList = new ArrayList<RoleFunction>();
103                 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunctionList);
104                 ModelAndView expectedResult = roleFunctionListController.welcome(mockedRequest);
105                 assertEquals(expectedResult.getViewName(), "Test");
106         }
107
108         @Test
109         public void welcomeExceptionTest() throws IOException {
110                 User user = new User();
111                 user.setOrgUserId("test12");
112                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
113                 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException);
114                 ModelAndView expectedResult = roleFunctionListController.welcome(mockedRequest);
115                 assertNull(expectedResult.getViewName());
116         }
117
118         @Test
119         public void getRoleFunctionListTest() throws IOException {
120                 User user = new User();
121                 user.setOrgUserId("test12");
122                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
123                 List<RoleFunction> roleFunctionList = new ArrayList<RoleFunction>();
124                 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunctionList);
125                 StringWriter sw = new StringWriter();
126                 PrintWriter writer = new PrintWriter(sw);
127                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
128                 roleFunctionListController.getRoleFunctionList(mockedRequest, mockedResponse);
129         }
130
131         @Test
132         public void getRoleFunctionListExceptionTest() throws IOException {
133                 User user = new User();
134                 user.setOrgUserId("test12");
135                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
136                 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException);
137                 roleFunctionListController.getRoleFunctionList(mockedRequest, mockedResponse);
138         }
139
140         @Test
141         public void saveRoleFunctionTest() throws IOException {
142                 User user = new User();
143                 user.setOrgUserId("test12");
144                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
145                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
146                 RoleFunction roleFunction = new RoleFunction();
147                 Mockito.when(roleService.getRoleFunction(user.getOrgUserId(), "Test")).thenReturn(roleFunction);
148                 StringWriter sw = new StringWriter();
149                 PrintWriter writer = new PrintWriter(sw);
150                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
151                 roleFunctionListController.saveRoleFunction(mockedRequest, mockedResponse, roleFun);
152         }
153
154         @Test(expected = java.io.IOException.class)
155         public void saveRoleFunctionExceptionTest() throws IOException {
156                 User user = new User();
157                 user.setOrgUserId("test12");
158                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
159                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
160                 Mockito.when(roleService.getRoleFunction(user.getOrgUserId(), "Test")).thenThrow(nullPointerException);
161                 roleFunctionListController.saveRoleFunction(mockedRequest, mockedResponse, roleFun);
162         }
163
164         @Test
165         public void addRoleFunctionTest() throws IOException {
166                 User user = new User();
167                 user.setOrgUserId("test12");
168                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
169                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
170                 List<RoleFunction> roleFunList = new ArrayList<>();
171                 RoleFunction roleFun1 = new RoleFunction();
172                 roleFun1.setName("TestRoleFun1");
173                 roleFun1.setCode("TestRoleFunCode1");
174                 roleFunList.add(roleFun1);
175                 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunList);
176                 StringWriter sw = new StringWriter();
177                 PrintWriter writer = new PrintWriter(sw);
178                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
179                 roleFunctionListController.addRoleFunction(mockedRequest, mockedResponse, roleFun);
180         }
181
182         @Test
183         public void addRoleFunctionExsistsTest() throws IOException {
184                 User user = new User();
185                 user.setOrgUserId("test12");
186                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
187                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
188                 List<RoleFunction> roleFunList = new ArrayList<>();
189                 RoleFunction roleFun1 = new RoleFunction();
190                 roleFun1.setName("Test");
191                 roleFun1.setCode("Test");
192                 roleFunList.add(roleFun1);
193                 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenReturn(roleFunList);
194                 StringWriter sw = new StringWriter();
195                 PrintWriter writer = new PrintWriter(sw);
196                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
197                 roleFunctionListController.addRoleFunction(mockedRequest, mockedResponse, roleFun);
198         }
199
200         @Test(expected = java.io.IOException.class)
201         public void addRoleFunctionExceptionTest() throws IOException {
202                 User user = new User();
203                 user.setOrgUserId("test12");
204                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
205                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
206                 List<RoleFunction> roleFunList = new ArrayList<>();
207                 RoleFunction roleFun1 = new RoleFunction();
208                 roleFun1.setName("Test");
209                 roleFun1.setCode("Test");
210                 roleFunList.add(roleFun1);
211                 Mockito.when(roleService.getRoleFunctions(user.getOrgUserId())).thenThrow(nullPointerException);
212                 StringWriter sw = new StringWriter();
213                 PrintWriter writer = new PrintWriter(sw);
214                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
215                 roleFunctionListController.addRoleFunction(mockedRequest, mockedResponse, roleFun);
216         }
217
218         @Test
219         public void removeRoleFunctionTest() throws IOException {
220                 User user = new User();
221                 user.setOrgUserId("test12");
222                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
223                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
224                 RoleFunction roleFun1 = new RoleFunction();
225                 roleFun1.setName("Test");
226                 roleFun1.setCode("Test");
227                 Mockito.when((roleService.getRoleFunction(user.getOrgUserId(), "Test"))).thenReturn(roleFun1);
228                 StringWriter sw = new StringWriter();
229                 PrintWriter writer = new PrintWriter(sw);
230                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
231                 roleFunctionListController.removeRoleFunction(mockedRequest, mockedResponse, roleFun);
232
233         }
234
235         @Test(expected = java.io.IOException.class)
236         public void removeRoleFunctionExceptionTest() throws IOException {
237                 User user = new User();
238                 user.setOrgUserId("test12");
239                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
240                 String roleFun = "{\"name\":\"Test\",\"code\":\"Test\"}";
241                 RoleFunction roleFun1 = new RoleFunction();
242                 roleFun1.setName("Test");
243                 roleFun1.setCode("Test");
244                 Mockito.when((roleService.getRoleFunction(user.getOrgUserId(), "Test"))).thenThrow(nullPointerException);
245                 StringWriter sw = new StringWriter();
246                 PrintWriter writer = new PrintWriter(sw);
247                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
248                 roleFunctionListController.removeRoleFunction(mockedRequest, mockedResponse, roleFun);
249         }
250 }