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);
}
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"));
}
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);
}
});
}
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);
}
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());
}
Aggregations