Skip to content

Commit a250733

Browse files
SLCORE-1550 Use ServerFixture for BindingSuggestionsMediumTests
1 parent ae1b32c commit a250733

File tree

3 files changed

+152
-94
lines changed

3 files changed

+152
-94
lines changed

medium-tests/src/test/java/mediumtest/BindingSuggestionsMediumTests.java

Lines changed: 14 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353

5454
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
5555
import static com.github.tomakehurst.wiremock.client.WireMock.get;
56-
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
57-
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
5856
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
5957
import static org.assertj.core.api.Assertions.assertThat;
6058
import static org.assertj.core.api.Assertions.tuple;
@@ -339,9 +337,6 @@ void test_uses_binding_clues_from_shared_configuration_when_updating_fs(SonarLin
339337
void should_suggest_binding_by_remote_url_when_no_other_suggestions_found(SonarLintTestHarness harness, @TempDir Path tmp) throws IOException, GitAPIException {
340338
var gitRepo = tmp.resolve("git-repo");
341339
Files.createDirectory(gitRepo);
342-
String encodedUrl = UrlUtils.urlEncode(REMOTE_URL);
343-
String expectedPath = "/dop-translation/project-bindings?url=" + encodedUrl;
344-
String expectedSearchProjectsPath = "/api/components/search_projects?projectIds=" + PROJECT_ID + "&organization=orgKey";
345340

346341
try (var git = GitUtils.createRepository(gitRepo)) {
347342
git.remoteAdd()
@@ -359,6 +354,9 @@ void should_suggest_binding_by_remote_url_when_no_other_suggestions_found(SonarL
359354
var scServer = harness.newFakeSonarCloudServer()
360355
.withOrganization("orgKey", organization ->
361356
organization.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main")))
357+
.withDopTranslation(dop -> dop
358+
.withProjectBinding(REMOTE_URL, PROJECT_ID, SLCORE_PROJECT_KEY)
359+
.withSearchProjectsResponse("orgKey", PROJECT_ID, 200, "{\"components\":[{\"key\":\"" + SLCORE_PROJECT_KEY + "\",\"name\":\"" + SLCORE_PROJECT_NAME + "\"}]}\n"))
362360
.start();
363361

364362
var backend = harness.newBackend()
@@ -369,30 +367,6 @@ void should_suggest_binding_by_remote_url_when_no_other_suggestions_found(SonarL
369367
.withTelemetryEnabled()
370368
.start(fakeClient);
371369

372-
scServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
373-
.willReturn(aResponse()
374-
.withStatus(200)
375-
.withHeader("Content-Type", "application/json")
376-
.withBody("{\"bindings\":[{\"projectId\":\"" + PROJECT_ID + "\"}]}")));
377-
378-
scServer.getMockServer().stubFor(get(urlEqualTo(expectedSearchProjectsPath))
379-
.willReturn(aResponse()
380-
.withStatus(200)
381-
.withHeader("Content-Type", "application/json")
382-
.withBody("{\"components\":[{\"key\":\"" + SLCORE_PROJECT_KEY + "\",\"name\":\"" + SLCORE_PROJECT_NAME + "\"}]}\n")));
383-
384-
scServer.getMockServer().stubFor(get(urlPathMatching("/api/components/search\\.protobuf"))
385-
.willReturn(aResponse()
386-
.withStatus(200)
387-
.withHeader("Content-Type", "application/x-protobuf")
388-
.withResponseBody(protobufBody(Components.SearchWsResponse.newBuilder()
389-
.addComponents(Components.Component.newBuilder()
390-
.setKey("my-project-key")
391-
.setName("My Project")
392-
.build())
393-
.setPaging(Common.Paging.newBuilder().setTotal(1).build())
394-
.build()))));
395-
396370
ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
397371
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());
398372

@@ -409,8 +383,6 @@ void should_suggest_binding_by_remote_url_when_no_other_suggestions_found_for_so
409383
GitAPIException {
410384
var gitRepo = tmp.resolve("git-repo");
411385
Files.createDirectory(gitRepo);
412-
String encodedUrl = UrlUtils.urlEncode(REMOTE_URL);
413-
String expectedPath = "/api/v2/dop-translation/project-bindings?repositoryUrl=" + encodedUrl;
414386

415387
try (var git = GitUtils.createRepository(gitRepo)) {
416388
git.remoteAdd()
@@ -426,7 +398,9 @@ void should_suggest_binding_by_remote_url_when_no_other_suggestions_found_for_so
426398
.build();
427399

428400
var sqServer = harness.newFakeSonarQubeServer()
429-
.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main"))
401+
.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main").withProjectName(SLCORE_PROJECT_NAME))
402+
.withDopTranslation(dop -> dop
403+
.withProjectBinding(REMOTE_URL, PROJECT_ID, SLCORE_PROJECT_KEY))
430404
.start();
431405

432406
var backend = harness.newBackend()
@@ -435,25 +409,6 @@ void should_suggest_binding_by_remote_url_when_no_other_suggestions_found_for_so
435409
.withTelemetryEnabled()
436410
.start(fakeClient);
437411

438-
sqServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
439-
.willReturn(aResponse()
440-
.withStatus(200)
441-
.withHeader("Content-Type", "application/json")
442-
.withBody("{\"projectBindings\":[{\"projectId\":\"" + PROJECT_ID + "\",\"projectKey\":\"" + SLCORE_PROJECT_KEY + "\"}]}")));
443-
444-
String expectedProjectPath = "/api/components/show.protobuf?component=" + UrlUtils.urlEncode(SLCORE_PROJECT_KEY);
445-
sqServer.getMockServer().stubFor(get(urlEqualTo(expectedProjectPath))
446-
.willReturn(aResponse()
447-
.withStatus(200)
448-
.withHeader("Content-Type", "application/x-protobuf")
449-
.withResponseBody(protobufBody(Components.ShowWsResponse.newBuilder()
450-
.setComponent(Components.Component.newBuilder()
451-
.setKey(SLCORE_PROJECT_KEY)
452-
.setName(SLCORE_PROJECT_NAME)
453-
.setIsAiCodeFixEnabled(false)
454-
.build())
455-
.build()))));
456-
457412
ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
458413
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());
459414

@@ -486,6 +441,7 @@ void should_return_empty_when_sqc_project_bindings_is_null(SonarLintTestHarness
486441
var scServer = harness.newFakeSonarCloudServer()
487442
.withOrganization("orgKey", organization ->
488443
organization.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main")))
444+
.withCustomEndpointResponse(expectedPath, 500, "text/plain", "Internal Server Error")
489445
.start();
490446

491447
var backend = harness.newBackend()
@@ -495,11 +451,6 @@ void should_return_empty_when_sqc_project_bindings_is_null(SonarLintTestHarness
495451
.withUnboundConfigScope(CONFIG_SCOPE_ID, "unmatched-project-name")
496452
.start(fakeClient);
497453

498-
scServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
499-
.willReturn(aResponse()
500-
.withStatus(500)
501-
.withBody("Internal Server Error")));
502-
503454
ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
504455
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());
505456

@@ -528,18 +479,14 @@ void should_return_empty_when_sqs_project_bindings_is_null(SonarLintTestHarness
528479

529480
var sqServer = harness.newFakeSonarQubeServer()
530481
.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main"))
482+
.withCustomEndpointResponse(expectedPath, 500, "text/plain", "Internal Server Error")
531483
.start();
532484

533485
var backend = harness.newBackend()
534486
.withSonarQubeConnection(MYSONAR, sqServer.baseUrl())
535487
.withUnboundConfigScope(CONFIG_SCOPE_ID, "unmatched-project-name")
536488
.start(fakeClient);
537489

538-
sqServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
539-
.willReturn(aResponse()
540-
.withStatus(500)
541-
.withBody("Internal Server Error")));
542-
543490
ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
544491
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());
545492

@@ -552,9 +499,6 @@ void should_return_empty_when_sqs_project_bindings_is_null(SonarLintTestHarness
552499
void should_return_empty_when_sqc_search_response_is_null(SonarLintTestHarness harness, @TempDir Path tmp) throws IOException, GitAPIException, URISyntaxException {
553500
var gitRepo = tmp.resolve("git-repo");
554501
Files.createDirectory(gitRepo);
555-
String encodedUrl = UrlUtils.urlEncode(REMOTE_URL);
556-
String expectedPath = "/dop-translation/project-bindings?url=" + encodedUrl;
557-
String expectedSearchProjectsPath = "/api/components/search_projects?projectIds=" + PROJECT_ID + "&organization=orgKey";
558502

559503
try (var git = GitUtils.createRepository(gitRepo)) {
560504
git.remoteAdd()
@@ -570,6 +514,9 @@ void should_return_empty_when_sqc_search_response_is_null(SonarLintTestHarness h
570514
var scServer = harness.newFakeSonarCloudServer()
571515
.withOrganization("orgKey", organization ->
572516
organization.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main")))
517+
.withDopTranslation(dop -> dop
518+
.withProjectBinding(REMOTE_URL, PROJECT_ID, SLCORE_PROJECT_KEY)
519+
.withSearchProjectsResponse("orgKey", PROJECT_ID, 500, "Internal Server Error"))
573520
.start();
574521

575522
var backend = harness.newBackend()
@@ -579,17 +526,6 @@ void should_return_empty_when_sqc_search_response_is_null(SonarLintTestHarness h
579526
.withUnboundConfigScope(CONFIG_SCOPE_ID, "unmatched-project-name")
580527
.start(fakeClient);
581528

582-
scServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
583-
.willReturn(aResponse()
584-
.withStatus(200)
585-
.withHeader("Content-Type", "application/json")
586-
.withBody("{\"bindings\":[{\"projectId\":\"" + PROJECT_ID + "\"}]}")));
587-
588-
scServer.getMockServer().stubFor(get(urlEqualTo(expectedSearchProjectsPath))
589-
.willReturn(aResponse()
590-
.withStatus(500)
591-
.withBody("Internal Server Error")));
592-
593529
ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
594530
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());
595531

@@ -602,9 +538,6 @@ void should_return_empty_when_sqc_search_response_is_null(SonarLintTestHarness h
602538
void should_return_empty_when_sqs_server_project_is_not_present(SonarLintTestHarness harness, @TempDir Path tmp) throws IOException, GitAPIException, URISyntaxException {
603539
var gitRepo = tmp.resolve("git-repo");
604540
Files.createDirectory(gitRepo);
605-
String encodedUrl = UrlUtils.urlEncode(REMOTE_URL);
606-
String expectedPath = "/api/v2/dop-translation/project-bindings?repositoryUrl=" + encodedUrl;
607-
String expectedProjectPath = "/api/components/show.protobuf?component=" + UrlUtils.urlEncode(SLCORE_PROJECT_KEY);
608541

609542
try (var git = GitUtils.createRepository(gitRepo)) {
610543
git.remoteAdd()
@@ -619,24 +552,16 @@ void should_return_empty_when_sqs_server_project_is_not_present(SonarLintTestHar
619552

620553
var sqServer = harness.newFakeSonarQubeServer()
621554
.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main"))
555+
.withDopTranslation(dop -> dop
556+
.withProjectBinding(REMOTE_URL, PROJECT_ID, SLCORE_PROJECT_KEY)
557+
.withComponentShowStatus(SLCORE_PROJECT_KEY, 404, "Project not found"))
622558
.start();
623559

624560
var backend = harness.newBackend()
625561
.withSonarQubeConnection(MYSONAR, sqServer.baseUrl())
626562
.withUnboundConfigScope(CONFIG_SCOPE_ID, "unmatched-project-name")
627563
.start(fakeClient);
628564

629-
sqServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
630-
.willReturn(aResponse()
631-
.withStatus(200)
632-
.withHeader("Content-Type", "application/json")
633-
.withBody("{\"projectBindings\":[{\"projectId\":\"" + PROJECT_ID + "\",\"projectKey\":\"" + SLCORE_PROJECT_KEY + "\"}]}")));
634-
635-
sqServer.getMockServer().stubFor(get(urlEqualTo(expectedProjectPath))
636-
.willReturn(aResponse()
637-
.withStatus(404)
638-
.withBody("Project not found")));
639-
640565
ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
641566
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());
642567

medium-tests/src/test/java/mediumtest/SonarLintTestHarnessMediumTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static class TestServer extends ServerFixture.Server {
221221
private boolean shutdownCalled = false;
222222

223223
public TestServer() {
224-
super(null, null, null, null, null, null, null, null, null, false, null, null, null);
224+
super(null, null, null, null, null, null, null, null, null, false, null, null, null, null, null);
225225
}
226226

227227
@Override
@@ -238,7 +238,7 @@ static class ThrowingTestServer extends ServerFixture.Server {
238238
private final RuntimeException exceptionToThrow;
239239

240240
ThrowingTestServer(RuntimeException exceptionToThrow) {
241-
super(null, null, null, null, null, null, null, null, null, false, null, null, null);
241+
super(null, null, null, null, null, null, null, null, null, false, null, null, null, null, null);
242242
this.exceptionToThrow = exceptionToThrow;
243243
}
244244

0 commit comments

Comments
 (0)