diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-09-30 12:23:26 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-10-12 20:03:44 +0000 |
commit | 50b12df3787d87958fcef7fbf8c00598b141f1ee (patch) | |
tree | cbc6fd2927bd89d8f68b84d06e00dd419c4025e5 /server/sonar-server-common | |
parent | c85e433a567b8580a1fd5e185b4f5bc73b6e53e6 (diff) | |
download | sonarqube-50b12df3787d87958fcef7fbf8c00598b141f1ee.tar.gz sonarqube-50b12df3787d87958fcef7fbf8c00598b141f1ee.zip |
SONAR-17352 Refactor component keys to not include branch suffix
Diffstat (limited to 'server/sonar-server-common')
3 files changed, 65 insertions, 173 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/setting/ProjectConfigurationLoader.java b/server/sonar-server-common/src/main/java/org/sonar/server/setting/ProjectConfigurationLoader.java index 7ccb239e9c2..afcb0965883 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/setting/ProjectConfigurationLoader.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/setting/ProjectConfigurationLoader.java @@ -19,31 +19,18 @@ */ package org.sonar.server.setting; -import java.util.Collections; -import java.util.Map; -import java.util.Set; import org.sonar.api.config.Configuration; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; -import static java.lang.String.format; -import static java.util.Objects.requireNonNull; - public interface ProjectConfigurationLoader { /** * Loads configuration for the specified components. - * * <p> * Returns the applicable component configuration with most specific configuration overriding more global ones * (eg. global > project > branch). - * * <p> * Any component is accepted but SQ only supports specific properties for projects and branches. */ - Map<String, Configuration> loadProjectConfigurations(DbSession dbSession, Set<ComponentDto> projects); - - default Configuration loadProjectConfiguration(DbSession dbSession, ComponentDto project) { - Map<String, Configuration> configurations = loadProjectConfigurations(dbSession, Collections.singleton(project)); - return requireNonNull(configurations.get(project.uuid()), () -> format("Configuration for project '%s' is not found", project.getKey())); - } + Configuration loadProjectConfiguration(DbSession dbSession, ComponentDto project); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/setting/ProjectConfigurationLoaderImpl.java b/server/sonar-server-common/src/main/java/org/sonar/server/setting/ProjectConfigurationLoaderImpl.java index fc7c5b17148..8343fc87f38 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/setting/ProjectConfigurationLoaderImpl.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/setting/ProjectConfigurationLoaderImpl.java @@ -20,10 +20,6 @@ package org.sonar.server.setting; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.function.Function; -import java.util.stream.Collectors; import org.sonar.api.config.Configuration; import org.sonar.api.config.internal.Settings; import org.sonar.db.DbClient; @@ -31,8 +27,6 @@ import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.property.PropertyDto; -import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; - public class ProjectConfigurationLoaderImpl implements ProjectConfigurationLoader { private final Settings globalSettings; private final DbClient dbClient; @@ -43,31 +37,26 @@ public class ProjectConfigurationLoaderImpl implements ProjectConfigurationLoade } @Override - public Map<String, Configuration> loadProjectConfigurations(DbSession dbSession, Set<ComponentDto> projects) { - Set<String> mainBranchDbKeys = projects.stream().map(ComponentDto::getKey).collect(Collectors.toSet()); - Map<String, ChildSettings> mainBranchSettingsByDbKey = loadMainBranchConfigurations(dbSession, mainBranchDbKeys); - return projects.stream() - .collect(uniqueIndex(ComponentDto::uuid, component -> { - if (component.getKey().equals(component.getKey())) { - return mainBranchSettingsByDbKey.get(component.getKey()).asConfiguration(); - } + public Configuration loadProjectConfiguration(DbSession dbSession, ComponentDto projectOrBranch) { + boolean isMainBranch = projectOrBranch.getMainBranchProjectUuid() == null; + String mainBranchUuid = isMainBranch ? projectOrBranch.branchUuid() : projectOrBranch.getMainBranchProjectUuid(); + ChildSettings mainBranchSettings = loadMainBranchConfiguration(dbSession, mainBranchUuid); + + if (isMainBranch) { + return mainBranchSettings.asConfiguration(); + } - ChildSettings settings = new ChildSettings(mainBranchSettingsByDbKey.get(component.getKey())); - dbClient.propertiesDao() - .selectProjectProperties(dbSession, component.getKey()) - .forEach(property -> settings.setProperty(property.getKey(), property.getValue())); - return settings.asConfiguration(); - })); + ChildSettings settings = new ChildSettings(mainBranchSettings); + dbClient.propertiesDao() + .selectComponentProperties(dbSession, projectOrBranch.uuid()) + .forEach(property -> settings.setProperty(property.getKey(), property.getValue())); + return settings.asConfiguration(); } - private Map<String, ChildSettings> loadMainBranchConfigurations(DbSession dbSession, Set<String> dbKeys) { - return dbKeys.stream().collect(uniqueIndex(Function.identity(), dbKey -> { - ChildSettings settings = new ChildSettings(globalSettings); - List<PropertyDto> propertyDtos = dbClient.propertiesDao() - .selectProjectProperties(dbSession, dbKey); - propertyDtos - .forEach(property -> settings.setProperty(property.getKey(), property.getValue())); - return settings; - })); + private ChildSettings loadMainBranchConfiguration(DbSession dbSession, String uuid) { + ChildSettings settings = new ChildSettings(globalSettings); + List<PropertyDto> propertyDtos = dbClient.propertiesDao().selectComponentProperties(dbSession, uuid); + propertyDtos.forEach(property -> settings.setProperty(property.getKey(), property.getValue())); + return settings; } } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/setting/ProjectConfigurationLoaderImplTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/setting/ProjectConfigurationLoaderImplTest.java index c25f8b7ea04..af93180a1be 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/setting/ProjectConfigurationLoaderImplTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/setting/ProjectConfigurationLoaderImplTest.java @@ -19,176 +19,92 @@ */ package org.sonar.server.setting; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import java.util.Collections; -import java.util.Map; +import javax.annotation.Nullable; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.sonar.api.config.Configuration; import org.sonar.api.config.internal.MapSettings; import org.sonar.db.DbClient; -import org.sonar.db.DbSession; +import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.property.PropertiesDao; -import org.sonar.db.property.PropertyDto; -import static java.util.Collections.emptyList; -import static java.util.Collections.singleton; -import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; -import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class ProjectConfigurationLoaderImplTest { - private final DbClient dbClient = mock(DbClient.class); - private final DbSession dbSession = mock(DbSession.class); - private final PropertiesDao propertiesDao = mock(PropertiesDao.class); + @Rule + public DbTester db = DbTester.create(); + + private final String globalPropKey = randomAlphanumeric(9); + private final String globalPropValue = randomAlphanumeric(10); + private final String mainBranchPropKey = randomAlphanumeric(7); + private final String mainBranchPropValue = randomAlphanumeric(8); + private final String branchPropKey = randomAlphanumeric(9); + private final String branchPropValue = randomAlphanumeric(10); + + private final String mainBranchUuid = randomAlphanumeric(6); + private final String branchUuid = randomAlphanumeric(6); private final MapSettings globalSettings = new MapSettings(); - private final ProjectConfigurationLoaderImpl underTest = new ProjectConfigurationLoaderImpl(globalSettings, dbClient); + private ProjectConfigurationLoaderImpl underTest; @Before public void setUp() { - when(dbClient.openSession(anyBoolean())) - .thenThrow(new IllegalStateException("ProjectConfigurationLoaderImpl should not open DB session")); - when(dbClient.propertiesDao()).thenReturn(propertiesDao); - } - - @Test - public void returns_empty_map_when_no_component() { - assertThat(underTest.loadProjectConfigurations(dbSession, Collections.emptySet())) - .isEmpty(); - - verifyNoInteractions(propertiesDao); + underTest = new ProjectConfigurationLoaderImpl(globalSettings, db.getDbClient()); } @Test public void return_configuration_with_just_global_settings_when_no_component_settings() { - String key = randomAlphanumeric(3); - String value = randomAlphanumeric(4); - String componentDbKey = randomAlphanumeric(5); - String componentUuid = randomAlphanumeric(6); - globalSettings.setProperty(key, value); - when(propertiesDao.selectProjectProperties(dbSession, componentDbKey)) - .thenReturn(emptyList()); - ComponentDto component = newComponentDto(componentDbKey, componentUuid); - - Map<String, Configuration> configurations = underTest.loadProjectConfigurations(dbSession, singleton(component)); - - assertThat(configurations) - .containsOnlyKeys(componentUuid); - assertThat(configurations.get(componentUuid).get(key)).contains(value); - } + globalSettings.setProperty(mainBranchPropKey, mainBranchPropValue); + ComponentDto component = newComponentDto(mainBranchUuid); - @Test - public void returns_single_configuration_for_single_project_load() { - String key = randomAlphanumeric(3); - String value = randomAlphanumeric(4); - String componentDbKey = randomAlphanumeric(5); - String componentUuid = randomAlphanumeric(6); - globalSettings.setProperty(key, value); - when(propertiesDao.selectProjectProperties(dbSession, componentDbKey)) - .thenReturn(emptyList()); - ComponentDto component = newComponentDto(componentDbKey, componentUuid); - - Configuration configuration = underTest.loadProjectConfiguration(dbSession, component); - assertThat(configuration.get(key)).hasValue(value); + Configuration configuration = underTest.loadProjectConfiguration(db.getSession(), component); + + assertThat(configuration.get(mainBranchPropKey)).contains(mainBranchPropValue); } @Test public void return_configuration_with_global_settings_and_component_settings() { - String globalKey = randomAlphanumeric(3); - String globalValue = randomAlphanumeric(4); - String componentDbKey = randomAlphanumeric(5); - String componentUuid = randomAlphanumeric(6); String projectPropKey1 = randomAlphanumeric(7); String projectPropValue1 = randomAlphanumeric(8); String projectPropKey2 = randomAlphanumeric(9); String projectPropValue2 = randomAlphanumeric(10); - globalSettings.setProperty(globalKey, globalValue); - when(propertiesDao.selectProjectProperties(dbSession, componentDbKey)) - .thenReturn(ImmutableList.of(newPropertyDto(projectPropKey1, projectPropValue1), newPropertyDto(projectPropKey2, projectPropValue2))); - ComponentDto component = newComponentDto(componentDbKey, componentUuid); - - Map<String, Configuration> configurations = underTest.loadProjectConfigurations(dbSession, singleton(component)); - - assertThat(configurations) - .containsOnlyKeys(componentUuid); - assertThat(configurations.get(componentUuid).get(globalKey)).contains(globalValue); - assertThat(configurations.get(componentUuid).get(projectPropKey1)).contains(projectPropValue1); - assertThat(configurations.get(componentUuid).get(projectPropKey2)).contains(projectPropValue2); + globalSettings.setProperty(globalPropKey, globalPropValue); + db.properties().insertProperty(projectPropKey1, projectPropValue1, mainBranchUuid); + db.properties().insertProperty(projectPropKey2, projectPropValue2, mainBranchUuid); + ComponentDto component = newComponentDto(mainBranchUuid); + + Configuration configuration = underTest.loadProjectConfiguration(db.getSession(), component); + + assertThat(configuration.get(globalPropKey)).contains(globalPropValue); + assertThat(configuration.get(projectPropKey1)).contains(projectPropValue1); + assertThat(configuration.get(projectPropKey2)).contains(projectPropValue2); } @Test public void return_configuration_with_global_settings_main_branch_settings_and_branch_settings() { - // TODO - String globalKey = randomAlphanumeric(3); - String globalValue = randomAlphanumeric(4); - String mainBranchDbKey = randomAlphanumeric(5); - String branchDbKey = mainBranchDbKey + ComponentDto.BRANCH_KEY_SEPARATOR + randomAlphabetic(5); - String branchUuid = randomAlphanumeric(6); - String mainBranchPropKey = randomAlphanumeric(7); - String mainBranchPropValue = randomAlphanumeric(8); - String branchPropKey = randomAlphanumeric(9); - String branchPropValue = randomAlphanumeric(10); - globalSettings.setProperty(globalKey, globalValue); - when(propertiesDao.selectProjectProperties(dbSession, mainBranchDbKey)) - .thenReturn(ImmutableList.of(newPropertyDto(mainBranchPropKey, mainBranchPropValue))); - when(propertiesDao.selectProjectProperties(dbSession, branchDbKey)) - .thenReturn(ImmutableList.of(newPropertyDto(branchPropKey, branchPropValue))); - ComponentDto component = newComponentDto(branchDbKey, branchUuid); - - Map<String, Configuration> configurations = underTest.loadProjectConfigurations(dbSession, singleton(component)); - - assertThat(configurations) - .containsOnlyKeys(branchUuid); - assertThat(configurations.get(branchUuid).get(globalKey)).contains(globalValue); - assertThat(configurations.get(branchUuid).get(mainBranchPropKey)).contains(mainBranchPropValue); - assertThat(configurations.get(branchUuid).get(branchPropKey)).contains(branchPropValue); - } + globalSettings.setProperty(globalPropKey, globalPropValue); - @Test - public void loads_configuration_of_any_given_component_only_once() { - String mainBranch1DbKey = randomAlphanumeric(4); - String mainBranch1Uuid = randomAlphanumeric(5); - String branch1DbKey = mainBranch1DbKey + ComponentDto.BRANCH_KEY_SEPARATOR + randomAlphabetic(5); - String branch1Uuid = randomAlphanumeric(6); - String branch2DbKey = mainBranch1DbKey + ComponentDto.BRANCH_KEY_SEPARATOR + randomAlphabetic(7); - String branch2Uuid = randomAlphanumeric(8); - String mainBranch2DbKey = randomAlphanumeric(14); - String mainBranch2Uuid = randomAlphanumeric(15); - String branch3DbKey = mainBranch2DbKey + ComponentDto.BRANCH_KEY_SEPARATOR + randomAlphabetic(5); - String branch3Uuid = randomAlphanumeric(16); - - ComponentDto mainBranch1 = newComponentDto(mainBranch1DbKey, mainBranch1Uuid); - ComponentDto branch1 = newComponentDto(branch1DbKey, branch1Uuid); - ComponentDto branch2 = newComponentDto(branch2DbKey, branch2Uuid); - ComponentDto mainBranch2 = newComponentDto(mainBranch2DbKey, mainBranch2Uuid); - ComponentDto branch3 = newComponentDto(branch3DbKey, branch3Uuid); - - underTest.loadProjectConfigurations(dbSession, ImmutableSet.of(mainBranch1, mainBranch2, branch1, branch2, branch3)); - - verify(propertiesDao, times(1)).selectProjectProperties(dbSession, mainBranch1DbKey); - verify(propertiesDao, times(1)).selectProjectProperties(dbSession, mainBranch2DbKey); - verify(propertiesDao, times(1)).selectProjectProperties(dbSession, branch1DbKey); - verify(propertiesDao, times(1)).selectProjectProperties(dbSession, branch2DbKey); - verify(propertiesDao, times(1)).selectProjectProperties(dbSession, branch3DbKey); - verifyNoMoreInteractions(propertiesDao); + db.properties().insertProperty(mainBranchPropKey, mainBranchPropValue, mainBranchUuid); + db.properties().insertProperty(branchPropKey, branchPropValue, branchUuid); + + ComponentDto component = newComponentDto(branchUuid, mainBranchUuid); + Configuration configuration = underTest.loadProjectConfiguration(db.getSession(), component); + + assertThat(configuration.get(globalPropKey)).contains(globalPropValue); + assertThat(configuration.get(mainBranchPropKey)).contains(mainBranchPropValue); + assertThat(configuration.get(branchPropKey)).contains(branchPropValue); } - private ComponentDto newComponentDto(String componentDbKey, String componentUuid) { - return new ComponentDto().setKey(componentDbKey).setUuid(componentUuid); + private ComponentDto newComponentDto(String uuid) { + return newComponentDto(uuid, null); } - private PropertyDto newPropertyDto(String projectKey1, String projectValue1) { - return new PropertyDto() - .setKey(projectKey1) - .setValue(projectValue1); + private ComponentDto newComponentDto(String uuid, @Nullable String mainBranchUuid) { + return new ComponentDto().setUuid(uuid).setBranchUuid(uuid).setMainBranchProjectUuid(mainBranchUuid); } } |