[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common-test / src / main / java / org / openecomp / portalapp / portal / test / controller / WebAnalyticsExtAppControllerTest.java
1 package org.openecomp.portalapp.portal.test.controller;
2
3 import static org.junit.Assert.assertTrue;
4
5 import java.io.InputStream;
6 import java.nio.charset.StandardCharsets;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
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;
34
35
36 @RunWith(PowerMockRunner.class)
37 @PrepareForTest({SystemProperties.class,IOUtils.class,Object.class})
38 public class WebAnalyticsExtAppControllerTest {
39
40         
41         
42         @InjectMocks
43         WebAnalyticsExtAppController webAnalyticsExtAppController = new WebAnalyticsExtAppController();
44
45         @Mock
46         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
47         
48         @Mock
49         AppsCacheService appCacheService = new AppsCacheServiceImple();
50
51         @Mock
52         AuditService auditService = new AuditServiceImpl();
53
54 //      @Mock 
55 //      InputStream analyticsFileStream;
56
57
58         @Before
59         public void setup() {
60                 MockitoAnnotations.initMocks(this);
61         }
62
63         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
64
65         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
66         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
67         NullPointerException nullPointerException = new NullPointerException();
68         
69         @Test(expected= NullPointerException.class)
70         public void getAnalyticsScriptTest() throws Exception
71         {
72                 String expectedResponse = "http://www.ecomp.com";
73
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));  
82         }
83
84         @Test(expected= NullPointerException.class)
85         public void getAnalyticsScriptExceptionTest() throws Exception
86         {
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);
94         
95         }
96
97         @Test
98         public void storeAnalyticsScriptIfAnalyticsNullTest() throws Exception
99         {
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()));  
106         }
107         
108         @Test
109         public void storeAnalyticsScriptTest() throws Exception
110         {
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()));  
119         }
120         @Test
121         public void storeAnalyticsScriptIfAppNullTest() throws Exception
122         {
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()));  
130         }
131 }