Skip to content

Commit 9539493

Browse files
Fix flaky IT
Wait for the sources API to ingest the report and be ready
1 parent fad6068 commit 9539493

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

its/tests/src/test/java/its/SonarCloudTests.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.concurrent.ConcurrentHashMap;
4040
import java.util.concurrent.ExecutionException;
4141
import java.util.concurrent.TimeUnit;
42+
import java.util.concurrent.atomic.AtomicReference;
4243
import java.util.function.Function;
4344
import java.util.stream.Collectors;
4445
import javax.annotation.Nullable;
@@ -55,6 +56,7 @@
5556
import org.junit.jupiter.api.Test;
5657
import org.junit.jupiter.api.TestInstance;
5758
import org.junit.jupiter.api.io.TempDir;
59+
import org.sonarqube.ws.Issues;
5860
import org.sonarqube.ws.MediaTypes;
5961
import org.sonarqube.ws.client.GetRequest;
6062
import org.sonarqube.ws.client.HttpConnector;
@@ -66,6 +68,7 @@
6668
import org.sonarqube.ws.client.issues.SearchRequest;
6769
import org.sonarqube.ws.client.settings.ResetRequest;
6870
import org.sonarqube.ws.client.settings.SetRequest;
71+
import org.sonarqube.ws.client.sources.RawRequest;
6972
import org.sonarsource.sonarlint.core.rpc.client.ClientJsonRpcLauncher;
7073
import org.sonarsource.sonarlint.core.rpc.client.ConnectionNotFoundException;
7174
import org.sonarsource.sonarlint.core.rpc.client.SonarLintRpcClientDelegate;
@@ -115,6 +118,7 @@
115118
import static org.assertj.core.api.Assertions.tuple;
116119
import static org.awaitility.Awaitility.await;
117120
import static org.awaitility.Awaitility.waitAtMost;
121+
import static org.junit.jupiter.api.Assertions.fail;
118122
import static org.sonarsource.sonarlint.core.rpc.protocol.common.Language.HTML;
119123
import static org.sonarsource.sonarlint.core.rpc.protocol.common.Language.JAVA;
120124
import static org.sonarsource.sonarlint.core.rpc.protocol.common.Language.JS;
@@ -227,14 +231,16 @@ private static void restoreProfile(String profile) {
227231
}
228232
}
229233

230-
private static void provisionProject(String key, String name) {
234+
private static String provisionProject(String key, String name) {
235+
var projectKey = projectKey(key);
231236
var request = new PostRequest("api/projects/create");
232237
request.setParam("name", name);
233-
request.setParam("project", projectKey(key));
238+
request.setParam("project", projectKey);
234239
request.setParam("organization", SONARCLOUD_ORGANIZATION);
235240
try (var response = adminWsClient.wsConnector().call(request)) {
236241
assertIsOk(response);
237242
}
243+
return projectKey;
238244
}
239245

240246
private static String projectKey(String key) {
@@ -564,11 +570,12 @@ private static void openUnboundConfigurationScope(String configScopeId) {
564570
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
565571
class TaintVulnerabilities {
566572
private static final String PROJECT_KEY_JAVA_TAINT = "sample-java-taint";
573+
private String projectKey;
567574

568575
@BeforeAll
569576
void prepare() throws Exception {
570577
restoreProfile("java-sonarlint-with-taint.xml");
571-
provisionProject(PROJECT_KEY_JAVA_TAINT, "Java With Taint Vulnerabilities");
578+
this.projectKey = provisionProject(PROJECT_KEY_JAVA_TAINT, "Java With Taint Vulnerabilities");
572579
associateProjectToQualityProfile(PROJECT_KEY_JAVA_TAINT, "java", "SonarLint Taint Java");
573580
analyzeMavenProject(projectKey(PROJECT_KEY_JAVA_TAINT), PROJECT_KEY_JAVA_TAINT);
574581
}
@@ -580,10 +587,24 @@ void download_taint_vulnerabilities_for_project() throws ExecutionException, Int
580587
waitForAnalysisToBeReady(configScopeId);
581588

582589
// Ensure a vulnerability has been reported on server side
583-
var issuesList = adminWsClient.issues().search(new SearchRequest().setTypes(List.of("VULNERABILITY")).setComponentKeys(List.of(projectKey(PROJECT_KEY_JAVA_TAINT))))
584-
.getIssuesList();
585-
assertThat(issuesList).hasSize(1);
586-
var issueKey = issuesList.get(0).getKey();
590+
AtomicReference<Issues.Issue> issue = new AtomicReference<>();
591+
await().untilAsserted(() -> {
592+
var issuesList = adminWsClient.issues().search(new SearchRequest().setTypes(List.of("VULNERABILITY")).setComponentKeys(List.of(projectKey(PROJECT_KEY_JAVA_TAINT))))
593+
.getIssuesList();
594+
assertThat(issuesList).hasSize(1);
595+
issue.set(issuesList.get(0));
596+
});
597+
// Ensure the source is available, it can take some time to propagate after the analysis, especially on SQC US
598+
await().untilAsserted(() -> {
599+
try {
600+
var rawSource = adminWsClient.sources().raw(new RawRequest().setKey(projectKey + ":src/main/java/foo/DbHelper.java"));
601+
assertThat(rawSource).isNotEmpty();
602+
} catch (Exception e) {
603+
fail("The source is not yet available", e);
604+
}
605+
});
606+
607+
var issueKey = issue.get().getKey();
587608

588609
var taintVulnerabilities = backend.getTaintVulnerabilityTrackingService().listAll(new ListAllParams(configScopeId, true)).get().getTaintVulnerabilities();
589610

0 commit comments

Comments
 (0)