Search in sources :

Example 6 with ParamChange

use of org.sonar.core.util.ParamChange in project sonarqube by SonarSource.

the class QualityProfileChangeEventServiceImplTest method publishRuleActivationToSonarLintClients.

@Test
public void publishRuleActivationToSonarLintClients() {
    ProjectDto projectDao = new ProjectDto();
    QProfileDto activatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
    db.qualityProfiles().insert(activatedQualityProfile);
    RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("xoo"));
    RuleParamDto rule1Param = db.rules().insertRuleParam(rule1);
    ActiveRuleDto activeRule1 = db.qualityProfiles().activateRule(activatedQualityProfile, rule1);
    ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param).setValue(randomAlphanumeric(20));
    db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule1, activeRuleParam1);
    db.getSession().commit();
    QProfileDto deactivatedQualityProfile = QualityProfileTesting.newQualityProfileDto();
    db.qualityProfiles().insert(deactivatedQualityProfile);
    RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("xoo"));
    RuleParamDto rule2Param = db.rules().insertRuleParam(rule2);
    ActiveRuleDto activeRule2 = db.qualityProfiles().activateRule(deactivatedQualityProfile, rule2);
    ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule2Param).setValue(randomAlphanumeric(20));
    db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule2, activeRuleParam2);
    db.getSession().commit();
    underTest.publishRuleActivationToSonarLintClients(projectDao, activatedQualityProfile, deactivatedQualityProfile);
    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[] { null });
    // activated rule
    assertThat(ruleSetChangedEvent.getActivatedRules()).extracting(RuleChange::getKey, RuleChange::getLanguage, RuleChange::getSeverity, RuleChange::getTemplateKey).containsExactly(tuple(rule1.getRuleKey(), "xoo", rule1.getSeverityString(), null));
    assertThat(ruleSetChangedEvent.getActivatedRules()[0].getParams()).hasSize(1);
    ParamChange actualParamChange = ruleSetChangedEvent.getActivatedRules()[0].getParams()[0];
    assertThat(actualParamChange).extracting(ParamChange::getKey, ParamChange::getValue).containsExactly(activeRuleParam1.getKey(), activeRuleParam1.getValue());
    // deactivated rule
    assertThat(ruleSetChangedEvent.getDeactivatedRules()).extracting(RuleChange::getKey, RuleChange::getLanguage, RuleChange::getSeverity, RuleChange::getTemplateKey).containsExactly(tuple(rule2.getRuleKey(), "xoo", rule2.getSeverityString(), null));
    assertThat(ruleSetChangedEvent.getDeactivatedRules()[0].getParams()).hasSize(1);
    ParamChange actualParamChangeDeactivated = ruleSetChangedEvent.getDeactivatedRules()[0].getParams()[0];
    assertThat(actualParamChangeDeactivated).extracting(ParamChange::getKey, ParamChange::getValue).containsExactly(activeRuleParam2.getKey(), activeRuleParam2.getValue());
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ParamChange(org.sonar.core.util.ParamChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

Aggregations

ParamChange (org.sonar.core.util.ParamChange)6 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)4 RuleChange (org.sonar.core.util.RuleChange)3 RuleSetChangedEvent (org.sonar.core.util.RuleSetChangedEvent)3 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ProjectDto (org.sonar.db.project.ProjectDto)2 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)2 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)2 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)2 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)2 RuleDto (org.sonar.db.rule.RuleDto)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 DbSession (org.sonar.db.DbSession)1 RuleParamDto (org.sonar.db.rule.RuleParamDto)1