/*- * ============LICENSE_START========================================== * ONAP Portal * =================================================================== * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * =================================================================== * * Unless otherwise specified, all software contained herein is licensed * under the Apache License, Version 2.0 (the "License"); * you may not use this software except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Unless otherwise specified, all documentation contained herein is licensed * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); * you may not use this documentation except in compliance with the License. * You may obtain a copy of the License at * * https://creativecommons.org/licenses/by/4.0/ * * Unless required by applicable law or agreed to in writing, documentation * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ============LICENSE_END============================================ * * */ package org.onap.portalapp.portal.controller; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.onap.portalapp.portal.controller.AppContactUsController; import org.onap.portalapp.portal.ecomp.model.AppCategoryFunctionsItem; import org.onap.portalapp.portal.ecomp.model.AppContactUsItem; import org.onap.portalapp.portal.ecomp.model.PortalRestResponse; import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum; import org.onap.portalapp.portal.framework.MockitoTestSuite; import org.onap.portalapp.portal.service.AppContactUsService; import org.onap.portalapp.portal.service.AppContactUsServiceImpl; import org.onap.portalapp.portal.utils.EPCommonSystemProperties; import org.onap.portalapp.util.EPUserUtils; import org.onap.portalsdk.core.domain.support.CollaborateList; import org.onap.portalsdk.core.util.SystemProperties; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest({SystemProperties.class, EPCommonSystemProperties.class}) public class AppContactUsControllerTest extends MockitoTestSuite{ @Mock AppContactUsService contactUsService = new AppContactUsServiceImpl(); @InjectMocks AppContactUsController appContactUsController; @Before public void setup() { MockitoAnnotations.initMocks(this); } MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); NullPointerException nullPointerException = new NullPointerException(); @Mock EPUserUtils ePUserUtils = new EPUserUtils(); public List mockResponse() { List appContactUsItemList = new ArrayList(); AppContactUsItem appContactUsItem = new AppContactUsItem(); appContactUsItem.setAppId((long) 1); appContactUsItem.setAppName("ECOMP Portal"); appContactUsItem.setDescription("Test"); appContactUsItem.setContactName("Test"); appContactUsItem.setContactEmail("person@onap.org"); appContactUsItem.setUrl("Test_URL"); appContactUsItem.setActiveYN("Y"); appContactUsItemList.add(appContactUsItem); return appContactUsItemList; } public PortalRestResponse> successPortalRestResponse() { PortalRestResponse> expectedportalRestResponse = new PortalRestResponse>(); List appContactUsItemList = mockResponse(); expectedportalRestResponse.setMessage("success"); expectedportalRestResponse.setResponse(appContactUsItemList); expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK); return expectedportalRestResponse; } public PortalRestResponse> exceptionPortalRestResponse() { PortalRestResponse> expectedportalRestResponse = new PortalRestResponse>(); expectedportalRestResponse.setMessage(null); expectedportalRestResponse.setResponse(null); expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR); return expectedportalRestResponse; } @Test public void getAppContactUsList() throws Exception { PortalRestResponse> expectedportalRestResponse = successPortalRestResponse(); List appContactUsItemList = mockResponse(); PortalRestResponse> actualPortalRestResponse = new PortalRestResponse>(); Mockito.when(contactUsService.getAppContactUs()).thenReturn(appContactUsItemList); actualPortalRestResponse = appContactUsController.getAppContactUsList(mockedRequest); assertEquals(actualPortalRestResponse, expectedportalRestResponse); } @Test public void getAppContactUsListCatchesExeptionTest() throws Exception { PortalRestResponse> expectedportalRestResponse = exceptionPortalRestResponse(); PortalRestResponse> actualPortalRestResponse = new PortalRestResponse>(); Mockito.when(contactUsService.getAppContactUs()).thenThrow(nullPointerException); actualPortalRestResponse = appContactUsController.getAppContactUsList(mockedRequest); assertEquals(actualPortalRestResponse, expectedportalRestResponse); } @Test public void getAppsAndContactsTest() throws Exception { PortalRestResponse> expectedportalRestResponse = successPortalRestResponse(); List appContactUsItemList = mockResponse(); PortalRestResponse> actualPortalRestResponse = new PortalRestResponse>(); Mockito.when(contactUsService.getAppsAndContacts()).thenReturn(appContactUsItemList); actualPortalRestResponse = appContactUsController.getAppsAndContacts(mockedRequest); assertEquals(actualPortalRestResponse, expectedportalRestResponse); } @Test public void getAppsAndContactsCatchesExceptionTest() throws Exception { PortalRestResponse> expectedportalRestResponse = exceptionPortalRestResponse(); PortalRestResponse> actualPortalRestResponse = new PortalRestResponse>(); Mockito.when(contactUsService.getAppsAndContacts()).thenThrow(nullPointerException); actualPortalRestResponse = appContactUsController.getAppsAndContacts(mockedRequest); assertEquals(actualPortalRestResponse, expectedportalRestResponse); } @Test public void getAppCategoryFunctionsTest() throws Exception { PortalRestResponse> actualportalRestResponse = null; List contents = new ArrayList(); AppCategoryFunctionsItem appCategoryFunctionsItem = new AppCategoryFunctionsItem(); AppCategoryFunctionsItem appCategoryFunctionsItem1 = new AppCategoryFunctionsItem(); appCategoryFunctionsItem.setRowId("1"); appCategoryFunctionsItem.setAppId("1"); appCategoryFunctionsItem.setApplication("Ecomp-portal"); appCategoryFunctionsItem.setCategory("test"); appCategoryFunctionsItem.setFunctions("test"); appCategoryFunctionsItem1.setRowId("2"); appCategoryFunctionsItem1.setAppId("2"); appCategoryFunctionsItem1.setApplication("Ecomp-portal-test"); appCategoryFunctionsItem1.setCategory("test"); appCategoryFunctionsItem1.setFunctions("test"); contents.add(appCategoryFunctionsItem); contents.add(appCategoryFunctionsItem1); PortalRestResponse> expectedportalRestResponse = new PortalRestResponse>(); expectedportalRestResponse.setMessage("success"); expectedportalRestResponse.setResponse(contents); expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK); Mockito.when(contactUsService.getAppCategoryFunctions()).thenReturn(contents); actualportalRestResponse = appContactUsController.getAppCategoryFunctions(mockedRequest); assertEquals(actualportalRestResponse, expectedportalRestResponse); } @Test public void getAppCategoryFunctionsCatchesExceptionTest() throws Exception { PortalRestResponse> actualportalRestResponse = null; PortalRestResponse> expectedportalRestResponse = exceptionPortalRestResponse(); Mockito.when(contactUsService.getAppCategoryFunctions()).thenThrow(nullPointerException); actualportalRestResponse = appContactUsController.getAppCategoryFunctions(mockedRequest); assertEquals(actualportalRestResponse, expectedportalRestResponse); } @Test public void saveTest() throws Exception { PortalRestResponse actualSaveAppContactUS = null; AppContactUsItem contactUs = new AppContactUsItem(); contactUs.setAppId((long) 1); contactUs.setAppName("Ecomp Portal"); contactUs.setDescription("Test"); contactUs.setContactName("Test"); contactUs.setContactEmail("person@onap.org"); contactUs.setUrl("Test_URL"); contactUs.setActiveYN("Y"); Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("SUCCESS"); actualSaveAppContactUS = appContactUsController.save(contactUs); assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS"); } @Test public void saveXSSTest() throws Exception { PortalRestResponse actualSaveAppContactUS = null; AppContactUsItem contactUs = new AppContactUsItem(); contactUs.setAppId((long) 1); contactUs.setAppName(""); contactUs.setDescription("Test"); contactUs.setContactName("Test"); contactUs.setContactEmail("person@onap.org"); contactUs.setUrl("Test_URL"); contactUs.setActiveYN("Y"); Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("FAILURE"); actualSaveAppContactUS = appContactUsController.save(contactUs); assertEquals("AppName is not valid.", actualSaveAppContactUS.getResponse()); assertEquals("failure", actualSaveAppContactUS.getMessage()); } @Test public void saveExceptionTest() throws Exception { PortalRestResponse actualSaveAppContactUS = null; AppContactUsItem contactUs = new AppContactUsItem(); contactUs.setAppId((long) 1); contactUs.setAppName("Ecomp Portal"); contactUs.setDescription("Test"); contactUs.setContactName("Test"); contactUs.setContactEmail("person@onap.org"); contactUs.setUrl("Test_URL"); contactUs.setActiveYN("Y"); Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenThrow(new Exception()); actualSaveAppContactUS = appContactUsController.save(contactUs); assertEquals(actualSaveAppContactUS.getMessage(), "failure"); } @Test public void saveWhenAppContactUsItemNullTest() throws Exception { PortalRestResponse actualSaveAppContactUS = null; AppContactUsItem contactUs = null; actualSaveAppContactUS = appContactUsController.save(contactUs); assertEquals(actualSaveAppContactUS.getMessage(), "failure"); } @Test public void saveAllTest() throws Exception { List contactUs = mockResponse(); PortalRestResponse actualSaveAppContactUS = null; Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("SUCCESS"); actualSaveAppContactUS = appContactUsController.save(contactUs); assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS"); } @Test public void saveAllXSSTest() throws Exception { List contactUs = mockResponse(); AppContactUsItem appContactUsItem = new AppContactUsItem(); appContactUsItem.setActiveYN(""); contactUs.add(appContactUsItem); PortalRestResponse actualSaveAppContactUS = null; Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenReturn("failure"); actualSaveAppContactUS = appContactUsController.save(contactUs); assertEquals("failure", actualSaveAppContactUS.getMessage()); } @Test public void saveAllExceptionTest() throws Exception { List contactUs = mockResponse(); PortalRestResponse actualSaveAppContactUS = null; Mockito.when(contactUsService.saveAppContactUs(contactUs)).thenThrow(new Exception()); actualSaveAppContactUS = appContactUsController.save(contactUs); assertEquals(actualSaveAppContactUS.getMessage(), "failure"); } @Test public void deleteTest() throws Exception { PortalRestResponse actualSaveAppContactUS = null; Long id = (long) 1; String saveAppContactUs = "SUCCESS"; Mockito.when(contactUsService.deleteContactUs(id)).thenReturn(saveAppContactUs); actualSaveAppContactUS = appContactUsController.delete(id); assertEquals(actualSaveAppContactUS.getMessage(), "SUCCESS"); } @Test public void deleteExceptionTest() throws Exception { PortalRestResponse actualSaveAppContactUS = null; Long id = (long) 1; Mockito.when(contactUsService.deleteContactUs(id)).thenThrow(new Exception()); actualSaveAppContactUS = appContactUsController.delete(id); assertEquals(actualSaveAppContactUS.getMessage(), "failure"); } @Test public void getPortalDetailsTest(){ PortalRestResponse actualResponse = new PortalRestResponse(); PortalRestResponse expectedResponse = new PortalRestResponse(); expectedResponse.setStatus(PortalRestStatusEnum.OK); expectedResponse.setMessage("success"); 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\""); PowerMockito.mockStatic(SystemProperties.class); PowerMockito.mockStatic(EPCommonSystemProperties.class); Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.USH_TICKET_URL)).thenReturn("http://todo_enter_ush_ticket_url"); Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.PORTAL_INFO_URL)).thenReturn("https://todo_enter_portal_info_url"); Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS)).thenReturn("portal@lists.onap.org"); actualResponse = appContactUsController.getPortalDetails(mockedRequest); assertTrue(actualResponse.getStatus().compareTo(PortalRestStatusEnum.OK) == 0); } @Test public void getPortalDetailsExceptionTest(){ PortalRestResponse actualResponse = new PortalRestResponse(); PortalRestResponse expectedResponse = new PortalRestResponse(); expectedResponse.setStatus(PortalRestStatusEnum.ERROR); expectedResponse.setMessage("failure"); expectedResponse.setResponse(null); PowerMockito.mockStatic(SystemProperties.class); PowerMockito.mockStatic(EPCommonSystemProperties.class); Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.USH_TICKET_URL)).thenThrow(nullPointerException); Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.PORTAL_INFO_URL)).thenReturn("https://todo_enter_portal_info_url"); Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.FEEDBACK_EMAIL_ADDRESS)).thenReturn("portal@lists.onap.org"); actualResponse = appContactUsController.getPortalDetails(mockedRequest); assertEquals(actualResponse, expectedResponse); } }