@Test public void createProfileWithRules() { Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> { NewBuiltInQualityProfile profile = c.createBuiltInQualityProfile("Foo", "xoo"); profile.activateRule("repo", "ruleWithoutParam"); profile.activateRule("repo", "ruleWithSeverity").overrideSeverity("CRITICAL"); profile.activateRule("repo", "ruleWithParam").overrideParam("param", "value"); profile.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(); assertThat(profile.rules()) .extracting(BuiltInQualityProfilesDefinition.BuiltInActiveRule::repoKey, BuiltInQualityProfilesDefinition.BuiltInActiveRule::ruleKey, BuiltInQualityProfilesDefinition.BuiltInActiveRule::overriddenSeverity, r -> r.overriddenParams().size()) .containsOnly( tuple("repo", "ruleWithoutParam", null, 0), tuple("repo", "ruleWithSeverity", "CRITICAL", 0), tuple("repo", "ruleWithParam", null, 1)); assertThat(profile.rule(RuleKey.of("repo", "ruleWithParam")).overriddenParam("param").key()).isEqualTo("param"); assertThat(profile.rule(RuleKey.of("repo", "ruleWithParam")).overriddenParam("param").overriddenValue()).isEqualTo("value"); }
@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")); }
private static BuiltInQProfile.ActiveRule[] toActiveRules(List<BuiltInActiveRule> rules, RuleDefinitionDto[] dbRules) { Map<RuleKey, RuleDefinitionDto> dbRulesByRuleKey = Arrays.stream(dbRules) .collect(MoreCollectors.uniqueIndex(RuleDefinitionDto::getKey)); return rules.stream() .map(r -> { RuleKey ruleKey = RuleKey.of(r.repoKey(), r.ruleKey()); RuleDefinitionDto ruleDefinitionDto = dbRulesByRuleKey.get(ruleKey); checkState(ruleDefinitionDto != null, "Rule '%s' not found", ruleKey); return new BuiltInQProfile.ActiveRule(ruleDefinitionDto.getId(), r); }).toArray(BuiltInQProfile.ActiveRule[]::new); }
private ActiveRuleChange insertActiveRule(DbSession dbSession, RulesProfileDto rulesProfileDto, BuiltInQProfile.ActiveRule activeRule, long now) { RuleKey ruleKey = activeRule.getRuleKey(); RuleDefinitionDto ruleDefinitionDto = ruleRepository.getDefinition(ruleKey) .orElseThrow(() -> new IllegalStateException("RuleDefinition not found for key " + ruleKey)); ActiveRuleDto dto = new ActiveRuleDto(); dto.setProfileId(rulesProfileDto.getId()); dto.setRuleId(ruleDefinitionDto.getId()); dto.setKey(ActiveRuleKey.of(rulesProfileDto, ruleDefinitionDto.getKey())); dto.setSeverity(firstNonNull(activeRule.getBuiltIn().overriddenSeverity(), ruleDefinitionDto.getSeverityString())); dto.setUpdatedAt(now); dto.setCreatedAt(now); dbClient.activeRuleDao().insert(dbSession, dto); List<ActiveRuleParamDto> paramDtos = insertActiveRuleParams(dbSession, activeRule, dto); ActiveRuleChange change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, dto, ruleDefinitionDto); change.setSeverity(dto.getSeverityString()); paramDtos.forEach(paramDto -> change.setParameter(paramDto.getKey(), paramDto.getValue())); return change; }
private List<ActiveRuleParamDto> insertActiveRuleParams(DbSession session, BuiltInQProfile.ActiveRule activeRule, ActiveRuleDto activeRuleDto) { Map<String, String> valuesByParamKey = activeRule.getBuiltIn().overriddenParams() .stream() .collect(MoreCollectors.uniqueIndex(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue)); return ruleRepository.getRuleParams(activeRule.getRuleKey()) .stream() .map(param -> { String activeRuleValue = valuesByParamKey.get(param.getName()); return createParamDto(param, activeRuleValue == null ? param.getDefaultValue() : activeRuleValue); }) .filter(Objects::nonNull) .peek(paramDto -> dbClient.activeRuleDao().insertParam(session, activeRuleDto, paramDto)) .collect(MoreCollectors.toList()); }
private static RuleActivation convert(BuiltInQProfile.ActiveRule ar) { Map<String, String> params = ar.getBuiltIn().overriddenParams().stream() .collect(MoreCollectors.uniqueIndex(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue)); return RuleActivation.create(ar.getRuleId(), ar.getBuiltIn().overriddenSeverity(), params); }
private static BuiltInQProfile.Builder updateOrCreateBuilder(String language, @Nullable BuiltInQProfile.Builder existingBuilder, BuiltInQualityProfile builtInProfile, Map<RuleKey, RuleDefinitionDto> rulesByRuleKey) { BuiltInQProfile.Builder builder = createOrReuseBuilder(existingBuilder, language, builtInProfile); builder.setDeclaredDefault(builtInProfile.isDefault()); builtInProfile.rules().forEach(builtInActiveRule -> { RuleKey ruleKey = RuleKey.of(builtInActiveRule.repoKey(), builtInActiveRule.ruleKey()); RuleDefinitionDto ruleDefinition = rulesByRuleKey.get(ruleKey); checkState(ruleDefinition != null, "Rule with key '%s' not found", ruleKey); builder.addRule(builtInActiveRule, ruleDefinition.getId()); }); return builder; }
private BuiltInQualityProfileImpl(NewBuiltInQualityProfileImpl newProfile) { this.name = newProfile.name(); this.language = newProfile.language(); this.isDefault = newProfile.isDefault(); Map<RuleKey, BuiltInActiveRule> ruleBuilder = new HashMap<>(); for (NewBuiltInActiveRule newActiveRule : newProfile.activeRules()) { ruleBuilder.put(RuleKey.of(newActiveRule.repoKey, newActiveRule.ruleKey), new BuiltInActiveRule(newActiveRule)); } this.activeRulesByKey = unmodifiableMap(ruleBuilder); }
private ActiveRuleChange insertActiveRule(DbSession dbSession, RulesProfileDto rulesProfileDto, BuiltInQProfile.ActiveRule activeRule, long now) { RuleKey ruleKey = activeRule.getRuleKey(); RuleDefinitionDto ruleDefinitionDto = ruleRepository.getDefinition(ruleKey) .orElseThrow(() -> new IllegalStateException("RuleDefinition not found for key " + ruleKey)); ActiveRuleDto dto = new ActiveRuleDto(); dto.setProfileId(rulesProfileDto.getId()); dto.setRuleId(ruleDefinitionDto.getId()); dto.setKey(ActiveRuleKey.of(rulesProfileDto, ruleDefinitionDto.getKey())); dto.setSeverity(firstNonNull(activeRule.getBuiltIn().overriddenSeverity(), ruleDefinitionDto.getSeverityString())); dto.setUpdatedAt(now); dto.setCreatedAt(now); dbClient.activeRuleDao().insert(dbSession, dto); List<ActiveRuleParamDto> paramDtos = insertActiveRuleParams(dbSession, activeRule, dto); ActiveRuleChange change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, dto, ruleDefinitionDto); change.setSeverity(dto.getSeverityString()); paramDtos.forEach(paramDto -> change.setParameter(paramDto.getKey(), paramDto.getValue())); return change; }
public BuiltInQProfile create(BuiltInQualityProfilesDefinition.BuiltInQualityProfile api, RuleDefinitionDto... rules) { BuiltInQProfile.Builder builder = new BuiltInQProfile.Builder() .setLanguage(api.language()) .setName(api.name()) .setDeclaredDefault(api.isDefault()); Map<RuleKey, RuleDefinitionDto> rulesByRuleKey = Arrays.stream(rules) .collect(MoreCollectors.uniqueIndex(RuleDefinitionDto::getKey)); api.rules().forEach(rule -> { RuleKey ruleKey = RuleKey.of(rule.repoKey(), rule.ruleKey()); RuleDefinitionDto ruleDefinition = rulesByRuleKey.get(ruleKey); Preconditions.checkState(ruleDefinition != null, "Rule '%s' not found", ruleKey); builder.addRule(rule, ruleDefinition.getId()); }); return builder .build(); } }
private List<ActiveRuleParamDto> insertActiveRuleParams(DbSession session, BuiltInQProfile.ActiveRule activeRule, ActiveRuleDto activeRuleDto) { Map<String, String> valuesByParamKey = activeRule.getBuiltIn().overriddenParams() .stream() .collect(MoreCollectors.uniqueIndex(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue)); return ruleRepository.getRuleParams(activeRule.getRuleKey()) .stream() .map(param -> { String activeRuleValue = valuesByParamKey.get(param.getName()); return createParamDto(param, activeRuleValue == null ? param.getDefaultValue() : activeRuleValue); }) .filter(Objects::nonNull) .peek(paramDto -> dbClient.activeRuleDao().insertParam(session, activeRuleDto, paramDto)) .collect(MoreCollectors.toList()); }
@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 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); }
private static RuleActivation convert(BuiltInQProfile.ActiveRule ar) { Map<String, String> params = ar.getBuiltIn().overriddenParams().stream() .collect(MoreCollectors.uniqueIndex(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue)); return RuleActivation.create(ar.getRuleId(), ar.getBuiltIn().overriddenSeverity(), params); }
private static BuiltInQProfile.Builder updateOrCreateBuilder(String language, @Nullable BuiltInQProfile.Builder existingBuilder, BuiltInQualityProfile builtInProfile, Map<RuleKey, RuleDefinitionDto> rulesByRuleKey) { BuiltInQProfile.Builder builder = createOrReuseBuilder(existingBuilder, language, builtInProfile); builder.setDeclaredDefault(builtInProfile.isDefault()); builtInProfile.rules().forEach(builtInActiveRule -> { RuleKey ruleKey = RuleKey.of(builtInActiveRule.repoKey(), builtInActiveRule.ruleKey()); RuleDefinitionDto ruleDefinition = rulesByRuleKey.get(ruleKey); checkState(ruleDefinition != null, "Rule with key '%s' not found", ruleKey); builder.addRule(builtInActiveRule, ruleDefinition.getId()); }); return builder; }
private BuiltInQualityProfileImpl(NewBuiltInQualityProfileImpl newProfile) { this.name = newProfile.name(); this.language = newProfile.language(); this.isDefault = newProfile.isDefault(); Map<RuleKey, BuiltInActiveRule> ruleBuilder = new HashMap<>(); for (NewBuiltInActiveRule newActiveRule : newProfile.activeRules()) { ruleBuilder.put(RuleKey.of(newActiveRule.repoKey, newActiveRule.ruleKey), new BuiltInActiveRule(newActiveRule)); } this.activeRulesByKey = unmodifiableMap(ruleBuilder); }