You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SecurityReviewRatingVisitorTest.java 4.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. package org.sonar.ce.task.projectanalysis.qualitymodel;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.ce.task.projectanalysis.component.Component;
  24. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  25. import org.sonar.ce.task.projectanalysis.component.VisitorsCrawler;
  26. import org.sonar.ce.task.projectanalysis.measure.Measure;
  27. import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
  28. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  29. import org.sonar.server.measure.Rating;
  30. import static java.util.Collections.singletonList;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.sonar.api.measures.CoreMetrics.NCLOC;
  33. import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
  34. import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS;
  35. import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
  36. import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING;
  37. import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY;
  38. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  39. import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
  40. public class SecurityReviewRatingVisitorTest {
  41. private static final int PROJECT_REF = 1;
  42. private static final Component PROJECT = builder(Component.Type.PROJECT, PROJECT_REF).setKey("project").build();
  43. @Rule
  44. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  45. @Rule
  46. public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
  47. .add(NCLOC)
  48. .add(SECURITY_HOTSPOTS)
  49. .add(SECURITY_REVIEW_RATING);
  50. @Rule
  51. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  52. private VisitorsCrawler underTest = new VisitorsCrawler(singletonList(new SecurityReviewRatingVisitor(measureRepository, metricRepository)));
  53. @Test
  54. public void compute_security_review_rating_on_project() {
  55. treeRootHolder.setRoot(PROJECT);
  56. measureRepository.addRawMeasure(PROJECT_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
  57. measureRepository.addRawMeasure(PROJECT_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(12));
  58. underTest.visit(PROJECT);
  59. Measure measure = measureRepository.getAddedRawMeasure(PROJECT_REF, SECURITY_REVIEW_RATING_KEY).get();
  60. assertThat(measure.getIntValue()).isEqualTo(Rating.C.getIndex());
  61. assertThat(measure.getData()).isEqualTo(Rating.C.name());
  62. }
  63. @Test
  64. public void compute_nothing_when_no_ncloc() {
  65. treeRootHolder.setRoot(PROJECT);
  66. measureRepository.addRawMeasure(PROJECT_REF, SECURITY_HOTSPOTS_KEY, newMeasureBuilder().create(2));
  67. underTest.visit(PROJECT);
  68. assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();
  69. }
  70. @Test
  71. public void compute_nothing_when_no_security_hotspot() {
  72. treeRootHolder.setRoot(PROJECT);
  73. measureRepository.addRawMeasure(PROJECT_REF, NCLOC_KEY, newMeasureBuilder().create(1000));
  74. underTest.visit(PROJECT);
  75. assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, SECURITY_REVIEW_RATING_KEY)).isEmpty();
  76. }
  77. }