@Test public void test_built_in_quality_profile() { BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); underTest.define(context); BuiltInQualityProfile profile = context.profile("xoo", "test BuiltInQualityProfilesDefinition"); assertThat(profile.isDefault()).isFalse(); assertThat(profile.name()).isEqualTo("test BuiltInQualityProfilesDefinition"); assertThat(profile.language()).isEqualTo("xoo"); assertThat(profile.rules()).hasSize(1); BuiltInQualityProfilesDefinition.BuiltInActiveRule activeRule = profile.rule(RuleKey.of("xoo", "HasTag")); assertThat(activeRule.overriddenSeverity()).isEqualTo("BLOCKER"); assertThat(activeRule.overriddenParams()).hasSize(1); assertThat(activeRule.overriddenParam("tag").overriddenValue()).isEqualTo("TODO"); } }
@Test public void bridgeProfileDefinitions() { BuiltInQProfileDefinitionsBridge bridge = new BuiltInQProfileDefinitionsBridge(new Profile1(), new NullProfile(), new ProfileWithError()); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); bridge.define(context); assertThat(context.profilesByLanguageAndName()).hasSize(1); assertThat(context.profilesByLanguageAndName().get("xoo")).hasSize(1); BuiltInQualityProfile profile1 = context.profile("xoo", "Profile 1"); assertThat(profile1).isNotNull(); assertThat(profile1.rules()).hasSize(3); BuiltInActiveRule defaultSeverity = profile1.rule(RuleKey.of("repo1", "defaultSeverity")); assertThat(defaultSeverity).isNotNull(); assertThat(defaultSeverity.overriddenSeverity()).isNull(); assertThat(defaultSeverity.overriddenParams()).isEmpty(); assertThat(profile1.rule(RuleKey.of("repo1", "overrideSeverity")).overriddenSeverity()).isEqualTo(Severity.CRITICAL); assertThat(profile1.rule(RuleKey.of("repo1", "overrideParam")).overriddenParams()) .extracting(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue).containsOnly(tuple("param", "value")); }
@Test public void coverage() { assertThat(new BuiltInQualityProfilesDefinition.Context().profile("Foo", "xoo")).isNull(); }
@Test public void should_create_sonar_way_profile() { ValidationMessages validation = ValidationMessages.create(); BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context(); new SonarWayProfile().define(context); assertThat(context.profilesByLanguageAndName()).hasSize(1); BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile("go", "Sonar way"); assertThat(profile.language()).isEqualTo("go"); assertThat(profile.name()).isEqualTo("Sonar way"); assertThat(profile.rules()).extracting("repoKey").containsOnly(GoRulesDefinition.REPOSITORY_KEY); assertThat(validation.hasErrors()).isFalse(); assertThat(profile.rules()).extracting("ruleKey").contains("S2068"); assertThat(profile.rules()).extracting("ruleKey").doesNotContain("S3801"); }
@Test public void activate_deactivate_and_update_three_rules_at_the_same_time() { RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("xoo")); RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("xoo")); RuleDefinitionDto rule3 = 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); // rule1 must be updated (blocker to critical) // rule2 must be activated // rule3 must be deactivated activateRuleInDb(persistedProfile, rule1, BLOCKER); activateRuleInDb(persistedProfile, rule3, BLOCKER); underTest.update(db.getSession(), builtIn, persistedProfile); List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile); assertThat(activeRules).hasSize(2); assertThatRuleIsUpdated(activeRules, rule1, CRITICAL); assertThatRuleIsNewlyActivated(activeRules, rule2, MAJOR); assertThatRuleIsDeactivated(activeRules, rule3); assertThatProfileIsMarkedAsUpdated(persistedProfile); }
@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 insert_active_rules_and_changelog() { OrganizationDto org = db.organizations().insert(); 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("the name", "xoo"); newQp.activateRule(rule1.getRepositoryKey(), rule1.getRuleKey()).overrideSeverity(Severity.CRITICAL); newQp.activateRule(rule2.getRepositoryKey(), rule2.getRuleKey()).overrideSeverity(Severity.MAJOR); newQp.done(); BuiltInQProfile builtIn = builtInQProfileRepository.create(context.profile("xoo", "the name"), rule1, rule2); call(builtIn); verifyTableSize("rules_profiles", 1); verifyTableSize("active_rules", 2); verifyTableSize("active_rule_parameters", 0); verifyTableSize("qprofile_changes", 2); QProfileDto profile = verifyProfileInDb(org, builtIn); verifyActiveRuleInDb(profile, rule1, Severity.CRITICAL); verifyActiveRuleInDb(profile, rule2, Severity.MAJOR); }
@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 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 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 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); }
@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()); }
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 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 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(); }
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); }
@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()); }