]> source.dussan.org Git - sonarqube.git/blob
2a78a36a36865506be4c36c09ad24bde646ee9bb
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.ce.task.projectanalysis.purge;
21
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.function.Predicate;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mockito;
34 import org.sonar.api.config.internal.MapSettings;
35 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
36 import org.sonar.ce.task.projectanalysis.component.Component;
37 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
38 import org.sonar.ce.task.projectanalysis.component.MutableDisabledComponentsHolder;
39 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
40 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
41 import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
42 import org.sonar.ce.task.projectanalysis.step.BaseStepTest;
43 import org.sonar.ce.task.step.ComputationStep;
44 import org.sonar.ce.task.step.TestComputationStepContext;
45 import org.sonar.db.DbClient;
46 import org.sonar.server.project.Project;
47 import org.sonar.ce.task.projectanalysis.util.WrapInSingleElementArray;
48
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.mockito.ArgumentMatchers.any;
51 import static org.mockito.ArgumentMatchers.anyString;
52 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
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 String PROJECT_UUID = "UUID-1234";
63
64   @Rule
65   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
66   @Rule
67   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
68
69   private ProjectCleaner projectCleaner = mock(ProjectCleaner.class);
70   private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.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, treeRootHolder,
74     settingsRepository, disabledComponentsHolder, analysisMetadataHolder);
75
76   @Before
77   public void before() {
78     analysisMetadataHolder.setProject(new Project("uuid", "key", "name", null, Collections.emptyList()));
79   }
80
81   @Test
82   public void call_purge_method_of_the_purge_task_for_project() {
83     Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build();
84
85     verify_call_purge_method_of_the_purge_task(project);
86   }
87
88   @Test
89   public void call_purge_method_of_the_purge_task_for_view() {
90     Component project = ViewsComponent.builder(Component.Type.VIEW, PROJECT_KEY).setUuid(PROJECT_UUID).build();
91
92     verify_call_purge_method_of_the_purge_task(project);
93   }
94
95   @DataProvider
96   public static Object[][] nonRootProjectComponentTypes() {
97     return dataproviderFromComponentTypeValues(input -> input.isReportType() && input != Component.Type.PROJECT);
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(input -> input.isViewsType() && input != Component.Type.VIEW);
111   }
112
113   @Test
114   @UseDataProvider("nonRootViewsComponentTypes")
115   public void do_not_call_purge_method_of_the_purge_task_for_other_views_components(Component.Type type) {
116     Component component = ViewsComponent.builder(type, PROJECT_KEY).setUuid(PROJECT_UUID).build();
117
118     verify_do_not_call_purge_method_of_the_purge_task(component);
119   }
120
121   private void verify_do_not_call_purge_method_of_the_purge_task(Component component) {
122     treeRootHolder.setRoot(component);
123
124     underTest.execute(new TestComputationStepContext());
125
126     verifyNoMoreInteractions(projectCleaner);
127   }
128
129   private void verify_call_purge_method_of_the_purge_task(Component project) {
130     treeRootHolder.setRoot(project);
131     when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig());
132
133     underTest.execute(new TestComputationStepContext());
134
135     ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
136     verify(projectCleaner).purge(any(), argumentCaptor.capture(), anyString(), any(), any());
137     assertThat(argumentCaptor.getValue()).isEqualTo(PROJECT_UUID);
138   }
139
140   private static Object[][] dataproviderFromComponentTypeValues(Predicate<Component.Type> predicate) {
141     return Arrays.stream(Component.Type.values())
142       .filter(predicate)
143       .map(WrapInSingleElementArray.INSTANCE)
144       .toArray(Object[][]::new);
145   }
146
147   @Override
148   protected ComputationStep step() {
149     return underTest;
150   }
151
152 }