@Test public void createEmptyProfile() { Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> { c.createBuiltInQualityProfile("Foo", "xoo").done(); }); assertThat(profiles).containsOnlyKeys("xoo"); assertThat(profiles.get("xoo")).containsOnlyKeys("Foo"); BuiltInQualityProfile profile = profiles.get("xoo").get("Foo"); assertThat(profile.name()).isEqualTo("Foo"); assertThat(profile.language()).isEqualTo("xoo"); assertThat(profile.isDefault()).isFalse(); }
@Test public void createDefaultProfile() { Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> { c.createBuiltInQualityProfile("Foo", "xoo") .setDefault(true) .done(); }); assertThat(profiles).containsOnlyKeys("xoo"); assertThat(profiles.get("xoo")).containsOnlyKeys("Foo"); BuiltInQualityProfile profile = profiles.get("xoo").get("Foo"); assertThat(profile.name()).isEqualTo("Foo"); assertThat(profile.language()).isEqualTo("xoo"); assertThat(profile.isDefault()).isTrue(); }
@Test public void shouldParseOnlyWantedProfile() { BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newProfile = context.createBuiltInQualityProfile("Foo way", "java"); new BuiltInQualityProfileAnnotationLoader().load(newProfile, "squid", FakeRule.class, RuleOnOtherProfile.class, RuleNoProfile.class); newProfile.done(); assertThat(context.profile("java", "Foo way").rule(RuleKey.of("squid", "fake"))).isNotNull(); assertThat(context.profile("java", "Foo way").rule(RuleKey.of("squid", "other"))).isNull(); } }
@Test public void activate_rule_on_built_in_profile_resets_params_to_default_if_not_overridden() { RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo")); RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10")); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo"); newQp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()); newQp.done(); BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile("xoo", "Sonar way"), rule); underTest.update(db.getSession(), builtIn, persistedProfile); List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThat(activeRules).hasSize(1); assertThatRuleHasParams(db, activeRules.get(0), tuple("min", "10")); // emulate an upgrade of analyzer that changes the default value of parameter min ruleParam.setDefaultValue("20"); db.getDbClient().ruleDao().updateRuleParam(db.getSession(), rule, ruleParam); underTest.update(db.getSession(), builtIn, persistedProfile); activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThat(activeRules).hasSize(1); assertThatRuleHasParams(db, activeRules.get(0), tuple("min", "20")); }
@Test public void deactivate_rule_that_is_not_in_built_in_definition_anymore() { RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("xoo")); RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("xoo")); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo"); newQp.activateRule(rule2.getRepositoryKey(), rule2.getRuleKey()).overrideSeverity(Severity.MAJOR); newQp.done(); BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile("xoo", "Sonar way"), rule1, rule2); // built-in definition contains only rule2 // so rule1 must be deactivated activateRuleInDb(persistedProfile, rule1, CRITICAL); underTest.update(db.getSession(), builtIn, persistedProfile); List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThat(activeRules).hasSize(1); assertThatRuleIsDeactivated(activeRules, rule1); assertThatProfileIsMarkedAsUpdated(persistedProfile); }
@Test public void activate_rule_on_built_in_profile_resets_severity_to_default_if_not_overridden() { RuleDefinitionDto rule = db.rules().insert(r -> r.setSeverity(Severity.MAJOR).setLanguage("xoo")); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo"); newQp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()); newQp.done(); BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile("xoo", "Sonar way"), rule); underTest.update(db.getSession(), builtIn, persistedProfile); List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThatRuleIsNewlyActivated(activeRules, rule, MAJOR); // emulate an upgrade of analyzer that changes the default severity of the rule rule.setSeverity(Severity.MINOR); db.rules().update(rule); underTest.update(db.getSession(), builtIn, persistedProfile); activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThatRuleIsNewlyActivated(activeRules, rule, MINOR); }
@Test public void activate_new_rules() { RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("xoo")); RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("xoo")); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo"); newQp.activateRule(rule1.getRepositoryKey(), rule1.getRuleKey()).overrideSeverity(Severity.CRITICAL); newQp.activateRule(rule2.getRepositoryKey(), rule2.getRuleKey()).overrideSeverity(Severity.MAJOR); newQp.done(); BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile("xoo", "Sonar way"), rule1, rule2); underTest.update(db.getSession(), builtIn, persistedProfile); List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThat(activeRules).hasSize(2); assertThatRuleIsNewlyActivated(activeRules, rule1, CRITICAL); assertThatRuleIsNewlyActivated(activeRules, rule2, MAJOR); assertThatProfileIsMarkedAsUpdated(persistedProfile); }
@Test public void already_activated_rule_is_not_touched_if_no_differences() { RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo")); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo"); newQp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()).overrideSeverity(Severity.CRITICAL); newQp.done(); BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile("xoo", "Sonar way"), rule); activateRuleInDb(persistedProfile, rule, CRITICAL); underTest.update(db.getSession(), builtIn, persistedProfile); List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThat(activeRules).hasSize(1); assertThatRuleIsUntouched(activeRules, rule, CRITICAL); assertThatProfileIsNotMarkedAsUpdated(persistedProfile); }
private static void define(Context context, RulesProfile profile) { NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(profile.getName(), profile.getLanguage()) .setDefault(profile.getDefaultProfile().booleanValue()); for (org.sonar.api.rules.ActiveRule ar : profile.getActiveRules()) { NewBuiltInActiveRule newActiveRule = newQp.activateRule(ar.getRepositoryKey(), ar.getRuleKey()); RulePriority overriddenSeverity = ar.getOverriddenSeverity(); if (overriddenSeverity != null) { newActiveRule.overrideSeverity(overriddenSeverity.name()); } for (ActiveRuleParam param : ar.getActiveRuleParams()) { newActiveRule.overrideParam(param.getKey(), param.getValue()); } } newQp.done(); }
@Test public void already_activated_rule_is_updated_in_case_of_differences() { RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo")); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo"); newQp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()).overrideSeverity(Severity.CRITICAL); newQp.done(); BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile("xoo", "Sonar way"), rule); activateRuleInDb(persistedProfile, rule, BLOCKER); underTest.update(db.getSession(), builtIn, persistedProfile); List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThat(activeRules).hasSize(1); assertThatRuleIsUpdated(activeRules, rule, CRITICAL); assertThatProfileIsMarkedAsUpdated(persistedProfile); }
@Test public void existing_default_profile_in_organization_must_not_be_changed() { BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("the name", "xoo").setDefault(true); newQp.done(); BuiltInQProfile builtIn = builtInQProfileRepository.create(context.profile("xoo", "the name")); OrganizationDto org = db.organizations().insert(); QProfileDto currentDefault = db.qualityProfiles().insert(org, p -> p.setLanguage("xoo")); db.qualityProfiles().setAsDefault(currentDefault); call(builtIn); QProfileDto defaultProfile = db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, org, "xoo"); assertThat(defaultProfile.getKee()).isEqualTo(currentDefault.getKee()); }
@Override public void define(Context context) { context.createBuiltInQualityProfile(name, language) .setDefault(defaultProfile).done(); }
@Test public void flag_profile_as_default_on_organization_if_declared_as_default_by_api() { OrganizationDto org = db.organizations().insert(); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("the name", "xoo").setDefault(true); newQp.done(); BuiltInQProfile builtIn = builtInQProfileRepository.create(context.profile("xoo", "the name")); call(builtIn); QProfileDto profile = verifyProfileInDb(org, builtIn); QProfileDto defaultProfile = db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, org, "xoo"); assertThat(defaultProfile.getKee()).isEqualTo(profile.getKee()); }
@Override public void define(Context context) { NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile("test BuiltInQualityProfilesDefinition", Xoo.KEY); profile.setDefault(false); NewBuiltInActiveRule rule = profile.activateRule(XOO_REPOSITORY, HasTagSensor.RULE_KEY); rule.overrideSeverity("BLOCKER"); rule.overrideParam("tag", "TODO"); profile.done(); } }
private void addPluginProfile(QProfileDto profile, RuleDefinitionDto... dbRules) { BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(profile.getName(), profile.getLanguage()); Arrays.stream(dbRules).forEach(dbRule -> newQp.activateRule(dbRule.getRepositoryKey(), dbRule.getRuleKey()).overrideSeverity(Severity.MAJOR)); newQp.done(); BuiltInQProfile.ActiveRule[] activeRules = toActiveRules(context.profile(profile.getLanguage(), profile.getName()).rules(), dbRules); builtInQProfileRepositoryRule.add(newLanguage(profile.getLanguage()), profile.getName(), false, activeRules); }
private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) { BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(dbProfile.getName(), dbProfile.getLanguage()); Arrays.stream(dbRules).forEach(dbRule -> newQp.activateRule(dbRule.getRepositoryKey(), dbRule.getRuleKey()).overrideSeverity(Severity.MAJOR)); newQp.done(); List<BuiltInActiveRule> rules = context.profile(dbProfile.getLanguage(), dbProfile.getName()).rules(); BuiltInQProfile.ActiveRule[] activeRules = toActiveRules(rules, dbRules); builtInQProfileRepositoryRule.add(newLanguage(dbProfile.getLanguage()), dbProfile.getName(), false, activeRules); }
@Test public void shouldParseAnnotatedClasses() { BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newProfile = context.createBuiltInQualityProfile("Foo way", "java"); new BuiltInQualityProfileAnnotationLoader().load(newProfile, "squid", FakeRule.class, RuleNoProfile.class); newProfile.done(); assertThat(context.profile("java", "Foo way").rule(RuleKey.of("squid", "fake")).overriddenSeverity()).isEqualTo(Severity.BLOCKER); }
@Test public void dont_flag_profile_as_default_on_organization_if_not_declared_as_default_by_api() { OrganizationDto org = db.organizations().insert(); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("the name", "xoo").setDefault(false); newQp.done(); BuiltInQProfile builtIn = builtInQProfileRepository.create(context.profile("xoo", "the name")); call(builtIn); QProfileDto defaultProfile = db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, org, "xoo"); assertThat(defaultProfile).isNull(); }
@Test public void createProfileWithDuplicateRules() { define(c -> { NewBuiltInQualityProfile profile = c.createBuiltInQualityProfile("Foo", "xoo"); profile.activateRule("repo", "rule"); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("The rule 'repo:rule' is already activated"); profile.activateRule("repo", "rule"); }); }
@Test public void duplicateProfile() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("There is already a quality profile with name 'Foo' for language 'xoo'"); define(c -> { c.createBuiltInQualityProfile("Foo", "xoo").done(); c.createBuiltInQualityProfile("Foo", "xoo").done(); }); }