3 * Copyright (C) 2009-2020 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.ce.task.projectanalysis.purge;
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;
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;
58 @RunWith(DataProviderRunner.class)
59 public class PurgeDatastoresStepTest extends BaseStepTest {
61 private static final String PROJECT_KEY = "PROJECT_KEY";
62 private static final String PROJECT_UUID = "UUID-1234";
65 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
67 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
69 private ProjectCleaner projectCleaner = mock(ProjectCleaner.class);
70 private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.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, treeRootHolder,
74 settingsRepository, disabledComponentsHolder, analysisMetadataHolder);
77 public void before() {
78 analysisMetadataHolder.setProject(new Project("uuid", "key", "name", null, Collections.emptyList()));
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();
85 verify_call_purge_method_of_the_purge_task(project);
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();
92 verify_call_purge_method_of_the_purge_task(project);
96 public static Object[][] nonRootProjectComponentTypes() {
97 return dataproviderFromComponentTypeValues(input -> 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(input -> input.isViewsType() && input != Component.Type.VIEW);
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();
118 verify_do_not_call_purge_method_of_the_purge_task(component);
121 private void verify_do_not_call_purge_method_of_the_purge_task(Component component) {
122 treeRootHolder.setRoot(component);
124 underTest.execute(new TestComputationStepContext());
126 verifyNoMoreInteractions(projectCleaner);
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());
133 underTest.execute(new TestComputationStepContext());
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);
140 private static Object[][] dataproviderFromComponentTypeValues(Predicate<Component.Type> predicate) {
141 return Arrays.stream(Component.Type.values())
143 .map(WrapInSingleElementArray.INSTANCE)
144 .toArray(Object[][]::new);
148 protected ComputationStep step() {