aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreAction.java3
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java30
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java1
4 files changed, 21 insertions, 15 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java
index 75defe5ec58..8baa7253d0f 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java
@@ -172,7 +172,7 @@ public class QProfileBackuperImpl implements QProfileBackuper {
BulkChangeResult changes = profileReset.reset(dbSession, targetProfile, ruleActivations);
return new QProfileRestoreSummary(targetProfile, changes);
} catch (XMLStreamException e) {
- throw new IllegalStateException("Fail to restore Quality profile backup", e);
+ throw new IllegalArgumentException("Fail to restore Quality profile backup, XML document is not well formed", e);
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreAction.java
index ad9d9718851..999c13d1421 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreAction.java
@@ -41,7 +41,6 @@ import static com.google.common.base.Preconditions.checkArgument;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_ORGANIZATION;
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_RESTORE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.RestoreActionParameters.PARAM_BACKUP;
public class RestoreAction implements QProfileWsAction {
@@ -62,7 +61,7 @@ public class RestoreAction implements QProfileWsAction {
@Override
public void define(WebService.NewController controller) {
- WebService.NewAction action = controller.createAction(ACTION_RESTORE)
+ WebService.NewAction action = controller.createAction("restore")
.setSince("5.2")
.setDescription("Restore a quality profile using an XML file. The restored profile name is taken from the backup file, " +
"so if a profile with the same name and language already exists, it will be overwritten.<br> " +
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java
index deea8443d0e..5836434ae0d 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java
@@ -26,7 +26,6 @@ import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
-import javax.xml.stream.XMLStreamException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -46,6 +45,7 @@ import org.sonar.db.rule.RuleParamDto;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
+import static org.junit.rules.ExpectedException.none;
public class QProfileBackuperImplTest {
@@ -58,7 +58,7 @@ public class QProfileBackuperImplTest {
private System2 system2 = new AlwaysIncreasingSystem2();
@Rule
- public ExpectedException expectedException = ExpectedException.none();
+ public ExpectedException expectedException = none();
@Rule
public DbTester db = DbTester.create(system2);
@@ -183,31 +183,39 @@ public class QProfileBackuperImplTest {
}
@Test
- public void fail_to_restore_if_not_xml_backup() {
+ public void fail_to_restore_if_bad_xml_format() {
OrganizationDto organization = db.organizations().insert();
try {
- underTest.restore(db.getSession(), new StringReader("foo"), organization, null);
+ underTest.restore(db.getSession(), new StringReader("<rules><rule></rules>"), organization, null);
fail();
- } catch (IllegalStateException e) {
- assertThat(e).hasMessage("Fail to restore Quality profile backup");
- assertThat(e.getCause()).isInstanceOf(XMLStreamException.class);
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessage("Backup XML is not valid. Root element must be <profile>.");
assertThat(reset.calledProfile).isNull();
}
}
@Test
- public void fail_to_restore_if_bad_xml_format() {
+ public void fail_to_restore_if_not_xml_backup() {
OrganizationDto organization = db.organizations().insert();
try {
- underTest.restore(db.getSession(), new StringReader("<rules><rule></rules>"), organization, null);
+ underTest.restore(db.getSession(), new StringReader("foo"), organization, null);
fail();
} catch (IllegalArgumentException e) {
- assertThat(e).hasMessage("Backup XML is not valid. Root element must be <profile>.");
assertThat(reset.calledProfile).isNull();
}
}
@Test
+ public void fail_to_restore_if_xml_is_not_well_formed() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Fail to restore Quality profile backup, XML document is not well formed");
+ OrganizationDto organization = db.organizations().insert();
+ String notWellFormedXml = "<?xml version='1.0' encoding='UTF-8'?><profile><name>\"profil\"</name><language>\"language\"</language><rules/></profile";
+
+ underTest.restore(db.getSession(), new StringReader(notWellFormedXml), organization, null);
+ }
+
+ @Test
public void fail_to_restore_if_duplicate_rule() throws Exception {
OrganizationDto organization = db.organizations().insert();
try {
@@ -221,7 +229,7 @@ public class QProfileBackuperImplTest {
}
@Test
- public void fail_to_restore_external_rule() throws Exception {
+ public void fail_to_restore_external_rule() {
db.rules().insert(RuleKey.of("sonarjs", "s001"), r -> r.setIsExternal(true)).getId();
OrganizationDto organization = db.organizations().insert();
Reader backup = new StringReader("<?xml version='1.0' encoding='UTF-8'?>" +
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java
index 4269a589464..cfd6e7ebd42 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java
@@ -41,7 +41,6 @@ public class QualityProfileWsParameters {
public static final String ACTION_REMOVE_PROJECT = "remove_project";
public static final String ACTION_REMOVE_GROUP = "remove_group";
public static final String ACTION_REMOVE_USER = "remove_user";
- public static final String ACTION_RESTORE = "restore";
public static final String ACTION_SEARCH = "search";
public static final String ACTION_SEARCH_USERS = "search_users";
public static final String ACTION_SEARCH_GROUPS = "search_groups";