b9ebe170f8f859adac6d29cf7363563d527cc45a
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38
39 package org.onap.portalsdk.analytics.controller;
40
41
42 import static org.junit.Assert.*;
43
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpSession;
46
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.mockito.MockitoAnnotations;
53 import org.mockito.invocation.InvocationOnMock;
54 import org.mockito.stubbing.Answer;
55 import org.onap.portalsdk.analytics.model.ReportHandler;
56 import org.onap.portalsdk.analytics.model.definition.ReportDefinition;
57 import org.onap.portalsdk.analytics.model.runtime.ReportRuntime;
58 import org.onap.portalsdk.analytics.system.AppUtils;
59 import org.onap.portalsdk.analytics.system.DbUtils;
60 import org.onap.portalsdk.analytics.system.Globals;
61 import org.onap.portalsdk.analytics.system.IAppUtils;
62 import org.onap.portalsdk.analytics.util.AppConstants;
63 import org.powermock.api.mockito.PowerMockito;
64 import org.powermock.core.classloader.annotations.PrepareForTest;
65 import org.powermock.modules.junit4.PowerMockRunner;
66
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({ WizardProcessor.class, AppUtils.class, Globals.class, DbUtils.class})
69 public class WizardProcessorTest {
70
71         WizardProcessor wizardProcessor;
72         
73         @Mock
74         HttpServletRequest httpServletRequest;
75         
76         @Mock
77         HttpSession httpSession;
78
79         @Mock
80         IAppUtils iAppUtils;
81         
82         @Mock
83         ReportRuntime reportRuntime;
84
85         @Mock 
86         ReportHandler reportHandler;
87         
88         @Mock
89         ReportDefinition reportDefinition;
90
91         @Mock
92         WizardSequence wizardSequence;
93         
94         
95         @Before
96     public void init() throws Exception {
97                                 
98                 PowerMockito.mockStatic(Globals.class);
99                 PowerMockito.mockStatic(AppUtils.class);
100                 PowerMockito.mockStatic(DbUtils.class);
101                                                 
102                 MockitoAnnotations.initMocks(this);
103
104                 PowerMockito.when(Globals.getAppUtils()).thenReturn(iAppUtils);
105             PowerMockito.when(AppUtils.getImgFolderURL()).thenReturn("http://sometesturl:9090/hi");
106             PowerMockito.when(httpServletRequest.getSession()).thenReturn(httpSession);
107         
108             PowerMockito.when(httpSession.getAttribute(AppConstants.SI_REPORT_DEFINITION)).thenReturn(reportDefinition);
109             
110                 PowerMockito.when(AppUtils.nvl(Mockito.anyString())).thenAnswer(new Answer<String>() {
111                         @Override
112                         public String answer(InvocationOnMock invocation) throws Throwable {
113                               Object[] args = invocation.getArguments();
114                               String inputString = (String) args[0];
115                                         return (inputString == null) ? "" : inputString;
116                         }
117                 } );
118                 
119                 PowerMockito.when(AppUtils.getRequestNvlValue(Mockito.anyObject(), Mockito.anyString())).thenAnswer(new Answer<String>() {
120                         @Override
121                         public String answer(InvocationOnMock invocation) throws Throwable {
122                               Object[] args = invocation.getArguments();
123                               HttpServletRequest request = (HttpServletRequest) args[0];
124                               String valueID = (String) args[1];
125                               String value = (String) request.getAttribute(valueID);
126
127                                 /**
128                                         if (value == null)
129                                                 value = ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(valueID));
130                                                 ***/
131
132                                         return (value == null) ? "" : value;
133                         }
134                 } );
135         
136                 
137                 PowerMockito.when(AppUtils.getRequestValue(Mockito.anyObject(), Mockito.anyString())).thenAnswer(new Answer<String>() {
138                         @Override
139                         public String answer(InvocationOnMock invocation) throws Throwable {
140                               Object[] args = invocation.getArguments();
141                               HttpServletRequest request = (HttpServletRequest) args[0];
142                               String valueID = (String) args[1];
143                               String value = (String) request.getAttribute(valueID);
144
145                               /***
146                                         if (value == null)
147                                                 value = ESAPI.encoder().encodeForSQL( SecurityCodecUtil.getCodec(), request.getParameter(valueID));
148                                    ***/
149                                         return value;
150                         }
151                 } );
152                 
153                 
154                 
155                 
156                 wizardProcessor = new WizardProcessor();
157         }
158
159         
160         @Test
161         public void testWizardProcessor() {
162                 WizardProcessor wizardProcessorLocal = new WizardProcessor();
163                                 
164                 assertNotNull(wizardProcessorLocal);
165         }
166
167         @Test(expected=NullPointerException.class)
168         public void testPersistReportDefinition_null_arguments() throws Exception {
169                 wizardProcessor.persistReportDefinition(null, null);
170         }
171
172         @Test(expected=NullPointerException.class)
173         public void testPersistReportDefinition_null_arguments_case1() throws Exception {
174                 wizardProcessor.persistReportDefinition(httpServletRequest, null);
175         }
176
177         @Test(expected=NullPointerException.class)
178         public void testPersistReportDefinition_null_arguments_case2() throws Exception {
179                 Mockito.when(httpSession.getAttribute(AppConstants.SI_REPORT_RUNTIME)).thenReturn(reportRuntime);
180                 wizardProcessor.persistReportDefinition(httpServletRequest, null);
181         }
182
183         @Test(expected=NullPointerException.class)
184         public void testPersistReportDefinition_null_arguments_case3() throws Exception {
185                 Mockito.when(httpSession.getAttribute(AppConstants.SI_REPORT_RUNTIME)).thenReturn(reportRuntime);
186                 PowerMockito.when(reportRuntime.getReportID()).thenReturn("Report#1");
187                 
188                 wizardProcessor.persistReportDefinition(httpServletRequest, null);
189         }
190
191         @Test
192         public void testPersistReportDefinition_not_null_arguments_case1() throws Exception {
193                 Mockito.when(httpSession.getAttribute(AppConstants.SI_REPORT_RUNTIME)).thenReturn(reportRuntime);
194                 Mockito.when(reportRuntime.getReportID()).thenReturn("Report#1");
195                 wizardProcessor.persistReportDefinition(httpServletRequest, reportDefinition);
196         }
197         
198         @Test
199         public void testPersistReportDefinition_not_null_arguments_case2() throws Exception {
200                 Mockito.when(httpSession.getAttribute(AppConstants.SI_REPORT_RUNTIME)).thenReturn(reportRuntime);
201                 Mockito.when(reportRuntime.getReportID()).thenReturn("Report#1");
202                 Mockito.when(reportDefinition.getReportID()).thenReturn("Report#1");
203
204                 wizardProcessor.persistReportDefinition(httpServletRequest, reportDefinition);
205         }
206         
207         @Test(expected=NullPointerException.class)
208         public void testProcessWizardStep_null_arguments_case1() throws Exception {
209                 wizardProcessor.processWizardStep(null);
210         }
211         
212         @Test(expected=NullPointerException.class)
213         public void testProcessWizardStep_not_null_arguments_case1() throws Exception {
214                 wizardProcessor.processWizardStep(httpServletRequest);
215         }
216         
217         @Test(expected=NullPointerException.class)
218         public void testProcessWizardStep_not_null_arguments_case2() throws Exception {
219                 Mockito.when(httpServletRequest.getParameter(AppConstants.RI_WIZARD_ACTION)).thenReturn(null);
220                 wizardProcessor.processWizardStep(httpServletRequest);
221         }
222
223         @Test(expected=NullPointerException.class)
224         public void testProcessWizardStep_not_null_arguments_case3() throws Exception {
225                 Mockito.when(httpServletRequest.getParameter(AppConstants.RI_WIZARD_ACTION)).thenReturn("NA");
226                 wizardProcessor.processWizardStep(httpServletRequest);
227         }
228         
229         @Test
230         public void testProcessWizardStep_not_null_arguments_case4() throws Exception {
231                 PowerMockito.whenNew(ReportHandler.class).withNoArguments().thenReturn(reportHandler);  
232                 Mockito.when(AppUtils.getRequestValue(httpServletRequest, AppConstants.RI_REPORT_ID)).thenReturn("Report#1");
233                 Mockito.when(reportHandler.loadReportDefinition(httpServletRequest,"Report#1")).thenReturn(reportDefinition);
234                 Mockito.when(httpServletRequest.getParameter(AppConstants.RI_WIZARD_ACTION)).thenReturn("NA");
235                 Mockito.when(AppUtils.getRequestNvlValue(httpServletRequest, "showDashboardOptions")).thenReturn("");
236                 Mockito.when(reportDefinition.getWizardSequence()).thenReturn(wizardSequence);
237                 Mockito.when(wizardSequence.getCurrentStep()).thenReturn("NA");
238                 Mockito.when(wizardSequence.getCurrentSubStep()).thenReturn("NA");
239                 wizardProcessor.processWizardStep(httpServletRequest);
240         }
241         
242         @Test
243         public void testProcessWizardStep_not_null_arguments_case5() throws Exception {
244                 PowerMockito.whenNew(ReportHandler.class).withNoArguments().thenReturn(reportHandler);  
245                 Mockito.when(AppUtils.getRequestValue(httpServletRequest, AppConstants.RI_REPORT_ID)).thenReturn("Report#1");
246                 Mockito.when(AppUtils.getRequestNvlValue(httpServletRequest, "showDashboardOptions")).thenReturn("");
247         
248                 Mockito.when(AppUtils.getRequestNvlValue(httpServletRequest, "reportType")).thenReturn(AppConstants.RT_DASHBOARD);
249                 Mockito.when(reportHandler.loadReportDefinition(httpServletRequest,"Report#1")).thenReturn(reportDefinition);
250                 
251                 Mockito.when(httpServletRequest.getParameter(AppConstants.RI_WIZARD_ACTION)).thenReturn("NA");
252                 Mockito.when(reportDefinition.getWizardSequence()).thenReturn(wizardSequence);
253                 Mockito.when(wizardSequence.getCurrentStep()).thenReturn(AppConstants.WS_DEFINITION);
254                 Mockito.when(wizardSequence.getCurrentSubStep()).thenReturn("NA");
255                 
256                 Mockito.when(reportDefinition.getReportID()).thenReturn("1");
257                 
258                 wizardProcessor.processWizardStep(httpServletRequest);
259         }
260         
261         
262         /***
263
264         @Test
265         public void testProcessImportSemaphorePopup() {
266                 fail("Not yet implemented");
267         }
268
269         @Test
270         public void testProcessSemaphorePopup() {
271                 fail("Not yet implemented");
272         }
273
274         @Test
275         public void testProcessAdhocSchedule() {
276                 fail("Not yet implemented");
277         }
278
279 */}