]> source.dussan.org Git - sonarqube.git/blob
a21e9b2955ff52d49f94f224c1ea10a98780b145
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
6  * SonarQube 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  * SonarQube 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
21 package org.sonar.server.computation.step;
22
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.ClassRule;
26 import org.junit.Test;
27 import org.sonar.api.utils.DateUtils;
28 import org.sonar.core.component.ComponentDto;
29 import org.sonar.core.component.SnapshotDto;
30 import org.sonar.core.computation.db.AnalysisReportDto;
31 import org.sonar.core.computation.db.AnalysisReportDto.Status;
32 import org.sonar.core.computation.dbcleaner.DbCleanerConstants;
33 import org.sonar.core.computation.dbcleaner.ProjectCleaner;
34 import org.sonar.core.persistence.DbSession;
35 import org.sonar.core.persistence.MyBatis;
36 import org.sonar.core.properties.PropertyDto;
37 import org.sonar.server.component.ComponentTesting;
38 import org.sonar.server.component.SnapshotTesting;
39 import org.sonar.server.computation.ComputationContext;
40 import org.sonar.server.db.DbClient;
41 import org.sonar.server.tester.ServerTester;
42
43 import java.util.Date;
44
45 import static org.assertj.core.api.Assertions.assertThat;
46
47 public class PurgeDatastoresStepMediumTest {
48
49   @ClassRule
50   public static ServerTester tester = new ServerTester();
51
52   private PurgeDatastoresStep sut;
53   private DbClient dbClient;
54   private DbSession dbSession;
55   private ProjectCleaner purgeTask;
56
57   @Before
58   public void before() throws Exception {
59     this.dbClient = tester.get(DbClient.class);
60     this.dbSession = dbClient.openSession(false);
61
62     this.purgeTask = tester.get(ProjectCleaner.class);
63
64     this.sut = new PurgeDatastoresStep(dbClient, purgeTask);
65   }
66
67   @After
68   public void after() throws Exception {
69     MyBatis.closeQuietly(dbSession);
70   }
71
72   @Test
73   public void use_global_settings_when_no_other_specified() throws Exception {
74     // ARRANGE
75     Date now = new Date();
76     Date aWeekAgo = DateUtils.addDays(now, -7);
77
78     ComponentDto project = ComponentTesting.newProjectDto()
79       .setId(1L)
80       .setKey("123")
81       .setCreatedAt(aWeekAgo)
82       .setUpdatedAt(aWeekAgo);
83     dbClient.componentDao().insert(dbSession, project);
84
85     SnapshotDto snapshot = SnapshotTesting.createForProject(project)
86       .setCreatedAt(aWeekAgo)
87       .setUpdatedAt(aWeekAgo);
88     dbClient.snapshotDao().insert(dbSession, snapshot);
89
90     AnalysisReportDto report = AnalysisReportDto.newForTests(1L)
91       .setProjectKey("123")
92       .setSnapshotId(snapshot.getId())
93       .setStatus(Status.PENDING);
94     dbClient.analysisReportDao().insert(dbSession, report);
95
96     dbClient.propertiesDao().setProperty(new PropertyDto().setKey(DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS).setValue("52"));
97     dbSession.commit();
98     ComputationContext context = new ComputationContext(report, project);
99
100     // ACT
101     sut.execute(context);
102
103     // ASSERT
104     assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshot.getId())).isNotNull();
105   }
106
107   @Test
108   public void use_project_settings_if_specified() throws Exception {
109     // ARRANGE
110     Date now = new Date();
111     Date twoWeeksAgo = DateUtils.addDays(now, -2 * 7);
112     Date aLongTimeAgo = DateUtils.addDays(now, -365 * 7);
113
114     ComponentDto project = ComponentTesting.newProjectDto()
115       .setKey("123")
116       .setCreatedAt(aLongTimeAgo)
117       .setUpdatedAt(aLongTimeAgo);
118     dbClient.componentDao().insert(dbSession, project);
119
120     SnapshotDto snapshot = SnapshotTesting.createForProject(project)
121       .setCreatedAt(twoWeeksAgo)
122       .setUpdatedAt(twoWeeksAgo)
123       .setStatus("P")
124       .setLast(true);
125
126     dbClient.snapshotDao().insert(dbSession, snapshot);
127
128     AnalysisReportDto report = new AnalysisReportDto()
129       .setProjectKey("123")
130       .setSnapshotId(snapshot.getId())
131       .setStatus(Status.PENDING);
132     dbClient.analysisReportDao().insert(dbSession, report);
133
134     dbClient.propertiesDao().setProperty(new PropertyDto().setKey(DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS).setValue("4"));
135     dbClient.propertiesDao().setProperty(new PropertyDto().setKey(DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS).setValue("1").setResourceId(project.getId()));
136     dbSession.commit();
137     ComputationContext context = new ComputationContext(report, project);
138
139     // ACT
140     sut.execute(context);
141     dbSession.commit();
142
143     // ASSERT
144     assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshot.getId())).isNull();
145   }
146 }