]> source.dussan.org Git - sonarqube.git/blob
354c4e8b2ad9e563a51f9544090991aa46b2834d
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
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
21 package org.sonar.server.computation.step;
22
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;
39
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;
47
48 public class ComputeQProfileMeasureStepTest {
49
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";
54
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;
59
60   private static final Component MULTI_MODULE_PROJECT = ReportComponent.builder(PROJECT, PROJECT_REF).setKey(PROJECT_KEY)
61     .addChildren(
62       ReportComponent.builder(MODULE, MODULE_REF)
63         .addChildren(
64           ReportComponent.builder(MODULE, SUB_MODULE_REF).build()
65         )
66         .build()
67     ).build();
68
69   @Rule
70   public ExpectedException thrown = ExpectedException.none();
71
72   @Rule
73   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
74
75   @Rule
76   public MetricRepositoryRule metricRepository = new MetricRepositoryRule().add(QUALITY_PROFILES);
77
78   @Rule
79   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
80
81   ComputeQProfileMeasureStep underTest;
82
83   @Before
84   public void setUp() throws Exception {
85     underTest = new ComputeQProfileMeasureStep(treeRootHolder, measureRepository, metricRepository);
86   }
87
88   @Test
89   public void add_quality_profile_measure_on_project() throws Exception {
90     treeRootHolder.setRoot(MULTI_MODULE_PROJECT);
91
92     QualityProfile qp = createQProfile(QP_NAME_1, LANGUAGE_KEY_1);
93     addMeasure(SUB_MODULE_REF, qp);
94
95     underTest.execute();
96
97     assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF).get(QUALITY_PROFILES_KEY)).extracting("data").containsOnly(toJson(qp));
98   }
99
100   @Test
101   public void add_quality_profile_measure_from_multiple_modules() throws Exception {
102     ReportComponent project = ReportComponent.builder(PROJECT, PROJECT_REF)
103       .addChildren(
104         ReportComponent.builder(MODULE, MODULE_REF)
105           .addChildren(
106             ReportComponent.builder(MODULE, SUB_MODULE_REF).build()
107           )
108           .build(),
109         ReportComponent.builder(MODULE, 12).build()
110       ).build();
111
112     treeRootHolder.setRoot(project);
113
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);
117     addMeasure(12, qp2);
118
119     underTest.execute();
120
121     assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF).get(QUALITY_PROFILES_KEY)).extracting("data").containsOnly(toJson(qp1, qp2));
122   }
123
124   @Test
125   public void nothing_to_add_when_measure_already_exists_on_project() throws Exception {
126     ReportComponent project = ReportComponent.builder(PROJECT, PROJECT_REF).build();
127
128     treeRootHolder.setRoot(project);
129
130     QualityProfile qp = createQProfile(QP_NAME_1, LANGUAGE_KEY_1);
131     addMeasure(PROJECT_REF, qp);
132
133     underTest.execute();
134
135     assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF)).isEmpty();
136   }
137
138   @Test
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'");
142
143     treeRootHolder.setRoot(MULTI_MODULE_PROJECT);
144
145     underTest.execute();
146
147     assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF)).isEmpty();
148   }
149
150   @Test
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.");
154
155     treeRootHolder.setRoot(MULTI_MODULE_PROJECT);
156     measureRepository.addRawMeasure(PROJECT_REF, QUALITY_PROFILES_KEY, newMeasureBuilder().create(toJson()));
157
158     underTest.execute();
159
160     assertThat(measureRepository.getAddedRawMeasures(PROJECT_REF)).isEmpty();
161   }
162
163   private static QualityProfile createQProfile(String qpName, String languageKey) {
164     return new QualityProfile(qpName + "-" + languageKey, qpName, languageKey, new Date());
165   }
166
167   private void addMeasure(int componentRef, QualityProfile... qps) {
168     Measure qualityProfileMeasure = newMeasureBuilder().create(toJson(qps));
169     measureRepository.addRawMeasure(componentRef, QUALITY_PROFILES_KEY, qualityProfileMeasure);
170   }
171
172   private static String toJson(QualityProfile... qps) {
173     List<QualityProfile> qualityProfiles = Arrays.asList(qps);
174     return QPMeasureData.toJson(new QPMeasureData(qualityProfiles));
175   }
176 }