-
Notifications
You must be signed in to change notification settings - Fork 55
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
eray-felek-sonarsource
wants to merge
1
commit into
master
Choose a base branch
from
feautre/SLCORE-1550-utilize-serverfixture
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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() | ||
|
@@ -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")) | ||
.start(); | ||
|
||
var backend = harness.newBackend() | ||
|
@@ -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()); | ||
|
||
|
@@ -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() | ||
|
@@ -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() | ||
|
@@ -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()); | ||
|
||
|
@@ -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") | ||
eray-felek-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.start(); | ||
|
||
var backend = harness.newBackend() | ||
|
@@ -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()); | ||
|
||
|
@@ -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()); | ||
|
||
|
@@ -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() | ||
|
@@ -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() | ||
|
@@ -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()); | ||
|
||
|
@@ -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() | ||
|
@@ -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")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andwithProjectBinding
are good, but I would removewithSearchProjectsResponse
. Just above, there is already a call toorganization.withProject
, you could make sure this call registers a response toapi/components/search_projects
. You can also modify the project builder to add awithName
. This would make the test even less verbose