3 * Copyright (C) 2009-2023 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.platform;
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.config.internal.ConfigurationBridge;
26 import org.sonar.api.config.internal.MapSettings;
27 import org.sonar.server.platform.db.migration.step.MigrationSteps;
28 import org.sonar.server.platform.db.migration.version.DatabaseVersion;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 public class DefaultServerUpgradeStatusTest {
35 private static final long LAST_VERSION = 150;
36 private MigrationSteps migrationSteps = mock(MigrationSteps.class);
37 private DatabaseVersion dbVersion = mock(DatabaseVersion.class);
38 private MapSettings settings = new MapSettings();
39 private DefaultServerUpgradeStatus underTest = new DefaultServerUpgradeStatus(dbVersion, migrationSteps, new ConfigurationBridge(settings));
43 when(migrationSteps.getMaxMigrationNumber()).thenReturn(LAST_VERSION);
47 public void shouldBeFreshInstallation() {
48 when(migrationSteps.getMaxMigrationNumber()).thenReturn(150L);
49 when(dbVersion.getVersion()).thenReturn(Optional.empty());
53 assertThat(underTest.isFreshInstall()).isTrue();
54 assertThat(underTest.isUpgraded()).isFalse();
55 assertThat(underTest.getInitialDbVersion()).isEqualTo(-1);
59 public void shouldBeUpgraded() {
60 when(dbVersion.getVersion()).thenReturn(Optional.of(50L));
64 assertThat(underTest.isFreshInstall()).isFalse();
65 assertThat(underTest.isUpgraded()).isTrue();
66 assertThat(underTest.getInitialDbVersion()).isEqualTo(50);
70 public void shouldNotBeUpgraded() {
71 when(dbVersion.getVersion()).thenReturn(Optional.of(LAST_VERSION));
75 assertThat(underTest.isFreshInstall()).isFalse();
76 assertThat(underTest.isUpgraded()).isFalse();
77 assertThat(underTest.getInitialDbVersion()).isEqualTo((int) LAST_VERSION);
81 public void isBlueGreen() {
83 assertThat(underTest.isBlueGreen()).isFalse();
85 settings.setProperty("sonar.blueGreenEnabled", true);
86 assertThat(underTest.isBlueGreen()).isTrue();
88 settings.setProperty("sonar.blueGreenEnabled", false);
89 assertThat(underTest.isBlueGreen()).isFalse();
94 public void isAutoDbUpgrade() {
96 assertThat(underTest.isAutoDbUpgrade()).isFalse();
98 settings.setProperty("sonar.autoDatabaseUpgrade", true);
99 assertThat(underTest.isAutoDbUpgrade()).isTrue();
101 settings.setProperty("sonar.autoDatabaseUpgrade", false);
102 assertThat(underTest.isAutoDbUpgrade()).isFalse();