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 org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.resources.Languages;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.server.ws.WebService.Param;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.qualityprofile.QProfileDto;
29 import org.sonar.server.language.LanguageTesting;
30 import org.sonar.server.qualityprofile.QProfileBackuper;
31 import org.sonar.server.qualityprofile.QProfileBackuperImpl;
32 import org.sonar.server.qualityprofile.QProfileParser;
33 import org.sonar.server.tester.UserSessionRule;
34 import org.sonar.server.ws.TestRequest;
35 import org.sonar.server.ws.TestResponse;
36 import org.sonar.server.ws.WsActionTester;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.assertThatThrownBy;
41 public class BackupActionTest {
43 private static final String A_LANGUAGE = "xoo";
46 public DbTester db = DbTester.create();
48 public UserSessionRule userSession = UserSessionRule.standalone();
50 private QProfileBackuper backuper = new QProfileBackuperImpl(db.getDbClient(), null, null, null, new QProfileParser());
51 private final QProfileWsSupport wsSupport = new QProfileWsSupport(db.getDbClient(), userSession);
52 private Languages languages = LanguageTesting.newLanguages(A_LANGUAGE);
53 private WsActionTester tester = new WsActionTester(new BackupAction(db.getDbClient(), backuper, wsSupport, languages));
56 public void returns_backup_of_profile_with_specified_key() {
57 QProfileDto profile = db.qualityProfiles().insert(qp -> qp.setLanguage("xoo"));
59 TestResponse response = tester.newRequest()
60 .setParam("language", profile.getLanguage())
61 .setParam("qualityProfile", profile.getName())
63 assertThat(response.getMediaType()).isEqualTo("application/xml");
64 assertThat(response.getInput()).isXmlEqualTo(xmlForProfileWithoutRules(profile));
65 assertThat(response.getHeader("Content-Disposition")).isEqualTo("attachment; filename=" + profile.getKee() + ".xml");
69 public void returns_backup_of_profile_with_specified_name_on_default_organization() {
70 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(A_LANGUAGE));
72 TestResponse response = tester.newRequest()
73 .setParam("language", profile.getLanguage())
74 .setParam("qualityProfile", profile.getName())
76 assertThat(response.getInput()).isXmlEqualTo(xmlForProfileWithoutRules(profile));
80 public void throws_IAE_if_profile_reference_is_not_set() {
81 TestRequest request = tester.newRequest();
83 assertThatThrownBy(request::execute)
84 .isInstanceOf(IllegalArgumentException.class);
88 public void test_definition() {
89 WebService.Action definition = tester.getDef();
91 assertThat(definition.key()).isEqualTo("backup");
92 assertThat(definition.responseExampleAsString()).isNotEmpty();
93 assertThat(definition.isInternal()).isFalse();
94 assertThat(definition.isPost()).isFalse();
97 assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("qualityProfile", "language");
98 Param language = definition.param("language");
99 assertThat(language.deprecatedSince()).isNullOrEmpty();
102 private static String xmlForProfileWithoutRules(QProfileDto profile) {
103 return "<?xml version='1.0' encoding='UTF-8'?>" +
105 " <name>" + profile.getName() + "</name>" +
106 " <language>" + profile.getLanguage() + "</language>" +