]> source.dussan.org Git - sonarqube.git/blob
c55beae60e7fa385be2f35f2ecf4f0c022f4938b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.step;
21
22 import com.google.common.collect.ImmutableList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import javax.annotation.Nullable;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.sonar.api.measures.CoreMetrics;
35 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
36 import org.sonar.ce.task.projectanalysis.component.Component;
37 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
38 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
39 import org.sonar.ce.task.projectanalysis.measure.Measure;
40 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
41 import org.sonar.ce.task.projectanalysis.metric.Metric;
42 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
43 import org.sonar.ce.task.projectanalysis.qualityprofile.MutableQProfileStatusRepository;
44 import org.sonar.ce.task.projectanalysis.qualityprofile.RegisterQualityProfileStatusStep;
45 import org.sonar.ce.task.step.TestComputationStepContext;
46 import org.sonar.server.qualityprofile.QPMeasureData;
47 import org.sonar.server.qualityprofile.QualityProfile;
48
49 import static org.mockito.ArgumentMatchers.eq;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.verify;
52 import static org.mockito.Mockito.verifyNoMoreInteractions;
53 import static org.mockito.Mockito.when;
54 import static org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository.Status.ADDED;
55 import static org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository.Status.REMOVED;
56 import static org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository.Status.UNCHANGED;
57 import static org.sonar.ce.task.projectanalysis.qualityprofile.QProfileStatusRepository.Status.UPDATED;
58
59
60 public class RegisterQualityProfileStatusStepTest {
61
62   private static final String QP_NAME_1 = "qp_1";
63   private static final String QP_NAME_2 = "qp_2";
64   private static final String LANGUAGE_KEY_1 = "language_key1";
65   private static final String LANGUAGE_KEY_2 = "language_key_2";
66
67   @Rule
68   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
69
70   private MetricRepository metricRepository = mock(MetricRepository.class);
71   private MeasureRepository measureRepository = mock(MeasureRepository.class);
72   private MutableQProfileStatusRepository qProfileStatusRepository = mock(MutableQProfileStatusRepository.class);
73   private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
74   private Metric qualityProfileMetric = mock(Metric.class);
75
76   private RegisterQualityProfileStatusStep underTest = new RegisterQualityProfileStatusStep(treeRootHolder, measureRepository, metricRepository, qProfileStatusRepository, analysisMetadataHolder);
77
78   @Before
79   public void setUp() {
80     when(metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY)).thenReturn(qualityProfileMetric);
81     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("uuid").setKey("key").build());
82   }
83
84   @Test
85   public void register_nothing_if_no_base_measure() {
86     when(measureRepository.getBaseMeasure(treeRootHolder.getRoot(), qualityProfileMetric)).thenReturn(Optional.empty());
87
88     underTest.execute(new TestComputationStepContext());
89
90     verifyNoMoreInteractions(qProfileStatusRepository);
91   }
92
93   @Test
94   public void register_nothing_if_no_base_and_quality_profile_measure_is_empty() {
95     mockBaseQPMeasures(treeRootHolder.getRoot(), null);
96
97     underTest.execute(new TestComputationStepContext());
98
99     verifyNoMoreInteractions(qProfileStatusRepository);
100   }
101
102   @Test
103   public void register_removed_profile() {
104     QualityProfile qp = qp(QP_NAME_1, LANGUAGE_KEY_1, new Date());
105
106     mockBaseQPMeasures(treeRootHolder.getRoot(), new QualityProfile[] {qp});
107
108     underTest.execute(new TestComputationStepContext());
109
110     verify(qProfileStatusRepository).register(eq(qp.getQpKey()), eq(REMOVED));
111     verifyNoMoreInteractions(qProfileStatusRepository);
112   }
113
114   @Test
115   public void register_added_profile() {
116     QualityProfile qp1 = qp(QP_NAME_1, LANGUAGE_KEY_1, new Date(1000L));
117     QualityProfile qp2 = qp(QP_NAME_2, LANGUAGE_KEY_2, new Date(1000L));
118
119     mockBaseQPMeasures(treeRootHolder.getRoot(), arrayOf(qp1));
120     mockRawQProfiles(ImmutableList.of(qp1, qp2));
121     underTest.execute(new TestComputationStepContext());
122
123     verify(qProfileStatusRepository).register(eq(qp1.getQpKey()), eq(UNCHANGED));
124     verify(qProfileStatusRepository).register(eq(qp2.getQpKey()), eq(ADDED));
125     verifyNoMoreInteractions(qProfileStatusRepository);
126   }
127
128   @Test
129   public void register_updated_profile() {
130     QualityProfile qp1 = qp(QP_NAME_1, LANGUAGE_KEY_1, new Date(1000L));
131     QualityProfile qp2 = qp(QP_NAME_1, LANGUAGE_KEY_1, new Date(1200L));
132
133     mockBaseQPMeasures(treeRootHolder.getRoot(), arrayOf(qp1));
134     mockRawQProfiles(ImmutableList.of(qp2));
135     underTest.execute(new TestComputationStepContext());
136
137     verify(qProfileStatusRepository).register(eq(qp2.getQpKey()), eq(UPDATED));
138     verifyNoMoreInteractions(qProfileStatusRepository);
139   }
140
141   @Test
142   public void register_unchanged_profile() {
143     QualityProfile qp1 = qp(QP_NAME_1, LANGUAGE_KEY_1, new Date(1000L));
144
145     mockBaseQPMeasures(treeRootHolder.getRoot(), arrayOf(qp1));
146     mockRawQProfiles(ImmutableList.of(qp1));
147     underTest.execute(new TestComputationStepContext());
148
149     verify(qProfileStatusRepository).register(eq(qp1.getQpKey()), eq(UNCHANGED));
150     verifyNoMoreInteractions(qProfileStatusRepository);
151   }
152
153   private void mockBaseQPMeasures(Component component, @Nullable QualityProfile[] previous) {
154     when(measureRepository.getBaseMeasure(component, qualityProfileMetric)).thenReturn(Optional.of(newMeasure(previous)));
155   }
156
157   private void mockRawQProfiles(@Nullable List<QualityProfile> previous) {
158     Map<String, QualityProfile> qpByLanguages = previous.stream().collect(Collectors.toMap(QualityProfile::getLanguageKey, q -> q));
159     when(analysisMetadataHolder.getQProfilesByLanguage()).thenReturn(qpByLanguages);
160   }
161
162   private static QualityProfile qp(String qpName, String languageKey, Date date) {
163     return new QualityProfile(qpName + "-" + languageKey, qpName, languageKey, date);
164   }
165
166   private static QualityProfile[] arrayOf(QualityProfile... qps) {
167     return qps;
168   }
169
170   private static Measure newMeasure(@Nullable QualityProfile... qps) {
171     return Measure.newMeasureBuilder().create(toJson(qps));
172   }
173
174   private static String toJson(@Nullable QualityProfile... qps) {
175     List<QualityProfile> qualityProfiles = qps == null ? Collections.emptyList() : Arrays.asList(qps);
176     return QPMeasureData.toJson(new QPMeasureData(qualityProfiles));
177   }
178
179 }