3 * Copyright (C) 2009-2020 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.qualityprofile.ws;
22 import java.io.IOException;
23 import java.io.Reader;
24 import java.io.Writer;
25 import javax.annotation.Nullable;
26 import org.apache.commons.io.IOUtils;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.sonar.api.resources.Languages;
31 import org.sonar.api.server.ws.WebService;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.organization.OrganizationDto;
35 import org.sonar.db.qualityprofile.QProfileDto;
36 import org.sonar.server.exceptions.ForbiddenException;
37 import org.sonar.server.exceptions.UnauthorizedException;
38 import org.sonar.server.language.LanguageTesting;
39 import org.sonar.server.organization.DefaultOrganizationProvider;
40 import org.sonar.server.organization.TestDefaultOrganizationProvider;
41 import org.sonar.server.qualityprofile.BulkChangeResult;
42 import org.sonar.server.qualityprofile.QProfileBackuper;
43 import org.sonar.server.qualityprofile.QProfileRestoreSummary;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.ws.TestRequest;
46 import org.sonar.server.ws.TestResponse;
47 import org.sonar.server.ws.WsActionTester;
48 import org.sonar.test.JsonAssert;
50 import static org.assertj.core.api.Assertions.assertThat;
51 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
53 public class RestoreActionTest {
55 private static final String A_LANGUAGE = "xoo";
58 public ExpectedException expectedException = ExpectedException.none();
60 public DbTester db = DbTester.create();
62 public UserSessionRule userSession = UserSessionRule.standalone();
64 private TestBackuper backuper = new TestBackuper();
65 private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
66 private Languages languages = LanguageTesting.newLanguages(A_LANGUAGE);
67 private WsActionTester tester = new WsActionTester(new RestoreAction(db.getDbClient(), backuper, languages, userSession));
70 public void test_definition() {
71 WebService.Action definition = tester.getDef();
73 assertThat(definition.key()).isEqualTo("restore");
74 assertThat(definition.isPost()).isTrue();
75 assertThat(definition.responseExampleAsString()).isNull();
76 assertThat(definition.description()).isNotEmpty();
79 assertThat(definition.params()).hasSize(1);
80 WebService.Param backupParam = definition.param("backup");
81 assertThat(backupParam.isRequired()).isTrue();
82 assertThat(backupParam.since()).isNull();
86 public void profile_is_restored_on_default_organization_with_the_name_provided_in_backup() {
87 logInAsQProfileAdministrator();
88 TestResponse response = restore("<backup/>", null);
90 assertThat(backuper.restoredBackup).isEqualTo("<backup/>");
91 assertThat(backuper.restoredSummary.getProfile().getName()).isEqualTo("the-name-in-backup");
92 JsonAssert.assertJson(response.getInput()).isSimilarTo("{" +
94 " \"name\": \"the-name-in-backup\"," +
95 " \"language\": \"xoo\"," +
96 " \"languageName\": \"Xoo\"," +
97 " \"isDefault\": false," +
98 " \"isInherited\": false" +
100 " \"ruleSuccesses\": 0," +
101 " \"ruleFailures\": 0" +
106 public void profile_is_restored_on_specified_organization_with_the_name_provided_in_backup() {
107 OrganizationDto org = db.organizations().insert();
108 logInAsQProfileAdministrator();
109 TestResponse response = restore("<backup/>", org.getKey());
111 assertThat(backuper.restoredBackup).isEqualTo("<backup/>");
112 assertThat(backuper.restoredSummary.getProfile().getName()).isEqualTo("the-name-in-backup");
113 JsonAssert.assertJson(response.getInput()).isSimilarTo("{" +
115 " \"name\": \"the-name-in-backup\"," +
116 " \"language\": \"xoo\"," +
117 " \"languageName\": \"Xoo\"," +
118 " \"isDefault\": false," +
119 " \"isInherited\": false" +
121 " \"ruleSuccesses\": 0," +
122 " \"ruleFailures\": 0" +
128 public void throw_IAE_if_backup_is_missing() {
129 logInAsQProfileAdministrator();
131 expectedException.expect(IllegalArgumentException.class);
132 expectedException.expectMessage("A backup file must be provided");
140 public void throw_ForbiddenException_if_not_profile_administrator() {
143 expectedException.expect(ForbiddenException.class);
144 expectedException.expectMessage("Insufficient privileges");
146 restore("<backup/>", null);
150 public void throw_UnauthorizedException_if_not_logged_in() {
151 userSession.anonymous();
153 expectedException.expect(UnauthorizedException.class);
154 expectedException.expectMessage("Authentication is required");
156 restore("<backup/>", null);
159 private void logInAsQProfileAdministrator() {
162 .addPermission(ADMINISTER_QUALITY_PROFILES);
165 private TestResponse restore(String backupContent, @Nullable String organizationKey) {
166 TestRequest request = tester.newRequest()
168 .setParam("backup", backupContent);
169 if (organizationKey != null) {
170 request.setParam("organization", organizationKey);
172 return request.execute();
175 private static class TestBackuper implements QProfileBackuper {
177 private String restoredBackup;
178 private QProfileRestoreSummary restoredSummary;
181 public void backup(DbSession dbSession, QProfileDto profile, Writer backupWriter) {
182 throw new UnsupportedOperationException();
186 public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, @Nullable String overriddenProfileName) {
187 if (restoredSummary != null) {
188 throw new IllegalStateException("Already restored");
191 restoredBackup = IOUtils.toString(backup);
192 } catch (IOException e) {
193 throw new IllegalStateException(e);
195 QProfileDto profile = new QProfileDto()
197 .setRulesProfileUuid("rp-P1")
199 .setName(overriddenProfileName != null ? overriddenProfileName : "the-name-in-backup");
200 restoredSummary = new QProfileRestoreSummary(profile, new BulkChangeResult());
201 return restoredSummary;
205 public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, QProfileDto profile) {
206 throw new UnsupportedOperationException();
209 @Override public QProfileRestoreSummary copy(DbSession dbSession, QProfileDto from, QProfileDto to) {
210 throw new UnsupportedOperationException();