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.openecomp.appc.lockmanager.impl.sql;
27 import org.junit.Rule;
28 import org.junit.rules.TestName;
29 import org.openecomp.appc.dao.util.DefaultJdbcConnectionFactory;
30 import org.openecomp.appc.lockmanager.api.LockManager;
31 import org.openecomp.appc.lockmanager.api.LockManagerBaseTests;
32 import org.openecomp.appc.lockmanager.impl.sql.JdbcLockManager;
33 import org.openecomp.appc.lockmanager.impl.sql.MySqlConnectionFactory;
35 import java.sql.Connection;
36 import java.sql.PreparedStatement;
37 import java.sql.SQLException;
39 public abstract class MySqlLockManagerBaseTests extends LockManagerBaseTests {
41 private static final boolean USE_REAL_DB = Boolean.getBoolean("lockmanager.tests.useRealDb");
42 private static final String TABLE_LOCK_MANAGEMENT = "TEST_LOCK_MANAGEMENT";
43 private static final String JDBC_URL = System.getProperty("lockmanager.tests.jdbcUrl", "jdbc:mysql://192.168.1.2/test");
44 private static final String JDBC_USERNAME = System.getProperty("lockmanager.tests.jdbcUsername", "test");
45 private static final String JDBC_PASSWORD = System.getProperty("lockmanager.tests.jdbcPassword", "123456");
47 protected static final int CONCURRENT_TEST_WAIT_TIME = 10; // secs
50 public TestName testName = new TestName();
53 protected LockManager createLockManager() {
54 JdbcLockManager jdbcLockManager = createJdbcLockManager(USE_REAL_DB);
55 DefaultJdbcConnectionFactory connectionFactory = new MySqlConnectionFactory();
56 connectionFactory.setJdbcURL(JDBC_URL);
57 connectionFactory.setJdbcUserName(JDBC_USERNAME);
58 connectionFactory.setJdbcPassword(JDBC_PASSWORD);
59 jdbcLockManager.setConnectionFactory(connectionFactory);
60 jdbcLockManager.setTableName(TABLE_LOCK_MANAGEMENT);
61 System.out.println("=> Running LockManager test [" + jdbcLockManager.getClass().getName() + "." + testName.getMethodName() + "]" + (USE_REAL_DB ? ". JDBC URL is [" + JDBC_URL + "]" : ""));
62 clearTestLocks(jdbcLockManager);
63 return jdbcLockManager;
66 protected abstract JdbcLockManager createJdbcLockManager(boolean useRealDb);
68 protected boolean setSynchronizer(Synchronizer synchronizer) {
69 if(!(lockManager instanceof SynchronizerReceiver)) {
70 System.err.println("Skipping concurrency test [" + testName.getMethodName() + "] for LockManager of type " + lockManager.getClass());
73 ((SynchronizerReceiver)lockManager).setSynchronizer(synchronizer);
77 private static final String SQL_DELETE_LOCK_RECORD = String.format("DELETE FROM %s WHERE RESOURCE_ID=?", TABLE_LOCK_MANAGEMENT);
78 private void clearTestLocks(JdbcLockManager jdbcLockManager) {
79 Connection connection = jdbcLockManager.openDbConnection();
80 if(connection == null) {
84 for(Resource resource: Resource.values()) {
85 try(PreparedStatement statement = connection.prepareStatement(SQL_DELETE_LOCK_RECORD)) {
86 statement.setString(1, resource.name());
87 statement.executeUpdate();
90 } catch(SQLException e) {
91 throw new RuntimeException("Cannot clear test resources in table", e);
93 jdbcLockManager.closeDbConnection(connection);