3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.server.computation.task.projectanalysis.qualitymodel;
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;
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;
52 public class ReliabilityAndSecurityRatingMeasuresVisitorForViewsTest {
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;
64 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
65 .setRoot(builder(VIEW, ROOT_REF)
67 builder(SUBVIEW, SUBVIEW_REF)
69 builder(SUBVIEW, SUB_SUBVIEW_1_REF)
71 builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build(),
72 builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build())
74 builder(SUBVIEW, SUB_SUBVIEW_2_REF)
76 builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build())
79 builder(PROJECT_VIEW, PROJECT_VIEW_4_REF).build())
83 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
84 .add(RELIABILITY_RATING)
85 .add(SECURITY_RATING);
88 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
91 public ComponentIssuesRepositoryRule componentIssuesRepositoryRule = new ComponentIssuesRepositoryRule(treeRootHolder);
93 VisitorsCrawler underTest = new VisitorsCrawler(
94 singletonList(new ReliabilityAndSecurityRatingMeasuresVisitor(metricRepository, measureRepository, componentIssuesRepositoryRule)));
97 public void measures_created_for_view_are_all_zero_when_no_child() {
98 ViewsComponent root = builder(VIEW, 1).build();
99 treeRootHolder.setRoot(root);
101 underTest.visit(root);
103 assertThat(toEntries(measureRepository.getRawMeasures(root)))
105 entryOf(RELIABILITY_RATING_KEY, createMaintainabilityRatingMeasure(A)),
106 entryOf(SECURITY_RATING_KEY, createMaintainabilityRatingMeasure(A)));
110 public void compute_reliability_rating() throws Exception {
111 treeRootHolder.setRoot(treeRootHolder.getRoot());
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);
118 underTest.visit(treeRootHolder.getRoot());
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);
127 public void compute_security_rating() throws Exception {
128 treeRootHolder.setRoot(treeRootHolder.getRoot());
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);
135 underTest.visit(treeRootHolder.getRoot());
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);
143 private void addRawMeasure(String metricKey, int componentRef, Rating value) {
144 measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value.getIndex(), value.name()));
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())));
151 private static Measure createMaintainabilityRatingMeasure(Rating rating) {
152 return newMeasureBuilder().create(rating.getIndex(), rating.name());