Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dispatcher / appc-dispatcher-common / appc-data-access-lib / src / test / java / org / onap / appc / dao / util / dbcp / DBConnectionPoolTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dao.util.dbcp;
26
27 import org.apache.commons.dbcp2.BasicDataSource;
28 import org.junit.After;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.mockito.Mockito;
35 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
36 import org.powermock.reflect.Whitebox;
37 import java.sql.Connection;
38 import java.sql.SQLException;
39 import java.util.Map;
40
41 public class DBConnectionPoolTest {
42     private final String connectURI = "jdbc:h2:mem:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1";
43     private final String username = "sa";
44     private final String password = "sa";
45     private final String driverClass = "org.h2.Driver";
46
47     private DBConnectionPool dbcp;
48     private DBConnectionPool dbcp2;
49     private Connection connection;
50
51     @Rule
52     public ExpectedException expectedEx = ExpectedException.none();
53
54     @Before
55     public void setUp() throws Exception {
56         dbcp = new DBConnectionPool(connectURI, username, password, driverClass);
57         dbcp2 = new DBConnectionPool(connectURI, username, password, driverClass);
58     }
59
60     @Test
61     public void testGetConnection() {
62         try {
63             connection = dbcp.getConnection();
64         } catch (DBConnectionPoolException e) {
65             Assert.fail(e.getMessage());
66         }
67         Assert.assertNotNull(connection);
68     }
69
70     @Test
71     public void testGetConnectionSQLExceptionFlow() throws SQLException {
72         DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass));
73         BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class);
74         Mockito.when(mockDataSource.getConnection()).thenThrow(new SQLException());
75         Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource);
76         expectedEx.expect(SQLException.class);
77         connection = dbcpSpy.getConnection();
78     }
79
80     @Test
81     public void testGetConnectionDBConnectionPoolExceptionFlow() throws SQLException {
82         DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass));
83         BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class);
84         Mockito.when(mockDataSource.getConnection()).thenReturn(null);
85         Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource);
86         expectedEx.expect(DBConnectionPoolException.class);
87         connection = dbcpSpy.getConnection();
88     }
89
90     @Test
91     public void testGetDataSourceStatus() {
92         Map<String, Integer> dataSourceStatus = dbcp.getDataSourceStatus();
93         Assert.assertNotNull(dataSourceStatus);
94     }
95
96     @Test(expected = DBConnectionPoolException.class)
97     public void testShutdown() throws DBConnectionPoolException {
98         dbcp2.shutdown();
99         connection = dbcp2.getConnection();
100         Assert.assertNull(connection);
101     }
102
103     @Test
104     public void testShutdownException() throws SQLException {
105         DBConnectionPool dbcpSpy =
106                 Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass, 0, 0, 0, 0, 0));
107         BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class);
108         Mockito.doThrow(new SQLException()).when(mockDataSource).close();
109         Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource);
110         dbcpSpy.shutdown();
111         Mockito.verify(mockDataSource).close();
112     }
113
114     @After
115     public void clean() {
116         if (dbcp != null) {
117             dbcp.shutdown();
118         }
119         if (dbcp2 != null) {
120             dbcp2.shutdown();
121         }
122         if (connection != null) {
123             try {
124                 connection.close();
125             } catch (SQLException e) {
126                 e.printStackTrace();
127             }
128         }
129     }
130 }