3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.issue;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.CoreProperties;
26 import org.sonar.api.config.internal.MapSettings;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.organization.OrganizationDto;
29 import org.sonar.db.user.UserDto;
30 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderImpl;
31 import org.sonar.server.computation.task.projectanalysis.analysis.Organization;
32 import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository;
33 import org.sonar.server.computation.task.projectanalysis.component.TestSettingsRepository;
35 import static org.assertj.core.api.Assertions.assertThat;
37 public class DefaultAssigneeTest {
39 public static final String PROJECT_KEY = "PROJECT_KEY";
40 public static final String ORGANIZATION_UUID = "ORGANIZATION_UUID";
43 public DbTester db = DbTester.create();
45 private MapSettings settings = new MapSettings();
46 private ConfigurationRepository settingsRepository = new TestSettingsRepository(settings.asConfig());
47 private AnalysisMetadataHolderImpl analysisMetadataHolder = new AnalysisMetadataHolderImpl();
48 private OrganizationDto organizationDto;
50 private DefaultAssignee underTest = new DefaultAssignee(db.getDbClient(), settingsRepository, analysisMetadataHolder);
53 public void setUp() throws Exception {
54 organizationDto = db.organizations().insertForUuid(ORGANIZATION_UUID);
55 analysisMetadataHolder.setOrganization(Organization.from(new OrganizationDto().setUuid(ORGANIZATION_UUID).setKey("Organization key").setName("Organization name")));
59 public void no_default_assignee() {
60 assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
64 public void set_default_assignee() {
65 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
66 UserDto userDto = db.users().insertUser("erik");
67 db.organizations().addMember(organizationDto, userDto);
69 assertThat(underTest.loadDefaultAssigneeLogin()).isEqualTo("erik");
73 public void configured_login_does_not_exist() {
74 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
76 assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
80 public void configured_login_is_disabled() {
81 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
82 db.users().insertUser(user -> user.setLogin("erik").setActive(false));
84 assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
88 public void configured_login_is_not_member_of_organization() {
89 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
90 OrganizationDto otherOrganization = db.organizations().insert();
91 UserDto userDto = db.users().insertUser("erik");
92 db.organizations().addMember(otherOrganization, userDto);
94 assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
98 public void default_assignee_is_cached() {
99 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
100 UserDto userDto = db.users().insertUser("erik");
101 db.organizations().addMember(organizationDto, userDto);
102 assertThat(underTest.loadDefaultAssigneeLogin()).isEqualTo("erik");
104 // The setting is updated but the assignee hasn't changed
105 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "other");
106 assertThat(underTest.loadDefaultAssigneeLogin()).isEqualTo("erik");