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.

ActiveRulesProviderTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.scanner.rule;
  21. import com.google.common.collect.ImmutableList;
  22. import com.google.common.collect.ImmutableMap;
  23. import com.google.common.collect.ImmutableSet;
  24. import java.util.Date;
  25. import java.util.LinkedList;
  26. import java.util.List;
  27. import org.assertj.core.groups.Tuple;
  28. import org.junit.Test;
  29. import org.sonar.api.batch.rule.ActiveRules;
  30. import org.sonar.api.batch.rule.LoadedActiveRule;
  31. import org.sonar.api.batch.rule.internal.DefaultActiveRules;
  32. import org.sonar.api.rule.RuleKey;
  33. import org.sonar.api.utils.DateUtils;
  34. import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.mockito.ArgumentMatchers.eq;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.verify;
  39. import static org.mockito.Mockito.verifyNoMoreInteractions;
  40. import static org.mockito.Mockito.when;
  41. public class ActiveRulesProviderTest {
  42. private final ActiveRulesProvider provider = new ActiveRulesProvider();
  43. private final DefaultActiveRulesLoader loader = mock(DefaultActiveRulesLoader.class);
  44. @Test
  45. public void testCombinationOfRules() {
  46. LoadedActiveRule r1 = mockRule("rule1");
  47. LoadedActiveRule r2 = mockRule("rule2");
  48. LoadedActiveRule r3 = mockRule("rule3");
  49. List<LoadedActiveRule> qp1Rules = ImmutableList.of(r1, r2);
  50. List<LoadedActiveRule> qp2Rules = ImmutableList.of(r2, r3);
  51. List<LoadedActiveRule> qp3Rules = ImmutableList.of(r1, r3);
  52. when(loader.load("qp1")).thenReturn(qp1Rules);
  53. when(loader.load("qp2")).thenReturn(qp2Rules);
  54. when(loader.load("qp3")).thenReturn(qp3Rules);
  55. QualityProfiles profiles = mockProfiles("qp1", "qp2", "qp3");
  56. DefaultActiveRules activeRules = provider.provide(loader, profiles);
  57. assertThat(activeRules.findAll()).hasSize(3);
  58. assertThat(activeRules.findAll()).extracting("ruleKey").containsOnly(
  59. RuleKey.of("rule1", "rule1"), RuleKey.of("rule2", "rule2"), RuleKey.of("rule3", "rule3"));
  60. verify(loader).load("qp1");
  61. verify(loader).load("qp2");
  62. verify(loader).load("qp3");
  63. assertThat(activeRules.getDeprecatedRuleKeys(RuleKey.of("rule1", "rule1"))).containsOnly("rule1old:rule1old");
  64. verifyNoMoreInteractions(loader);
  65. }
  66. @Test
  67. public void testParamsAreTransformed() {
  68. LoadedActiveRule r1 = mockRule("rule1");
  69. LoadedActiveRule r2 = mockRule("rule2");
  70. r2.setParams(ImmutableMap.of("foo1", "bar1", "foo2", "bar2"));
  71. List<LoadedActiveRule> qpRules = ImmutableList.of(r1, r2);
  72. when(loader.load("qp")).thenReturn(qpRules);
  73. QualityProfiles profiles = mockProfiles("qp");
  74. ActiveRules activeRules = provider.provide(loader, profiles);
  75. assertThat(activeRules.findAll()).hasSize(2);
  76. assertThat(activeRules.findAll()).extracting("ruleKey", "params").containsOnly(
  77. Tuple.tuple(RuleKey.of("rule1", "rule1"), ImmutableMap.of()),
  78. Tuple.tuple(RuleKey.of("rule2", "rule2"), ImmutableMap.of("foo1", "bar1", "foo2", "bar2")));
  79. verify(loader).load("qp");
  80. verifyNoMoreInteractions(loader);
  81. }
  82. private static QualityProfiles mockProfiles(String... keys) {
  83. List<QualityProfile> profiles = new LinkedList<>();
  84. for (String k : keys) {
  85. QualityProfile p = QualityProfile.newBuilder().setKey(k).setLanguage(k).setRulesUpdatedAt(DateUtils.formatDateTime(new Date())).build();
  86. profiles.add(p);
  87. }
  88. return new QualityProfiles(profiles);
  89. }
  90. private static LoadedActiveRule mockRule(String name) {
  91. LoadedActiveRule r = new LoadedActiveRule();
  92. r.setName(name);
  93. r.setRuleKey(RuleKey.of(name, name));
  94. r.setDeprecatedKeys(ImmutableSet.of(RuleKey.of(name + "old", name + "old")));
  95. return r;
  96. }
  97. }