summaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2014-01-15 17:09:04 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2014-01-15 17:09:04 +0100
commit0e4b58bb419fee422eb1bb873031af4b1ce9ba70 (patch)
treed28e80d10e8553097d23dec1cce23c9e2103fafd /sonar-server
parent3d5df31e4f4089e90ce073961981729ccb15c528 (diff)
downloadsonarqube-0e4b58bb419fee422eb1bb873031af4b1ce9ba70.tar.gz
sonarqube-0e4b58bb419fee422eb1bb873031af4b1ce9ba70.zip
SONAR-4923 Create one common profile result object instead of having 2
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java45
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java48
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileResult.java66
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java4
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java8
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java31
6 files changed, 109 insertions, 93 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java
index e55d6b03e0b..4edd170a5a3 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java
@@ -62,10 +62,10 @@ public class QProfileBackup implements ServerComponent {
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);
@@ -100,7 +100,7 @@ public class QProfileBackup implements ServerComponent {
}
}
- 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()) {
@@ -117,43 +117,4 @@ public class QProfileBackup implements ServerComponent {
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;
- }
- }
}
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java
index f6d22438cea..aae23b1fb5f 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java
@@ -86,12 +86,12 @@ public class QProfileOperations implements ServerComponent {
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);
@@ -188,7 +188,7 @@ public class QProfileOperations implements ServerComponent {
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()) {
@@ -227,7 +227,7 @@ public class QProfileOperations implements ServerComponent {
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()) {
@@ -275,44 +275,4 @@ public class QProfileOperations implements ServerComponent {
}
}
- 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;
- }
- }
-
}
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileResult.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileResult.java
new file mode 100644
index 00000000000..9f7315329f4
--- /dev/null
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileResult.java
@@ -0,0 +1,66 @@
+/*
+ * 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;
+ }
+
+}
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java
index e082dadaaa8..b0cc11a853e 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java
@@ -114,7 +114,7 @@ public class QProfiles implements ServerComponent {
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());
@@ -147,7 +147,7 @@ public class QProfiles implements ServerComponent {
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());
}
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java
index d18db593871..e29e05f5ed6 100644
--- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java
@@ -103,7 +103,7 @@ public class QProfileBackupTest {
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);
@@ -157,7 +157,7 @@ public class QProfileBackupTest {
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));
@@ -184,7 +184,7 @@ public class QProfileBackupTest {
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();
@@ -230,7 +230,7 @@ public class QProfileBackupTest {
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));
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java
index 060bd288af9..75706e7cb38 100644
--- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java
@@ -129,7 +129,7 @@ public class QProfileOperationsTest {
@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");
@@ -206,6 +206,35 @@ public class QProfileOperationsTest {
}
@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 {
Map<String, String> xmlProfilesByPlugin = newHashMap();