]> source.dussan.org Git - sonarqube.git/blob
446a24397acf3bfe45413adaa500deeb3c222016
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.step;
21
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;
39
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;
48
49 public class ViewsPersistComponentsStepTest extends BaseStepTest {
50
51   private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
52
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";
64
65   @Rule
66   public DbTester dbTester = DbTester.create(System2.INSTANCE);
67   @Rule
68   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
69   @Rule
70   public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder);
71
72   System2 system2 = mock(System2.class);
73
74   DbClient dbClient = dbTester.getDbClient();
75
76   Date now;
77
78   ComponentDbTester componentDbTester = new ComponentDbTester(dbTester);
79
80   PersistComponentsStep underTest;
81
82   @Before
83   public void setup() throws Exception {
84     now = DATE_FORMAT.parse("2015-06-02");
85     when(system2.now()).thenReturn(now.getTime());
86
87     underTest = new PersistComponentsStep(dbClient, treeRootHolder, dbIdsRepository, system2);
88   }
89
90   @Override
91   protected ComputationStep step() {
92     return underTest;
93   }
94
95   @Test
96   public void persist_empty_view() {
97     treeRootHolder.setRoot(createViewBuilder().build());
98
99     underTest.execute();
100
101     assertRowsCountInTableProjects(1);
102
103     ComponentDto projectDto = getComponentFromDb(VIEW_KEY);
104     assertDtoIsView(projectDto);
105   }
106
107   @Test
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());
111
112     treeRootHolder.setRoot(createViewBuilder().build());
113
114     underTest.execute();
115
116     assertRowsCountInTableProjects(1);
117
118     assertDtoNotUpdated(VIEW_KEY);
119   }
120
121   @Test
122   public void persist_view_with_projectView() {
123     ComponentDto project = newProjectDto();
124     persistComponents(project);
125
126     treeRootHolder.setRoot(
127       createViewBuilder()
128         .addChildren(createProjectView1Builder(project).build())
129         .build());
130
131     underTest.execute();
132
133     assertRowsCountInTableProjects(3);
134
135     ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
136     assertDtoIsView(viewDto);
137
138     ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
139     assertDtoIsProjectView1(pv1Dto, viewDto, viewDto, project);
140   }
141
142   @Test
143   public void persist_empty_subview() {
144     treeRootHolder.setRoot(
145       createViewBuilder()
146         .addChildren(
147           createSubView1Builder().build())
148         .build());
149
150     underTest.execute();
151
152     assertRowsCountInTableProjects(2);
153
154     ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
155     assertDtoIsView(viewDto);
156
157     ComponentDto sv1Dto = getComponentFromDb(SUBVIEW_1_KEY);
158     assertDtoIsSubView1(viewDto, sv1Dto);
159   }
160
161   @Test
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));
166
167     treeRootHolder.setRoot(
168       createViewBuilder()
169         .addChildren(
170           createSubView1Builder().build())
171         .build());
172
173     underTest.execute();
174
175     assertRowsCountInTableProjects(2);
176
177     assertDtoNotUpdated(VIEW_KEY);
178     assertDtoNotUpdated(SUBVIEW_1_KEY);
179   }
180
181   @Test
182   public void persist_empty_subview_under_existing_view() {
183     persistComponents(newViewDto());
184
185     treeRootHolder.setRoot(
186       createViewBuilder()
187         .addChildren(
188           createSubView1Builder().build())
189         .build());
190
191     underTest.execute();
192
193     assertRowsCountInTableProjects(2);
194
195     assertDtoNotUpdated(VIEW_KEY);
196     assertDtoIsSubView1(getComponentFromDb(VIEW_KEY), getComponentFromDb(SUBVIEW_1_KEY));
197   }
198
199   @Test
200   public void persist_project_view_under_subview() {
201     ComponentDto project = newProjectDto();
202     persistComponents(project);
203
204     treeRootHolder.setRoot(
205       createViewBuilder()
206         .addChildren(
207           createSubView1Builder()
208             .addChildren(
209               createProjectView1Builder(project).build())
210             .build())
211         .build());
212
213     underTest.execute();
214
215     assertRowsCountInTableProjects(4);
216
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);
223   }
224
225   @Test
226   public void update_view_name_and_longName() {
227     ComponentDto viewDto = newViewDto().setLongName("another long name").setCreatedAt(now);
228     persistComponents(viewDto);
229
230     treeRootHolder.setRoot(createViewBuilder().build());
231
232     underTest.execute();
233
234     assertRowsCountInTableProjects(1);
235
236     ComponentDto newViewDto = getComponentFromDb(VIEW_KEY);
237     assertDtoIsView(newViewDto);
238   }
239
240   @Test
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)
247       .setName("Old name")
248       .setCreatedAt(now);
249     persistComponents(projectView);
250
251     treeRootHolder.setRoot(
252       createViewBuilder()
253         .addChildren(createProjectView1Builder(project).build())
254         .build());
255
256     underTest.execute();
257
258     assertRowsCountInTableProjects(3);
259
260     ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
261     assertDtoIsProjectView1(pv1Dto, view, view, project);
262   }
263
264   @Test
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);
270
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)
274       .setCreatedAt(now);
275     persistComponents(projectView);
276
277     treeRootHolder.setRoot(
278       createViewBuilder()
279         // Project view in the View is linked to the first project2
280         .addChildren(createProjectView1Builder(project2).build())
281         .build());
282
283     underTest.execute();
284
285     ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
286     // Project view should now be linked to project2
287     assertDtoIsProjectView1(pv1Dto, view, view, project2);
288   }
289
290   private static ViewsComponent.Builder createViewBuilder() {
291     return builder(VIEW, VIEW_KEY).setUuid(VIEW_UUID).setName(VIEW_NAME).setDescription(VIEW_DESCRIPTION);
292   }
293
294   private ViewsComponent.Builder createSubView1Builder() {
295     return builder(SUBVIEW, SUBVIEW_1_KEY).setUuid(SUBVIEW_1_UUID).setName(SUBVIEW_1_NAME).setDescription(SUBVIEW_1_DESCRIPTION);
296   }
297
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()));
304   }
305
306   private void persistComponents(ComponentDto... componentDtos) {
307     componentDbTester.insertComponents(componentDtos);
308   }
309
310   private ComponentDto getComponentFromDb(String componentKey) {
311     return dbClient.componentDao().selectByKey(dbTester.getSession(), componentKey).get();
312   }
313
314   private void assertRowsCountInTableProjects(int rowCount) {
315     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(rowCount);
316   }
317
318   private void assertDtoNotUpdated(String componentKey) {
319     assertThat(getComponentFromDb(componentKey).getCreatedAt()).isNotEqualTo(now);
320   }
321
322   private static ComponentDto newViewDto() {
323     return ComponentTesting.newView(VIEW_UUID).setKey(VIEW_KEY).setName(VIEW_NAME);
324   }
325
326   /**
327    * Assertions to verify the DTO created from {@link #createViewBuilder()}
328    */
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);
343   }
344
345   /**
346    * Assertions to verify the DTO created from {@link #createProjectView1Builder(ComponentDto)}
347    */
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);
362   }
363
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);
378   }
379
380 }