Search in sources :

Example 1 with OracleEnvironmentHandler

use of com.axway.ats.environment.database.OracleEnvironmentHandler in project ats-framework by Axway.

the class Test_OracleEnvironmentHandler method createBackupNoForeignKeysNoLockNoDelete.

@Test
public void createBackupNoForeignKeysNoLockNoDelete() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException {
    DbTable table1 = new DbTable("table1");
    DbTable table2 = new DbTable("table2");
    //the columns meta data
    DbRecordValuesList column1MetaData = new DbRecordValuesList();
    column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
    column1MetaData.add(new DbRecordValue("", "DATA_TYPE", "varchar(32)"));
    column1MetaData.add(new DbRecordValue("", "DATA_DEFAULT", true));
    DbRecordValuesList[] columnsMetaData = new DbRecordValuesList[] { column1MetaData };
    DbRecordValuesList record1Value = new DbRecordValuesList();
    record1Value.add(new DbRecordValue("table1", "name1", "value1"));
    DbRecordValuesList[] recordValues = new DbRecordValuesList[] { record1Value };
    OracleEnvironmentHandler envHandler = new OracleEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
    expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
    expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues);
    expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
    expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues);
    //expect the file writer calls
    //table1
    mockFileWriter.write("INSERT INTO table1(name1) VALUES ('value1');" + EOL_MARKER + LINE_SEPARATOR);
    //table2
    mockFileWriter.write("INSERT INTO table2(name1) VALUES ('value1');" + EOL_MARKER + LINE_SEPARATOR);
    replay(mockDbConnection);
    replay(mockDbProvider);
    replay(mockFileWriter);
    envHandler.setForeignKeyCheck(false);
    envHandler.setIncludeDeleteStatements(false);
    envHandler.setLockTables(false);
    envHandler.writeBackupToFile(mockFileWriter);
    verify(mockDbConnection);
    verify(mockDbProvider);
    verify(mockFileWriter);
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) OracleEnvironmentHandler(com.axway.ats.environment.database.OracleEnvironmentHandler) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 2 with OracleEnvironmentHandler

use of com.axway.ats.environment.database.OracleEnvironmentHandler in project ats-framework by Axway.

the class Test_OracleEnvironmentHandler method restoreBackupNegativeExceptionThrownOnExecuteBatch.

@Test(expected = DatabaseEnvironmentCleanupException.class)
public void restoreBackupNegativeExceptionThrownOnExecuteBatch() throws DatabaseEnvironmentCleanupException, SQLException {
    expect(mockDbConnection.getConnHash()).andReturn(Long.toString(System.nanoTime()));
    expect(mockDbConnection.getDataSource()).andReturn(mockDataSource);
    expect(mockDbConnection.getUser()).andReturn("user");
    expect(mockDbConnection.getPassword()).andReturn("password");
    expect(mockDataSource.getConnection("user", "password")).andReturn(mockConnection);
    //now the restore begins
    expect(mockConnection.getAutoCommit()).andReturn(true);
    mockConnection.setAutoCommit(false);
    expect(mockConnection.createStatement()).andReturn(mockStatement);
    //the backup restore
    expect(mockConnection.prepareStatement("SET FOREIGN_KEY_CHECKS = 0")).andReturn(mockStatement);
    expect(mockStatement.execute()).andThrow(new SQLException());
    mockStatement.close();
    mockConnection.rollback();
    mockConnection.setAutoCommit(true);
    mockConnection.close();
    replay(mockDbConnection);
    replay(mockDataSource);
    replay(mockConnection);
    replay(mockStatement);
    replay(mockDbProvider);
    OracleEnvironmentHandler envHandler = new OracleEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.restore(Test_OracleEnvironmentHandler.class.getResource("backupFile.txt").getFile());
    envHandler.disconnect();
    verify(mockDbConnection);
    verify(mockDataSource);
    verify(mockConnection);
    verify(mockStatement);
    verify(mockDbProvider);
}
Also used : SQLException(java.sql.SQLException) OracleEnvironmentHandler(com.axway.ats.environment.database.OracleEnvironmentHandler) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 3 with OracleEnvironmentHandler

use of com.axway.ats.environment.database.OracleEnvironmentHandler in project ats-framework by Axway.

the class Test_OracleEnvironmentHandler method createBackupPositive.

@Test
public void createBackupPositive() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException {
    DbTable table1 = new DbTable("table1");
    DbTable table2 = new DbTable("table2");
    //the columns meta data
    DbRecordValuesList column1MetaData = new DbRecordValuesList();
    column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
    column1MetaData.add(new DbRecordValue("", "DATA_TYPE", "varchar(32)"));
    column1MetaData.add(new DbRecordValue("", "DATA_DEFAULT", true));
    DbRecordValuesList column2MetaData = new DbRecordValuesList();
    column2MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name2"));
    column2MetaData.add(new DbRecordValue("", "DATA_TYPE", "varchar(32)"));
    column2MetaData.add(new DbRecordValue("", "DATA_DEFAULT", true));
    DbRecordValuesList column3MetaData = new DbRecordValuesList();
    column3MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name3"));
    column3MetaData.add(new DbRecordValue("", "DATA_TYPE", "bit"));
    column3MetaData.add(new DbRecordValue("", "DATA_DEFAULT", 1));
    DbRecordValuesList[] columnsMetaData = new DbRecordValuesList[] { column1MetaData, column2MetaData, column3MetaData };
    DbRecordValuesList record1Value = new DbRecordValuesList();
    record1Value.add(new DbRecordValue("table1", "name1", "value1"));
    record1Value.add(new DbRecordValue("table1", "name2", null));
    record1Value.add(new DbRecordValue("table1", "name3", new String(new char[] { 1 })));
    DbRecordValuesList[] recordValues = new DbRecordValuesList[] { record1Value };
    OracleEnvironmentHandler envHandler = new OracleEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
    expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData).times(2);
    expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues).times(2);
    //expect the file writer calls
    //foreign keys check start
    mockFileWriter.write("SET CONSTRAINTS ALL DEFERRED;" + EOL_MARKER + LINE_SEPARATOR);
    //table1
    mockFileWriter.write("DELETE FROM table1;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("INSERT INTO table1(name1,name2,name3) VALUES ('value1',NULL,'" + new String(new char[] { 1 }) + "');" + EOL_MARKER + LINE_SEPARATOR);
    //table2
    mockFileWriter.write("DELETE FROM table2;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("INSERT INTO table2(name1,name2,name3) VALUES ('value1',NULL,'" + new String(new char[] { 1 }) + "');" + EOL_MARKER + LINE_SEPARATOR);
    //foreign keys check end
    mockFileWriter.write("");
    replay(mockDbConnection);
    replay(mockDbProvider);
    replay(mockFileWriter);
    envHandler.writeBackupToFile(mockFileWriter);
    verify(mockDbConnection);
    verify(mockDbProvider);
    verify(mockFileWriter);
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) OracleEnvironmentHandler(com.axway.ats.environment.database.OracleEnvironmentHandler) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 4 with OracleEnvironmentHandler

use of com.axway.ats.environment.database.OracleEnvironmentHandler in project ats-framework by Axway.

the class Test_OracleEnvironmentHandler method createBackupSkipColumnsPositive.

@Test
public void createBackupSkipColumnsPositive() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException {
    DbTable table1 = new DbTable("table1");
    List<String> columnsToSkip = new ArrayList<String>();
    columnsToSkip.add("name2");
    DbTable table2 = new DbTable("table2", columnsToSkip);
    //the columns meta data
    DbRecordValuesList column1MetaData = new DbRecordValuesList();
    column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
    column1MetaData.add(new DbRecordValue("", "DATA_TYPE", "varchar(32)"));
    column1MetaData.add(new DbRecordValue("", "DATA_DEFAULT", true));
    DbRecordValuesList column2MetaData = new DbRecordValuesList();
    column2MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name2"));
    column2MetaData.add(new DbRecordValue("", "DATA_TYPE", "varchar(32)"));
    column2MetaData.add(new DbRecordValue("", "DATA_DEFAULT", true));
    DbRecordValuesList column3MetaData = new DbRecordValuesList();
    column3MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name3"));
    column3MetaData.add(new DbRecordValue("", "DATA_TYPE", "bit"));
    column3MetaData.add(new DbRecordValue("", "DATA_DEFAULT", true));
    DbRecordValuesList[] columnsMetaData = new DbRecordValuesList[] { column1MetaData, column2MetaData, column3MetaData };
    //to be returned for the first table
    DbRecordValuesList recordValue1 = new DbRecordValuesList();
    recordValue1.add(new DbRecordValue("table1", "name1", "value1"));
    recordValue1.add(new DbRecordValue("table1", "name2", null));
    recordValue1.add(new DbRecordValue("table1", "name3", new String(new char[] { 5 })));
    DbRecordValuesList[] recordValues1 = new DbRecordValuesList[] { recordValue1 };
    //to be returned for the seconds table - one of the columns should be skipped
    DbRecordValuesList recordValue2 = new DbRecordValuesList();
    recordValue2.add(new DbRecordValue("table1", "name1", "value1"));
    recordValue2.add(new DbRecordValue("table1", "name3", new String(new char[] { 5 })));
    DbRecordValuesList[] recordValues2 = new DbRecordValuesList[] { recordValue2 };
    OracleEnvironmentHandler envHandler = new OracleEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
    expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
    expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues1);
    expect(mockDbProvider.select(isA(String.class))).andReturn(columnsMetaData);
    expect(mockDbProvider.select(isA(DbQuery.class), eq(DbReturnModes.ESCAPED_STRING))).andReturn(recordValues2);
    //expect the file writer calls
    //foreign keys check start
    mockFileWriter.write("SET CONSTRAINTS ALL DEFERRED;" + EOL_MARKER + LINE_SEPARATOR);
    //table1
    mockFileWriter.write("DELETE FROM table1;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("INSERT INTO table1(name1,name2,name3) VALUES ('value1',NULL,'" + new String(new char[] { 5 }) + "');" + EOL_MARKER + LINE_SEPARATOR);
    //table2
    mockFileWriter.write("DELETE FROM table2;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("INSERT INTO table2(name1,name3) VALUES ('value1','" + new String(new char[] { 5 }) + "');" + EOL_MARKER + LINE_SEPARATOR);
    //foreign keys check end
    mockFileWriter.write("");
    replay(mockDbConnection);
    replay(mockDbProvider);
    replay(mockFileWriter);
    envHandler.writeBackupToFile(mockFileWriter);
    verify(mockDbConnection);
    verify(mockDbProvider);
    verify(mockFileWriter);
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) OracleEnvironmentHandler(com.axway.ats.environment.database.OracleEnvironmentHandler) ArrayList(java.util.ArrayList) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 5 with OracleEnvironmentHandler

use of com.axway.ats.environment.database.OracleEnvironmentHandler in project ats-framework by Axway.

the class Test_OracleEnvironmentHandler method restoreBackupPositive.

@Test
public void restoreBackupPositive() throws DatabaseEnvironmentCleanupException, SQLException {
    expect(mockDbConnection.getConnHash()).andReturn(Long.toString(System.nanoTime()));
    expect(mockDbConnection.getDataSource()).andReturn(mockDataSource);
    expect(mockDbConnection.getUser()).andReturn("user");
    expect(mockDbConnection.getPassword()).andReturn("password");
    expect(mockDataSource.getConnection("user", "password")).andReturn(mockConnection);
    //now the restore begins
    expect(mockConnection.getAutoCommit()).andReturn(true);
    mockConnection.setAutoCommit(false);
    //the backup restore
    expect(mockConnection.prepareStatement("SET FOREIGN_KEY_CHECKS = 0")).andReturn(mockStatement);
    expect(mockConnection.prepareStatement(LINE_SEPARATOR + "LOCK TABLES `Revision` WRITE")).andReturn(mockStatement);
    expect(mockConnection.prepareStatement("DELETE FROM `Revision`")).andReturn(mockStatement);
    expect(mockConnection.prepareStatement("INSERT INTO `Revision` (id,SchemaVersion,STVersion,InstallDate) " + LINE_SEPARATOR + "VALUES(1,2,'4.9  206','2008-11-27 12:35:49.0')")).andReturn(mockStatement);
    expect(mockConnection.prepareStatement("UNLOCK TABLES")).andReturn(mockStatement);
    expect(mockStatement.execute()).andReturn(true).times(5);
    mockStatement.close();
    expectLastCall().times(5);
    mockConnection.commit();
    mockConnection.setAutoCommit(true);
    mockConnection.close();
    replay(mockDbConnection);
    replay(mockDataSource);
    replay(mockConnection);
    replay(mockStatement);
    replay(mockDbProvider);
    OracleEnvironmentHandler envHandler = new OracleEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.restore(Test_OracleEnvironmentHandler.class.getResource("backupFile.txt").getFile());
    //        envHandler.disconnect();
    verify(mockDbConnection);
    verify(mockDataSource);
    verify(mockConnection);
    verify(mockStatement);
    verify(mockDbProvider);
}
Also used : OracleEnvironmentHandler(com.axway.ats.environment.database.OracleEnvironmentHandler) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Aggregations

BaseTest (com.axway.ats.environment.BaseTest)7 OracleEnvironmentHandler (com.axway.ats.environment.database.OracleEnvironmentHandler)7 Test (org.junit.Test)7 DbTable (com.axway.ats.environment.database.model.DbTable)5 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)4 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)4 DbQuery (com.axway.ats.common.dbaccess.DbQuery)3 ArrayList (java.util.ArrayList)2 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 SQLException (java.sql.SQLException)1