2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
21 package org.sonar.server.computation.step;
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;
43 import java.util.Date;
45 import static org.assertj.core.api.Assertions.assertThat;
47 public class PurgeDatastoresStepMediumTest {
50 public static ServerTester tester = new ServerTester();
52 private PurgeDatastoresStep sut;
53 private DbClient dbClient;
54 private DbSession dbSession;
55 private ProjectCleaner purgeTask;
58 public void before() throws Exception {
59 this.dbClient = tester.get(DbClient.class);
60 this.dbSession = dbClient.openSession(false);
62 this.purgeTask = tester.get(ProjectCleaner.class);
64 this.sut = new PurgeDatastoresStep(dbClient, purgeTask);
68 public void after() throws Exception {
69 MyBatis.closeQuietly(dbSession);
73 public void use_global_settings_when_no_other_specified() throws Exception {
75 Date now = new Date();
76 Date aWeekAgo = DateUtils.addDays(now, -7);
78 ComponentDto project = ComponentTesting.newProjectDto()
81 .setCreatedAt(aWeekAgo)
82 .setUpdatedAt(aWeekAgo);
83 dbClient.componentDao().insert(dbSession, project);
85 SnapshotDto snapshot = SnapshotTesting.createForProject(project)
86 .setCreatedAt(aWeekAgo)
87 .setUpdatedAt(aWeekAgo);
88 dbClient.snapshotDao().insert(dbSession, snapshot);
90 AnalysisReportDto report = AnalysisReportDto.newForTests(1L)
92 .setSnapshotId(snapshot.getId())
93 .setStatus(Status.PENDING);
94 dbClient.analysisReportDao().insert(dbSession, report);
96 dbClient.propertiesDao().setProperty(new PropertyDto().setKey(DbCleanerConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS).setValue("52"));
98 ComputationContext context = new ComputationContext(report, project);
101 sut.execute(context);
104 assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshot.getId())).isNotNull();
108 public void use_project_settings_if_specified() throws Exception {
110 Date now = new Date();
111 Date twoWeeksAgo = DateUtils.addDays(now, -2 * 7);
112 Date aLongTimeAgo = DateUtils.addDays(now, -365 * 7);
114 ComponentDto project = ComponentTesting.newProjectDto()
116 .setCreatedAt(aLongTimeAgo)
117 .setUpdatedAt(aLongTimeAgo);
118 dbClient.componentDao().insert(dbSession, project);
120 SnapshotDto snapshot = SnapshotTesting.createForProject(project)
121 .setCreatedAt(twoWeeksAgo)
122 .setUpdatedAt(twoWeeksAgo)
126 dbClient.snapshotDao().insert(dbSession, snapshot);
128 AnalysisReportDto report = new AnalysisReportDto()
129 .setProjectKey("123")
130 .setSnapshotId(snapshot.getId())
131 .setStatus(Status.PENDING);
132 dbClient.analysisReportDao().insert(dbSession, report);
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()));
137 ComputationContext context = new ComputationContext(report, project);
140 sut.execute(context);
144 assertThat(dbClient.snapshotDao().getNullableByKey(dbSession, snapshot.getId())).isNull();