XSS Vulnerability fix in AppContactUsController
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / AppContactUsControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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.portal.controller;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
42
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Map;
46
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
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.portal.controller.AppContactUsController;
58 import org.onap.portalapp.portal.ecomp.model.AppCategoryFunctionsItem;
59 import org.onap.portalapp.portal.ecomp.model.AppContactUsItem;
60 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
61 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
62 import org.onap.portalapp.portal.framework.MockitoTestSuite;
63 import org.onap.portalapp.portal.service.AppContactUsService;
64 import org.onap.portalapp.portal.service.AppContactUsServiceImpl;
65 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
66 import org.onap.portalapp.util.EPUserUtils;
67 import org.onap.portalsdk.core.domain.support.CollaborateList;
68 import org.onap.portalsdk.core.util.SystemProperties;
69 import org.powermock.api.mockito.PowerMockito;
70 import org.powermock.core.classloader.annotations.PrepareForTest;
71 import org.powermock.modules.junit4.PowerMockRunner;
72
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({SystemProperties.class, EPCommonSystemProperties.class})
75 public class AppContactUsControllerTest extends MockitoTestSuite{
76
77         @Mock
78         AppContactUsService contactUsService = new AppContactUsServiceImpl();
79
80         @InjectMocks
81         AppContactUsController appContactUsController;
82
83         @Before
84         public void setup() {
85                 MockitoAnnotations.initMocks(this);
86         }
87
88         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
89
90         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
91         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
92         NullPointerException nullPointerException = new NullPointerException();
93
94         @Mock
95         EPUserUtils ePUserUtils = new EPUserUtils();
96
97         public List<AppContactUsItem> mockResponse() {
98                 List<AppContactUsItem> appContactUsItemList = new ArrayList<AppContactUsItem>();
99                 AppContactUsItem appContactUsItem = new AppContactUsItem();
100                 appContactUsItem.setAppId((long) 1);
101                 appContactUsItem.setAppName("ECOMP Portal");
102                 appContactUsItem.setDescription("Test");
103                 appContactUsItem.setContactName("Test");
104                 appContactUsItem.setContactEmail("person@onap.org");
105                 appContactUsItem.setUrl("Test_URL");
106                 appContactUsItem.setActiveYN("Y");
107                 appContactUsItemList.add(appContactUsItem);
108
109                 return appContactUsItemList;
110
111         }
112
113         public PortalRestResponse<List<AppContactUsItem>> successPortalRestResponse() {
114                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
115                 List<AppContactUsItem> appContactUsItemList = mockResponse();
116                 expectedportalRestResponse.setMessage("success");
117                 expectedportalRestResponse.setResponse(appContactUsItemList);
118                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
119                 return expectedportalRestResponse;
120
121         }
122
123         public PortalRestResponse<List<AppContactUsItem>> exceptionPortalRestResponse() {
124                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
125                 expectedportalRestResponse.setMessage(null);
126                 expectedportalRestResponse.setResponse(null);
127                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
128                 return expectedportalRestResponse;
129
130         }
131
132         @Test
133         public void getAppContactUsList() throws Exception {
134                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = successPortalRestResponse();
135                 List<AppContactUsItem> appContactUsItemList = mockResponse();
136                 PortalRestResponse<List<AppContactUsItem>> actualPortalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
137                 Mockito.when(contactUsService.getAppContactUs()).thenReturn(appContactUsItemList);
138                 actualPortalRestResponse = appContactUsController.getAppContactUsList(mockedRequest);
139                 assertEquals(actualPortalRestResponse, expectedportalRestResponse);
140         }
141
142         @Test
143         public void getAppContactUsListCatchesExeptionTest() throws Exception {
144
145                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = exceptionPortalRestResponse();
146                 PortalRestResponse<List<AppContactUsItem>> actualPortalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
147                 Mockito.when(contactUsService.getAppContactUs()).thenThrow(nullPointerException);
148                 actualPortalRestResponse = appContactUsController.getAppContactUsList(mockedRequest);
149                 assertEquals(actualPortalRestResponse, expectedportalRestResponse);
150         }
151
152         @Test
153         public void getAppsAndContactsTest() throws Exception {
154                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = successPortalRestResponse();
155                 List<AppContactUsItem> appContactUsItemList = mockResponse();
156                 PortalRestResponse<List<AppContactUsItem>> actualPortalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
157                 Mockito.when(contactUsService.getAppsAndContacts()).thenReturn(appContactUsItemList);
158                 actualPortalRestResponse = appContactUsController.getAppsAndContacts(mockedRequest);
159                 assertEquals(actualPortalRestResponse, expectedportalRestResponse);
160
161         }
162
163         @Test
164         public void getAppsAndContactsCatchesExceptionTest() throws Exception {
165                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = exceptionPortalRestResponse();
166                 PortalRestResponse<List<AppContactUsItem>> actualPortalRestResponse = new PortalRestResponse<List<AppContactUsItem>>();
167                 Mockito.when(contactUsService.getAppsAndContacts()).thenThrow(nullPointerException);
168                 actualPortalRestResponse = appContactUsController.getAppsAndContacts(mockedRequest);
169                 assertEquals(actualPortalRestResponse, expectedportalRestResponse);
170
171         }
172
173         @Test
174         public void getAppCategoryFunctionsTest() throws Exception {
175                 PortalRestResponse<List<AppCategoryFunctionsItem>> actualportalRestResponse = null;
176
177                 List<AppCategoryFunctionsItem> contents = new ArrayList<AppCategoryFunctionsItem>();
178
179                 AppCategoryFunctionsItem appCategoryFunctionsItem = new AppCategoryFunctionsItem();
180                 AppCategoryFunctionsItem appCategoryFunctionsItem1 = new AppCategoryFunctionsItem();
181
182                 appCategoryFunctionsItem.setRowId("1");
183                 appCategoryFunctionsItem.setAppId("1");
184                 appCategoryFunctionsItem.setApplication("Ecomp-portal");
185                 appCategoryFunctionsItem.setCategory("test");
186                 appCategoryFunctionsItem.setFunctions("test");
187
188                 appCategoryFunctionsItem1.setRowId("2");
189                 appCategoryFunctionsItem1.setAppId("2");
190                 appCategoryFunctionsItem1.setApplication("Ecomp-portal-test");
191                 appCategoryFunctionsItem1.setCategory("test");
192                 appCategoryFunctionsItem1.setFunctions("test");
193                 contents.add(appCategoryFunctionsItem);
194                 contents.add(appCategoryFunctionsItem1);
195
196                 PortalRestResponse<List<AppCategoryFunctionsItem>> expectedportalRestResponse = new PortalRestResponse<List<AppCategoryFunctionsItem>>();
197                 expectedportalRestResponse.setMessage("success");
198                 expectedportalRestResponse.setResponse(contents);
199                 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
200
201                 Mockito.when(contactUsService.getAppCategoryFunctions()).thenReturn(contents);
202                 actualportalRestResponse = appContactUsController.getAppCategoryFunctions(mockedRequest);
203                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
204
205         }
206
207         @Test
208         public void getAppCategoryFunctionsCatchesExceptionTest() throws Exception {
209                 PortalRestResponse<List<AppCategoryFunctionsItem>> actualportalRestResponse = null;
210                 PortalRestResponse<List<AppContactUsItem>> expectedportalRestResponse = exceptionPortalRestResponse();
211                 Mockito.when(contactUsService.getAppCategoryFunctions()).thenThrow(nullPointerException);
212                 actualportalRestResponse = appContactUsController.getAppCategoryFunctions(mockedRequest);
213                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
214
215         }
216
217         @Test
218         public void saveTest() throws Exception {
219                 PortalRestResponse<String> actualSaveAppContactUS = null;
220
221                 AppContactUsItem contactUs = new AppContactUsItem();
222                 contactUs.setAppId((long) 1);
223                 contactUs.setAppName("Ecomp Portal");
224                 contactUs.setDescription("Test");
225                 contactUs.setContactName("Test");
226                 contactUs.setContactEmail("person@onap.org");
227                 contactUs.setUrl("Test_URL");
228                 contactUs.setActiveYN("Y");
229
230                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("SUCCESS");
231                 actualSaveAppContactUS = appContactUsController.save(contactUs);
232                 assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS");
233         }
234
235         @Test
236         public void saveXSSTest() throws Exception {
237                 PortalRestResponse<String> actualSaveAppContactUS = null;
238
239                 AppContactUsItem contactUs = new AppContactUsItem();
240                 contactUs.setAppId((long) 1);
241                 contactUs.setAppName("<meta content=\"&NewLine; 1 &NewLine;; JAVASCRIPT&colon; alert(1)\" http-equiv=\"refresh\"/>");
242                 contactUs.setDescription("Test");
243                 contactUs.setContactName("Test");
244                 contactUs.setContactEmail("person@onap.org");
245                 contactUs.setUrl("Test_URL");
246                 contactUs.setActiveYN("Y");
247
248                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("FAILURE");
249                 actualSaveAppContactUS = appContactUsController.save(contactUs);
250                 assertEquals("AppName is not valid.", actualSaveAppContactUS.getResponse());
251                 assertEquals("failure", actualSaveAppContactUS.getMessage());
252         }
253
254         @Test
255         public void saveExceptionTest() throws Exception {
256                 PortalRestResponse<String> actualSaveAppContactUS = null;
257
258                 AppContactUsItem contactUs = new AppContactUsItem();
259                 contactUs.setAppId((long) 1);
260                 contactUs.setAppName("Ecomp Portal");
261                 contactUs.setDescription("Test");
262                 contactUs.setContactName("Test");
263                 contactUs.setContactEmail("person@onap.org");
264                 contactUs.setUrl("Test_URL");
265                 contactUs.setActiveYN("Y");
266
267                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenThrow(new Exception());
268                 actualSaveAppContactUS = appContactUsController.save(contactUs);
269                 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
270         }
271
272         @Test
273         public void saveWhenAppContactUsItemNullTest() throws Exception {
274                 PortalRestResponse<String> actualSaveAppContactUS = null;
275                 AppContactUsItem contactUs = null;
276                 actualSaveAppContactUS = appContactUsController.save(contactUs);
277                 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
278
279         }
280
281         @Test
282         public void saveAllTest() throws Exception {
283
284                 List<AppContactUsItem> contactUs = mockResponse();
285                 PortalRestResponse<String> actualSaveAppContactUS = null;
286                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("SUCCESS");
287                 actualSaveAppContactUS = appContactUsController.save(contactUs);
288                 assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS");
289         }
290
291         @Test
292         public void saveAllXSSTest() throws Exception {
293
294                 List<AppContactUsItem> contactUs = mockResponse();
295                 AppContactUsItem appContactUsItem = new AppContactUsItem();
296                 appContactUsItem.setActiveYN("<script/&Tab; src='https://dl.dropbox.com/u/13018058/js.js' /&Tab;></script>");
297                 contactUs.add(appContactUsItem);
298                 PortalRestResponse<String> actualSaveAppContactUS = null;
299                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("failure");
300                 actualSaveAppContactUS = appContactUsController.save(contactUs);
301                 assertEquals("failure", actualSaveAppContactUS.getMessage());
302         }
303
304         @Test
305         public void saveAllExceptionTest() throws Exception {
306
307                 List<AppContactUsItem> contactUs = mockResponse();
308                 PortalRestResponse<String> actualSaveAppContactUS = null;
309                 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenThrow(new Exception());
310                 actualSaveAppContactUS = appContactUsController.save(contactUs);
311                 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
312         }
313
314         @Test
315         public void deleteTest() throws Exception {
316
317                 PortalRestResponse<String> actualSaveAppContactUS = null;
318                 Long id = (long) 1;
319                 String saveAppContactUs = "SUCCESS";
320                 Mockito.when(contactUsService.deleteContactUs(id)).thenReturn(saveAppContactUs);
321                 actualSaveAppContactUS = appContactUsController.delete(id);
322                 assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS");
323         }
324
325         @Test
326         public void deleteExceptionTest() throws Exception {
327
328                 PortalRestResponse<String> actualSaveAppContactUS = null;
329                 Long id = (long) 1;
330                 Mockito.when(contactUsService.deleteContactUs(id)).thenThrow(new Exception());
331                 actualSaveAppContactUS = appContactUsController.delete(id);
332                 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
333         }
334
335         @Test
336         public void getPortalDetailsTest(){
337                 PortalRestResponse<String> actualResponse = new PortalRestResponse<String>();
338                 PortalRestResponse<String> expectedResponse = new PortalRestResponse<String>();
339                 expectedResponse.setStatus(PortalRestStatusEnum.OK);
340                 expectedResponse.setMessage("success");
341                 expectedResponse.setResponse("\"ush_ticket_url\":\"http://todo_enter_ush_ticket_url\",\"portal_info_url\":\"https://todo_enter_portal_info_url\",\"feedback_email_address\":\"portal@lists.onap.org\"");
342                 PowerMockito.mockStatic(SystemProperties.class);
343                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
344                 
345                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.USH_TICKET_URL)).thenReturn("http://todo_enter_ush_ticket_url"); 
346                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.PORTAL_INFO_URL)).thenReturn("https://todo_enter_portal_info_url"); 
347                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS)).thenReturn("portal@lists.onap.org"); 
348                 
349                 actualResponse = appContactUsController.getPortalDetails(mockedRequest);
350                 assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.OK) == 0);
351         }
352         
353         @Test
354         public void getPortalDetailsExceptionTest(){
355                 PortalRestResponse<String> actualResponse = new PortalRestResponse<String>();
356                 PortalRestResponse<String> expectedResponse = new PortalRestResponse<String>();
357                 expectedResponse.setStatus(PortalRestStatusEnum.ERROR);
358                 expectedResponse.setMessage("failure");
359                 expectedResponse.setResponse(null);
360                 PowerMockito.mockStatic(SystemProperties.class);
361                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
362                 
363                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.USH_TICKET_URL)).thenThrow(nullPointerException); 
364                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.PORTAL_INFO_URL)).thenReturn("https://todo_enter_portal_info_url"); 
365                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS)).thenReturn("portal@lists.onap.org"); 
366                 
367                 actualResponse = appContactUsController.getPortalDetails(mockedRequest);
368                 assertEquals(actualResponse, expectedResponse);
369         }
370 }