]> source.dussan.org Git - sonarqube.git/blob
e3830d1132c7ab8ae2539b46b9a0e97969e4087c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.projectanalysis.issue;
21
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;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.DUMB_PROJECT;
39
40 public class DefaultAssigneeTest {
41
42   public static final String PROJECT_KEY = "PROJECT_KEY";
43   public static final String ORGANIZATION_UUID = "ORGANIZATION_UUID";
44
45   @Rule
46   public DbTester db = DbTester.create();
47
48   @Rule
49   public TreeRootHolderRule rootHolder = new TreeRootHolderRule().setRoot(DUMB_PROJECT);
50
51   private Settings settings = new MapSettings();
52   private SettingsRepository settingsRepository = new TestSettingsRepository(settings);
53   private AnalysisMetadataHolderImpl analysisMetadataHolder = new AnalysisMetadataHolderImpl();
54   private OrganizationDto organizationDto;
55
56   private DefaultAssignee underTest = new DefaultAssignee(db.getDbClient(), rootHolder, settingsRepository, analysisMetadataHolder);
57
58   @Before
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")));
62   }
63
64   @Test
65   public void no_default_assignee() {
66     assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
67   }
68
69   @Test
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);
74
75     assertThat(underTest.loadDefaultAssigneeLogin()).isEqualTo("erik");
76   }
77
78   @Test
79   public void configured_login_does_not_exist() {
80     settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
81
82     assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
83   }
84
85   @Test
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));
89
90     assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
91   }
92
93   @Test
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);
99
100     assertThat(underTest.loadDefaultAssigneeLogin()).isNull();
101   }
102
103   @Test
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");
109
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");
113   }
114 }