]> source.dussan.org Git - sonarqube.git/blob
905cd528922f5231123720bcc8fba2ff07f28b4f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.task.projectanalysis.qualitymodel;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
25 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
26 import org.sonar.server.computation.task.projectanalysis.component.VisitorsCrawler;
27 import org.sonar.server.computation.task.projectanalysis.issue.ComponentIssuesRepositoryRule;
28 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
29 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
30 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
31 import org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating;
32
33 import static java.util.Collections.singletonList;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING;
36 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY;
37 import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING;
38 import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY;
39 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
40 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW;
41 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
42 import static org.sonar.server.computation.task.projectanalysis.component.ViewsComponent.builder;
43 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
44 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
45 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
46 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.A;
47 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.B;
48 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.C;
49 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.D;
50 import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.E;
51
52 public class ReliabilityAndSecurityRatingMeasuresVisitorForViewsTest {
53
54   private static final int ROOT_REF = 1;
55   private static final int SUBVIEW_REF = 11;
56   private static final int SUB_SUBVIEW_1_REF = 111;
57   private static final int SUB_SUBVIEW_2_REF = 112;
58   private static final int PROJECT_VIEW_1_REF = 1111;
59   private static final int PROJECT_VIEW_2_REF = 1112;
60   private static final int PROJECT_VIEW_3_REF = 1121;
61   private static final int PROJECT_VIEW_4_REF = 12;
62
63   @Rule
64   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
65     .setRoot(builder(VIEW, ROOT_REF)
66       .addChildren(
67         builder(SUBVIEW, SUBVIEW_REF)
68           .addChildren(
69             builder(SUBVIEW, SUB_SUBVIEW_1_REF)
70               .addChildren(
71                 builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build(),
72                 builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build())
73               .build(),
74             builder(SUBVIEW, SUB_SUBVIEW_2_REF)
75               .addChildren(
76                 builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build())
77               .build())
78           .build(),
79         builder(PROJECT_VIEW, PROJECT_VIEW_4_REF).build())
80       .build());
81
82   @Rule
83   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
84     .add(RELIABILITY_RATING)
85     .add(SECURITY_RATING);
86
87   @Rule
88   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
89
90   @Rule
91   public ComponentIssuesRepositoryRule componentIssuesRepositoryRule = new ComponentIssuesRepositoryRule(treeRootHolder);
92
93   VisitorsCrawler underTest = new VisitorsCrawler(
94     singletonList(new ReliabilityAndSecurityRatingMeasuresVisitor(metricRepository, measureRepository, componentIssuesRepositoryRule)));
95
96   @Test
97   public void measures_created_for_view_are_all_zero_when_no_child() {
98     ViewsComponent root = builder(VIEW, 1).build();
99     treeRootHolder.setRoot(root);
100
101     underTest.visit(root);
102
103     assertThat(toEntries(measureRepository.getRawMeasures(root)))
104       .containsOnly(
105         entryOf(RELIABILITY_RATING_KEY, createMaintainabilityRatingMeasure(A)),
106         entryOf(SECURITY_RATING_KEY, createMaintainabilityRatingMeasure(A)));
107   }
108
109   @Test
110   public void compute_reliability_rating() throws Exception {
111     treeRootHolder.setRoot(treeRootHolder.getRoot());
112
113     addRawMeasure(RELIABILITY_RATING_KEY, PROJECT_VIEW_1_REF, B);
114     addRawMeasure(RELIABILITY_RATING_KEY, PROJECT_VIEW_2_REF, C);
115     addRawMeasure(RELIABILITY_RATING_KEY, PROJECT_VIEW_3_REF, D);
116     addRawMeasure(RELIABILITY_RATING_KEY, PROJECT_VIEW_4_REF, E);
117
118     underTest.visit(treeRootHolder.getRoot());
119
120     verifyRawMeasure(SUB_SUBVIEW_1_REF, RELIABILITY_RATING_KEY, C);
121     verifyRawMeasure(SUB_SUBVIEW_2_REF, RELIABILITY_RATING_KEY, D);
122     verifyRawMeasure(SUBVIEW_REF, RELIABILITY_RATING_KEY, D);
123     verifyRawMeasure(ROOT_REF, RELIABILITY_RATING_KEY, E);
124   }
125
126   @Test
127   public void compute_security_rating() throws Exception {
128     treeRootHolder.setRoot(treeRootHolder.getRoot());
129
130     addRawMeasure(SECURITY_RATING_KEY, PROJECT_VIEW_2_REF, B);
131     addRawMeasure(SECURITY_RATING_KEY, PROJECT_VIEW_1_REF, C);
132     addRawMeasure(SECURITY_RATING_KEY, PROJECT_VIEW_3_REF, D);
133     addRawMeasure(SECURITY_RATING_KEY, PROJECT_VIEW_4_REF, E);
134
135     underTest.visit(treeRootHolder.getRoot());
136
137     verifyRawMeasure(SUB_SUBVIEW_1_REF, SECURITY_RATING_KEY, C);
138     verifyRawMeasure(SUB_SUBVIEW_2_REF, SECURITY_RATING_KEY, D);
139     verifyRawMeasure(SUBVIEW_REF, SECURITY_RATING_KEY, D);
140     verifyRawMeasure(ROOT_REF, SECURITY_RATING_KEY, E);
141   }
142
143   private void addRawMeasure(String metricKey, int componentRef, Rating value) {
144     measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value.getIndex(), value.name()));
145   }
146
147   private void verifyRawMeasure(int componentRef, String metricKey, Rating rating) {
148     assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(rating.getIndex(), rating.name())));
149   }
150
151   private static Measure createMaintainabilityRatingMeasure(Rating rating) {
152     return newMeasureBuilder().create(rating.getIndex(), rating.name());
153   }
154
155 }