]> source.dussan.org Git - sonarqube.git/blob
41c7ef9c31ef9d835a2df189d7988881e9defd13
[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 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;
46
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;
50
51 public class ExportActionTest {
52
53   private static final String XOO_LANGUAGE = "xoo";
54   private static final String JAVA_LANGUAGE = "java";
55
56   @Rule
57   public DbTester db = DbTester.create(System2.INSTANCE);
58   @Rule
59   public UserSessionRule userSession = UserSessionRule.standalone();
60   @Rule
61   public ExpectedException expectedException = ExpectedException.none();
62
63   private DbClient dbClient = db.getDbClient();
64   private QProfileBackuper backuper = new TestBackuper();
65
66   @Test
67   public void export_profile_in_default_organization() {
68     QProfileDto profile = createProfile(false);
69
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()
75       .getInput();
76
77     assertThat(result).isEqualTo("Profile " + profile.getLanguage() + "/" + profile.getName() + " exported by polop");
78   }
79
80   @Test
81   public void export_default_profile() {
82     QProfileDto nonDefaultProfile = createProfile(false);
83     QProfileDto defaultProfile = createProfile(true);
84
85     WsActionTester tester = newWsActionTester(newExporter("polop"), newExporter("palap"));
86     String result = tester.newRequest()
87       .setParam("language", XOO_LANGUAGE)
88       .setParam("exporterKey", "polop")
89       .execute()
90       .getInput();
91
92     assertThat(result).isEqualTo("Profile " + defaultProfile.getLanguage() + "/" + defaultProfile.getName() + " exported by polop");
93   }
94
95   @Test
96   public void return_backup_when_exporter_is_not_specified() {
97     QProfileDto profile = createProfile(false);
98
99     String result = newWsActionTester(newExporter("polop")).newRequest()
100       .setParam("language", profile.getLanguage())
101       .setParam("qualityProfile", profile.getName())
102       .execute()
103       .getInput();
104
105     assertThat(result).isEqualTo("Backup of " + profile.getLanguage() + "/" + profile.getKee());
106   }
107
108   @Test
109   public void throw_NotFoundException_if_profile_with_specified_name_does_not_exist_in_default_organization() {
110     expectedException.expect(NotFoundException.class);
111
112     newWsActionTester().newRequest()
113       .setParam("language", XOO_LANGUAGE)
114       .setParam("exporterKey", "polop").execute();
115   }
116
117   @Test
118   public void throw_IAE_if_export_with_specified_key_does_not_exist() {
119     QProfileDto profile = createProfile(true);
120
121     expectedException.expect(IllegalArgumentException.class);
122     expectedException.expectMessage("Value of parameter 'exporterKey' (unknown) must be one of: [polop, palap]");
123
124     newWsActionTester(newExporter("polop"), newExporter("palap")).newRequest()
125       .setParam("language", XOO_LANGUAGE)
126       .setParam("exporterKey", "unknown").execute();
127   }
128
129   @Test
130   public void definition_without_exporters() {
131     WebService.Action definition = newWsActionTester().getDef();
132
133     assertThat(definition.isPost()).isFalse();
134     assertThat(definition.isInternal()).isFalse();
135     assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("language", "qualityProfile");
136
137     WebService.Param name = definition.param("qualityProfile");
138     assertThat(name.deprecatedSince()).isNullOrEmpty();
139
140     WebService.Param language = definition.param("language");
141     assertThat(language.deprecatedSince()).isNullOrEmpty();
142   }
143
144   @Test
145   public void definition_with_exporters() {
146     WebService.Action definition = newWsActionTester(newExporter("polop"), newExporter("palap")).getDef();
147
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();
156   }
157
158   private QProfileDto createProfile(boolean isDefault) {
159     QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO_LANGUAGE));
160     if (isDefault) {
161       db.qualityProfiles().setAsDefault(profile);
162     }
163     return profile;
164   }
165
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)));
169   }
170
171   private static ProfileExporter newExporter(String key) {
172     return new ProfileExporter(key, StringUtils.capitalize(key)) {
173       @Override
174       public String getMimeType() {
175         return "text/plain+" + key;
176       }
177
178       @Override
179       public void exportProfile(RulesProfile profile, Writer writer) {
180         try {
181           writer.write(format("Profile %s/%s exported by %s", profile.getLanguage(), profile.getName(), key));
182         } catch (IOException ioe) {
183           throw new RuntimeException(ioe);
184         }
185       }
186     };
187   }
188
189   private static class TestBackuper implements QProfileBackuper {
190
191     @Override
192     public void backup(DbSession dbSession, QProfileDto profile, Writer backupWriter) {
193       try {
194         backupWriter.write(format("Backup of %s/%s", profile.getLanguage(), profile.getKee()));
195       } catch (IOException e) {
196         throw new IllegalStateException(e);
197       }
198     }
199
200     @Override
201     public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, @Nullable String overriddenProfileName) {
202       throw new UnsupportedOperationException();
203     }
204
205     @Override
206     public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, QProfileDto profile) {
207       throw new UnsupportedOperationException();
208     }
209
210     @Override
211     public QProfileRestoreSummary copy(DbSession dbSession, QProfileDto from, QProfileDto to) {
212       throw new UnsupportedOperationException();
213     }
214   }
215 }