2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.analytics.scheduler;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
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;
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
58 import javax.sql.DataSource;
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;
78 @SuppressWarnings("static-access")
79 @RunWith(PowerMockRunner.class)
80 @PrepareForTest({ AppConstants.class, SchedulerUtil.class, Globals.class, AppUtils.class, DbUtils.class })
81 public class SchedulerUtilTest {
84 SchedulerUtil schedulerUtil;
90 DatabaseMetaData dMData;
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);
104 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
105 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
106 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
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);
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);
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);
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));
154 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
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));
169 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
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));
185 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
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));
200 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
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);
215 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
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));
230 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
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);
246 schedulerUtil.insertOrUpdateWithPrepared(sql, params, types);
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);
258 when(rset.getObject(Matchers.anyString())).thenReturn(obj);
259 schedulerUtil.getSingleResult(sql, "test");
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");
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);
287 public void trunc_hourTest() {
288 schedulerUtil.trunc_hour(new Date());
292 public void add_hoursTest() {
293 schedulerUtil.add_hours(new Date(), 1);
297 public void add_monthsTest() {
298 schedulerUtil.add_months(new Date(), 1);
302 public void add_daysTest() {
303 schedulerUtil.add_days(new Date(), 1);
307 public void to_dateTest() {
308 schedulerUtil.to_date("/", "1/1/1");
312 public void to_date_strTest() {
313 schedulerUtil.to_date_str(new Date(), "");
317 public void cr_dissecturlTest() {
318 schedulerUtil.cr_dissecturl("test&123", "1");