]> source.dussan.org Git - sonarqube.git/blob
750aeace8a05cd9ba750a83c5e6ff935a627d26d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.ce.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.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;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36
37 public class DefaultAssigneeTest {
38
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";
42
43   @Rule
44   public DbTester db = DbTester.create();
45
46   private MapSettings settings = new MapSettings();
47   private ConfigurationRepository settingsRepository = new TestSettingsRepository(settings.asConfig());
48   private AnalysisMetadataHolderImpl analysisMetadataHolder = new AnalysisMetadataHolderImpl();
49   private OrganizationDto organizationDto;
50
51   private DefaultAssignee underTest = new DefaultAssignee(db.getDbClient(), settingsRepository, analysisMetadataHolder);
52
53   @Before
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)));
58   }
59
60   @Test
61   public void no_default_assignee() {
62     assertThat(underTest.loadDefaultAssigneeUuid()).isNull();
63   }
64
65   @Test
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);
70
71     assertThat(underTest.loadDefaultAssigneeUuid()).isEqualTo(userDto.getUuid());
72   }
73
74   @Test
75   public void configured_login_does_not_exist() {
76     settings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, "erik");
77
78     assertThat(underTest.loadDefaultAssigneeUuid()).isNull();
79   }
80
81   @Test
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));
85
86     assertThat(underTest.loadDefaultAssigneeUuid()).isNull();
87   }
88
89   @Test
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);
95
96     assertThat(underTest.loadDefaultAssigneeUuid()).isNull();
97   }
98
99   @Test
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());
105
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());
109   }
110 }