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.ComponentDto;
33 import org.sonar.db.component.ComponentTesting;
34 import org.sonar.server.computation.batch.TreeRootHolderRule;
35 import org.sonar.server.computation.component.MutableDbIdsRepositoryRule;
36 import org.sonar.server.computation.component.ProjectViewAttributes;
37 import org.sonar.server.computation.component.ViewsComponent;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42 import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW;
43 import static org.sonar.server.computation.component.Component.Type.SUBVIEW;
44 import static org.sonar.server.computation.component.Component.Type.VIEW;
45 import static org.sonar.server.computation.component.ViewsComponent.builder;
48 public class ViewsPersistComponentsStepTest extends BaseStepTest {
50 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
52 private static final String VIEW_KEY = "VIEW_KEY";
53 private static final String VIEW_NAME = "VIEW_NAME";
54 private static final String VIEW_DESCRIPTION = "view description";
55 private static final String VIEW_UUID = "VIEW_UUID";
56 private static final String SUBVIEW_1_KEY = "SUBVIEW_1_KEY";
57 private static final String SUBVIEW_1_NAME = "SUBVIEW_1_NAME";
58 private static final String SUBVIEW_1_DESCRIPTION = "subview 1 description";
59 private static final String SUBVIEW_1_UUID = "SUBVIEW_1_UUID";
60 private static final String PROJECT_VIEW_1_KEY = "PV1_KEY";
61 private static final String PROJECT_VIEW_1_NAME = "PV1_NAME";
62 private static final String PROJECT_VIEW_1_UUID = "PV1_UUID";
63 private static final long PROJECT_1_ID = 123;
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 PersistComponentsStep underTest;
81 public void setup() throws Exception {
82 dbTester.truncateTables();
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 persistComponentDto(createViewDto());
112 treeRootHolder.setRoot(createViewBuilder().build());
116 assertRowsCountInTableProjects(1);
118 assertDtoNotUpdated(VIEW_KEY);
122 public void persist_view_with_projectView() {
123 treeRootHolder.setRoot(
125 .addChildren(createProjectView1Builder().build())
130 assertRowsCountInTableProjects(2);
132 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
133 assertDtoIsView(viewDto);
135 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
136 assertDtoIsProjectView1(pv1Dto, viewDto, viewDto);
140 public void persist_empty_subview() {
141 treeRootHolder.setRoot(
144 createSubView1Builder().build())
149 assertRowsCountInTableProjects(2);
151 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
152 assertDtoIsView(viewDto);
154 ComponentDto sv1Dto = getComponentFromDb(SUBVIEW_1_KEY);
155 assertDtoIsSubView1(viewDto, sv1Dto);
159 public void persist_existing_empty_subview_under_existing_view() {
160 ComponentDto viewDto = createViewDto();
161 persistComponentDto(viewDto);
162 persistComponentDto(ComponentTesting.newSubView(viewDto, SUBVIEW_1_UUID, SUBVIEW_1_KEY).setName(SUBVIEW_1_NAME));
164 treeRootHolder.setRoot(
167 createSubView1Builder().build())
172 assertRowsCountInTableProjects(2);
174 assertDtoNotUpdated(VIEW_KEY);
175 assertDtoNotUpdated(SUBVIEW_1_KEY);
179 public void persist_empty_subview_under_existing_view() {
180 persistComponentDto(createViewDto());
182 treeRootHolder.setRoot(
185 createSubView1Builder().build())
190 assertRowsCountInTableProjects(2);
192 assertDtoNotUpdated(VIEW_KEY);
193 assertDtoIsSubView1(getComponentFromDb(VIEW_KEY), getComponentFromDb(SUBVIEW_1_KEY));
197 public void persist_project_view_under_subview() {
198 treeRootHolder.setRoot(
201 createSubView1Builder()
203 createProjectView1Builder().build())
209 assertRowsCountInTableProjects(3);
211 ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
212 assertDtoIsView(viewDto);
213 ComponentDto subView1Dto = getComponentFromDb(SUBVIEW_1_KEY);
214 assertDtoIsSubView1(viewDto, subView1Dto);
215 ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
216 assertDtoIsProjectView1(pv1Dto, viewDto, subView1Dto);
220 public void update_view_name_and_longName() {
221 ComponentDto viewDto = createViewDto().setLongName("another long name").setCreatedAt(now);
222 persistComponentDto(viewDto);
224 treeRootHolder.setRoot(createViewBuilder().build());
228 assertRowsCountInTableProjects(1);
230 ComponentDto newViewDto = getComponentFromDb(VIEW_KEY);
231 assertDtoIsView(newViewDto);
234 private static ViewsComponent.Builder createViewBuilder() {
235 return builder(VIEW, VIEW_KEY).setUuid(VIEW_UUID).setName(VIEW_NAME).setDescription(VIEW_DESCRIPTION);
238 private ViewsComponent.Builder createSubView1Builder() {
239 return builder(SUBVIEW, SUBVIEW_1_KEY).setUuid(SUBVIEW_1_UUID).setName(SUBVIEW_1_NAME).setDescription(SUBVIEW_1_DESCRIPTION);
242 private static ViewsComponent.Builder createProjectView1Builder() {
243 return builder(PROJECT_VIEW, PROJECT_VIEW_1_KEY)
244 .setUuid(PROJECT_VIEW_1_UUID)
245 .setName(PROJECT_VIEW_1_NAME)
246 .setDescription("project view description is not persisted")
247 .setProjectViewAttributes(new ProjectViewAttributes(PROJECT_1_ID));
250 private void persistComponentDto(ComponentDto componentDto) {
251 dbClient.componentDao().insert(dbTester.getSession(), componentDto);
252 dbTester.getSession().commit();
255 private ComponentDto getComponentFromDb(String componentKey) {
256 return dbClient.componentDao().selectByKey(dbTester.getSession(), componentKey).get();
259 private void assertRowsCountInTableProjects(int rowCount) {
260 assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(rowCount);
263 private void assertDtoNotUpdated(String componentKey) {
264 assertThat(getComponentFromDb(componentKey).getCreatedAt()).isNotEqualTo(now);
267 private ComponentDto createViewDto() {
268 return ComponentTesting.newView(VIEW_UUID).setKey(VIEW_KEY).setName(VIEW_NAME);
272 * Assertions to verify the DTO created from {@link #createViewBuilder()}
274 private void assertDtoIsView(ComponentDto projectDto) {
275 assertThat(projectDto.name()).isEqualTo(VIEW_NAME);
276 assertThat(projectDto.longName()).isEqualTo(VIEW_NAME);
277 assertThat(projectDto.description()).isEqualTo(VIEW_DESCRIPTION);
278 assertThat(projectDto.path()).isNull();
279 assertThat(projectDto.uuid()).isEqualTo(VIEW_UUID);
280 assertThat(projectDto.projectUuid()).isEqualTo(projectDto.uuid());
281 assertThat(projectDto.parentProjectId()).isNull();
282 assertThat(projectDto.moduleUuid()).isNull();
283 assertThat(projectDto.moduleUuidPath()).isEqualTo("." + projectDto.uuid() + ".");
284 assertThat(projectDto.qualifier()).isEqualTo(Qualifiers.VIEW);
285 assertThat(projectDto.scope()).isEqualTo(Scopes.PROJECT);
286 assertThat(projectDto.getCopyResourceId()).isNull();
287 assertThat(projectDto.getCreatedAt()).isEqualTo(now);
291 * Assertions to verify the DTO created from {@link #createProjectView1Builder()}
293 private void assertDtoIsSubView1(ComponentDto viewDto, ComponentDto sv1Dto) {
294 assertThat(sv1Dto.name()).isEqualTo(SUBVIEW_1_NAME);
295 assertThat(sv1Dto.longName()).isEqualTo(SUBVIEW_1_NAME);
296 assertThat(sv1Dto.description()).isEqualTo(SUBVIEW_1_DESCRIPTION);
297 assertThat(sv1Dto.path()).isNull();
298 assertThat(sv1Dto.uuid()).isEqualTo(SUBVIEW_1_UUID);
299 assertThat(sv1Dto.projectUuid()).isEqualTo(viewDto.uuid());
300 assertThat(sv1Dto.parentProjectId()).isEqualTo(viewDto.getId());
301 assertThat(sv1Dto.moduleUuid()).isEqualTo(viewDto.uuid());
302 assertThat(sv1Dto.moduleUuidPath()).isEqualTo(viewDto.moduleUuidPath() + sv1Dto.uuid() + ".");
303 assertThat(sv1Dto.qualifier()).isEqualTo(Qualifiers.SUBVIEW);
304 assertThat(sv1Dto.scope()).isEqualTo(Scopes.PROJECT);
305 assertThat(sv1Dto.getCopyResourceId()).isNull();
306 assertThat(sv1Dto.getCreatedAt()).isEqualTo(now);
309 private void assertDtoIsProjectView1(ComponentDto pv1Dto, ComponentDto viewDto, ComponentDto parentViewDto) {
310 assertThat(pv1Dto.name()).isEqualTo(PROJECT_VIEW_1_NAME);
311 assertThat(pv1Dto.longName()).isEqualTo(PROJECT_VIEW_1_NAME);
312 assertThat(pv1Dto.description()).isNull();
313 assertThat(pv1Dto.path()).isNull();
314 assertThat(pv1Dto.uuid()).isEqualTo(PROJECT_VIEW_1_UUID);
315 assertThat(pv1Dto.projectUuid()).isEqualTo(viewDto.uuid());
316 assertThat(pv1Dto.parentProjectId()).isEqualTo(viewDto.getId());
317 assertThat(pv1Dto.moduleUuid()).isEqualTo(parentViewDto.uuid());
318 assertThat(pv1Dto.moduleUuidPath()).isEqualTo(parentViewDto.moduleUuidPath() + pv1Dto.uuid() + ".");
319 assertThat(pv1Dto.qualifier()).isEqualTo(Qualifiers.PROJECT);
320 assertThat(pv1Dto.scope()).isEqualTo(Scopes.FILE);
321 assertThat(pv1Dto.getCopyResourceId()).isEqualTo(PROJECT_1_ID);
322 assertThat(pv1Dto.getCreatedAt()).isEqualTo(now);