6c84ce1233a93b4957eed6b9b96e371f29d47108
[portal/sdk.git] /
1 package org.onap.portalsdk.analytics.controller;
2 /*
3  * ============LICENSE_START==========================================
4  * ONAP Portal SDK
5  * ===================================================================
6  * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
7  * ===================================================================
8  *
9  * Unless otherwise specified, all software contained herein is licensed
10  * under the Apache License, Version 2.0 (the "License");
11  * you may not use this software except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *             http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * Unless otherwise specified, all documentation contained herein is licensed
23  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
24  * you may not use this documentation except in compliance with the License.
25  * You may obtain a copy of the License at
26  *
27  *             https://creativecommons.org/licenses/by/4.0/
28  *
29  * Unless required by applicable law or agreed to in writing, documentation
30  * distributed under the License is distributed on an "AS IS" BASIS,
31  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32  * See the License for the specific language governing permissions and
33  * limitations under the License.
34  *
35  * ============LICENSE_END============================================
36  *
37  * 
38  */
39
40
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertTrue;
43 import static org.mockito.Mockito.when;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpSession;
47
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.MockitoAnnotations;
52 import org.onap.portalsdk.analytics.error.RaptorException;
53 import org.onap.portalsdk.analytics.error.ReportSQLException;
54 import org.onap.portalsdk.analytics.model.definition.ReportDefinition;
55 import org.onap.portalsdk.analytics.model.runtime.ReportRuntime;
56 import org.onap.portalsdk.analytics.system.AppUtils;
57 import org.onap.portalsdk.analytics.system.Globals;
58 import org.onap.portalsdk.analytics.util.AppConstants;
59 import org.onap.portalsdk.analytics.xmlobj.MockitoTestSuite;
60 import org.powermock.api.mockito.PowerMockito;
61 import org.powermock.core.classloader.annotations.PrepareForTest;
62 import org.powermock.modules.junit4.PowerMockRunner;
63
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({Globals.class, AppUtils.class})
66 public class ErrorHandlerTest {
67
68         ErrorHandler errorHandler;
69         private String ERROR_MESSAGE = "testErrorMessage";
70         private String TEST_PAGE = "testPage";
71         private String TEST_ID = "testID";
72         private String REPORT_NAME = "testReport";
73         private String REPORT_SQL = "testReportSQL";
74         private String LOG_VAR_IN_SESSION = "test";
75         
76         @Before
77         public void setup() {
78                 errorHandler = new ErrorHandler();
79                 MockitoAnnotations.initMocks(this);
80         }
81
82         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
83         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
84         
85         @Test
86         public void processErrorTest() {
87                 when(mockedRequest.getAttribute(AppConstants.RI_ERROR_LIST)).thenReturn(null);
88                 errorHandler.processError(mockedRequest, ERROR_MESSAGE);
89         }
90         
91         @Test
92         public void processErrorRaptorExceptionTest() {
93                 when(mockedRequest.getAttribute(AppConstants.RI_ERROR_LIST)).thenReturn(null);
94                 RaptorException re = new RaptorException(ERROR_MESSAGE);
95                 errorHandler.processError(mockedRequest, re);
96         }
97         
98         @Test
99         public void processFatalErrorTest_WhenReportWrapperIsNotNull() throws Exception{
100                 PowerMockito.mockStatic(Globals.class);
101                 PowerMockito.mockStatic(AppUtils.class);
102                 HttpSession session = mockedRequest.getSession();
103                 when(Globals.getLogVariablesInSession()).thenReturn(LOG_VAR_IN_SESSION);
104                 ReportRuntime rr = PowerMockito.mock(ReportRuntime.class);
105                 when(rr.getReportID()).thenReturn(TEST_ID);
106                 when(rr.getReportName()).thenReturn(REPORT_NAME);
107                 when(session.getAttribute(AppConstants.SI_REPORT_RUNTIME)).thenReturn(rr);
108                 ReportSQLException rse = new ReportSQLException(ERROR_MESSAGE,REPORT_SQL);
109                 PowerMockito.doNothing().when(AppUtils.class, "processErrorNotification", new Object[]{mockedRequest, rse});
110                 when(AppUtils.getErrorPage()).thenReturn(TEST_PAGE);
111                 assertEquals(TEST_PAGE, errorHandler.processFatalError(mockedRequest, rse));
112         }
113         
114         @Test
115         public void processFatalErrorTest_WhenReportDefinitionIsNotNull() throws Exception{
116                 PowerMockito.mockStatic(Globals.class);
117                 PowerMockito.mockStatic(AppUtils.class);
118                 HttpSession session = mockedRequest.getSession();
119                 when(Globals.getLogVariablesInSession()).thenReturn(LOG_VAR_IN_SESSION);
120                 ReportDefinition rd = PowerMockito.mock(ReportDefinition.class);
121                 when(rd.getReportID()).thenReturn(TEST_ID);
122                 when(rd.getReportName()).thenReturn(REPORT_NAME);
123                 when(session.getAttribute(AppConstants.SI_REPORT_DEFINITION)).thenReturn(rd);
124                 ReportSQLException rse = new ReportSQLException(ERROR_MESSAGE,REPORT_SQL);
125                 PowerMockito.doNothing().when(AppUtils.class, "processErrorNotification", new Object[]{mockedRequest, rse});
126                 when(AppUtils.getErrorPage()).thenReturn(TEST_PAGE);
127                 assertEquals(TEST_PAGE, errorHandler.processFatalError(mockedRequest, rse));
128         }
129         
130         @Test
131         public void processFatalErrorJSONTest_WhenReportWrapperIsNotNull() {
132                 PowerMockito.mockStatic(Globals.class);
133                 PowerMockito.mockStatic(AppUtils.class);
134                 HttpSession session = mockedRequest.getSession();
135                 when(Globals.getLogVariablesInSession()).thenReturn(LOG_VAR_IN_SESSION);
136                 ReportRuntime rr = PowerMockito.mock(ReportRuntime.class);
137                 when(rr.getReportID()).thenReturn(TEST_ID);
138                 when(rr.getReportName()).thenReturn(REPORT_NAME);
139                 when(session.getAttribute(AppConstants.SI_REPORT_RUNTIME)).thenReturn(rr);
140                 ReportSQLException rse = new ReportSQLException(ERROR_MESSAGE,REPORT_SQL);
141                 String jsonString = errorHandler.processFatalErrorJSON(mockedRequest, rse);
142                 assertTrue(jsonString.contains(ERROR_MESSAGE));
143         }
144         
145         @Test
146         public void processFatalErrorJSONTest_WhenReportDefinitionIsNotNull() {
147                 PowerMockito.mockStatic(Globals.class);
148                 PowerMockito.mockStatic(AppUtils.class);
149                 HttpSession session = mockedRequest.getSession();
150                 when(Globals.getLogVariablesInSession()).thenReturn(LOG_VAR_IN_SESSION);
151                 ReportDefinition rd = PowerMockito.mock(ReportDefinition.class);
152                 when(rd.getReportID()).thenReturn(TEST_ID);
153                 when(rd.getReportName()).thenReturn(REPORT_NAME);
154                 when(session.getAttribute(AppConstants.SI_REPORT_DEFINITION)).thenReturn(rd);
155                 ReportSQLException rse = new ReportSQLException(ERROR_MESSAGE,REPORT_SQL);
156                 String jsonString = errorHandler.processFatalErrorJSON(mockedRequest, rse);
157                 assertTrue(jsonString.contains(ERROR_MESSAGE));
158         }
159         
160         @Test
161         public void processFatalErrorWMenuTest_WhenReportWrapperIsNotNull() throws Exception {
162                 PowerMockito.mockStatic(Globals.class);
163                 PowerMockito.mockStatic(AppUtils.class);
164                 when(Globals.getLogVariablesInSession()).thenReturn(LOG_VAR_IN_SESSION);
165                 ReportRuntime rr = PowerMockito.mock(ReportRuntime.class);
166                 when(rr.getReportID()).thenReturn(TEST_ID);
167                 when(rr.getReportName()).thenReturn(REPORT_NAME);
168                 HttpSession session = mockedRequest.getSession();
169                 when(session.getAttribute(AppConstants.SI_REPORT_RUNTIME)).thenReturn(rr);
170                 ReportSQLException rse = new ReportSQLException(ERROR_MESSAGE,REPORT_SQL);
171                 PowerMockito.doNothing().when(AppUtils.class, "processErrorNotification", new Object[]{mockedRequest, rse});
172                 when(AppUtils.getErrorPageWMenu()).thenReturn(TEST_PAGE);
173                 assertEquals(TEST_PAGE, errorHandler.processFatalErrorWMenu(mockedRequest, rse));
174         }
175         
176         @Test
177         public void processFatalErrorWMenuTest_WhenReportDefinitionIsNotNull() throws Exception {
178                 PowerMockito.mockStatic(Globals.class);
179                 PowerMockito.mockStatic(AppUtils.class);
180                 when(Globals.getLogVariablesInSession()).thenReturn(LOG_VAR_IN_SESSION);
181                 ReportDefinition rd = PowerMockito.mock(ReportDefinition.class);
182                 when(rd.getReportID()).thenReturn(TEST_ID);
183                 when(rd.getReportName()).thenReturn(REPORT_NAME);
184                 HttpSession session = mockedRequest.getSession();
185                 when(session.getAttribute(AppConstants.SI_REPORT_DEFINITION)).thenReturn(rd);
186                 ReportSQLException rse = new ReportSQLException(ERROR_MESSAGE,REPORT_SQL);
187                 PowerMockito.doNothing().when(AppUtils.class, "processErrorNotification", new Object[]{mockedRequest, rse});
188                 when(AppUtils.getErrorPageWMenu()).thenReturn(TEST_PAGE);
189                 assertEquals(TEST_PAGE, errorHandler.processFatalErrorWMenu(mockedRequest, rse));
190         }
191 }