3 * Copyright (C) 2009-2022 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.setting;
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.collect.ImmutableSet;
24 import java.util.Collections;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.sonar.api.config.Configuration;
29 import org.sonar.api.config.internal.MapSettings;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.property.PropertiesDao;
34 import org.sonar.db.property.PropertyDto;
36 import static java.util.Collections.emptyList;
37 import static java.util.Collections.singleton;
38 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
39 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.ArgumentMatchers.anyBoolean;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.verifyNoInteractions;
46 import static org.mockito.Mockito.verifyNoMoreInteractions;
47 import static org.mockito.Mockito.when;
49 public class ProjectConfigurationLoaderImplTest {
50 private final DbClient dbClient = mock(DbClient.class);
51 private final DbSession dbSession = mock(DbSession.class);
52 private final PropertiesDao propertiesDao = mock(PropertiesDao.class);
53 private final MapSettings globalSettings = new MapSettings();
54 private final ProjectConfigurationLoaderImpl underTest = new ProjectConfigurationLoaderImpl(globalSettings, dbClient);
58 when(dbClient.openSession(anyBoolean()))
59 .thenThrow(new IllegalStateException("ProjectConfigurationLoaderImpl should not open DB session"));
60 when(dbClient.propertiesDao()).thenReturn(propertiesDao);
64 public void returns_empty_map_when_no_component() {
65 assertThat(underTest.loadProjectConfigurations(dbSession, Collections.emptySet()))
68 verifyNoInteractions(propertiesDao);
72 public void return_configuration_with_just_global_settings_when_no_component_settings() {
73 String key = randomAlphanumeric(3);
74 String value = randomAlphanumeric(4);
75 String componentDbKey = randomAlphanumeric(5);
76 String componentUuid = randomAlphanumeric(6);
77 globalSettings.setProperty(key, value);
78 when(propertiesDao.selectProjectProperties(dbSession, componentDbKey))
79 .thenReturn(emptyList());
80 ComponentDto component = newComponentDto(componentDbKey, componentUuid);
82 Map<String, Configuration> configurations = underTest.loadProjectConfigurations(dbSession, singleton(component));
84 assertThat(configurations)
85 .containsOnlyKeys(componentUuid);
86 assertThat(configurations.get(componentUuid).get(key)).contains(value);
90 public void returns_single_configuration_for_single_project_load() {
91 String key = randomAlphanumeric(3);
92 String value = randomAlphanumeric(4);
93 String componentDbKey = randomAlphanumeric(5);
94 String componentUuid = randomAlphanumeric(6);
95 globalSettings.setProperty(key, value);
96 when(propertiesDao.selectProjectProperties(dbSession, componentDbKey))
97 .thenReturn(emptyList());
98 ComponentDto component = newComponentDto(componentDbKey, componentUuid);
100 Configuration configuration = underTest.loadProjectConfiguration(dbSession, component);
101 assertThat(configuration.get(key)).hasValue(value);
105 public void return_configuration_with_global_settings_and_component_settings() {
106 String globalKey = randomAlphanumeric(3);
107 String globalValue = randomAlphanumeric(4);
108 String componentDbKey = randomAlphanumeric(5);
109 String componentUuid = randomAlphanumeric(6);
110 String projectPropKey1 = randomAlphanumeric(7);
111 String projectPropValue1 = randomAlphanumeric(8);
112 String projectPropKey2 = randomAlphanumeric(9);
113 String projectPropValue2 = randomAlphanumeric(10);
114 globalSettings.setProperty(globalKey, globalValue);
115 when(propertiesDao.selectProjectProperties(dbSession, componentDbKey))
116 .thenReturn(ImmutableList.of(newPropertyDto(projectPropKey1, projectPropValue1), newPropertyDto(projectPropKey2, projectPropValue2)));
117 ComponentDto component = newComponentDto(componentDbKey, componentUuid);
119 Map<String, Configuration> configurations = underTest.loadProjectConfigurations(dbSession, singleton(component));
121 assertThat(configurations)
122 .containsOnlyKeys(componentUuid);
123 assertThat(configurations.get(componentUuid).get(globalKey)).contains(globalValue);
124 assertThat(configurations.get(componentUuid).get(projectPropKey1)).contains(projectPropValue1);
125 assertThat(configurations.get(componentUuid).get(projectPropKey2)).contains(projectPropValue2);
129 public void return_configuration_with_global_settings_main_branch_settings_and_branch_settings() {
131 String globalKey = randomAlphanumeric(3);
132 String globalValue = randomAlphanumeric(4);
133 String mainBranchDbKey = randomAlphanumeric(5);
134 String branchDbKey = mainBranchDbKey + ComponentDto.BRANCH_KEY_SEPARATOR + randomAlphabetic(5);
135 String branchUuid = randomAlphanumeric(6);
136 String mainBranchPropKey = randomAlphanumeric(7);
137 String mainBranchPropValue = randomAlphanumeric(8);
138 String branchPropKey = randomAlphanumeric(9);
139 String branchPropValue = randomAlphanumeric(10);
140 globalSettings.setProperty(globalKey, globalValue);
141 when(propertiesDao.selectProjectProperties(dbSession, mainBranchDbKey))
142 .thenReturn(ImmutableList.of(newPropertyDto(mainBranchPropKey, mainBranchPropValue)));
143 when(propertiesDao.selectProjectProperties(dbSession, branchDbKey))
144 .thenReturn(ImmutableList.of(newPropertyDto(branchPropKey, branchPropValue)));
145 ComponentDto component = newComponentDto(branchDbKey, branchUuid);
147 Map<String, Configuration> configurations = underTest.loadProjectConfigurations(dbSession, singleton(component));
149 assertThat(configurations)
150 .containsOnlyKeys(branchUuid);
151 assertThat(configurations.get(branchUuid).get(globalKey)).contains(globalValue);
152 assertThat(configurations.get(branchUuid).get(mainBranchPropKey)).contains(mainBranchPropValue);
153 assertThat(configurations.get(branchUuid).get(branchPropKey)).contains(branchPropValue);
157 public void loads_configuration_of_any_given_component_only_once() {
158 String mainBranch1DbKey = randomAlphanumeric(4);
159 String mainBranch1Uuid = randomAlphanumeric(5);
160 String branch1DbKey = mainBranch1DbKey + ComponentDto.BRANCH_KEY_SEPARATOR + randomAlphabetic(5);
161 String branch1Uuid = randomAlphanumeric(6);
162 String branch2DbKey = mainBranch1DbKey + ComponentDto.BRANCH_KEY_SEPARATOR + randomAlphabetic(7);
163 String branch2Uuid = randomAlphanumeric(8);
164 String mainBranch2DbKey = randomAlphanumeric(14);
165 String mainBranch2Uuid = randomAlphanumeric(15);
166 String branch3DbKey = mainBranch2DbKey + ComponentDto.BRANCH_KEY_SEPARATOR + randomAlphabetic(5);
167 String branch3Uuid = randomAlphanumeric(16);
169 ComponentDto mainBranch1 = newComponentDto(mainBranch1DbKey, mainBranch1Uuid);
170 ComponentDto branch1 = newComponentDto(branch1DbKey, branch1Uuid);
171 ComponentDto branch2 = newComponentDto(branch2DbKey, branch2Uuid);
172 ComponentDto mainBranch2 = newComponentDto(mainBranch2DbKey, mainBranch2Uuid);
173 ComponentDto branch3 = newComponentDto(branch3DbKey, branch3Uuid);
175 underTest.loadProjectConfigurations(dbSession, ImmutableSet.of(mainBranch1, mainBranch2, branch1, branch2, branch3));
177 verify(propertiesDao, times(1)).selectProjectProperties(dbSession, mainBranch1DbKey);
178 verify(propertiesDao, times(1)).selectProjectProperties(dbSession, mainBranch2DbKey);
179 verify(propertiesDao, times(1)).selectProjectProperties(dbSession, branch1DbKey);
180 verify(propertiesDao, times(1)).selectProjectProperties(dbSession, branch2DbKey);
181 verify(propertiesDao, times(1)).selectProjectProperties(dbSession, branch3DbKey);
182 verifyNoMoreInteractions(propertiesDao);
185 private ComponentDto newComponentDto(String componentDbKey, String componentUuid) {
186 return new ComponentDto().setKey(componentDbKey).setUuid(componentUuid);
189 private PropertyDto newPropertyDto(String projectKey1, String projectValue1) {
190 return new PropertyDto()
192 .setValue(projectValue1);