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