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.

QProfileRulesImplTest.java 4.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 com.google.common.collect.ImmutableMap;
  22. import java.util.Collections;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.rule.Severity;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.db.DbTester;
  28. import org.sonar.db.organization.OrganizationDto;
  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.qualityprofile.index.ActiveRuleIndexer;
  37. import org.sonar.server.rule.index.RuleIndex;
  38. import org.sonar.server.tester.UserSessionRule;
  39. import org.sonar.server.util.IntegerTypeValidation;
  40. import org.sonar.server.util.TypeValidations;
  41. import static java.util.Collections.singleton;
  42. import static java.util.Collections.singletonList;
  43. import static org.assertj.core.api.Assertions.assertThat;
  44. import static org.assertj.core.api.Assertions.tuple;
  45. public class QProfileRulesImplTest {
  46. @Rule
  47. public UserSessionRule userSession = UserSessionRule.standalone();
  48. @Rule
  49. public DbTester db = DbTester.create();
  50. @Rule
  51. public EsTester es = EsTester.create();
  52. private RuleIndex ruleIndex = new RuleIndex(es.client(), System2.INSTANCE);
  53. private ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(db.getDbClient(), es.client());
  54. private RuleActivator ruleActivator = new RuleActivator(System2.INSTANCE, db.getDbClient(), new TypeValidations(singletonList(new IntegerTypeValidation())), userSession);
  55. private QProfileRules qProfileRules = new QProfileRulesImpl(db.getDbClient(), ruleActivator, ruleIndex, activeRuleIndexer);
  56. @Test
  57. public void activate_one_rule() {
  58. OrganizationDto organization = db.organizations().insert();
  59. QProfileDto qProfile = db.qualityProfiles().insert(organization);
  60. RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(qProfile.getLanguage()));
  61. RuleActivation ruleActivation = RuleActivation.create(rule.getId(), Severity.CRITICAL, Collections.emptyMap());
  62. qProfileRules.activateAndCommit(db.getSession(), qProfile, singleton(ruleActivation));
  63. assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), qProfile))
  64. .extracting(ActiveRuleDto::getRuleKey, ActiveRuleDto::getSeverityString)
  65. .containsExactlyInAnyOrder(tuple(rule.getKey(), Severity.CRITICAL));
  66. }
  67. @Test
  68. public void active_rule_change() {
  69. UserDto user = db.users().insertUser();
  70. userSession.logIn(user);
  71. OrganizationDto organization = db.organizations().insert();
  72. QProfileDto qProfile = db.qualityProfiles().insert(organization);
  73. RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(qProfile.getLanguage()));
  74. RuleActivation ruleActivation = RuleActivation.create(rule.getId(), Severity.CRITICAL, Collections.emptyMap());
  75. qProfileRules.activateAndCommit(db.getSession(), qProfile, singleton(ruleActivation));
  76. assertThat(db.getDbClient().qProfileChangeDao().selectByQuery(db.getSession(), new QProfileChangeQuery(qProfile.getKee())))
  77. .extracting(QProfileChangeDto::getUserUuid, QProfileChangeDto::getDataAsMap)
  78. .containsExactlyInAnyOrder(tuple(user.getUuid(), ImmutableMap.of("ruleId", Integer.toString(rule.getId()), "severity", Severity.CRITICAL)));
  79. }
  80. }