]> source.dussan.org Git - sonarqube.git/blob
638b8f1aeda15d64565090c396782e708ff75899
[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.util.List;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.DateUtils;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.component.SnapshotDto;
32 import org.sonar.db.component.SnapshotQuery;
33 import org.sonar.server.computation.analysis.AnalysisMetadataHolderRule;
34 import org.sonar.server.computation.batch.TreeRootHolderRule;
35 import org.sonar.server.computation.component.Component;
36 import org.sonar.server.computation.component.ViewsComponent;
37 import org.sonar.server.computation.period.Period;
38 import org.sonar.server.computation.period.PeriodsHolderRule;
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.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_DATE;
44 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
45 import static org.sonar.db.component.ComponentTesting.newProjectDto;
46 import static org.sonar.db.component.ComponentTesting.newSubView;
47 import static org.sonar.db.component.ComponentTesting.newView;
48 import static org.sonar.server.computation.component.Component.Type.PROJECT_VIEW;
49 import static org.sonar.server.computation.component.Component.Type.SUBVIEW;
50 import static org.sonar.server.computation.component.Component.Type.VIEW;
51
52 public class ViewsPersistAnalysisStepTest extends BaseStepTest {
53
54   private static final String ANALYSIS_UUID = "U1";
55
56   @Rule
57   public DbTester dbTester = DbTester.create(System2.INSTANCE);
58
59   @Rule
60   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
61
62   @Rule
63   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
64
65   @Rule
66   public PeriodsHolderRule periodsHolder = new PeriodsHolderRule();
67
68   System2 system2 = mock(System2.class);
69
70   DbClient dbClient = dbTester.getDbClient();
71
72   long analysisDate;
73
74   long now;
75
76   PersistAnalysisStep underTest;
77
78   @Before
79   public void setup() {
80     analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
81     analysisMetadataHolder.setUuid(ANALYSIS_UUID);
82     analysisMetadataHolder.setAnalysisDate(analysisDate);
83
84     now = DateUtils.parseDateQuietly("2015-06-02").getTime();
85
86     when(system2.now()).thenReturn(now);
87
88     underTest = new PersistAnalysisStep(system2, dbClient, treeRootHolder, analysisMetadataHolder, periodsHolder);
89
90     // initialize PeriodHolder to empty by default
91     periodsHolder.setPeriods();
92   }
93
94   @Override
95   protected ComputationStep step() {
96     return underTest;
97   }
98
99   @Test
100   public void persist_analysis() {
101     ComponentDto viewDto = save(newView("UUID_VIEW").setKey("KEY_VIEW"));
102     ComponentDto subViewDto = save(newSubView(viewDto, "UUID_SUBVIEW", "KEY_SUBVIEW"));
103     ComponentDto projectDto = save(newProjectDto("proj"));
104     ComponentDto projectViewDto = save(newProjectCopy("UUID_PROJECT_COPY", projectDto, subViewDto).setKey("KEY_PROJECT_COPY"));
105     dbTester.getSession().commit();
106
107     Component projectView = ViewsComponent.builder(PROJECT_VIEW, "KEY_PROJECT_COPY").setUuid("UUID_PROJECT_COPY").build();
108     Component subView = ViewsComponent.builder(SUBVIEW, "KEY_SUBVIEW").setUuid("UUID_SUBVIEW").addChildren(projectView).build();
109     Component view = ViewsComponent.builder(VIEW, "KEY_VIEW").setUuid("UUID_VIEW").addChildren(subView).build();
110     treeRootHolder.setRoot(view);
111
112     underTest.execute();
113
114     assertThat(dbTester.countRowsOfTable("snapshots")).isEqualTo(1);
115
116     SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.uuid());
117     assertThat(viewSnapshot.getComponentUuid()).isEqualTo(view.getUuid());
118     assertThat(viewSnapshot.getVersion()).isNull();
119     assertThat(viewSnapshot.getLast()).isFalse();
120     assertThat(viewSnapshot.getStatus()).isEqualTo("U");
121     assertThat(viewSnapshot.getCreatedAt()).isEqualTo(analysisDate);
122     assertThat(viewSnapshot.getBuildDate()).isEqualTo(now);
123   }
124
125   @Test
126   public void persist_snapshots_with_periods() {
127     ComponentDto viewDto = save(newView("UUID_VIEW").setKey("KEY_VIEW"));
128     ComponentDto subViewDto = save(newSubView(viewDto, "UUID_SUBVIEW", "KEY_SUBVIEW"));
129     dbTester.getSession().commit();
130
131     Component subView = ViewsComponent.builder(SUBVIEW, "KEY_SUBVIEW").setUuid("UUID_SUBVIEW").build();
132     Component view = ViewsComponent.builder(VIEW, "KEY_VIEW").setUuid("UUID_VIEW").addChildren(subView).build();
133     treeRootHolder.setRoot(view);
134
135     periodsHolder.setPeriods(new Period(1, TIMEMACHINE_MODE_DATE, "2015-01-01", analysisDate, "u1"));
136
137     underTest.execute();
138
139     SnapshotDto viewSnapshot = getUnprocessedSnapshot(viewDto.uuid());
140     assertThat(viewSnapshot.getPeriodMode(1)).isEqualTo(TIMEMACHINE_MODE_DATE);
141     assertThat(viewSnapshot.getPeriodDate(1)).isEqualTo(analysisDate);
142     assertThat(viewSnapshot.getPeriodModeParameter(1)).isNotNull();
143   }
144
145   private ComponentDto save(ComponentDto componentDto) {
146     dbClient.componentDao().insert(dbTester.getSession(), componentDto);
147     return componentDto;
148   }
149
150   private SnapshotDto getUnprocessedSnapshot(String componentUuid) {
151     List<SnapshotDto> projectSnapshots = dbClient.snapshotDao().selectAnalysesByQuery(dbTester.getSession(),
152       new SnapshotQuery().setComponentUuid(componentUuid).setIsLast(false).setStatus(SnapshotDto.STATUS_UNPROCESSED));
153     assertThat(projectSnapshots).hasSize(1);
154     return projectSnapshots.get(0);
155   }
156
157 }