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.

PersistAdHocRulesStepTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 java.util.Optional;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.rule.RuleKey;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.ce.task.projectanalysis.issue.AdHocRuleCreator;
  28. import org.sonar.ce.task.projectanalysis.issue.NewAdHocRule;
  29. import org.sonar.ce.task.projectanalysis.issue.RuleRepositoryImpl;
  30. import org.sonar.ce.task.step.ComputationStep;
  31. import org.sonar.ce.task.step.TestComputationStepContext;
  32. import org.sonar.core.util.SequenceUuidFactory;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbTester;
  35. import org.sonar.db.rule.RuleDao;
  36. import org.sonar.db.rule.RuleDefinitionDto;
  37. import org.sonar.scanner.protocol.output.ScannerReport;
  38. import org.sonar.server.es.EsTester;
  39. import org.sonar.server.rule.index.RuleIndexDefinition;
  40. import org.sonar.server.rule.index.RuleIndexer;
  41. import static org.assertj.core.api.Assertions.assertThat;
  42. public class PersistAdHocRulesStepTest extends BaseStepTest {
  43. @Rule
  44. public DbTester db = DbTester.create(System2.INSTANCE);
  45. private DbClient dbClient = db.getDbClient();
  46. private ComputationStep underTest;
  47. private RuleRepositoryImpl ruleRepository;
  48. @org.junit.Rule
  49. public EsTester es = EsTester.create();
  50. private RuleIndexer indexer = new RuleIndexer(es.client(), dbClient);
  51. private AdHocRuleCreator adHocRuleCreator = new AdHocRuleCreator(dbClient, System2.INSTANCE, indexer, new SequenceUuidFactory());
  52. @Override
  53. protected ComputationStep step() {
  54. return underTest;
  55. }
  56. @Before
  57. public void setup() {
  58. ruleRepository = new RuleRepositoryImpl(adHocRuleCreator, dbClient);
  59. underTest = new PersistAdHocRulesStep(dbClient, ruleRepository);
  60. }
  61. @Test
  62. public void persist_and_index_new_ad_hoc_rules() {
  63. RuleKey ruleKey = RuleKey.of("external_eslint", "no-cond-assign");
  64. ruleRepository.addOrUpdateAddHocRuleIfNeeded(ruleKey,
  65. () -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));
  66. underTest.execute(new TestComputationStepContext());
  67. RuleDao ruleDao = dbClient.ruleDao();
  68. Optional<RuleDefinitionDto> ruleDefinitionDtoOptional = ruleDao.selectDefinitionByKey(dbClient.openSession(false), ruleKey);
  69. assertThat(ruleDefinitionDtoOptional).isPresent();
  70. RuleDefinitionDto reloaded = ruleDefinitionDtoOptional.get();
  71. assertThat(reloaded.getRuleKey()).isEqualTo("no-cond-assign");
  72. assertThat(reloaded.getRepositoryKey()).isEqualTo("external_eslint");
  73. assertThat(reloaded.isExternal()).isTrue();
  74. assertThat(reloaded.isAdHoc()).isTrue();
  75. assertThat(reloaded.getType()).isZero();
  76. assertThat(reloaded.getSeverity()).isNull();
  77. assertThat(reloaded.getName()).isEqualTo("eslint:no-cond-assign");
  78. assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isOne();
  79. assertThat(es.getDocuments(RuleIndexDefinition.TYPE_RULE).iterator().next().getId()).isEqualTo(reloaded.getUuid());
  80. }
  81. @Test
  82. public void do_not_persist_existing_external_rules() {
  83. RuleKey ruleKey = RuleKey.of("eslint", "no-cond-assign");
  84. db.rules().insert(ruleKey, r -> r.setIsExternal(true));
  85. ruleRepository.addOrUpdateAddHocRuleIfNeeded(ruleKey,
  86. () -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));
  87. underTest.execute(new TestComputationStepContext());
  88. RuleDao ruleDao = dbClient.ruleDao();
  89. assertThat(ruleDao.selectAllDefinitions(dbClient.openSession(false))).hasSize(1);
  90. assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isZero();
  91. }
  92. }