2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.computation.step;
23 import java.util.Arrays;
24 import java.util.Date;
25 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.sonar.api.utils.MessageException;
31 import org.sonar.server.computation.batch.TreeRootHolderRule;
32 import org.sonar.server.computation.component.Component;
33 import org.sonar.server.computation.component.ReportComponent;
34 import org.sonar.server.computation.measure.Measure;
35 import org.sonar.server.computation.measure.MeasureRepositoryRule;
36 import org.sonar.server.computation.metric.MetricRepositoryRule;
37 import org.sonar.server.computation.qualityprofile.QPMeasureData;
38 import org.sonar.server.computation.qualityprofile.QualityProfile;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.guava.api.Assertions.assertThat;
42 import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES;
43 import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES_KEY;
44 import static org.sonar.server.computation.component.Component.Type.MODULE;
45 import static org.sonar.server.computation.component.Component.Type.PROJECT;
46 import static org.sonar.server.computation.measure.Measure.newMeasureBuilder;
48 public class ComputeQProfileMeasureStepTest {
50 private static final String QP_NAME_1 = "qp1";
51 private static final String QP_NAME_2 = "qp1";
52 private static final String LANGUAGE_KEY_1 = "language_key1";
53 private static final String LANGUAGE_KEY_2 = "language_key2";
55 private static final String PROJECT_KEY = "PROJECT KEY";
56 private static final int PROJECT_REF = 1;
57 private static final int MODULE_REF = 11;
58 private static final int SUB_MODULE_REF = 111;
60 private static final Component MULTI_MODULE_PROJECT = ReportComponent.builder(PROJECT, PROJECT_REF).setKey(PROJECT_KEY)
62 ReportComponent.builder(MODULE, MODULE_REF)
64 ReportComponent.builder(MODULE, SUB_MODULE_REF).build()
70 public ExpectedException thrown = ExpectedException.none();
73 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
76 public MetricRepositoryRule metricRepository = new MetricRepositoryRule().add(QUALITY_PROFILES);
79 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
81 ComputeQProfileMeasureStep underTest;
84 public void setUp() throws Exception {
85 underTest = new ComputeQProfileMeasureStep(treeRootHolder, measureRepository, metricRepository);
89 public void add_quality_profile_measure_on_project() throws Exception {
90 treeRootHolder.setRoot(MULTI_MODULE_PROJECT);
92 QualityProfile qp = createQProfile(QP_NAME_1, LANGUAGE_KEY_1);
93 addMeasure(SUB_MODULE_REF, qp);
97 assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF).get(QUALITY_PROFILES_KEY)).extracting("data").containsOnly(toJson(qp));
101 public void add_quality_profile_measure_from_multiple_modules() throws Exception {
102 ReportComponent project = ReportComponent.builder(PROJECT, PROJECT_REF)
104 ReportComponent.builder(MODULE, MODULE_REF)
106 ReportComponent.builder(MODULE, SUB_MODULE_REF).build()
109 ReportComponent.builder(MODULE, 12).build()
112 treeRootHolder.setRoot(project);
114 QualityProfile qp1 = createQProfile(QP_NAME_1, LANGUAGE_KEY_1);
115 addMeasure(SUB_MODULE_REF, qp1);
116 QualityProfile qp2 = createQProfile(QP_NAME_2, LANGUAGE_KEY_2);
121 assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF).get(QUALITY_PROFILES_KEY)).extracting("data").containsOnly(toJson(qp1, qp2));
125 public void nothing_to_add_when_measure_already_exists_on_project() throws Exception {
126 ReportComponent project = ReportComponent.builder(PROJECT, PROJECT_REF).build();
128 treeRootHolder.setRoot(project);
130 QualityProfile qp = createQProfile(QP_NAME_1, LANGUAGE_KEY_1);
131 addMeasure(PROJECT_REF, qp);
135 assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF)).isEmpty();
139 public void fail_with_message_exception_when_no_qprofile_computed_on_project() throws Exception {
140 thrown.expect(MessageException.class);
141 thrown.expectMessage("No quality profiles has been found on project 'PROJECT KEY'");
143 treeRootHolder.setRoot(MULTI_MODULE_PROJECT);
147 assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF)).isEmpty();
151 public void fail_with_message_exception_when_qprofiles_computed_on_project_are_empty() throws Exception {
152 thrown.expect(MessageException.class);
153 thrown.expectMessage("No quality profiles has been found on project 'PROJECT KEY', you probably don't have any language plugin suitable for this analysis.");
155 treeRootHolder.setRoot(MULTI_MODULE_PROJECT);
156 measureRepository.addRawMeasure(PROJECT_REF, QUALITY_PROFILES_KEY, newMeasureBuilder().create(toJson()));
160 assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF)).isEmpty();
163 private static QualityProfile createQProfile(String qpName, String languageKey) {
164 return new QualityProfile(qpName + "-" + languageKey, qpName, languageKey, new Date());
167 private void addMeasure(int componentRef, QualityProfile... qps) {
168 Measure qualityProfileMeasure = newMeasureBuilder().create(toJson(qps));
169 measureRepository.addRawMeasure(componentRef, QUALITY_PROFILES_KEY, qualityProfileMeasure);
172 private static String toJson(QualityProfile... qps) {
173 List<QualityProfile> qualityProfiles = Arrays.asList(qps);
174 return QPMeasureData.toJson(new QPMeasureData(qualityProfiles));