ad1b4ea0f017661d5e6ddc23afbbc57f93365577
[appc.git] / appc-dispatcher / appc-dispatcher-common / appc-data-access-lib / src / test / java / org / onap / appc / dao / util / AppcDatabaseConnectionPoolTest.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  * Modifications Copyright (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  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dao.util;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.mockito.Mockito.spy;
30 import static org.mockito.Mockito.times;
31 import static org.powermock.api.mockito.PowerMockito.mock;
32 import static org.powermock.api.mockito.PowerMockito.mockStatic;
33 import static org.powermock.api.mockito.PowerMockito.when;
34 import java.sql.Connection;
35 import java.sql.SQLException;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.onap.appc.configuration.Configuration;
42 import org.onap.appc.configuration.ConfigurationFactory;
43 import org.onap.appc.dao.util.dbcp.DBConnectionPool;
44 import org.powermock.core.classloader.annotations.PowerMockIgnore;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import org.powermock.reflect.Whitebox;
48
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({ConfigurationFactory.class})
51 @PowerMockIgnore("javax.management.*")
52 public class AppcDatabaseConnectionPoolTest {
53     private String dbUrl = "jdbc:h2:mem:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1";
54     private String username = "sa";
55     private String password = "sa";
56     private String driver = "org.h2.Driver";
57
58     private Configuration configuration;
59
60     private DBConnectionPool dbConnectionPool;
61     private AppcDatabaseConnectionPool appcDatabaseConnectionPool;
62
63     @Before
64     public void setUp() throws Exception {
65         mockStatic(ConfigurationFactory.class);
66         when(ConfigurationFactory.getConfiguration()).thenReturn(configuration);
67         appcDatabaseConnectionPool = spy(new AppcDatabaseConnectionPool(dbUrl, username, password, driver));
68         dbConnectionPool = mock(DBConnectionPool.class);
69         Whitebox.setInternalState(appcDatabaseConnectionPool, "dbConnectionPool", dbConnectionPool);
70     }
71
72     @Test
73     public void testDBURL() {
74         String dbString;
75
76         dbString = PropertyPattern.DBURL.getPattern();
77         dbString = String.format(dbString, "test");
78         Assert.assertEquals("org.onap.appc.db.url.test", dbString);
79     }
80
81     @Test
82     public void testUSERNAME() {
83         String dbString;
84
85         dbString = PropertyPattern.USERNAME.getPattern();
86         dbString = String.format(dbString, "test");
87         Assert.assertEquals("org.onap.appc.db.user.test", dbString);
88     }
89
90     @Test
91     public void testPASSWORD() {
92         String dbString;
93
94         dbString = PropertyPattern.PASSWORD.getPattern();
95         dbString = String.format(dbString, "test");
96         Assert.assertEquals("org.onap.appc.db.pass.test", dbString);
97     }
98
99     @Test
100     public void testDRIVER() {
101         String dbString;
102
103         dbString = PropertyPattern.DRIVER.getPattern();
104         Assert.assertEquals("org.onap.appc.db.jdbc.driver", dbString);
105     }
106
107     @Test
108     public void testArgumentConstructor() {
109         AppcDatabaseConnectionPool appcDatabaseConnectionPool = new AppcDatabaseConnectionPool(dbUrl, username,
110             password, driver);
111         Object dbConnectionPool = Whitebox.getInternalState(appcDatabaseConnectionPool, "dbConnectionPool");
112         Assert.assertNotNull(dbConnectionPool);
113     }
114
115     @Test
116     public void testGetConnection() throws SQLException {
117         appcDatabaseConnectionPool.getConnection();
118         Mockito.verify(dbConnectionPool, times(1)).getConnection();
119     }
120
121     @Test
122     public void testDestroy() throws SQLException {
123         appcDatabaseConnectionPool.destroy();
124         Mockito.verify(dbConnectionPool, times(1)).shutdown();
125     }
126
127     @Test
128     public void testGetDataSourceStatus() {
129         appcDatabaseConnectionPool.getDataSourceStatus();
130         Mockito.verify(dbConnectionPool, times(1)).getDataSourceStatus();
131     }
132
133     @Test
134     public void testInit() throws SQLException {
135         Configuration mockConfiguration = Mockito.mock(Configuration.class);
136         when(ConfigurationFactory.getConfiguration()).thenReturn(mockConfiguration);
137         when(mockConfiguration.getProperty(Mockito.anyString(), Mockito.anyString())).thenReturn("");
138         DBConnectionPool mockDbConnectionPool = Mockito.mock(DBConnectionPool.class);
139         Connection mockConnection = Mockito.mock(Connection.class);
140         when(mockDbConnectionPool.getConnection()).thenReturn(mockConnection);
141         when(appcDatabaseConnectionPool.getDBConnectionPool("", "", "", "")).thenReturn(mockDbConnectionPool);
142         appcDatabaseConnectionPool.init();
143         Mockito.verify(mockConnection).close();
144     }
145
146     @Test
147     public void testSetDbName() {
148         AppcDatabaseConnectionPool pool = new AppcDatabaseConnectionPool();
149         pool.setDbName("TEST");
150         assertEquals("TEST", Whitebox.getInternalState(pool, "dbName"));
151     }
152 }