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.

DefaultActiveRulesLoaderTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.scanner.rule;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.InputStream;
  23. import java.util.Collection;
  24. import java.util.Map;
  25. import java.util.stream.IntStream;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import org.sonar.api.batch.rule.LoadedActiveRule;
  29. import org.sonar.api.rule.RuleKey;
  30. import org.sonar.api.rule.Severity;
  31. import org.sonar.scanner.WsTestUtil;
  32. import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
  33. import org.sonar.scanner.scan.branch.BranchConfiguration;
  34. import org.sonarqube.ws.Common;
  35. import org.sonarqube.ws.Rules;
  36. import org.sonarqube.ws.Rules.Active;
  37. import org.sonarqube.ws.Rules.ActiveList;
  38. import org.sonarqube.ws.Rules.Actives;
  39. import org.sonarqube.ws.Rules.Rule;
  40. import static org.assertj.core.api.Assertions.assertThat;
  41. import static org.mockito.Mockito.mock;
  42. import static org.mockito.Mockito.verifyNoMoreInteractions;
  43. import static org.mockito.Mockito.when;
  44. public class DefaultActiveRulesLoaderTest {
  45. private static final int PAGE_SIZE_1 = 150;
  46. private static final int PAGE_SIZE_2 = 76;
  47. private static final RuleKey EXAMPLE_KEY = RuleKey.of("java", "S108");
  48. private static final String FORMAT_KEY = "format";
  49. private static final String FORMAT_VALUE = "^[a-z][a-zA-Z0-9]*$";
  50. private static final String SEVERITY_VALUE = Severity.MINOR;
  51. private DefaultActiveRulesLoader loader;
  52. private DefaultScannerWsClient wsClient;
  53. @Before
  54. public void setUp() {
  55. wsClient = mock(DefaultScannerWsClient.class);
  56. BranchConfiguration branchConfig = mock(BranchConfiguration.class);
  57. when(branchConfig.isPullRequest()).thenReturn(false);
  58. loader = new DefaultActiveRulesLoader(wsClient);
  59. }
  60. @Test
  61. public void load_shouldRequestRulesAndParseResponse() {
  62. int total = PAGE_SIZE_1 + PAGE_SIZE_2;
  63. WsTestUtil.mockStream(wsClient, urlOfPage(1), responseOfSize(1, PAGE_SIZE_1, total));
  64. WsTestUtil.mockStream(wsClient, urlOfPage(2), responseOfSize(2, PAGE_SIZE_2, total));
  65. Collection<LoadedActiveRule> activeRules = loader.load("c+-test_c+-values-17445");
  66. assertThat(activeRules).hasSize(total);
  67. assertThat(activeRules)
  68. .filteredOn(r -> r.getRuleKey().equals(EXAMPLE_KEY))
  69. .extracting(LoadedActiveRule::getParams)
  70. .extracting(p -> p.get(FORMAT_KEY))
  71. .containsExactly(FORMAT_VALUE);
  72. assertThat(activeRules)
  73. .filteredOn(r -> r.getRuleKey().equals(EXAMPLE_KEY))
  74. .extracting(LoadedActiveRule::getSeverity)
  75. .containsExactly(SEVERITY_VALUE);
  76. WsTestUtil.verifyCall(wsClient, urlOfPage(1));
  77. WsTestUtil.verifyCall(wsClient, urlOfPage(2));
  78. verifyNoMoreInteractions(wsClient);
  79. }
  80. private String urlOfPage(int page) {
  81. return "/api/rules/list.protobuf?qprofile=c%2B-test_c%2B-values-17445&ps=500&p=" + page + "";
  82. }
  83. /**
  84. * Generates an imaginary protobuf result.
  85. *
  86. * @param pageIndex page index, that the response should contain
  87. * @param numberOfRules the number of rules, that the response should contain
  88. * @param total the number of results on all pages
  89. * @return the binary stream
  90. */
  91. private InputStream responseOfSize(int pageIndex, int numberOfRules, int total) {
  92. Rules.ListResponse.Builder rules = Rules.ListResponse.newBuilder();
  93. Actives.Builder actives = Actives.newBuilder();
  94. IntStream.rangeClosed(1, numberOfRules)
  95. .mapToObj(i -> RuleKey.of("java", "S" + i))
  96. .forEach(key -> {
  97. Rule.Builder ruleBuilder = Rule.newBuilder();
  98. ruleBuilder.setKey(key.toString());
  99. rules.addRules(ruleBuilder);
  100. Active.Builder activeBuilder = Active.newBuilder();
  101. activeBuilder.setCreatedAt("2014-05-27T15:50:45+0100");
  102. activeBuilder.setUpdatedAt("2014-05-27T15:50:45+0100");
  103. if (EXAMPLE_KEY.equals(key)) {
  104. activeBuilder.addParams(Rules.Active.Param.newBuilder().setKey(FORMAT_KEY).setValue(FORMAT_VALUE));
  105. activeBuilder.setSeverity(SEVERITY_VALUE);
  106. }
  107. ActiveList activeList = Rules.ActiveList.newBuilder().addActiveList(activeBuilder).build();
  108. actives.putAllActives(Map.of(key.toString(), activeList));
  109. });
  110. rules.setActives(actives);
  111. rules.setPaging(Common.Paging.newBuilder()
  112. .setTotal(total)
  113. .setPageIndex(pageIndex)
  114. .setPageSize(numberOfRules)
  115. .build());
  116. return new ByteArrayInputStream(rules.build().toByteArray());
  117. }
  118. }