3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.computation.step;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.resources.Qualifiers;
28 import org.sonar.api.resources.Scopes;
29 import org.sonar.api.utils.System2;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.component.ComponentDbTester;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.db.component.ComponentTesting;
35 import org.sonar.server.computation.batch.TreeRootHolderRule;
36 import org.sonar.server.computation.component.MutableDbIdsRepositoryRule;
37 import org.sonar.server.computation.component.ProjectViewAttributes;
38 import org.sonar.server.computation.component.ViewsComponent;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43 import static org.sonar.db.component.ComponentTesting.newProjectDto;
44 import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW;
45 import static org.sonar.server.computation.component.Component.Type.SUBVIEW;
46 import static org.sonar.server.computation.component.Component.Type.VIEW;
47 import static org.sonar.server.computation.component.ViewsComponent.builder;
49 public class ViewsPersistComponentsStepTest extends BaseStepTest {
51 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
53 private static final String VIEW_KEY = "VIEW_KEY";
54 private static final String VIEW_NAME = "VIEW_NAME";
55 private static final String VIEW_DESCRIPTION = "view description";
56 private static final String VIEW_UUID = "VIEW_UUID";
57 private static final String SUBVIEW_1_KEY = "SUBVIEW_1_KEY";
58 private static final String SUBVIEW_1_NAME = "SUBVIEW_1_NAME";
59 private static final String SUBVIEW_1_DESCRIPTION = "subview 1 description";
60 private static final String SUBVIEW_1_UUID = "SUBVIEW_1_UUID";
61 private static final String PROJECT_VIEW_1_KEY = "PV1_KEY";
62 private static final String PROJECT_VIEW_1_NAME = "PV1_NAME";
63 private static final String PROJECT_VIEW_1_UUID = "PV1_UUID";
66 public DbTester dbTester = DbTester.create(System2.INSTANCE);
68 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
70 public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder);
72 System2 system2 = mock(System2.class);
74 DbClient dbClient = dbTester.getDbClient();
78 ComponentDbTester componentDbTester = new ComponentDbTester(dbTester);
80 PersistComponentsStep underTest;
83 public void setup() throws Exception {
84 now = DATE_FORMAT.parse("2015-06-02");
85 when(system2.now()).thenReturn(now.getTime());
87 underTest = new PersistComponentsStep(dbClient, treeRootHolder, dbIdsRepository, system2);
91 protected ComputationStep step() {
96 public void persist_empty_view() {
97 treeRootHolder.setRoot(createViewBuilder().build());
101 assertRowsCountInTableProjects(1);
103 ComponentDto projectDto = getComponentFromDb(VIEW_KEY);
104 assertDtoIsView(projectDto);
108 public void persist_existing_empty_view() {
109 // most of the time view already exists since its supposed to be created when config is uploaded
110 persistComponents(newViewDto());
112 treeRootHolder.setRoot(createViewBuilder().build());
116 assertRowsCountInTableProjects(1);
118 assertDtoNotUpdated(VIEW_KEY);
122 public void persist_view_with_projectView() {
123 ComponentDto project = newProjectDto();
124 persistComponents(project);
126 treeRootHolder.setRoot(
128 .addChildren(createProjectView1Builder(project).build())
133 assertRowsCountInTableProjects(3);
135 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
136 assertDtoIsView(viewDto);
138 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
139 assertDtoIsProjectView1(pv1Dto, viewDto, viewDto, project);
143 public void persist_empty_subview() {
144 treeRootHolder.setRoot(
147 createSubView1Builder().build())
152 assertRowsCountInTableProjects(2);
154 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
155 assertDtoIsView(viewDto);
157 ComponentDto sv1Dto = getComponentFromDb(SUBVIEW_1_KEY);
158 assertDtoIsSubView1(viewDto, sv1Dto);
162 public void persist_existing_empty_subview_under_existing_view() {
163 ComponentDto viewDto = newViewDto();
164 persistComponents(viewDto);
165 persistComponents(ComponentTesting.newSubView(viewDto, SUBVIEW_1_UUID, SUBVIEW_1_KEY).setName(SUBVIEW_1_NAME));
167 treeRootHolder.setRoot(
170 createSubView1Builder().build())
175 assertRowsCountInTableProjects(2);
177 assertDtoNotUpdated(VIEW_KEY);
178 assertDtoNotUpdated(SUBVIEW_1_KEY);
182 public void persist_empty_subview_under_existing_view() {
183 persistComponents(newViewDto());
185 treeRootHolder.setRoot(
188 createSubView1Builder().build())
193 assertRowsCountInTableProjects(2);
195 assertDtoNotUpdated(VIEW_KEY);
196 assertDtoIsSubView1(getComponentFromDb(VIEW_KEY), getComponentFromDb(SUBVIEW_1_KEY));
200 public void persist_project_view_under_subview() {
201 ComponentDto project = newProjectDto();
202 persistComponents(project);
204 treeRootHolder.setRoot(
207 createSubView1Builder()
209 createProjectView1Builder(project).build())
215 assertRowsCountInTableProjects(4);
217 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
218 assertDtoIsView(viewDto);
219 ComponentDto subView1Dto = getComponentFromDb(SUBVIEW_1_KEY);
220 assertDtoIsSubView1(viewDto, subView1Dto);
221 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
222 assertDtoIsProjectView1(pv1Dto, viewDto, subView1Dto, project);
226 public void update_view_name_and_longName() {
227 ComponentDto viewDto = newViewDto().setLongName("another long name").setCreatedAt(now);
228 persistComponents(viewDto);
230 treeRootHolder.setRoot(createViewBuilder().build());
234 assertRowsCountInTableProjects(1);
236 ComponentDto newViewDto = getComponentFromDb(VIEW_KEY);
237 assertDtoIsView(newViewDto);
241 public void update_project_view() {
242 ComponentDto view = newViewDto();
243 ComponentDto project = newProjectDto();
244 persistComponents(view, project);
245 ComponentDto projectView = ComponentTesting.newProjectCopy(PROJECT_VIEW_1_UUID, project, view)
246 .setKey(PROJECT_VIEW_1_KEY)
249 persistComponents(projectView);
251 treeRootHolder.setRoot(
253 .addChildren(createProjectView1Builder(project).build())
258 assertRowsCountInTableProjects(3);
260 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
261 assertDtoIsProjectView1(pv1Dto, view, view, project);
265 public void update_copy_resource_id_of_project_view() {
266 ComponentDto view = newViewDto();
267 ComponentDto project1 = newProjectDto();
268 ComponentDto project2 = newProjectDto();
269 persistComponents(view, project1, project2);
271 // Project view in DB is linked to project1
272 ComponentDto projectView = ComponentTesting.newProjectCopy(PROJECT_VIEW_1_UUID, project1, view)
273 .setKey(PROJECT_VIEW_1_KEY)
275 persistComponents(projectView);
277 treeRootHolder.setRoot(
279 // Project view in the View is linked to the first project2
280 .addChildren(createProjectView1Builder(project2).build())
285 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
286 // Project view should now be linked to project2
287 assertDtoIsProjectView1(pv1Dto, view, view, project2);
290 private static ViewsComponent.Builder createViewBuilder() {
291 return builder(VIEW, VIEW_KEY).setUuid(VIEW_UUID).setName(VIEW_NAME).setDescription(VIEW_DESCRIPTION);
294 private ViewsComponent.Builder createSubView1Builder() {
295 return builder(SUBVIEW, SUBVIEW_1_KEY).setUuid(SUBVIEW_1_UUID).setName(SUBVIEW_1_NAME).setDescription(SUBVIEW_1_DESCRIPTION);
298 private static ViewsComponent.Builder createProjectView1Builder(ComponentDto project) {
299 return builder(PROJECT_VIEW, PROJECT_VIEW_1_KEY)
300 .setUuid(PROJECT_VIEW_1_UUID)
301 .setName(PROJECT_VIEW_1_NAME)
302 .setDescription("project view description is not persisted")
303 .setProjectViewAttributes(new ProjectViewAttributes(project.uuid()));
306 private void persistComponents(ComponentDto... componentDtos) {
307 componentDbTester.insertComponents(componentDtos);
310 private ComponentDto getComponentFromDb(String componentKey) {
311 return dbClient.componentDao().selectByKey(dbTester.getSession(), componentKey).get();
314 private void assertRowsCountInTableProjects(int rowCount) {
315 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(rowCount);
318 private void assertDtoNotUpdated(String componentKey) {
319 assertThat(getComponentFromDb(componentKey).getCreatedAt()).isNotEqualTo(now);
322 private static ComponentDto newViewDto() {
323 return ComponentTesting.newView(VIEW_UUID).setKey(VIEW_KEY).setName(VIEW_NAME);
327 * Assertions to verify the DTO created from {@link #createViewBuilder()}
329 private void assertDtoIsView(ComponentDto projectDto) {
330 assertThat(projectDto.name()).isEqualTo(VIEW_NAME);
331 assertThat(projectDto.longName()).isEqualTo(VIEW_NAME);
332 assertThat(projectDto.description()).isEqualTo(VIEW_DESCRIPTION);
333 assertThat(projectDto.path()).isNull();
334 assertThat(projectDto.uuid()).isEqualTo(VIEW_UUID);
335 assertThat(projectDto.projectUuid()).isEqualTo(VIEW_UUID);
336 assertThat(projectDto.getRootUuid()).isEqualTo(VIEW_UUID);
337 assertThat(projectDto.moduleUuid()).isNull();
338 assertThat(projectDto.moduleUuidPath()).isEqualTo("." + projectDto.uuid() + ".");
339 assertThat(projectDto.qualifier()).isEqualTo(Qualifiers.VIEW);
340 assertThat(projectDto.scope()).isEqualTo(Scopes.PROJECT);
341 assertThat(projectDto.getCopyResourceUuid()).isNull();
342 assertThat(projectDto.getCreatedAt()).isEqualTo(now);
346 * Assertions to verify the DTO created from {@link #createProjectView1Builder(ComponentDto)}
348 private void assertDtoIsSubView1(ComponentDto viewDto, ComponentDto sv1Dto) {
349 assertThat(sv1Dto.name()).isEqualTo(SUBVIEW_1_NAME);
350 assertThat(sv1Dto.longName()).isEqualTo(SUBVIEW_1_NAME);
351 assertThat(sv1Dto.description()).isEqualTo(SUBVIEW_1_DESCRIPTION);
352 assertThat(sv1Dto.path()).isNull();
353 assertThat(sv1Dto.uuid()).isEqualTo(SUBVIEW_1_UUID);
354 assertThat(sv1Dto.projectUuid()).isEqualTo(viewDto.uuid());
355 assertThat(sv1Dto.getRootUuid()).isEqualTo(viewDto.uuid());
356 assertThat(sv1Dto.moduleUuid()).isEqualTo(viewDto.uuid());
357 assertThat(sv1Dto.moduleUuidPath()).isEqualTo(viewDto.moduleUuidPath() + sv1Dto.uuid() + ".");
358 assertThat(sv1Dto.qualifier()).isEqualTo(Qualifiers.SUBVIEW);
359 assertThat(sv1Dto.scope()).isEqualTo(Scopes.PROJECT);
360 assertThat(sv1Dto.getCopyResourceUuid()).isNull();
361 assertThat(sv1Dto.getCreatedAt()).isEqualTo(now);
364 private void assertDtoIsProjectView1(ComponentDto pv1Dto, ComponentDto viewDto, ComponentDto parentViewDto, ComponentDto project) {
365 assertThat(pv1Dto.name()).isEqualTo(PROJECT_VIEW_1_NAME);
366 assertThat(pv1Dto.longName()).isEqualTo(PROJECT_VIEW_1_NAME);
367 assertThat(pv1Dto.description()).isNull();
368 assertThat(pv1Dto.path()).isNull();
369 assertThat(pv1Dto.uuid()).isEqualTo(PROJECT_VIEW_1_UUID);
370 assertThat(pv1Dto.projectUuid()).isEqualTo(viewDto.uuid());
371 assertThat(pv1Dto.getRootUuid()).isEqualTo(viewDto.uuid());
372 assertThat(pv1Dto.moduleUuid()).isEqualTo(parentViewDto.uuid());
373 assertThat(pv1Dto.moduleUuidPath()).isEqualTo(parentViewDto.moduleUuidPath() + pv1Dto.uuid() + ".");
374 assertThat(pv1Dto.qualifier()).isEqualTo(Qualifiers.PROJECT);
375 assertThat(pv1Dto.scope()).isEqualTo(Scopes.FILE);
376 assertThat(pv1Dto.getCopyResourceUuid()).isEqualTo(project.uuid());
377 assertThat(pv1Dto.getCreatedAt()).isEqualTo(now);