]> source.dussan.org Git - sonarqube.git/blob
8ac4ba02b10371c741d60efdc8dbe44b5460b772
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.db.migration;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.slf4j.event.Level;
26 import org.sonar.api.testfixtures.log.LogTester;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.dialect.Dialect;
29 import org.sonar.db.dialect.H2;
30 import org.sonar.db.dialect.MsSql;
31 import org.sonar.db.dialect.Oracle;
32 import org.sonar.db.dialect.PostgreSql;
33 import org.sonar.server.platform.DefaultServerUpgradeStatus;
34 import org.sonar.server.platform.db.migration.engine.MigrationEngine;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.verifyNoInteractions;
40 import static org.mockito.Mockito.when;
41
42 public class AutoDbMigrationTest {
43   @Rule
44   public LogTester logTester = new LogTester();
45
46   private DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
47   private DefaultServerUpgradeStatus serverUpgradeStatus = mock(DefaultServerUpgradeStatus.class);
48   private MigrationEngine migrationEngine = mock(MigrationEngine.class);
49   private AutoDbMigration underTest = new AutoDbMigration(serverUpgradeStatus, migrationEngine);
50
51   @Test
52   public void start_runs_MigrationEngine_on_h2_if_fresh_install() {
53     start_runs_MigrationEngine_for_dialect_if_fresh_install(new H2());
54   }
55
56   @Test
57   public void start_runs_MigrationEngine_on_postgre_if_fresh_install() {
58     start_runs_MigrationEngine_for_dialect_if_fresh_install(new PostgreSql());
59   }
60
61   @Test
62   public void start_runs_MigrationEngine_on_Oracle_if_fresh_install() {
63     start_runs_MigrationEngine_for_dialect_if_fresh_install(new Oracle());
64   }
65
66   @Test
67   public void start_runs_MigrationEngine_on_MsSQL_if_fresh_install() {
68     start_runs_MigrationEngine_for_dialect_if_fresh_install(new MsSql());
69   }
70
71   private void start_runs_MigrationEngine_for_dialect_if_fresh_install(Dialect dialect) {
72     mockDialect(dialect);
73     mockFreshInstall(true);
74
75     underTest.start();
76
77     verify(migrationEngine).execute();
78     verifyInfoLog();
79   }
80
81   @Test
82   public void start_does_nothing_if_not_fresh_install() {
83     mockFreshInstall(false);
84
85     underTest.start();
86
87     verifyNoInteractions(migrationEngine);
88     assertThat(logTester.logs(Level.INFO)).isEmpty();
89   }
90
91   @Test
92   public void start_runs_MigrationEngine_if_autoDbMigration_enabled() {
93     mockFreshInstall(false);
94     when(serverUpgradeStatus.isUpgraded()).thenReturn(true);
95     when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
96
97     underTest.start();
98
99     verify(migrationEngine).execute();
100     assertThat(logTester.logs(Level.INFO)).contains("Automatically perform DB migration, as automatic database upgrade is enabled");
101   }
102
103   @Test
104   public void start_does_nothing_if_autoDbMigration_but_no_upgrade() {
105     mockFreshInstall(false);
106     when(serverUpgradeStatus.isUpgraded()).thenReturn(false);
107     when(serverUpgradeStatus.isAutoDbUpgrade()).thenReturn(true);
108
109     underTest.start();
110
111     verifyNoInteractions(migrationEngine);
112     assertThat(logTester.logs(Level.INFO)).isEmpty();
113   }
114
115   @Test
116   public void stop_has_no_effect() {
117     underTest.stop();
118   }
119
120   private void mockFreshInstall(boolean value) {
121     when(serverUpgradeStatus.isFreshInstall()).thenReturn(value);
122   }
123
124   private void mockDialect(Dialect dialect) {
125     when(dbClient.getDatabase().getDialect()).thenReturn(dialect);
126   }
127
128   private void verifyInfoLog() {
129     assertThat(logTester.logs()).hasSize(1);
130     assertThat(logTester.logs(Level.INFO)).containsExactly("Automatically perform DB migration on fresh install");
131   }
132
133 }