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.

LoadQualityProfilesStepTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.step;
  21. import org.assertj.core.data.MapEntry;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.api.rule.RuleStatus;
  25. import org.sonar.api.rule.Severity;
  26. import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
  27. import org.sonar.ce.task.projectanalysis.issue.DumbRule;
  28. import org.sonar.ce.task.projectanalysis.issue.RuleRepositoryRule;
  29. import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
  30. import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolderImpl;
  31. import org.sonar.ce.task.step.TestComputationStepContext;
  32. import org.sonar.scanner.protocol.Constants;
  33. import org.sonar.scanner.protocol.output.ScannerReport;
  34. import static java.util.Arrays.asList;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.sonar.db.rule.RuleTesting.XOO_X1;
  37. import static org.sonar.db.rule.RuleTesting.XOO_X2;
  38. public class LoadQualityProfilesStepTest {
  39. @Rule
  40. public BatchReportReaderRule batchReportReader = new BatchReportReaderRule();
  41. @Rule
  42. public RuleRepositoryRule ruleRepository = new RuleRepositoryRule();
  43. private ActiveRulesHolderImpl activeRulesHolder = new ActiveRulesHolderImpl();
  44. private LoadQualityProfilesStep underTest = new LoadQualityProfilesStep(batchReportReader, activeRulesHolder, ruleRepository);
  45. @Test
  46. public void feed_active_rules() {
  47. ruleRepository.add(XOO_X1)
  48. .setPluginKey("xoo");
  49. ruleRepository.add(XOO_X2)
  50. .setPluginKey("xoo");
  51. ScannerReport.ActiveRule.Builder batch1 = ScannerReport.ActiveRule.newBuilder()
  52. .setRuleRepository(XOO_X1.repository())
  53. .setRuleKey(XOO_X1.rule())
  54. .setSeverity(Constants.Severity.BLOCKER)
  55. .setCreatedAt(1000L)
  56. .setUpdatedAt(1200L);
  57. batch1.getMutableParamsByKey().put("p1", "v1");
  58. ScannerReport.ActiveRule.Builder batch2 = ScannerReport.ActiveRule.newBuilder()
  59. .setRuleRepository(XOO_X2.repository()).setRuleKey(XOO_X2.rule()).setSeverity(Constants.Severity.MAJOR);
  60. batchReportReader.putActiveRules(asList(batch1.build(), batch2.build()));
  61. underTest.execute(new TestComputationStepContext());
  62. assertThat(activeRulesHolder.getAll()).hasSize(2);
  63. ActiveRule ar1 = activeRulesHolder.get(XOO_X1).get();
  64. assertThat(ar1.getSeverity()).isEqualTo(Severity.BLOCKER);
  65. assertThat(ar1.getParams()).containsExactly(MapEntry.entry("p1", "v1"));
  66. assertThat(ar1.getPluginKey()).isEqualTo("xoo");
  67. assertThat(ar1.getUpdatedAt()).isEqualTo(1200L);
  68. ActiveRule ar2 = activeRulesHolder.get(XOO_X2).get();
  69. assertThat(ar2.getSeverity()).isEqualTo(Severity.MAJOR);
  70. assertThat(ar2.getParams()).isEmpty();
  71. assertThat(ar2.getPluginKey()).isEqualTo("xoo");
  72. assertThat(ar1.getUpdatedAt()).isEqualTo(1200L);
  73. }
  74. @Test
  75. public void ignore_rules_with_status_REMOVED() {
  76. ruleRepository.add(new DumbRule(XOO_X1).setStatus(RuleStatus.REMOVED));
  77. ScannerReport.ActiveRule.Builder batch1 = ScannerReport.ActiveRule.newBuilder()
  78. .setRuleRepository(XOO_X1.repository()).setRuleKey(XOO_X1.rule())
  79. .setSeverity(Constants.Severity.BLOCKER);
  80. batchReportReader.putActiveRules(asList(batch1.build()));
  81. underTest.execute(new TestComputationStepContext());
  82. assertThat(activeRulesHolder.getAll()).isEmpty();
  83. }
  84. @Test
  85. public void ignore_not_found_rules() {
  86. ScannerReport.ActiveRule.Builder batch1 = ScannerReport.ActiveRule.newBuilder()
  87. .setRuleRepository(XOO_X1.repository()).setRuleKey(XOO_X1.rule())
  88. .setSeverity(Constants.Severity.BLOCKER);
  89. batchReportReader.putActiveRules(asList(batch1.build()));
  90. underTest.execute(new TestComputationStepContext());
  91. assertThat(activeRulesHolder.getAll()).isEmpty();
  92. }
  93. }