use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.
the class SearchActionMediumTest method search_all_active_rules_params.
@Test
public void search_all_active_rules_params() throws Exception {
QualityProfileDto profile = QProfileTesting.newXooP1("org-123");
tester.get(QualityProfileDao.class).insert(dbSession, profile);
RuleDto rule = RuleTesting.newXooX1();
ruleDao.insert(dbSession, rule);
dbSession.commit();
RuleParamDto param = RuleParamDto.createFor(rule).setDefaultValue("some value").setType("string").setDescription("My small description").setName("my_var");
ruleDao.insertRuleParam(dbSession, rule, param);
RuleParamDto param2 = RuleParamDto.createFor(rule).setDefaultValue("other value").setType("integer").setDescription("My small description").setName("the_var");
ruleDao.insertRuleParam(dbSession, rule, param2);
ActiveRuleDto activeRule = newActiveRule(profile, rule);
tester.get(ActiveRuleDao.class).insert(dbSession, activeRule);
ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param).setValue("The VALUE");
tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam);
ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2).setValue("The Other Value");
tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam2);
dbSession.commit();
ruleIndexer.index();
activeRuleIndexer.index();
WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
request.setParam(WebService.Param.TEXT_QUERY, "x1");
request.setParam(PARAM_ACTIVATION, "true");
request.setParam(WebService.Param.FIELDS, "params");
WsTester.Result result = request.execute();
result.assertJson(this.getClass(), "search_active_rules_params.json");
}
use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.
the class RuleUpdaterMediumTest method update_active_rule_parameters_when_updating_custom_rule.
@Test
public void update_active_rule_parameters_when_updating_custom_rule() {
// Create template rule with 3 parameters
RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo");
ruleDao.insert(dbSession, templateRule);
RuleParamDto templateRuleParam1 = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
RuleParamDto templateRuleParam2 = RuleParamDto.createFor(templateRule).setName("format").setType("STRING").setDescription("format").setDefaultValue("csv");
ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);
RuleParamDto templateRuleParam3 = RuleParamDto.createFor(templateRule).setName("message").setType("STRING").setDescription("message");
ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam3);
// Create custom rule
RuleDto customRule = RuleTesting.newCustomRule(templateRule).setSeverity(Severity.MAJOR).setLanguage("xoo");
ruleDao.insert(dbSession, customRule);
ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue("txt"));
ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam3);
// Create a quality profile
QualityProfileDto profileDto = QProfileTesting.newXooP1("org-123");
db.qualityProfileDao().insert(dbSession, profileDto);
dbSession.commit();
// Activate the custom rule
RuleActivation activation = new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER);
tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME);
dbSession.commit();
dbSession.clearCache();
// Update custom rule parameter 'regex', add 'message' and remove 'format'
RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()).setParameters(ImmutableMap.of("regex", "b.*", "message", "a message"));
underTest.update(update, userSessionRule);
dbSession.clearCache();
// Verify custom rule parameters has been updated
List<RuleParamDto> params = ruleDao.selectRuleParamsByRuleKey(dbSession, customRule.getKey());
assertThat(params).hasSize(3);
Map<String, RuleParamDto> paramsByKey = paramsByKey(params);
assertThat(paramsByKey.get("regex")).isNotNull();
assertThat(paramsByKey.get("regex").getDefaultValue()).isEqualTo("b.*");
assertThat(paramsByKey.get("message")).isNotNull();
assertThat(paramsByKey.get("message").getDefaultValue()).isEqualTo("a message");
assertThat(paramsByKey.get("format")).isNotNull();
assertThat(paramsByKey.get("format").getDefaultValue()).isNull();
// Verify that severity has not changed
ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, ActiveRuleKey.of(profileDto.getKey(), customRule.getKey()));
assertThat(activeRuleDto.getSeverityString()).isEqualTo(Severity.BLOCKER);
// Verify active rule parameters has been updated
List<ActiveRuleParamDto> activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(activeRuleParams).hasSize(2);
Map<String, ActiveRuleParamDto> activeRuleParamsByKey = ActiveRuleParamDto.groupByKey(activeRuleParams);
assertThat(activeRuleParamsByKey.get("regex").getValue()).isEqualTo("b.*");
assertThat(activeRuleParamsByKey.get("message").getValue()).isEqualTo("a message");
assertThat(activeRuleParamsByKey.get("format")).isNull();
}
use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.
the class QualityProfileChangeEventServiceImplTest method distributeRuleChangeEvent.
@Test
public void distributeRuleChangeEvent() {
QProfileDto qualityProfileDto = QualityProfileTesting.newQualityProfileDto();
// Template rule
RuleDto templateRule = newTemplateRule(RuleKey.of("xoo", "template-key"));
db.rules().insert(templateRule.getDefinition());
// Custom rule
RuleDefinitionDto rule1 = newCustomRule(templateRule.getDefinition()).setLanguage("xoo").setDescription("<div>line1\nline2</div>").setDescriptionFormat(MARKDOWN);
db.rules().insert(rule1);
ActiveRuleDto activeRuleDto = ActiveRuleDto.createFor(qualityProfileDto, rule1);
ActiveRuleChange activeRuleChange = new ActiveRuleChange(ACTIVATED, activeRuleDto, rule1);
activeRuleChange.setParameter("paramChangeKey", "paramChangeValue");
Collection<QProfileDto> profiles = Collections.singleton(qualityProfileDto);
ProjectDto project = db.components().insertPrivateProjectDto();
db.qualityProfiles().associateWithProject(project, qualityProfileDto);
underTest.distributeRuleChangeEvent(profiles, of(activeRuleChange), "xoo");
ArgumentCaptor<RuleSetChangedEvent> eventCaptor = ArgumentCaptor.forClass(RuleSetChangedEvent.class);
verify(eventsDistributor).pushEvent(eventCaptor.capture());
RuleSetChangedEvent ruleSetChangedEvent = eventCaptor.getValue();
assertThat(ruleSetChangedEvent).isNotNull();
assertThat(ruleSetChangedEvent).extracting(RuleSetChangedEvent::getEvent, RuleSetChangedEvent::getLanguage, RuleSetChangedEvent::getProjects).containsExactly("RuleSetChanged", "xoo", new String[] { project.getKey() });
assertThat(ruleSetChangedEvent.getActivatedRules()).extracting(RuleChange::getKey, RuleChange::getLanguage, RuleChange::getSeverity, RuleChange::getTemplateKey).containsExactly(tuple(rule1.getRuleKey(), "xoo", null, "template-key"));
assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
assertThat(actualParamChange).extracting(ParamChange::getKey, ParamChange::getValue).containsExactly("paramChangeKey", "paramChangeValue");
assertThat(ruleSetChangedEvent.getDeactivatedRules()).isEmpty();
}
use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.
the class ActiveRuleIndexerTest method commitAndIndex_keeps_elements_to_recover_in_ES_QUEUE_on_errors.
@Test
public void commitAndIndex_keeps_elements_to_recover_in_ES_QUEUE_on_errors() {
ActiveRuleDto ar = db.qualityProfiles().activateRule(profile1, rule1);
es.lockWrites(TYPE_ACTIVE_RULE);
commitAndIndex(rule1, ar);
EsQueueDto expectedItem = EsQueueDto.create(TYPE_ACTIVE_RULE.format(), "ar_" + ar.getUuid(), "activeRuleUuid", ar.getRuleUuid());
assertThatEsQueueContainsExactly(expectedItem);
es.unlockWrites(TYPE_ACTIVE_RULE);
}
use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.
the class ActiveRuleIndexerTest method test_commitAndIndex.
@Test
public void test_commitAndIndex() {
ActiveRuleDto ar1 = db.qualityProfiles().activateRule(profile1, rule1);
ActiveRuleDto ar2 = db.qualityProfiles().activateRule(profile2, rule1);
db.qualityProfiles().activateRule(profile2, rule2);
commitAndIndex(rule1, ar1, ar2);
verifyOnlyIndexed(ar1, ar2);
assertThatEsQueueTableIsEmpty();
}
Aggregations