3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import com.google.common.base.Predicate;
23 import com.google.common.collect.FluentIterable;
24 import com.tngtech.java.junit.dataprovider.DataProvider;
25 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
26 import com.tngtech.java.junit.dataprovider.UseDataProvider;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mockito;
32 import org.sonar.api.config.Settings;
33 import org.sonar.api.config.MapSettings;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.purge.IdUuidPair;
37 import org.sonar.server.computation.dbcleaner.ProjectCleaner;
38 import org.sonar.server.computation.task.projectanalysis.component.Component;
39 import org.sonar.server.computation.task.projectanalysis.component.MutableDbIdsRepositoryRule;
40 import org.sonar.server.computation.task.projectanalysis.component.MutableDisabledComponentsHolder;
41 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
42 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
43 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
44 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
45 import org.sonar.server.computation.task.step.ComputationStep;
46 import org.sonar.server.util.WrapInSingleElementArray;
48 import static java.util.Arrays.asList;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.mockito.Matchers.anyList;
51 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
52 import static org.mockito.Mockito.any;
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.verify;
55 import static org.mockito.Mockito.verifyNoMoreInteractions;
56 import static org.mockito.Mockito.when;
58 @RunWith(DataProviderRunner.class)
59 public class PurgeDatastoresStepTest extends BaseStepTest {
61 private static final String PROJECT_KEY = "PROJECT_KEY";
62 private static final long PROJECT_ID = 123L;
63 private static final String PROJECT_UUID = "UUID-1234";
66 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
68 public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.standalone();
70 private ProjectCleaner projectCleaner = mock(ProjectCleaner.class);
71 private SettingsRepository settingsRepository = mock(SettingsRepository.class);
72 private MutableDisabledComponentsHolder disabledComponentsHolder = mock(MutableDisabledComponentsHolder.class, RETURNS_DEEP_STUBS);
74 private PurgeDatastoresStep underTest = new PurgeDatastoresStep(mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS), projectCleaner, dbIdsRepository, treeRootHolder, settingsRepository, disabledComponentsHolder);
77 public void call_purge_method_of_the_purge_task_for_project() {
78 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build();
80 verify_call_purge_method_of_the_purge_task(project);
84 public void call_purge_method_of_the_purge_task_for_view() {
85 Component project = ViewsComponent.builder(Component.Type.VIEW, PROJECT_KEY).setUuid(PROJECT_UUID).build();
87 verify_call_purge_method_of_the_purge_task(project);
91 public static Object[][] nonRootProjectComponentTypes() {
92 return dataproviderFromComponentTypeValues(new Predicate<Component.Type>() {
94 public boolean apply(Component.Type input) {
95 return input.isReportType() && input != Component.Type.PROJECT;
101 @UseDataProvider("nonRootProjectComponentTypes")
102 public void do_not_call_purge_method_of_the_purge_task_for_other_report_components(Component.Type type) {
103 Component component = ReportComponent.builder(type, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build();
105 verify_do_not_call_purge_method_of_the_purge_task(component);
109 public static Object[][] nonRootViewsComponentTypes() {
110 return dataproviderFromComponentTypeValues(new Predicate<Component.Type>() {
112 public boolean apply(Component.Type input) {
113 return input.isViewsType() && input != Component.Type.VIEW;
119 @UseDataProvider("nonRootViewsComponentTypes")
120 public void do_not_call_purge_method_of_the_purge_task_for_other_views_components(Component.Type type) {
121 Component component = ViewsComponent.builder(type, PROJECT_KEY).setUuid(PROJECT_UUID).build();
123 verify_do_not_call_purge_method_of_the_purge_task(component);
126 private void verify_do_not_call_purge_method_of_the_purge_task(Component component) {
127 treeRootHolder.setRoot(component);
131 verifyNoMoreInteractions(projectCleaner);
134 private void verify_call_purge_method_of_the_purge_task(Component project) {
135 treeRootHolder.setRoot(project);
136 when(settingsRepository.getSettings(project)).thenReturn(new MapSettings());
137 dbIdsRepository.setComponentId(project, PROJECT_ID);
141 ArgumentCaptor<IdUuidPair> argumentCaptor = ArgumentCaptor.forClass(IdUuidPair.class);
142 verify(projectCleaner).purge(any(DbSession.class), argumentCaptor.capture(), any(Settings.class), anyList());
143 assertThat(argumentCaptor.getValue().getId()).isEqualTo(PROJECT_ID);
144 assertThat(argumentCaptor.getValue().getUuid()).isEqualTo(PROJECT_UUID);
147 private static Object[][] dataproviderFromComponentTypeValues(Predicate<Component.Type> predicate) {
148 return FluentIterable.from(asList(Component.Type.values()))
150 .transform(WrapInSingleElementArray.INSTANCE)
151 .toArray(Object[].class);
155 protected ComputationStep step() {