import com.google.common.collect.Multimap;
import java.util.Arrays;
import java.util.Random;
+import java.util.function.Consumer;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.rule.Severity;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.RulePriority;
import org.sonar.api.utils.System2;
import static org.sonar.server.language.LanguageTesting.newLanguage;
import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
+import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED;
public class RegisterQualityProfilesNotificationTest {
.extracting(QProfileName::getName, QProfileName::getLanguage)
.containsExactlyInAnyOrder(
tuple(dbProfile1.getName(), dbProfile1.getLanguage()),
- tuple(dbProfile2.getName(), dbProfile2.getLanguage())
- );
+ tuple(dbProfile2.getName(), dbProfile2.getLanguage()));
assertThat(updatedProfiles.values())
.extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
.containsExactlyInAnyOrder(
tuple(newRule1.getId(), ACTIVATED),
- tuple(newRule2.getId(), ACTIVATED)
- );
+ tuple(newRule2.getId(), ACTIVATED));
}
@Test
- public void do_not_include_inherited_quality_profile_changes() {
+ public void do_not_include_inherited_quality_profile_change_on_new_rule() {
String language = newLanguageKey();
+ RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
+ OrganizationDto organization = db.organizations().insert();
+
+ QProfileDto builtInQProfileDto = insertProfile(organization, orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
+ QProfileDto childQProfileDto = insertProfile(organization, orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
+ addPluginProfile(builtInQProfileDto, newRule);
+ builtInQProfileRepositoryRule.initialize();
+
+ underTest.start();
+ ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
+ verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
+ Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
+ assertThat(updatedProfiles.keySet())
+ .extracting(QProfileName::getName, QProfileName::getLanguage)
+ .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
+ assertThat(updatedProfiles.values())
+ .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
+ .containsExactlyInAnyOrder(tuple(newRule.getId(), ACTIVATED));
+ }
+
+ @Test
+ public void do_not_include_inherited_quality_profile_change_on_existing_rule() {
+ String language = newLanguageKey();
+ RuleDefinitionDto ruleDefinitionDto = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
OrganizationDto organization = db.organizations().insert();
- QProfileDto builtInQProfileDto = db.qualityProfiles().insert(organization, orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
- QProfileDto customQProfileDto = db.qualityProfiles().insert(organization, orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
+
+ QProfileDto builtInQProfileDto = insertProfile(organization, orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
+ ruleActivator.activate(db.getSession(), RuleActivation.create(ruleDefinitionDto.getKey()), builtInQProfileDto);
+ QProfileDto childQProfileDto = insertProfile(organization, orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
+ ruleActivator.activate(db.getSession(), RuleActivation.create(ruleDefinitionDto.getKey()), childQProfileDto);
db.commit();
- RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
+ addPluginProfile(builtInQProfileDto, ruleDefinitionDto);
+ builtInQProfileRepositoryRule.initialize();
+
+ underTest.start();
+
+ ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
+ verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
+ Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.<Multimap<QProfileName, ActiveRuleChange>>getValue();
+ assertThat(updatedProfiles.keySet())
+ .extracting(QProfileName::getName, QProfileName::getLanguage)
+ .containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
+ assertThat(updatedProfiles.values())
+ .extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
+ .containsExactlyInAnyOrder(tuple(ruleDefinitionDto.getId(), UPDATED));
+ }
- RulesProfile pluginProfile = RulesProfile.create(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage());
- pluginProfile.activateRule(create(newRule.getRepositoryKey(), newRule.getRuleKey()), MAJOR);
- builtInQProfileRepositoryRule.add(newLanguage(builtInQProfileDto.getLanguage()), builtInQProfileDto.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
+ @Test
+ public void do_not_include_inherited_quality_profile_change_on_deactivated_rule() {
+ String language = newLanguageKey();
+ RuleDefinitionDto ruleDefinitionDto = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
+ OrganizationDto organization = db.organizations().insert();
+
+ QProfileDto builtInQProfileDto = insertProfile(organization,
+ orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
+ ruleActivator.activate(db.getSession(), RuleActivation.create(ruleDefinitionDto.getKey()), builtInQProfileDto);
+ QProfileDto childQProfileDto = insertProfile(organization,
+ orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
+ ruleActivator.activate(db.getSession(), RuleActivation.create(ruleDefinitionDto.getKey()), childQProfileDto);
+ db.commit();
+
+ addPluginProfile(builtInQProfileDto);
builtInQProfileRepositoryRule.initialize();
underTest.start();
.containsExactlyInAnyOrder(tuple(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
assertThat(updatedProfiles.values())
.extracting(value -> value.getActiveRule().getRuleId(), ActiveRuleChange::getType)
- .containsExactlyInAnyOrder(tuple(newRule.getId(), ACTIVATED));
+ .containsExactlyInAnyOrder(tuple(ruleDefinitionDto.getId(), DEACTIVATED));
}
@Test
private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) {
RulesProfile pluginProfile = RulesProfile.create(dbProfile.getName(), dbProfile.getLanguage());
- Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), MAJOR));
+ Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), null));
builtInQProfileRepositoryRule.add(newLanguage(dbProfile.getLanguage()), dbProfile.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
}
+ private void addPluginProfile(QProfileDto profile, RuleDefinitionDto... dbRules) {
+ RulesProfile pluginProfile = RulesProfile.create(profile.getName(), profile.getLanguage());
+ Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), null));
+ builtInQProfileRepositoryRule.add(newLanguage(profile.getLanguage()), profile.getName(), false, pluginProfile.getActiveRules().toArray(new ActiveRule[0]));
+ }
+
private RulesProfileDto insertBuiltInProfile(String language) {
RulesProfileDto ruleProfileDto = newRuleProfileDto(rp -> rp.setIsBuiltIn(true).setLanguage(language));
db.getDbClient().qualityProfileDao().insert(db.getSession(), ruleProfileDto);
db.commit();
}
+ private QProfileDto insertProfile(OrganizationDto organization, Consumer<QProfileDto> consumer) {
+ QProfileDto builtInQProfileDto = db.qualityProfiles().insert(organization, consumer);
+ db.commit();
+ return builtInQProfileDto;
+ }
+
private static String newLanguageKey() {
return randomAlphanumeric(20).toLowerCase();
}
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.permission.AddGroupWsRequest;
import org.sonarqube.ws.client.permission.AddUserWsRequest;
+import org.sonarqube.ws.client.qualityprofile.ChangeParentRequest;
+import org.sonarqube.ws.client.qualityprofile.CreateRequest;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
import util.ItUtils;
.setServerProperty("email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort()))
.build();
orchestrator.start();
-
userRule = UserRule.from(orchestrator);
-
WsUsers.CreateWsResponse.User profileAdmin1 = userRule.generate();
WsClient wsClient = ItUtils.newAdminWsClient(orchestrator);
wsClient.permissions().addUser(new AddUserWsRequest().setLogin(profileAdmin1.getLogin()).setPermission("profileadmin"));
- WsUsers.CreateWsResponse.User profileAdmin2 = userRule.generate();
- String groupName = randomAlphanumeric(20);
- wsClient.wsConnector().call(new PostRequest("api/user_groups/create").setParam("name", groupName)).failIfNotSuccessful();
- wsClient.permissions().addGroup(new AddGroupWsRequest().setPermission("profileadmin").setGroupName(groupName));
- wsClient.wsConnector().call(new PostRequest("api/user_groups/add_user").setParam("name", groupName).setParam("login", profileAdmin2.getLogin())).failIfNotSuccessful();
-
- WsUsers.CreateWsResponse.User noProfileAdmin = userRule.generate();
-
orchestrator.restartServer();
waitUntilAllNotificationsAreDelivered(1, 10, 100);
userRule = UserRule.from(orchestrator);
+ // Create a quality profile administrator (user having direct permission)
WsUsers.CreateWsResponse.User profileAdmin1 = userRule.generate();
WsClient wsClient = ItUtils.newAdminWsClient(orchestrator);
wsClient.permissions().addUser(new AddUserWsRequest().setLogin(profileAdmin1.getLogin()).setPermission("profileadmin"));
-
+ // Create a quality profile administrator (user having permission from a group)
WsUsers.CreateWsResponse.User profileAdmin2 = userRule.generate();
String groupName = randomAlphanumeric(20);
wsClient.wsConnector().call(new PostRequest("api/user_groups/create").setParam("name", groupName)).failIfNotSuccessful();
wsClient.permissions().addGroup(new AddGroupWsRequest().setPermission("profileadmin").setGroupName(groupName));
wsClient.wsConnector().call(new PostRequest("api/user_groups/add_user").setParam("name", groupName).setParam("login", profileAdmin2.getLogin())).failIfNotSuccessful();
-
+ // Create a user not being quality profile administrator
WsUsers.CreateWsResponse.User noProfileAdmin = userRule.generate();
+ // Create a child profile on the built-in profile => The notification should not take into account updates of this profile
+ wsClient.qualityProfiles().create(CreateRequest.builder().setLanguage("foo").setProfileName("child").build());
+ wsClient.qualityProfiles().changeParent(ChangeParentRequest.builder().setProfileName("child").setParentName("Basic").setLanguage("foo").build());
+
// uninstall plugin V1
wsClient.wsConnector().call(new PostRequest("api/plugins/uninstall").setParam("key", "foo")).failIfNotSuccessful();
-
// install plugin V2
File pluginsDir = new File(orchestrator.getServer().getHome() + "/extensions/plugins");
orchestrator.getConfiguration().fileSystem().copyToDirectory(pluginArtifact("foo-plugin-v2"), pluginsDir);
.setServerProperty("email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort()))
.build();
orchestrator.start();
-
userRule = UserRule.from(orchestrator);
-
WsUsers.CreateWsResponse.User profileAdmin1 = userRule.generate();
WsClient wsClient = ItUtils.newAdminWsClient(orchestrator);
wsClient.permissions().addUser(new AddUserWsRequest().setLogin(profileAdmin1.getLogin()).setPermission("profileadmin"));
- WsUsers.CreateWsResponse.User profileAdmin2 = userRule.generate();
- String groupName = randomAlphanumeric(20);
- wsClient.wsConnector().call(new PostRequest("api/user_groups/create").setParam("name", groupName)).failIfNotSuccessful();
- wsClient.permissions().addGroup(new AddGroupWsRequest().setPermission("profileadmin").setGroupName(groupName));
- wsClient.wsConnector().call(new PostRequest("api/user_groups/add_user").setParam("name", groupName).setParam("login", profileAdmin2.getLogin())).failIfNotSuccessful();
-
- WsUsers.CreateWsResponse.User noProfileAdmin = userRule.generate();
-
// uninstall plugin V1
wsClient.wsConnector().call(new PostRequest("api/plugins/uninstall").setParam("key", "foo")).failIfNotSuccessful();
-
// install plugin V2
File pluginsDir = new File(orchestrator.getServer().getHome() + "/extensions/plugins");
orchestrator.getConfiguration().fileSystem().copyToDirectory(pluginArtifact("foo-plugin-v2"), pluginsDir);