3 * Copyright (C) 2009-2019 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.
21 package org.sonar.ce.task.projectanalysis.qualitymodel;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.ce.task.projectanalysis.component.Component;
26 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
27 import org.sonar.ce.task.projectanalysis.component.VisitorsCrawler;
28 import org.sonar.ce.task.projectanalysis.measure.Measure;
29 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
30 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
31 import org.sonar.server.measure.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.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.component.ReportComponent.builder;
42 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
44 public class SecurityReviewRatingVisitorTest {
46 private static final int PROJECT_REF = 1;
47 private static final Component PROJECT = builder(Component.Type.PROJECT, PROJECT_REF).setKey("project").build();
50 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
53 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
55 .add(SECURITY_HOTSPOTS)
56 .add(SECURITY_REVIEW_RATING);
59 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
61 private VisitorsCrawler underTest = new VisitorsCrawler(singletonList(new SecurityReviewRatingVisitor(measureRepository, metricRepository)));
64 public void compute_security_review_rating_on_project() {
65 treeRootHolder.setRoot(PROJECT);
66 measureRepository.addRawMeasure(PROJECT_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
67 measureRepository.addRawMeasure(PROJECT_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(12));
69 underTest.visit(PROJECT);
71 Measure measure = measureRepository.getAddedRawMeasure(PROJECT_REF, SECURITY_REVIEW_RATING_KEY).get();
72 assertThat(measure.getIntValue()).isEqualTo(Rating.C.getIndex());
73 assertThat(measure.getData()).isEqualTo(Rating.C.name());
77 public void compute_nothing_when_no_ncloc() {
78 treeRootHolder.setRoot(PROJECT);
79 measureRepository.addRawMeasure(PROJECT_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(2));
81 underTest.visit(PROJECT);
83 assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();
87 public void compute_nothing_when_no_security_hotspot() {
88 treeRootHolder.setRoot(PROJECT);
89 measureRepository.addRawMeasure(PROJECT_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
91 underTest.visit(PROJECT);
93 assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();