static void validateDependencies(Set<Artifact> dependencies, Set<String> allowedRules, boolean failOnUnmatched)
            throws EnforcerRuleException {
        SortedSet<Artifact> unmatchedArtifacts = new TreeSet<>();
        Set<String> matchedRules = new HashSet<>();
        for (Artifact dependency : dependencies) {
            boolean matches = false;
            for (String rule : allowedRules) {
                if (matches(dependency, rule)){
                    matchedRules.add(rule);
                    matches = true;
                    break;
                }
            }
            if (!matches) {
                unmatchedArtifacts.add(dependency);
            }
        }
        SortedSet<String> unmatchedRules = new TreeSet<>(allowedRules);
        unmatchedRules.removeAll(matchedRules);
        if (!unmatchedArtifacts.isEmpty() || (failOnUnmatched && !unmatchedRules.isEmpty())) {
            StringBuilder errorMessage = new StringBuilder("Vespa dependency enforcer failed:\n");
            if (!unmatchedArtifacts.isEmpty()) {
                errorMessage.append("Dependencies not matching any rule:\n");
                unmatchedArtifacts.forEach(a -> errorMessage.append(" - ").append(a.toString()).append('\n'));
            }
            if (failOnUnmatched && !unmatchedRules.isEmpty()) {
                errorMessage.append("Rules not matching any dependency:\n");
                unmatchedRules.forEach(p -> errorMessage.append(" - ").append(p).append('\n'));
            }
            throw new EnforcerRuleException(errorMessage.toString());
        }
    }
@Test
    void fails_on_unmatched_dependency() {
        Set<Artifact> dependencies = Set.of(
                artifact("com.yahoo.vespa", "container-core", "8.0.0", "provided"),
                artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
        Set<String> rules = Set.of("com.yahoo.vespa:*:jar:*:test");
        EnforcerRuleException exception = assertThrows(
                EnforcerRuleException.class,
                () -> EnforceDependencies.validateDependencies(dependencies, rules, true));
        String expectedErrorMessage =
                """
                Vespa dependency enforcer failed:
                Dependencies not matching any rule:
                 - com.yahoo.vespa:container-core:jar:8.0.0:provided
                """;
        assertEquals(expectedErrorMessage, exception.getMessage());
    }