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.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import java.sql.SQLException;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.property.PropertyDto;
30 import org.sonar.server.exceptions.ForbiddenException;
31 import org.sonar.server.platform.db.migration.adhoc.AddMeasuresMigratedColumnToPortfoliosTable;
32 import org.sonar.server.platform.db.migration.adhoc.AddMeasuresMigratedColumnToProjectBranchesTable;
33 import org.sonar.server.platform.db.migration.adhoc.CreateIndexOnPortfoliosMeasuresMigrated;
34 import org.sonar.server.platform.db.migration.adhoc.CreateIndexOnProjectBranchesMeasuresMigrated;
35 import org.sonar.server.platform.db.migration.adhoc.CreateMeasuresTable;
36 import org.sonar.server.tester.UserSessionRule;
37 import org.sonar.server.ws.TestRequest;
38 import org.sonar.server.ws.TestResponse;
39 import org.sonar.server.ws.WsActionTester;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
43 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
44 import static org.mockito.Mockito.doThrow;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.verifyNoInteractions;
48 import static org.sonar.core.config.CorePropertyDefinitions.SYSTEM_MEASURES_MIGRATION_ENABLED;
49 import static org.sonar.test.JsonAssert.assertJson;
51 @RunWith(DataProviderRunner.class)
52 public class PrepareMigrationActionTest {
54 public static final String PARAM_ENABLE = "enable";
56 public UserSessionRule userSessionRule = UserSessionRule.standalone().logIn().setSystemAdministrator();
59 public DbTester dbTester = DbTester.create();
61 private final CreateMeasuresTable createMeasuresTable = mock();
62 private final AddMeasuresMigratedColumnToProjectBranchesTable addMeasuresMigratedColumnToProjectBranchesTable = mock();
63 private final AddMeasuresMigratedColumnToPortfoliosTable addMeasuresMigratedColumnToPortfoliosTable = mock();
64 private final CreateIndexOnProjectBranchesMeasuresMigrated createIndexOnProjectBranchesMeasuresMigrated = mock();
65 private final CreateIndexOnPortfoliosMeasuresMigrated createIndexOnPortfoliosMeasuresMigrated = mock();
67 private final PrepareMigrationAction underTest = new PrepareMigrationAction(userSessionRule, dbTester.getDbClient(), createMeasuresTable,
68 addMeasuresMigratedColumnToProjectBranchesTable, addMeasuresMigratedColumnToPortfoliosTable, createIndexOnProjectBranchesMeasuresMigrated, createIndexOnPortfoliosMeasuresMigrated);
69 private final WsActionTester tester = new WsActionTester(underTest);
72 public void should_throw_if_enable_parameter_is_missing() {
73 TestRequest request = tester.newRequest();
75 assertThatIllegalArgumentException()
76 .isThrownBy(request::execute)
77 .withMessage("The 'enable' parameter is missing");
81 public void verify_example() {
82 TestResponse response = tester.newRequest()
83 .setParam(PARAM_ENABLE, "true")
86 assertJson(response.getInput()).isSimilarTo(getClass().getResource("example-prepare_migration.json"));
90 public void throws_ForbiddenException_if_user_is_not_logged_in() {
91 userSessionRule.anonymous();
93 TestRequest request = tester.newRequest();
95 assertThatExceptionOfType(ForbiddenException.class)
96 .isThrownBy(request::execute);
100 public void throws_ForbiddenException_if_user_is_not_system_admin() {
101 userSessionRule.logIn();
103 TestRequest request = tester.newRequest();
105 assertThatExceptionOfType(ForbiddenException.class)
106 .isThrownBy(request::execute);
110 @DataProvider(value = {"true", "yes"})
111 public void should_enable_migration(String enableParamValue) throws SQLException {
112 assertThat(getPropertyValue()).isNull();
114 TestResponse response = tester.newRequest()
115 .setParam(PARAM_ENABLE, enableParamValue)
118 assertThat(response.getStatus()).isEqualTo(200);
119 assertThat(getPropertyValue()).isTrue();
121 verify(createMeasuresTable).execute();
122 verify(addMeasuresMigratedColumnToProjectBranchesTable).execute();
123 verify(addMeasuresMigratedColumnToPortfoliosTable).execute();
124 verify(createIndexOnProjectBranchesMeasuresMigrated).execute();
125 verify(createIndexOnPortfoliosMeasuresMigrated).execute();
128 response = tester.newRequest()
129 .setParam(PARAM_ENABLE, enableParamValue)
132 assertThat(response.getStatus()).isEqualTo(200);
133 assertThat(getPropertyValue()).isTrue();
137 public void property_is_unchanged_if_the_migrations_failed() throws SQLException {
138 doThrow(new SQLException("Oops")).when(createMeasuresTable).execute();
140 TestRequest request = tester.newRequest()
141 .setParam(PARAM_ENABLE, "true");
143 assertThatExceptionOfType(RuntimeException.class)
144 .isThrownBy(request::execute);
146 assertThat(getPropertyValue()).isNull();
150 @DataProvider(value = {"false", "no"})
151 public void should_disable_migration(String disableParamValue) {
152 dbTester.getDbClient().propertiesDao().saveProperty(new PropertyDto().setKey(SYSTEM_MEASURES_MIGRATION_ENABLED).setValue("true"));
154 TestResponse response = tester.newRequest()
155 .setParam(PARAM_ENABLE, disableParamValue)
158 assertThat(response.getStatus()).isEqualTo(200);
159 assertThat(getPropertyValue()).isFalse();
161 verifyNoInteractions(createMeasuresTable, addMeasuresMigratedColumnToPortfoliosTable, addMeasuresMigratedColumnToProjectBranchesTable,
162 createIndexOnProjectBranchesMeasuresMigrated, createIndexOnPortfoliosMeasuresMigrated);
165 response = tester.newRequest()
166 .setParam(PARAM_ENABLE, disableParamValue)
169 assertThat(response.getStatus()).isEqualTo(200);
170 assertThat(getPropertyValue()).isFalse();
173 private Boolean getPropertyValue() {
174 PropertyDto propertyDto = dbTester.getDbClient().propertiesDao().selectGlobalProperty(SYSTEM_MEASURES_MIGRATION_ENABLED);
175 if (propertyDto == null) {
178 return Boolean.parseBoolean(propertyDto.getValue());