557b0ace6b49bd40b6d132ed55047bf9a088d1c9
[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 package org.onap.portalsdk.analytics.scheduler;
39
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.math.BigDecimal;
46 import java.sql.Connection;
47 import java.sql.DatabaseMetaData;
48 import java.sql.PreparedStatement;
49 import java.sql.ResultSet;
50 import java.sql.SQLException;
51 import java.sql.Statement;
52 import java.util.ArrayList;
53 import java.util.Date;
54 import java.util.List;
55
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
58 import javax.sql.DataSource;
59
60 import org.junit.Before;
61 import org.junit.Test;
62 import org.junit.runner.RunWith;
63 import org.mockito.InjectMocks;
64 import org.mockito.Matchers;
65 import org.mockito.Mock;
66 import org.mockito.MockitoAnnotations;
67 import org.onap.portalsdk.analytics.error.ReportSQLException;
68 import org.onap.portalsdk.analytics.scheduler.SchedulerUtil.Executor;
69 import org.onap.portalsdk.analytics.system.AppUtils;
70 import org.onap.portalsdk.analytics.system.DbUtils;
71 import org.onap.portalsdk.analytics.system.Globals;
72 import org.onap.portalsdk.analytics.util.AppConstants;
73 import org.onap.portalsdk.analytics.xmlobj.MockitoTestSuite;
74 import org.powermock.api.mockito.PowerMockito;
75 import org.powermock.core.classloader.annotations.PrepareForTest;
76 import org.powermock.modules.junit4.PowerMockRunner;
77
78 @SuppressWarnings("static-access")
79 @RunWith(PowerMockRunner.class)
80 @PrepareForTest({ AppConstants.class, SchedulerUtil.class, Globals.class, AppUtils.class, DbUtils.class })
81 public class SchedulerUtilTest {
82
83         @InjectMocks
84         SchedulerUtil schedulerUtil;
85
86         @Mock
87         Connection conn;
88         
89         @Mock
90         DatabaseMetaData  dMData;
91
92
93         @Before
94         public void init() throws Exception {
95                 PowerMockito.mockStatic(Globals.class);
96                 PowerMockito.mockStatic(AppUtils.class);
97                 PowerMockito.mockStatic(DbUtils.class);
98                 DataSource ds = mock(DataSource.class);
99                 when(ds.getConnection()).thenReturn(conn);
100                 when(DbUtils.getConnection()).thenReturn(conn);
101                 MockitoAnnotations.initMocks(this);
102         }
103
104         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
105         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
106         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
107
108         @Test
109         public void insertOrUpdateTest() throws Exception {
110                 Statement stat = mock(Statement.class);
111                 ResultSet rset = mock(ResultSet.class);
112                 String sql = "select * 1";
113                 when(conn.createStatement()).thenReturn(stat);
114                 when(stat.executeQuery(sql)).thenReturn(rset);
115                 schedulerUtil.insertOrUpdate(sql);
116         }
117         
118         @Test
119         public void updateBinaryStreamTest() throws ReportSQLException, SQLException, IOException {
120                 PreparedStatement stat = mock(PreparedStatement.class);
121                 ResultSet rset = mock(ResultSet.class);
122                 String sql = "select * 1";
123                 InputStream is = mock(InputStream.class);
124                 BigDecimal id = BigDecimal.valueOf(10);
125                 when(conn.getMetaData()).thenReturn(dMData);
126                 when(dMData.getDatabaseProductName()).thenReturn("test");
127                 when(conn.prepareStatement(sql)).thenReturn(stat);
128                 when(stat.executeQuery()).thenReturn(rset);
129                 schedulerUtil.updateBinaryStream(sql, id, is, 1);
130         }
131         
132         @Test(expected = ReportSQLException.class)
133         public void updateBinaryStreamExceptionTest() throws ReportSQLException, SQLException, IOException {
134                 String sql = "select * 1";
135                 InputStream is = mock(InputStream.class);
136                 BigDecimal id = BigDecimal.valueOf(10);
137                 when(conn.getMetaData()).thenReturn(dMData);
138                 when(dMData.getDatabaseProductName()).thenReturn("oracle");
139                 schedulerUtil.updateBinaryStream(sql, id, is, 1);
140         }
141         
142         @Test
143         public void insertOrUpdateWithBigINTPreparedTest() throws ReportSQLException, SQLException {
144                 String sql = "select * 1";
145                 List<Object> params = new ArrayList<>();
146                 List<Integer> types = new ArrayList<>();
147                 PreparedStatement stat = mock(PreparedStatement.class);
148                 ResultSet rset = mock(ResultSet.class);
149                 when(conn.getMetaData()).thenReturn(dMData);
150                 when(conn.prepareStatement(sql)).thenReturn(stat);
151                 when(stat.executeQuery()).thenReturn(rset);
152                 params.add(BigDecimal.valueOf(2));
153                 types.add(-5);
154                 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
155         }
156         
157         @Test
158         public void insertOrUpdateWithPreparedTimestampTest() throws ReportSQLException, SQLException {
159                 String sql = "select * 1";
160                 List<Object> params = new ArrayList<>();
161                 List<Integer> types = new ArrayList<>();
162                 PreparedStatement stat = mock(PreparedStatement.class);
163                 ResultSet rset = mock(ResultSet.class);
164                 when(conn.getMetaData()).thenReturn(dMData);
165                 when(conn.prepareStatement(sql)).thenReturn(stat);
166                 when(stat.executeQuery()).thenReturn(rset);
167                 params.add(new java.sql.Timestamp(1));
168                 types.add(93);
169                 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
170         }
171         
172
173         @Test
174         public void insertOrUpdateWithPreparedDateTest() throws ReportSQLException, SQLException {
175                 String sql = "select * 1";
176                 List<Object> params = new ArrayList<>();
177                 List<Integer> types = new ArrayList<>();
178                 PreparedStatement stat = mock(PreparedStatement.class);
179                 ResultSet rset = mock(ResultSet.class);
180                 when(conn.getMetaData()).thenReturn(dMData);
181                 when(conn.prepareStatement(sql)).thenReturn(stat);
182                 when(stat.executeQuery()).thenReturn(rset);
183                 params.add(new java.sql.Date(1));
184                 types.add(91);
185                 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
186         }
187         
188         @Test
189         public void insertOrUpdateWithPreparedDoubleTest() throws ReportSQLException, SQLException {
190                 String sql = "select * 1";
191                 List<Object> params = new ArrayList<>();
192                 List<Integer> types = new ArrayList<>();
193                 PreparedStatement stat = mock(PreparedStatement.class);
194                 ResultSet rset = mock(ResultSet.class);
195                 when(conn.getMetaData()).thenReturn(dMData);
196                 when(conn.prepareStatement(sql)).thenReturn(stat);
197                 when(stat.executeQuery()).thenReturn(rset);
198                 params.add(Double.valueOf(12));
199                 types.add(8);
200                 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
201         }
202         
203         @Test
204         public void insertOrUpdateWithPreparedIntegerTest() throws ReportSQLException, SQLException {
205                 String sql = "select * 1";
206                 List<Object> params = new ArrayList<>();
207                 List<Integer> types = new ArrayList<>();
208                 PreparedStatement stat = mock(PreparedStatement.class);
209                 ResultSet rset = mock(ResultSet.class);
210                 when(conn.getMetaData()).thenReturn(dMData);
211                 when(conn.prepareStatement(sql)).thenReturn(stat);
212                 when(stat.executeQuery()).thenReturn(rset);
213                 params.add(2);
214                 types.add(4);
215                 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
216         }
217         
218         @Test
219         public void insertOrUpdateWithPreparedNumericTest() throws ReportSQLException, SQLException {
220                 String sql = "select * 1";
221                 List<Object> params = new ArrayList<>();
222                 List<Integer> types = new ArrayList<>();
223                 PreparedStatement stat = mock(PreparedStatement.class);
224                 ResultSet rset = mock(ResultSet.class);
225                 when(conn.getMetaData()).thenReturn(dMData);
226                 when(conn.prepareStatement(sql)).thenReturn(stat);
227                 when(stat.executeQuery()).thenReturn(rset);
228                 params.add(Long.valueOf(1));
229                 types.add(2);
230                 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
231         }
232         
233         
234         @Test
235         public void insertOrUpdateWithPreparedVarcharTest() throws ReportSQLException, SQLException {
236                 String sql = "select * 1";
237                 List<Object> params = new ArrayList<>();
238                 List<Integer> types = new ArrayList<>();
239                 PreparedStatement stat = mock(PreparedStatement.class);
240                 ResultSet rset = mock(ResultSet.class);
241                 when(conn.getMetaData()).thenReturn(dMData);
242                 when(conn.prepareStatement(sql)).thenReturn(stat);
243                 when(stat.executeQuery()).thenReturn(rset);
244                 params.add("test");
245                 types.add(12);
246                 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
247         }
248         
249         @Test
250         public void getSingleResultTest() throws ReportSQLException, SQLException {
251                 Statement stat = mock(Statement.class);
252                 ResultSet rset = mock(ResultSet.class);
253                 String sql = "select * 1";
254                 rset.setFetchSize(1);
255                 when(conn.createStatement()).thenReturn(stat);
256                 when(stat.executeQuery(sql)).thenReturn(rset);
257                 Object obj = "test";
258                 when(rset.getObject(Matchers.anyString())).thenReturn(obj);
259                 schedulerUtil.getSingleResult(sql, "test");
260         }
261         
262         @Test(expected = NullPointerException.class)
263         public void getDBStreamTest() throws ReportSQLException, SQLException, IOException {
264                 PreparedStatement stat = mock(PreparedStatement.class);
265                 ResultSet rset = mock(ResultSet.class);
266                 String sql = "select * 1";
267                 when(conn.getMetaData()).thenReturn(dMData);
268                 when(dMData.getDatabaseProductName()).thenReturn("test");
269                 when(conn.createStatement()).thenReturn(stat);
270                 when(stat.executeQuery()).thenReturn(rset);
271                 schedulerUtil.getDBStream(sql, "test");
272         }
273         
274         @Test
275         public void getAndExecuteTest() throws ReportSQLException, SQLException {
276                 Statement stat = mock(Statement.class);
277                 ResultSet rset = mock(ResultSet.class);
278                 String sql = "select * 1";
279                 rset.setFetchSize(1);
280                 when(conn.createStatement()).thenReturn(stat);
281                 when(stat.executeQuery(sql)).thenReturn(rset);
282                 Executor exe = mock(Executor.class);
283                 schedulerUtil.getAndExecute(sql, exe);
284         }
285         
286         @Test
287         public void trunc_hourTest() {
288                 schedulerUtil.trunc_hour(new Date());
289         }
290         
291         @Test
292         public void add_hoursTest() {
293                 schedulerUtil.add_hours(new Date(), 1);
294         }
295         
296         @Test
297         public void add_monthsTest() {
298                 schedulerUtil.add_months(new Date(), 1);
299         }
300         
301         @Test
302         public void add_daysTest() {
303                 schedulerUtil.add_days(new Date(), 1);
304         }
305         
306         @Test
307         public void to_dateTest() {
308                 schedulerUtil.to_date("/", "1/1/1");
309         }
310         
311         @Test
312         public void to_date_strTest() {
313                 schedulerUtil.to_date_str(new Date(), "");
314         }
315         
316         @Test
317         public void cr_dissecturlTest() {
318                 schedulerUtil.cr_dissecturl("test&123", "1");
319         }
320 }