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.

ActiveRulesPublisherTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.scanner.report;
  21. import java.io.File;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.TemporaryFolder;
  25. import org.sonar.api.batch.rule.ActiveRules;
  26. import org.sonar.api.batch.rule.internal.DefaultActiveRules;
  27. import org.sonar.api.batch.rule.internal.NewActiveRule;
  28. import org.sonar.api.rule.RuleKey;
  29. import org.sonar.core.util.CloseableIterator;
  30. import org.sonar.scanner.protocol.Constants;
  31. import org.sonar.scanner.protocol.output.ScannerReport;
  32. import org.sonar.scanner.protocol.output.ScannerReportReader;
  33. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  34. import static java.util.Collections.singletonList;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. public class ActiveRulesPublisherTest {
  37. @Rule
  38. public TemporaryFolder temp = new TemporaryFolder();
  39. @Test
  40. public void write() throws Exception {
  41. File outputDir = temp.newFolder();
  42. ScannerReportWriter writer = new ScannerReportWriter(outputDir);
  43. NewActiveRule ar = new NewActiveRule.Builder()
  44. .setRuleKey(RuleKey.of("java", "S001"))
  45. .setSeverity("BLOCKER")
  46. .setParam("p1", "v1")
  47. .setCreatedAt(1_000L)
  48. .setUpdatedAt(2_000L)
  49. .setQProfileKey("qp1")
  50. .build();
  51. ActiveRules activeRules = new DefaultActiveRules(singletonList(ar));
  52. ActiveRulesPublisher underTest = new ActiveRulesPublisher(activeRules);
  53. underTest.publish(writer);
  54. ScannerReportReader reader = new ScannerReportReader(outputDir);
  55. try (CloseableIterator<ScannerReport.ActiveRule> readIt = reader.readActiveRules()) {
  56. ScannerReport.ActiveRule reportAr = readIt.next();
  57. assertThat(reportAr.getRuleRepository()).isEqualTo("java");
  58. assertThat(reportAr.getRuleKey()).isEqualTo("S001");
  59. assertThat(reportAr.getSeverity()).isEqualTo(Constants.Severity.BLOCKER);
  60. assertThat(reportAr.getCreatedAt()).isEqualTo(1_000L);
  61. assertThat(reportAr.getUpdatedAt()).isEqualTo(2_000L);
  62. assertThat(reportAr.getQProfileKey()).isEqualTo("qp1");
  63. assertThat(reportAr.getParamsByKeyMap()).hasSize(1);
  64. assertThat(reportAr.getParamsByKeyMap().entrySet().iterator().next().getKey()).isEqualTo("p1");
  65. assertThat(reportAr.getParamsByKeyMap().entrySet().iterator().next().getValue()).isEqualTo("v1");
  66. assertThat(readIt.hasNext()).isFalse();
  67. }
  68. }
  69. }