Search in sources :

Example 1 with DatabaseEnvironmentUnit

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");
    }
}
Also used : ComponentEnvironment(com.axway.ats.agent.core.ComponentEnvironment) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) EnvironmentUnit(com.axway.ats.environment.EnvironmentUnit) ComponentRepository(com.axway.ats.agent.core.ComponentRepository) Properties(java.util.Properties) NoSuchComponentException(com.axway.ats.agent.core.exceptions.NoSuchComponentException) DbConnection(com.axway.ats.core.dbaccess.DbConnection)

Example 2 with DatabaseEnvironmentUnit

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);
}
Also used : ArrayList(java.util.ArrayList) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 3 with DatabaseEnvironmentUnit

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);
}
Also used : ArrayList(java.util.ArrayList) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 4 with DatabaseEnvironmentUnit

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);
}
Also used : ArrayList(java.util.ArrayList) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 5 with DatabaseEnvironmentUnit

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());
}
Also used : DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

DatabaseEnvironmentUnit (com.axway.ats.environment.database.DatabaseEnvironmentUnit)14 Test (org.junit.Test)11 Properties (java.util.Properties)6 ArrayList (java.util.ArrayList)5 DbTable (com.axway.ats.environment.database.model.DbTable)4 BaseTest (com.axway.ats.environment.BaseTest)3 EnvironmentUnit (com.axway.ats.environment.EnvironmentUnit)3 InputStream (java.io.InputStream)3 DbConnection (com.axway.ats.core.dbaccess.DbConnection)2 DirectoryEnvironmentUnit (com.axway.ats.environment.file.DirectoryEnvironmentUnit)2 FileEnvironmentUnit (com.axway.ats.environment.file.FileEnvironmentUnit)2 ComponentEnvironment (com.axway.ats.agent.core.ComponentEnvironment)1 ComponentRepository (com.axway.ats.agent.core.ComponentRepository)1 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 NoSuchComponentException (com.axway.ats.agent.core.exceptions.NoSuchComponentException)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1