*/
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;
* 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);
}
}
@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;
}
}
- 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();
/**
* 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);
}
/**
*/
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())) {
--- /dev/null
+/*
+ * 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;
+ }
+}
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;
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);
}
}
- 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);
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();
}
}
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;
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();
}
}
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;
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\"," +
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\"," +
"}");
}
+
@Test
public void throw_IAE_if_backup_is_missing() throws Exception {
logInAsQProfileAdministrator(db.getDefaultOrganization());
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) {
}
@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;
}
}
}