]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10653 - api/qualityprofiles/restore handle badly formed xml document (#244)
authorGuillaume Jambet <guillaume.jambet@gmail.com>
Tue, 15 May 2018 09:53:31 +0000 (11:53 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 15 May 2018 18:20:50 +0000 (20:20 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuperImpl.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperImplTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java

index 75defe5ec5856883f7c69232bb389526cd3219df..8baa7253d0fbc8fdec96ffe63421f572f2205785 100644 (file)
@@ -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);
     }
   }
 
index ad9d9718851596265536e18a4422e8a2c17680c6..999c13d1421f5bd9f20060e23c885030371ee8bf 100644 (file)
@@ -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> " +
index deea8443d0e1bf919056fe2507895f9e9c25ce6c..5836434ae0dd2e4b81163660ea5b0c5b5524b1ba 100644 (file)
@@ -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("<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();
@@ -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'?>" +
index 4269a5894646f3a4edb08fa62b67ab3df88748f0..cfd6e7ebd4248ca06375731e271770a91749562e 100644 (file)
@@ -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";