3939import java .util .concurrent .ConcurrentHashMap ;
4040import java .util .concurrent .ExecutionException ;
4141import java .util .concurrent .TimeUnit ;
42+ import java .util .concurrent .atomic .AtomicReference ;
4243import java .util .function .Function ;
4344import java .util .stream .Collectors ;
4445import javax .annotation .Nullable ;
5556import org .junit .jupiter .api .Test ;
5657import org .junit .jupiter .api .TestInstance ;
5758import org .junit .jupiter .api .io .TempDir ;
59+ import org .sonarqube .ws .Issues ;
5860import org .sonarqube .ws .MediaTypes ;
5961import org .sonarqube .ws .client .GetRequest ;
6062import org .sonarqube .ws .client .HttpConnector ;
6668import org .sonarqube .ws .client .issues .SearchRequest ;
6769import org .sonarqube .ws .client .settings .ResetRequest ;
6870import org .sonarqube .ws .client .settings .SetRequest ;
71+ import org .sonarqube .ws .client .sources .RawRequest ;
6972import org .sonarsource .sonarlint .core .rpc .client .ClientJsonRpcLauncher ;
7073import org .sonarsource .sonarlint .core .rpc .client .ConnectionNotFoundException ;
7174import org .sonarsource .sonarlint .core .rpc .client .SonarLintRpcClientDelegate ;
115118import static org .assertj .core .api .Assertions .tuple ;
116119import static org .awaitility .Awaitility .await ;
117120import static org .awaitility .Awaitility .waitAtMost ;
121+ import static org .junit .jupiter .api .Assertions .fail ;
118122import static org .sonarsource .sonarlint .core .rpc .protocol .common .Language .HTML ;
119123import static org .sonarsource .sonarlint .core .rpc .protocol .common .Language .JAVA ;
120124import 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