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.collect.ImmutableMap;
23 import java.util.Collections;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.rule.Severity;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.qualityprofile.ActiveRuleDto;
30 import org.sonar.db.qualityprofile.QProfileChangeDto;
31 import org.sonar.db.qualityprofile.QProfileChangeQuery;
32 import org.sonar.db.qualityprofile.QProfileDto;
33 import org.sonar.db.rule.RuleDefinitionDto;
34 import org.sonar.db.user.UserDto;
35 import org.sonar.server.es.EsTester;
36 import org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService;
37 import org.sonar.server.qualityprofile.builtin.RuleActivator;
38 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
39 import org.sonar.server.rule.index.RuleIndex;
40 import org.sonar.server.tester.UserSessionRule;
41 import org.sonar.server.util.IntegerTypeValidation;
42 import org.sonar.server.util.TypeValidations;
44 import static java.util.Collections.singleton;
45 import static java.util.Collections.singletonList;
46 import static org.assertj.core.api.Assertions.assertThat;
47 import static org.assertj.core.api.Assertions.tuple;
48 import static org.mockito.ArgumentMatchers.any;
49 import static org.mockito.ArgumentMatchers.eq;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.verify;
53 public class QProfileRulesImplTest {
56 public UserSessionRule userSession = UserSessionRule.standalone();
58 public DbTester db = DbTester.create();
60 public EsTester es = EsTester.create();
62 private RuleIndex ruleIndex = new RuleIndex(es.client(), System2.INSTANCE);
63 private ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(db.getDbClient(), es.client());
64 private RuleActivator ruleActivator = new RuleActivator(System2.INSTANCE, db.getDbClient(), new TypeValidations(singletonList(new IntegerTypeValidation())),
66 private QualityProfileChangeEventService qualityProfileChangeEventService = mock(QualityProfileChangeEventService.class);
68 private QProfileRules qProfileRules = new QProfileRulesImpl(db.getDbClient(), ruleActivator, ruleIndex, activeRuleIndexer, qualityProfileChangeEventService);
71 public void activate_one_rule() {
72 QProfileDto qProfile = db.qualityProfiles().insert();
73 RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(qProfile.getLanguage()));
74 RuleActivation ruleActivation = RuleActivation.create(rule.getUuid(), Severity.CRITICAL, Collections.emptyMap());
76 qProfileRules.activateAndCommit(db.getSession(), qProfile, singleton(ruleActivation));
78 assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), qProfile))
79 .extracting(ActiveRuleDto::getRuleKey, ActiveRuleDto::getSeverityString)
80 .containsExactlyInAnyOrder(tuple(rule.getKey(), Severity.CRITICAL));
81 verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(qProfile.getLanguage()));
85 public void active_rule_change() {
86 UserDto user = db.users().insertUser();
87 userSession.logIn(user);
88 QProfileDto qProfile = db.qualityProfiles().insert();
89 RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(qProfile.getLanguage()));
90 RuleActivation ruleActivation = RuleActivation.create(rule.getUuid(), Severity.CRITICAL, Collections.emptyMap());
92 qProfileRules.activateAndCommit(db.getSession(), qProfile, singleton(ruleActivation));
94 assertThat(db.getDbClient().qProfileChangeDao().selectByQuery(db.getSession(), new QProfileChangeQuery(qProfile.getKee())))
95 .extracting(QProfileChangeDto::getUserUuid, QProfileChangeDto::getDataAsMap)
96 .containsExactlyInAnyOrder(tuple(user.getUuid(), ImmutableMap.of("ruleUuid", rule.getUuid(), "severity", Severity.CRITICAL)));
97 verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(qProfile.getLanguage()));