]> source.dussan.org Git - sonarqube.git/blob
340394bc72dccb5d0cf4ebd8b0ddef21fdddceff
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.qualityprofile.ws;
21
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;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.assertThatThrownBy;
40
41 public class BackupActionTest {
42
43   private static final String A_LANGUAGE = "xoo";
44
45   @Rule
46   public DbTester db = DbTester.create();
47   @Rule
48   public UserSessionRule userSession = UserSessionRule.standalone();
49
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));
54
55   @Test
56   public void returns_backup_of_profile_with_specified_key() {
57     QProfileDto profile = db.qualityProfiles().insert(qp -> qp.setLanguage("xoo"));
58
59     TestResponse response = tester.newRequest()
60       .setParam("language", profile.getLanguage())
61       .setParam("qualityProfile", profile.getName())
62       .execute();
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");
66   }
67
68   @Test
69   public void returns_backup_of_profile_with_specified_name_on_default_organization() {
70     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(A_LANGUAGE));
71
72     TestResponse response = tester.newRequest()
73       .setParam("language", profile.getLanguage())
74       .setParam("qualityProfile", profile.getName())
75       .execute();
76     assertThat(response.getInput()).isXmlEqualTo(xmlForProfileWithoutRules(profile));
77   }
78
79   @Test
80   public void throws_IAE_if_profile_reference_is_not_set() {
81     TestRequest request = tester.newRequest();
82
83     assertThatThrownBy(request::execute)
84       .isInstanceOf(IllegalArgumentException.class);
85   }
86
87   @Test
88   public void test_definition() {
89     WebService.Action definition = tester.getDef();
90
91     assertThat(definition.key()).isEqualTo("backup");
92     assertThat(definition.responseExampleAsString()).isNotEmpty();
93     assertThat(definition.isInternal()).isFalse();
94     assertThat(definition.isPost()).isFalse();
95
96     // parameters
97     assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("qualityProfile", "language");
98     Param language = definition.param("language");
99     assertThat(language.deprecatedSince()).isNullOrEmpty();
100   }
101
102   private static String xmlForProfileWithoutRules(QProfileDto profile) {
103     return "<?xml version='1.0' encoding='UTF-8'?>" +
104       "<profile>" +
105       "  <name>" + profile.getName() + "</name>" +
106       "  <language>" + profile.getLanguage() + "</language>" +
107       "  <rules/>" +
108       "</profile>";
109   }
110
111 }