Changes made to upgrade pom version
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / WebAnalyticsExtAppControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import static org.junit.Assert.*;
41
42 import java.io.InputStream;
43 import java.nio.charset.StandardCharsets;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.apache.commons.io.IOUtils;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.InjectMocks;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.onap.portalapp.portal.controller.WebAnalyticsExtAppController;
57 import org.onap.portalapp.portal.domain.EPApp;
58 import org.onap.portalapp.portal.framework.MockitoTestSuite;
59 import org.onap.portalapp.portal.service.AdminRolesService;
60 import org.onap.portalapp.portal.service.AdminRolesServiceImpl;
61 import org.onap.portalapp.portal.service.AppsCacheService;
62 import org.onap.portalapp.portal.service.AppsCacheServiceImple;
63 import org.onap.portalapp.portal.transport.Analytics;
64 import org.onap.portalsdk.core.onboarding.crossapi.PortalAPIResponse;
65 import org.onap.portalsdk.core.service.AuditService;
66 import org.onap.portalsdk.core.service.AuditServiceImpl;
67 import org.onap.portalsdk.core.util.SystemProperties;
68 import org.powermock.api.mockito.PowerMockito;
69 import org.powermock.core.classloader.annotations.PrepareForTest;
70 import org.powermock.modules.junit4.PowerMockRunner;
71
72
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({SystemProperties.class,IOUtils.class,Object.class})
75 public class WebAnalyticsExtAppControllerTest {
76
77         
78         
79         @InjectMocks
80         WebAnalyticsExtAppController webAnalyticsExtAppController = new WebAnalyticsExtAppController() {
81                 
82                 protected void storeAuxAnalytics(Analytics analyticsMap, String appName) {
83                         storeAnalyticsCalled = true;
84                 }
85         };
86
87         @Mock
88         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
89         
90         @Mock
91         AppsCacheService appCacheService = new AppsCacheServiceImple();
92
93         @Mock
94         AuditService auditService = new AuditServiceImpl();
95         @Mock
96         Analytics analytics;
97         
98         Boolean storeAnalyticsCalled = false;
99         
100
101 //      @Mock 
102 //      InputStream analyticsFileStream;
103
104
105         @Before
106         public void setup() {
107                 MockitoAnnotations.initMocks(this);
108         }
109
110         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
111
112         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
113         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
114         NullPointerException nullPointerException = new NullPointerException();
115         
116         /*@Test
117         public void getAnalyticsScriptTest() throws Exception
118         {
119                 String expectedResponse = "http://www.ecomp.com";
120
121                 InputStream analyticsFileStream = null;
122                 PowerMockito.mockStatic(SystemProperties.class);
123                 PowerMockito.mockStatic(IOUtils.class);
124                 Mockito.when(IOUtils.toString(analyticsFileStream, StandardCharsets.UTF_8.name())).thenReturn("PORTAL_ENV_URL");
125                 Mockito.when(SystemProperties.getProperty("frontend_url")).thenReturn("http://www.ecomp.com/test");
126                 String actualResponse = webAnalyticsExtAppController.getAnalyticsScript(mockedRequest);
127 //      assertNull(webAnalyticsExtAppController.getAnalyticsScript(mockedRequest));
128                 
129         //      System.out.println(actualResponse);
130                 assertTrue(actualResponse.equals(expectedResponse));    
131         }*/
132
133         /*@Test
134         public void getAnalyticsScriptExceptionTest() throws Exception
135         {
136                 String expectedResponse = "";
137                 InputStream analyticsFileStream = null;
138                 PowerMockito.mockStatic(SystemProperties.class);
139                 PowerMockito.mockStatic(IOUtils.class);
140                 Mockito.when(IOUtils.toString(analyticsFileStream, StandardCharsets.UTF_8.name())).thenThrow(nullPointerException);
141                 Mockito.when(SystemProperties.getProperty("frontend_url")).thenReturn("http://www.ecomp.com/test");
142        String actualResponse = webAnalyticsExtAppController.getAnalyticsScript(mockedRequest);
143           assertEquals(actualResponse,expectedResponse);
144         }*/
145
146         @Test
147         public void storeAnalyticsScriptIfAnalyticsNullTest() throws Exception
148         {
149                 PortalAPIResponse       expectedPortalAPIResponse = new PortalAPIResponse(true, "error");
150                 Analytics analytics= null;
151                 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
152                 PortalAPIResponse       actualPortalAPIResponse =       webAnalyticsExtAppController.storeAnalyticsScript(mockedRequest, analytics);
153                 assertTrue(expectedPortalAPIResponse.getMessage().equals(actualPortalAPIResponse.getMessage()));
154                 assertTrue(expectedPortalAPIResponse.getStatus().equals(actualPortalAPIResponse.getStatus()));  
155         }
156         
157         @Test
158         public void storeAnalyticsScriptIfAnalyticsTest() throws Exception
159         {
160                 PortalAPIResponse       expectedPortalAPIResponse = new PortalAPIResponse(true, "ok");          
161                 expectedPortalAPIResponse.setMessage("success");
162                 EPApp appRecord =new EPApp();
163                 appRecord.setName("test");
164                 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn("test");
165                 Mockito.when(analytics.getUserid()).thenReturn("test");
166                 Mockito.when(analytics.getFunction()).thenReturn("test");
167                 PortalAPIResponse       actualPortalAPIResponse =       webAnalyticsExtAppController.storeAnalyticsScript(mockedRequest, analytics);
168                 assertTrue(expectedPortalAPIResponse.getMessage().equals(actualPortalAPIResponse.getMessage()));
169                 assertTrue(expectedPortalAPIResponse.getStatus().equals(actualPortalAPIResponse.getStatus()));  
170         }
171         
172         
173         @Test(expected = NullPointerException.class)
174         public void testGetAnalyticsScript() {
175                 PowerMockito.mockStatic(SystemProperties.class);
176                 Mockito.when(SystemProperties.getProperty("frontend_url")).thenReturn("http://www.ecomp.com/test");
177                 webAnalyticsExtAppController.getAnalyticsScript(mockedRequest);
178                 
179         }
180         
181         @Test
182         public void storeAnalyticsAuxScriptIfAnalyticsCallTest() throws Exception
183         {
184                 Analytics analytics= null;
185                 PowerMockito.mockStatic(SystemProperties.class);
186                 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
187                 Mockito.when(SystemProperties.containsProperty(WebAnalyticsExtAppController.FEED_ML)).thenReturn(true);
188                 Mockito.when(SystemProperties.getProperty(WebAnalyticsExtAppController.FEED_ML)).thenReturn("true");
189                 webAnalyticsExtAppController.storeAnalyticsScript(mockedRequest, analytics);
190                 assertTrue(storeAnalyticsCalled);
191         
192         }
193         
194         @Test
195         public void storeAnalyticsAuxScriptIfAnalyticsNoCallTest() throws Exception
196         {
197                 Analytics analytics= null;
198                 PowerMockito.mockStatic(SystemProperties.class);
199                 Mockito.when(mockedRequest.getHeader("uebkey")).thenReturn(null);
200                 Mockito.when(SystemProperties.containsProperty(WebAnalyticsExtAppController.FEED_ML)).thenReturn(false);
201                 Mockito.when(SystemProperties.getProperty(WebAnalyticsExtAppController.FEED_ML)).thenReturn("true");
202                 webAnalyticsExtAppController.storeAnalyticsScript(mockedRequest, analytics);
203                 assertTrue(!storeAnalyticsCalled);
204         
205         }
206                 
207 }