3 * Copyright (C) 2009-2024 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.ws;
22 import com.google.common.collect.ImmutableList;
23 import com.tngtech.java.junit.dataprovider.DataProvider;
24 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
25 import com.tngtech.java.junit.dataprovider.UseDataProvider;
26 import java.util.Arrays;
27 import java.util.Date;
28 import java.util.Optional;
29 import javax.annotation.Nullable;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.sonar.api.utils.DateUtils;
34 import org.sonar.db.Database;
35 import org.sonar.db.dialect.Dialect;
36 import org.sonar.server.platform.db.migration.DatabaseMigrationState;
37 import org.sonar.server.platform.db.migration.DatabaseMigrationState.Status;
38 import org.sonar.server.platform.db.migration.version.DatabaseVersion;
39 import org.sonar.server.ws.TestResponse;
40 import org.sonar.server.ws.WsActionTester;
42 import static com.google.common.base.Predicates.in;
43 import static com.google.common.base.Predicates.not;
44 import static com.google.common.collect.Iterables.filter;
45 import static org.assertj.core.api.Assertions.assertThatThrownBy;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.reset;
48 import static org.mockito.Mockito.when;
49 import static org.sonar.server.platform.db.migration.DatabaseMigrationState.Status.FAILED;
50 import static org.sonar.server.platform.db.migration.DatabaseMigrationState.Status.NONE;
51 import static org.sonar.server.platform.db.migration.DatabaseMigrationState.Status.RUNNING;
52 import static org.sonar.server.platform.db.migration.DatabaseMigrationState.Status.SUCCEEDED;
53 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.MESSAGE_MIGRATION_REQUIRED;
54 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.MESSAGE_NO_MIGRATION_ON_EMBEDDED_DATABASE;
55 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.MESSAGE_STATUS_NONE;
56 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.MESSAGE_STATUS_RUNNING;
57 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.MESSAGE_STATUS_SUCCEEDED;
58 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.STATUS_MIGRATION_FAILED;
59 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.STATUS_MIGRATION_REQUIRED;
60 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.STATUS_MIGRATION_RUNNING;
61 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.STATUS_MIGRATION_SUCCEEDED;
62 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.STATUS_NOT_SUPPORTED;
63 import static org.sonar.server.platform.ws.DbMigrationJsonWriter.STATUS_NO_MIGRATION;
64 import static org.sonar.test.JsonAssert.assertJson;
66 @RunWith(DataProviderRunner.class)
67 public class DbMigrationStatusActionTest {
69 private static final Date SOME_DATE = new Date();
70 private static final String SOME_THROWABLE_MSG = "blablabla pop !";
71 private static final String DEFAULT_ERROR_MSG = "No failure error";
73 private DatabaseVersion databaseVersion = mock(DatabaseVersion.class);
74 private Database database = mock(Database.class);
75 private Dialect dialect = mock(Dialect.class);
76 private DatabaseMigrationState migrationState = mock(DatabaseMigrationState.class);
77 private DbMigrationStatusAction underTest = new DbMigrationStatusAction(databaseVersion, database, migrationState);
78 private WsActionTester tester = new WsActionTester(underTest);
81 public void wireMocksTogether() {
82 when(database.getDialect()).thenReturn(dialect);
83 when(databaseVersion.getVersion()).thenReturn(Optional.of(150L));
87 public void verify_example() {
88 when(dialect.supportsMigration()).thenReturn(true);
89 when(migrationState.getStatus()).thenReturn(RUNNING);
90 when(migrationState.getStartedAt()).thenReturn(DateUtils.parseDateTime("2015-02-23T18:54:23+0100"));
92 TestResponse response = tester.newRequest().execute();
94 assertJson(response.getInput()).isSimilarTo(getClass().getResource("example-migrate_db.json"));
98 public void throws_ISE_when_database_has_no_version() {
100 when(databaseVersion.getVersion()).thenReturn(Optional.empty());
102 assertThatThrownBy(() -> tester.newRequest().execute())
103 .isInstanceOf(IllegalStateException.class)
104 .hasMessageContaining("Cannot connect to Database.");
108 public void msg_is_operational_and_state_from_databasemigration_when_databaseversion_status_is_UP_TO_DATE() {
109 when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE);
110 when(migrationState.getStatus()).thenReturn(NONE);
112 TestResponse response = tester.newRequest().execute();
114 assertJson(response.getInput()).isSimilarTo(expectedResponse(STATUS_NO_MIGRATION, MESSAGE_STATUS_NONE));
117 // this test will raise a IllegalArgumentException when an unsupported value is added to the Status enum
119 @UseDataProvider("statusRequiringDbMigration")
120 public void defensive_test_all_values_of_Status_must_be_supported(DatabaseVersion.Status status) {
121 when(databaseVersion.getStatus()).thenReturn(status);
122 for (Status migrationStatus : filter(Arrays.asList(DatabaseMigrationState.Status.values()), not(in(ImmutableList.of(NONE, RUNNING, FAILED, SUCCEEDED))))) {
123 when(migrationState.getStatus()).thenReturn(migrationStatus);
125 tester.newRequest().execute();
130 public void state_from_databasemigration_when_databaseversion_status_is_REQUIRES_DOWNGRADE() {
131 when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.REQUIRES_DOWNGRADE);
132 when(migrationState.getStatus()).thenReturn(NONE);
134 TestResponse response = tester.newRequest().execute();
136 assertJson(response.getInput()).isSimilarTo(expectedResponse(STATUS_NO_MIGRATION, MESSAGE_STATUS_NONE));
140 @UseDataProvider("statusRequiringDbMigration")
141 public void state_is_NONE_with_specific_msg_when_db_requires_upgrade_but_dialect_does_not_support_migration(DatabaseVersion.Status status) {
142 when(databaseVersion.getStatus()).thenReturn(status);
143 when(dialect.supportsMigration()).thenReturn(false);
145 TestResponse response = tester.newRequest().execute();
147 assertJson(response.getInput()).isSimilarTo(expectedResponse(STATUS_NOT_SUPPORTED, MESSAGE_NO_MIGRATION_ON_EMBEDDED_DATABASE));
151 @UseDataProvider("statusRequiringDbMigration")
152 public void state_from_database_migration_when_dbmigration_status_is_RUNNING(DatabaseVersion.Status status) {
153 when(databaseVersion.getStatus()).thenReturn(status);
154 when(dialect.supportsMigration()).thenReturn(true);
155 when(migrationState.getStatus()).thenReturn(RUNNING);
156 when(migrationState.getStartedAt()).thenReturn(SOME_DATE);
158 TestResponse response = tester.newRequest().execute();
160 assertJson(response.getInput()).isSimilarTo(expectedResponse(STATUS_MIGRATION_RUNNING, MESSAGE_STATUS_RUNNING, SOME_DATE));
164 @UseDataProvider("statusRequiringDbMigration")
165 public void state_from_database_migration_and_msg_includes_error_when_dbmigration_status_is_FAILED(DatabaseVersion.Status status) {
166 when(databaseVersion.getStatus()).thenReturn(status);
167 when(dialect.supportsMigration()).thenReturn(true);
168 when(migrationState.getStatus()).thenReturn(FAILED);
169 when(migrationState.getStartedAt()).thenReturn(SOME_DATE);
170 when(migrationState.getError()).thenReturn(new UnsupportedOperationException(SOME_THROWABLE_MSG));
172 TestResponse response = tester.newRequest().execute();
174 assertJson(response.getInput()).isSimilarTo(expectedResponse(STATUS_MIGRATION_FAILED, failedMsg(SOME_THROWABLE_MSG), SOME_DATE));
178 @UseDataProvider("statusRequiringDbMigration")
179 public void state_from_database_migration_and_msg_has_default_msg_when_dbmigration_status_is_FAILED(DatabaseVersion.Status status) {
180 when(databaseVersion.getStatus()).thenReturn(status);
181 when(dialect.supportsMigration()).thenReturn(true);
182 when(migrationState.getStatus()).thenReturn(FAILED);
183 when(migrationState.getStartedAt()).thenReturn(SOME_DATE);
184 when(migrationState.getError()).thenReturn(null); // no failure throwable caught
186 TestResponse response = tester.newRequest().execute();
188 assertJson(response.getInput()).isSimilarTo(expectedResponse(STATUS_MIGRATION_FAILED, failedMsg(DEFAULT_ERROR_MSG), SOME_DATE));
192 @UseDataProvider("statusRequiringDbMigration")
193 public void state_from_database_migration_and_msg_has_default_msg_when_dbmigration_status_is_SUCCEEDED(DatabaseVersion.Status status) {
194 when(databaseVersion.getStatus()).thenReturn(status);
195 when(dialect.supportsMigration()).thenReturn(true);
196 when(migrationState.getStatus()).thenReturn(SUCCEEDED);
197 when(migrationState.getStartedAt()).thenReturn(SOME_DATE);
199 TestResponse response = tester.newRequest().execute();
201 assertJson(response.getInput()).isSimilarTo(expectedResponse(STATUS_MIGRATION_SUCCEEDED, MESSAGE_STATUS_SUCCEEDED, SOME_DATE));
205 @UseDataProvider("statusRequiringDbMigration")
206 public void start_migration_and_return_state_from_databasemigration_when_dbmigration_status_is_NONE(DatabaseVersion.Status status) {
207 when(databaseVersion.getStatus()).thenReturn(status);
208 when(dialect.supportsMigration()).thenReturn(true);
209 when(migrationState.getStatus()).thenReturn(NONE);
210 when(migrationState.getStartedAt()).thenReturn(SOME_DATE);
212 TestResponse response = tester.newRequest().execute();
214 assertJson(response.getInput()).isSimilarTo(expectedResponse(STATUS_MIGRATION_REQUIRED, MESSAGE_MIGRATION_REQUIRED));
218 public static Object[][] statusRequiringDbMigration() {
219 return new Object[][] {
220 {DatabaseVersion.Status.FRESH_INSTALL},
221 {DatabaseVersion.Status.REQUIRES_UPGRADE},
225 private static String failedMsg(@Nullable String t) {
226 return "Migration failed: " + t + ".<br/> Please check logs.";
229 private static String expectedResponse(String status, String msg) {
231 "\"state\":\"" + status + "\"," +
232 "\"message\":\"" + msg + "\"" +
236 private static String expectedResponse(String status, String msg, Date date) {
238 "\"state\":\"" + status + "\"," +
239 "\"message\":\"" + msg + "\"," +
240 "\"startedAt\":\"" + DateUtils.formatDateTime(date) + "\"" +