]> source.dussan.org Git - sonarqube.git/blob
4c19104084298415d4a1e0bedcf0a212e2daabd8
[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.task.projectanalysis.step;
21
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;
46
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;
56
57 @RunWith(DataProviderRunner.class)
58 public class PurgeDatastoresStepTest extends BaseStepTest {
59
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";
63
64   @Rule
65   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
66   @Rule
67   public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.standalone();
68
69   private ProjectCleaner projectCleaner = mock(ProjectCleaner.class);
70   private SettingsRepository settingsRepository = mock(SettingsRepository.class);
71   private MutableDisabledComponentsHolder disabledComponentsHolder = mock(MutableDisabledComponentsHolder.class, RETURNS_DEEP_STUBS);
72
73   private PurgeDatastoresStep underTest = new PurgeDatastoresStep(mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS), projectCleaner, dbIdsRepository, treeRootHolder, settingsRepository, disabledComponentsHolder);
74
75   @Test
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();
78
79     verify_call_purge_method_of_the_purge_task(project);
80   }
81
82   @Test
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();
85
86     verify_call_purge_method_of_the_purge_task(project);
87   }
88
89   @DataProvider
90   public static Object[][] nonRootProjectComponentTypes() {
91     return dataproviderFromComponentTypeValues(new Predicate<Component.Type>() {
92       @Override
93       public boolean apply(Component.Type input) {
94         return input.isReportType() && input != Component.Type.PROJECT;
95       }
96     });
97   }
98
99   @Test
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();
103
104     verify_do_not_call_purge_method_of_the_purge_task(component);
105   }
106
107   @DataProvider
108   public static Object[][] nonRootViewsComponentTypes() {
109     return dataproviderFromComponentTypeValues(new Predicate<Component.Type>() {
110       @Override
111       public boolean apply(Component.Type input) {
112         return input.isViewsType() && input != Component.Type.VIEW;
113       }
114     });
115   }
116
117   @Test
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();
121
122     verify_do_not_call_purge_method_of_the_purge_task(component);
123   }
124
125   private void verify_do_not_call_purge_method_of_the_purge_task(Component component) {
126     treeRootHolder.setRoot(component);
127
128     underTest.execute();
129
130     verifyNoMoreInteractions(projectCleaner);
131   }
132
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);
137
138     underTest.execute();
139
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);
144   }
145
146   private static Object[][] dataproviderFromComponentTypeValues(Predicate<Component.Type> predicate) {
147     return FluentIterable.from(asList(Component.Type.values()))
148         .filter(predicate)
149         .transform(WrapInSingleElementArray.INSTANCE)
150         .toArray(Object[].class);
151   }
152
153   @Override
154   protected ComputationStep step() {
155     return underTest;
156   }
157
158 }