]> source.dussan.org Git - sonarqube.git/blob
d471a915e7ea4e43a57228d5d9ee4787b557471f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.projectexport.steps;
21
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;
39
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;
43
44 public class ExportSettingsStepTest {
45
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");
59
60   @Rule
61   public LogTester logTester = new LogTester();
62   @Rule
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);
68
69   @Before
70   public void setUp() {
71     dbTester.components().insertPublicProject(PROJECT);
72     dbTester.components().insertPublicProject(ANOTHER_PROJECT);
73     dbTester.commit();
74     projectHolder.setProjectDto(dbTester.components().getProjectDto(PROJECT));
75     componentRepository.register(1, PROJECT.uuid(), false);
76   }
77
78   @Test
79   public void export_zero_settings() {
80     underTest.execute(new TestComputationStepContext());
81
82     assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.SETTINGS)).isEmpty();
83     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 settings exported");
84   }
85
86   @Test
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);
96
97     underTest.execute(new TestComputationStepContext());
98
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");
103   }
104
105   @Test
106   public void exclude_properties_specific_to_environment() {
107     insertProperties(PROJECT.getKey(), PROJECT.name(), newDto("sonar.issues.defaultAssigneeLogin", null, PROJECT));
108
109     underTest.execute(new TestComputationStepContext());
110
111     assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.SETTINGS)).isEmpty();
112     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 settings exported");
113   }
114
115   @Test
116   public void test_exported_fields() {
117     PropertyDto dto = newDto("p1", "v1", PROJECT);
118     insertProperties(PROJECT.getKey(), PROJECT.name(), dto);
119
120     underTest.execute(new TestComputationStepContext());
121
122     ProjectDump.Setting exportedProp = dumpWriter.getWrittenMessagesOf(DumpElement.SETTINGS).get(0);
123     assertThat(exportedProp.getKey()).isEqualTo(dto.getKey());
124     assertThat(exportedProp.getValue()).isEqualTo(dto.getValue());
125   }
126
127   @Test
128   public void property_can_have_empty_value() {
129     insertProperties(PROJECT.getKey(), PROJECT.name(), newDto("p1", null, PROJECT));
130
131     underTest.execute(new TestComputationStepContext());
132
133     ProjectDump.Setting exportedProp = dumpWriter.getWrittenMessagesOf(DumpElement.SETTINGS).get(0);
134     assertThat(exportedProp.getKey()).isEqualTo("p1");
135     assertThat(exportedProp.getValue()).isEmpty();
136   }
137
138   @Test
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));
143
144     assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
145       .isInstanceOf(IllegalStateException.class)
146       .hasMessage("Settings Export failed after processing 1 settings successfully");
147   }
148
149   @Test
150   public void test_getDescription() {
151     assertThat(underTest.getDescription()).isEqualTo("Export settings");
152   }
153
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());
158     }
159     return dto;
160   }
161
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);
165     }
166     dbTester.commit();
167   }
168 }