]> source.dussan.org Git - sonarqube.git/blob
ee8e616336d8c1c52a2811fbd518f563fd434c95
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 SonarSource SA
4  * mailto:info 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
21 package org.sonar.ce.task.projectanalysis.qualitymodel;
22
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;
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.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;
43
44 public class SecurityReviewRatingVisitorTest {
45
46   private static final int PROJECT_REF = 1;
47   private static final Component PROJECT = builder(Component.Type.PROJECT, PROJECT_REF).setKey("project").build();
48
49   @Rule
50   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
51
52   @Rule
53   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
54     .add(NCLOC)
55     .add(SECURITY_HOTSPOTS)
56     .add(SECURITY_REVIEW_RATING);
57
58   @Rule
59   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
60
61   private VisitorsCrawler underTest = new VisitorsCrawler(singletonList(new SecurityReviewRatingVisitor(measureRepository, metricRepository)));
62
63   @Test
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));
68
69     underTest.visit(PROJECT);
70
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());
74   }
75
76   @Test
77   public void compute_nothing_when_no_ncloc() {
78     treeRootHolder.setRoot(PROJECT);
79     measureRepository.addRawMeasure(PROJECT_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(2));
80
81     underTest.visit(PROJECT);
82
83     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();
84   }
85
86   @Test
87   public void compute_nothing_when_no_security_hotspot() {
88     treeRootHolder.setRoot(PROJECT);
89     measureRepository.addRawMeasure(PROJECT_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
90
91     underTest.visit(PROJECT);
92
93     assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();
94   }
95 }