2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dao.util;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.onap.appc.configuration.Configuration;
34 import org.onap.appc.configuration.ConfigurationFactory;
35 import org.onap.appc.dao.util.dbcp.DBConnectionPool;
36 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PowerMockIgnore;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import org.powermock.reflect.Whitebox;
43 import java.sql.Connection;
44 import java.sql.SQLException;
47 import static org.mockito.Matchers.any;
48 import static org.mockito.Matchers.anyObject;
49 import static org.mockito.Matchers.anyString;
50 import static org.mockito.Mockito.doNothing;
51 import static org.mockito.Mockito.spy;
52 import static org.mockito.Mockito.times;
53 import static org.powermock.api.mockito.PowerMockito.doReturn;
54 import static org.powermock.api.mockito.PowerMockito.mock;
55 import static org.powermock.api.mockito.PowerMockito.mockStatic;
56 import static org.powermock.api.mockito.PowerMockito.when;
57 import static org.powermock.api.support.membermodification.MemberMatcher.method;
59 @RunWith(PowerMockRunner.class)
60 @PrepareForTest({ConfigurationFactory.class})
61 @PowerMockIgnore("javax.management.*")
62 public class AppcDatabaseConnectionPoolTest {
63 private String dbName = "dbName";
64 private String dbUrl = "jdbc:h2:mem:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1";
65 private String username = "sa";
66 private String password = "sa";
67 private String driver = "org.h2.Driver";
69 private Configuration configuration;
71 private DBConnectionPool dbConnectionPool;
72 private AppcDatabaseConnectionPool appcDatabaseConnectionPool;
75 public void setUp() throws Exception {
76 mockStatic(ConfigurationFactory.class);
77 when(ConfigurationFactory.getConfiguration()).thenReturn(configuration);
78 appcDatabaseConnectionPool = spy(new AppcDatabaseConnectionPool(dbUrl, username, password, driver));
79 dbConnectionPool = mock(DBConnectionPool.class);
80 Whitebox.setInternalState(appcDatabaseConnectionPool, "dbConnectionPool", dbConnectionPool);
84 public void testArgumentConstructor() {
85 AppcDatabaseConnectionPool appcDatabaseConnectionPool = new AppcDatabaseConnectionPool(dbUrl, username,
87 Object dbConnectionPool = Whitebox.getInternalState(appcDatabaseConnectionPool, "dbConnectionPool");
88 Assert.assertNotNull(dbConnectionPool);
92 public void testGetConnection() throws SQLException {
93 final Connection connection = appcDatabaseConnectionPool.getConnection();
94 Mockito.verify(dbConnectionPool, times(1)).getConnection();
98 public void testDestroy() throws SQLException {
99 appcDatabaseConnectionPool.destroy();
100 Mockito.verify(dbConnectionPool, times(1)).shutdown();
104 public void testGetDataSourceStatus() {
105 Map<String, Integer> dataSourceStatus = appcDatabaseConnectionPool.getDataSourceStatus();
106 Mockito.verify(dbConnectionPool, times(1)).getDataSourceStatus();