3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.purge.IdUuidPair;
36 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
37 import org.sonar.server.computation.task.projectanalysis.component.Component;
38 import org.sonar.server.computation.task.projectanalysis.component.MutableDbIdsRepositoryRule;
39 import org.sonar.server.computation.task.projectanalysis.component.MutableDisabledComponentsHolder;
40 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
41 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
42 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
43 import org.sonar.server.computation.dbcleaner.ProjectCleaner;
44 import org.sonar.server.computation.task.step.ComputationStep;
45 import org.sonar.server.util.WrapInSingleElementArray;
47 import static java.util.Arrays.asList;
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.mockito.Matchers.anyList;
50 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
51 import static org.mockito.Mockito.any;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.verify;
54 import static org.mockito.Mockito.verifyNoMoreInteractions;
55 import static org.mockito.Mockito.when;
57 @RunWith(DataProviderRunner.class)
58 public class PurgeDatastoresStepTest extends BaseStepTest {
60 private static final String PROJECT_KEY = "PROJECT_KEY";
61 private static final long PROJECT_ID = 123L;
62 private static final String PROJECT_UUID = "UUID-1234";
65 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
67 public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.standalone();
69 private ProjectCleaner projectCleaner = mock(ProjectCleaner.class);
70 private SettingsRepository settingsRepository = mock(SettingsRepository.class);
71 private MutableDisabledComponentsHolder disabledComponentsHolder = mock(MutableDisabledComponentsHolder.class, RETURNS_DEEP_STUBS);
73 private PurgeDatastoresStep underTest = new PurgeDatastoresStep(mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS), projectCleaner, dbIdsRepository, treeRootHolder, settingsRepository, disabledComponentsHolder);
76 public void call_purge_method_of_the_purge_task_for_project() {
77 Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build();
79 verify_call_purge_method_of_the_purge_task(project);
83 public void call_purge_method_of_the_purge_task_for_view() {
84 Component project = ViewsComponent.builder(Component.Type.VIEW, PROJECT_KEY).setUuid(PROJECT_UUID).build();
86 verify_call_purge_method_of_the_purge_task(project);
90 public static Object[][] nonRootProjectComponentTypes() {
91 return dataproviderFromComponentTypeValues(new Predicate<Component.Type>() {
93 public boolean apply(Component.Type input) {
94 return input.isReportType() && input != Component.Type.PROJECT;
100 @UseDataProvider("nonRootProjectComponentTypes")
101 public void do_not_call_purge_method_of_the_purge_task_for_other_report_components(Component.Type type) {
102 Component component = ReportComponent.builder(type, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build();
104 verify_do_not_call_purge_method_of_the_purge_task(component);
108 public static Object[][] nonRootViewsComponentTypes() {
109 return dataproviderFromComponentTypeValues(new Predicate<Component.Type>() {
111 public boolean apply(Component.Type input) {
112 return input.isViewsType() && input != Component.Type.VIEW;
118 @UseDataProvider("nonRootViewsComponentTypes")
119 public void do_not_call_purge_method_of_the_purge_task_for_other_views_components(Component.Type type) {
120 Component component = ViewsComponent.builder(type, PROJECT_KEY).setUuid(PROJECT_UUID).build();
122 verify_do_not_call_purge_method_of_the_purge_task(component);
125 private void verify_do_not_call_purge_method_of_the_purge_task(Component component) {
126 treeRootHolder.setRoot(component);
130 verifyNoMoreInteractions(projectCleaner);
133 private void verify_call_purge_method_of_the_purge_task(Component project) {
134 treeRootHolder.setRoot(project);
135 when(settingsRepository.getSettings(project)).thenReturn(new Settings());
136 dbIdsRepository.setComponentId(project, PROJECT_ID);
140 ArgumentCaptor<IdUuidPair> argumentCaptor = ArgumentCaptor.forClass(IdUuidPair.class);
141 verify(projectCleaner).purge(any(DbSession.class), argumentCaptor.capture(), any(Settings.class), anyList());
142 assertThat(argumentCaptor.getValue().getId()).isEqualTo(PROJECT_ID);
143 assertThat(argumentCaptor.getValue().getUuid()).isEqualTo(PROJECT_UUID);
146 private static Object[][] dataproviderFromComponentTypeValues(Predicate<Component.Type> predicate) {
147 return FluentIterable.from(asList(Component.Type.values()))
149 .transform(WrapInSingleElementArray.INSTANCE)
150 .toArray(Object[].class);
154 protected ComputationStep step() {