diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-12-15 15:19:48 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-12-16 14:35:29 +0100 |
commit | 5801183df4cefa028241606f029c4576c6f26a6b (patch) | |
tree | 1aeb6179da23b676e10288949b43f6649a9afd05 /server/sonar-server | |
parent | 1c34d6ef83b6e9dbfada259c3111acbad3c1ff03 (diff) | |
download | sonarqube-5801183df4cefa028241606f029c4576c6f26a6b.tar.gz sonarqube-5801183df4cefa028241606f029c4576c6f26a6b.zip |
SONAR-8445 drop field DatabaseVersion.LAST_VERSION
replace usages of this field by either MigrationSteps.getMaxMigrationNumber() or DatabaseVersion.getStatus()
also, change return type of DatabaseVersion.getVersion from int to Optional<Long>
MigrationStep configuration is now avaiable at level2, in both web and CE container (because it is used by DatabaseVersion class)
Diffstat (limited to 'server/sonar-server')
13 files changed, 180 insertions, 107 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/DatabaseServerCompatibility.java b/server/sonar-server/src/main/java/org/sonar/server/platform/DatabaseServerCompatibility.java index ad44c4e2079..99fe6279fd0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/DatabaseServerCompatibility.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/DatabaseServerCompatibility.java @@ -19,6 +19,7 @@ */ package org.sonar.server.platform; +import java.util.Optional; import org.picocontainer.Startable; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.Loggers; @@ -42,8 +43,8 @@ public class DatabaseServerCompatibility implements Startable { throw MessageException.of("Database was upgraded to a more recent of SonarQube. Backup must probably be restored or db settings are incorrect."); } if (status == DatabaseVersion.Status.REQUIRES_UPGRADE) { - Integer currentVersion = this.version.getVersion(); - if (currentVersion != null && currentVersion < DatabaseVersion.MIN_UPGRADE_VERSION) { + Optional<Long> currentVersion = this.version.getVersion(); + if (currentVersion.isPresent() && currentVersion.get() < DatabaseVersion.MIN_UPGRADE_VERSION) { throw MessageException.of("Current version is too old. Please upgrade to Long Term Support version firstly."); } String msg = "Database must be upgraded. Please backup database and browse /setup"; diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerUpgradeStatus.java b/server/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerUpgradeStatus.java index 56b3df3f962..0a99d097d68 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerUpgradeStatus.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/DefaultServerUpgradeStatus.java @@ -19,10 +19,12 @@ */ package org.sonar.server.platform; +import java.util.Optional; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.picocontainer.Startable; import org.sonar.api.platform.ServerUpgradeStatus; +import org.sonar.server.platform.db.migration.step.MigrationSteps; import org.sonar.server.platform.db.migration.version.DatabaseVersion; /** @@ -31,18 +33,20 @@ import org.sonar.server.platform.db.migration.version.DatabaseVersion; public final class DefaultServerUpgradeStatus implements ServerUpgradeStatus, Startable { private final DatabaseVersion dbVersion; + private final MigrationSteps migrationSteps; // available when connected to db - private int initialDbVersion; + private long initialDbVersion; - public DefaultServerUpgradeStatus(DatabaseVersion dbVersion) { + public DefaultServerUpgradeStatus(DatabaseVersion dbVersion, MigrationSteps migrationSteps) { this.dbVersion = dbVersion; + this.migrationSteps = migrationSteps; } @Override public void start() { - Integer v = dbVersion.getVersion(); - this.initialDbVersion = v != null ? v : -1; + Optional<Long> v = dbVersion.getVersion(); + this.initialDbVersion = v.orElse(-1L); } @Override @@ -52,7 +56,7 @@ public final class DefaultServerUpgradeStatus implements ServerUpgradeStatus, St @Override public boolean isUpgraded() { - return !isFreshInstall() && (initialDbVersion < DatabaseVersion.LAST_VERSION); + return !isFreshInstall() && (initialDbVersion < migrationSteps.getMaxMigrationNumber()); } @Override @@ -62,7 +66,7 @@ public final class DefaultServerUpgradeStatus implements ServerUpgradeStatus, St @Override public int getInitialDbVersion() { - return initialDbVersion; + return (int) initialDbVersion; } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java index bd1cd454b1b..827c7c98f60 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel2.java @@ -24,15 +24,17 @@ import org.sonar.core.i18n.DefaultI18n; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.core.platform.PluginClassloaderFactory; import org.sonar.core.platform.PluginLoader; -import org.sonar.server.platform.db.migration.version.DatabaseVersion; import org.sonar.server.platform.DatabaseServerCompatibility; import org.sonar.server.platform.DefaultServerUpgradeStatus; import org.sonar.server.platform.StartupMetadataProvider; import org.sonar.server.platform.db.CheckDatabaseCharsetAtStartup; import org.sonar.server.platform.db.migration.DatabaseMigrationExecutorServiceImpl; import org.sonar.server.platform.db.migration.DatabaseMigrationStateImpl; +import org.sonar.server.platform.db.migration.MigrationConfigurationModule; import org.sonar.server.platform.db.migration.charset.DatabaseCharsetChecker; +import org.sonar.server.platform.db.migration.history.MigrationHistoryTable; import org.sonar.server.platform.db.migration.history.MigrationHistoryTableImpl; +import org.sonar.server.platform.db.migration.version.DatabaseVersion; import org.sonar.server.platform.web.RailsAppsDeployer; import org.sonar.server.plugins.InstalledPluginReferentialFactory; import org.sonar.server.plugins.ServerPluginJarExploder; @@ -49,6 +51,7 @@ public class PlatformLevel2 extends PlatformLevel { @Override protected void configureLevel() { add( + MigrationConfigurationModule.class, DatabaseVersion.class, DatabaseServerCompatibility.class, @@ -75,12 +78,20 @@ public class PlatformLevel2 extends PlatformLevel { // Migration state must be kept at level2 to survive moving in and then out of safe mode // ExecutorService must be kept at level2 because stopping it when stopping safe mode level causes error making SQ fail - add(DatabaseMigrationStateImpl.class, + add( + MigrationHistoryTableImpl.class, + DatabaseMigrationStateImpl.class, DatabaseMigrationExecutorServiceImpl.class); addIfStartupLeader( - MigrationHistoryTableImpl.class, DatabaseCharsetChecker.class, CheckDatabaseCharsetAtStartup.class); } + + @Override + public PlatformLevel start() { + // ensuring the HistoryTable exists must be the first thing done when this level is started + getOptional(MigrationHistoryTable.class).ifPresent(MigrationHistoryTable::start); + return super.start(); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java index e83661bdcaa..bd4b34496f5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java @@ -21,10 +21,9 @@ package org.sonar.server.platform.platformlevel; import org.sonar.server.organization.NoopDefaultOrganizationCache; import org.sonar.server.platform.ServerImpl; -import org.sonar.server.platform.db.migration.DatabaseMigrationImpl; import org.sonar.server.platform.db.migration.AutoDbMigration; +import org.sonar.server.platform.db.migration.DatabaseMigrationImpl; import org.sonar.server.platform.db.migration.MigrationEngineModule; -import org.sonar.server.platform.db.migration.version.DbVersionModule; import org.sonar.server.platform.web.WebPagesFilter; import org.sonar.server.platform.ws.DbMigrationStatusAction; import org.sonar.server.platform.ws.IndexAction; @@ -66,7 +65,6 @@ public class PlatformLevelSafeMode extends PlatformLevel { NoopDefaultOrganizationCache.class); add(DatabaseMigrationImpl.class); - add(DbVersionModule.class); addIfStartupLeader( MigrationEngineModule.class, AutoDbMigration.class); diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/DbMigrationStatusAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/DbMigrationStatusAction.java index 3e383c3ddc7..0d9959ce9bf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/DbMigrationStatusAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/DbMigrationStatusAction.java @@ -20,13 +20,14 @@ package org.sonar.server.platform.ws; import com.google.common.io.Resources; +import java.util.Optional; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.text.JsonWriter; import org.sonar.db.Database; -import org.sonar.server.platform.db.migration.version.DatabaseVersion; import org.sonar.server.platform.db.migration.DatabaseMigrationState; +import org.sonar.server.platform.db.migration.version.DatabaseVersion; import static com.google.common.base.Preconditions.checkState; import static org.sonar.server.platform.ws.DbMigrationJsonWriter.NO_CONNECTION_TO_DB; @@ -64,12 +65,13 @@ public class DbMigrationStatusAction implements SystemWsAction { @Override public void handle(Request request, Response response) throws Exception { - Integer currentVersion = databaseVersion.getVersion(); - checkState(currentVersion != null, NO_CONNECTION_TO_DB); + Optional<Long> currentVersion = databaseVersion.getVersion(); + checkState(currentVersion.isPresent(), NO_CONNECTION_TO_DB); JsonWriter json = response.newJsonWriter(); try { - if (currentVersion >= DatabaseVersion.LAST_VERSION) { + DatabaseVersion.Status status = databaseVersion.getStatus(); + if (status == DatabaseVersion.Status.UP_TO_DATE || status == DatabaseVersion.Status.REQUIRES_DOWNGRADE) { write(json, databaseMigrationState); } else if (!database.getDialect().supportsMigration()) { writeNotSupportedResponse(json); diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbAction.java index 029dd1dfd4d..720c7772714 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/MigrateDbAction.java @@ -20,6 +20,7 @@ package org.sonar.server.platform.ws; import com.google.common.io.Resources; +import java.util.Optional; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -72,12 +73,13 @@ public class MigrateDbAction implements SystemWsAction { @Override public void handle(Request request, Response response) throws Exception { - Integer currentVersion = databaseVersion.getVersion(); - checkState(currentVersion != null, NO_CONNECTION_TO_DB); + Optional<Long> currentVersion = databaseVersion.getVersion(); + checkState(currentVersion.isPresent(), NO_CONNECTION_TO_DB); JsonWriter json = response.newJsonWriter(); try { - if (currentVersion >= DatabaseVersion.LAST_VERSION) { + DatabaseVersion.Status status = databaseVersion.getStatus(); + if (status == DatabaseVersion.Status.UP_TO_DATE || status == DatabaseVersion.Status.REQUIRES_DOWNGRADE) { write(json, migrationState); } else if (!database.getDialect().supportsMigration()) { writeNotSupportedResponse(json); diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index de8a5bff789..15a6c001a08 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Optional; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.Plugin; @@ -308,21 +309,21 @@ public final class JRubyFacade { public boolean isSonarAccessAllowed() { ComponentContainer container = Platform.getInstance().getContainer(); DatabaseMigrationState databaseMigrationState = container.getComponentByType(DatabaseMigrationState.class); - Status status = databaseMigrationState.getStatus(); - if (status == Status.RUNNING - || status == Status.FAILED) { + Status migrationStatus = databaseMigrationState.getStatus(); + if (migrationStatus == Status.RUNNING || migrationStatus == Status.FAILED) { return false; } - if (status == Status.SUCCEEDED) { + if (migrationStatus == Status.SUCCEEDED) { return true; } DatabaseVersion databaseVersion = container.getComponentByType(DatabaseVersion.class); - Integer currentVersion = databaseVersion.getVersion(); - if (currentVersion == null) { + Optional<Long> currentVersion = databaseVersion.getVersion(); + if (!currentVersion.isPresent()) { throw new IllegalStateException("Version can not be retrieved from Database. Database is either blank or corrupted"); } - if (currentVersion >= DatabaseVersion.LAST_VERSION) { + DatabaseVersion.Status status = databaseVersion.getStatus(); + if (status == DatabaseVersion.Status.UP_TO_DATE || status == DatabaseVersion.Status.REQUIRES_DOWNGRADE) { return true; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/DatabaseServerCompatibilityTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/DatabaseServerCompatibilityTest.java index 687e25860a8..09cbe5e2b8d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/DatabaseServerCompatibilityTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/DatabaseServerCompatibilityTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.platform; +import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -55,7 +56,7 @@ public class DatabaseServerCompatibilityTest { DatabaseVersion version = mock(DatabaseVersion.class); when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE); - when(version.getVersion()).thenReturn(12); + when(version.getVersion()).thenReturn(Optional.of(12L)); new DatabaseServerCompatibility(version).start(); } @@ -63,7 +64,7 @@ public class DatabaseServerCompatibilityTest { public void log_warning_if_requires_upgrade() { DatabaseVersion version = mock(DatabaseVersion.class); when(version.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_UPGRADE); - when(version.getVersion()).thenReturn(DatabaseVersion.MIN_UPGRADE_VERSION); + when(version.getVersion()).thenReturn(Optional.of(DatabaseVersion.MIN_UPGRADE_VERSION)); new DatabaseServerCompatibility(version).start(); assertThat(logTester.logs()).hasSize(2); diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/DefaultServerUpgradeStatusTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/DefaultServerUpgradeStatusTest.java index 4b7f2fca8d6..256809b7dbd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/DefaultServerUpgradeStatusTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/DefaultServerUpgradeStatusTest.java @@ -19,52 +19,58 @@ */ package org.sonar.server.platform; +import java.util.Optional; +import org.junit.Before; import org.junit.Test; +import org.sonar.server.platform.db.migration.step.MigrationSteps; import org.sonar.server.platform.db.migration.version.DatabaseVersion; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class DefaultServerUpgradeStatusTest { + private static final long LAST_VERSION = 150; + private MigrationSteps migrationSteps = mock(MigrationSteps.class); + private DatabaseVersion dbVersion = mock(DatabaseVersion.class); + private DefaultServerUpgradeStatus underTest = new DefaultServerUpgradeStatus(dbVersion, migrationSteps); + + @Before + public void setUp() throws Exception { + when(migrationSteps.getMaxMigrationNumber()).thenReturn(LAST_VERSION); + } @Test public void shouldBeFreshInstallation() { - DatabaseVersion dbVersion = mock(DatabaseVersion.class); - when(dbVersion.getVersion()).thenReturn(null); + when(migrationSteps.getMaxMigrationNumber()).thenReturn(150L); + when(dbVersion.getVersion()).thenReturn(Optional.empty()); - DefaultServerUpgradeStatus status = new DefaultServerUpgradeStatus(dbVersion); - status.start(); + underTest.start(); - assertThat(status.isFreshInstall(), is(true)); - assertThat(status.isUpgraded(), is(false)); - assertThat(status.getInitialDbVersion(), is(-1)); + assertThat(underTest.isFreshInstall()).isTrue(); + assertThat(underTest.isUpgraded()).isFalse(); + assertThat(underTest.getInitialDbVersion()).isEqualTo(-1); } @Test public void shouldBeUpgraded() { - DatabaseVersion dbVersion = mock(DatabaseVersion.class); - when(dbVersion.getVersion()).thenReturn(50); + when(dbVersion.getVersion()).thenReturn(Optional.of(50L)); - DefaultServerUpgradeStatus status = new DefaultServerUpgradeStatus(dbVersion); - status.start(); + underTest.start(); - assertThat(status.isFreshInstall(), is(false)); - assertThat(status.isUpgraded(), is(true)); - assertThat(status.getInitialDbVersion(), is(50)); + assertThat(underTest.isFreshInstall()).isFalse(); + assertThat(underTest.isUpgraded()).isTrue(); + assertThat(underTest.getInitialDbVersion()).isEqualTo(50); } @Test public void shouldNotBeUpgraded() { - DatabaseVersion dbVersion = mock(DatabaseVersion.class); - when(dbVersion.getVersion()).thenReturn(DatabaseVersion.LAST_VERSION); + when(dbVersion.getVersion()).thenReturn(Optional.of(LAST_VERSION)); - DefaultServerUpgradeStatus status = new DefaultServerUpgradeStatus(dbVersion); - status.start(); + underTest.start(); - assertThat(status.isFreshInstall(), is(false)); - assertThat(status.isUpgraded(), is(false)); - assertThat(status.getInitialDbVersion(), is(DatabaseVersion.LAST_VERSION)); + assertThat(underTest.isFreshInstall()).isFalse(); + assertThat(underTest.isUpgraded()).isFalse(); + assertThat(underTest.getInitialDbVersion()).isEqualTo((int) LAST_VERSION); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseMonitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseMonitorTest.java index 228b19650e4..3210faf6799 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseMonitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseMonitorTest.java @@ -20,6 +20,7 @@ package org.sonar.server.platform.monitoring; import java.util.Map; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; @@ -27,13 +28,21 @@ import org.sonar.db.DbTester; import org.sonar.server.platform.db.migration.version.DatabaseVersion; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class DatabaseMonitorTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - private DatabaseMonitor underTest = new DatabaseMonitor(new DatabaseVersion(dbTester.getDbClient()), dbTester.getDbClient()); + private DatabaseVersion databaseVersion = mock(DatabaseVersion.class); + private DatabaseMonitor underTest = new DatabaseMonitor(databaseVersion, dbTester.getDbClient()); + + @Before + public void setUp() throws Exception { + when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE); + } @Test public void name_is_not_empty() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java b/server/sonar-server/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java index 568745297f5..1cff26eb3e4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/platformlevel/PlatformLevel2Test.java @@ -52,10 +52,10 @@ public class PlatformLevel2Test { @Test public void add_all_components_by_default() { PlatformLevel1 level1 = new PlatformLevel1(mock(Platform.class), props); - level1.configureLevel(); + level1.configure(); PlatformLevel2 underTest = new PlatformLevel2(level1); - underTest.configureLevel(); + underTest.configure(); // some level1 components assertThat(underTest.getOptional(Cluster.class)).isPresent(); @@ -72,10 +72,10 @@ public class PlatformLevel2Test { public void do_not_add_all_components_when_startup_follower() { props.setProperty("sonar.cluster.enabled", "true"); PlatformLevel1 level1 = new PlatformLevel1(mock(Platform.class), props); - level1.configureLevel(); + level1.configure(); PlatformLevel2 underTest = new PlatformLevel2(level1); - underTest.configureLevel(); + underTest.configure(); assertThat(underTest.get(Cluster.class).isStartupLeader()).isFalse(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/DbMigrationStatusActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/DbMigrationStatusActionTest.java index b5c833e7d1f..e7469eca294 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/DbMigrationStatusActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/DbMigrationStatusActionTest.java @@ -20,13 +20,18 @@ package org.sonar.server.platform.ws; import com.google.common.collect.ImmutableList; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.Arrays; import java.util.Date; +import java.util.Optional; import javax.annotation.Nullable; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import org.sonar.api.server.ws.Request; import org.sonar.api.utils.DateUtils; import org.sonar.db.Database; @@ -40,6 +45,7 @@ import static com.google.common.base.Predicates.in; import static com.google.common.base.Predicates.not; import static com.google.common.collect.Iterables.filter; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; import static org.mockito.Mockito.when; import static org.sonar.server.platform.db.migration.DatabaseMigrationState.Status.FAILED; import static org.sonar.server.platform.db.migration.DatabaseMigrationState.Status.NONE; @@ -58,6 +64,7 @@ import static org.sonar.server.platform.ws.DbMigrationJsonWriter.STATUS_NOT_SUPP import static org.sonar.server.platform.ws.DbMigrationJsonWriter.STATUS_NO_MIGRATION; import static org.sonar.test.JsonAssert.assertJson; +@RunWith(DataProviderRunner.class) public class DbMigrationStatusActionTest { @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -65,9 +72,6 @@ public class DbMigrationStatusActionTest { private static final Date SOME_DATE = new Date(); private static final String SOME_THROWABLE_MSG = "blablabla pop !"; private static final String DEFAULT_ERROR_MSG = "No failure error"; - private static final int CURRENT_VERSION = DatabaseVersion.LAST_VERSION; - private static final int OLD_VERSION = CURRENT_VERSION - 1; - private static final int NEWER_VERSION = CURRENT_VERSION + 1; private DatabaseVersion databaseVersion = mock(DatabaseVersion.class); private Database database = mock(Database.class); @@ -81,11 +85,11 @@ public class DbMigrationStatusActionTest { @Before public void wireMocksTogether() { when(database.getDialect()).thenReturn(dialect); + when(databaseVersion.getVersion()).thenReturn(Optional.of(150L)); } @Test public void verify_example() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(RUNNING); when(migrationState.getStartedAt()).thenReturn(DateUtils.parseDateTime("2015-02-23T18:54:23+0100")); @@ -95,8 +99,9 @@ public class DbMigrationStatusActionTest { } @Test - public void throws_ISE_when_databaseVersion_can_not_be_determined() throws Exception { - when(databaseVersion.getVersion()).thenReturn(null); + public void throws_ISE_when_database_has_no_version() throws Exception { + reset(database); + when(databaseVersion.getVersion()).thenReturn(Optional.empty()); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Cannot connect to Database."); @@ -105,8 +110,8 @@ public class DbMigrationStatusActionTest { } @Test - public void msg_is_operational_and_state_from_databasemigration_when_databaseversion_is_equal_to_currentversion() throws Exception { - when(databaseVersion.getVersion()).thenReturn(CURRENT_VERSION); + public void msg_is_operational_and_state_from_databasemigration_when_databaseversion_status_is_UP_TO_DATE() throws Exception { + when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE); when(migrationState.getStatus()).thenReturn(NONE); underTest.handle(request, response); @@ -116,18 +121,19 @@ public class DbMigrationStatusActionTest { // this test will raise a IllegalArgumentException when an unsupported value is added to the Status enum @Test - public void defensive_test_all_values_of_Status_must_be_supported() throws Exception { - for (Status status : filter(Arrays.asList(DatabaseMigrationState.Status.values()), not(in(ImmutableList.of(NONE, RUNNING, FAILED, SUCCEEDED))))) { - when(databaseVersion.getVersion()).thenReturn(CURRENT_VERSION); - when(migrationState.getStatus()).thenReturn(status); + @UseDataProvider("statusRequiringDbMigration") + public void defensive_test_all_values_of_Status_must_be_supported(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); + for (Status migrationStatus : filter(Arrays.asList(DatabaseMigrationState.Status.values()), not(in(ImmutableList.of(NONE, RUNNING, FAILED, SUCCEEDED))))) { + when(migrationState.getStatus()).thenReturn(migrationStatus); underTest.handle(request, response); } } @Test - public void state_from_databasemigration_when_databaseversion_greater_than_currentversion() throws Exception { - when(databaseVersion.getVersion()).thenReturn(NEWER_VERSION); + public void state_from_databasemigration_when_databaseversion_status_is_REQUIRES_DOWNGRADE() throws Exception { + when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE); when(migrationState.getStatus()).thenReturn(NONE); underTest.handle(request, response); @@ -136,8 +142,9 @@ public class DbMigrationStatusActionTest { } @Test - public void state_is_NONE_with_specific_msg_when_version_is_less_than_current_version_and_dialect_does_not_support_migration() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_is_NONE_with_specific_msg_when_db_requires_upgrade_but_dialect_does_not_support_migration(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(false); underTest.handle(request, response); @@ -146,8 +153,9 @@ public class DbMigrationStatusActionTest { } @Test - public void state_from_databasemigration_when_dbmigration_status_is_RUNNING() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_from_database_migration_when_dbmigration_status_is_RUNNING(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(RUNNING); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -158,8 +166,9 @@ public class DbMigrationStatusActionTest { } @Test - public void state_from_databasemigration_and_msg_includes_error_when_dbmigration_status_is_FAILED() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_from_database_migration_and_msg_includes_error_when_dbmigration_status_is_FAILED(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(FAILED); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -171,8 +180,9 @@ public class DbMigrationStatusActionTest { } @Test - public void state_from_databasemigration_and_msg_has_default_msg_when_dbmigration_status_is_FAILED() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_from_database_migration_and_msg_has_default_msg_when_dbmigration_status_is_FAILED(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(FAILED); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -184,8 +194,9 @@ public class DbMigrationStatusActionTest { } @Test - public void state_from_databasemigration_and_msg_has_default_msg_when_dbmigration_status_is_SUCCEEDED() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_from_database_migration_and_msg_has_default_msg_when_dbmigration_status_is_SUCCEEDED(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(SUCCEEDED); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -196,8 +207,9 @@ public class DbMigrationStatusActionTest { } @Test - public void start_migration_and_return_state_from_databasemigration_when_dbmigration_status_is_NONE() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void start_migration_and_return_state_from_databasemigration_when_dbmigration_status_is_NONE(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(NONE); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -207,6 +219,14 @@ public class DbMigrationStatusActionTest { assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_MIGRATION_REQUIRED, MESSAGE_MIGRATION_REQUIRED)); } + @DataProvider + public static Object[][] statusRequiringDbMigration() { + return new Object[][] { + { DatabaseVersion.Status.FRESH_INSTALL }, + { DatabaseVersion.Status.REQUIRES_UPGRADE }, + }; + } + private static String failedMsg(@Nullable String t) { return "Migration failed: " + t + ".<br/> Please check logs."; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java index 64ef74ad62d..06a76dc1279 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/MigrateDbActionTest.java @@ -20,13 +20,18 @@ package org.sonar.server.platform.ws; import com.google.common.collect.ImmutableList; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.Arrays; import java.util.Date; +import java.util.Optional; import javax.annotation.Nullable; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import org.sonar.api.server.ws.Request; import org.sonar.api.utils.DateUtils; import org.sonar.db.Database; @@ -41,6 +46,7 @@ import static com.google.common.base.Predicates.in; import static com.google.common.base.Predicates.not; import static com.google.common.collect.Iterables.filter; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.sonar.server.platform.db.migration.DatabaseMigrationState.Status.FAILED; @@ -49,6 +55,7 @@ import static org.sonar.server.platform.db.migration.DatabaseMigrationState.Stat import static org.sonar.server.platform.db.migration.DatabaseMigrationState.Status.SUCCEEDED; import static org.sonar.test.JsonAssert.assertJson; +@RunWith(DataProviderRunner.class) public class MigrateDbActionTest { @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -56,9 +63,6 @@ public class MigrateDbActionTest { private static final Date SOME_DATE = new Date(); private static final String SOME_THROWABLE_MSG = "blablabla pop !"; private static final String DEFAULT_ERROR_MSG = "No failure error"; - private static final int CURRENT_VERSION = DatabaseVersion.LAST_VERSION; - private static final int OLD_VERSION = CURRENT_VERSION - 1; - private static final int NEWER_VERSION = CURRENT_VERSION + 1; private static final String STATUS_NO_MIGRATION = "NO_MIGRATION"; private static final String STATUS_NOT_SUPPORTED = "NOT_SUPPORTED"; @@ -84,11 +88,13 @@ public class MigrateDbActionTest { @Before public void wireMocksTogether() { when(database.getDialect()).thenReturn(dialect); + when(databaseVersion.getVersion()).thenReturn(Optional.of(150L)); } @Test public void ISE_is_thrown_when_version_can_not_be_retrieved_from_database() throws Exception { - when(databaseVersion.getVersion()).thenReturn(null); + reset(databaseVersion); + when(databaseVersion.getVersion()).thenReturn(Optional.empty()); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Cannot connect to Database."); @@ -98,7 +104,6 @@ public class MigrateDbActionTest { @Test public void verify_example() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(RUNNING); when(migrationState.getStartedAt()).thenReturn(DateUtils.parseDateTime("2015-02-23T18:54:23+0100")); @@ -108,8 +113,8 @@ public class MigrateDbActionTest { } @Test - public void msg_is_operational_and_state_from_database_migration_when_database_version_is_equal_to_current_version() throws Exception { - when(databaseVersion.getVersion()).thenReturn(CURRENT_VERSION); + public void msg_is_operational_and_state_from_database_migration_when_databaseversion_status_is_UP_TO_DATE() throws Exception { + when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE); when(migrationState.getStatus()).thenReturn(NONE); underTest.handle(request, response); @@ -119,9 +124,8 @@ public class MigrateDbActionTest { // this test will raise a IllegalArgumentException when an unsupported value is added to the Status enum @Test - public void defensive_test_all_values_of_Status_must_be_supported() throws Exception { + public void defensive_test_all_values_of_migration_Status_must_be_supported() throws Exception { for (Status status : filter(Arrays.asList(DatabaseMigrationState.Status.values()), not(in(ImmutableList.of(NONE, RUNNING, FAILED, SUCCEEDED))))) { - when(databaseVersion.getVersion()).thenReturn(CURRENT_VERSION); when(migrationState.getStatus()).thenReturn(status); underTest.handle(request, response); @@ -129,8 +133,8 @@ public class MigrateDbActionTest { } @Test - public void state_from_database_migration_when_databaseversion_greater_than_currentversion() throws Exception { - when(databaseVersion.getVersion()).thenReturn(NEWER_VERSION); + public void state_from_database_migration_when_databaseversion_status_is_REQUIRES_DOWNGRADE() throws Exception { + when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE); when(migrationState.getStatus()).thenReturn(NONE); underTest.handle(request, response); @@ -139,8 +143,9 @@ public class MigrateDbActionTest { } @Test - public void state_is_NONE_with_specific_msg_when_version_is_less_than_current_version_and_dialect_does_not_support_migration() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_is_NONE_with_specific_msg_when_db_requires_upgrade_but_dialect_does_not_support_migration(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(false); underTest.handle(request, response); @@ -149,8 +154,9 @@ public class MigrateDbActionTest { } @Test - public void state_from_database_migration_when_dbmigration_status_is_RUNNING() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_from_database_migration_when_dbmigration_status_is_RUNNING(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(RUNNING); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -161,8 +167,9 @@ public class MigrateDbActionTest { } @Test - public void state_from_database_migration_and_msg_includes_error_when_dbmigration_status_is_FAILED() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_from_database_migration_and_msg_includes_error_when_dbmigration_status_is_FAILED(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(FAILED); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -174,8 +181,9 @@ public class MigrateDbActionTest { } @Test - public void state_from_database_migration_and_msg_has_default_msg_when_dbmigration_status_is_FAILED() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_from_database_migration_and_msg_has_default_msg_when_dbmigration_status_is_FAILED(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(FAILED); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -187,8 +195,9 @@ public class MigrateDbActionTest { } @Test - public void state_from_database_migration_and_msg_has_default_msg_when_dbmigration_status_is_SUCCEEDED() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void state_from_database_migration_and_msg_has_default_msg_when_dbmigration_status_is_SUCCEEDED(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(SUCCEEDED); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -199,8 +208,9 @@ public class MigrateDbActionTest { } @Test - public void start_migration_and_return_state_from_database_migration_when_dbmigration_status_is_NONE() throws Exception { - when(databaseVersion.getVersion()).thenReturn(OLD_VERSION); + @UseDataProvider("statusRequiringDbMigration") + public void start_migration_and_return_state_from_databasemigration_when_dbmigration_status_is_NONE(DatabaseVersion.Status status) throws Exception { + when(databaseVersion.getStatus()).thenReturn(status); when(dialect.supportsMigration()).thenReturn(true); when(migrationState.getStatus()).thenReturn(NONE); when(migrationState.getStartedAt()).thenReturn(SOME_DATE); @@ -211,6 +221,14 @@ public class MigrateDbActionTest { assertJson(response.outputAsString()).isSimilarTo(expectedResponse(STATUS_MIGRATION_RUNNING, MESSAGE_STATUS_RUNNING, SOME_DATE)); } + @DataProvider + public static Object[][] statusRequiringDbMigration() { + return new Object[][] { + { DatabaseVersion.Status.FRESH_INSTALL }, + { DatabaseVersion.Status.REQUIRES_UPGRADE }, + }; + } + private static String failedMsg(@Nullable String t) { return "Migration failed: " + t + ".<br/> Please check logs."; } |