e6bba34e0aaabbad6d269effcfbdcb39789b7e0b
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.lockmanager.impl.sql;
24
25 import org.junit.Rule;
26 import org.junit.rules.TestName;
27 import org.openecomp.appc.dao.util.DefaultJdbcConnectionFactory;
28 import org.openecomp.appc.lockmanager.api.LockManager;
29 import org.openecomp.appc.lockmanager.api.LockManagerBaseTests;
30 import org.openecomp.appc.lockmanager.impl.sql.JdbcLockManager;
31 import org.openecomp.appc.lockmanager.impl.sql.MySqlConnectionFactory;
32
33 import java.sql.Connection;
34 import java.sql.PreparedStatement;
35 import java.sql.SQLException;
36
37 public abstract class MySqlLockManagerBaseTests extends LockManagerBaseTests {
38
39         private static final boolean USE_REAL_DB = Boolean.getBoolean("lockmanager.tests.useRealDb");
40         private static final String TABLE_LOCK_MANAGEMENT = "TEST_LOCK_MANAGEMENT";
41         private static final String JDBC_URL = System.getProperty("lockmanager.tests.jdbcUrl", "jdbc:mysql://192.168.1.2/test");
42         private static final String JDBC_USERNAME = System.getProperty("lockmanager.tests.jdbcUsername", "test");
43         private static final String JDBC_PASSWORD = System.getProperty("lockmanager.tests.jdbcPassword", "123456");
44
45         protected static final int CONCURRENT_TEST_WAIT_TIME = 10; // secs
46
47         @Rule
48         public TestName testName = new TestName();
49
50         @Override
51         protected LockManager createLockManager() {
52                 JdbcLockManager jdbcLockManager = createJdbcLockManager(USE_REAL_DB);
53                 DefaultJdbcConnectionFactory connectionFactory = new MySqlConnectionFactory();
54                 connectionFactory.setJdbcURL(JDBC_URL);
55                 connectionFactory.setJdbcUserName(JDBC_USERNAME);
56                 connectionFactory.setJdbcPassword(JDBC_PASSWORD);
57                 jdbcLockManager.setConnectionFactory(connectionFactory);
58                 jdbcLockManager.setTableName(TABLE_LOCK_MANAGEMENT);
59                 System.out.println("=> Running LockManager test [" + jdbcLockManager.getClass().getName() + "." + testName.getMethodName() + "]" + (USE_REAL_DB ? ". JDBC URL is [" + JDBC_URL + "]" : ""));
60                 clearTestLocks(jdbcLockManager);
61                 return jdbcLockManager;
62         }
63
64         protected abstract JdbcLockManager createJdbcLockManager(boolean useRealDb);
65
66         protected boolean setSynchronizer(Synchronizer synchronizer) {
67                 if(!(lockManager instanceof SynchronizerReceiver)) {
68                         System.err.println("Skipping concurrency test [" + testName.getMethodName() + "] for LockManager of type " + lockManager.getClass());
69                         return false;
70                 }
71                 ((SynchronizerReceiver)lockManager).setSynchronizer(synchronizer);
72                 return true;
73         }
74
75         private static final String SQL_DELETE_LOCK_RECORD = String.format("DELETE FROM %s WHERE RESOURCE_ID=?", TABLE_LOCK_MANAGEMENT);
76         private void clearTestLocks(JdbcLockManager jdbcLockManager) {
77                 Connection connection = jdbcLockManager.openDbConnection();
78                 if(connection == null) {
79                         return;
80                 }
81                 try {
82                         for(Resource resource: Resource.values()) {
83                                 try(PreparedStatement statement = connection.prepareStatement(SQL_DELETE_LOCK_RECORD)) {
84                                         statement.setString(1, resource.name());
85                                         statement.executeUpdate();
86                                 }
87                         }
88                 } catch(SQLException e) {
89                         throw new RuntimeException("Cannot clear test resources in table", e);
90                 } finally {
91                         jdbcLockManager.closeDbConnection(connection);
92                 }
93         }
94 }