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