2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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.portal.controller;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
43 import java.util.ArrayList;
44 import java.util.List;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
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;
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({SystemProperties.class, EPCommonSystemProperties.class})
75 public class AppContactUsControllerTest extends MockitoTestSuite{
78 AppContactUsService contactUsService = new AppContactUsServiceImpl();
81 AppContactUsController appContactUsController;
85 MockitoAnnotations.initMocks(this);
88 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
90 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
91 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
92 NullPointerException nullPointerException = new NullPointerException();
95 EPUserUtils ePUserUtils = new EPUserUtils();
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);
109 return appContactUsItemList;
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;
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;
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);
143 public void getAppContactUsListCatchesExeptionTest() throws Exception {
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);
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);
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);
174 public void getAppCategoryFunctionsTest() throws Exception {
175 PortalRestResponse<List<AppCategoryFunctionsItem>> actualportalRestResponse = null;
177 List<AppCategoryFunctionsItem> contents = new ArrayList<AppCategoryFunctionsItem>();
179 AppCategoryFunctionsItem appCategoryFunctionsItem = new AppCategoryFunctionsItem();
180 AppCategoryFunctionsItem appCategoryFunctionsItem1 = new AppCategoryFunctionsItem();
182 appCategoryFunctionsItem.setRowId("1");
183 appCategoryFunctionsItem.setAppId("1");
184 appCategoryFunctionsItem.setApplication("Ecomp-portal");
185 appCategoryFunctionsItem.setCategory("test");
186 appCategoryFunctionsItem.setFunctions("test");
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);
196 PortalRestResponse<List<AppCategoryFunctionsItem>> expectedportalRestResponse = new PortalRestResponse<List<AppCategoryFunctionsItem>>();
197 expectedportalRestResponse.setMessage("success");
198 expectedportalRestResponse.setResponse(contents);
199 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
201 Mockito.when(contactUsService.getAppCategoryFunctions()).thenReturn(contents);
202 actualportalRestResponse = appContactUsController.getAppCategoryFunctions(mockedRequest);
203 assertEquals(actualportalRestResponse, expectedportalRestResponse);
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);
218 public void saveTest() throws Exception {
219 PortalRestResponse<String> actualSaveAppContactUS = null;
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");
230 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("SUCCESS");
231 actualSaveAppContactUS = appContactUsController.save(contactUs);
232 assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS");
236 public void saveXSSTest() throws Exception {
237 PortalRestResponse<String> actualSaveAppContactUS = null;
239 AppContactUsItem contactUs = new AppContactUsItem();
240 contactUs.setAppId((long) 1);
241 contactUs.setAppName("<meta content=\"
 1 
; JAVASCRIPT: 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");
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());
255 public void saveExceptionTest() throws Exception {
256 PortalRestResponse<String> actualSaveAppContactUS = null;
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");
267 Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenThrow(new Exception());
268 actualSaveAppContactUS = appContactUsController.save(contactUs);
269 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
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");
282 public void saveAllTest() throws Exception {
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");
292 public void saveAllXSSTest() throws Exception {
294 List<AppContactUsItem> contactUs = mockResponse();
295 AppContactUsItem appContactUsItem = new AppContactUsItem();
296 appContactUsItem.setActiveYN("<script/	 src='https://dl.dropbox.com/u/13018058/js.js' /	></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());
305 public void saveAllExceptionTest() throws Exception {
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");
315 public void deleteTest() throws Exception {
317 PortalRestResponse<String> actualSaveAppContactUS = null;
319 String saveAppContactUs = "SUCCESS";
320 Mockito.when(contactUsService.deleteContactUs(id)).thenReturn(saveAppContactUs);
321 actualSaveAppContactUS = appContactUsController.delete(id);
322 assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS");
326 public void deleteExceptionTest() throws Exception {
328 PortalRestResponse<String> actualSaveAppContactUS = null;
330 Mockito.when(contactUsService.deleteContactUs(id)).thenThrow(new Exception());
331 actualSaveAppContactUS = appContactUsController.delete(id);
332 assertEquals(actualSaveAppContactUS.getMessage(), "failure");
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);
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");
349 actualResponse = appContactUsController.getPortalDetails(mockedRequest);
350 assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.OK) == 0);
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);
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");
367 actualResponse = appContactUsController.getPortalDetails(mockedRequest);
368 assertEquals(actualResponse, expectedResponse);