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.

ExportRuleStepTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.projectexport.rule;
  21. import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import java.util.List;
  25. import org.junit.Test;
  26. import org.slf4j.event.Level;
  27. import org.sonar.api.rule.RuleKey;
  28. import org.sonar.api.testfixtures.log.LogTester;
  29. import org.sonar.ce.task.projectexport.steps.DumpElement;
  30. import org.sonar.ce.task.projectexport.steps.FakeDumpWriter;
  31. import org.sonar.ce.task.step.TestComputationStepContext;
  32. import org.sonar.core.util.Uuids;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  35. public class ExportRuleStepTest {
  36. private static final String REPOSITORY = "repository";
  37. @org.junit.Rule
  38. public LogTester logTester = new LogTester();
  39. private FakeDumpWriter dumpWriter = new FakeDumpWriter();
  40. private SimpleRuleRepository ruleRepository = new SimpleRuleRepository();
  41. private ExportRuleStep underTest = new ExportRuleStep(ruleRepository, dumpWriter);
  42. @Test
  43. public void getDescription_is_set() {
  44. assertThat(underTest.getDescription()).isEqualTo("Export rules");
  45. }
  46. @Test
  47. public void execute_writes_no_rules_when_repository_is_empty() {
  48. underTest.execute(new TestComputationStepContext());
  49. assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.RULES)).isEmpty();
  50. }
  51. @Test
  52. public void execute_writes_all_rules_in_order_returned_by_repository() {
  53. String[] keys = new String[10];
  54. for (int i = 0; i < 10; i++) {
  55. String key = "key_" + i;
  56. ruleRepository.add(key);
  57. keys[i] = key;
  58. }
  59. underTest.execute(new TestComputationStepContext());
  60. List<ProjectDump.Rule> rules = dumpWriter.getWrittenMessagesOf(DumpElement.RULES);
  61. assertThat(rules).extracting(ProjectDump.Rule::getKey).containsExactly(keys);
  62. assertThat(rules).extracting(ProjectDump.Rule::getRepository).containsOnly(REPOSITORY);
  63. }
  64. @Test
  65. public void execute_logs_number_total_exported_rules_count_when_successful() {
  66. logTester.setLevel(Level.DEBUG);
  67. ruleRepository.add("A").add("B").add("C").add("D");
  68. underTest.execute(new TestComputationStepContext());
  69. assertThat(logTester.logs(Level.DEBUG)).containsExactly("4 rules exported");
  70. }
  71. @Test
  72. public void excuse_throws_ISE_exception_with_number_of_successfully_exported_rules() {
  73. ruleRepository.add("A").add("B").add("C")
  74. // will cause NPE
  75. .addNull();
  76. assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
  77. .isInstanceOf(IllegalStateException.class)
  78. .hasMessage("Rule Export failed after processing 3 rules successfully");
  79. }
  80. private static class SimpleRuleRepository implements RuleRepository {
  81. private List<Rule> rules = new ArrayList<>();
  82. @Override
  83. public Rule register(String uuid, RuleKey ruleKey) {
  84. throw new UnsupportedOperationException("getByRuleKey not implemented");
  85. }
  86. public SimpleRuleRepository add(String key) {
  87. this.rules.add(new Rule(Uuids.createFast(), REPOSITORY, key));
  88. return this;
  89. }
  90. public SimpleRuleRepository addNull() {
  91. this.rules.add(null);
  92. return this;
  93. }
  94. @Override
  95. public Collection<Rule> getAll() {
  96. return rules;
  97. }
  98. }
  99. }