3 * Copyright (C) 2009-2019 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.ce.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.ce.task.projectanalysis.analysis.AnalysisMetadataHolderImpl;
28 import org.sonar.ce.task.projectanalysis.analysis.Organization;
29 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
30 import org.sonar.ce.task.projectanalysis.component.TestSettingsRepository;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.organization.OrganizationDto;
33 import org.sonar.db.user.UserDto;
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";
41 public static final String QUALITY_GATE_UUID = "QUALITY_GATE_UUID";
44 public DbTester db = DbTester.create();
46 private MapSettings settings = new MapSettings();
47 private ConfigurationRepository settingsRepository = new TestSettingsRepository(settings.asConfig());
48 private AnalysisMetadataHolderImpl analysisMetadataHolder = new AnalysisMetadataHolderImpl();
49 private OrganizationDto organizationDto;
51 private DefaultAssignee underTest = new DefaultAssignee(db.getDbClient(), settingsRepository, analysisMetadataHolder);
54 public void setUp() throws Exception {
55 organizationDto = db.organizations().insertForUuid(ORGANIZATION_UUID);
56 analysisMetadataHolder.setOrganization(Organization.from(
57 new OrganizationDto().setUuid(ORGANIZATION_UUID).setKey("Organization key").setName("Organization name").setDefaultQualityGateUuid(QUALITY_GATE_UUID)));
61 public void no_default_assignee() {
62 assertThat(underTest.loadDefaultAssigneeUuid()).isNull();
66 public void set_default_assignee() {
67 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
68 UserDto userDto = db.users().insertUser("erik");
69 db.organizations().addMember(organizationDto, userDto);
71 assertThat(underTest.loadDefaultAssigneeUuid()).isEqualTo(userDto.getUuid());
75 public void configured_login_does_not_exist() {
76 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
78 assertThat(underTest.loadDefaultAssigneeUuid()).isNull();
82 public void configured_login_is_disabled() {
83 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
84 db.users().insertUser(user -> user.setLogin("erik").setActive(false));
86 assertThat(underTest.loadDefaultAssigneeUuid()).isNull();
90 public void configured_login_is_not_member_of_organization() {
91 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
92 OrganizationDto otherOrganization = db.organizations().insert();
93 UserDto userDto = db.users().insertUser("erik");
94 db.organizations().addMember(otherOrganization, userDto);
96 assertThat(underTest.loadDefaultAssigneeUuid()).isNull();
100 public void default_assignee_is_cached() {
101 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
102 UserDto userDto = db.users().insertUser("erik");
103 db.organizations().addMember(organizationDto, userDto);
104 assertThat(underTest.loadDefaultAssigneeUuid()).isEqualTo(userDto.getUuid());
106 // The setting is updated but the assignee hasn't changed
107 settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "other");
108 assertThat(underTest.loadDefaultAssigneeUuid()).isEqualTo(userDto.getUuid());