import static org.sonar.server.edition.EditionManagementState.PendingStatus.AUTOMATIC_READY;
import static org.sonar.server.edition.EditionManagementState.PendingStatus.MANUAL_IN_PROGRESS;
import static org.sonar.server.edition.EditionManagementState.PendingStatus.NONE;
+import static org.sonar.server.edition.EditionManagementState.PendingStatus.UNINSTALL_IN_PROGRESS;
public class StandaloneEditionManagementStateImpl implements MutableEditionManagementState, Startable {
private static final String CURRENT_EDITION_KEY = "currentEditionKey";
@Override
public synchronized PendingStatus finalizeInstallation() {
ensureStarted();
- changeStatusToFrom(NONE, AUTOMATIC_READY, MANUAL_IN_PROGRESS);
+ changeStatusToFrom(NONE, AUTOMATIC_READY, MANUAL_IN_PROGRESS, UNINSTALL_IN_PROGRESS);
this.pendingInstallationStatus = NONE;
this.currentEditionKey = this.pendingEditionKey;
return this.pendingInstallationStatus;
}
+ @Override
+ public synchronized PendingStatus uninstall() {
+ ensureStarted();
+ changeStatusToFrom(UNINSTALL_IN_PROGRESS, NONE);
+ checkState(currentEditionKey != null, "There is no edition currently installed");
+
+ this.pendingInstallationStatus = UNINSTALL_IN_PROGRESS;
+ this.pendingEditionKey = null;
+ this.pendingLicense = null;
+ this.currentEditionKey = null;
+ persistProperties();
+ return this.pendingInstallationStatus;
+ }
+
private void ensureStarted() {
checkState(pendingInstallationStatus != null, "%s is not started", getClass().getSimpleName());
}
private void persistProperties() {
try (DbSession dbSession = dbClient.openSession(false)) {
InternalPropertiesDao internalPropertiesDao = dbClient.internalPropertiesDao();
- if (pendingInstallationStatus == NONE) {
+ if (pendingInstallationStatus == NONE || pendingInstallationStatus == UNINSTALL_IN_PROGRESS) {
internalPropertiesDao.saveAsEmpty(dbSession, PENDING_EDITION_KEY);
internalPropertiesDao.saveAsEmpty(dbSession, PENDING_LICENSE);
} else {
import static org.sonar.server.edition.EditionManagementState.PendingStatus.AUTOMATIC_READY;
import static org.sonar.server.edition.EditionManagementState.PendingStatus.MANUAL_IN_PROGRESS;
import static org.sonar.server.edition.EditionManagementState.PendingStatus.NONE;
+import static org.sonar.server.edition.EditionManagementState.PendingStatus.UNINSTALL_IN_PROGRESS;
public class StandaloneEditionManagementStateImplTest {
private static final License LICENSE_WITHOUT_PLUGINS = new License(randomAlphanumeric(3), Collections.emptyList(), randomAlphanumeric(10));
underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
}
+ @Test
+ public void uninstall_fails_with_ISE_if_not_started() {
+ expectISENotStarted();
+
+ underTest.uninstall();
+ }
+
+ @Test
+ public void uninstall_resets_fields_after_start_and_install() {
+ String value = randomAlphanumeric(10);
+
+ underTest.start();
+ underTest.newEditionWithoutInstall(value);
+ PendingStatus newStatus = underTest.uninstall();
+
+ assertThat(newStatus).isEqualTo(UNINSTALL_IN_PROGRESS);
+ assertThat(underTest.getPendingInstallationStatus()).isEqualTo(UNINSTALL_IN_PROGRESS);
+ assertThat(underTest.getPendingEditionKey()).isEmpty();
+ assertThat(underTest.getPendingLicense()).isEmpty();
+ assertThat(underTest.getCurrentEditionKey()).isEmpty();
+ }
+
+ @Test
+ public void uninstall_fails_with_ISE_if_called_after_uninstall() {
+ String value = randomAlphanumeric(10);
+ underTest.start();
+ underTest.newEditionWithoutInstall(value);
+ underTest.uninstall();
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Can't move to UNINSTALL_IN_PROGRESS when status is UNINSTALL_IN_PROGRESS (should be any of [NONE])");
+
+ underTest.uninstall();
+ }
+
+ @Test
+ public void uninstall_resets_fields_after_newEditionWithoutInstall() {
+ String value = randomAlphanumeric(10);
+ underTest.start();
+ underTest.newEditionWithoutInstall(value);
+
+ PendingStatus newStatus = underTest.uninstall();
+
+ assertThat(newStatus).isEqualTo(UNINSTALL_IN_PROGRESS);
+ assertThat(underTest.getPendingInstallationStatus()).isEqualTo(UNINSTALL_IN_PROGRESS);
+ assertThat(underTest.getPendingEditionKey()).isEmpty();
+ assertThat(underTest.getPendingLicense()).isEmpty();
+ assertThat(underTest.getCurrentEditionKey()).isEmpty();
+ }
+
+ @Test
+ public void uninstall_fails_with_ISE_if_called_after_startAutomaticInstall() {
+ underTest.start();
+ underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Can't move to UNINSTALL_IN_PROGRESS when status is AUTOMATIC_IN_PROGRESS (should be any of [NONE])");
+
+ underTest.uninstall();
+ }
+
+ @Test
+ public void uninstall_fails_with_ISE_if_called_after_automaticInstallReady() {
+ underTest.start();
+ underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
+ underTest.automaticInstallReady();
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Can't move to UNINSTALL_IN_PROGRESS when status is AUTOMATIC_READY (should be any of [NONE])");
+
+ underTest.uninstall();
+ }
+
+ @Test
+ public void uninstall_fails_with_ISE_if_called_after_manualInstall() {
+ underTest.start();
+ underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Can't move to UNINSTALL_IN_PROGRESS when status is MANUAL_IN_PROGRESS (should be any of [NONE])");
+
+ underTest.uninstall();
+ }
+
@Test
public void startManualInstall_fails_with_ISE_if_not_started() {
expectISENotStarted();
underTest.start();
expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Can't move to NONE when status is NONE (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS])");
+ expectedException.expectMessage("Can't move to NONE when status is NONE (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS, UNINSTALL_IN_PROGRESS])");
underTest.finalizeInstallation();
}
underTest.newEditionWithoutInstall(randomAlphanumeric(3));
expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Can't move to NONE when status is NONE (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS])");
+ expectedException.expectMessage("Can't move to NONE when status is NONE (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS, UNINSTALL_IN_PROGRESS])");
underTest.finalizeInstallation();
}
underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Can't move to NONE when status is AUTOMATIC_IN_PROGRESS (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS])");
+ expectedException.expectMessage("Can't move to NONE when status is AUTOMATIC_IN_PROGRESS (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS, UNINSTALL_IN_PROGRESS])");
underTest.finalizeInstallation();
}
assertThat(underTest.getCurrentEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
}
+ @Test
+ public void finalizeInstallation_set_new_edition_and_clear_pending_fields_after_uninstall() {
+ underTest.start();
+ String value = randomAlphanumeric(10);
+ underTest.newEditionWithoutInstall(value);
+ underTest.uninstall();
+
+ PendingStatus newStatus = underTest.finalizeInstallation();
+
+ assertThat(newStatus).isEqualTo(NONE);
+ assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE);
+ assertThat(underTest.getPendingEditionKey()).isEmpty();
+ assertThat(underTest.getPendingLicense()).isEmpty();
+ assertThat(underTest.getCurrentEditionKey()).isEmpty();
+ }
+
@Test
public void finalizeInstallation_overwrites_current_edition_and_clear_pending_fields_after_startManualInstall() {
String value = randomAlphanumeric(10);
underTest.finalizeInstallation();
expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Can't move to NONE when status is NONE (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS])");
+ expectedException.expectMessage("Can't move to NONE when status is NONE (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS, UNINSTALL_IN_PROGRESS])");
underTest.finalizeInstallation();
}