2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.dao.util;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.onap.appc.configuration.Configuration;
33 import org.onap.appc.configuration.ConfigurationFactory;
34 import org.onap.appc.dao.util.dbcp.DBConnectionPool;
35 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PowerMockIgnore;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40 import org.powermock.reflect.Whitebox;
42 import java.sql.Connection;
43 import java.sql.SQLException;
46 import static org.mockito.Matchers.any;
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Matchers.anyString;
49 import static org.mockito.Mockito.doNothing;
50 import static org.mockito.Mockito.spy;
51 import static org.mockito.Mockito.times;
52 import static org.powermock.api.mockito.PowerMockito.doReturn;
53 import static org.powermock.api.mockito.PowerMockito.mock;
54 import static org.powermock.api.mockito.PowerMockito.mockStatic;
55 import static org.powermock.api.mockito.PowerMockito.when;
56 import static org.powermock.api.support.membermodification.MemberMatcher.method;
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({ConfigurationFactory.class})
60 @PowerMockIgnore("javax.management.*")
61 public class AppcDatabaseConnectionPoolTest {
62 private String dbName = "dbName";
63 private String dbUrl = "jdbc:h2:mem:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1";
64 private String username = "sa";
65 private String password = "sa";
66 private String driver = "org.h2.Driver";
68 private Configuration configuration;
70 private DBConnectionPool dbConnectionPool;
71 private AppcDatabaseConnectionPool appcDatabaseConnectionPool;
74 public void setUp() throws Exception {
75 mockStatic(ConfigurationFactory.class);
76 when(ConfigurationFactory.getConfiguration()).thenReturn(configuration);
77 appcDatabaseConnectionPool = spy(new AppcDatabaseConnectionPool(dbUrl, username, password, driver));
78 dbConnectionPool = mock(DBConnectionPool.class);
79 Whitebox.setInternalState(appcDatabaseConnectionPool, "dbConnectionPool", dbConnectionPool);
83 public void testDBURL() {
86 dbString = PropertyPattern.DBURL.getPattern();
87 dbString = String.format(dbString, "test");
88 Assert.assertEquals("org.onap.appc.db.url.test", dbString);
92 public void testUSERNAME() {
95 dbString = PropertyPattern.USERNAME.getPattern();
96 dbString = String.format(dbString, "test");
97 Assert.assertEquals("org.onap.appc.db.user.test", dbString);
101 public void testPASSWORD() {
104 dbString = PropertyPattern.PASSWORD.getPattern();
105 dbString = String.format(dbString, "test");
106 Assert.assertEquals("org.onap.appc.db.pass.test", dbString);
110 public void testDRIVER() {
113 dbString = PropertyPattern.DRIVER.getPattern();
114 Assert.assertEquals("org.onap.appc.db.jdbc.driver", dbString);
118 public void testArgumentConstructor() {
119 AppcDatabaseConnectionPool appcDatabaseConnectionPool = new AppcDatabaseConnectionPool(dbUrl, username,
121 Object dbConnectionPool = Whitebox.getInternalState(appcDatabaseConnectionPool, "dbConnectionPool");
122 Assert.assertNotNull(dbConnectionPool);
126 public void testGetConnection() throws SQLException {
127 final Connection connection = appcDatabaseConnectionPool.getConnection();
128 Mockito.verify(dbConnectionPool, times(1)).getConnection();
132 public void testDestroy() throws SQLException {
133 appcDatabaseConnectionPool.destroy();
134 Mockito.verify(dbConnectionPool, times(1)).shutdown();
138 public void testGetDataSourceStatus() {
139 Map<String, Integer> dataSourceStatus = appcDatabaseConnectionPool.getDataSourceStatus();
140 Mockito.verify(dbConnectionPool, times(1)).getDataSourceStatus();