1 package org.openecomp.portalapp.portal.test.controller;
 
   3 import static org.junit.Assert.assertTrue;
 
   5 import java.io.InputStream;
 
   6 import java.nio.charset.StandardCharsets;
 
   8 import javax.servlet.http.HttpServletRequest;
 
   9 import javax.servlet.http.HttpServletResponse;
 
  11 import org.apache.commons.io.IOUtils;
 
  12 import org.junit.Before;
 
  13 import org.junit.Test;
 
  14 import org.junit.runner.RunWith;
 
  15 import org.mockito.InjectMocks;
 
  16 import org.mockito.Mock;
 
  17 import org.mockito.Mockito;
 
  18 import org.mockito.MockitoAnnotations;
 
  19 import org.openecomp.portalapp.portal.controller.WebAnalyticsExtAppController;
 
  20 import org.openecomp.portalapp.portal.domain.EPApp;
 
  21 import org.openecomp.portalapp.portal.service.AdminRolesService;
 
  22 import org.openecomp.portalapp.portal.service.AdminRolesServiceImpl;
 
  23 import org.openecomp.portalapp.portal.service.AppsCacheService;
 
  24 import org.openecomp.portalapp.portal.service.AppsCacheServiceImple;
 
  25 import org.openecomp.portalapp.portal.transport.Analytics;
 
  26 import org.openecomp.portalapp.test.framework.MockitoTestSuite;
 
  27 import org.openecomp.portalsdk.core.onboarding.crossapi.PortalAPIResponse;
 
  28 import org.openecomp.portalsdk.core.service.AuditService;
 
  29 import org.openecomp.portalsdk.core.service.AuditServiceImpl;
 
  30 import org.openecomp.portalsdk.core.util.SystemProperties;
 
  31 import org.powermock.api.mockito.PowerMockito;
 
  32 import org.powermock.core.classloader.annotations.PrepareForTest;
 
  33 import org.powermock.modules.junit4.PowerMockRunner;
 
  36 @RunWith(PowerMockRunner.class)
 
  37 @PrepareForTest({SystemProperties.class,IOUtils.class,Object.class})
 
  38 public class WebAnalyticsExtAppControllerTest {
 
  43         WebAnalyticsExtAppController webAnalyticsExtAppController = new WebAnalyticsExtAppController();
 
  46         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
 
  49         AppsCacheService appCacheService = new AppsCacheServiceImple();
 
  52         AuditService auditService = new AuditServiceImpl();
 
  55 //      InputStream analyticsFileStream;
 
  60                 MockitoAnnotations.initMocks(this);
 
  63         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
 
  65         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
 
  66         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
 
  67         NullPointerException nullPointerException = new NullPointerException();
 
  69         @Test(expected= NullPointerException.class)
 
  70         public void getAnalyticsScriptTest() throws Exception
 
  72                 String expectedResponse = "http://www.ecomp.com";
 
  74                 InputStream analyticsFileStream = null;
 
  75                 PowerMockito.mockStatic(SystemProperties.class);
 
  76                 PowerMockito.mockStatic(IOUtils.class);
 
  77                 Mockito.when(IOUtils.toString(analyticsFileStream, StandardCharsets.UTF_8.name())).thenReturn("PORTAL_ENV_URL");
 
  78                 Mockito.when(SystemProperties.getProperty("frontend_url")).thenReturn("http://www.ecomp.com/test");
 
  79                 //String actualResponse = webAnalyticsExtAppController.getAnalyticsScript(mockedRequest);
 
  80                 webAnalyticsExtAppController.getAnalyticsScript(mockedRequest);
 
  81                 //assertTrue(actualResponse.equals(expectedResponse));  
 
  84         @Test(expected= NullPointerException.class)
 
  85         public void getAnalyticsScriptExceptionTest() throws Exception
 
  87                 String expectedResponse = "";
 
  88                 InputStream analyticsFileStream = null;
 
  89                 PowerMockito.mockStatic(SystemProperties.class);
 
  90                 PowerMockito.mockStatic(IOUtils.class);
 
  91                 Mockito.when(IOUtils.toString(analyticsFileStream, StandardCharsets.UTF_8.name())).thenThrow(nullPointerException);
 
  92                 Mockito.when(SystemProperties.getProperty("frontend_url")).thenReturn("http://www.ecomp.com/test");
 
  93        webAnalyticsExtAppController.getAnalyticsScript(mockedRequest);
 
  98         public void storeAnalyticsScriptIfAnalyticsNullTest() throws Exception
 
 100                 PortalAPIResponse       expectedPortalAPIResponse = new PortalAPIResponse(true, "error");
 
 101                 Analytics analytics= null;
 
 102                 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
 
 103                 PortalAPIResponse       actualPortalAPIResponse =       webAnalyticsExtAppController.storeAnalyticsScript(mockedRequest, analytics);
 
 104                 assertTrue(expectedPortalAPIResponse.getMessage().equals(actualPortalAPIResponse.getMessage()));
 
 105                 assertTrue(expectedPortalAPIResponse.getStatus().equals(actualPortalAPIResponse.getStatus()));  
 
 109         public void storeAnalyticsScriptTest() throws Exception
 
 111                 PortalAPIResponse       expectedPortalAPIResponse = new PortalAPIResponse(true, "success");
 
 112                 Analytics analytics=  new Analytics();
 
 113                 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("Test_Key");
 
 114                 EPApp app = new EPApp();
 
 115                 Mockito.when(appCacheService.getAppForAnalytics("Test_Key")).thenReturn(app);
 
 116                 PortalAPIResponse       actualPortalAPIResponse =       webAnalyticsExtAppController.storeAnalyticsScript(mockedRequest, analytics);
 
 117                 assertTrue(expectedPortalAPIResponse.getMessage().equals(actualPortalAPIResponse.getMessage()));
 
 118                 assertTrue(expectedPortalAPIResponse.getStatus().equals(actualPortalAPIResponse.getStatus()));  
 
 121         public void storeAnalyticsScriptIfAppNullTest() throws Exception
 
 123                 PortalAPIResponse       expectedPortalAPIResponse = new PortalAPIResponse(true, "success");
 
 124                 Analytics analytics=  new Analytics();
 
 125                 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("Test_Key");
 
 126                 Mockito.when(appCacheService.getAppForAnalytics("Test_Key")).thenReturn(null);
 
 127                 PortalAPIResponse       actualPortalAPIResponse =       webAnalyticsExtAppController.storeAnalyticsScript(mockedRequest, analytics);
 
 128                 assertTrue(expectedPortalAPIResponse.getMessage().equals(actualPortalAPIResponse.getMessage()));
 
 129                 assertTrue(expectedPortalAPIResponse.getStatus().equals(actualPortalAPIResponse.getStatus()));