1 package org.openecomp.portalapp.portal.test.controller;
3 import static org.junit.Assert.*;
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();
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 // assertNull(webAnalyticsExtAppController.getAnalyticsScript(mockedRequest));
82 // System.out.println(actualResponse);
83 assertTrue(actualResponse.equals(expectedResponse));
87 public void getAnalyticsScriptExceptionTest() throws Exception
89 String expectedResponse = "";
90 InputStream analyticsFileStream = null;
91 PowerMockito.mockStatic(SystemProperties.class);
92 PowerMockito.mockStatic(IOUtils.class);
93 Mockito.when(IOUtils.toString(analyticsFileStream, StandardCharsets.UTF_8.name())).thenThrow(nullPointerException);
94 Mockito.when(SystemProperties.getProperty("frontend_url")).thenReturn("http://www.ecomp.com/test");
95 String actualResponse = webAnalyticsExtAppController.getAnalyticsScript(mockedRequest);
96 assertEquals(actualResponse,expectedResponse);
100 public void storeAnalyticsScriptIfAnalyticsNullTest() throws Exception
102 PortalAPIResponse expectedPortalAPIResponse = new PortalAPIResponse(true, "error");
103 Analytics analytics= null;
104 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
105 PortalAPIResponse actualPortalAPIResponse = webAnalyticsExtAppController.storeAnalyticsScript(mockedRequest, analytics);
106 assertTrue(expectedPortalAPIResponse.getMessage().equals(actualPortalAPIResponse.getMessage()));
107 assertTrue(expectedPortalAPIResponse.getStatus().equals(actualPortalAPIResponse.getStatus()));