package net.raymanoz.command;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import net.raymanoz.command.MigrateCommand.Helper;
import net.raymanoz.config.Configuration;
import net.raymanoz.config.ScriptStatus;
import net.raymanoz.domain.SchemaVersion;
import net.raymanoz.io.File;
import net.raymanoz.migrate.SchemaVersionRepository;
import net.raymanoz.migrate.Script;
import net.raymanoz.migrate.ScriptList;
import net.raymanoz.ui.UserInteractionStrategy;
import org.junit.*;
import org.mockito.InOrder;
import static org.mockito.Mockito.*;
public class MigrateCommandTest {
private final SchemaVersionRepository repository = mock(SchemaVersionRepository.class);
private final Configuration config = mock(Configuration.class);
private final CommandAssembler assembler = mock(CommandAssembler.class);
private final UserInteractionStrategy userInteractionStrategy = mock(UserInteractionStrategy.class);
private final SchemaVersion version = mock(SchemaVersion.class);
final SchemaVersion afterFinish = mock(SchemaVersion.class);
private final Helper helper = mock(Helper.class);
private final Connection connection = mock(Connection.class);
private MigrateCommand command;
@Before
public void setUp(){
when(config.getUserInteractionStrategy()).thenReturn(userInteractionStrategy);
command = new MigrateCommand(repository, config, assembler, helper);
verify(config).getUserInteractionStrategy();
when(repository.getVersion()).thenReturn(version).thenReturn(afterFinish);
when(config.getConnection()).thenReturn(connection);
}
private void setUpScripts(long currentVersion, ScriptList ... listOfVersionScripts){
when(version.getDBVersion()).thenReturn(currentVersion);
when(config.getLatestDBVersion()).thenReturn(currentVersion + listOfVersionScripts.length - 1);
long idx = 0;
for(ScriptList versionScripts: listOfVersionScripts){
File scriptsDir = mock(File.class);
File[] listFiles = new File[]{mock(File.class)};
when(scriptsDir.listFiles()).thenReturn(listFiles);
final long dbVersion = currentVersion + idx;
when(config.getScriptDirectory(dbVersion)).thenReturn(scriptsDir);
when(assembler.newScriptList(listFiles, dbVersion)).thenReturn(versionScripts);
when(versionScripts.DBVersion()).thenReturn(dbVersion);
idx++;
}
}
@Test
public void getCommandString(){
assertEquals(MigrateCommand.COMMAND_STRING, command.getCommandString());
}
@Test
public void version(){
setUpScripts(122l);
assertEquals(version, command.version());
verify(repository).getVersion();
verifyNoMoreInteractions(repository);
// should cache version
assertEquals(version, command.version());
verifyNoMoreInteractions(repository);
}
@Test
public void scripts(){
final ScriptList[] scripts = new ScriptList[]{mock(ScriptList.class),mock(ScriptList.class),mock(ScriptList.class)};
final long currentVersion = 982L;
setUpScripts(currentVersion, scripts);
List<ScriptList> result = command.scripts();
assertNotNull(result);
assertArrayEquals(scripts, result.toArray());
verify(repository).getVersion();
verify(repository).validateNoOtherLaterActivity(version);
verify(version, never()).setDBVersion(anyLong());
verify(version, never()).setPatchNo(anyLong());
verifyNoMoreInteractions(repository);
// should cache scripts
result = command.scripts();
assertNotNull(result);
assertArrayEquals(scripts, result.toArray());
verifyNoMoreInteractions(repository);
}
@Test
public void scriptsWhenDbVersionNewerLastScriptsReturnEmptyList(){
when(version.getDBVersion()).thenReturn(1234L);
when(config.getLatestDBVersion()).thenReturn(1233L);
List<ScriptList> result = command.scripts();
assertNotNull(result);
assertEquals(0, result.size());
}
@Test
public void summary(){
final ScriptList[] scripts = new ScriptList[]{mock(ScriptList.class),mock(ScriptList.class),mock(ScriptList.class)};
final long currentVersion = 10L;
final long patch = 12L;
setUpScripts(currentVersion, scripts);
when(helper.scriptsToBeApplied(version, scripts[0])).thenReturn(1L);
when(helper.scriptsToBeApplied(version, scripts[1])).thenReturn(2L);
when(helper.scriptsToBeApplied(version, scripts[2])).thenReturn(3L);
when(scripts[0].size()).thenReturn(10);
when(scripts[1].size()).thenReturn(2);
when(scripts[2].size()).thenReturn(3);
when(version.getPatchNo()).thenReturn(patch);
final String summary =
"Schema DB version at startup: 10 patch 12\n" +
"10 scripts found for version 10, 1 to be applied\n" +
"2 scripts found for version 11, 2 to be applied\n" +
"3 scripts found for version 12, 3 to be applied\n" +
"15 scripts found for 3 versions, 6 to be applied\n";
assertEquals(summary, command.summary());
assertEquals(6, command.noScriptsToMigrate());
}
@Test
public void summaryOneVersionNeedsApplying(){
final ScriptList[] scripts = new ScriptList[]{mock(ScriptList.class)};
final long currentVersion = 10L;
final long patch = 12L;
setUpScripts(currentVersion, scripts);
when(helper.scriptsToBeApplied(version, scripts[0])).thenReturn(1L);
when(scripts[0].size()).thenReturn(10);
when(version.getPatchNo()).thenReturn(patch);
final String summary =
"Schema DB version at startup: 10 patch 12\n" +
"10 scripts found for version 10, 1 to be applied\n";
assertEquals(summary, command.summary());
assertEquals(1, command.noScriptsToMigrate());
}
@Test
public void summaryOlderThanDb(){
final ScriptList[] scripts = new ScriptList[]{};
final long currentVersion = 10L;
final long patch = 12L;
setUpScripts(currentVersion, scripts);
when(version.getPatchNo()).thenReturn(patch);
final String summary =
"Schema DB version at startup: 10 patch 12\n" +
"0 scripts found for 0 versions, 0 to be applied\n";
assertEquals(summary, command.summary());
verifyNoMoreInteractions(helper);
assertEquals(0, command.noScriptsToMigrate());
}
@Test
public void noScriptsToMigrate(){
final ScriptList[] scripts = new ScriptList[]{mock(ScriptList.class),mock(ScriptList.class),mock(ScriptList.class)};
final long currentVersion = 10L;
final long patch = 12L;
setUpScripts(currentVersion, scripts);
when(helper.scriptsToBeApplied(version, scripts[0])).thenReturn(1L);
when(helper.scriptsToBeApplied(version, scripts[1])).thenReturn(2L);
when(helper.scriptsToBeApplied(version, scripts[2])).thenReturn(3L);
when(scripts[0].size()).thenReturn(10);
when(scripts[1].size()).thenReturn(2);
when(scripts[2].size()).thenReturn(3);
when(version.getPatchNo()).thenReturn(patch);
assertEquals(6, command.noScriptsToMigrate());
}
private void setVersion(long dbVersion, long patch){
when(version.getDBVersion()).thenReturn(dbVersion);
when(version.getPatchNo()).thenReturn(patch);
}
private void setFinishVersion(long dbVersion, long patch){
when(afterFinish.getDBVersion()).thenReturn(dbVersion);
when(afterFinish.getPatchNo()).thenReturn(patch);
}
private Script mockScript(long dbVersion, long patch, ScriptStatus returnedScriptStatus){
Script result = mock(Script.class);
when(result.execute(any(UserInteractionStrategy.class))).thenReturn(returnedScriptStatus);
when(result.getDBVersion()).thenReturn(dbVersion);
when(result.getPatch()).thenReturn(patch);
return result;
}
private Script mockScript(long dbVersion, long patch){
return mockScript(dbVersion, patch, null);
}
@Test
public void alreadyMigrated(){
final Script script_1_1 = mockScript(1, 1);
final Script script_1_10 = mockScript(1, 10);
final Script script_2_1 = mockScript(2, 1);
final Script script_3_10 = mockScript(3, 10);
setVersion(1, 0);
assertFalse(command.alreadyMigrated(script_1_1));
assertFalse(command.alreadyMigrated(script_1_10));
assertFalse(command.alreadyMigrated(script_2_1));
assertFalse(command.alreadyMigrated(script_3_10));
setVersion(1, 1);
assertTrue(command.alreadyMigrated(script_1_1));
assertFalse(command.alreadyMigrated(script_1_10));
assertFalse(command.alreadyMigrated(script_2_1));
assertFalse(command.alreadyMigrated(script_3_10));
setVersion(1, 9);
assertTrue(command.alreadyMigrated(script_1_1));
assertFalse(command.alreadyMigrated(script_1_10));
assertFalse(command.alreadyMigrated(script_2_1));
assertFalse(command.alreadyMigrated(script_3_10));
setVersion(1, 10);
assertTrue(command.alreadyMigrated(script_1_1));
assertTrue(command.alreadyMigrated(script_1_10));
assertFalse(command.alreadyMigrated(script_2_1));
assertFalse(command.alreadyMigrated(script_3_10));
setVersion(1, 11);
assertTrue(command.alreadyMigrated(script_1_1));
assertTrue(command.alreadyMigrated(script_1_10));
assertFalse(command.alreadyMigrated(script_2_1));
assertFalse(command.alreadyMigrated(script_3_10));
setVersion(2, 10);
assertTrue(command.alreadyMigrated(script_1_1));
assertTrue(command.alreadyMigrated(script_1_10));
assertTrue(command.alreadyMigrated(script_2_1));
assertFalse(command.alreadyMigrated(script_3_10));
setVersion(3, 10);
assertTrue(command.alreadyMigrated(script_1_1));
assertTrue(command.alreadyMigrated(script_1_10));
assertTrue(command.alreadyMigrated(script_2_1));
assertTrue(command.alreadyMigrated(script_3_10));
setVersion(4, 1);
assertTrue(command.alreadyMigrated(script_1_1));
assertTrue(command.alreadyMigrated(script_1_10));
assertTrue(command.alreadyMigrated(script_2_1));
assertTrue(command.alreadyMigrated(script_3_10));
}
@Test
public void migrateSkipsIfScriptAlreadyMigrated() {
final Script script = mockScript(1, 1);
setVersion(1, 1);
assertTrue(command.migrate(script));
verify(repository).getVersion();
verify(script).getDBVersion();
verify(script).getPatch();
verifyNoMoreInteractions(repository, userInteractionStrategy, script);
}
private void checkMigrate(ScriptStatus returnedScriptStatus, int scriptIdx, boolean expected){
final long dbVersion = 23;
final long patch = 2098;
final Script script = mockScript(dbVersion, patch, returnedScriptStatus);
setVersion(dbVersion, patch - 1);
assertEquals(expected, command.migrate(script));
verify(script, times(2)).getDBVersion();
verify(script, times(2)).getPatch();
verify(script, times(2)).description();
InOrder inOrder = inOrder(repository, script, userInteractionStrategy, version);
inOrder.verify(repository).getVersion();
inOrder.verify(version, times(2)).getDBVersion();
inOrder.verify(version, times(1)).getPatchNo();
inOrder.verify(repository).validateNoOtherActivity(dbVersion, patch);
inOrder.verify(userInteractionStrategy).scriptStatusMessage(scriptIdx, script, ScriptStatus.STARTED);
inOrder.verify(repository).recordStartPatch(script);
inOrder.verify(script).execute(userInteractionStrategy);
inOrder.verify(repository).recordFinishPatch(script, returnedScriptStatus);
inOrder.verify(userInteractionStrategy).scriptStatusMessage(scriptIdx, script, returnedScriptStatus);
inOrder.verify(version).setDBVersion(dbVersion);
inOrder.verify(version).setPatchNo(patch);
verifyNoMoreInteractions(repository, userInteractionStrategy, script, version);
}
@Test
public void migrateWhenSkipped() {
checkMigrate(ScriptStatus.SKIPPED, 1, true);
}
@Test
public void migrateWhenCompleted() {
checkMigrate(ScriptStatus.COMPLETED, 1, true);
}
@Test
public void migrateWhenErrored() {
checkMigrate(ScriptStatus.ERRORED, 1, false);
}
@Test
public void migrateWhenHalted() {
checkMigrate(ScriptStatus.HALTED, 1, false);
}
private String requiresDialog(long dbVersion, int patch){
return String.format("Var %d %d", dbVersion, patch);
}
private Set<String> forRequiresDialog(long dbVersion, int patch){
Set<String> result = new HashSet<String>();
result.add(requiresDialog(dbVersion, patch));
return result;
}
private List<Script> mockScripts(long dbVersion, boolean includeRequiresDialog, ScriptStatus ... scriptStatus){
List<Script> list = new ArrayList<Script>();
int patch = 0;
for(ScriptStatus status: scriptStatus){
patch++;
Script script = mockScript(dbVersion, patch, status);
if (includeRequiresDialog) {
when(script.variablesRequiringDialog()).thenReturn(forRequiresDialog(dbVersion, patch));
}
list.add(script);
}
return list;
}
private List<Script> mockScripts(long dbVersion, ScriptStatus ... scriptStatus){
return mockScripts(dbVersion, false, scriptStatus);
}
private ScriptList mockScriptList(List<Script> scripts){
ScriptList result = mock(ScriptList.class);
when(result.iterator()).thenReturn(scripts.iterator());
when(result.size()).thenReturn(scripts.size());
return result;
}
private void checkScriptExecuted(InOrder inOrder, Script script, int scriptIdx, ScriptStatus returnedScriptStatus){
inOrder.verify(userInteractionStrategy).scriptStatusMessage(scriptIdx, script, ScriptStatus.STARTED);
inOrder.verify(repository).recordStartPatch(script);
inOrder.verify(script).execute(userInteractionStrategy);
inOrder.verify(repository).recordFinishPatch(script, returnedScriptStatus);
inOrder.verify(userInteractionStrategy).scriptStatusMessage(scriptIdx, script, returnedScriptStatus);
}
private void checkScriptNotExecuted(Script script){
verify(userInteractionStrategy, never()).scriptStatusMessage(anyInt(), eq(script), any(ScriptStatus.class));
verify(repository, never()).recordStartPatch(script);
verify(script, never()).execute(any(UserInteractionStrategy.class));
verify(repository, never()).recordFinishPatch(eq(script), any(ScriptStatus.class));
}
@Test
public void doMigration() throws SQLException{
List<Script> scriptsDbV1 = mockScripts(1, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.SKIPPED, ScriptStatus.COMPLETED);
List<Script> scriptsDbV2 = mockScripts(2, ScriptStatus.COMPLETED, ScriptStatus.SKIPPED);
List<Script> scriptsDbV3 = mockScripts(3, ScriptStatus.SKIPPED, ScriptStatus.COMPLETED);
setUpScripts(1, mockScriptList(scriptsDbV1), mockScriptList(scriptsDbV2), mockScriptList(scriptsDbV3));
setVersion(1, 3);
setFinishVersion(5, 9);
command.doMigration();
checkScriptNotExecuted(scriptsDbV1.get(0));
checkScriptNotExecuted(scriptsDbV1.get(1));
checkScriptNotExecuted(scriptsDbV1.get(2));
InOrder inOrder = inOrder(connection, repository, userInteractionStrategy, scriptsDbV1.get(3), scriptsDbV1.get(4), scriptsDbV2.get(0), scriptsDbV2.get(1), scriptsDbV3.get(0), scriptsDbV3.get(1));
checkScriptExecuted(inOrder, scriptsDbV1.get(3), 1, ScriptStatus.SKIPPED);
checkScriptExecuted(inOrder, scriptsDbV1.get(4), 2, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV2.get(0), 3, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV2.get(1), 4, ScriptStatus.SKIPPED);
checkScriptExecuted(inOrder, scriptsDbV3.get(0), 5, ScriptStatus.SKIPPED);
checkScriptExecuted(inOrder, scriptsDbV3.get(1), 6, ScriptStatus.COMPLETED);
inOrder.verify(connection).close();
inOrder.verify(userInteractionStrategy).completed("Schema DB version at startup: 1 patch 3\nSchema DB version now: 5 patch 9\n3 scripts applied\n3 scripts skipped\n6 scripts in total\n", true);
}
@Test
public void doMigrationNoSkips() throws SQLException{
List<Script> scriptsDbV1 = mockScripts(1, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED);
List<Script> scriptsDbV2 = mockScripts(2, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED);
List<Script> scriptsDbV3 = mockScripts(3, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED);
setUpScripts(1, mockScriptList(scriptsDbV1), mockScriptList(scriptsDbV2), mockScriptList(scriptsDbV3));
setVersion(1, 3);
setFinishVersion(2, 6);
command.doMigration();
checkScriptNotExecuted(scriptsDbV1.get(0));
checkScriptNotExecuted(scriptsDbV1.get(1));
checkScriptNotExecuted(scriptsDbV1.get(2));
InOrder inOrder = inOrder(connection, repository, userInteractionStrategy, scriptsDbV1.get(3), scriptsDbV1.get(4), scriptsDbV2.get(0), scriptsDbV2.get(1), scriptsDbV3.get(0), scriptsDbV3.get(1));
checkScriptExecuted(inOrder, scriptsDbV1.get(3), 1, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV1.get(4), 2, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV2.get(0), 3, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV2.get(1), 4, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV3.get(0), 5, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV3.get(1), 6, ScriptStatus.COMPLETED);
inOrder.verify(connection).close();
inOrder.verify(userInteractionStrategy).completed("Schema DB version at startup: 1 patch 3\nSchema DB version now: 2 patch 6\n6 scripts applied\n", true);
}
@Test
public void doMigrationHalts() throws SQLException{
List<Script> scriptsDbV1 = mockScripts(1, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.HALTED, ScriptStatus.COMPLETED);
List<Script> scriptsDbV2 = mockScripts(2, ScriptStatus.COMPLETED, ScriptStatus.SKIPPED);
List<Script> scriptsDbV3 = mockScripts(3, ScriptStatus.SKIPPED, ScriptStatus.COMPLETED);
setUpScripts(1, mockScriptList(scriptsDbV1), mockScriptList(scriptsDbV2), mockScriptList(scriptsDbV3));
setVersion(1, 3);
setFinishVersion(1, 4);
command.doMigration();
checkScriptNotExecuted(scriptsDbV1.get(0));
checkScriptNotExecuted(scriptsDbV1.get(1));
checkScriptNotExecuted(scriptsDbV1.get(2));
checkScriptNotExecuted(scriptsDbV1.get(4));
checkScriptNotExecuted(scriptsDbV2.get(0));
checkScriptNotExecuted(scriptsDbV2.get(1));
checkScriptNotExecuted(scriptsDbV3.get(0));
checkScriptNotExecuted(scriptsDbV3.get(1));
InOrder inOrder = inOrder(connection, repository, userInteractionStrategy, scriptsDbV1.get(3), scriptsDbV1.get(4), scriptsDbV2.get(0), scriptsDbV2.get(1), scriptsDbV3.get(0), scriptsDbV3.get(1));
checkScriptExecuted(inOrder, scriptsDbV1.get(3), 1, ScriptStatus.HALTED);
inOrder.verify(connection).close();
verify(userInteractionStrategy).completed("Schema DB version at startup: 1 patch 3\nSchema DB version now: 1 patch 4\n0 scripts applied\n", false);
}
@Test
public void doMigrationErrors() throws SQLException{
List<Script> scriptsDbV1 = mockScripts(1, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.ERRORED);
List<Script> scriptsDbV2 = mockScripts(2, ScriptStatus.COMPLETED, ScriptStatus.SKIPPED);
List<Script> scriptsDbV3 = mockScripts(3, ScriptStatus.SKIPPED, ScriptStatus.COMPLETED);
setUpScripts(1, mockScriptList(scriptsDbV1), mockScriptList(scriptsDbV2), mockScriptList(scriptsDbV3));
setVersion(1, 3);
setFinishVersion(9, 3);
command.doMigration();
checkScriptNotExecuted(scriptsDbV1.get(0));
checkScriptNotExecuted(scriptsDbV1.get(1));
checkScriptNotExecuted(scriptsDbV1.get(2));
checkScriptNotExecuted(scriptsDbV2.get(0));
checkScriptNotExecuted(scriptsDbV2.get(1));
checkScriptNotExecuted(scriptsDbV3.get(0));
checkScriptNotExecuted(scriptsDbV3.get(1));
InOrder inOrder = inOrder(connection, repository, userInteractionStrategy, scriptsDbV1.get(3), scriptsDbV1.get(4), scriptsDbV2.get(0), scriptsDbV2.get(1), scriptsDbV3.get(0), scriptsDbV3.get(1));
checkScriptExecuted(inOrder, scriptsDbV1.get(3), 1, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV1.get(4), 2, ScriptStatus.ERRORED);
inOrder.verify(connection).close();
verify(userInteractionStrategy).completed("Schema DB version at startup: 1 patch 3\nSchema DB version now: 9 patch 3\n1 script applied\n", false);
}
@Test
public void closeConnection() throws SQLException{
command.closeConnection();
verify(connection).close();
verifyNoMoreInteractions(repository, userInteractionStrategy, version);
}
@Test
public void closeConnectionWhenThrowsSqlException() throws SQLException{
doThrow(new SQLException()).when(connection).close();
command.closeConnection();
verify(connection).close();
verify(userInteractionStrategy).errorMessage(anyString());
verifyNoMoreInteractions(repository, userInteractionStrategy, version);
}
@Test
public void closeConnectionWhenThrowsException() throws SQLException{
doThrow(new RuntimeException()).when(connection).close();
command.closeConnection();
verify(connection).close();
verify(userInteractionStrategy).errorMessage(anyString());
verifyNoMoreInteractions(repository, userInteractionStrategy, version);
}
@Test
public void addPropertiesNeedingValuesAlreadyMigrated(){
final Script script = mockScript(1, 1);
@SuppressWarnings("unchecked")
final Set<String> variables = mock(Set.class);
setVersion(1, 1);
command.addPropertiesNeedingValues(script, variables);
verify(script).getDBVersion();
verify(script).getPatch();
verify(repository).getVersion();
verifyNoMoreInteractions(repository, userInteractionStrategy, script, variables);
}
@Test
@SuppressWarnings("unchecked")
public void addPropertiesNeedingValues(){
final Script script = mockScript(2, 1);
final Set<String> variables = mock(Set.class);
final Set<String> returned = mock(Set.class);
when(script.variablesRequiringDialog()).thenReturn(returned);
setVersion(1, 1);
command.addPropertiesNeedingValues(script, variables);
verify(script).getDBVersion();
verify(script).getPatch();
verify(script).variablesRequiringDialog();
verify(repository).getVersion();
verify(variables).addAll(returned);
verifyNoMoreInteractions(repository, userInteractionStrategy, script, variables);
}
private void checkStrCollection(Collection<String> actuals, String ... expected){
String[] actual = actuals.toArray(new String[6]);
Arrays.sort(actual);
Arrays.sort(expected);
assertArrayEquals(expected, actual);
}
@Test
public void variablesRequiringDialogInOutstanding(){
List<Script> scriptsDbV1 = mockScripts(1, true, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.HALTED, ScriptStatus.COMPLETED);
List<Script> scriptsDbV2 = mockScripts(2, true, ScriptStatus.COMPLETED, ScriptStatus.SKIPPED);
List<Script> scriptsDbV3 = mockScripts(3, true, ScriptStatus.SKIPPED, ScriptStatus.COMPLETED);
setUpScripts(1, mockScriptList(scriptsDbV1), mockScriptList(scriptsDbV2), mockScriptList(scriptsDbV3));
setVersion(1, 3);
checkStrCollection(command.variablesRequiringDialogInOutstanding(),
requiresDialog(1, 4), requiresDialog(1, 5),
requiresDialog(2, 1), requiresDialog(2, 2),
requiresDialog(3, 1), requiresDialog(3, 2)
);
}
@Test
public void executeWhenUIRequired(){
when(userInteractionStrategy.startUIProcess(any(MigrateCallBack.class))).thenReturn(true);
command.execute();
verify(userInteractionStrategy).startUIProcess(command);
verifyNoMoreInteractions(userInteractionStrategy, repository, config);
}
@Test
public void executeWithArgsWhenUIRequired(){
when(userInteractionStrategy.startUIProcess(any(MigrateCallBack.class))).thenReturn(true);
command.execute(new String[]{});
verify(userInteractionStrategy).startUIProcess(command);
verifyNoMoreInteractions(userInteractionStrategy, repository, config);
}
@Test
public void execute() throws SQLException{
when(userInteractionStrategy.startUIProcess(any(MigrateCallBack.class))).thenReturn(false);
List<Script> scriptsDbV1 = mockScripts(1, true, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.COMPLETED, ScriptStatus.SKIPPED, ScriptStatus.COMPLETED);
List<Script> scriptsDbV2 = mockScripts(2, true, ScriptStatus.COMPLETED, ScriptStatus.SKIPPED);
List<Script> scriptsDbV3 = mockScripts(3, true, ScriptStatus.SKIPPED, ScriptStatus.COMPLETED);
ScriptList[] scripts = new ScriptList[]{mockScriptList(scriptsDbV1), mockScriptList(scriptsDbV2), mockScriptList(scriptsDbV3)};
setUpScripts(1, scripts);
setVersion(1, 3);
setFinishVersion(2l, 6l);
when(helper.scriptsToBeApplied(version, scripts[0])).thenReturn(11L);
when(helper.scriptsToBeApplied(version, scripts[1])).thenReturn(22L);
when(helper.scriptsToBeApplied(version, scripts[2])).thenReturn(33L);
when(repository.getVersion()).thenReturn(version).thenReturn(afterFinish);
command.execute();
verify(userInteractionStrategy).startUIProcess(command);
checkScriptNotExecuted(scriptsDbV1.get(0));
checkScriptNotExecuted(scriptsDbV1.get(1));
checkScriptNotExecuted(scriptsDbV1.get(2));
InOrder inOrder = inOrder(connection, repository, userInteractionStrategy, scriptsDbV1.get(3), scriptsDbV1.get(4), scriptsDbV2.get(0), scriptsDbV2.get(1), scriptsDbV3.get(0), scriptsDbV3.get(1));
checkScriptExecuted(inOrder, scriptsDbV1.get(3), 1, ScriptStatus.SKIPPED);
checkScriptExecuted(inOrder, scriptsDbV1.get(4), 2, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV2.get(0), 3, ScriptStatus.COMPLETED);
checkScriptExecuted(inOrder, scriptsDbV2.get(1), 4, ScriptStatus.SKIPPED);
checkScriptExecuted(inOrder, scriptsDbV3.get(0), 5, ScriptStatus.SKIPPED);
checkScriptExecuted(inOrder, scriptsDbV3.get(1), 6, ScriptStatus.COMPLETED);
inOrder.verify(connection).close();
inOrder.verify(userInteractionStrategy).completed("Schema DB version at startup: 1 patch 3\nSchema DB version now: 2 patch 6\n3 scripts applied\n3 scripts skipped\n6 scripts in total\n", true);
assertEquals(66, command.noScriptsToMigrate());
}
}