aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-enforcer-extensions/src/test/java/com/yahoo/vespa/maven/plugin/enforcer/EnforceDependenciesTest.java
blob: fe372f74cb516f375b7a0e48f089b8c7e803b048 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.yahoo.vespa.maven.plugin.enforcer;// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.junit.jupiter.api.Test;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
 * @author bjorncs
 */
class EnforceDependenciesTest {

    @Test
    void succeeds_when_all_dependencies_and_rules_match() {
        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:container-core:jar:*:provided",
                "com.yahoo.vespa:*:jar:*:test");
        assertDoesNotThrow(() -> EnforceDependencies.validateDependencies(dependencies, rules, true));
    }

    @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());
    }

    @Test
    void fails_on_unmatched_rule() {
        Set<Artifact> dependencies = Set.of(
                artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
        Set<String> rules = Set.of(
                "com.yahoo.vespa:container-core:jar:*:provided",
                "com.yahoo.vespa:*:jar:*:test");
        EnforcerRuleException exception = assertThrows(
                EnforcerRuleException.class,
                () -> EnforceDependencies.validateDependencies(dependencies, rules, true));
        String expectedErrorMessage =
                """
                Vespa dependency enforcer failed:
                Rules not matching any dependency:
                 - com.yahoo.vespa:container-core:jar:*:provided
                """;
        assertEquals(expectedErrorMessage, exception.getMessage());
    }

    @Test
    void fails_on_version_mismatch() {
        Set<Artifact> dependencies = Set.of(
                artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
        Set<String> rules = Set.of(
                "com.yahoo.vespa:testutils:jar:7.0.0: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:testutils:jar:8.0.0:test
                Rules not matching any dependency:
                 - com.yahoo.vespa:testutils:jar:7.0.0:test
                """;
        assertEquals(expectedErrorMessage, exception.getMessage());
    }

    @Test
    void fails_on_scope_mismatch() {
        Set<Artifact> dependencies = Set.of(
                artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
        Set<String> rules = Set.of(
                "com.yahoo.vespa:testutils:jar:8.0.0:provided");
        EnforcerRuleException exception = assertThrows(
                EnforcerRuleException.class,
                () -> EnforceDependencies.validateDependencies(dependencies, rules, true));
        String expectedErrorMessage =
                """
                Vespa dependency enforcer failed:
                Dependencies not matching any rule:
                 - com.yahoo.vespa:testutils:jar:8.0.0:test
                Rules not matching any dependency:
                 - com.yahoo.vespa:testutils:jar:8.0.0:provided
                """;
        assertEquals(expectedErrorMessage, exception.getMessage());
    }

    @Test
    void matches_shorter_rule_variant_without_type() {
        Set<Artifact> dependencies = Set.of(
                artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
        assertDoesNotThrow(() -> EnforceDependencies.validateDependencies(
                dependencies, Set.of("com.yahoo.vespa:testutils:jar:8.0.0:test"), true));
        assertDoesNotThrow(() -> EnforceDependencies.validateDependencies(
                dependencies, Set.of("com.yahoo.vespa:testutils:8.0.0:test"), true));
    }

    @Test
    void matches_artifact_with_classifier() {
        Set<Artifact> dependencies = Set.of(
                artifact("com.google.inject", "guice", "4.2.3", "provided", "no_aop"));
        assertDoesNotThrow(() -> EnforceDependencies.validateDependencies(
                dependencies, Set.of("com.google.inject:guice:jar:no_aop:4.2.3:provided"), true));
        EnforcerRuleException exception = assertThrows(
                EnforcerRuleException.class,
                () -> EnforceDependencies.validateDependencies(
                        dependencies, Set.of("com.google.inject:guice:4.2.3:provided"), true));
        String expectedErrorMessage =
                """
                Vespa dependency enforcer failed:
                Dependencies not matching any rule:
                 - com.google.inject:guice:jar:no_aop:4.2.3:provided
                Rules not matching any dependency:
                 - com.google.inject:guice:4.2.3:provided
                """;
        assertEquals(expectedErrorMessage, exception.getMessage());
    }

    @Test
    void matches_on_version_in_allowed_range() {
        var dependencies = Set.of(
                artifact("com.yahoo.testing", "test", "1.2.3", "compile"));
        var rules = Set.of("com.yahoo.testing:test:jar:[1.0,2):compile");
        assertDoesNotThrow(() -> EnforceDependencies.validateDependencies(dependencies, rules, true));
    }

    @Test
    void fails_on_version_outside_allowed_range() {
        var dependencies = Set.of(
                artifact("com.yahoo.testing", "test", "2", "compile"));
        var rules = Set.of("com.yahoo.testing:test:jar:[1.0,2):compile");

        var exception = assertThrows(
                EnforcerRuleException.class,
                () -> EnforceDependencies.validateDependencies(dependencies, rules, true));
        String expectedErrorMessage =
                """
                Vespa dependency enforcer failed:
                Dependencies not matching any rule:
                 - com.yahoo.testing:test:jar:2:compile
                Rules not matching any dependency:
                 - com.yahoo.testing:test:jar:[1.0,2):compile
                """;
        assertEquals(expectedErrorMessage, exception.getMessage());
    }

    private static Artifact artifact(String groupId, String artifactId, String version, String scope) {
        return artifact(groupId, artifactId, version, scope, null);
    }
    private static Artifact artifact(String groupId, String artifactId, String version, String scope, String classifier) {
        return new DefaultArtifact(
                groupId, artifactId, version, scope, "jar", classifier, new DefaultArtifactHandler("jar"));
    }

}