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.ce.task.projectexport.steps;
22 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
23 import java.util.List;
24 import javax.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.resources.Qualifiers;
29 import org.sonar.api.utils.System2;
30 import org.sonar.api.utils.log.LogTester;
31 import org.sonar.api.utils.log.LoggerLevel;
32 import org.sonar.ce.task.projectexport.component.ComponentRepositoryImpl;
33 import org.sonar.ce.task.projectexport.component.MutableComponentRepository;
34 import org.sonar.ce.task.step.TestComputationStepContext;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.component.ComponentDto;
37 import org.sonar.db.project.ProjectExportMapper;
38 import org.sonar.db.property.PropertyDto;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.assertThatThrownBy;
42 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
44 public class ExportSettingsStepTest {
46 private static final ComponentDto GLOBAL = null;
47 private static final ComponentDto PROJECT = new ComponentDto()
48 .setUuid("project_uuid")
49 .setUuidPath(UUID_PATH_OF_ROOT)
50 .setRootUuid("project_uuid")
51 .setProjectUuid("project_uuid")
52 .setDbKey("the_project");
53 private static final ComponentDto ANOTHER_PROJECT = new ComponentDto()
54 .setUuid("another_project_uuid")
55 .setUuidPath(UUID_PATH_OF_ROOT)
56 .setRootUuid("another_project_uuid")
57 .setProjectUuid("another_project_uuid")
58 .setDbKey("another_project");
61 public LogTester logTester = new LogTester();
63 public DbTester dbTester = DbTester.createWithExtensionMappers(System2.INSTANCE, ProjectExportMapper.class);
64 private MutableComponentRepository componentRepository = new ComponentRepositoryImpl();
65 private MutableProjectHolder projectHolder = new MutableProjectHolderImpl();
66 private FakeDumpWriter dumpWriter = new FakeDumpWriter();
67 private ExportSettingsStep underTest = new ExportSettingsStep(dbTester.getDbClient(), projectHolder, componentRepository, dumpWriter);
71 dbTester.components().insertPublicProject(PROJECT);
72 dbTester.components().insertPublicProject(ANOTHER_PROJECT);
74 projectHolder.setProjectDto(dbTester.components().getProjectDto(PROJECT));
75 componentRepository.register(1, PROJECT.uuid(), false);
79 public void export_zero_settings() {
80 underTest.execute(new TestComputationStepContext());
82 assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.SETTINGS)).isEmpty();
83 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 settings exported");
87 public void export_only_project_settings() {
88 PropertyDto projectProperty1 = newDto("p1", "v1", PROJECT);
89 PropertyDto projectProperty2 = newDto("p2", "v2", PROJECT);
90 // the following properties are not exported
91 PropertyDto propOnOtherProject = newDto("p3", "v3", ANOTHER_PROJECT);
92 PropertyDto globalProperty = newDto("p4", "v4", GLOBAL);
93 insertProperties(PROJECT.getKey(), PROJECT.name(), projectProperty1, projectProperty2);
94 insertProperties(ANOTHER_PROJECT.getKey(), ANOTHER_PROJECT.name(), propOnOtherProject);
95 insertProperties(null, null, globalProperty);
97 underTest.execute(new TestComputationStepContext());
99 List<ProjectDump.Setting> exportedProps = dumpWriter.getWrittenMessagesOf(DumpElement.SETTINGS);
100 assertThat(exportedProps).hasSize(2);
101 assertThat(exportedProps).extracting(ProjectDump.Setting::getKey).containsOnly("p1", "p2");
102 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("2 settings exported");
106 public void exclude_properties_specific_to_environment() {
107 insertProperties(PROJECT.getKey(), PROJECT.name(), newDto("sonar.issues.defaultAssigneeLogin", null, PROJECT));
109 underTest.execute(new TestComputationStepContext());
111 assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.SETTINGS)).isEmpty();
112 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 settings exported");
116 public void test_exported_fields() {
117 PropertyDto dto = newDto("p1", "v1", PROJECT);
118 insertProperties(PROJECT.getKey(), PROJECT.name(), dto);
120 underTest.execute(new TestComputationStepContext());
122 ProjectDump.Setting exportedProp = dumpWriter.getWrittenMessagesOf(DumpElement.SETTINGS).get(0);
123 assertThat(exportedProp.getKey()).isEqualTo(dto.getKey());
124 assertThat(exportedProp.getValue()).isEqualTo(dto.getValue());
128 public void property_can_have_empty_value() {
129 insertProperties(PROJECT.getKey(), PROJECT.name(), newDto("p1", null, PROJECT));
131 underTest.execute(new TestComputationStepContext());
133 ProjectDump.Setting exportedProp = dumpWriter.getWrittenMessagesOf(DumpElement.SETTINGS).get(0);
134 assertThat(exportedProp.getKey()).isEqualTo("p1");
135 assertThat(exportedProp.getValue()).isEmpty();
139 public void throws_ISE_if_error() {
140 dumpWriter.failIfMoreThan(1, DumpElement.SETTINGS);
141 insertProperties(PROJECT.getKey(), PROJECT.name(), newDto("p1", null, PROJECT),
142 newDto("p2", null, PROJECT));
144 assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
145 .isInstanceOf(IllegalStateException.class)
146 .hasMessage("Settings Export failed after processing 1 settings successfully");
150 public void test_getDescription() {
151 assertThat(underTest.getDescription()).isEqualTo("Export settings");
154 private static PropertyDto newDto(String key, @Nullable String value, @Nullable ComponentDto project) {
155 PropertyDto dto = new PropertyDto().setKey(key).setValue(value);
156 if (project != null) {
157 dto.setComponentUuid(project.uuid());
162 private void insertProperties(@Nullable String componentKey, @Nullable String componentName, PropertyDto... dtos) {
163 for (PropertyDto dto : dtos) {
164 dbTester.getDbClient().propertiesDao().saveProperty(dbTester.getSession(), dto, null, componentKey, componentName, Qualifiers.VIEW);