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.

ListActionIT.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.server.rule.ws;
  21. import java.text.ParseException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Map;
  24. import java.util.Optional;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.api.resources.Languages;
  28. import org.sonar.api.rule.RuleKey;
  29. import org.sonar.api.server.ws.WebService;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.qualityprofile.QProfileDto;
  33. import org.sonar.db.rule.RuleDto;
  34. import org.sonar.db.rule.RuleTesting;
  35. import org.sonar.server.exceptions.NotFoundException;
  36. import org.sonar.server.language.LanguageTesting;
  37. import org.sonar.server.rule.RuleDescriptionFormatter;
  38. import org.sonar.server.tester.UserSessionRule;
  39. import org.sonar.server.text.MacroInterpreter;
  40. import org.sonar.server.ws.TestRequest;
  41. import org.sonar.server.ws.WsActionTester;
  42. import org.sonarqube.ws.Rules;
  43. import static org.assertj.core.api.Assertions.assertThat;
  44. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  45. import static org.mockito.Mockito.mock;
  46. public class ListActionIT {
  47. private static final String RULE_KEY_1 = "java:S001";
  48. private static final String RULE_KEY_2 = "java:S002";
  49. @Rule
  50. public DbTester db = DbTester.create(System2.INSTANCE);
  51. @org.junit.Rule
  52. public UserSessionRule userSession = UserSessionRule.standalone();
  53. private final Languages languages = LanguageTesting.newLanguages("java", "js");
  54. private final MacroInterpreter macroInterpreter = mock(MacroInterpreter.class);
  55. private final RuleMapper ruleMapper = new RuleMapper(languages, macroInterpreter, new RuleDescriptionFormatter());
  56. private final RuleWsSupport ruleWsSupport = new RuleWsSupport(db.getDbClient(), userSession);
  57. private final RulesResponseFormatter rulesResponseFormatter = new RulesResponseFormatter(db.getDbClient(), ruleWsSupport, ruleMapper, languages);
  58. private final WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(), rulesResponseFormatter));
  59. @Test
  60. public void define_shouldDefineParameters() {
  61. WebService.Action def = ws.getDef();
  62. assertThat(def.params()).extracting(WebService.Param::key)
  63. .containsExactlyInAnyOrder("asc", "p", "s", "ps", "available_since", "qprofile");
  64. }
  65. @Test
  66. public void execute_shouldReturnRules() {
  67. db.rules().insert(RuleTesting.newRule(RuleKey.parse(RULE_KEY_1)).setConfigKey(null).setName(null));
  68. db.rules().insert(RuleTesting.newRule(RuleKey.parse(RULE_KEY_2)).setConfigKey("I002").setName("Rule Two"));
  69. db.getSession().commit();
  70. Rules.ListResponse listResponse = ws.newRequest()
  71. .executeProtobuf(Rules.ListResponse.class);
  72. assertThat(listResponse.getRulesCount()).isEqualTo(2);
  73. Rules.Rule ruleS001 = getRule(listResponse, RULE_KEY_1);
  74. assertThat(ruleS001.getKey()).isEqualTo(RULE_KEY_1);
  75. assertThat(ruleS001.getInternalKey()).isEmpty();
  76. assertThat(ruleS001.getName()).isEmpty();
  77. Rules.Rule ruleS002 = getRule(listResponse, RULE_KEY_2);
  78. assertThat(ruleS002.getKey()).isEqualTo(RULE_KEY_2);
  79. assertThat(ruleS002.getInternalKey()).isEqualTo("I002");
  80. assertThat(ruleS002.getName()).isEqualTo("Rule Two");
  81. }
  82. @Test
  83. public void execute_shouldReturnFilteredRules_whenQProfileDefined() {
  84. QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage("java"));
  85. RuleDto rule = db.rules().insert(r -> r.setRuleKey(RuleKey.parse(RULE_KEY_1)));
  86. db.qualityProfiles().activateRule(profile, rule);
  87. Rules.ListResponse result = ws.newRequest()
  88. .setParam("qprofile", profile.getKee())
  89. .executeProtobuf(Rules.ListResponse.class);
  90. assertThat(result.getPaging().getTotal()).isOne();
  91. assertThat(result.getPaging().getPageIndex()).isOne();
  92. assertThat(result.getRulesCount()).isOne();
  93. assertThat(result.getActives()).isNotNull();
  94. Map<String, Rules.ActiveList> activeRules = result.getActives().getActivesMap();
  95. assertThat(activeRules.get(rule.getKey().toString())).isNotNull();
  96. assertThat(activeRules.get(rule.getKey().toString()).getActiveListList()).hasSize(1);
  97. }
  98. @Test
  99. public void execute_shouldReturnFilteredRules_whenAvailableSinceDefined() throws ParseException {
  100. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  101. long recentDay = dateFormat.parse("2023-02-20").getTime();
  102. long oldDay = dateFormat.parse("2022-09-17").getTime();
  103. db.rules().insert(r -> r.setRuleKey(RuleKey.parse(RULE_KEY_1)).setCreatedAt(recentDay));
  104. db.rules().insert(r -> r.setRuleKey(RuleKey.parse(RULE_KEY_2)).setCreatedAt(oldDay));
  105. Rules.ListResponse result = ws.newRequest()
  106. .setParam("available_since", "2022-11-23")
  107. .executeProtobuf(Rules.ListResponse.class);
  108. assertThat(result.getRulesCount()).isOne();
  109. assertThat(result.getRulesList().stream().map(Rules.Rule::getKey)).containsOnly(RULE_KEY_1);
  110. }
  111. @Test
  112. public void execute_shouldFailWithNotFoundException_whenQProfileDoesNotExist() {
  113. String unknownProfile = "unknown_profile";
  114. TestRequest request = ws.newRequest()
  115. .setParam("qprofile", unknownProfile);
  116. assertThatThrownBy(() -> request.executeProtobuf(Rules.SearchResponse.class))
  117. .isInstanceOf(NotFoundException.class)
  118. .hasMessage("The specified qualityProfile '" + unknownProfile + "' does not exist");
  119. }
  120. private Rules.Rule getRule(Rules.ListResponse listResponse, String ruleKey) {
  121. Optional<Rules.Rule> rule = listResponse.getRulesList().stream()
  122. .filter(r -> ruleKey.equals(r.getKey()))
  123. .findFirst();
  124. assertThat(rule).isPresent();
  125. return rule.get();
  126. }
  127. }