Search in sources :

Example 6 with RuleSetChangedEvent

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

the class SonarLintClientsRegistryTest method listen_givenOneClientInterestedInJsEventsAndJavaEventGenerated_sendZeroEvents.

@Test
public void listen_givenOneClientInterestedInJsEventsAndJavaEventGenerated_sendZeroEvents() throws IOException {
    Set<String> jsLanguageKey = Set.of("js");
    when(defaultAsyncContext.getResponse()).thenReturn(response);
    when(response.getOutputStream()).thenReturn(outputStream);
    SonarLintClient sonarLintClient = new SonarLintClient(defaultAsyncContext, exampleKeys, jsLanguageKey, USER_UUID);
    underTest.registerClient(sonarLintClient);
    RuleChange javaRuleChange = createRuleChange();
    RuleChange[] activatedRules = {};
    RuleChange[] deactivatedRules = { javaRuleChange };
    RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
    underTest.listen(ruleSetChangedEvent);
    verifyNoInteractions(outputStream);
}
Also used : RuleChange(org.sonar.core.util.RuleChange) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

Example 7 with RuleSetChangedEvent

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

the class SonarLintClientsRegistryTest method listen_givenOneClientInterestedInJavaEvents_sendOneJavaEvent.

@Test
public void listen_givenOneClientInterestedInJavaEvents_sendOneJavaEvent() throws IOException {
    Set<String> javaLanguageKey = Set.of("java");
    when(defaultAsyncContext.getResponse()).thenReturn(response);
    when(response.getOutputStream()).thenReturn(outputStream);
    SonarLintClient sonarLintClient = new SonarLintClient(defaultAsyncContext, exampleKeys, javaLanguageKey, USER_UUID);
    underTest.registerClient(sonarLintClient);
    RuleChange javaRule = createRuleChange();
    RuleChange[] activatedRules = { javaRule };
    RuleChange[] deactivatedRules = { javaRule };
    RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
    underTest.listen(ruleSetChangedEvent);
    ArgumentCaptor<byte[]> captor = ArgumentCaptor.forClass(byte[].class);
    verify(outputStream).write(captor.capture());
    String message = new String(captor.getValue());
    assertThatEvent(message).hasType("RuleSetChanged").hasJsonData(getClass().getResource("rule-change-event-data.json"));
}
Also used : RuleChange(org.sonar.core.util.RuleChange) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

Example 8 with RuleSetChangedEvent

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

the class SonarLintClientsRegistry method broadcastMessage.

public void broadcastMessage(RuleSetChangedEvent event, Predicate<SonarLintClient> filter) {
    clients.stream().filter(filter).forEach(c -> {
        Set<String> projectKeysInterestingForClient = new HashSet<>(c.getClientProjectKeys());
        projectKeysInterestingForClient.retainAll(Set.of(event.getProjects()));
        try {
            sonarLintClientPermissionsValidator.validateUserCanReceivePushEventForProjects(c.getUserUuid(), projectKeysInterestingForClient);
            RuleSetChangedEvent personalizedEvent = new RuleSetChangedEvent(projectKeysInterestingForClient.toArray(String[]::new), event.getActivatedRules(), event.getDeactivatedRules());
            String message = getMessage(personalizedEvent);
            c.writeAndFlush(message);
        } catch (ForbiddenException forbiddenException) {
            LOG.debug("Client is no longer authenticated: " + forbiddenException.getMessage());
            unregisterClient(c);
        } catch (IllegalStateException | IOException e) {
            LOG.error("Unable to send message to a client: " + e.getMessage());
            unregisterClient(c);
        }
    });
}
Also used : ForbiddenException(org.sonar.server.exceptions.ForbiddenException) IOException(java.io.IOException) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) HashSet(java.util.HashSet)

Example 9 with RuleSetChangedEvent

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

the class QualityProfileChangeEventServiceImpl method distributeRuleChangeEvent.

public void distributeRuleChangeEvent(Collection<QProfileDto> profiles, List<ActiveRuleChange> activeRuleChanges, String language) {
    if (activeRuleChanges.isEmpty()) {
        return;
    }
    Set<RuleChange> activatedRules = new HashSet<>();
    Set<RuleChange> deactivatedRules = new HashSet<>();
    for (ActiveRuleChange arc : activeRuleChanges) {
        ActiveRuleDto activeRule = arc.getActiveRule();
        if (activeRule == null) {
            continue;
        }
        RuleChange ruleChange = new RuleChange();
        ruleChange.setKey(activeRule.getRuleKey().rule());
        ruleChange.setSeverity(arc.getSeverity());
        ruleChange.setLanguage(language);
        Optional<String> templateKey = templateKey(arc);
        templateKey.ifPresent(ruleChange::setTemplateKey);
        // params
        List<ParamChange> paramChanges = new ArrayList<>();
        for (Map.Entry<String, String> entry : arc.getParameters().entrySet()) {
            paramChanges.add(new ParamChange(entry.getKey(), entry.getValue()));
        }
        ruleChange.setParams(paramChanges.toArray(new ParamChange[0]));
        switch(arc.getType()) {
            case ACTIVATED:
            case UPDATED:
                activatedRules.add(ruleChange);
                break;
            case DEACTIVATED:
                deactivatedRules.add(ruleChange);
                break;
        }
    }
    Set<String> projectKeys = getProjectKeys(profiles);
    if (activatedRules.isEmpty() && deactivatedRules.isEmpty()) {
        return;
    }
    RuleSetChangedEvent event = new RuleSetChangedEvent(projectKeys.toArray(new String[0]), activatedRules.toArray(new RuleChange[0]), deactivatedRules.toArray(new RuleChange[0]));
    eventsDistributor.pushEvent(event);
}
Also used : ParamChange(org.sonar.core.util.ParamChange) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) ArrayList(java.util.ArrayList) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleChange(org.sonar.core.util.RuleChange) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) Map(java.util.Map) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) HashSet(java.util.HashSet)

Example 10 with RuleSetChangedEvent

use of org.sonar.core.util.RuleSetChangedEvent 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

RuleSetChangedEvent (org.sonar.core.util.RuleSetChangedEvent)10 Test (org.junit.Test)7 RuleChange (org.sonar.core.util.RuleChange)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ParamChange (org.sonar.core.util.ParamChange)3 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)3 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ProjectDto (org.sonar.db.project.ProjectDto)2 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)2 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)2 ForbiddenException (org.sonar.server.exceptions.ForbiddenException)2 Map (java.util.Map)1 Set (java.util.Set)1 ArgumentMatchers.anySet (org.mockito.ArgumentMatchers.anySet)1 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)1 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)1 RuleDto (org.sonar.db.rule.RuleDto)1