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.

BuiltInQProfileDefinitionsBridgeTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.server.qualityprofile;
  21. import java.util.Arrays;
  22. import org.junit.Test;
  23. import org.sonar.api.profiles.ProfileDefinition;
  24. import org.sonar.api.profiles.RulesProfile;
  25. import org.sonar.api.rule.RuleKey;
  26. import org.sonar.api.rule.Severity;
  27. import org.sonar.api.rules.ActiveRule;
  28. import org.sonar.api.rules.Rule;
  29. import org.sonar.api.rules.RuleParam;
  30. import org.sonar.api.rules.RulePriority;
  31. import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
  32. import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
  33. import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
  34. import org.sonar.api.utils.ValidationMessages;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.assertj.core.groups.Tuple.tuple;
  37. public class BuiltInQProfileDefinitionsBridgeTest {
  38. @Test
  39. public void noProfileDefinitions() {
  40. BuiltInQProfileDefinitionsBridge bridge = new BuiltInQProfileDefinitionsBridge();
  41. BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
  42. bridge.define(context);
  43. assertThat(context.profilesByLanguageAndName()).isEmpty();
  44. }
  45. @Test
  46. public void bridgeProfileDefinitions() {
  47. BuiltInQProfileDefinitionsBridge bridge = new BuiltInQProfileDefinitionsBridge(new Profile1(), new NullProfile(), new ProfileWithError());
  48. BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
  49. bridge.define(context);
  50. assertThat(context.profilesByLanguageAndName()).hasSize(1);
  51. assertThat(context.profilesByLanguageAndName().get("xoo")).hasSize(1);
  52. BuiltInQualityProfile profile1 = context.profile("xoo", "Profile 1");
  53. assertThat(profile1).isNotNull();
  54. assertThat(profile1.rules()).hasSize(3);
  55. BuiltInActiveRule defaultSeverity = profile1.rule(RuleKey.of("repo1", "defaultSeverity"));
  56. assertThat(defaultSeverity).isNotNull();
  57. assertThat(defaultSeverity.overriddenSeverity()).isNull();
  58. assertThat(defaultSeverity.overriddenParams()).isEmpty();
  59. assertThat(profile1.rule(RuleKey.of("repo1", "overrideSeverity")).overriddenSeverity()).isEqualTo(Severity.CRITICAL);
  60. assertThat(profile1.rule(RuleKey.of("repo1", "overrideParam")).overriddenParams())
  61. .extracting(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue).containsOnly(tuple("param", "value"));
  62. }
  63. private static class Profile1 extends ProfileDefinition {
  64. @Override
  65. public RulesProfile createProfile(ValidationMessages validation) {
  66. RulesProfile profile1 = RulesProfile.create("Profile 1", "xoo");
  67. profile1.activateRule(Rule.create("repo1", "defaultSeverity"), null);
  68. profile1.activateRule(Rule.create("repo1", "overrideSeverity"), RulePriority.CRITICAL);
  69. Rule ruleWithParam = Rule.create("repo1", "overrideParam");
  70. ruleWithParam.setParams(Arrays.asList(new RuleParam(ruleWithParam, "param", "", "")));
  71. ActiveRule arWithParam = profile1.activateRule(ruleWithParam, null);
  72. arWithParam.setParameter("param", "value");
  73. return profile1;
  74. }
  75. }
  76. private static class NullProfile extends ProfileDefinition {
  77. @Override
  78. public RulesProfile createProfile(ValidationMessages validation) {
  79. return null;
  80. }
  81. }
  82. private static class ProfileWithError extends ProfileDefinition {
  83. @Override
  84. public RulesProfile createProfile(ValidationMessages validation) {
  85. validation.addErrorText("Foo");
  86. return RulesProfile.create("Profile with errors", "xoo");
  87. }
  88. }
  89. }