this.dryRunCache = dryRunCache;
}
- public Result restore(String xmlBackup, boolean deleteExisting, UserSession userSession) {
+ public QProfileResult restore(String xmlBackup, boolean deleteExisting, UserSession userSession) {
checkPermission(userSession);
- Result result = new Result();
+ QProfileResult result = new QProfileResult();
ValidationMessages messages = ValidationMessages.create();
RulesProfile importedProfile = xmlProfileParser.parse(new StringReader(xmlBackup), messages);
processValidationMessages(messages, result);
}
}
- private void processValidationMessages(ValidationMessages messages, Result result) {
+ private void processValidationMessages(ValidationMessages messages, QProfileResult result) {
if (!messages.getErrors().isEmpty()) {
List<BadRequestException.Message> errors = newArrayList();
for (String error : messages.getErrors()) {
userSession.checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
}
- public static class Result {
-
- private List<String> warnings;
- private List<String> infos;
-
- private QProfile profile;
-
- public Result() {
- warnings = newArrayList();
- infos = newArrayList();
- }
-
- public List<String> warnings() {
- return warnings;
- }
-
- public Result setWarnings(List<String> warnings) {
- this.warnings = warnings;
- return this;
- }
-
- public List<String> infos() {
- return infos;
- }
-
- public Result setInfos(List<String> infos) {
- this.infos = infos;
- return this;
- }
-
- public QProfile profile() {
- return profile;
- }
-
- public Result setProfile(QProfile profile) {
- this.profile = profile;
- return this;
- }
- }
}
this.profilesManager = profilesManager;
}
- public NewProfileResult newProfile(String name, String language, Map<String, String> xmlProfilesByPlugin, UserSession userSession) {
+ public QProfileResult newProfile(String name, String language, Map<String, String> xmlProfilesByPlugin, UserSession userSession) {
SqlSession session = myBatis.openSession();
try {
QProfile profile = newProfile(name, language, userSession, session);
- NewProfileResult result = new NewProfileResult();
+ QProfileResult result = new QProfileResult();
List<RulesProfile> importProfiles = readProfilesFromXml(result, xmlProfilesByPlugin);
for (RulesProfile rulesProfile : importProfiles) {
importProfile(profile.id(), rulesProfile, session);
return null;
}
- private List<RulesProfile> readProfilesFromXml(NewProfileResult result, Map<String, String> xmlProfilesByPlugin) {
+ private List<RulesProfile> readProfilesFromXml(QProfileResult result, Map<String, String> xmlProfilesByPlugin) {
List<RulesProfile> profiles = newArrayList();
ValidationMessages messages = ValidationMessages.create();
for (Map.Entry<String, String> entry : xmlProfilesByPlugin.entrySet()) {
return null;
}
- private void processValidationMessages(ValidationMessages messages, NewProfileResult result) {
+ private void processValidationMessages(ValidationMessages messages, QProfileResult result) {
if (!messages.getErrors().isEmpty()) {
List<BadRequestException.Message> errors = newArrayList();
for (String error : messages.getErrors()) {
}
}
- public static class NewProfileResult {
-
- private List<String> warnings;
- private List<String> infos;
-
- private QProfile profile;
-
- public NewProfileResult() {
- warnings = newArrayList();
- infos = newArrayList();
- }
-
- public List<String> warnings() {
- return warnings;
- }
-
- public NewProfileResult setWarnings(List<String> warnings) {
- this.warnings = warnings;
- return this;
- }
-
- public List<String> infos() {
- return infos;
- }
-
- public NewProfileResult setInfos(List<String> infos) {
- this.infos = infos;
- return this;
- }
-
- public QProfile profile() {
- return profile;
- }
-
- public NewProfileResult setProfile(QProfile profile) {
- this.profile = profile;
- return this;
- }
- }
-
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.qualityprofile;
+
+import java.util.List;
+
+import static com.google.common.collect.Lists.newArrayList;
+
+public class QProfileResult {
+
+ private List<String> warnings;
+ private List<String> infos;
+
+ private QProfile profile;
+
+ public QProfileResult() {
+ warnings = newArrayList();
+ infos = newArrayList();
+ }
+
+ public List<String> warnings() {
+ return warnings;
+ }
+
+ public QProfileResult setWarnings(List<String> warnings) {
+ this.warnings = warnings;
+ return this;
+ }
+
+ public List<String> infos() {
+ return infos;
+ }
+
+ public QProfileResult setInfos(List<String> infos) {
+ this.infos = infos;
+ return this;
+ }
+
+ public QProfile profile() {
+ return profile;
+ }
+
+ public QProfileResult setProfile(QProfile profile) {
+ this.profile = profile;
+ return this;
+ }
+
+}
return profileLookup.countChildren(profile);
}
- public QProfileOperations.NewProfileResult newProfile(String name, String language, Map<String, String> xmlProfilesByPlugin) {
+ public QProfileResult newProfile(String name, String language, Map<String, String> xmlProfilesByPlugin) {
validateProfileName(name);
Validation.checkMandatoryParameter(language, LANGUAGE_PARAM);
return operations.newProfile(name, language, xmlProfilesByPlugin, UserSession.get());
operations.updateParentProfile(profileId, parentId, UserSession.get());
}
- public QProfileBackup.Result restore(String xmlBackup, boolean deleteExisting) {
+ public QProfileResult restore(String xmlBackup, boolean deleteExisting) {
return backup.restore(xmlBackup, deleteExisting, UserSession.get());
}
when(hibernateSession.getSingleResult(any(Class.class), eq("name"), eq("Default"), eq("language"), eq("java"))).thenReturn(null);
when(qProfileLookup.profile(anyInt())).thenReturn(new QProfile().setId(1));
- QProfileBackup.Result result = backup.restore("<xml/>", false, userSession);
+ QProfileResult result = backup.restore("<xml/>", false, userSession);
assertThat(result.profile()).isNotNull();
verify(hibernateSession).saveWithoutFlush(profile);
when(hibernateSession.getSingleResult(any(Class.class), eq("name"), eq("Default"), eq("language"), eq("java"))).thenReturn(existingProfile);
when(qProfileLookup.profile(anyInt())).thenReturn(new QProfile().setId(1));
- QProfileBackup.Result result = backup.restore("<xml/>", true, userSession);
+ QProfileResult result = backup.restore("<xml/>", true, userSession);
assertThat(result.profile()).isNotNull();
verify(hibernateSession).removeWithoutFlush(eq(existingProfile));
when(hibernateSession.getSingleResult(any(Class.class), eq("name"), eq("Default"), eq("language"), eq("java"))).thenReturn(null);
when(qProfileLookup.profile(anyInt())).thenReturn(new QProfile().setId(1));
- QProfileBackup.Result result = backup.restore("<xml/>", true, userSession);
+ QProfileResult result = backup.restore("<xml/>", true, userSession);
assertThat(result.profile()).isNotNull();
assertThat(result.warnings()).isNotEmpty();
when(hibernateSession.getSingleResult(any(Class.class), eq("name"), eq("Default"), eq("language"), eq("java"))).thenReturn(null);
when(qProfileLookup.profile(anyInt())).thenReturn(new QProfile().setId(1));
- QProfileBackup.Result result = backup.restore("<xml/>", false, userSession);
+ QProfileResult result = backup.restore("<xml/>", false, userSession);
assertThat(result.profile()).isNull();
verify(hibernateSession, never()).saveWithoutFlush(any(RulesProfile.class));
@Test
public void create_profile() throws Exception {
- QProfileOperations.NewProfileResult result = operations.newProfile("Default", "java", Maps.<String, String>newHashMap(), authorizedUserSession);
+ QProfileResult result = operations.newProfile("Default", "java", Maps.<String, String>newHashMap(), authorizedUserSession);
assertThat(result.profile().name()).isEqualTo("Default");
assertThat(result.profile().language()).isEqualTo("java");
verify(ruleRegistry).bulkIndexActiveRules(anyListOf(ActiveRuleDto.class), any(Multimap.class));
}
+ @Test
+ public void create_profile_from_xml_plugin_add_infos_and_warnings() throws Exception {
+ final RulesProfile profile = RulesProfile.create("Default", "java");
+ Rule rule = Rule.create("pmd", "rule1");
+ rule.createParameter("max");
+ rule.setId(10);
+ ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
+ activeRule.setParameter("max", "10");
+
+ Map<String, String> xmlProfilesByPlugin = newHashMap();
+ xmlProfilesByPlugin.put("pmd", "<xml/>");
+ ProfileImporter importer = mock(ProfileImporter.class);
+ when(importer.getKey()).thenReturn("pmd");
+ doAnswer(new Answer() {
+ public Object answer(InvocationOnMock invocation) {
+ Object[] args = invocation.getArguments();
+ ValidationMessages validationMessages = (ValidationMessages) args[1];
+ validationMessages.addInfoText("an info message");
+ validationMessages.addWarningText("a warning message");
+ return profile;
+ }
+ }).when(importer).importProfile(any(Reader.class), any(ValidationMessages.class));
+ importers.add(importer);
+
+ QProfileResult result = operations.newProfile("Default", "java", xmlProfilesByPlugin, authorizedUserSession);
+ assertThat(result.infos()).hasSize(1);
+ assertThat(result.warnings()).hasSize(1);
+ }
+
@Test
public void fail_to_create_profile_from_xml_plugin_if_error() throws Exception {
try {