aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/api/AthenzDomainTest.java
blob: d7d5428c11b685548fbb3a025f7d11d6dd08f56a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.api;

import org.junit.jupiter.api.Test;

import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author bjorncs
 */
public class AthenzDomainTest {

    @Test
    void domain_can_be_constructed_from_valid_string() {
        new AthenzDomain("home.john.my-app");
    }

    @Test
    void invalid_domain_throws_exception() {
        assertInvalid(() -> new AthenzDomain("endswithdot."));
        assertInvalid(() -> new AthenzDomain(".startswithdot"));
    }

    @Test
    void parent_domain_is_without_name_suffix() {
        assertEquals(new AthenzDomain("home.john"), new AthenzDomain("home.john.myapp").getParent());
    }

    @Test
    void domain_name_suffix_is_the_suffix_after_last_dot() {
        assertEquals("myapp", new AthenzDomain("home.john.myapp").getNameSuffix());
    }

    @Test
    void domain_without_dot_is_toplevel() {
        assertTrue(new AthenzDomain("toplevel").isTopLevelDomain());
        assertFalse(new AthenzDomain("not.toplevel").isTopLevelDomain());
    }

    private static void assertInvalid(Supplier<AthenzDomain> domainCreator) {
        try {
            AthenzDomain domain = domainCreator.get();
            fail("Expected IllegalArgumentException for domain: " + domain.getName());
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().startsWith("Not a valid domain name"));
        }
    }


}