aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-enforcer-extensions/src/test/java/com/yahoo/vespa/maven/plugin/enforcer/EnforceDependenciesAllProjectsTest.java
blob: 910d18b25cc07168d98705123fdaf3e2a231ab82 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.maven.plugin.enforcer;

import com.yahoo.vespa.maven.plugin.enforcer.EnforceDependenciesAllProjects.Dependency;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import static com.yahoo.vespa.maven.plugin.enforcer.EnforceDependenciesAllProjects.validateDependencies;
import static com.yahoo.vespa.maven.plugin.enforcer.EnforceDependenciesAllProjects.writeDependencySpec;
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 EnforceDependenciesAllProjectsTest {

    private static final Path POM_FILE = Paths.get("/vespa-src/pom.xml");

    @Test
    void succeeds_dependencies_matches_spec() {
        SortedSet<Dependency> dependencies = new TreeSet<>(Set.of(
                Dependency.fromString("com.example:foo:1.2.3"),
                Dependency.fromString("com.example:bar:2.3.4")));
        Path specFile = Paths.get("src/test/resources/allowed-dependencies.txt");
        assertDoesNotThrow(() -> validateDependencies(dependencies, specFile, POM_FILE, "my-dep-enforcer"));
    }

    @Test
    void fails_on_forbidden_dependency() {
        SortedSet<Dependency> dependencies = new TreeSet<>(Set.of(
                Dependency.fromString("com.example:foo:1.2.3"),
                Dependency.fromString("com.example:bar:2.3.4"),
                Dependency.fromString("com.example:foobar:3.4.5")));
        Path specFile = Paths.get("src/test/resources/allowed-dependencies.txt");
        var exception = assertThrows(EnforcerRuleException.class,
                                     () -> validateDependencies(dependencies, specFile, POM_FILE, "my-dep-enforcer"));
        String expectedErrorMessage =
                """
                The dependency enforcer failed:
                Forbidden dependencies:
                 - com.example:foobar:3.4.5
                Maven dependency validation failed. If this change was intentional, update the dependency spec by running:
                $ mvn validate -DdependencyEnforcer.writeSpec -pl my-dep-enforcer -f /vespa-src/pom.xml
                """;
        assertEquals(expectedErrorMessage, exception.getMessage());
    }

    @Test
    void fails_on_missing_dependency() {
        SortedSet<Dependency> dependencies = new TreeSet<>(Set.of(
                Dependency.fromString("com.example:foo:1.2.3")));
        Path specFile = Paths.get("src/test/resources/allowed-dependencies.txt");
        var exception = assertThrows(EnforcerRuleException.class,
                                     () -> validateDependencies(dependencies, specFile, POM_FILE, "my-dep-enforcer"));
        String expectedErrorMessage =
                """
                The dependency enforcer failed:
                Removed dependencies:
                 - com.example:bar:2.3.4
                Maven dependency validation failed. If this change was intentional, update the dependency spec by running:
                $ mvn validate -DdependencyEnforcer.writeSpec -pl my-dep-enforcer -f /vespa-src/pom.xml
                """;
        assertEquals(expectedErrorMessage, exception.getMessage());
    }

    @Test
    void writes_valid_spec_file(@TempDir Path tempDir) throws IOException {
        SortedSet<Dependency> dependencies = new TreeSet<>(Set.of(
                Dependency.fromString("com.example:foo:1.2.3"),
                Dependency.fromString("com.example:bar:2.3.4")));
        Path outputFile = tempDir.resolve("allowed-dependencies.txt");
        writeDependencySpec(outputFile, dependencies);
        assertEquals(
                Files.readString(Paths.get("src/test/resources/allowed-dependencies.txt")),
                Files.readString(outputFile));

    }

}