]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8857 add organization to response of api/qualityprofiles/restore
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 15 Mar 2017 09:31:08 +0000 (10:31 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Mar 2017 16:38:34 +0000 (17:38 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BulkChangeResult.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileReset.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRestoreSummary.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/OldRestoreAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RestoreActionTest.java

index 272e9f90f20a96b3154d8e84c43575d6a6310225..2ca34485ab3e2084d6f653baadc4a092f5ff0496 100644 (file)
  */
 package org.sonar.server.qualityprofile;
 
-import com.google.common.collect.Lists;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.sonar.db.qualityprofile.QualityProfileDto;
 
 public class BulkChangeResult {
 
-  private final QualityProfileDto profile;
   private final List<String> errors = new ArrayList<>();
   private int succeeded = 0;
   private int failed = 0;
-  private final List<ActiveRuleChange> changes = Lists.newArrayList();
-
-  public BulkChangeResult() {
-    this(null);
-  }
-
-  public BulkChangeResult(@Nullable QualityProfileDto profile) {
-    this.profile = profile;
-  }
-
-  @CheckForNull
-  public QualityProfileDto profile() {
-    return profile;
-  }
+  private final List<ActiveRuleChange> changes = new ArrayList<>();
 
   public List<String> getErrors() {
     return errors;
index cb8cb04cf2b0a58f05c529d1f8c4d986c415111e..553804bbef0c64b8a61664f7397e849726040d3f 100644 (file)
@@ -37,6 +37,6 @@ public interface QProfileBackuper {
    * Restore a profile backup in the specified organization. The parameter {@code overriddenProfileName}
    * is the name of the profile to be used. If null then name is loaded from backup.
    */
-  BulkChangeResult restore(DbSession dbSession, Reader backup, OrganizationDto organization, @Nullable String overriddenProfileName);
+  QProfileRestoreSummary restore(DbSession dbSession, Reader backup, OrganizationDto organization, @Nullable String overriddenProfileName);
 
 }
index 807732db8272f9271163eef29217ebac76461d7b..45158135e6d0dd90f2f0462c20bf30c1440408a7 100644 (file)
@@ -96,7 +96,7 @@ public class QProfileBackuperImpl implements QProfileBackuper {
   }
 
   @Override
-  public BulkChangeResult restore(DbSession dbSession, Reader backup, OrganizationDto organization, @Nullable String overriddenProfileName) {
+  public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, OrganizationDto organization, @Nullable String overriddenProfileName) {
     try {
       String profileLang = null;
       String profileName = null;
@@ -129,7 +129,7 @@ public class QProfileBackuperImpl implements QProfileBackuper {
     }
   }
 
-  private List<RuleActivation> parseRuleActivations(SMInputCursor rulesCursor) throws XMLStreamException {
+  private static List<RuleActivation> parseRuleActivations(SMInputCursor rulesCursor) throws XMLStreamException {
     List<RuleActivation> activations = Lists.newArrayList();
     Set<RuleKey> activatedKeys = Sets.newHashSet();
     List<RuleKey> duplicatedKeys = Lists.newArrayList();
index 1705f16ae37a619d435b73b2179687df32b9f8fd..4c509b65c0116f8aa6658639a05de2543255745e 100644 (file)
@@ -107,9 +107,10 @@ public class QProfileReset {
   /**
    * Reset the profile, which is created if it does not exist
    */
-  BulkChangeResult reset(DbSession dbSession, OrganizationDto organization, QProfileName profileName, Collection<RuleActivation> activations) {
+  QProfileRestoreSummary reset(DbSession dbSession, OrganizationDto organization, QProfileName profileName, Collection<RuleActivation> activations) {
     QualityProfileDto profile = factory.getOrCreate(dbSession, organization, profileName);
-    return doReset(dbSession, profile, activations);
+    BulkChangeResult ruleChanges = doReset(dbSession, profile, activations);
+    return new QProfileRestoreSummary(organization, profile, ruleChanges);
   }
 
   /**
@@ -118,7 +119,7 @@ public class QProfileReset {
    */
   private BulkChangeResult doReset(DbSession dbSession, QualityProfileDto profile, Collection<RuleActivation> activations) {
     Preconditions.checkNotNull(profile.getId(), "Quality profile must be persisted");
-    BulkChangeResult result = new BulkChangeResult(profile);
+    BulkChangeResult result = new BulkChangeResult();
     Set<RuleKey> ruleToBeDeactivated = Sets.newHashSet();
     // Keep reference to all the activated rules before backup restore
     for (ActiveRuleDto activeRuleDto : db.activeRuleDao().selectByProfileKey(dbSession, profile.getKee())) {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRestoreSummary.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileRestoreSummary.java
new file mode 100644 (file)
index 0000000..a703f75
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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 org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.qualityprofile.QualityProfileDto;
+
+import static java.util.Objects.requireNonNull;
+
+public final class QProfileRestoreSummary {
+  private final OrganizationDto organization;
+  private final QualityProfileDto profile;
+  private final BulkChangeResult ruleChanges;
+
+  public QProfileRestoreSummary(OrganizationDto organization, QualityProfileDto profile, BulkChangeResult ruleChanges) {
+    this.organization = requireNonNull(organization);
+    this.profile = requireNonNull(profile);
+    this.ruleChanges = requireNonNull(ruleChanges);
+  }
+
+  public OrganizationDto getOrganization() {
+    return organization;
+  }
+
+  public QualityProfileDto getProfile() {
+    return profile;
+  }
+
+  public BulkChangeResult getRuleChanges() {
+    return ruleChanges;
+  }
+}
index 0b0eca3e0fdaa25381e14a85d233dc28a252ba3e..1221609c3b945a35e945433277d207b94a2a22a4 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.qualityprofile.BulkChangeResult;
 import org.sonar.server.qualityprofile.QProfileBackuper;
+import org.sonar.server.qualityprofile.QProfileRestoreSummary;
 import org.sonar.server.ws.WsAction;
 
 import static com.google.common.base.Preconditions.checkArgument;
@@ -87,7 +88,7 @@ public class OldRestoreAction implements WsAction {
       checkArgument(backup != null, "A backup file must be provided");
       reader = new InputStreamReader(backup, StandardCharsets.UTF_8);
       OrganizationDto defaultOrg = qProfileWsSupport.getOrganizationByKey(dbSession, null);
-      BulkChangeResult result = backuper.restore(dbSession, reader, defaultOrg,null);
+      QProfileRestoreSummary result = backuper.restore(dbSession, reader, defaultOrg,null);
       writeResponse(response.newJsonWriter(), result);
     } finally {
       IOUtils.closeQuietly(reader);
@@ -95,9 +96,8 @@ public class OldRestoreAction implements WsAction {
     }
   }
 
-  private void writeResponse(JsonWriter json, BulkChangeResult result) {
-    QualityProfileDto profile = result.profile();
-    if (profile != null) {
+  private void writeResponse(JsonWriter json, QProfileRestoreSummary result) {
+    QualityProfileDto profile = result.getProfile();
       String languageKey = profile.getLanguage();
       Language language = languages.get(languageKey);
 
@@ -112,9 +112,10 @@ public class OldRestoreAction implements WsAction {
         jsonProfile.prop("languageName", language.getName());
       }
       jsonProfile.endObject();
-    }
-    json.prop("ruleSuccesses", result.countSucceeded());
-    json.prop("ruleFailures", result.countFailed());
+
+    BulkChangeResult ruleChanges = result.getRuleChanges();
+    json.prop("ruleSuccesses", ruleChanges.countSucceeded());
+    json.prop("ruleFailures", ruleChanges.countFailed());
     json.endObject().close();
   }
 }
index 35d220ea277e944a7a9fea4cdb85420d8f0539a0..1576014c2b6193a82b0da3fb3c95f8848810a32a 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.db.permission.OrganizationPermission;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.qualityprofile.BulkChangeResult;
 import org.sonar.server.qualityprofile.QProfileBackuper;
+import org.sonar.server.qualityprofile.QProfileRestoreSummary;
 import org.sonar.server.user.UserSession;
 
 import static com.google.common.base.Preconditions.checkArgument;
@@ -92,34 +93,35 @@ public class RestoreAction implements QProfileWsAction {
       OrganizationDto organization = wsSupport.getOrganizationByKey(dbSession, organizationKey);
       userSession.checkPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
 
-      BulkChangeResult result = backuper.restore(dbSession, reader, organization, null);
-      writeResponse(response.newJsonWriter(), result);
+      QProfileRestoreSummary summary = backuper.restore(dbSession, reader, organization, null);
+      writeResponse(response.newJsonWriter(), summary);
     } finally {
       IOUtils.closeQuietly(reader);
       IOUtils.closeQuietly(backup);
     }
   }
 
-  private void writeResponse(JsonWriter json, BulkChangeResult result) {
-    QualityProfileDto profile = result.profile();
-    if (profile != null) {
-      String languageKey = profile.getLanguage();
-      Language language = languages.get(languageKey);
-
-      JsonWriter jsonProfile = json.beginObject().name("profile").beginObject();
-      jsonProfile
-        .prop("key", profile.getKey())
-        .prop("name", profile.getName())
-        .prop("language", languageKey)
-        .prop("isDefault", false)
-        .prop("isInherited", false);
-      if (language != null) {
-        jsonProfile.prop("languageName", language.getName());
-      }
-      jsonProfile.endObject();
+  private void writeResponse(JsonWriter json, QProfileRestoreSummary summary) {
+    QualityProfileDto profile = summary.getProfile();
+    String languageKey = profile.getLanguage();
+    Language language = languages.get(languageKey);
+
+    JsonWriter jsonProfile = json.beginObject().name("profile").beginObject();
+    jsonProfile
+      .prop("organization", summary.getOrganization().getKey())
+      .prop("key", profile.getKey())
+      .prop("name", profile.getName())
+      .prop("language", languageKey)
+      .prop("isDefault", false)
+      .prop("isInherited", false);
+    if (language != null) {
+      jsonProfile.prop("languageName", language.getName());
     }
-    json.prop("ruleSuccesses", result.countSucceeded());
-    json.prop("ruleFailures", result.countFailed());
+    jsonProfile.endObject();
+
+    BulkChangeResult ruleChanges = summary.getRuleChanges();
+    json.prop("ruleSuccesses", ruleChanges.countSucceeded());
+    json.prop("ruleFailures", ruleChanges.countFailed());
     json.endObject().close();
   }
 }
index 9bf7869a72ada87e323bcca2a2075780d28c9106..6bb5c652afa8a78ac2f814ec329fa386b4728734 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.organization.TestDefaultOrganizationProvider;
 import org.sonar.server.qualityprofile.BulkChangeResult;
 import org.sonar.server.qualityprofile.QProfileBackuper;
+import org.sonar.server.qualityprofile.QProfileRestoreSummary;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.TestResponse;
@@ -91,11 +92,12 @@ public class RestoreActionTest {
     logInAsQProfileAdministrator(db.getDefaultOrganization());
     TestResponse response = restore("<backup/>", null);
 
-    assertThat(backuper.restoredOrganization.getUuid()).isEqualTo(db.getDefaultOrganization().getUuid());
+    assertThat(backuper.restoredSummary.getOrganization().getUuid()).isEqualTo(db.getDefaultOrganization().getUuid());
     assertThat(backuper.restoredBackup).isEqualTo("<backup/>");
-    assertThat(backuper.restoredProfile.getName()).isEqualTo("the-name-in-backup");
+    assertThat(backuper.restoredSummary.getProfile().getName()).isEqualTo("the-name-in-backup");
     JsonAssert.assertJson(response.getInput()).isSimilarTo("{" +
       "  \"profile\": {" +
+      "    \"organization\": \"" + db.getDefaultOrganization().getKey() + "\"," +
       "    \"name\": \"the-name-in-backup\"," +
       "    \"language\": \"xoo\"," +
       "    \"languageName\": \"Xoo\"," +
@@ -113,11 +115,12 @@ public class RestoreActionTest {
     logInAsQProfileAdministrator(org);
     TestResponse response = restore("<backup/>", org.getKey());
 
-    assertThat(backuper.restoredOrganization.getUuid()).isEqualTo(org.getUuid());
+    assertThat(backuper.restoredSummary.getOrganization().getUuid()).isEqualTo(org.getUuid());
     assertThat(backuper.restoredBackup).isEqualTo("<backup/>");
-    assertThat(backuper.restoredProfile.getName()).isEqualTo("the-name-in-backup");
+    assertThat(backuper.restoredSummary.getProfile().getName()).isEqualTo("the-name-in-backup");
     JsonAssert.assertJson(response.getInput()).isSimilarTo("{" +
       "  \"profile\": {" +
+      "    \"organization\": \"" + org.getKey() + "\"," +
       "    \"name\": \"the-name-in-backup\"," +
       "    \"language\": \"xoo\"," +
       "    \"languageName\": \"Xoo\"," +
@@ -129,6 +132,7 @@ public class RestoreActionTest {
       "}");
 
   }
+
   @Test
   public void throw_IAE_if_backup_is_missing() throws Exception {
     logInAsQProfileAdministrator(db.getDefaultOrganization());
@@ -201,8 +205,7 @@ public class RestoreActionTest {
   private static class TestBackuper implements QProfileBackuper {
 
     private String restoredBackup;
-    private OrganizationDto restoredOrganization;
-    private QualityProfileDto restoredProfile;
+    private QProfileRestoreSummary restoredSummary;
 
     @Override
     public void backup(DbSession dbSession, QualityProfileDto profile, Writer backupWriter) {
@@ -210,21 +213,21 @@ public class RestoreActionTest {
     }
 
     @Override
-    public BulkChangeResult restore(DbSession dbSession, Reader backup, OrganizationDto organization, @Nullable String overriddenProfileName) {
-      if (restoredProfile != null) {
+    public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, OrganizationDto organization, @Nullable String overriddenProfileName) {
+      if (restoredSummary != null) {
         throw new IllegalStateException("Already restored");
       }
       try {
-        this.restoredBackup = IOUtils.toString(backup);
+        restoredBackup = IOUtils.toString(backup);
       } catch (IOException e) {
         throw new IllegalStateException(e);
       }
-      this.restoredOrganization = organization;
-      restoredProfile = QualityProfileDto.createFor("P1")
+      QualityProfileDto profile = QualityProfileDto.createFor("P1")
         .setDefault(false)
         .setLanguage("xoo")
         .setName(overriddenProfileName != null ? overriddenProfileName : "the-name-in-backup");
-      return new BulkChangeResult(restoredProfile);
+      restoredSummary = new QProfileRestoreSummary(organization, profile, new BulkChangeResult());
+      return restoredSummary;
     }
   }
 }