]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6900 Add IT to simulate a build breaker strategy
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 8 Dec 2015 13:59:13 +0000 (14:59 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 9 Dec 2015 15:36:56 +0000 (16:36 +0100)
it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java

index 39006cc4c094b5a02cd836756ffa6a1db16c7bb0..21f68c4cba96381069e4dfbb5bae250b9bb49655 100644 (file)
@@ -8,7 +8,14 @@ package it.qualityGate;
 import com.sonar.orchestrator.Orchestrator;
 import com.sonar.orchestrator.build.SonarRunner;
 import it.Category1Suite;
+import java.io.File;
+import java.io.IOException;
+import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+import org.apache.commons.io.FileUtils;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.ClassRule;
 import org.junit.Test;
 import org.sonar.wsclient.project.NewProject;
@@ -18,23 +25,37 @@ import org.sonar.wsclient.qualitygate.QualityGateClient;
 import org.sonar.wsclient.services.Measure;
 import org.sonar.wsclient.services.Resource;
 import org.sonar.wsclient.services.ResourceQuery;
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.WsCe;
+import org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.WsResponse;
+import org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.newAdminWsClient;
 import static util.ItUtils.projectDir;
 
 public class QualityGateTest {
 
   private static final String PROJECT_KEY = "sample";
 
-  private long provisionnedProjectId = -1L;
+  private long provisionedProjectId = -1L;
 
   @ClassRule
   public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
+  static WsClient wsClient;
+
+  @BeforeClass
+  public static void startOrchestrator() {
+    wsClient = newAdminWsClient(orchestrator);
+  }
 
   @Before
   public void cleanUp() {
     orchestrator.resetData();
-    provisionnedProjectId = Long.parseLong(orchestrator.getServer().adminWsClient().projectClient().create(NewProject.create().key(PROJECT_KEY).name("Sample")).id());
+    provisionedProjectId = Long.parseLong(orchestrator.getServer().adminWsClient().projectClient().create(NewProject.create().key(PROJECT_KEY).name("Sample")).id());
   }
 
   @Test
@@ -121,7 +142,7 @@ public class QualityGateTest {
     qgClient().createCondition(NewCondition.create(error.id()).metricKey("ncloc").operator("GT").errorThreshold("10"));
 
     qgClient().setDefault(alert.id());
-    qgClient().selectProject(error.id(), provisionnedProjectId);
+    qgClient().selectProject(error.id(), provisionedProjectId);
 
     try {
       SonarRunner build = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
@@ -159,6 +180,54 @@ public class QualityGateTest {
     }
   }
 
+  @Test
+  public void ad_hoc_build_break_strategy() throws IOException {
+    QualityGate simple = qgClient().create("SimpleWithLowThresholdForBuildBreakStrategy");
+    qgClient().setDefault(simple.id());
+    qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").errorThreshold("7"));
+
+    try {
+      File projectDir = projectDir("qualitygate/xoo-sample");
+      SonarRunner build = SonarRunner.create(projectDir);
+      orchestrator.executeBuild(build);
+
+      String taskId = getTaskIdInLocalReport(projectDir);
+      String analysisId = getAnalysisId(taskId);
+
+      ProjectStatusWsResponse projectStatusWsResponse = wsClient.qualityGates().projectStatus(new ProjectStatusWsRequest().setAnalysisId(analysisId));
+      ProjectStatusWsResponse.ProjectStatus projectStatus = projectStatusWsResponse.getProjectStatus();
+      assertThat(projectStatus.getStatus()).isEqualTo(ProjectStatusWsResponse.Status.ERROR);
+      assertThat(projectStatus.getConditionsCount()).isEqualTo(1);
+      ProjectStatusWsResponse.Condition condition = projectStatus.getConditionsList().get(0);
+      assertThat(condition.getMetricKey()).isEqualTo("ncloc");
+      assertThat(condition.getErrorThreshold()).isEqualTo("7");
+    } finally {
+      qgClient().unsetDefault();
+      qgClient().destroy(simple.id());
+    }
+  }
+
+  private String getAnalysisId(String taskId) throws IOException {
+    WsResponse activity = wsClient
+      .wsConnector()
+      .call(new GetRequest("api/ce/task")
+        .setParam("id", taskId)
+        .setMediaType(MediaTypes.PROTOBUF));
+    WsCe.TaskResponse activityWsResponse = WsCe.TaskResponse.parseFrom(activity.contentStream());
+    return activityWsResponse.getTask().getAnalysisId();
+  }
+
+  private String getTaskIdInLocalReport(File projectDirectory) throws IOException {
+    File metadata = new File(projectDirectory, ".sonar/report-task.txt");
+    assertThat(metadata).exists().isFile();
+    // verify properties
+    Properties props = new Properties();
+    props.load(new StringReader(FileUtils.readFileToString(metadata, StandardCharsets.UTF_8)));
+    assertThat(props.getProperty("ceTaskId")).isNotEmpty();
+
+    return props.getProperty("ceTaskId");
+  }
+
   private Measure fetchGateStatus() {
     return fetchResourceWithGateStatus().getMeasure("alert_status");
   }