3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.qualitymodel;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.ce.task.projectanalysis.component.Component;
25 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
26 import org.sonar.ce.task.projectanalysis.component.ViewAttributes;
27 import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
28 import org.sonar.ce.task.projectanalysis.component.VisitorsCrawler;
29 import org.sonar.ce.task.projectanalysis.measure.Measure;
30 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
31 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
33 import static java.util.Collections.singletonList;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.sonar.api.measures.CoreMetrics.NCLOC;
36 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
37 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS;
38 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
39 import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING;
40 import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY;
41 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
42 import static org.sonar.server.measure.Rating.B;
43 import static org.sonar.server.measure.Rating.C;
45 public class SecurityReviewRatingVisitorForPortfoliosAndApplicationsTest {
47 private static final int PORTFOLIO_REF = 10;
48 private static final int SUB_PORTFOLIO_1_REF = 11;
49 private static final int SUB_PORTFOLIO_2_REF = 12;
50 private static final Component PORTFOLIO = ViewsComponent.builder(Component.Type.VIEW, Integer.toString(PORTFOLIO_REF))
52 ViewsComponent.builder(Component.Type.SUBVIEW, Integer.toString(SUB_PORTFOLIO_1_REF)).build(),
53 ViewsComponent.builder(Component.Type.SUBVIEW, Integer.toString(SUB_PORTFOLIO_2_REF)).build())
56 private static final int APPLICATION_REF = 20;
57 private static final Component APPLICATION = ViewsComponent.builder(Component.Type.VIEW, Integer.toString(APPLICATION_REF))
58 .setViewAttributes(new ViewAttributes(ViewAttributes.Type.APPLICATION))
62 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
65 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
67 .add(SECURITY_HOTSPOTS)
68 .add(SECURITY_REVIEW_RATING);
71 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
73 private VisitorsCrawler underTest = new VisitorsCrawler(singletonList(new SecurityReviewRatingVisitorForPortfoliosAndApplications(measureRepository, metricRepository)));
76 public void compute_security_review_rating_on_portfolio() {
77 treeRootHolder.setRoot(PORTFOLIO);
78 measureRepository.addRawMeasure(PORTFOLIO_REF, NCLOC_KEY, newMeasureBuilder().create(2000));
79 measureRepository.addRawMeasure(PORTFOLIO_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(20));
80 measureRepository.addRawMeasure(SUB_PORTFOLIO_1_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
81 measureRepository.addRawMeasure(SUB_PORTFOLIO_1_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(5));
82 measureRepository.addRawMeasure(SUB_PORTFOLIO_2_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
83 measureRepository.addRawMeasure(SUB_PORTFOLIO_2_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(15));
85 underTest.visit(PORTFOLIO);
87 assertThat(measureRepository.getAddedRawMeasure(SUB_PORTFOLIO_1_REF, SECURITY_REVIEW_RATING_KEY).get().getIntValue()).isEqualTo(B.getIndex());
88 assertThat(measureRepository.getAddedRawMeasure(SUB_PORTFOLIO_2_REF, SECURITY_REVIEW_RATING_KEY).get().getIntValue()).isEqualTo(C.getIndex());
89 assertThat(measureRepository.getAddedRawMeasure(PORTFOLIO_REF, SECURITY_REVIEW_RATING_KEY).get().getIntValue()).isEqualTo(B.getIndex());
93 public void compute_security_review_rating_on_application() {
94 treeRootHolder.setRoot(APPLICATION);
95 measureRepository.addRawMeasure(APPLICATION_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
96 measureRepository.addRawMeasure(APPLICATION_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(12));
98 underTest.visit(APPLICATION);
100 Measure measure = measureRepository.getAddedRawMeasure(APPLICATION_REF, SECURITY_REVIEW_RATING_KEY).get();
101 assertThat(measure.getIntValue()).isEqualTo(C.getIndex());
102 assertThat(measure.getData()).isEqualTo(C.name());
106 public void compute_nothing_when_no_ncloc() {
107 treeRootHolder.setRoot(PORTFOLIO);
108 measureRepository.addRawMeasure(PORTFOLIO_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(2));
110 underTest.visit(PORTFOLIO);
112 assertThat(measureRepository.getAddedRawMeasure(PORTFOLIO_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();
116 public void compute_nothing_when_no_security_hotspot() {
117 treeRootHolder.setRoot(PORTFOLIO);
118 measureRepository.addRawMeasure(PORTFOLIO_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
120 underTest.visit(PORTFOLIO);
122 assertThat(measureRepository.getAddedRawMeasure(PORTFOLIO_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();