3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualityprofile;
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.ImmutableList;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
28 import org.junit.rules.ExternalResource;
29 import org.sonar.api.resources.Language;
30 import org.sonar.api.rule.RuleKey;
31 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
32 import org.sonar.core.util.stream.MoreCollectors;
33 import org.sonar.db.rule.RuleDefinitionDto;
35 import static com.google.common.base.Preconditions.checkState;
37 public class BuiltInQProfileRepositoryRule extends ExternalResource implements BuiltInQProfileRepository {
38 private boolean initializeCalled = false;
39 private List<BuiltInQProfile> profiles = new ArrayList<>();
42 protected void before() {
43 this.initializeCalled = false;
44 this.profiles.clear();
48 public void initialize() {
49 checkState(!initializeCalled, "initialize must be called only once");
50 this.initializeCalled = true;
54 public List<BuiltInQProfile> get() {
55 checkState(initializeCalled, "initialize must be called first");
57 return ImmutableList.copyOf(profiles);
60 public boolean isInitialized() {
61 return initializeCalled;
64 public BuiltInQProfile add(Language language, String profileName) {
65 return add(language, profileName, false);
68 public BuiltInQProfile add(Language language, String profileName, boolean isDefault) {
69 return add(language, profileName, isDefault, new BuiltInQProfile.ActiveRule[0]);
72 public BuiltInQProfile add(Language language, String profileName, boolean isDefault, BuiltInQProfile.ActiveRule... rules) {
73 BuiltInQProfile builtIn = create(language, profileName, isDefault, rules);
74 profiles.add(builtIn);
78 public BuiltInQProfile create(Language language, String profileName, boolean isDefault, BuiltInQProfile.ActiveRule... rules) {
79 BuiltInQProfile.Builder builder = new BuiltInQProfile.Builder()
80 .setLanguage(language.getKey())
82 .setDeclaredDefault(isDefault);
83 Arrays.stream(rules).forEach(builder::addRule);
84 return builder.build();
87 public BuiltInQProfile create(BuiltInQualityProfilesDefinition.BuiltInQualityProfile api, RuleDefinitionDto... rules) {
88 BuiltInQProfile.Builder builder = new BuiltInQProfile.Builder()
89 .setLanguage(api.language())
91 .setDeclaredDefault(api.isDefault());
92 Map<RuleKey, RuleDefinitionDto> rulesByRuleKey = Arrays.stream(rules)
93 .collect(MoreCollectors.uniqueIndex(RuleDefinitionDto::getKey));
94 api.rules().forEach(rule -> {
95 RuleKey ruleKey = RuleKey.of(rule.repoKey(), rule.ruleKey());
96 RuleDefinitionDto ruleDefinition = rulesByRuleKey.get(ruleKey);
97 Preconditions.checkState(ruleDefinition != null, "Rule '%s' not found", ruleKey);
98 builder.addRule(new BuiltInQProfile.ActiveRule(ruleDefinition.getUuid(), rule));