]> source.dussan.org Git - sonarqube.git/blob
11bc6c6922695feb6f7628decbb6f8e3acde383b
[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.ws;
21
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;
40
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;
50
51 @RunWith(DataProviderRunner.class)
52 public class PrepareMigrationActionTest {
53
54   public static final String PARAM_ENABLE = "enable";
55   @Rule
56   public UserSessionRule userSessionRule = UserSessionRule.standalone().logIn().setSystemAdministrator();
57
58   @Rule
59   public DbTester dbTester = DbTester.create();
60
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();
66
67   private final PrepareMigrationAction underTest = new PrepareMigrationAction(userSessionRule, dbTester.getDbClient(), createMeasuresTable,
68     addMeasuresMigratedColumnToProjectBranchesTable, addMeasuresMigratedColumnToPortfoliosTable, createIndexOnProjectBranchesMeasuresMigrated, createIndexOnPortfoliosMeasuresMigrated);
69   private final WsActionTester tester = new WsActionTester(underTest);
70
71   @Test
72   public void should_throw_if_enable_parameter_is_missing() {
73     TestRequest request = tester.newRequest();
74
75     assertThatIllegalArgumentException()
76       .isThrownBy(request::execute)
77       .withMessage("The 'enable' parameter is missing");
78   }
79
80   @Test
81   public void verify_example() {
82     TestResponse response = tester.newRequest()
83       .setParam(PARAM_ENABLE, "true")
84       .execute();
85
86     assertJson(response.getInput()).isSimilarTo(getClass().getResource("example-prepare_migration.json"));
87   }
88
89   @Test
90   public void throws_ForbiddenException_if_user_is_not_logged_in() {
91     userSessionRule.anonymous();
92
93     TestRequest request = tester.newRequest();
94
95     assertThatExceptionOfType(ForbiddenException.class)
96       .isThrownBy(request::execute);
97   }
98
99   @Test
100   public void throws_ForbiddenException_if_user_is_not_system_admin() {
101     userSessionRule.logIn();
102
103     TestRequest request = tester.newRequest();
104
105     assertThatExceptionOfType(ForbiddenException.class)
106       .isThrownBy(request::execute);
107   }
108
109   @Test
110   @DataProvider(value = {"true", "yes"})
111   public void should_enable_migration(String enableParamValue) throws SQLException {
112     assertThat(getPropertyValue()).isNull();
113
114     TestResponse response = tester.newRequest()
115       .setParam(PARAM_ENABLE, enableParamValue)
116       .execute();
117
118     assertThat(response.getStatus()).isEqualTo(200);
119     assertThat(getPropertyValue()).isTrue();
120
121     verify(createMeasuresTable).execute();
122     verify(addMeasuresMigratedColumnToProjectBranchesTable).execute();
123     verify(addMeasuresMigratedColumnToPortfoliosTable).execute();
124     verify(createIndexOnProjectBranchesMeasuresMigrated).execute();
125     verify(createIndexOnPortfoliosMeasuresMigrated).execute();
126
127     // reentrant
128     response = tester.newRequest()
129       .setParam(PARAM_ENABLE, enableParamValue)
130       .execute();
131
132     assertThat(response.getStatus()).isEqualTo(200);
133     assertThat(getPropertyValue()).isTrue();
134   }
135
136   @Test
137   public void property_is_unchanged_if_the_migrations_failed() throws SQLException {
138     doThrow(new SQLException("Oops")).when(createMeasuresTable).execute();
139
140     TestRequest request = tester.newRequest()
141       .setParam(PARAM_ENABLE, "true");
142
143     assertThatExceptionOfType(RuntimeException.class)
144       .isThrownBy(request::execute);
145
146     assertThat(getPropertyValue()).isNull();
147   }
148
149   @Test
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"));
153
154     TestResponse response = tester.newRequest()
155       .setParam(PARAM_ENABLE, disableParamValue)
156       .execute();
157
158     assertThat(response.getStatus()).isEqualTo(200);
159     assertThat(getPropertyValue()).isFalse();
160
161     verifyNoInteractions(createMeasuresTable, addMeasuresMigratedColumnToPortfoliosTable, addMeasuresMigratedColumnToProjectBranchesTable,
162       createIndexOnProjectBranchesMeasuresMigrated, createIndexOnPortfoliosMeasuresMigrated);
163
164     // reentrant
165     response = tester.newRequest()
166       .setParam(PARAM_ENABLE, disableParamValue)
167       .execute();
168
169     assertThat(response.getStatus()).isEqualTo(200);
170     assertThat(getPropertyValue()).isFalse();
171   }
172
173   private Boolean getPropertyValue() {
174     PropertyDto propertyDto = dbTester.getDbClient().propertiesDao().selectGlobalProperty(SYSTEM_MEASURES_MIGRATION_ENABLED);
175     if (propertyDto == null) {
176       return null;
177     }
178     return Boolean.parseBoolean(propertyDto.getValue());
179   }
180
181 }