CassandraMigrationService { public Task upgradeToVersion(SchemaVersion target) { checkTarget(target); return taskFactory.create(target); } @Inject  CassandraMigrationService(CassandraSchemaVersionDAO schemaVersionDAO, CassandraSchemaTransitions transitions,
                                     MigrationTask.Factory factory, @Named(LATEST_VERSION) SchemaVersion latestVersion); Optional<SchemaVersion> getCurrentVersion(); Optional<SchemaVersion> getLatestVersion(); Task upgradeToVersion(SchemaVersion target); Task upgradeToLastVersion(); static final String LATEST_VERSION; }
@Test void upgradeToVersionShouldNotThrowWhenCurrentVersionIsUpToDate() throws InterruptedException { when(schemaVersionDAO.getCurrentSchemaVersion()).thenReturn(Mono.just(Optional.of(CURRENT_VERSION))); assertThat(testee.upgradeToVersion(OLDER_VERSION).run()) .isEqualTo(Task.Result.COMPLETED); }
@Test void upgradeToVersionShouldUpdateToVersion() throws InterruptedException { when(schemaVersionDAO.getCurrentSchemaVersion()).thenReturn(Mono.just(Optional.of(OLDER_VERSION))); testee.upgradeToVersion(CURRENT_VERSION).run(); verify(schemaVersionDAO, times(1)).updateVersion(eq(CURRENT_VERSION)); }
@Test void upgradeToVersionShouldThrowOnMissingVersion() throws InterruptedException { CassandraSchemaTransitions transitions = new CassandraSchemaTransitions(ImmutableMap.of(FROM_OLDER_TO_CURRENT, successfulMigration)); testee = new CassandraMigrationService(schemaVersionDAO, transitions, version -> new MigrationTask(schemaVersionDAO, transitions, version), LATEST_VERSION); when(schemaVersionDAO.getCurrentSchemaVersion()).thenReturn(Mono.just(Optional.of(OLDER_VERSION))); assertThatThrownBy(() -> testee.upgradeToVersion(LATEST_VERSION).run()) .isInstanceOf(NotImplementedException.class); }
@Test void upgradeToVersionShouldUpdateIntermediarySuccessfulMigrationsInCaseOfError() throws InterruptedException { try { CassandraSchemaTransitions transitions = new CassandraSchemaTransitions(ImmutableMap.of( FROM_OLDER_TO_CURRENT, successfulMigration, FROM_CURRENT_TO_LATEST, () -> { throw new RuntimeException(); })); testee = new CassandraMigrationService(schemaVersionDAO, transitions, version -> new MigrationTask(schemaVersionDAO, transitions, version), LATEST_VERSION); when(schemaVersionDAO.getCurrentSchemaVersion()).thenReturn(Mono.just(Optional.of(OLDER_VERSION))); assertThatThrownBy(() -> testee.upgradeToVersion(LATEST_VERSION).run()) .isInstanceOf(RuntimeException.class); } finally { verify(schemaVersionDAO).updateVersion(CURRENT_VERSION); } }
@Test void partialMigrationShouldThrow() throws InterruptedException { InMemorySchemaDAO schemaVersionDAO = new InMemorySchemaDAO(OLDER_VERSION); Task failingTask = mock(Task.class); when(failingTask.run()).thenThrow(MigrationException.class); Migration migration1 = failingTask::run; Migration migration2 = successfulMigration; CassandraSchemaTransitions transitions = new CassandraSchemaTransitions(ImmutableMap.of( FROM_OLDER_TO_CURRENT, migration1, FROM_CURRENT_TO_LATEST, migration2)); testee = new CassandraMigrationService(schemaVersionDAO, transitions, version -> new MigrationTask(schemaVersionDAO, transitions, version), LATEST_VERSION); assertThatThrownBy(() -> testee.upgradeToVersion(LATEST_VERSION).run()) .isInstanceOf(MigrationException.class); }
@Test void partialMigrationShouldAbortMigrations() throws InterruptedException { InMemorySchemaDAO schemaVersionDAO = new InMemorySchemaDAO(OLDER_VERSION); Task failingTask = mock(Task.class); when(failingTask.run()).thenThrow(MigrationException.class); Migration migration1 = failingTask::run; Migration migration2 = mock(Migration.class); CassandraSchemaTransitions transitions = new CassandraSchemaTransitions(ImmutableMap.of( FROM_OLDER_TO_CURRENT, migration1, FROM_CURRENT_TO_LATEST, migration2)); testee = new CassandraMigrationService(schemaVersionDAO, transitions, version -> new MigrationTask(schemaVersionDAO, transitions, version), LATEST_VERSION); assertThatThrownBy(() -> testee.upgradeToVersion(LATEST_VERSION).run()) .isInstanceOf(MigrationException.class); verify(failingTask, times(1)).run(); verifyNoMoreInteractions(failingTask); verifyZeroInteractions(migration2); }