Fixed health check issue
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / ExternalAppsRestfulControllerTest.java
index ae2497e..890319d 100644 (file)
@@ -4,6 +4,8 @@
  * ===================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
  * ===================================================================
+ *  Modifications Copyright (c) 2019 Samsung
+ * ===================================================================
  *
  * Unless otherwise specified, all software contained herein is licensed
  * under the Apache License, Version 2.0 (the "License");
  *
  * ============LICENSE_END============================================
  *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * 
  */
 package org.onap.portalapp.portal.controller;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.mockito.Matchers.any;
 
 import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -49,13 +57,16 @@ import javax.servlet.http.HttpServletResponse;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.onap.portalapp.portal.controller.ExternalAppsRestfulController;
 import org.onap.portalapp.portal.core.MockEPUser;
+import org.onap.portalapp.portal.domain.EPApp;
+import org.onap.portalapp.portal.domain.EPRole;
 import org.onap.portalapp.portal.domain.EPUser;
+import org.onap.portalapp.portal.domain.EpAppType;
 import org.onap.portalapp.portal.framework.MockitoTestSuite;
 import org.onap.portalapp.portal.service.AdminRolesService;
 import org.onap.portalapp.portal.service.AdminRolesServiceImpl;
@@ -67,10 +78,12 @@ import org.onap.portalapp.portal.service.FunctionalMenuService;
 import org.onap.portalapp.portal.service.FunctionalMenuServiceImpl;
 import org.onap.portalapp.portal.service.UserNotificationService;
 import org.onap.portalapp.portal.service.UserNotificationServiceImpl;
+import org.onap.portalapp.portal.transport.EpNotificationItem;
 import org.onap.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
 import org.onap.portalapp.portal.transport.FunctionalMenuItem;
 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
 import org.onap.portalapp.portal.utils.EcompPortalUtils;
+import org.onap.portalsdk.core.onboarding.crossapi.PortalAPIResponse;
 import org.onap.portalsdk.core.service.DataAccessService;
 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
 import org.powermock.api.mockito.PowerMockito;
@@ -79,7 +92,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
 import org.slf4j.MDC;
 
 @RunWith(PowerMockRunner.class)
-@PrepareForTest({ MDC.class, EPCommonSystemProperties.class })
+@PrepareForTest({ MDC.class, EPCommonSystemProperties.class ,EPApp.class})
 public class ExternalAppsRestfulControllerTest {
 
        @InjectMocks
@@ -117,6 +130,32 @@ public class ExternalAppsRestfulControllerTest {
        NullPointerException nullPointerException = new NullPointerException();
 
        MockEPUser mockUser = new MockEPUser();
+       
+       public EPApp mockApp() {
+               EPApp app = new EPApp();
+               app.setName("Test");
+               app.setImageUrl("test");
+               app.setNameSpace("com.test.app");
+               app.setRolesInAAF(true);
+               app.setAppDescription("test");
+               app.setAppNotes("test");
+               app.setLandingPage("test");
+               app.setId((long) 10);
+               app.setAppRestEndpoint("test");
+               app.setAlternateLandingPage("test");
+               app.setName("test");
+               app.setMlAppName("test");
+               app.setMlAppAdminId("test");
+               app.setAppBasicAuthUsername("test");
+               app.setAppBasicAuthPassword("test");
+               app.setOpen(false);
+               app.setEnabled(true);
+               app.setUebKey("test");
+               app.setUebSecret("test");
+               app.setUebTopicName("test");
+               app.setAppType(EpAppType.GUI);
+               return app;
+       }
 
         @Test(expected = Exception.class)
         public void getFunctionalMenuItemsForUserIfUSerNullTest() throws
@@ -208,4 +247,165 @@ public class ExternalAppsRestfulControllerTest {
                                .getFavoritesForUser(mockedRequest, mockedResponse);
                assertEquals(actaulFavorites.size(), 1);
        }
+
+
+    @Test
+    public void publishNotificationTest_Success() throws Exception {
+        // input
+        EpNotificationItem notificationItem = new EpNotificationItem();
+        List<Long> roleList = new ArrayList<Long>();
+        Long role1 = 1L;
+        roleList.add(role1);
+        notificationItem.setRoleIds(roleList);
+        notificationItem.setPriority(1L);
+        notificationItem.setMsgHeader("testHeader");
+        notificationItem.setMsgDescription("Test Description");
+        Date currentDate = new Date();
+        Calendar c = Calendar.getInstance();
+        c.setTime(currentDate);
+        c.add(Calendar.DATE, 1);
+        Date currentDatePlusOne = c.getTime();
+        notificationItem.setStartTime(currentDate);
+        notificationItem.setEndTime(currentDatePlusOne);
+
+        // mock calls
+        Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("RxH3983AHiyBOQmj");
+        Map<String, String> params = new HashMap<>();
+        params.put("appKey", "RxH3983AHiyBOQmj");
+        List<EPApp> apps = new ArrayList<>();
+        EPApp app = new EPApp();
+        app.setId(123L);
+        apps.add(app);
+        Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null)).thenReturn(apps);
+        EPRole role = new EPRole();
+        role.setId(543L);
+        Mockito.when(epRoleService.getRole(123L, 1L)).thenReturn(role);
+
+        // run
+        Mockito.when(userNotificationService.saveNotification(notificationItem)).thenReturn("Test");
+        PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
+        // verify answer
+        assertNotNull(response);
+        assertEquals("ok", response.getStatus());
+        assertEquals("success", response.getMessage());
+        ArgumentCaptor<EpNotificationItem> capture = ArgumentCaptor.forClass(EpNotificationItem.class);
+        Mockito.verify(userNotificationService).saveNotification(capture.capture());
+        assertNotNull(capture.getValue());
+        EpNotificationItem createdNofification = capture.getValue();
+        assertNotNull(createdNofification.getRoleIds());
+        assertEquals(1, createdNofification.getRoleIds().size());
+        assertEquals(543L, createdNofification.getRoleIds().get(0).longValue());
+    }
+
+       @Test
+       public void publishNotificationXSSTest() throws Exception {
+               // input
+               EpNotificationItem notificationItem = new EpNotificationItem();
+               List<Long> roleList = new ArrayList<Long>();
+               Long role1 = 1L;
+               roleList.add(role1);
+               notificationItem.setRoleIds(roleList);
+               notificationItem.setPriority(1L);
+               notificationItem.setMsgHeader("<script>alert(‘XSS’)</script>");
+               notificationItem.setMsgDescription("Test Description");
+               Date currentDate = new Date();
+               Calendar c = Calendar.getInstance();
+               c.setTime(currentDate);
+               c.add(Calendar.DATE, 1);
+               Date currentDatePlusOne = c.getTime();
+               notificationItem.setStartTime(currentDate);
+               notificationItem.setEndTime(currentDatePlusOne);
+
+               // mock calls
+               Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("RxH3983AHiyBOQmj");
+               Map<String, String> params = new HashMap<>();
+               params.put("appKey", "RxH3983AHiyBOQmj");
+               List<EPApp> apps = new ArrayList<>();
+               EPApp app = new EPApp();
+               app.setId(123L);
+               apps.add(app);
+               Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null)).thenReturn(apps);
+               EPRole role = new EPRole();
+               role.setId(543L);
+               Mockito.when(epRoleService.getRole(123L, 1L)).thenReturn(role);
+
+               // run
+               Mockito.when(userNotificationService.saveNotification(notificationItem)).thenReturn("Test");
+               PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
+               // verify answer
+               assertNotNull(response);
+               assertEquals("error", response.getStatus());
+               assertEquals("failed", response.getMessage());
+       }
+
+    @Test
+    public void publishNotificationTest_EmptyAppHeader() throws Exception {
+        // input
+        EpNotificationItem notificationItem = new EpNotificationItem();
+        List<Long> roleList = new ArrayList<Long>();
+        Long role1 = 1L;
+        roleList.add(role1);
+        notificationItem.setRoleIds(roleList);
+        notificationItem.setPriority(1L);
+        notificationItem.setMsgHeader("testHeader");
+        notificationItem.setMsgDescription("Test Description");
+        Date currentDate = new Date();
+        Calendar c = Calendar.getInstance();
+        c.setTime(currentDate);
+        c.add(Calendar.DATE, 1);
+        Date currentDatePlusOne = c.getTime();
+        notificationItem.setStartTime(currentDate);
+        notificationItem.setEndTime(currentDatePlusOne);
+
+        Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
+        final Map<String, String> params = new HashMap<>();
+        params.put("appKey", null);
+        Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null))
+            .thenThrow(NullPointerException.class);
+
+        PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
+        assertNotNull(response);
+        assertEquals("ok", response.getStatus());
+        assertEquals("success", response.getMessage());
+        ArgumentCaptor<EpNotificationItem> capture = ArgumentCaptor.forClass(EpNotificationItem.class);
+        Mockito.verify(userNotificationService).saveNotification(capture.capture());
+        assertNotNull(capture.getValue());
+        EpNotificationItem createdNofification = capture.getValue();
+        assertNotNull(createdNofification.getRoleIds());
+        assertEquals(0, createdNofification.getRoleIds().size());
+    }
+
+    @Test
+    public void publishNotificationTest_ErrorResponse() throws Exception {
+        // input
+        EpNotificationItem notificationItem = new EpNotificationItem();
+        List<Long> roleList = new ArrayList<Long>();
+        Long role1 = 1L;
+        roleList.add(role1);
+        notificationItem.setRoleIds(roleList);
+        notificationItem.setPriority(1L);
+        notificationItem.setMsgHeader("testHeader");
+        notificationItem.setMsgDescription("Test Description");
+        Date currentDate = new Date();
+        Calendar c = Calendar.getInstance();
+        c.setTime(currentDate);
+        c.add(Calendar.DATE, 1);
+        Date currentDatePlusOne = c.getTime();
+        notificationItem.setStartTime(currentDate);
+        notificationItem.setEndTime(currentDatePlusOne);
+
+        Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
+        final Map<String, String> params = new HashMap<>();
+        params.put("appKey", null);
+        Mockito.when(DataAccessService.executeNamedQuery("getMyAppDetailsByUebKey", params, null))
+            .thenThrow(NullPointerException.class);
+        Mockito.when(userNotificationService.saveNotification(any(EpNotificationItem.class))).
+            thenThrow(new NullPointerException("expected message"));
+
+        PortalAPIResponse response = externalAppsRestfulController.publishNotification(mockedRequest, notificationItem);
+        assertNotNull(response);
+        assertEquals("error", response.getStatus());
+        assertEquals("expected message", response.getMessage());
+    }
+
 }