private Optional<RuleDefinitionDto> getDbRuleFor(RulesDefinition.Rule ruleDef) { RuleKey ruleKey = RuleKey.of(ruleDef.repository().key(), ruleDef.key()); Optional<RuleDefinitionDto> res = Stream.concat(Stream.of(ruleKey), ruleDef.deprecatedRuleKeys().stream()) .map(dbRules::get) .filter(Objects::nonNull) .findFirst(); // may occur in case of plugin downgrade if (!res.isPresent()) { return Optional.ofNullable(dbRulesByDbDeprecatedKey.get(ruleKey)); } return res; }
private static void verifyRuleKeyConsistency(List<RulesDefinition.ExtendedRepository> repositories, RegisterRulesContext registerRulesContext) { List<RulesDefinition.Rule> definedRules = repositories.stream() .flatMap(r -> r.rules().stream()) .collect(toList()); Set<RuleKey> definedRuleKeys = definedRules.stream() .map(r -> RuleKey.of(r.repository().key(), r.key())) .collect(toSet()); List<RuleKey> definedDeprecatedRuleKeys = definedRules.stream() .flatMap(r -> r.deprecatedRuleKeys().stream()) .collect(toList()); // Find duplicates in declared deprecated rule keys Set<RuleKey> duplicates = findDuplicates(definedDeprecatedRuleKeys); checkState(duplicates.isEmpty(), "The following deprecated rule keys are declared at least twice [%s]", lazyToString(() -> duplicates.stream().map(RuleKey::toString).collect(Collectors.joining(",")))); // Find rule keys that are both deprecated and used Set<RuleKey> intersection = intersection(new HashSet<>(definedRuleKeys), new HashSet<>(definedDeprecatedRuleKeys)).immutableCopy(); checkState(intersection.isEmpty(), "The following rule keys are declared both as deprecated and used key [%s]", lazyToString(() -> intersection.stream().map(RuleKey::toString).collect(Collectors.joining(",")))); // Find incorrect usage of deprecated keys ImmutableMap<RuleKey, SingleDeprecatedRuleKey> dbDeprecatedRuleKeysByOldRuleKey = registerRulesContext.getDbDeprecatedKeysByOldRuleKey(); Set<String> incorrectRuleKeyMessage = definedRules.stream() .flatMap(r -> filterInvalidDeprecatedRuleKeys(dbDeprecatedRuleKeysByOldRuleKey, r)) .filter(Objects::nonNull) .collect(Collectors.toSet()); checkState(incorrectRuleKeyMessage.isEmpty(), "An incorrect state of deprecated rule keys has been detected.\n %s", lazyToString(() -> incorrectRuleKeyMessage.stream().collect(Collectors.joining("\n")))); }
public static Set<SingleDeprecatedRuleKey> from(RulesDefinition.Rule rule) { return rule.deprecatedRuleKeys().stream() .map(r -> new SingleDeprecatedRuleKey() .setNewRepositoryKey(rule.repository().key()) .setNewRuleKey(rule.key()) .setOldRepositoryKey(r.repository()) .setOldRuleKey(r.rule())) .collect(MoreCollectors.toSet(rule.deprecatedRuleKeys().size())); }
private static Stream<String> filterInvalidDeprecatedRuleKeys(ImmutableMap<RuleKey, SingleDeprecatedRuleKey> dbDeprecatedRuleKeysByOldRuleKey, RulesDefinition.Rule rule) { return rule.deprecatedRuleKeys().stream() .map(rk -> { SingleDeprecatedRuleKey singleDeprecatedRuleKey = dbDeprecatedRuleKeysByOldRuleKey.get(rk); if (singleDeprecatedRuleKey == null) { // new deprecated rule key : OK return null; } RuleKey parentRuleKey = RuleKey.of(rule.repository().key(), rule.key()); if (parentRuleKey.equals(singleDeprecatedRuleKey.getNewRuleKeyAsRuleKey())) { // same parent : OK return null; } if (rule.deprecatedRuleKeys().contains(parentRuleKey)) { // the new rule is deprecating the old parentRuleKey : OK return null; } return format("The deprecated rule key [%s] was previously deprecated by [%s]. [%s] should be a deprecated key of [%s],", rk.toString(), singleDeprecatedRuleKey.getNewRuleKeyAsRuleKey().toString(), singleDeprecatedRuleKey.getNewRuleKeyAsRuleKey().toString(), RuleKey.of(rule.repository().key(), rule.key()).toString()); }); }
private static void verifyRuleKeyConsistency(List<RulesDefinition.ExtendedRepository> repositories, RegisterRulesContext registerRulesContext) { List<RulesDefinition.Rule> definedRules = repositories.stream() .flatMap(r -> r.rules().stream()) .collect(toList()); Set<RuleKey> definedRuleKeys = definedRules.stream() .map(r -> RuleKey.of(r.repository().key(), r.key())) .collect(toSet()); List<RuleKey> definedDeprecatedRuleKeys = definedRules.stream() .flatMap(r -> r.deprecatedRuleKeys().stream()) .collect(toList()); // Find duplicates in declared deprecated rule keys Set<RuleKey> duplicates = findDuplicates(definedDeprecatedRuleKeys); checkState(duplicates.isEmpty(), "The following deprecated rule keys are declared at least twice [%s]", lazyToString(() -> duplicates.stream().map(RuleKey::toString).collect(Collectors.joining(",")))); // Find rule keys that are both deprecated and used Set<RuleKey> intersection = intersection(new HashSet<>(definedRuleKeys), new HashSet<>(definedDeprecatedRuleKeys)).immutableCopy(); checkState(intersection.isEmpty(), "The following rule keys are declared both as deprecated and used key [%s]", lazyToString(() -> intersection.stream().map(RuleKey::toString).collect(Collectors.joining(",")))); // Find incorrect usage of deprecated keys ImmutableMap<RuleKey, SingleDeprecatedRuleKey> dbDeprecatedRuleKeysByOldRuleKey = registerRulesContext.getDbDeprecatedKeysByOldRuleKey(); Set<String> incorrectRuleKeyMessage = definedRules.stream() .flatMap(r -> filterInvalidDeprecatedRuleKeys(dbDeprecatedRuleKeysByOldRuleKey, r)) .filter(Objects::nonNull) .collect(Collectors.toSet()); checkState(incorrectRuleKeyMessage.isEmpty(), "An incorrect state of deprecated rule keys has been detected.\n %s", lazyToString(() -> incorrectRuleKeyMessage.stream().collect(Collectors.joining("\n")))); }
public static Set<SingleDeprecatedRuleKey> from(RulesDefinition.Rule rule) { return rule.deprecatedRuleKeys().stream() .map(r -> new SingleDeprecatedRuleKey() .setNewRepositoryKey(rule.repository().key()) .setNewRuleKey(rule.key()) .setOldRepositoryKey(r.repository()) .setOldRuleKey(r.rule())) .collect(MoreCollectors.toSet(rule.deprecatedRuleKeys().size())); }
private Optional<RuleDefinitionDto> getDbRuleFor(RulesDefinition.Rule ruleDef) { RuleKey ruleKey = RuleKey.of(ruleDef.repository().key(), ruleDef.key()); Optional<RuleDefinitionDto> res = Stream.concat(Stream.of(ruleKey), ruleDef.deprecatedRuleKeys().stream()) .map(dbRules::get) .filter(Objects::nonNull) .findFirst(); // may occur in case of plugin downgrade if (!res.isPresent()) { return Optional.ofNullable(dbRulesByDbDeprecatedKey.get(ruleKey)); } return res; }
private static Stream<String> filterInvalidDeprecatedRuleKeys(ImmutableMap<RuleKey, SingleDeprecatedRuleKey> dbDeprecatedRuleKeysByOldRuleKey, RulesDefinition.Rule rule) { return rule.deprecatedRuleKeys().stream() .map(rk -> { SingleDeprecatedRuleKey singleDeprecatedRuleKey = dbDeprecatedRuleKeysByOldRuleKey.get(rk); if (singleDeprecatedRuleKey == null) { // new deprecated rule key : OK return null; } RuleKey parentRuleKey = RuleKey.of(rule.repository().key(), rule.key()); if (parentRuleKey.equals(singleDeprecatedRuleKey.getNewRuleKeyAsRuleKey())) { // same parent : OK return null; } if (rule.deprecatedRuleKeys().contains(parentRuleKey)) { // the new rule is deprecating the old parentRuleKey : OK return null; } return format("The deprecated rule key [%s] was previously deprecated by [%s]. [%s] should be a deprecated key of [%s],", rk.toString(), singleDeprecatedRuleKey.getNewRuleKeyAsRuleKey().toString(), singleDeprecatedRuleKey.getNewRuleKeyAsRuleKey().toString(), RuleKey.of(rule.repository().key(), rule.key()).toString()); }); }
@Test public void getDeprecatedKeys_does_not_return_the_same_key_more_than_once() { RuleKey duplicatedRuleKey = RuleKey.of("foo", "AAA"); RuleKey ruleKey2 = RuleKey.of("bar", "CCCC"); RuleKey ruleKey3 = RuleKey.of("foo", "BBBBBBBBBB"); List<RuleKey> ruleKeys = ImmutableList.of(duplicatedRuleKey, ruleKey2, duplicatedRuleKey, duplicatedRuleKey, ruleKey3); String repositoryKey = "foo"; String ruleKey = "doh"; RulesDefinition.NewRepository newRepository = context.createRepository(repositoryKey, "bar"); RulesDefinition.NewRule newRule = newRepository.createRule(ruleKey) .setName("doh rule") .setHtmlDescription("doh description"); ruleKeys.forEach(r -> newRule.addDeprecatedRuleKey(r.repository(), r.rule())); newRepository.done(); RulesDefinition.Repository repository = context.repository(repositoryKey); RulesDefinition.Rule rule = repository.rule(ruleKey); assertThat(rule.deprecatedRuleKeys()) .containsExactly(ruleKey2, duplicatedRuleKey, ruleKey3); }
@Test public void test_creation_from_RulesDefinitionRule() { // Creation from RulesDefinition.Rule ImmutableSet<RuleKey> deprecatedRuleKeys = ImmutableSet.of( RuleKey.of(randomAlphanumeric(50), randomAlphanumeric(50)), RuleKey.of(randomAlphanumeric(50), randomAlphanumeric(50)), RuleKey.of(randomAlphanumeric(50), randomAlphanumeric(50)) ); RulesDefinition.Repository repository = mock(RulesDefinition.Repository.class); when(repository.key()).thenReturn(randomAlphanumeric(50)); RulesDefinition.Rule rule = mock(RulesDefinition.Rule.class); when(rule.key()).thenReturn(randomAlphanumeric(50)); when(rule.deprecatedRuleKeys()).thenReturn(deprecatedRuleKeys); when(rule.repository()).thenReturn(repository); Set<SingleDeprecatedRuleKey> singleDeprecatedRuleKeys = SingleDeprecatedRuleKey.from(rule); assertThat(singleDeprecatedRuleKeys).hasSize(deprecatedRuleKeys.size()); assertThat(singleDeprecatedRuleKeys) .extracting(SingleDeprecatedRuleKey::getUuid, SingleDeprecatedRuleKey::getOldRepositoryKey, SingleDeprecatedRuleKey::getOldRuleKey, SingleDeprecatedRuleKey::getNewRepositoryKey, SingleDeprecatedRuleKey::getNewRuleKey, SingleDeprecatedRuleKey::getOldRuleKeyAsRuleKey) .containsExactlyInAnyOrder( deprecatedRuleKeys.stream().map( r -> tuple(null, r.repository(), r.rule(), rule.repository().key(), rule.key(), RuleKey.of(r.repository(), r.rule())) ).collect(MoreCollectors.toArrayList(deprecatedRuleKeys.size())).toArray(new Tuple[deprecatedRuleKeys.size()]) ); }
@Test public void getDeprecatedKeys_returns_keys_in_order_of_addDeprecatedKeys_calls() { Set<RuleKey> ruleKeys = ImmutableSet.of(RuleKey.of("foo", "AAA"), RuleKey.of("bar", "CCCC"), RuleKey.of("doh", "CCCC"), RuleKey.of("foo", "BBBBBBBBBB")); List<RuleKey> sortedRuleKeys = ruleKeys.stream().sorted(Ordering.natural().onResultOf(RuleKey::toString)).collect(Collectors.toList()); // ensure we don't have the same order Assume.assumeTrue(!ImmutableList.copyOf(ruleKeys).equals(sortedRuleKeys)); String repositoryKey = "foo"; String ruleKey = "doh"; RulesDefinition.NewRepository newRepository = context.createRepository(repositoryKey, "bar"); RulesDefinition.NewRule newRule = newRepository.createRule(ruleKey) .setName("doh rule") .setHtmlDescription("doh description"); sortedRuleKeys.forEach(r -> newRule.addDeprecatedRuleKey(r.repository(), r.rule())); newRepository.done(); RulesDefinition.Repository repository = context.repository(repositoryKey); RulesDefinition.Rule rule = repository.rule(ruleKey); assertThat(ImmutableList.copyOf(rule.deprecatedRuleKeys())) .isEqualTo(sortedRuleKeys); }
@Test public void getDeprecatedKeys_returns_empty_if_addDeprecatedKeys_never_called() { String repositoryKey = "foo"; String ruleKey = "doh"; RulesDefinition.NewRepository newRepository = context.createRepository(repositoryKey, "bar"); newRepository.createRule(ruleKey) .setName("doh rule") .setHtmlDescription("doh description"); newRepository.done(); RulesDefinition.Repository repository = context.repository(repositoryKey); RulesDefinition.Rule rule = repository.rule(ruleKey); assertThat(rule.deprecatedRuleKeys()).isEmpty(); }