/*- * ============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.*; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; 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.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.onap.portalapp.portal.core.MockEPUser; import org.onap.portalapp.portal.domain.EPUser; import org.onap.portalapp.portal.ecomp.model.PortalRestResponse; import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum; import org.onap.portalapp.portal.ecomp.model.SearchResultItem; import org.onap.portalapp.portal.framework.MockitoTestSuite; import org.onap.portalapp.portal.service.AdminRolesService; import org.onap.portalapp.portal.service.AdminRolesServiceImpl; import org.onap.portalapp.portal.service.DashboardSearchService; import org.onap.portalapp.portal.service.DashboardSearchServiceImpl; import org.onap.portalapp.portal.transport.CommonWidget; import org.onap.portalapp.portal.transport.CommonWidgetMeta; import org.onap.portalapp.portal.utils.EPCommonSystemProperties; import org.onap.portalapp.util.EPUserUtils; import org.onap.portalsdk.core.domain.AuditLog; import org.onap.portalsdk.core.domain.support.CollaborateList; import org.onap.portalsdk.core.service.AuditService; 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; import org.springframework.beans.factory.annotation.Autowired; @RunWith(PowerMockRunner.class) @PrepareForTest({EPUserUtils.class, CollaborateList.class, SystemProperties.class, EPCommonSystemProperties.class}) public class DashboardControllerTest { @Mock DashboardSearchService searchService = new DashboardSearchServiceImpl(); @InjectMocks DashboardController dashboardController; @Mock AdminRolesService adminRolesService = new AdminRolesServiceImpl(); @Autowired AuditService auditService; @Before public void setup() { MockitoAnnotations.initMocks(this); } MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); NullPointerException nullPointerException = new NullPointerException(); MockEPUser mockUser = new MockEPUser(); public CommonWidgetMeta mockCommonWidgetMeta() { CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta(); List widgetList = new ArrayList<>(); CommonWidget commonWidget = new CommonWidget(); commonWidget.setId((long) 1); commonWidget.setCategory("test"); commonWidget.setHref("testhref"); commonWidget.setTitle("testTitle"); commonWidget.setContent("testcontent"); commonWidget.setEventDate("2017-03-24"); commonWidget.setSortOrder(1); widgetList.add(commonWidget); commonWidgetMeta.setItems(widgetList); return commonWidgetMeta; } public CommonWidget mockCommonWidget() { CommonWidget commonWidget = new CommonWidget(); commonWidget.setId((long) 1); commonWidget.setCategory("test"); commonWidget.setHref("testhref"); commonWidget.setTitle("testTitle"); commonWidget.setContent("testcontent"); commonWidget.setEventDate("testDate"); commonWidget.setSortOrder(1); return commonWidget; } @Test public void getWidgetDataTest() throws IOException { String resourceType = null; PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setMessage("Unexpected resource type null"); expectedData.setResponse(null); PortalRestResponse actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType); assertEquals(expectedData,actualResponse); } @Test public void getWidgetDataTestXSS() { String resourceType = "“>"; PortalRestResponse expectedData = new PortalRestResponse<>(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setMessage("Unexpected resource type “>"); expectedData.setResponse(null); PortalRestResponse actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType); assertEquals(expectedData, actualResponse); } @Test public void getWidgetDataWithValidResourceTest() throws IOException { String resourceType = "EVENTS"; CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta(); commonWidgetMeta.setCategory(null); Mockito.when(searchService.getWidgetData(resourceType)).thenReturn(commonWidgetMeta); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.OK); expectedData.setMessage("success"); expectedData.setResponse(commonWidgetMeta); PortalRestResponse actualResponse = dashboardController.getWidgetData(mockedRequest, resourceType); assertEquals(expectedData,actualResponse); } @Test public void saveWidgetDataBulkNullTest() throws IOException { CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta(); commonWidgetMeta.setCategory(null); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setMessage("ERROR"); expectedData.setResponse("Category cannot be null or empty"); PortalRestResponse actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta); assertEquals(expectedData,actualResponse); } @Test public void saveWidgetDataBulkXSSTest() { CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta(); commonWidgetMeta.setCategory(""); PortalRestResponse expectedData = new PortalRestResponse<>(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setResponse("ERROR"); expectedData.setMessage("Unsafe resource type " + commonWidgetMeta.toString()); PortalRestResponse actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta); assertEquals(expectedData,actualResponse); } @Test public void saveWidgetUnexpectedDataBulkTest() throws IOException { CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta(); commonWidgetMeta.setCategory("Unexpected Data"); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setMessage("Unexpected resource type Unexpected Data"); expectedData.setResponse(null); PortalRestResponse actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta); assertEquals(expectedData,actualResponse); } @Test public void saveWidgetInvalidDataBulkTest() throws IOException { CommonWidgetMeta commonWidgetMeta= mockCommonWidgetMeta(); commonWidgetMeta.setCategory("EVENTS"); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setMessage("Invalid category: test"); expectedData.setResponse(null); PortalRestResponse actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta); assertEquals(expectedData,actualResponse); } @Test public void saveWidgetDataBulkTest() throws IOException { List widgetList = new ArrayList<>(); CommonWidget commonWidget = new CommonWidget("EVENTS", "http://test.com", "testTitle", "testcontent", "2017-07-01", 1); widgetList.add(commonWidget); CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta("EVENTS", widgetList); commonWidgetMeta.setItems(widgetList); commonWidgetMeta.setCategory("EVENTS"); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.OK); expectedData.setMessage("success"); expectedData.setResponse("success"); Mockito.when(searchService.saveWidgetDataBulk(commonWidgetMeta)).thenReturn("success"); PortalRestResponse actualResponse = dashboardController.saveWidgetDataBulk(commonWidgetMeta); assertEquals(expectedData,actualResponse); } @Test public void saveWidgetDataNullTest() throws IOException { CommonWidget commonWidget = mockCommonWidget(); commonWidget.setId((long)1); commonWidget.setContent("test"); commonWidget.setCategory(null); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setMessage("ERROR"); expectedData.setResponse("Category cannot be null or empty"); Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); PortalRestResponse actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse); assertEquals(expectedData,actualResponse); } @Test public void saveWidgetDataXSSTest() { CommonWidget commonWidget = mockCommonWidget(); commonWidget.setId((long)1); commonWidget.setContent("test"); commonWidget.setCategory("
X"); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setResponse("ERROR"); expectedData.setMessage("Unsafe resource type " + commonWidget.toString()); Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); PortalRestResponse actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse); assertEquals(expectedData,actualResponse); } @Test public void saveWidgetDataTitleTest() throws IOException { CommonWidget commonWidget = mockCommonWidget(); commonWidget.setId((long)1); commonWidget.setContent("test"); commonWidget.setTitle("test"); commonWidget.setEventDate("2017-05-06"); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setMessage("Invalid category: test"); expectedData.setResponse(null); Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); PortalRestResponse actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse); assertEquals(expectedData.getMessage(),actualResponse.getMessage()); } @Test public void saveWidgetDataErrorTest() throws IOException { CommonWidget commonWidget = mockCommonWidget(); commonWidget.setEventDate("2017-03-05"); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.ERROR); expectedData.setMessage("Invalid category: test"); expectedData.setResponse(null); Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); PortalRestResponse actualResponse = dashboardController.saveWidgetData(commonWidget,mockedRequest, mockedResponse); assertEquals(expectedData,actualResponse); } @Test public void saveWidgetDataTest() throws IOException { CommonWidgetMeta commonWidgetMeta= new CommonWidgetMeta(); List widgetList = new ArrayList<>(); CommonWidget commonWidget = new CommonWidget(); commonWidget.setId((long) 1); commonWidget.setCategory("EVENTS"); commonWidget.setHref("http://test.com"); commonWidget.setTitle("testTitle"); commonWidget.setContent("testcontent"); commonWidget.setEventDate("2017-07-01"); commonWidget.setSortOrder(1); widgetList.add(commonWidget); commonWidgetMeta.setItems(widgetList); commonWidgetMeta.setCategory("EVENTS"); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.OK); expectedData.setMessage("success"); expectedData.setResponse("success"); Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true); Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success"); PortalRestResponse actualResponse = dashboardController.saveWidgetData(commonWidget, mockedRequest, mockedResponse); assertEquals(expectedData,actualResponse); } @Test public void deleteWidgetDataTest() throws IOException { CommonWidget commonWidget = mockCommonWidget(); commonWidget.setEventDate("2017-03-25"); PortalRestResponse expectedData = new PortalRestResponse(); expectedData.setStatus(PortalRestStatusEnum.OK); expectedData.setMessage("success"); expectedData.setResponse(null); Mockito.when(searchService.saveWidgetData(commonWidget)).thenReturn("success"); PortalRestResponse actualResponse = dashboardController.deleteWidgetData(commonWidget); assertEquals(expectedData,actualResponse); } @Test public void deleteWidgetDataXSSTest() { CommonWidget commonWidget = mockCommonWidget(); commonWidget.setCategory("