904e48f325d11b29c7e44d03aeffaf1d1a0a040b
[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.sql.Connection;
44 import java.sql.DatabaseMetaData;
45 import java.sql.ResultSet;
46 import java.sql.Statement;
47
48 import javax.sql.DataSource;
49
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.Matchers;
54 import org.mockito.Mock;
55 import org.mockito.MockitoAnnotations;
56 import org.mockito.invocation.InvocationOnMock;
57 import org.mockito.stubbing.Answer;
58 import org.onap.portalsdk.analytics.error.RaptorException;
59 import org.onap.portalsdk.analytics.scheduler.SchedulerUtil.Executor;
60 import org.onap.portalsdk.analytics.system.DbUtils;
61 import org.onap.portalsdk.analytics.system.Globals;
62 import org.onap.portalsdk.analytics.util.AppConstants;
63 import org.onap.portalsdk.core.web.support.AppUtils;
64 import org.powermock.api.mockito.PowerMockito;
65 import org.powermock.core.classloader.annotations.PrepareForTest;
66 import org.powermock.modules.junit4.PowerMockRunner;
67
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest({ AppConstants.class, SendNotifications.class, SchedulerUtil.class, SendEmail.class, Globals.class, AppUtils.class,
70                 DbUtils.class })
71 public class SendNotificationsTest {
72
73         @Mock
74         SchedulerUtil schedulerUtil;
75         
76         @Mock
77         SendEmail sendEmail;
78
79         @Mock
80         Connection conn;
81
82         @Mock
83         DatabaseMetaData dMData;
84         
85         @Mock
86         Executor executor;
87
88         @Before
89         public void init() throws Exception {
90                 PowerMockito.mockStatic(Globals.class);
91                 PowerMockito.mockStatic(AppUtils.class);
92                 PowerMockito.mockStatic(DbUtils.class);
93                 DataSource ds = mock(DataSource.class);
94                 when(ds.getConnection()).thenReturn(conn);
95                 when(DbUtils.getConnection()).thenReturn(conn);
96                 MockitoAnnotations.initMocks(this);
97                 PowerMockito.whenNew(SchedulerUtil.class).withNoArguments().thenReturn(schedulerUtil);
98                 PowerMockito.doNothing().when(sendEmail).setSchedulerUtil(schedulerUtil);
99         }
100         
101
102         @Test
103         public void send_notificationTest() throws RaptorException, Exception {
104                 SendNotifications sendNotifications = new SendNotifications();
105                 Statement stat = mock(Statement.class);
106                 ResultSet rset = mock(ResultSet.class);
107                 when(Globals.getSchedulerInterval()).thenReturn(2147483647);
108                 when(Globals.getAvailableSchedules()).thenReturn("test");
109                 when(Globals.getCurrentDateString()).thenReturn("test");
110                 when(schedulerUtil.getConnection()).thenReturn(conn);
111                 when(conn.createStatement()).thenReturn(stat);
112                 when(rset.getInt(Matchers.anyInt())).thenReturn(10);
113                 when(rset.next()).thenReturn(true).thenReturn(false);
114                 when(rset.getTimestamp("run_date")).thenReturn(new java.sql.Timestamp(0));
115                 when(stat.executeQuery(Matchers.anyString())).thenReturn(rset);
116                 sendNotifications.send_notification("test", "test", "test", "test", 30);
117         }
118 }