From 32b2fe0570c8f7adba76850f6d44ab230b8b13f5 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 22 Nov 2016 13:36:51 +0100 Subject: [PATCH] SONAR-8418 Stop computing ratings for views Ratings for views should be computed in Governance --- .../MaintainabilityMeasuresVisitor.java | 36 +-- ...ilityAndSecurityRatingMeasuresVisitor.java | 36 +-- ...ainabilityMeasuresVisitorForViewsTest.java | 235 ------------------ ...> MaintainabilityMeasuresVisitorTest.java} | 2 +- ...rityRatingMeasuresVisitorForViewsTest.java | 155 ------------ ...AndSecurityRatingMeasuresVisitorTest.java} | 2 +- 6 files changed, 7 insertions(+), 459 deletions(-) delete mode 100644 server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorForViewsTest.java rename server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/{MaintainabilityMeasuresVisitorForReportTest.java => MaintainabilityMeasuresVisitorTest.java} (99%) delete mode 100644 server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorForViewsTest.java rename server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/{ReliabilityAndSecurityRatingMeasuresVisitorForReportTest.java => ReliabilityAndSecurityRatingMeasuresVisitorTest.java} (99%) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitor.java index 8859f889450..80a340eb1d3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitor.java @@ -21,8 +21,6 @@ package org.sonar.server.computation.task.projectanalysis.qualitymodel; import com.google.common.base.Optional; import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; import org.sonar.server.computation.task.projectanalysis.component.Component; import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit; import org.sonar.server.computation.task.projectanalysis.component.PathAwareVisitorAdapter; @@ -43,15 +41,13 @@ import static org.sonar.server.computation.task.projectanalysis.component.Compon import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder; /** - * Compute measures related to maintainability : + * Compute measures related to maintainability for projects and descendants : * {@link CoreMetrics#DEVELOPMENT_COST_KEY} * {@link CoreMetrics#SQALE_DEBT_RATIO_KEY} * {@link CoreMetrics#SQALE_RATING_KEY} * {@link CoreMetrics#EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY} */ public class MaintainabilityMeasuresVisitor extends PathAwareVisitorAdapter { - private static final Logger LOG = Loggers.get(MaintainabilityMeasuresVisitor.class); - private final MeasureRepository measureRepository; private final RatingSettings ratingSettings; private final RatingGrid ratingGrid; @@ -65,7 +61,7 @@ public class MaintainabilityMeasuresVisitor extends PathAwareVisitorAdapter
path) { - computeAndSaveMeasures(view, path); - } - - @Override - public void visitSubView(Component subView, Path path) { - computeAndSaveMeasures(subView, path); - } - - @Override - public void visitProjectView(Component projectView, Path path) { - Optional developmentCostMeasure = measureRepository.getRawMeasure(projectView, developmentCostMetric); - if (developmentCostMeasure.isPresent()) { - try { - path.parent().addDevCosts(Long.valueOf(developmentCostMeasure.get().getStringValue())); - } catch (NumberFormatException e) { - LOG.trace("Failed to parse value of metric {} for component {}", developmentCostMetric.getName(), projectView.getKey()); - } - } - } - private void computeAndSaveMeasures(Component component, Path path) { addDevelopmentCostMeasure(component, path.current()); @@ -210,11 +184,5 @@ public class MaintainabilityMeasuresVisitor extends PathAwareVisitorAdapter
metricsByKey; public ReliabilityAndSecurityRatingMeasuresVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, ComponentIssuesRepository componentIssuesRepository) { - super(LEAVES, POST_ORDER, CounterFactory.INSTANCE); + super(FILE, POST_ORDER, CounterFactory.INSTANCE); this.measureRepository = measureRepository; this.componentIssuesRepository = componentIssuesRepository; @@ -111,26 +109,6 @@ public class ReliabilityAndSecurityRatingMeasuresVisitor extends PathAwareVisito computeAndSaveMeasures(file, path); } - @Override - public void visitView(Component view, Path path) { - computeAndSaveMeasures(view, path); - } - - @Override - public void visitSubView(Component subView, Path path) { - computeAndSaveMeasures(subView, path); - } - - @Override - public void visitProjectView(Component projectView, Path path) { - path.parent().ratingValueByMetric.entrySet().forEach(entry -> { - Optional ratingMeasure = measureRepository.getRawMeasure(projectView, metricsByKey.get(entry.getKey())); - if (ratingMeasure.isPresent()) { - entry.getValue().increment(valueOf(ratingMeasure.get().getData())); - } - }); - } - private void computeAndSaveMeasures(Component component, Path path) { processIssues(component, path); path.current().ratingValueByMetric.entrySet().forEach( @@ -190,13 +168,5 @@ public class ReliabilityAndSecurityRatingMeasuresVisitor extends PathAwareVisito public Counter createForAny(Component component) { return new Counter(); } - - /** - * Counter is not used at ProjectView level, saves on instantiating useless objects - */ - @Override - public Counter createForProjectView(Component projectView) { - return null; - } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorForViewsTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorForViewsTest.java deleted file mode 100644 index bbc984eedc7..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorForViewsTest.java +++ /dev/null @@ -1,235 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.computation.task.projectanalysis.qualitymodel; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule; -import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent; -import org.sonar.server.computation.task.projectanalysis.component.VisitorsCrawler; -import org.sonar.server.computation.task.projectanalysis.measure.Measure; -import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule; -import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule; -import org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating; - -import static java.util.Collections.singletonList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.api.measures.CoreMetrics.DEVELOPMENT_COST; -import static org.sonar.api.measures.CoreMetrics.DEVELOPMENT_COST_KEY; -import static org.sonar.api.measures.CoreMetrics.EFFORT_TO_REACH_MAINTAINABILITY_RATING_A; -import static org.sonar.api.measures.CoreMetrics.EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY; -import static org.sonar.api.measures.CoreMetrics.NCLOC; -import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING; -import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING; -import static org.sonar.api.measures.CoreMetrics.SQALE_DEBT_RATIO; -import static org.sonar.api.measures.CoreMetrics.SQALE_DEBT_RATIO_KEY; -import static org.sonar.api.measures.CoreMetrics.SQALE_RATING; -import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY; -import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT; -import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY; -import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT_VIEW; -import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW; -import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW; -import static org.sonar.server.computation.task.projectanalysis.component.ViewsComponent.builder; -import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder; -import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.entryOf; -import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.toEntries; -import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.A; -import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.B; -import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.D; -import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.E; - -public class MaintainabilityMeasuresVisitorForViewsTest { - - private static final double[] RATING_GRID = new double[] {34, 50, 362, 900, 36258}; - - private static final int ROOT_REF = 1; - private static final int SUBVIEW_REF = 11; - private static final int SUB_SUBVIEW_1_REF = 111; - private static final int SUB_SUBVIEW_2_REF = 112; - private static final int PROJECT_VIEW_1_REF = 1111; - private static final int PROJECT_VIEW_2_REF = 1112; - private static final int PROJECT_VIEW_3_REF = 1121; - private static final int PROJECT_VIEW_4_REF = 12; - - @Rule - public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule() - .setRoot(builder(VIEW, ROOT_REF) - .addChildren( - builder(SUBVIEW, SUBVIEW_REF) - .addChildren( - builder(SUBVIEW, SUB_SUBVIEW_1_REF) - .addChildren( - builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build(), - builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build()) - .build(), - builder(SUBVIEW, SUB_SUBVIEW_2_REF) - .addChildren( - builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build()) - .build()) - .build(), - builder(PROJECT_VIEW, PROJECT_VIEW_4_REF).build()) - .build()); - - @Rule - public MetricRepositoryRule metricRepository = new MetricRepositoryRule() - .add(NCLOC) - .add(DEVELOPMENT_COST) - .add(TECHNICAL_DEBT) - .add(SQALE_DEBT_RATIO) - .add(SQALE_RATING) - .add(EFFORT_TO_REACH_MAINTAINABILITY_RATING_A) - .add(RELIABILITY_RATING) - .add(SECURITY_RATING); - - @Rule - public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); - - private RatingSettings ratingSettings = mock(RatingSettings.class); - - private VisitorsCrawler underTest; - - @Before - public void setUp() { - when(ratingSettings.getRatingGrid()).thenReturn(new RatingGrid(RATING_GRID)); - underTest = new VisitorsCrawler(singletonList(new MaintainabilityMeasuresVisitor(metricRepository, measureRepository, ratingSettings))); - } - - @Test - public void measures_created_for_view_are_all_zero_when_no_child() { - ViewsComponent root = builder(VIEW, 1).build(); - treeRootHolder.setRoot(root); - - underTest.visit(root); - - assertThat(toEntries(measureRepository.getRawMeasures(root))) - .containsOnly( - entryOf(DEVELOPMENT_COST_KEY, newMeasureBuilder().create("0")), - entryOf(SQALE_DEBT_RATIO_KEY, newMeasureBuilder().create(0d, 1)), - entryOf(SQALE_RATING_KEY, createMaintainabilityRatingMeasure(A)), - entryOf(EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY, newMeasureBuilder().create(0L))); - } - - @Test - public void verify_aggregation_of_developmentCost_and_value_of_measures_computed_from_that() { - long debtRoot = 9999l; - long debtSubView = 96325l; - long debtSubSubView1 = 96325l; - long debtSubSubView2 = 99633l; - - addRawMeasure(TECHNICAL_DEBT_KEY, ROOT_REF, debtRoot); - addRawMeasure(TECHNICAL_DEBT_KEY, SUBVIEW_REF, debtSubView); - addRawMeasure(TECHNICAL_DEBT_KEY, SUB_SUBVIEW_1_REF, debtSubSubView1); - addRawMeasure(TECHNICAL_DEBT_KEY, PROJECT_VIEW_1_REF, 66000l); - addRawMeasure(TECHNICAL_DEBT_KEY, PROJECT_VIEW_2_REF, 4200l); - addRawMeasure(TECHNICAL_DEBT_KEY, SUB_SUBVIEW_2_REF, debtSubSubView2); - addRawMeasure(TECHNICAL_DEBT_KEY, PROJECT_VIEW_3_REF, 25200l); - addRawMeasure(TECHNICAL_DEBT_KEY, PROJECT_VIEW_4_REF, 33000l); - - addRawMeasure(DEVELOPMENT_COST_KEY, PROJECT_VIEW_1_REF, "40"); - addRawMeasure(DEVELOPMENT_COST_KEY, PROJECT_VIEW_2_REF, "70"); - addRawMeasure(DEVELOPMENT_COST_KEY, PROJECT_VIEW_3_REF, "50"); - addRawMeasure(DEVELOPMENT_COST_KEY, PROJECT_VIEW_4_REF, "100"); - - underTest.visit(treeRootHolder.getRoot()); - - assertNoNewRawMeasureOnProjectViews(); - assertNewRawMeasures(SUB_SUBVIEW_1_REF, debtSubSubView1, 110, D); - assertNewRawMeasures(SUB_SUBVIEW_2_REF, debtSubSubView2, 50, E); - assertNewRawMeasures(SUBVIEW_REF, debtSubView, 160, D); - assertNewRawMeasures(ROOT_REF, debtRoot, 260, B); - } - - @Test - public void compute_effort_to_maintainability_rating_A_measure() throws Exception { - treeRootHolder.setRoot(treeRootHolder.getRoot()); - - long projectView1DevCosts = 40L; - addRawMeasure(DEVELOPMENT_COST_KEY, PROJECT_VIEW_1_REF, Long.toString(projectView1DevCosts)); - long projectView2DevCosts = 70L; - addRawMeasure(DEVELOPMENT_COST_KEY, PROJECT_VIEW_2_REF, Long.toString(projectView2DevCosts)); - long projectView3DevCosts = 50L; - addRawMeasure(DEVELOPMENT_COST_KEY, PROJECT_VIEW_3_REF, Long.toString(projectView3DevCosts)); - long projectView4DevCosts = 100L; - addRawMeasure(DEVELOPMENT_COST_KEY, PROJECT_VIEW_4_REF, Long.toString(projectView4DevCosts)); - - long subSubView1Effort = 10000L; - addRawMeasure(TECHNICAL_DEBT_KEY, SUB_SUBVIEW_1_REF, subSubView1Effort); - - long subSubView2Effort = 20000L; - addRawMeasure(TECHNICAL_DEBT_KEY, SUB_SUBVIEW_2_REF, subSubView2Effort); - - long subViewEffort = 30000L; - addRawMeasure(TECHNICAL_DEBT_KEY, SUBVIEW_REF, subViewEffort); - - long viewEffort = 35000L; - addRawMeasure(TECHNICAL_DEBT_KEY, ROOT_REF, viewEffort); - - underTest.visit(treeRootHolder.getRoot()); - - verifyRawMeasure(SUB_SUBVIEW_1_REF, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY, - (long) (subSubView1Effort - RATING_GRID[0] * (projectView1DevCosts + projectView2DevCosts))); - verifyRawMeasure(SUB_SUBVIEW_2_REF, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY, - (long) (subSubView2Effort - RATING_GRID[0] * projectView3DevCosts)); - verifyRawMeasure(SUBVIEW_REF, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY, - (long) (subViewEffort - RATING_GRID[0] * (projectView1DevCosts + projectView2DevCosts + projectView3DevCosts))); - verifyRawMeasure(ROOT_REF, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY, - (long) (viewEffort - RATING_GRID[0] * (projectView1DevCosts + projectView2DevCosts + projectView3DevCosts + - projectView4DevCosts))); - } - - private void assertNewRawMeasures(int componentRef, long debt, long devCost, Rating rating) { - assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains( - entryOf(DEVELOPMENT_COST_KEY, newMeasureBuilder().create(String.valueOf(devCost))), - entryOf(SQALE_DEBT_RATIO_KEY, newMeasureBuilder().create(debt / (double) devCost * 100.0, 1)), - entryOf(SQALE_RATING_KEY, createMaintainabilityRatingMeasure(rating))); - } - - private void assertNoNewRawMeasureOnProjectViews() { - assertNoNewRawMeasure(PROJECT_VIEW_1_REF); - assertNoNewRawMeasure(PROJECT_VIEW_2_REF); - assertNoNewRawMeasure(PROJECT_VIEW_3_REF); - assertNoNewRawMeasure(PROJECT_VIEW_4_REF); - } - - private void addRawMeasure(String metricKey, int componentRef, String value) { - measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value)); - } - - private void addRawMeasure(String metricKey, int componentRef, long value) { - measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value)); - } - - private void assertNoNewRawMeasure(int componentRef) { - assertThat(measureRepository.getAddedRawMeasures(componentRef).isEmpty()).isTrue(); - } - - private void verifyRawMeasure(int componentRef, String metricKey, long value) { - assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(value))); - } - - private static Measure createMaintainabilityRatingMeasure(Rating rating) { - return newMeasureBuilder().create(rating.getIndex(), rating.name()); - } - -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorForReportTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorTest.java similarity index 99% rename from server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorForReportTest.java rename to server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorTest.java index 4dd13473124..53acf238294 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorForReportTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitorTest.java @@ -61,7 +61,7 @@ import static org.sonar.server.computation.task.projectanalysis.qualitymodel.Rat import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.C; import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.E; -public class MaintainabilityMeasuresVisitorForReportTest { +public class MaintainabilityMeasuresVisitorTest { static final String LANGUAGE_KEY_1 = "lKey1"; static final String LANGUAGE_KEY_2 = "lKey2"; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorForViewsTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorForViewsTest.java deleted file mode 100644 index 905cd528922..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorForViewsTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.computation.task.projectanalysis.qualitymodel; - -import org.junit.Rule; -import org.junit.Test; -import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule; -import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent; -import org.sonar.server.computation.task.projectanalysis.component.VisitorsCrawler; -import org.sonar.server.computation.task.projectanalysis.issue.ComponentIssuesRepositoryRule; -import org.sonar.server.computation.task.projectanalysis.measure.Measure; -import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule; -import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule; -import org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating; - -import static java.util.Collections.singletonList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING; -import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY; -import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING; -import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY; -import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT_VIEW; -import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW; -import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW; -import static org.sonar.server.computation.task.projectanalysis.component.ViewsComponent.builder; -import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder; -import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.entryOf; -import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.toEntries; -import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.A; -import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.B; -import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.C; -import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.D; -import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.E; - -public class ReliabilityAndSecurityRatingMeasuresVisitorForViewsTest { - - private static final int ROOT_REF = 1; - private static final int SUBVIEW_REF = 11; - private static final int SUB_SUBVIEW_1_REF = 111; - private static final int SUB_SUBVIEW_2_REF = 112; - private static final int PROJECT_VIEW_1_REF = 1111; - private static final int PROJECT_VIEW_2_REF = 1112; - private static final int PROJECT_VIEW_3_REF = 1121; - private static final int PROJECT_VIEW_4_REF = 12; - - @Rule - public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule() - .setRoot(builder(VIEW, ROOT_REF) - .addChildren( - builder(SUBVIEW, SUBVIEW_REF) - .addChildren( - builder(SUBVIEW, SUB_SUBVIEW_1_REF) - .addChildren( - builder(PROJECT_VIEW, PROJECT_VIEW_1_REF).build(), - builder(PROJECT_VIEW, PROJECT_VIEW_2_REF).build()) - .build(), - builder(SUBVIEW, SUB_SUBVIEW_2_REF) - .addChildren( - builder(PROJECT_VIEW, PROJECT_VIEW_3_REF).build()) - .build()) - .build(), - builder(PROJECT_VIEW, PROJECT_VIEW_4_REF).build()) - .build()); - - @Rule - public MetricRepositoryRule metricRepository = new MetricRepositoryRule() - .add(RELIABILITY_RATING) - .add(SECURITY_RATING); - - @Rule - public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); - - @Rule - public ComponentIssuesRepositoryRule componentIssuesRepositoryRule = new ComponentIssuesRepositoryRule(treeRootHolder); - - VisitorsCrawler underTest = new VisitorsCrawler( - singletonList(new ReliabilityAndSecurityRatingMeasuresVisitor(metricRepository, measureRepository, componentIssuesRepositoryRule))); - - @Test - public void measures_created_for_view_are_all_zero_when_no_child() { - ViewsComponent root = builder(VIEW, 1).build(); - treeRootHolder.setRoot(root); - - underTest.visit(root); - - assertThat(toEntries(measureRepository.getRawMeasures(root))) - .containsOnly( - entryOf(RELIABILITY_RATING_KEY, createMaintainabilityRatingMeasure(A)), - entryOf(SECURITY_RATING_KEY, createMaintainabilityRatingMeasure(A))); - } - - @Test - public void compute_reliability_rating() throws Exception { - treeRootHolder.setRoot(treeRootHolder.getRoot()); - - addRawMeasure(RELIABILITY_RATING_KEY, PROJECT_VIEW_1_REF, B); - addRawMeasure(RELIABILITY_RATING_KEY, PROJECT_VIEW_2_REF, C); - addRawMeasure(RELIABILITY_RATING_KEY, PROJECT_VIEW_3_REF, D); - addRawMeasure(RELIABILITY_RATING_KEY, PROJECT_VIEW_4_REF, E); - - underTest.visit(treeRootHolder.getRoot()); - - verifyRawMeasure(SUB_SUBVIEW_1_REF, RELIABILITY_RATING_KEY, C); - verifyRawMeasure(SUB_SUBVIEW_2_REF, RELIABILITY_RATING_KEY, D); - verifyRawMeasure(SUBVIEW_REF, RELIABILITY_RATING_KEY, D); - verifyRawMeasure(ROOT_REF, RELIABILITY_RATING_KEY, E); - } - - @Test - public void compute_security_rating() throws Exception { - treeRootHolder.setRoot(treeRootHolder.getRoot()); - - addRawMeasure(SECURITY_RATING_KEY, PROJECT_VIEW_2_REF, B); - addRawMeasure(SECURITY_RATING_KEY, PROJECT_VIEW_1_REF, C); - addRawMeasure(SECURITY_RATING_KEY, PROJECT_VIEW_3_REF, D); - addRawMeasure(SECURITY_RATING_KEY, PROJECT_VIEW_4_REF, E); - - underTest.visit(treeRootHolder.getRoot()); - - verifyRawMeasure(SUB_SUBVIEW_1_REF, SECURITY_RATING_KEY, C); - verifyRawMeasure(SUB_SUBVIEW_2_REF, SECURITY_RATING_KEY, D); - verifyRawMeasure(SUBVIEW_REF, SECURITY_RATING_KEY, D); - verifyRawMeasure(ROOT_REF, SECURITY_RATING_KEY, E); - } - - private void addRawMeasure(String metricKey, int componentRef, Rating value) { - measureRepository.addRawMeasure(componentRef, metricKey, newMeasureBuilder().create(value.getIndex(), value.name())); - } - - private void verifyRawMeasure(int componentRef, String metricKey, Rating rating) { - assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).contains(entryOf(metricKey, newMeasureBuilder().create(rating.getIndex(), rating.name()))); - } - - private static Measure createMaintainabilityRatingMeasure(Rating rating) { - return newMeasureBuilder().create(rating.getIndex(), rating.name()); - } - -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorForReportTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorTest.java similarity index 99% rename from server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorForReportTest.java rename to server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorTest.java index 7f69d94b10b..8ba41fd9195 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorForReportTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitorTest.java @@ -68,7 +68,7 @@ import static org.sonar.server.computation.task.projectanalysis.qualitymodel.Rat import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.D; import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.E; -public class ReliabilityAndSecurityRatingMeasuresVisitorForReportTest { +public class ReliabilityAndSecurityRatingMeasuresVisitorTest { static final String LANGUAGE_KEY_1 = "lKey1"; -- 2.39.5