From c449191069573ef348bdd5a0c43f2593bd84f198 Mon Sep 17 00:00:00 2001 From: Guillaume Jambet Date: Tue, 15 May 2018 11:53:31 +0200 Subject: [PATCH] SONAR-10653 - api/qualityprofiles/restore handle badly formed xml document (#244) --- .../qualityprofile/QProfileBackuperImpl.java | 2 +- .../qualityprofile/ws/RestoreAction.java | 3 +- .../QProfileBackuperImplTest.java | 30 ++++++++++++------- .../QualityProfileWsParameters.java | 1 - 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.
" + 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,30 +183,38 @@ 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(""), 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 ."); 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(""), 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 ."); 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 = "\"profil\"\"language\" r.setIsExternal(true)).getId(); OrganizationDto organization = db.organizations().insert(); Reader backup = new StringReader("" + 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"; -- 2.39.5