use of com.axway.ats.environment.database.DatabaseEnvironmentUnit in project ats-framework by Axway.
the class EnvironmentConfigurator method apply.
@Override
public void apply() throws ConfigurationException {
for (Properties dbProperties : dbPropertiesList) {
int dbConfigurationIndex = -1;
if (dbProperties.get(DB_CONFIGURATION_INDEX) != null) {
dbConfigurationIndex = (Integer) dbProperties.get(DB_CONFIGURATION_INDEX);
}
ComponentRepository componentRepository = ComponentRepository.getInstance();
ComponentEnvironment componentEnvironment;
try {
componentEnvironment = componentRepository.getComponentEnvironment(component);
} catch (NoSuchComponentException nsce) {
throw new ConfigurationException("Error changing environment configuration. CAUSE: " + nsce.getMessage());
}
int foundDbConfigurationIndex = 0;
for (EnvironmentUnit environmentUnit : componentEnvironment.getEnvironmentUnits()) {
if (environmentUnit instanceof DatabaseEnvironmentUnit) {
if (foundDbConfigurationIndex == dbConfigurationIndex) {
DatabaseEnvironmentUnit dbEnvironmentUnit = (DatabaseEnvironmentUnit) environmentUnit;
DbConnection dbConnection = dbEnvironmentUnit.getDbConnection();
// the database port is kept in a list of custom properties
Map<String, Object> customProperties = dbConnection.getCustomProperties();
// get the new configuration properties
String newDbHost = chooseNewProperty(dbProperties.getProperty(DB_HOST), dbConnection.getHost());
String newDbName = chooseNewProperty(dbProperties.getProperty(DB_NAME), dbConnection.getDb());
String newDbUserName = chooseNewProperty(dbProperties.getProperty(DB_USER_NAME), dbConnection.getUser());
String newDbUserPassword = chooseNewProperty(dbProperties.getProperty(DB_USER_PASSWORD), dbConnection.getPassword());
Object newDbPort = chooseDbPort(dbProperties.get(DB_PORT), customProperties.get(DbKeys.PORT_KEY));
// create a new connection object
customProperties.put(DbKeys.PORT_KEY, newDbPort);
DbConnection newDbConnection = DatabaseProviderFactory.createDbConnection(dbConnection.getDbType(), newDbHost, newDbName, newDbUserName, newDbUserPassword, customProperties);
// apply the changes
dbEnvironmentUnit.setDbConnection(newDbConnection);
log.info("Database configuration for index " + dbConfigurationIndex + " is changed. DbConnection: " + newDbConnection.getDescription());
return;
} else {
// still searching the exact database configuration
foundDbConfigurationIndex++;
}
}
}
throw new ConfigurationException("Database configuration with index " + dbConfigurationIndex + " is not available");
}
}
use of com.axway.ats.environment.database.DatabaseEnvironmentUnit in project ats-framework by Axway.
the class Test_DatabaseEnvironmentUnit method createBackupMysql.
@Test
public void createBackupMysql() throws EnvironmentCleanupException, IOException {
DbTable table1 = new DbTable("table1");
DbTable table2 = new DbTable("table2");
List<DbTable> tables = new ArrayList<DbTable>();
tables.add(table1);
tables.add(table2);
expect(mockFactory.createDbBackupHandler(mockDbConnection)).andReturn(mockBackupHandler);
mockBackupHandler.setLockTables(true);
mockBackupHandler.setForeignKeyCheck(true);
mockBackupHandler.setIncludeDeleteStatements(true);
mockBackupHandler.addTable(table1);
mockBackupHandler.addTable(table2);
mockBackupHandler.createBackup(tempFile.getCanonicalPath());
mockBackupHandler.disconnect();
replay(mockFactory);
replay(mockBackupHandler);
DatabaseEnvironmentUnit dbEnvironmentUnit = new DatabaseEnvironmentUnit(tempFile.getParentFile().getCanonicalPath(), tempFile.getName(), mockDbConnection, tables, mockFactory);
dbEnvironmentUnit.backup();
verify(mockFactory);
verify(mockBackupHandler);
}
use of com.axway.ats.environment.database.DatabaseEnvironmentUnit in project ats-framework by Axway.
the class Test_DatabaseEnvironmentUnit method executeRestore.
@Test
public void executeRestore() throws EnvironmentCleanupException, IOException {
//temp file is created and it exists, so backup will not be triggered
DbTable table1 = new DbTable("table1");
DbTable table2 = new DbTable("table2");
List<DbTable> tables = new ArrayList<DbTable>();
tables.add(table1);
tables.add(table2);
expect(mockFactory.createDbRestoreHandler(mockDbConnection)).andReturn(mockRestoreHandler);
mockRestoreHandler.restore(tempFile.getCanonicalPath());
mockRestoreHandler.disconnect();
replay(mockFactory);
replay(mockBackupHandler);
DatabaseEnvironmentUnit dbEnvironmentUnit = new DatabaseEnvironmentUnit(tempFile.getParentFile().getCanonicalPath(), tempFile.getName(), mockDbConnection, tables, mockFactory);
dbEnvironmentUnit.executeRestoreIfNecessary();
verify(mockFactory);
verify(mockBackupHandler);
}
use of com.axway.ats.environment.database.DatabaseEnvironmentUnit in project ats-framework by Axway.
the class Test_DatabaseEnvironmentUnit method getDescription.
@Test
public void getDescription() throws EnvironmentCleanupException, IOException {
//temp file is created and it exists, so backup will not be triggered
DbTable table1 = new DbTable("table1");
DbTable table2 = new DbTable("table2");
List<DbTable> tables = new ArrayList<DbTable>();
tables.add(table1);
tables.add(table2);
expect(mockDbConnection.getDbType()).andReturn(DbConnMySQL.DATABASE_TYPE);
replay(mockDbConnection);
DatabaseEnvironmentUnit dbEnvironmentUnit = new DatabaseEnvironmentUnit(tempFile.getParentFile().getCanonicalPath(), tempFile.getName(), mockDbConnection, tables, mockFactory);
assertEquals("MYSQL database in file " + tempFile.getCanonicalPath(), dbEnvironmentUnit.getDescription());
verify(mockDbConnection);
}
use of com.axway.ats.environment.database.DatabaseEnvironmentUnit in project ats-framework by Axway.
the class MockDbEnvironmentUnit method testChangingDbUsername.
@Test
public void testChangingDbUsername() throws ConfigurationException, ComponentAlreadyDefinedException {
Properties properties = new Properties();
properties.put(EnvironmentConfigurator.DB_CONFIGURATION_INDEX, 0);
properties.put(EnvironmentConfigurator.DB_USER_NAME, "new_test_username");
EnvironmentConfigurator environmentConfigurator = new EnvironmentConfigurator(COMPONENT_NAME, Arrays.asList(properties));
environmentConfigurator.apply();
DatabaseEnvironmentUnit dbEnvUnit = (DatabaseEnvironmentUnit) testComponent.getEnvironments().get(0).getEnvironmentUnits().get(0);
assertEquals("new_test_username", dbEnvUnit.getDbConnection().getUser());
}
Aggregations