]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10002 add EditionManagementState#getInstallErrorMessage
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 18 Oct 2017 10:06:46 +0000 (12:06 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Mon, 23 Oct 2017 15:01:13 +0000 (08:01 -0700)
server/sonar-server/src/main/java/org/sonar/server/edition/EditionManagementState.java
server/sonar-server/src/main/java/org/sonar/server/edition/StandaloneEditionManagementStateImpl.java
server/sonar-server/src/test/java/org/sonar/server/edition/StandaloneEditionManagementStateImplTest.java

index bf14219a3cc6cb6610b00aa87c3fe81d0e1fce65..ee5210438be8e1774217142959888952a1b16033 100644 (file)
@@ -46,6 +46,14 @@ public interface EditionManagementState {
    */
   Optional<String> 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<String> getInstallErrorMessage();
+
   enum PendingStatus {
     NONE,
     AUTOMATIC_IN_PROGRESS,
index b9c691df4221d5fa568d8828bdead130b435766e..674859d15a115422f79d48ca48209b33f39ef0a9 100644 (file)
@@ -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<String, Optional<String>> 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<String> 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");
   }
index f1cbe67f43cd13bca30e07f597d1da22029d3bdc..d28a757d8d59ea1fe3ed04976c72f56725077f21 100644 (file)
@@ -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