From e450a986a1b8f51f681d0fb0d84520b0a5c7784c Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 18 Oct 2017 12:06:46 +0200 Subject: [PATCH] SONAR-10002 add EditionManagementState#getInstallErrorMessage --- .../edition/EditionManagementState.java | 8 ++ .../StandaloneEditionManagementStateImpl.java | 40 ++++-- ...ndaloneEditionManagementStateImplTest.java | 122 ++++++++++++++++-- 3 files changed, 146 insertions(+), 24 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/edition/EditionManagementState.java b/server/sonar-server/src/main/java/org/sonar/server/edition/EditionManagementState.java index bf14219a3cc..ee5210438be 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/edition/EditionManagementState.java +++ b/server/sonar-server/src/main/java/org/sonar/server/edition/EditionManagementState.java @@ -46,6 +46,14 @@ public interface EditionManagementState { */ Optional getPendingLicense(); + /** + * The message explaining the error that made the install fail (if any). + * + * @return a {@link String} if {@link #getPendingInstallationStatus()} returns {@link PendingStatus#NONE} and an error + * occurred during install, otherwise {@link Optional#empty() empty} + */ + Optional getInstallErrorMessage(); + enum PendingStatus { NONE, AUTOMATIC_IN_PROGRESS, diff --git a/server/sonar-server/src/main/java/org/sonar/server/edition/StandaloneEditionManagementStateImpl.java b/server/sonar-server/src/main/java/org/sonar/server/edition/StandaloneEditionManagementStateImpl.java index b9c691df422..674859d15a1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/edition/StandaloneEditionManagementStateImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/edition/StandaloneEditionManagementStateImpl.java @@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableSet; import java.util.Arrays; import java.util.Map; import java.util.Optional; +import javax.annotation.Nullable; import org.picocontainer.Startable; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -43,12 +44,14 @@ public class StandaloneEditionManagementStateImpl implements MutableEditionManag private static final String PENDING_INSTALLATION_STATUS = "pendingInstallStatus"; private static final String PENDING_EDITION_KEY = "pendingEditionKey"; private static final String PENDING_LICENSE = "pendingLicense"; + private static final String INSTALL_ERROR_MESSAGE = "installError"; private final DbClient dbClient; private String currentEditionKey; private PendingStatus pendingInstallationStatus; private String pendingEditionKey; private String pendingLicense; + private String installErrorMessage; public StandaloneEditionManagementStateImpl(DbClient dbClient) { this.dbClient = dbClient; @@ -59,7 +62,7 @@ public class StandaloneEditionManagementStateImpl implements MutableEditionManag try (DbSession dbSession = dbClient.openSession(false)) { // load current state value Map> internalPropertyValues = dbClient.internalPropertiesDao().selectByKeys(dbSession, - ImmutableSet.of(CURRENT_EDITION_KEY, PENDING_INSTALLATION_STATUS, PENDING_EDITION_KEY, PENDING_LICENSE)); + ImmutableSet.of(CURRENT_EDITION_KEY, PENDING_INSTALLATION_STATUS, PENDING_EDITION_KEY, PENDING_LICENSE, INSTALL_ERROR_MESSAGE)); this.currentEditionKey = internalPropertyValues.getOrDefault(CURRENT_EDITION_KEY, empty()) .map(StandaloneEditionManagementStateImpl::emptyToNull) .orElse(null); @@ -73,6 +76,9 @@ public class StandaloneEditionManagementStateImpl implements MutableEditionManag this.pendingLicense = internalPropertyValues.getOrDefault(PENDING_LICENSE, empty()) .map(StandaloneEditionManagementStateImpl::emptyToNull) .orElse(null); + this.installErrorMessage = internalPropertyValues.getOrDefault(INSTALL_ERROR_MESSAGE, empty()) + .map(StandaloneEditionManagementStateImpl::emptyToNull) + .orElse(null); } } @@ -105,6 +111,12 @@ public class StandaloneEditionManagementStateImpl implements MutableEditionManag return Optional.ofNullable(pendingLicense); } + @Override + public Optional getInstallErrorMessage() { + ensureStarted(); + return Optional.ofNullable(installErrorMessage); + } + @Override public synchronized PendingStatus startAutomaticInstall(License license) { ensureStarted(); @@ -188,23 +200,23 @@ public class StandaloneEditionManagementStateImpl implements MutableEditionManag private void persistProperties() { try (DbSession dbSession = dbClient.openSession(false)) { InternalPropertiesDao internalPropertiesDao = dbClient.internalPropertiesDao(); - if (pendingInstallationStatus == NONE || pendingInstallationStatus == UNINSTALL_IN_PROGRESS) { - internalPropertiesDao.saveAsEmpty(dbSession, PENDING_EDITION_KEY); - internalPropertiesDao.saveAsEmpty(dbSession, PENDING_LICENSE); - } else { - internalPropertiesDao.save(dbSession, PENDING_EDITION_KEY, pendingEditionKey); - internalPropertiesDao.save(dbSession, PENDING_LICENSE, pendingLicense); - } - if (currentEditionKey == null) { - internalPropertiesDao.saveAsEmpty(dbSession, CURRENT_EDITION_KEY); - } else { - internalPropertiesDao.save(dbSession, CURRENT_EDITION_KEY, currentEditionKey); - } - internalPropertiesDao.save(dbSession, PENDING_INSTALLATION_STATUS, pendingInstallationStatus.name()); + saveInternalProperty(internalPropertiesDao, dbSession, PENDING_EDITION_KEY, pendingEditionKey); + saveInternalProperty(internalPropertiesDao, dbSession, PENDING_LICENSE, pendingLicense); + saveInternalProperty(internalPropertiesDao, dbSession, INSTALL_ERROR_MESSAGE, installErrorMessage); + saveInternalProperty(internalPropertiesDao, dbSession, CURRENT_EDITION_KEY, currentEditionKey); + saveInternalProperty(internalPropertiesDao, dbSession, PENDING_INSTALLATION_STATUS, pendingInstallationStatus.name()); dbSession.commit(); } } + private static void saveInternalProperty(InternalPropertiesDao dao, DbSession dbSession, String key, @Nullable String value) { + if (value == null) { + dao.saveAsEmpty(dbSession, key); + } else { + dao.save(dbSession, key, value); + } + } + private static void checkLicense(License license) { requireNonNull(license, "license can't be null"); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/edition/StandaloneEditionManagementStateImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/edition/StandaloneEditionManagementStateImplTest.java index f1cbe67f43c..d28a757d8d5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/edition/StandaloneEditionManagementStateImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/edition/StandaloneEditionManagementStateImplTest.java @@ -183,6 +183,37 @@ public class StandaloneEditionManagementStateImplTest { assertThat(underTest.getPendingLicense()).isEmpty(); } + @Test + public void getInstallErrorMessage_fails_with_ISE_if_start_has_not_been_called() { + expectISENotStarted(); + + underTest.getInstallErrorMessage(); + } + + @Test + public void getInstallErrorMessage_returns_empty_when_internal_properties_table_is_empty() { + underTest.start(); + + assertThat(underTest.getInstallErrorMessage()).isEmpty(); + } + + @Test + public void getInstallErrorMessage_returns_value_in_db_for_key_pendingEditionKey() { + String value = randomAlphanumeric(10); + dbTester.properties().insertInternal("installError", value); + underTest.start(); + + assertThat(underTest.getInstallErrorMessage()).contains(value); + } + + @Test + public void getInstallErrorMessage_returns_empty_when_value_in_db_is_empty_for_key_pendingEditionKey() { + dbTester.properties().insertEmptyInternal("installError"); + underTest.start(); + + assertThat(underTest.getInstallErrorMessage()).isEmpty(); + } + @Test public void startAutomaticInstall_fails_with_ISE_if_not_started() { expectISENotStarted(); @@ -277,6 +308,18 @@ public class StandaloneEditionManagementStateImplTest { underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS); } + @Test + public void startAutomaticInstall_fails_with_ISE_if_called_after_uninstall() { + underTest.start(); + underTest.newEditionWithoutInstall("foo"); + underTest.uninstall(); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Can't move to AUTOMATIC_IN_PROGRESS when status is UNINSTALL_IN_PROGRESS (should be any of [NONE])"); + + underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS); + } + @Test public void uninstall_fails_with_ISE_if_not_started() { expectISENotStarted(); @@ -455,6 +498,18 @@ public class StandaloneEditionManagementStateImplTest { underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS); } + @Test + public void startManualInstall_fails_with_ISE_if_called_after_uninstall() { + underTest.start(); + underTest.newEditionWithoutInstall("foo"); + underTest.uninstall(); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Can't move to MANUAL_IN_PROGRESS when status is UNINSTALL_IN_PROGRESS (should be any of [NONE])"); + + underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS); + } + @Test public void automaticInstallReady_fails_with_ISE_if_not_started() { expectISENotStarted(); @@ -483,6 +538,40 @@ public class StandaloneEditionManagementStateImplTest { underTest.automaticInstallReady(); } + @Test + public void automaticInstallReady_fails_with_ISE_if_called_after_newEditionWithoutInstall() { + underTest.start(); + underTest.newEditionWithoutInstall("foo"); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Can't move to AUTOMATIC_READY when status is NONE (should be any of [AUTOMATIC_IN_PROGRESS])"); + + underTest.automaticInstallReady(); + } + + @Test + public void automaticInstallReady_fails_with_ISE_if_called_after_uninstall() { + underTest.start(); + underTest.newEditionWithoutInstall("foo"); + underTest.uninstall(); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Can't move to AUTOMATIC_READY when status is UNINSTALL_IN_PROGRESS (should be any of [AUTOMATIC_IN_PROGRESS])"); + + underTest.automaticInstallReady(); + } + + @Test + public void automaticInstallReady_fails_with_ISE_if_called_after_startManualInstall() { + underTest.start(); + underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Can't move to AUTOMATIC_READY when status is MANUAL_IN_PROGRESS (should be any of [AUTOMATIC_IN_PROGRESS])"); + + underTest.automaticInstallReady(); + } + @Test public void automaticInstallReady_after_startAutomaticInstall_changes_status_to_AUTOMATIC_READY_but_does_not_change_editions() { underTest.start(); @@ -592,6 +681,19 @@ public class StandaloneEditionManagementStateImplTest { underTest.newEditionWithoutInstall(newEditionKey); } + @Test + public void newEditionWithoutInstall_fails_with_ISE_if_called_after_uninstall() { + String newEditionKey = randomAlphanumeric(3); + underTest.start(); + underTest.newEditionWithoutInstall("foo"); + underTest.uninstall(); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Can't move to NONE when status is UNINSTALL_IN_PROGRESS (should be any of [NONE])"); + + underTest.newEditionWithoutInstall(newEditionKey); + } + @Test public void finalizeInstallation_fails_with_ISE_if_not_started() { expectISENotStarted(); @@ -661,11 +763,11 @@ public class StandaloneEditionManagementStateImplTest { } @Test - public void finalizeInstallation_set_new_edition_and_clear_pending_fields_after_uninstall() { - underTest.start(); + public void finalizeInstallation_overwrites_current_edition_and_clear_pending_fields_after_startManualInstall() { String value = randomAlphanumeric(10); - underTest.newEditionWithoutInstall(value); - underTest.uninstall(); + dbTester.properties().insertInternal("currentEditionKey", value); + underTest.start(); + underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS); PendingStatus newStatus = underTest.finalizeInstallation(); @@ -673,15 +775,15 @@ public class StandaloneEditionManagementStateImplTest { assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE); assertThat(underTest.getPendingEditionKey()).isEmpty(); assertThat(underTest.getPendingLicense()).isEmpty(); - assertThat(underTest.getCurrentEditionKey()).isEmpty(); + assertThat(underTest.getCurrentEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey()); } @Test - public void finalizeInstallation_overwrites_current_edition_and_clear_pending_fields_after_startManualInstall() { - String value = randomAlphanumeric(10); - dbTester.properties().insertInternal("currentEditionKey", value); + public void finalizeInstallation_set_new_edition_and_clear_pending_fields_after_uninstall() { underTest.start(); - underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS); + String value = randomAlphanumeric(10); + underTest.newEditionWithoutInstall(value); + underTest.uninstall(); PendingStatus newStatus = underTest.finalizeInstallation(); @@ -689,7 +791,7 @@ public class StandaloneEditionManagementStateImplTest { assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE); assertThat(underTest.getPendingEditionKey()).isEmpty(); assertThat(underTest.getPendingLicense()).isEmpty(); - assertThat(underTest.getCurrentEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey()); + assertThat(underTest.getCurrentEditionKey()).isEmpty(); } @Test -- 2.39.5