]> source.dussan.org Git - sonarqube.git/blob
a55e2db19bea2873a6a80232ea7c1a3a9bb68509
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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 package org.sonar.ce.task.projectanalysis.qualitymodel;
21
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;
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.measure.Measure.newMeasureBuilder;
42 import static org.sonar.server.measure.Rating.B;
43 import static org.sonar.server.measure.Rating.C;
44
45 public class SecurityReviewRatingVisitorForPortfoliosAndApplicationsTest {
46
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))
51     .addChildren(
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())
54     .build();
55
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))
59     .build();
60
61   @Rule
62   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
63
64   @Rule
65   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
66     .add(NCLOC)
67     .add(SECURITY_HOTSPOTS)
68     .add(SECURITY_REVIEW_RATING);
69
70   @Rule
71   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
72
73   private VisitorsCrawler underTest = new VisitorsCrawler(singletonList(new SecurityReviewRatingVisitorForPortfoliosAndApplications(measureRepository, metricRepository)));
74
75   @Test
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));
84
85     underTest.visit(PORTFOLIO);
86
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());
90   }
91
92   @Test
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));
97
98     underTest.visit(APPLICATION);
99
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());
103   }
104
105   @Test
106   public void compute_nothing_when_no_ncloc() {
107     treeRootHolder.setRoot(PORTFOLIO);
108     measureRepository.addRawMeasure(PORTFOLIO_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(2));
109
110     underTest.visit(PORTFOLIO);
111
112     assertThat(measureRepository.getAddedRawMeasure(PORTFOLIO_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();
113   }
114
115   @Test
116   public void compute_nothing_when_no_security_hotspot() {
117     treeRootHolder.setRoot(PORTFOLIO);
118     measureRepository.addRawMeasure(PORTFOLIO_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
119
120     underTest.visit(PORTFOLIO);
121
122     assertThat(measureRepository.getAddedRawMeasure(PORTFOLIO_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();
123   }
124 }