]> source.dussan.org Git - sonarqube.git/blob
b4888664d88ab4bbd7606c4452e796a50b277c1e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform;
21
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;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
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));
40
41   @Before
42   public void setUp() {
43     when(migrationSteps.getMaxMigrationNumber()).thenReturn(LAST_VERSION);
44   }
45
46   @Test
47   public void shouldBeFreshInstallation() {
48     when(migrationSteps.getMaxMigrationNumber()).thenReturn(150L);
49     when(dbVersion.getVersion()).thenReturn(Optional.empty());
50
51     underTest.start();
52
53     assertThat(underTest.isFreshInstall()).isTrue();
54     assertThat(underTest.isUpgraded()).isFalse();
55     assertThat(underTest.getInitialDbVersion()).isEqualTo(-1);
56   }
57
58   @Test
59   public void shouldBeUpgraded() {
60     when(dbVersion.getVersion()).thenReturn(Optional.of(50L));
61
62     underTest.start();
63
64     assertThat(underTest.isFreshInstall()).isFalse();
65     assertThat(underTest.isUpgraded()).isTrue();
66     assertThat(underTest.getInitialDbVersion()).isEqualTo(50);
67   }
68
69   @Test
70   public void shouldNotBeUpgraded() {
71     when(dbVersion.getVersion()).thenReturn(Optional.of(LAST_VERSION));
72
73     underTest.start();
74
75     assertThat(underTest.isFreshInstall()).isFalse();
76     assertThat(underTest.isUpgraded()).isFalse();
77     assertThat(underTest.getInitialDbVersion()).isEqualTo((int) LAST_VERSION);
78   }
79
80   @Test
81   public void isBlueGreen() {
82     settings.clear();
83     assertThat(underTest.isBlueGreen()).isFalse();
84
85     settings.setProperty("sonar.blueGreenEnabled", true);
86     assertThat(underTest.isBlueGreen()).isTrue();
87
88     settings.setProperty("sonar.blueGreenEnabled", false);
89     assertThat(underTest.isBlueGreen()).isFalse();
90   }
91
92
93   @Test
94   public void isAutoDbUpgrade() {
95     settings.clear();
96     assertThat(underTest.isAutoDbUpgrade()).isFalse();
97
98     settings.setProperty("sonar.autoDatabaseUpgrade", true);
99     assertThat(underTest.isAutoDbUpgrade()).isTrue();
100
101     settings.setProperty("sonar.autoDatabaseUpgrade", false);
102     assertThat(underTest.isAutoDbUpgrade()).isFalse();
103   }
104 }