*/
package it.issue;
+import java.util.Arrays;
import java.util.List;
+import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.wsclient.issue.Issue;
import org.sonar.wsclient.issue.IssueQuery;
+import org.sonarqube.ws.WsUsers;
import org.sonarqube.ws.client.PostRequest;
-import org.sonarqube.ws.client.WsResponse;
+import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.user.CreateRequest;
+import org.sonarqube.ws.client.user.SearchRequest;
import util.ProjectAnalysis;
import util.ProjectAnalysisRule;
public class AutoAssignTest extends AbstractIssueTest {
- static final String SIMON_USER = "simon";
-
@Rule
public final ProjectAnalysisRule projectAnalysisRule = ProjectAnalysisRule.from(ORCHESTRATOR);
@Before
public void setup() {
- ORCHESTRATOR.resetData();
-
String qualityProfileKey = projectAnalysisRule.registerProfile("/issue/IssueActionTest/xoo-one-issue-per-line-profile.xml");
- String projectKey = projectAnalysisRule.registerProject("issue/xoo-with-scm");
+ String projectKey = projectAnalysisRule.registerProject("issue/AutoAssignTest");
projectAnalysis = projectAnalysisRule.newProjectAnalysis(projectKey)
.withQualityProfile(qualityProfileKey)
.withProperties("sonar.scm.disabled", "false", "sonar.scm.provider", "xoo");
@After
public void resetData() throws Exception {
- // Remove user simon
- newAdminWsClient(ORCHESTRATOR).wsConnector().call(
- new PostRequest("api/users/deactivate")
- .setParam("login", SIMON_USER));
+ newAdminWsClient(ORCHESTRATOR).wsConnector().call(new PostRequest("api/projects/delete").setParam("project", "AutoAssignTest"));
+ deleteAllUsers();
// Reset default assignee
setServerProperty(ORCHESTRATOR, "sonar.issues.defaultAssigneeLogin", null);
@Test
public void auto_assign_issues_to_user() throws Exception {
- createUser(SIMON_USER, SIMON_USER);
+ // verify that login matches, case-sensitive
+ createUser("user1", "User 1", "user1@email.com");
+ createUser("USER2", "User 2", "user2@email.com");
+ // verify that name is not used to match, whatever the case
+ createUser("user3", "User 3", "user3@email.com");
+ createUser("user4", "USER 4", "user4@email.com");
+ // verify that email matches, case-insensitive
+ createUser("user5", "User 5", "user5@email.com");
+ createUser("user6", "User 6", "USER6@email.COM");
+ // verify that SCM account matches, case-insensitive
+ createUser("user7", "User 7", "user7@email.com", "user7ScmAccount");
+ createUser("user8", "User 8", "user8@email.com", "user8SCMaccOUNT");
+
projectAnalysis.run();
- // Simon has 3 issues
- assertThat(search(IssueQuery.create().assignees(SIMON_USER)).list()).hasSize(3);
- // Other issues are not assigned as no user have been created on their author
- assertThat(search(IssueQuery.create().assigned(false)).list()).hasSize(10);
+ List<Issue> issues = search(IssueQuery.create().components("AutoAssignTest:src/sample.xoo").sort("FILE_LINE")).list();
+ // login match, case-sensitive
+ verifyIssueAssignee(issues, 1, "user1");
+ verifyIssueAssignee(issues, 2, null);
+ // user name is not used to match
+ verifyIssueAssignee(issues, 3, null);
+ verifyIssueAssignee(issues, 4, null);
+ // email match, case-insensitive
+ verifyIssueAssignee(issues, 5, "user5");
+ verifyIssueAssignee(issues, 6, "user6");
+ // SCM account match, case-insensitive
+ verifyIssueAssignee(issues, 7, "user7");
+ verifyIssueAssignee(issues, 8, "user8");
+ }
+
+ private static void verifyIssueAssignee(List<Issue> issues, int line, @Nullable String expectedAssignee) {
+ assertThat(issues.get(line - 1).assignee()).isEqualTo(expectedAssignee);
}
@Test
public void auto_assign_issues_to_default_assignee() throws Exception {
- createUser(SIMON_USER, SIMON_USER);
- setServerProperty(ORCHESTRATOR, "sonar.issues.defaultAssigneeLogin", SIMON_USER);
+ createUser("user1", "User 1", "user1@email.com");
+ createUser("user2", "User 2", "user2@email.com");
+ setServerProperty(ORCHESTRATOR, "sonar.issues.defaultAssigneeLogin", "user2");
projectAnalysis.run();
- // Simon has all issues
- assertThat(search(IssueQuery.create().assignees(SIMON_USER)).list()).hasSize(13);
+ // user1 is assigned to his issues. All other issues are assigned to the default assignee.
+ assertThat(search(IssueQuery.create().assignees("user1")).list()).hasSize(1);
+ assertThat(search(IssueQuery.create().assignees("user2")).list()).hasSize(8);
// No unassigned issues
assertThat(search(IssueQuery.create().assigned(false)).list()).isEmpty();
}
*/
@Test
public void update_author_and_assignee_when_scm_is_activated() {
- createUser(SIMON_USER, SIMON_USER);
+ createUser("user1", "User 1", "user1@email.com");
// Run a first analysis without SCM
projectAnalysis.withProperties("sonar.scm.disabled", "true").run();
for (Issue issue : issues) {
assertThat(issue.author()).isNotEmpty();
}
- assertThat(search(IssueQuery.create().assignees(SIMON_USER)).list()).hasSize(3);
+ assertThat(search(IssueQuery.create().assignees("user1")).list()).hasSize(1);
+ }
+
+ private static void createUser(String login, String name, String email, String... scmAccounts) {
+ newAdminWsClient(ORCHESTRATOR).users().create(
+ CreateRequest.builder()
+ .setLogin(login)
+ .setName(name)
+ .setEmail(email)
+ .setPassword("xxxxxxx")
+ .setScmAccounts(Arrays.asList(scmAccounts))
+ .build());
}
- private void createUser(String login, String password) {
- WsResponse response = newAdminWsClient(ORCHESTRATOR).wsConnector().call(
- new PostRequest("api/users/create")
- .setParam("login", login)
- .setParam("name", login)
- .setParam("password", password));
- assertThat(response.code()).isEqualTo(200);
+ private static void deleteAllUsers() {
+ WsClient wsClient = newAdminWsClient(ORCHESTRATOR);
+ WsUsers.SearchWsResponse searchResponse = wsClient.users().search(SearchRequest.builder().build());
+ searchResponse.getUsersList().forEach(user -> {
+ wsClient.wsConnector().call(new PostRequest("api/users/deactivate").setParam("login", user.getLogin()));
+ });
}
}