### Question:
RenameTableOperation { public void execute(CloseableMetaStoreClient client, Table from, Table to) throws Exception { LOG .info("Renaming table {}.{} to {}.{}", from.getDbName(), from.getTableName(), to.getDbName(), to.getTableName()); Table fromTable = client.getTable(from.getDbName(), from.getTableName()); Table toTable = client.getTable(to.getDbName(), to.getTableName()); String fromTableName = fromTable.getTableName(); String toTableName = toTable.getTableName(); String toDelete = toTableName + DELETE_ME; try { fromTable.setTableName(toTableName); toTable.setTableName(toDelete); client.alter_table(toTable.getDbName(), toTableName, toTable); client.alter_table(fromTable.getDbName(), fromTableName, fromTable); } finally { dropTableService.dropTable(client, toTable.getDbName(), toDelete); } } RenameTableOperation(DropTableService dropTableService); void execute(CloseableMetaStoreClient client, Table from, Table to);  }### Answer:
@Test public void typicalRenameTable() throws Exception { Table toTableTemp = new Table(toTable); toTableTemp.setTableName(TO_TABLE_NAME_TEMP); operation.execute(client, fromTable, toTable); fromTable.setTableName(TO_TABLE_NAME); verify(client).alter_table(TO_DB_NAME, TO_TABLE_NAME, toTableTemp); verify(client).alter_table(FROM_DB_NAME, FROM_TABLE_NAME, fromTable); verify(dropTableService).dropTable(client, TO_DB_NAME, TO_TABLE_NAME_TEMP); }
@Test public void renameToTableException() throws Exception { Table toTableTemp = new Table(toTable); toTableTemp.setTableName(TO_TABLE_NAME_TEMP); TException toBeThrown = new TException(); doThrow(toBeThrown).when(client).alter_table(TO_DB_NAME, TO_TABLE_NAME, toTableTemp); try { operation.execute(client, fromTable, toTable); fail("Should throw exception."); } catch (Exception e) { verify(client).getTable(FROM_DB_NAME, FROM_TABLE_NAME); verify(client).getTable(TO_DB_NAME, TO_TABLE_NAME); fromTable.setTableName(TO_TABLE_NAME); verify(client).alter_table(TO_DB_NAME, TO_TABLE_NAME, toTableTemp); verify(dropTableService).dropTable(client, TO_DB_NAME, TO_TABLE_NAME_TEMP); verifyNoMoreInteractions(client); assertThat(e, is(toBeThrown)); } }
@Test public void renameFromTableException() throws Exception { Table toTableTemp = new Table(toTable); toTableTemp.setTableName(TO_TABLE_NAME_TEMP); TException toBeThrown = new TException(); doThrow(toBeThrown).when(client).alter_table(FROM_DB_NAME, FROM_TABLE_NAME, fromTable); try { operation.execute(client, fromTable, toTable); fail("Should throw exception."); } catch (Exception e) { verify(client).getTable(FROM_DB_NAME, FROM_TABLE_NAME); verify(client).getTable(TO_DB_NAME, TO_TABLE_NAME); fromTable.setTableName(TO_TABLE_NAME); verify(client).alter_table(TO_DB_NAME, TO_TABLE_NAME, toTableTemp); verify(client).alter_table(FROM_DB_NAME, FROM_TABLE_NAME, fromTable); verify(dropTableService).dropTable(client, TO_DB_NAME, TO_TABLE_NAME_TEMP); verifyNoMoreInteractions(client); assertThat(e, is(toBeThrown)); } }