]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4923 Create one common profile result object instead of having 2
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 15 Jan 2014 16:09:04 +0000 (17:09 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 15 Jan 2014 16:09:04 +0000 (17:09 +0100)
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackup.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileResult.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackupTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java

index e55d6b03e0b5a149ab0160360f3f3a43ec7f6cae..4edd170a5a32c6c9d4ee0fc7be14c0dfd530c100 100644 (file)
@@ -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;
-    }
-  }
 }
index f6d22438cea1f0747bd83a6cc9542f9d3412a28c..aae23b1fb5ff805301abbcd43d65c006b3921689 100644 (file)
@@ -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 (file)
index 0000000..9f73153
--- /dev/null
@@ -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;
+  }
+
+}
index e082dadaaa84e8de158126fba50c7ccb24878b4a..b0cc11a853e25e79f8265c1e2019d4a2ebd01755 100644 (file)
@@ -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());
   }
 
index d18db5938711c720ee62f2c861657e3771857246..e29e05f5ed612916c9595c8c7a641759ab33a513 100644 (file)
@@ -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));
index 060bd288af9ff174c0565fa75a35471369d03f2d..75706e7cb3878b1b75cfc5a55e3e9ffbf8476383 100644 (file)
@@ -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");
 
@@ -205,6 +205,35 @@ public class QProfileOperationsTest {
     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 {