]> source.dussan.org Git - sonarqube.git/blob
3dde69b489edb59448541460d948ada71310d358
[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.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;
38
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;
46
47
48 public class ViewsPersistComponentsStepTest extends BaseStepTest {
49
50   private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
51
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;
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   PersistComponentsStep underTest;
79
80   @Before
81   public void setup() throws Exception {
82     dbTester.truncateTables();
83
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     persistComponentDto(createViewDto());
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     treeRootHolder.setRoot(
124         createViewBuilder()
125             .addChildren(createProjectView1Builder().build())
126             .build());
127
128     underTest.execute();
129
130     assertRowsCountInTableProjects(2);
131
132     ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
133     assertDtoIsView(viewDto);
134
135     ComponentDto pv1Dto = getComponentFromDb(PROJECT_VIEW_1_KEY);
136     assertDtoIsProjectView1(pv1Dto, viewDto, viewDto);
137   }
138
139   @Test
140   public void persist_empty_subview() {
141     treeRootHolder.setRoot(
142       createViewBuilder()
143         .addChildren(
144           createSubView1Builder().build())
145         .build());
146
147     underTest.execute();
148
149     assertRowsCountInTableProjects(2);
150
151     ComponentDto viewDto = getComponentFromDb(VIEW_KEY);
152     assertDtoIsView(viewDto);
153
154     ComponentDto sv1Dto = getComponentFromDb(SUBVIEW_1_KEY);
155     assertDtoIsSubView1(viewDto, sv1Dto);
156   }
157
158   @Test
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));
163
164     treeRootHolder.setRoot(
165       createViewBuilder()
166         .addChildren(
167           createSubView1Builder().build())
168         .build());
169
170     underTest.execute();
171
172     assertRowsCountInTableProjects(2);
173
174     assertDtoNotUpdated(VIEW_KEY);
175     assertDtoNotUpdated(SUBVIEW_1_KEY);
176   }
177
178   @Test
179   public void persist_empty_subview_under_existing_view() {
180     persistComponentDto(createViewDto());
181
182     treeRootHolder.setRoot(
183       createViewBuilder()
184         .addChildren(
185           createSubView1Builder().build())
186         .build());
187
188     underTest.execute();
189
190     assertRowsCountInTableProjects(2);
191
192     assertDtoNotUpdated(VIEW_KEY);
193     assertDtoIsSubView1(getComponentFromDb(VIEW_KEY), getComponentFromDb(SUBVIEW_1_KEY));
194   }
195
196   @Test
197   public void persist_project_view_under_subview() {
198     treeRootHolder.setRoot(
199         createViewBuilder()
200             .addChildren(
201                 createSubView1Builder()
202                     .addChildren(
203                         createProjectView1Builder().build())
204                     .build())
205             .build());
206
207     underTest.execute();
208
209     assertRowsCountInTableProjects(3);
210
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);
217   }
218
219   @Test
220   public void update_view_name_and_longName() {
221     ComponentDto viewDto = createViewDto().setLongName("another long name").setCreatedAt(now);
222     persistComponentDto(viewDto);
223
224     treeRootHolder.setRoot(createViewBuilder().build());
225
226     underTest.execute();
227
228     assertRowsCountInTableProjects(1);
229
230     ComponentDto newViewDto = getComponentFromDb(VIEW_KEY);
231     assertDtoIsView(newViewDto);
232   }
233
234   private static ViewsComponent.Builder createViewBuilder() {
235     return builder(VIEW, VIEW_KEY).setUuid(VIEW_UUID).setName(VIEW_NAME).setDescription(VIEW_DESCRIPTION);
236   }
237
238   private ViewsComponent.Builder createSubView1Builder() {
239     return builder(SUBVIEW, SUBVIEW_1_KEY).setUuid(SUBVIEW_1_UUID).setName(SUBVIEW_1_NAME).setDescription(SUBVIEW_1_DESCRIPTION);
240   }
241
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));
248   }
249
250   private void persistComponentDto(ComponentDto componentDto) {
251     dbClient.componentDao().insert(dbTester.getSession(), componentDto);
252     dbTester.getSession().commit();
253   }
254
255   private ComponentDto getComponentFromDb(String componentKey) {
256     return dbClient.componentDao().selectByKey(dbTester.getSession(), componentKey).get();
257   }
258
259   private void assertRowsCountInTableProjects(int rowCount) {
260     assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(rowCount);
261   }
262
263   private void assertDtoNotUpdated(String componentKey) {
264     assertThat(getComponentFromDb(componentKey).getCreatedAt()).isNotEqualTo(now);
265   }
266
267   private ComponentDto createViewDto() {
268     return ComponentTesting.newView(VIEW_UUID).setKey(VIEW_KEY).setName(VIEW_NAME);
269   }
270
271   /**
272    * Assertions to verify the DTO created from {@link #createViewBuilder()}
273    */
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);
288   }
289
290   /**
291    * Assertions to verify the DTO created from {@link #createProjectView1Builder()}
292    */
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);
307   }
308
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);
323   }
324
325 }