]> source.dussan.org Git - sonarqube.git/blob
c86aa2e4d601648e1000927fe8735834ab659dc5
[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.component;
21
22 import java.util.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.config.Configuration;
26 import org.sonar.api.config.internal.MapSettings;
27 import org.sonar.api.utils.System2;
28 import org.sonar.ce.settings.ProjectConfigurationFactory;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.property.PropertyDto;
33 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
34 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
35 import org.sonar.server.computation.task.projectanalysis.analysis.Project;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40
41 public class ConfigurationRepositoryTest {
42
43   private static Project PROJECT = new Project("UUID", "KEY", "NAME");
44
45   @Rule
46   public final DbTester db = DbTester.create(System2.INSTANCE);
47
48   private DbClient dbClient = db.getDbClient();
49   private MapSettings globalSettings = new MapSettings();
50   private AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
51   private ConfigurationRepository underTest = new ConfigurationRepositoryImpl(analysisMetadataHolder, new ProjectConfigurationFactory(globalSettings, dbClient));
52
53   @Test
54   public void get_project_settings_from_global_settings() {
55     analysisMetadataHolder.setProject(PROJECT).setBranch(null);
56     globalSettings.setProperty("key", "value");
57
58     Configuration config = underTest.getConfiguration();
59
60     assertThat(config.get("key")).hasValue("value");
61   }
62
63   @Test
64   public void get_project_settings_from_db() {
65     ComponentDto project = db.components().insertPrivateProject();
66     analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
67     insertProjectProperty(project, "key", "value");
68
69     Configuration config = underTest.getConfiguration();
70
71     assertThat(config.get("key")).hasValue("value");
72   }
73
74   @Test
75   public void call_twice_get_project_settings() {
76     analysisMetadataHolder.setProject(PROJECT).setBranch(null);
77     globalSettings.setProperty("key", "value");
78
79     Configuration config = underTest.getConfiguration();
80     assertThat(config.get("key")).hasValue("value");
81
82     config = underTest.getConfiguration();
83     assertThat(config.get("key")).hasValue("value");
84   }
85
86   @Test
87   public void project_settings_override_global_settings() {
88     globalSettings.setProperty("key", "value1");
89     ComponentDto project = db.components().insertPrivateProject();
90     insertProjectProperty(project, "key", "value2");
91     analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
92
93     Configuration config = underTest.getConfiguration();
94     assertThat(config.get("key")).hasValue("value2");
95   }
96
97   @Test
98   public void project_settings_are_cached_to_avoid_db_access() {
99     ComponentDto project = db.components().insertPrivateProject();
100     insertProjectProperty(project, "key", "value");
101     analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(null);
102
103     Configuration config = underTest.getConfiguration();
104     assertThat(config.get("key")).hasValue("value");
105
106     db.executeUpdateSql("delete from properties");
107     db.commit();
108
109     assertThat(config.get("key")).hasValue("value");
110   }
111
112   @Test
113   public void branch_settings() {
114     ComponentDto project = db.components().insertMainBranch();
115     ComponentDto branchDto = db.components().insertProjectBranch(project);
116     Branch branch = mock(Branch.class);
117     when(branch.getName()).thenReturn(Optional.of(branchDto.getBranch()));
118     analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(branch);
119     globalSettings.setProperty("global", "global value");
120     insertProjectProperty(project, "project", "project value");
121     insertProjectProperty(branchDto, "branch", "branch value");
122
123     Configuration config = underTest.getConfiguration();
124
125     assertThat(config.get("global")).hasValue("global value");
126     assertThat(config.get("project")).hasValue("project value");
127     assertThat(config.get("branch")).hasValue("branch value");
128   }
129
130   private void insertProjectProperty(ComponentDto project, String propertyKey, String propertyValue) {
131     db.properties().insertProperties(new PropertyDto().setKey(propertyKey).setValue(propertyValue).setResourceId(project.getId()));
132   }
133 }