Skip to content

SLCORE-1550 Use ServerFixture for BindingSuggestionsMediumTests #1443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@

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

try (var git = GitUtils.createRepository(gitRepo)) {
git.remoteAdd()
Expand All @@ -359,6 +354,9 @@ void should_suggest_binding_by_remote_url_when_no_other_suggestions_found(SonarL
var scServer = harness.newFakeSonarCloudServer()
.withOrganization("orgKey", organization ->
organization.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main")))
.withDopTranslation(dop -> dop
.withProjectBinding(REMOTE_URL, PROJECT_ID, SLCORE_PROJECT_KEY)
.withSearchProjectsResponse("orgKey", PROJECT_ID, 200, "{\"components\":[{\"key\":\"" + SLCORE_PROJECT_KEY + "\",\"name\":\"" + SLCORE_PROJECT_NAME + "\"}]}\n"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do this slightly differently. I think withDopTranslation and withProjectBinding are good, but I would remove withSearchProjectsResponse. Just above, there is already a call to organization.withProject, you could make sure this call registers a response to api/components/search_projects. You can also modify the project builder to add a withName. This would make the test even less verbose

.start();

var backend = harness.newBackend()
Expand All @@ -369,30 +367,6 @@ void should_suggest_binding_by_remote_url_when_no_other_suggestions_found(SonarL
.withTelemetryEnabled()
.start(fakeClient);

scServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"bindings\":[{\"projectId\":\"" + PROJECT_ID + "\"}]}")));

scServer.getMockServer().stubFor(get(urlEqualTo(expectedSearchProjectsPath))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"components\":[{\"key\":\"" + SLCORE_PROJECT_KEY + "\",\"name\":\"" + SLCORE_PROJECT_NAME + "\"}]}\n")));

scServer.getMockServer().stubFor(get(urlPathMatching("/api/components/search\\.protobuf"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/x-protobuf")
.withResponseBody(protobufBody(Components.SearchWsResponse.newBuilder()
.addComponents(Components.Component.newBuilder()
.setKey("my-project-key")
.setName("My Project")
.build())
.setPaging(Common.Paging.newBuilder().setTotal(1).build())
.build()))));

ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());

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

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

var sqServer = harness.newFakeSonarQubeServer()
.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main"))
.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main").withProjectName(SLCORE_PROJECT_NAME))
.withDopTranslation(dop -> dop
.withProjectBinding(REMOTE_URL, PROJECT_ID, SLCORE_PROJECT_KEY))
.start();

var backend = harness.newBackend()
Expand All @@ -435,25 +409,6 @@ void should_suggest_binding_by_remote_url_when_no_other_suggestions_found_for_so
.withTelemetryEnabled()
.start(fakeClient);

sqServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"projectBindings\":[{\"projectId\":\"" + PROJECT_ID + "\",\"projectKey\":\"" + SLCORE_PROJECT_KEY + "\"}]}")));

String expectedProjectPath = "/api/components/show.protobuf?component=" + UrlUtils.urlEncode(SLCORE_PROJECT_KEY);
sqServer.getMockServer().stubFor(get(urlEqualTo(expectedProjectPath))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/x-protobuf")
.withResponseBody(protobufBody(Components.ShowWsResponse.newBuilder()
.setComponent(Components.Component.newBuilder()
.setKey(SLCORE_PROJECT_KEY)
.setName(SLCORE_PROJECT_NAME)
.setIsAiCodeFixEnabled(false)
.build())
.build()))));

ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());

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

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

scServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
.willReturn(aResponse()
.withStatus(500)
.withBody("Internal Server Error")));

ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());

Expand Down Expand Up @@ -528,18 +479,14 @@ void should_return_empty_when_sqs_project_bindings_is_null(SonarLintTestHarness

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

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

sqServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
.willReturn(aResponse()
.withStatus(500)
.withBody("Internal Server Error")));

ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());

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

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

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

scServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"bindings\":[{\"projectId\":\"" + PROJECT_ID + "\"}]}")));

scServer.getMockServer().stubFor(get(urlEqualTo(expectedSearchProjectsPath))
.willReturn(aResponse()
.withStatus(500)
.withBody("Internal Server Error")));

ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());

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

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

var sqServer = harness.newFakeSonarQubeServer()
.withProject(SLCORE_PROJECT_KEY, project -> project.withBranch("main"))
.withDopTranslation(dop -> dop
.withProjectBinding(REMOTE_URL, PROJECT_ID, SLCORE_PROJECT_KEY)
.withComponentShowStatus(SLCORE_PROJECT_KEY, 404, "Project not found"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

.start();

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

sqServer.getMockServer().stubFor(get(urlEqualTo(expectedPath))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"projectBindings\":[{\"projectId\":\"" + PROJECT_ID + "\",\"projectKey\":\"" + SLCORE_PROJECT_KEY + "\"}]}")));

sqServer.getMockServer().stubFor(get(urlEqualTo(expectedProjectPath))
.willReturn(aResponse()
.withStatus(404)
.withBody("Project not found")));

ArgumentCaptor<Map<String, List<BindingSuggestionDto>>> suggestionCaptor = ArgumentCaptor.forClass(Map.class);
verify(fakeClient, timeout(5000)).suggestBinding(suggestionCaptor.capture());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static class TestServer extends ServerFixture.Server {
private boolean shutdownCalled = false;

public TestServer() {
super(null, null, null, null, null, null, null, null, null, false, null, null, null);
super(null, null, null, null, null, null, null, null, null, false, null, null, null, null, null);
}

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

ThrowingTestServer(RuntimeException exceptionToThrow) {
super(null, null, null, null, null, null, null, null, null, false, null, null, null);
super(null, null, null, null, null, null, null, null, null, false, null, null, null, null, null);
this.exceptionToThrow = exceptionToThrow;
}

Expand Down
Loading