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;
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;
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);
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);
}
}
return Optional.ofNullable(pendingLicense);
}
+ @Override
+ public Optional<String> getInstallErrorMessage() {
+ ensureStarted();
+ return Optional.ofNullable(installErrorMessage);
+ }
+
@Override
public synchronized PendingStatus startAutomaticInstall(License license) {
ensureStarted();
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");
}
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();
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();
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();
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();
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();
}
@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();
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();
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