3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.edition;
22 import java.util.Collections;
23 import java.util.Random;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbTester;
30 import org.sonar.server.edition.EditionManagementState.PendingStatus;
32 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.sonar.server.edition.EditionManagementState.PendingStatus.AUTOMATIC_IN_PROGRESS;
35 import static org.sonar.server.edition.EditionManagementState.PendingStatus.AUTOMATIC_READY;
36 import static org.sonar.server.edition.EditionManagementState.PendingStatus.MANUAL_IN_PROGRESS;
37 import static org.sonar.server.edition.EditionManagementState.PendingStatus.NONE;
39 public class StandaloneEditionManagementStateImplTest {
40 private static final License LICENSE_WITHOUT_PLUGINS = new License(randomAlphanumeric(3), Collections.emptyList(), randomAlphanumeric(10));
43 public DbTester dbTester = DbTester.create(System2.INSTANCE);
45 public ExpectedException expectedException = ExpectedException.none();
47 private DbClient dbClient = dbTester.getDbClient();
48 private final StandaloneEditionManagementStateImpl underTest = new StandaloneEditionManagementStateImpl(dbClient);
51 public void getCurrentEditionKey_fails_with_ISE_if_start_has_not_been_called() {
52 expectISENotStarted();
54 underTest.getCurrentEditionKey();
58 public void getCurrentEditionKey_returns_empty_when_internal_properties_table_is_empty() {
61 assertThat(underTest.getCurrentEditionKey()).isEmpty();
65 public void getCurrentEditionKey_returns_value_in_db_for_key_currentEditionKey() {
66 String value = randomAlphanumeric(10);
67 dbTester.properties().insertInternal("currentEditionKey", value);
70 assertThat(underTest.getCurrentEditionKey()).contains(value);
74 public void getCurrentEditionKey_returns_empty_when_value_in_db_is_empty_for_key_currentEditionKey() {
75 dbTester.properties().insertEmptyInternal("currentEditionKey");
78 assertThat(underTest.getCurrentEditionKey()).isEmpty();
82 public void getPendingInstallationStatus_fails_with_ISE_if_start_has_not_been_called() {
83 expectISENotStarted();
85 underTest.getPendingInstallationStatus();
89 public void getPendingInstallationStatus_returns_NONE_when_internal_properties_table_is_empty() {
92 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE);
96 public void getPendingInstallationStatus_returns_value_in_db_for_key_pendingInstallStatus() {
97 PendingStatus value = PendingStatus.values()[new Random().nextInt(PendingStatus.values().length)];
98 dbTester.properties().insertInternal("pendingInstallStatus", value.name());
101 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(value);
105 public void getPendingInstallationStatus_returns_NONE_when_value_in_db_is_empty_for_key_pendingInstallStatus() {
106 dbTester.properties().insertEmptyInternal("pendingInstallStatus");
109 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE);
113 public void start_fails_when_value_in_db_for_key_pendingInstallStatus_cannot_be_parsed_to_enum() {
114 String value = randomAlphanumeric(30);
115 dbTester.properties().insertInternal("pendingInstallStatus", value);
117 expectedException.expect(IllegalArgumentException.class);
118 expectedException.expectMessage("No enum constant org.sonar.server.edition.EditionManagementState.PendingStatus." + value);
124 public void getPendingEditionKey_fails_with_ISE_if_start_has_not_been_called() {
125 expectISENotStarted();
127 underTest.getPendingEditionKey();
131 public void getPendingEditionKey_returns_empty_when_internal_properties_table_is_empty() {
134 assertThat(underTest.getPendingEditionKey()).isEmpty();
138 public void getPendingEditionKey_returns_value_in_db_for_key_pendingEditionKey() {
139 String value = randomAlphanumeric(10);
140 dbTester.properties().insertInternal("pendingEditionKey", value);
143 assertThat(underTest.getPendingEditionKey()).contains(value);
147 public void getPendingEditionKey_returns_empty_when_value_in_db_is_empty_for_key_pendingEditionKey() {
148 dbTester.properties().insertEmptyInternal("pendingEditionKey");
151 assertThat(underTest.getPendingEditionKey()).isEmpty();
155 public void getPendingLicense_fails_with_ISE_if_start_has_not_been_called() {
156 expectISENotStarted();
158 underTest.getPendingLicense();
162 public void getPendingLicense_returns_empty_when_internal_properties_table_is_empty() {
165 assertThat(underTest.getPendingLicense()).isEmpty();
169 public void getPendingLicense_returns_empty_value_in_db_for_key_pendingLicense() {
170 String value = randomAlphanumeric(10);
171 dbTester.properties().insertInternal("pendingLicense", value);
174 assertThat(underTest.getPendingLicense()).contains(value);
178 public void getPendingLicense_returns_empty_when_value_in_db_is_empty_for_key_pendingLicense() {
179 dbTester.properties().insertEmptyInternal("pendingLicense");
182 assertThat(underTest.getPendingLicense()).isEmpty();
186 public void startAutomaticInstall_fails_with_ISE_if_not_started() {
187 expectISENotStarted();
189 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
193 public void startAutomaticInstall_fails_with_NPE_if_license_is_null() {
196 expectedException.expect(NullPointerException.class);
197 expectedException.expectMessage("license can't be null");
199 underTest.startAutomaticInstall(null);
203 public void startAutomaticInstall_sets_pending_fields_after_start() {
206 PendingStatus newStatus = underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
208 assertThat(newStatus).isEqualTo(AUTOMATIC_IN_PROGRESS);
209 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(AUTOMATIC_IN_PROGRESS);
210 assertThat(underTest.getPendingEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
211 assertThat(underTest.getPendingLicense()).contains(LICENSE_WITHOUT_PLUGINS.getContent());
212 assertThat(underTest.getCurrentEditionKey()).isEmpty();
216 public void startAutomaticInstall_sets_pending_fields_after_start_with_existing_currentEditionKey() {
217 String value = randomAlphanumeric(10);
218 dbTester.properties().insertInternal("currentEditionKey", value);
221 PendingStatus newStatus = underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
223 assertThat(newStatus).isEqualTo(AUTOMATIC_IN_PROGRESS);
224 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(AUTOMATIC_IN_PROGRESS);
225 assertThat(underTest.getPendingEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
226 assertThat(underTest.getPendingLicense()).contains(LICENSE_WITHOUT_PLUGINS.getContent());
227 assertThat(underTest.getCurrentEditionKey()).contains(value);
231 public void startAutomaticInstall_sets_pending_fields_after_newEditionWithoutInstall() {
232 String value = randomAlphanumeric(10);
234 underTest.newEditionWithoutInstall(value);
236 PendingStatus newStatus = underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
238 assertThat(newStatus).isEqualTo(AUTOMATIC_IN_PROGRESS);
239 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(AUTOMATIC_IN_PROGRESS);
240 assertThat(underTest.getPendingEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
241 assertThat(underTest.getPendingLicense()).contains(LICENSE_WITHOUT_PLUGINS.getContent());
242 assertThat(underTest.getCurrentEditionKey()).contains(value);
246 public void startAutomaticInstall_fails_with_ISE_if_called_after_startAutomaticInstall() {
248 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
250 expectedException.expect(IllegalStateException.class);
251 expectedException.expectMessage("Can't move to AUTOMATIC_IN_PROGRESS when status is AUTOMATIC_IN_PROGRESS (should be any of [NONE])");
253 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
257 public void startAutomaticInstall_fails_with_ISE_if_called_after_automaticInstallReady() {
259 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
260 underTest.automaticInstallReady();
262 expectedException.expect(IllegalStateException.class);
263 expectedException.expectMessage("Can't move to AUTOMATIC_IN_PROGRESS when status is AUTOMATIC_READY (should be any of [NONE])");
265 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
269 public void startAutomaticInstall_fails_with_ISE_if_called_after_manualInstall() {
271 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
273 expectedException.expect(IllegalStateException.class);
274 expectedException.expectMessage("Can't move to AUTOMATIC_IN_PROGRESS when status is MANUAL_IN_PROGRESS (should be any of [NONE])");
276 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
280 public void startManualInstall_fails_with_ISE_if_not_started() {
281 expectISENotStarted();
283 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
287 public void startManualInstall_fails_with_NPE_if_license_is_null() {
290 expectedException.expect(NullPointerException.class);
291 expectedException.expectMessage("license can't be null");
293 underTest.startManualInstall(null);
297 public void startManualInstall_sets_pending_fields_after_start() {
300 PendingStatus newStatus = underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
302 assertThat(newStatus).isEqualTo(MANUAL_IN_PROGRESS);
303 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(MANUAL_IN_PROGRESS);
304 assertThat(underTest.getPendingEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
305 assertThat(underTest.getPendingLicense()).contains(LICENSE_WITHOUT_PLUGINS.getContent());
306 assertThat(underTest.getCurrentEditionKey()).isEmpty();
310 public void startManualInstall_sets_pending_fields_after_start_with_existing_currentEditionKey() {
311 String value = randomAlphanumeric(10);
312 dbTester.properties().insertInternal("currentEditionKey", value);
315 PendingStatus newStatus = underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
317 assertThat(newStatus).isEqualTo(MANUAL_IN_PROGRESS);
318 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(MANUAL_IN_PROGRESS);
319 assertThat(underTest.getPendingEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
320 assertThat(underTest.getPendingLicense()).contains(LICENSE_WITHOUT_PLUGINS.getContent());
321 assertThat(underTest.getCurrentEditionKey()).contains(value);
325 public void startManualInstall_sets_pending_fields_after_newEditionWithoutInstall() {
326 String value = randomAlphanumeric(10);
328 underTest.newEditionWithoutInstall(value);
330 PendingStatus newStatus = underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
332 assertThat(newStatus).isEqualTo(MANUAL_IN_PROGRESS);
333 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(MANUAL_IN_PROGRESS);
334 assertThat(underTest.getPendingEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
335 assertThat(underTest.getPendingLicense()).contains(LICENSE_WITHOUT_PLUGINS.getContent());
336 assertThat(underTest.getCurrentEditionKey()).contains(value);
340 public void startManualInstall_fails_with_ISE_if_called_after_startAutomaticInstall() {
342 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
344 expectedException.expect(IllegalStateException.class);
345 expectedException.expectMessage("Can't move to MANUAL_IN_PROGRESS when status is AUTOMATIC_IN_PROGRESS (should be any of [NONE])");
347 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
351 public void startManualInstall_fails_with_ISE_if_called_after_automaticInstallReady() {
353 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
354 underTest.automaticInstallReady();
356 expectedException.expect(IllegalStateException.class);
357 expectedException.expectMessage("Can't move to MANUAL_IN_PROGRESS when status is AUTOMATIC_READY (should be any of [NONE])");
359 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
363 public void startManualInstall_fails_with_ISE_if_called_after_manualInstall() {
365 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
367 expectedException.expect(IllegalStateException.class);
368 expectedException.expectMessage("Can't move to MANUAL_IN_PROGRESS when status is MANUAL_IN_PROGRESS (should be any of [NONE])");
370 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
374 public void automaticInstallReady_fails_with_ISE_if_not_started() {
375 expectISENotStarted();
377 underTest.automaticInstallReady();
381 public void automaticInstallReady_fails_with_ISE_if_called() {
384 expectedException.expect(IllegalStateException.class);
385 expectedException.expectMessage("Can't move to AUTOMATIC_READY when status is NONE (should be any of [AUTOMATIC_IN_PROGRESS])");
387 underTest.automaticInstallReady();
391 public void automaticInstallReady_fails_with_ISE_if_called_after_manualInstall() {
393 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
395 expectedException.expect(IllegalStateException.class);
396 expectedException.expectMessage("Can't move to AUTOMATIC_READY when status is MANUAL_IN_PROGRESS (should be any of [AUTOMATIC_IN_PROGRESS])");
398 underTest.automaticInstallReady();
402 public void automaticInstallReady_after_startAutomaticInstall_changes_status_to_AUTOMATIC_READY_but_does_not_change_editions() {
404 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
406 PendingStatus newStatus = underTest.automaticInstallReady();
408 assertThat(newStatus).isEqualTo(AUTOMATIC_READY);
409 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(AUTOMATIC_READY);
410 assertThat(underTest.getPendingEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
411 assertThat(underTest.getPendingLicense()).contains(LICENSE_WITHOUT_PLUGINS.getContent());
412 assertThat(underTest.getCurrentEditionKey()).isEmpty();
416 public void newEditionWithoutInstall_fails_with_ISE_if_not_started() {
417 expectISENotStarted();
419 underTest.newEditionWithoutInstall(randomAlphanumeric(3));
423 public void newEditionWithoutInstall_fails_with_NPE_if_newEditionKey_is_null() {
426 expectedException.expect(NullPointerException.class);
427 expectedException.expectMessage("newEditionKey can't be null");
429 underTest.newEditionWithoutInstall(null);
433 public void newEditionWithoutInstall_fails_with_IAE_if_newEditionKey_is_empty() {
436 expectedException.expect(IllegalArgumentException.class);
437 expectedException.expectMessage("newEditionKey can't be empty");
439 underTest.newEditionWithoutInstall("");
443 public void newEditionWithoutInstall_changes_current_edition() {
444 String newEditionKey = randomAlphanumeric(3);
447 PendingStatus newStatus = underTest.newEditionWithoutInstall(newEditionKey);
449 assertThat(newStatus).isEqualTo(NONE);
450 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE);
451 assertThat(underTest.getPendingEditionKey()).isEmpty();
452 assertThat(underTest.getPendingLicense()).isEmpty();
453 assertThat(underTest.getCurrentEditionKey()).contains(newEditionKey);
457 public void newEditionWithoutInstall_overwrite_current_edition() {
458 String newEditionKey = randomAlphanumeric(3);
461 PendingStatus newStatus = underTest.newEditionWithoutInstall(newEditionKey);
463 assertThat(newStatus).isEqualTo(NONE);
464 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE);
465 assertThat(underTest.getPendingEditionKey()).isEmpty();
466 assertThat(underTest.getPendingLicense()).isEmpty();
467 assertThat(underTest.getCurrentEditionKey()).contains(newEditionKey);
471 public void newEditionWithoutInstall_overwrites_from_previous_newEditionWithoutInstall() {
472 String newEditionKey1 = randomAlphanumeric(3);
473 String newEditionKey2 = randomAlphanumeric(3);
475 underTest.newEditionWithoutInstall(newEditionKey1);
477 PendingStatus newStatus = underTest.newEditionWithoutInstall(newEditionKey2);
479 assertThat(newStatus).isEqualTo(NONE);
480 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE);
481 assertThat(underTest.getPendingEditionKey()).isEmpty();
482 assertThat(underTest.getPendingLicense()).isEmpty();
483 assertThat(underTest.getCurrentEditionKey()).contains(newEditionKey2);
487 public void newEditionWithoutInstall_fails_with_ISE_if_called_after_startAutomaticInstall() {
488 String newEditionKey = randomAlphanumeric(3);
490 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
492 expectedException.expect(IllegalStateException.class);
493 expectedException.expectMessage("Can't move to NONE when status is AUTOMATIC_IN_PROGRESS (should be any of [NONE])");
495 underTest.newEditionWithoutInstall(newEditionKey);
499 public void newEditionWithoutInstall_fails_with_ISE_if_called_after_startManualInstall() {
500 String newEditionKey = randomAlphanumeric(3);
502 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
504 expectedException.expect(IllegalStateException.class);
505 expectedException.expectMessage("Can't move to NONE when status is MANUAL_IN_PROGRESS (should be any of [NONE])");
507 underTest.newEditionWithoutInstall(newEditionKey);
511 public void finalizeInstallation_fails_with_ISE_if_not_started() {
512 expectISENotStarted();
514 underTest.finalizeInstallation();
518 public void finalizeInstallation_fails_with_ISE_after_start() {
521 expectedException.expect(IllegalStateException.class);
522 expectedException.expectMessage("Can't move to NONE when status is NONE (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS])");
524 underTest.finalizeInstallation();
528 public void finalizeInstallation_fails_with_ISE_after_newEditionWithoutInstall() {
530 underTest.newEditionWithoutInstall(randomAlphanumeric(3));
532 expectedException.expect(IllegalStateException.class);
533 expectedException.expectMessage("Can't move to NONE when status is NONE (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS])");
535 underTest.finalizeInstallation();
539 public void finalizeInstallation_fails_with_ISE_after_startAutomaticInstall() {
541 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
543 expectedException.expect(IllegalStateException.class);
544 expectedException.expectMessage("Can't move to NONE when status is AUTOMATIC_IN_PROGRESS (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS])");
546 underTest.finalizeInstallation();
550 public void finalizeInstallation_set_new_edition_and_clear_pending_fields_after_manualInstallationReady() {
552 underTest.startAutomaticInstall(LICENSE_WITHOUT_PLUGINS);
553 underTest.automaticInstallReady();
555 PendingStatus newStatus = underTest.finalizeInstallation();
557 assertThat(newStatus).isEqualTo(NONE);
558 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE);
559 assertThat(underTest.getPendingEditionKey()).isEmpty();
560 assertThat(underTest.getPendingLicense()).isEmpty();
561 assertThat(underTest.getCurrentEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
565 public void finalizeInstallation_set_new_edition_and_clear_pending_fields_after_startManualInstall() {
567 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
569 PendingStatus newStatus = underTest.finalizeInstallation();
571 assertThat(newStatus).isEqualTo(NONE);
572 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE);
573 assertThat(underTest.getPendingEditionKey()).isEmpty();
574 assertThat(underTest.getPendingLicense()).isEmpty();
575 assertThat(underTest.getCurrentEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
579 public void finalizeInstallation_overwrites_current_edition_and_clear_pending_fields_after_startManualInstall() {
580 String value = randomAlphanumeric(10);
581 dbTester.properties().insertInternal("currentEditionKey", value);
583 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
585 PendingStatus newStatus = underTest.finalizeInstallation();
587 assertThat(newStatus).isEqualTo(NONE);
588 assertThat(underTest.getPendingInstallationStatus()).isEqualTo(NONE);
589 assertThat(underTest.getPendingEditionKey()).isEmpty();
590 assertThat(underTest.getPendingLicense()).isEmpty();
591 assertThat(underTest.getCurrentEditionKey()).contains(LICENSE_WITHOUT_PLUGINS.getEditionKey());
595 public void finalizeInstallation_fails_with_ISE_after_finalizeInstallation() {
597 underTest.startManualInstall(LICENSE_WITHOUT_PLUGINS);
598 underTest.finalizeInstallation();
600 expectedException.expect(IllegalStateException.class);
601 expectedException.expectMessage("Can't move to NONE when status is NONE (should be any of [AUTOMATIC_READY, MANUAL_IN_PROGRESS])");
603 underTest.finalizeInstallation();
606 private void expectISENotStarted() {
607 expectedException.expect(IllegalStateException.class);
608 expectedException.expectMessage("StandaloneEditionManagementStateImpl is not started");