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.MapSettings;
27 import org.sonar.api.config.Settings;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.organization.OrganizationDto;
30 import org.sonar.db.user.UserDto;
31 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderImpl;
32 import org.sonar.server.computation.task.projectanalysis.analysis.Organization;
33 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
34 import org.sonar.server.computation.task.projectanalysis.component.TestSettingsRepository;
35 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.DUMB_PROJECT;
40 public class DefaultAssigneeTest {
42 public static final String PROJECT_KEY = "PROJECT_KEY";
43 public static final String ORGANIZATION_UUID = "ORGANIZATION_UUID";
46 public DbTester db = DbTester.create();
49 public TreeRootHolderRule rootHolder = new TreeRootHolderRule().setRoot(DUMB_PROJECT);
51 private Settings settings = new MapSettings();
52 private SettingsRepository settingsRepository = new TestSettingsRepository(settings);
53 private AnalysisMetadataHolderImpl analysisMetadataHolder = new AnalysisMetadataHolderImpl();
54 private OrganizationDto organizationDto;
56 private DefaultAssignee underTest = new DefaultAssignee(db.getDbClient(), rootHolder, settingsRepository, analysisMetadataHolder);
59 public void setUp() throws Exception {
60 organizationDto = db.organizations().insertForUuid(ORGANIZATION_UUID);
61 analysisMetadataHolder.setOrganization(Organization.from(new OrganizationDto().setUuid(ORGANIZATION_UUID).setKey("Organization key").setName("Organization name")));
65 public void no_default_assignee() {
66 assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
70 public void set_default_assignee() {
71 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
72 UserDto userDto = db.users().insertUser("erik");
73 db.organizations().addMember(organizationDto, userDto);
75 assertThat(underTest.loadDefaultAssigneeLogin()).isEqualTo("erik");
79 public void configured_login_does_not_exist() {
80 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
82 assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
86 public void configured_login_is_disabled() {
87 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
88 db.users().insertUser(user -> user.setLogin("erik").setActive(false));
90 assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
94 public void configured_login_is_not_member_of_organization() {
95 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
96 OrganizationDto otherOrganization = db.organizations().insert();
97 UserDto userDto = db.users().insertUser("erik");
98 db.organizations().addMember(otherOrganization, userDto);
100 assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
104 public void default_assignee_is_cached() {
105 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
106 UserDto userDto = db.users().insertUser("erik");
107 db.organizations().addMember(organizationDto, userDto);
108 assertThat(underTest.loadDefaultAssigneeLogin()).isEqualTo("erik");
110 // The setting is updated but the assignee hasn't changed
111 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "other");
112 assertThat(underTest.loadDefaultAssigneeLogin()).isEqualTo("erik");