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.lang.StringUtils;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.sonar.api.profiles.ProfileExporter;
31 import org.sonar.api.profiles.RulesProfile;
32 import org.sonar.api.server.ws.WebService;
33 import org.sonar.api.utils.System2;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.qualityprofile.QProfileDto;
38 import org.sonar.db.user.UserDto;
39 import org.sonar.server.exceptions.NotFoundException;
40 import org.sonar.server.language.LanguageTesting;
41 import org.sonar.server.qualityprofile.QProfileBackuper;
42 import org.sonar.server.qualityprofile.QProfileExporters;
43 import org.sonar.server.qualityprofile.QProfileRestoreSummary;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.ws.WsActionTester;
47 import static java.lang.String.format;
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.sonar.db.organization.OrganizationDto.Subscription.PAID;
51 public class ExportActionTest {
53 private static final String XOO_LANGUAGE = "xoo";
54 private static final String JAVA_LANGUAGE = "java";
57 public DbTester db = DbTester.create(System2.INSTANCE);
59 public UserSessionRule userSession = UserSessionRule.standalone();
61 public ExpectedException expectedException = ExpectedException.none();
63 private DbClient dbClient = db.getDbClient();
64 private QProfileBackuper backuper = new TestBackuper();
67 public void export_profile_in_default_organization() {
68 QProfileDto profile = createProfile(false);
70 WsActionTester tester = newWsActionTester(newExporter("polop"), newExporter("palap"));
71 String result = tester.newRequest()
72 .setParam("language", profile.getLanguage())
73 .setParam("qualityProfile", profile.getName())
74 .setParam("exporterKey", "polop").execute()
77 assertThat(result).isEqualTo("Profile " + profile.getLanguage() + "/" + profile.getName() + " exported by polop");
81 public void export_default_profile() {
82 QProfileDto nonDefaultProfile = createProfile(false);
83 QProfileDto defaultProfile = createProfile(true);
85 WsActionTester tester = newWsActionTester(newExporter("polop"), newExporter("palap"));
86 String result = tester.newRequest()
87 .setParam("language", XOO_LANGUAGE)
88 .setParam("exporterKey", "polop")
92 assertThat(result).isEqualTo("Profile " + defaultProfile.getLanguage() + "/" + defaultProfile.getName() + " exported by polop");
96 public void return_backup_when_exporter_is_not_specified() {
97 QProfileDto profile = createProfile(false);
99 String result = newWsActionTester(newExporter("polop")).newRequest()
100 .setParam("language", profile.getLanguage())
101 .setParam("qualityProfile", profile.getName())
105 assertThat(result).isEqualTo("Backup of " + profile.getLanguage() + "/" + profile.getKee());
109 public void throw_NotFoundException_if_profile_with_specified_name_does_not_exist_in_default_organization() {
110 expectedException.expect(NotFoundException.class);
112 newWsActionTester().newRequest()
113 .setParam("language", XOO_LANGUAGE)
114 .setParam("exporterKey", "polop").execute();
118 public void throw_IAE_if_export_with_specified_key_does_not_exist() {
119 QProfileDto profile = createProfile(true);
121 expectedException.expect(IllegalArgumentException.class);
122 expectedException.expectMessage("Value of parameter 'exporterKey' (unknown) must be one of: [polop, palap]");
124 newWsActionTester(newExporter("polop"), newExporter("palap")).newRequest()
125 .setParam("language", XOO_LANGUAGE)
126 .setParam("exporterKey", "unknown").execute();
130 public void definition_without_exporters() {
131 WebService.Action definition = newWsActionTester().getDef();
133 assertThat(definition.isPost()).isFalse();
134 assertThat(definition.isInternal()).isFalse();
135 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("language", "qualityProfile");
137 WebService.Param name = definition.param("qualityProfile");
138 assertThat(name.deprecatedSince()).isNullOrEmpty();
140 WebService.Param language = definition.param("language");
141 assertThat(language.deprecatedSince()).isNullOrEmpty();
145 public void definition_with_exporters() {
146 WebService.Action definition = newWsActionTester(newExporter("polop"), newExporter("palap")).getDef();
148 assertThat(definition.isPost()).isFalse();
149 assertThat(definition.isInternal()).isFalse();
150 assertThat(definition.params()).extracting("key").containsExactlyInAnyOrder("language", "qualityProfile", "exporterKey");
151 WebService.Param exportersParam = definition.param("exporterKey");
152 assertThat(exportersParam.possibleValues()).containsOnly("polop", "palap");
153 assertThat(exportersParam.deprecatedKey()).isEqualTo("format");
154 assertThat(exportersParam.deprecatedKeySince()).isEqualTo("6.3");
155 assertThat(exportersParam.isInternal()).isFalse();
158 private QProfileDto createProfile(boolean isDefault) {
159 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO_LANGUAGE));
161 db.qualityProfiles().setAsDefault(profile);
166 private WsActionTester newWsActionTester(ProfileExporter... profileExporters) {
167 QProfileExporters exporters = new QProfileExporters(dbClient, null, null, profileExporters, null);
168 return new WsActionTester(new ExportAction(dbClient, backuper, exporters, LanguageTesting.newLanguages(XOO_LANGUAGE, JAVA_LANGUAGE)));
171 private static ProfileExporter newExporter(String key) {
172 return new ProfileExporter(key, StringUtils.capitalize(key)) {
174 public String getMimeType() {
175 return "text/plain+" + key;
179 public void exportProfile(RulesProfile profile, Writer writer) {
181 writer.write(format("Profile %s/%s exported by %s", profile.getLanguage(), profile.getName(), key));
182 } catch (IOException ioe) {
183 throw new RuntimeException(ioe);
189 private static class TestBackuper implements QProfileBackuper {
192 public void backup(DbSession dbSession, QProfileDto profile, Writer backupWriter) {
194 backupWriter.write(format("Backup of %s/%s", profile.getLanguage(), profile.getKee()));
195 } catch (IOException e) {
196 throw new IllegalStateException(e);
201 public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, @Nullable String overriddenProfileName) {
202 throw new UnsupportedOperationException();
206 public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, QProfileDto profile) {
207 throw new UnsupportedOperationException();
211 public QProfileRestoreSummary copy(DbSession dbSession, QProfileDto from, QProfileDto to) {
212 throw new UnsupportedOperationException();