From 1c92ea547e7a91585f419279dbf655cb44cc3164 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 28 Jun 2016 00:03:53 +0200 Subject: [PATCH] SONAR-7794 Update db column RULES_PROFILES.USER_UPDATED_AT when a user action is done --- .../server/qualityprofile/RuleActivator.java | 19 +++++--- .../RuleActivatorMediumTest.java | 47 +++++++++++++++++-- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java index 4e1bc6ae9bd..96a923b6970 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java @@ -46,6 +46,7 @@ import org.sonar.server.exceptions.BadRequestException; 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; @@ -63,10 +64,11 @@ 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; @@ -74,6 +76,7 @@ public class RuleActivator { this.typeValidations = typeValidations; this.activeRuleIndexer = activeRuleIndexer; this.activityService = activityService; + this.userSession = userSession; } public List activate(DbSession dbSession, RuleActivation activation, String profileKey) { @@ -144,14 +147,18 @@ public class RuleActivator { } 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); } /** @@ -373,7 +380,7 @@ public class RuleActivator { } if (!changes.isEmpty()) { - updateProfileDate(dbSession, context); + updateProfileDates(dbSession, context); } return changes; diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java index 9d4fa754ed0..a515b80bb2b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java @@ -33,6 +33,7 @@ import org.junit.Test; 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; @@ -165,12 +166,32 @@ public class RuleActivatorMediumTest { 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 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 changes = ruleActivator.activate(dbSession, activation, profileDto); dbSession.commit(); dbSession.clearCache(); @@ -180,6 +201,7 @@ public class RuleActivatorMediumTest { ImmutableMap.of("max", "7", "min", "3")); assertThat(changes).hasSize(1); assertThat(changes.get(0).getType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED); + assertProfileHasBeenUpdatedManually(profileDto.getKey()); } @Test @@ -467,12 +489,14 @@ public class RuleActivatorMediumTest { 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 @@ -571,6 +595,7 @@ public class RuleActivatorMediumTest { @Test public void propagate_activation_update_on_child_profiles() { createChildProfiles(); + userSessionRule.login(); // activate on root profile RuleActivation activation = new RuleActivation(XOO_X1); @@ -610,6 +635,10 @@ public class RuleActivatorMediumTest { 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 @@ -910,14 +939,14 @@ public class RuleActivatorMediumTest { 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.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.emptyMap()); + verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, null, Collections.emptyMap()); } @Test @@ -993,7 +1022,7 @@ public class RuleActivatorMediumTest { 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.emptyMap()); + verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, INHERITED, Collections.emptyMap()); } @Test @@ -1138,4 +1167,16 @@ public class RuleActivatorMediumTest { .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(); + } } -- 2.39.5