diff options
author | Guillaume Jambet <guillaume.jambet@gmail.com> | 2018-05-15 11:53:31 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-05-15 20:20:50 +0200 |
commit | c449191069573ef348bdd5a0c43f2593bd84f198 (patch) | |
tree | 4c7c6aa45973bd0fd7eb8048ef591d1909e598a8 /server | |
parent | 34db3b05cf4dcdb11faa5b392837cc4d979d19ff (diff) | |
download | sonarqube-c449191069573ef348bdd5a0c43f2593bd84f198.tar.gz sonarqube-c449191069573ef348bdd5a0c43f2593bd84f198.zip |
SONAR-10653 - api/qualityprofiles/restore handle badly formed xml document (#244)
Diffstat (limited to 'server')
3 files changed, 21 insertions, 14 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'?>" + |