Search in sources :

Example 1 with DbTable

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

the class Test_MysqlEnvironmentHandler method createBackupSkipColumnsPositive.

@Test
public void createBackupSkipColumnsPositive() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException, SQLException {
    // the columns meta data
    DbRecordValuesList column1MetaData = new DbRecordValuesList();
    column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
    column1MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "varchar(32)"));
    column1MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", true));
    DbRecordValuesList column2MetaData = new DbRecordValuesList();
    column2MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name2"));
    column2MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "varchar(32)"));
    column2MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", true));
    DbRecordValuesList column3MetaData = new DbRecordValuesList();
    column3MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name3"));
    column3MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "bit(2)"));
    column3MetaData.add(new DbRecordValue("", "COLUMN_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", "0x00"));
    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", "0x10"));
    DbRecordValuesList[] recordValues2 = new DbRecordValuesList[] { recordValue2 };
    expect(mockDbProvider.getConnection()).andReturn(mockConnection);
    expect(mockConnection.getMetaData()).andReturn(metaData);
    expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
    expect(metaData.getDriverMajorVersion()).andReturn(5);
    expect(metaData.getDriverMinorVersion()).andReturn(1);
    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 FOREIGN_KEY_CHECKS = 0;" + EOL_MARKER + LINE_SEPARATOR);
    // table1
    mockFileWriter.write("LOCK TABLES `table1` WRITE;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("DELETE FROM `table1`;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("INSERT INTO `table1` (name1,name2,name3) VALUES('value1',NULL,0x00);" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.flush();
    mockFileWriter.write("UNLOCK TABLES;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write(LINE_SEPARATOR);
    // table2
    mockFileWriter.write("LOCK TABLES `table2` WRITE;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("DELETE FROM `table2`;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("INSERT INTO `table2` (name1,name3) VALUES('value1',0x10);" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.flush();
    mockFileWriter.write("UNLOCK TABLES;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write(LINE_SEPARATOR);
    // foreign keys check end
    mockFileWriter.write("");
    replayAll();
    DbTable table1 = new DbTable("table1");
    List<String> columnsToSkip = new ArrayList<String>();
    columnsToSkip.add("name2");
    DbTable table2 = new DbTable("table2", "dbo", columnsToSkip);
    MysqlEnvironmentHandler envHandler = new MysqlEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    envHandler.writeBackupToFile(mockFileWriter);
    verifyAll();
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) ArrayList(java.util.ArrayList) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 2 with DbTable

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

the class Test_MysqlEnvironmentHandler method createBackupNegativeNoColumns.

@Test(expected = DatabaseEnvironmentCleanupException.class)
public void createBackupNegativeNoColumns() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException, SQLException {
    expect(mockDbProvider.getConnection()).andReturn(mockConnection);
    expect(mockConnection.getMetaData()).andReturn(metaData);
    expect(mockDbConnection.getUser()).andReturn("myUserName");
    expect(metaData.getDriverMajorVersion()).andReturn(5);
    expect(metaData.getDriverMinorVersion()).andReturn(1);
    expect(mockDbProvider.select(isA(String.class))).andReturn(new DbRecordValuesList[] {});
    mockFileWriter.write("SET FOREIGN_KEY_CHECKS = 0;" + EOL_MARKER + LINE_SEPARATOR);
    replayAll();
    DbTable table1 = new DbTable("table1");
    DbTable table2 = new DbTable("table2");
    MysqlEnvironmentHandler envHandler = new MysqlEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    envHandler.writeBackupToFile(mockFileWriter);
}
Also used : DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 3 with DbTable

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

the class Test_MysqlEnvironmentHandler method createBackupNegativeSkippedColumnIsNotNullable.

@Test(expected = ColumnHasNoDefaultValueException.class)
public void createBackupNegativeSkippedColumnIsNotNullable() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException, SQLException {
    // the columns meta data
    DbRecordValuesList column1MetaData = new DbRecordValuesList();
    column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
    column1MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "varchar(32)"));
    column1MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", true));
    DbRecordValuesList column2MetaData = new DbRecordValuesList();
    column2MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name2"));
    column2MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "varchar(32)"));
    column2MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", null));
    DbRecordValuesList column3MetaData = new DbRecordValuesList();
    column3MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name3"));
    column3MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "bit(1)"));
    column3MetaData.add(new DbRecordValue("", "COLUMN_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", "1"));
    DbRecordValuesList[] recordValues = new DbRecordValuesList[] { record1Value };
    expect(mockDbProvider.getConnection()).andReturn(mockConnection);
    expect(mockConnection.getMetaData()).andReturn(metaData);
    expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
    expect(metaData.getDriverMajorVersion()).andReturn(5);
    expect(metaData.getDriverMinorVersion()).andReturn(1);
    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);
    mockFileWriter.write("SET FOREIGN_KEY_CHECKS = 0;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("DELETE FROM `table1`;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("DELETE FROM `table2`;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("LOCK TABLES `table1` WRITE;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write("INSERT INTO `table1` (name1,name2,name3) VALUES('value1',NULL,0x1);" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.flush();
    mockFileWriter.write("UNLOCK TABLES;" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.write(LINE_SEPARATOR);
    replayAll();
    DbTable table1 = new DbTable("table1");
    List<String> columnsToSkip = new ArrayList<String>();
    columnsToSkip.add("name2");
    DbTable table2 = new DbTable("table2", "dbo", columnsToSkip);
    MysqlEnvironmentHandler envHandler = new MysqlEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    envHandler.writeBackupToFile(mockFileWriter);
    verifyAll();
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) ArrayList(java.util.ArrayList) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 4 with DbTable

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

the class Test_MysqlEnvironmentHandler method createBackupNoForeignKeysNoLockNoDelete.

@Test
public void createBackupNoForeignKeysNoLockNoDelete() throws DatabaseEnvironmentCleanupException, DbException, IOException, ParseException, SQLException {
    // the columns meta data
    DbRecordValuesList column1MetaData = new DbRecordValuesList();
    column1MetaData.add(new DbRecordValue("", "COLUMN_NAME", "name1"));
    column1MetaData.add(new DbRecordValue("", "COLUMN_TYPE", "varchar(32)"));
    column1MetaData.add(new DbRecordValue("", "COLUMN_DEFAULT", true));
    DbRecordValuesList[] columnsMetaData = new DbRecordValuesList[] { column1MetaData };
    DbRecordValuesList record1Value = new DbRecordValuesList();
    record1Value.add(new DbRecordValue("table1", "name1", "value1"));
    DbRecordValuesList[] recordValues = new DbRecordValuesList[] { record1Value };
    expect(mockDbProvider.getConnection()).andReturn(mockConnection);
    expect(mockConnection.getMetaData()).andReturn(metaData);
    expect(mockDbConnection.getUser()).andReturn("myUserName").atLeastOnce();
    expect(metaData.getDriverMajorVersion()).andReturn(5);
    expect(metaData.getDriverMinorVersion()).andReturn(1);
    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);
    mockFileWriter.flush();
    mockFileWriter.write(LINE_SEPARATOR);
    // table2
    mockFileWriter.write("INSERT INTO `table2` (name1) VALUES('value1');" + EOL_MARKER + LINE_SEPARATOR);
    mockFileWriter.flush();
    mockFileWriter.write(LINE_SEPARATOR);
    replayAll();
    DbTable table1 = new DbTable("table1");
    DbTable table2 = new DbTable("table2");
    MysqlEnvironmentHandler envHandler = new MysqlEnvironmentHandler(mockDbConnection, mockDbProvider);
    envHandler.addTable(table1);
    envHandler.addTable(table2);
    envHandler.setForeignKeyCheck(false);
    envHandler.setIncludeDeleteStatements(false);
    envHandler.setLockTables(false);
    envHandler.writeBackupToFile(mockFileWriter);
    verifyAll();
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValue(com.axway.ats.core.dbaccess.DbRecordValue) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 5 with DbTable

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

the class Test_DbTable method accessors.

@Test
public void accessors() {
    DbTable dbTable = new DbTable("table1");
    assertEquals("table1", dbTable.getTableName());
    assertEquals(0, dbTable.getColumnsToExclude().size());
    List<String> columns = new ArrayList<String>();
    dbTable = new DbTable("table2", "dbo", columns);
    assertEquals("table2", dbTable.getTableName());
    assertEquals(columns, dbTable.getColumnsToExclude());
}
Also used : ArrayList(java.util.ArrayList) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Aggregations

DbTable (com.axway.ats.environment.database.model.DbTable)30 BaseTest (com.axway.ats.environment.BaseTest)20 Test (org.junit.Test)20 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)15 DbQuery (com.axway.ats.common.dbaccess.DbQuery)14 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)12 ArrayList (java.util.ArrayList)12 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)3 DatabaseEnvironmentCleanupException (com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 DatabaseOperations (com.axway.ats.action.dbaccess.DatabaseOperations)1 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 DbConnection (com.axway.ats.core.dbaccess.DbConnection)1 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 DbConnPostgreSQL (com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL)1 PostgreSqlColumnDescription (com.axway.ats.core.dbaccess.postgresql.PostgreSqlColumnDescription)1 DatabaseEnvironmentUnit (com.axway.ats.environment.database.DatabaseEnvironmentUnit)1 BackupHandler (com.axway.ats.environment.database.model.BackupHandler)1 TestBox (com.axway.ats.harness.config.TestBox)1