Skip to content

Commit

Permalink
SONAR-7794 Update db column RULES_PROFILES.USER_UPDATED_AT when a use…
Browse files Browse the repository at this point in the history
…r action is done
  • Loading branch information
teryk committed Jun 29, 2016
1 parent 55f378b commit 1c92ea5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
Expand Up @@ -46,6 +46,7 @@
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.user.UserSession;
import org.sonar.server.util.TypeValidations;

import static com.google.common.collect.Lists.newArrayList;
Expand All @@ -63,17 +64,19 @@ public class RuleActivator {
private final RuleIndex ruleIndex;
private final ActiveRuleIndexer activeRuleIndexer;
private final ActivityService activityService;
private final UserSession userSession;

public RuleActivator(System2 system2, DbClient db, RuleIndex ruleIndex,
RuleActivatorContextFactory contextFactory, TypeValidations typeValidations,
ActiveRuleIndexer activeRuleIndexer, ActivityService activityService) {
ActiveRuleIndexer activeRuleIndexer, ActivityService activityService, UserSession userSession) {
this.system2 = system2;
this.db = db;
this.ruleIndex = ruleIndex;
this.contextFactory = contextFactory;
this.typeValidations = typeValidations;
this.activeRuleIndexer = activeRuleIndexer;
this.activityService = activityService;
this.userSession = userSession;
}

public List<ActiveRuleChange> activate(DbSession dbSession, RuleActivation activation, String profileKey) {
Expand Down Expand Up @@ -144,14 +147,18 @@ private List<ActiveRuleChange> doActivate(DbSession dbSession, RuleActivation ac
}

if (!changes.isEmpty()) {
updateProfileDate(dbSession, context);
updateProfileDates(dbSession, context);
}
return changes;
}

private void updateProfileDate(DbSession dbSession, RuleActivatorContext context) {
context.profile().setRulesUpdatedAtAsDate(context.getInitDate());
db.qualityProfileDao().update(dbSession, context.profile());
private void updateProfileDates(DbSession dbSession, RuleActivatorContext context) {
QualityProfileDto profile = context.profile();
profile.setRulesUpdatedAtAsDate(context.getInitDate());
if (userSession.isLoggedIn()) {
profile.setUserUpdatedAt(context.getInitDate().getTime());
}
db.qualityProfileDao().update(dbSession, profile);
}

/**
Expand Down Expand Up @@ -373,7 +380,7 @@ private List<ActiveRuleChange> cascadeDeactivation(ActiveRuleKey key, DbSession
}

if (!changes.isEmpty()) {
updateProfileDate(dbSession, context);
updateProfileDates(dbSession, context);
}

return changes;
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleDto;
Expand Down Expand Up @@ -165,12 +166,32 @@ public void activate() {
assertThat(changes.get(0).getType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED);
}

@Test
public void automatic_activation_does_not_update_intended_column() {
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activation.setParameter("min", "3");
List<ActiveRuleChange> changes = ruleActivator.activate(dbSession, activation, XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
userSessionRule.anonymous();

assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), BLOCKER, null,
ImmutableMap.of("max", "7", "min", "3"));
assertThat(changes).hasSize(1);
assertThat(changes.get(0).getType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED);
assertProfileHasBeenUpdatedAutomatically(XOO_P1_KEY);
}

@Test
public void activate_with_profile_dto() {
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activation.setParameter("min", "3");
userSessionRule.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
List<ActiveRuleChange> changes = ruleActivator.activate(dbSession, activation, profileDto);
dbSession.commit();
dbSession.clearCache();
Expand All @@ -180,6 +201,7 @@ public void activate_with_profile_dto() {
ImmutableMap.of("max", "7", "min", "3"));
assertThat(changes).hasSize(1);
assertThat(changes.get(0).getType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED);
assertProfileHasBeenUpdatedManually(profileDto.getKey());
}

@Test
Expand Down Expand Up @@ -467,12 +489,14 @@ public void deactivate() {
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
userSessionRule.login();
activate(activation, XOO_P1_KEY);

// deactivation
ruleActivator.deactivate(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1));

verifyZeroActiveRules(XOO_P1_KEY);
assertProfileHasBeenUpdatedManually(XOO_P1_KEY);
}

@Test
Expand Down Expand Up @@ -571,6 +595,7 @@ public void propagate_activation_on_child_profiles() {
@Test
public void propagate_activation_update_on_child_profiles() {
createChildProfiles();
userSessionRule.login();

// activate on root profile
RuleActivation activation = new RuleActivation(XOO_X1);
Expand Down Expand Up @@ -610,6 +635,10 @@ public void propagate_activation_update_on_child_profiles() {
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, MINOR, OVERRIDES, ImmutableMap.of("max", "9"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, OVERRIDES, ImmutableMap.of("max", "10"));

assertProfileHasBeenUpdatedManually(XOO_P1_KEY);
assertProfileHasBeenUpdatedManually(XOO_P2_KEY);
assertProfileHasBeenUpdatedManually(XOO_P3_KEY);
}

@Test
Expand Down Expand Up @@ -910,14 +939,14 @@ public void set_and_unset_parent_profile() {
assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P2_KEY).getParentKee()).isEqualTo(XOO_P1_KEY);

verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X1), MAJOR, INHERITED, ImmutableMap.of("max", "10"));
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, null, Collections.<String, String>emptyMap());
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, null, Collections.emptyMap());

// unset parent
dbSession.clearCache();
ruleActivator.setParent(XOO_P2_KEY, null);
assertThat(countActiveRules(XOO_P2_KEY)).isEqualTo(1);
assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P2_KEY).getParentKee()).isNull();
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, null, Collections.<String, String>emptyMap());
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, null, Collections.emptyMap());
}

@Test
Expand Down Expand Up @@ -993,7 +1022,7 @@ public void ignore_activation_errors_when_setting_parent() {

assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P2_KEY).getParentKee()).isEqualTo(XOO_P1_KEY);
assertThat(countActiveRules(XOO_P2_KEY)).isEqualTo(1);
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, INHERITED, Collections.<String, String>emptyMap());
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, INHERITED, Collections.emptyMap());
}

@Test
Expand Down Expand Up @@ -1138,4 +1167,16 @@ private void verifyZeroActiveRules(String key) {
.setQProfileKey(key)
.setActivation(true))).isEmpty();
}

private void assertProfileHasBeenUpdatedManually(String profileKey) {
QualityProfileDto profile = db.qualityProfileDao().selectByKey(dbSession, profileKey);
assertThat(profile.getRulesUpdatedAt()).isNotEmpty();
assertThat(profile.getUserUpdatedAt()).isNotNull();
}

private void assertProfileHasBeenUpdatedAutomatically(String profileKey) {
QualityProfileDto profile = db.qualityProfileDao().selectByKey(dbSession, profileKey);
assertThat(profile.getRulesUpdatedAt()).isNotEmpty();
assertThat(profile.getUserUpdatedAt()).isNull();
}
}

0 comments on commit 1c92ea5

Please sign in to comment.