Update license header in appc-dispatcher files
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.dao.util.dbcp;
25
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
32 import org.powermock.core.classloader.annotations.PowerMockIgnore;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35
36 import java.sql.Connection;
37 import java.sql.SQLException;
38 import java.util.Map;
39
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest({DBConnectionPool.class})
42 @PowerMockIgnore("javax.management.*")
43 public class DBConnectionPoolTest {
44     private final String connectURI = "jdbc:h2:mem:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1";
45     private final String username = "sa";
46     private final String password = "sa";
47     private final String driverClass = "org.h2.Driver";
48
49     private DBConnectionPool dbcp;
50     private DBConnectionPool dbcp2;
51     private Connection connection;
52
53     @Before
54     public void setUp() throws Exception {
55         dbcp = new DBConnectionPool(connectURI, username, password, driverClass);
56         dbcp2 = new DBConnectionPool(connectURI, username, password, driverClass);
57     }
58
59     @Test
60     public void testGetConnection() {
61         try {
62             connection = dbcp.getConnection();
63         } catch (DBConnectionPoolException e) {
64             Assert.fail(e.getMessage());
65         }
66         Assert.assertNotNull(connection);
67     }
68
69     @Test
70     public void testGetDataSourceStatus() {
71         Map<String, Integer> dataSourceStatus = dbcp.getDataSourceStatus();
72         Assert.assertNotNull(dataSourceStatus);
73     }
74
75     @Test(expected = DBConnectionPoolException.class)
76     public void testShutdown() throws DBConnectionPoolException {
77         dbcp2.shutdown();
78         connection = dbcp2.getConnection();
79         Assert.assertNull(connection);
80     }
81
82     @After
83     public void clean() {
84         if (dbcp != null) {
85             dbcp.shutdown();
86         }
87         if (dbcp2 != null) {
88             dbcp2.shutdown();
89         }
90         if (connection != null) {
91             try {
92                 connection.close();
93             } catch (SQLException e) {
94                 e.printStackTrace();
95             }
96         }
97     }
98 }