-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When fetching issues from SonarQube, we should only fetch unresolved issues. This will avoid showing issues which were discovered in previous runs with the same "PR" ID but have since been resolved and are no longer issues.
- Loading branch information
Showing
5 changed files
with
166 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
114 changes: 114 additions & 0 deletions
114
...enkinsci/plugins/sonargerrit/sonar/pull_request_analysis/PullRequestAnalysisTaskTest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package org.jenkinsci.plugins.sonargerrit.sonar.pull_request_analysis; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertIterableEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import hudson.model.TaskListener; | ||
import hudson.util.NullStream; | ||
import hudson.util.StreamTaskListener; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import me.redaalaoui.org.sonarqube.ws.Ce; | ||
import me.redaalaoui.org.sonarqube.ws.Issues; | ||
import me.redaalaoui.org.sonarqube.ws.ProjectPullRequests; | ||
import me.redaalaoui.org.sonarqube.ws.client.WsClient; | ||
import me.redaalaoui.org.sonarqube.ws.client.ce.CeService; | ||
import me.redaalaoui.org.sonarqube.ws.client.ce.TaskRequest; | ||
import me.redaalaoui.org.sonarqube.ws.client.issues.IssuesService; | ||
import me.redaalaoui.org.sonarqube.ws.client.issues.SearchRequest; | ||
import me.redaalaoui.org.sonarqube.ws.client.projectpullrequests.ListRequest; | ||
import me.redaalaoui.org.sonarqube.ws.client.projectpullrequests.ProjectPullRequestsService; | ||
import org.jenkinsci.plugins.sonargerrit.sonar.Issue; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class PullRequestAnalysisTaskTest { | ||
static final String SERVER_URL = "http://sonar.example.com:9000/"; | ||
static final String TASK_ID = "task"; | ||
static final String COMPONENT_ID = "component"; | ||
static final String PR_KEY = "PR-9999"; | ||
|
||
@Mock WsClient sonarClient; | ||
@Mock CeService ceService; | ||
@Mock Ce.TaskResponse ceTaskResponse; | ||
@Mock Ce.Task ceTask; | ||
@Mock ProjectPullRequestsService pullRequestsService; | ||
@Mock ProjectPullRequests.ListWsResponse prListResponse; | ||
@Mock ProjectPullRequests.PullRequest pullRequest; | ||
@Mock IssuesService issuesService; | ||
@Mock Issues.SearchWsResponse issuesSearchResponse; | ||
@Mock Issues.Component component; | ||
@Mock Issues.Issue issue; | ||
PullRequestAnalysisTask prAnalysisTask; | ||
TaskListener taskListener; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
taskListener = new StreamTaskListener(new NullStream()); | ||
prAnalysisTask = new PullRequestAnalysisTask(sonarClient, SERVER_URL, TASK_ID, COMPONENT_ID); | ||
} | ||
|
||
@Test | ||
void searchOnlyUnresolvedIssues() throws InterruptedException { | ||
final AtomicReference<TaskRequest> taskRequest = new AtomicReference<>(); | ||
final AtomicReference<ListRequest> listRequest = new AtomicReference<>(); | ||
final AtomicReference<SearchRequest> searchRequest = new AtomicReference<>(); | ||
|
||
when(sonarClient.ce()).thenReturn(ceService); | ||
when(ceService.task(any(TaskRequest.class))) | ||
.thenAnswer( | ||
invocation -> { | ||
taskRequest.set(invocation.getArgument(0)); | ||
return ceTaskResponse; | ||
}); | ||
when(ceTaskResponse.getTask()).thenReturn(ceTask); | ||
when(ceTask.getStatus()).thenReturn(Ce.TaskStatus.SUCCESS); | ||
when(ceTask.getPullRequest()).thenReturn(PR_KEY); | ||
|
||
when(sonarClient.projectPullRequests()).thenReturn(pullRequestsService); | ||
when(pullRequestsService.list(any(ListRequest.class))) | ||
.thenAnswer( | ||
invocation -> { | ||
listRequest.set(invocation.getArgument(0)); | ||
return prListResponse; | ||
}); | ||
when(prListResponse.getPullRequestsList()).thenReturn(Collections.singletonList(pullRequest)); | ||
when(pullRequest.getKey()).thenReturn(PR_KEY); | ||
|
||
when(sonarClient.issues()).thenReturn(issuesService); | ||
when(issuesService.search(any(SearchRequest.class))) | ||
.thenAnswer( | ||
invocation -> { | ||
searchRequest.set(invocation.getArgument(0)); | ||
return issuesSearchResponse; | ||
}); | ||
when(issuesSearchResponse.getComponentsList()).thenReturn(Collections.singletonList(component)); | ||
when(component.getKey()).thenReturn(COMPONENT_ID); | ||
when(issuesSearchResponse.getIssuesList()).thenReturn(Collections.singletonList(issue)); | ||
when(issue.getComponent()).thenReturn(COMPONENT_ID); | ||
|
||
List<Issue> issues = prAnalysisTask.fetchIssues(taskListener); | ||
|
||
assertEquals(1, issues.size()); | ||
|
||
assertNotNull(taskRequest.get()); | ||
assertEquals(TASK_ID, taskRequest.get().getId()); | ||
|
||
assertNotNull(listRequest.get()); | ||
assertEquals(COMPONENT_ID, listRequest.get().getProject()); | ||
|
||
assertNotNull(searchRequest.get()); | ||
assertEquals("false", searchRequest.get().getResolved()); | ||
assertIterableEquals( | ||
Collections.singletonList(COMPONENT_ID), searchRequest.get().getComponentKeys()); | ||
assertEquals(PR_KEY, searchRequest.get().getPullRequest()); | ||
} | ||
} |
This file contains 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
This file contains 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