aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/host/HostRegistryTest.java
blob: 0de8b9c8f2459cdd4fb29f5b5950c8d24cb731d8 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.host;

import com.yahoo.config.provision.ApplicationId;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author Ulf Lilleengen
 */
public class HostRegistryTest {

    private final ApplicationId foo = ApplicationId.from("foo", "app1", "default");
    private final ApplicationId bar = ApplicationId.from("bar", "app2", "default");

    @Test
    public void old_hosts_are_removed() {
        HostRegistry reg = new HostRegistry();
        assertNull(reg.getApplicationId("foo.com"));
        reg.update(foo, List.of("foo.com", "bar.com", "baz.com"));
        assertGetKey(reg, "foo.com", foo);
        assertGetKey(reg, "bar.com", foo);
        assertGetKey(reg, "baz.com", foo);
        assertEquals(3, reg.getAllHosts().size());
        reg.update(foo, List.of("bar.com", "baz.com"));
        assertNull(reg.getApplicationId("foo.com"));
        assertGetKey(reg, "bar.com", foo);
        assertGetKey(reg, "baz.com", foo);

        assertEquals(2, reg.getAllHosts().size());
        assertTrue(reg.getAllHosts().containsAll(List.of("bar.com", "baz.com")));
        reg.removeHosts(foo);
        assertTrue(reg.getAllHosts().isEmpty());
        assertNull(reg.getApplicationId("foo.com"));
        assertNull(reg.getApplicationId("bar.com"));
    }

    @Test
    public void multiple_keys_are_handled() {
        HostRegistry reg = new HostRegistry();
        reg.update(foo, List.of("foo.com", "bar.com"));
        reg.update(bar, List.of("baz.com", "quux.com"));
        assertGetKey(reg, "foo.com", foo);
        assertGetKey(reg, "bar.com", foo);
        assertGetKey(reg, "baz.com", bar);
        assertGetKey(reg, "quux.com", bar);
    }

    @Test(expected = IllegalArgumentException.class)
    public void keys_cannot_overlap() {
        HostRegistry reg = new HostRegistry();
        reg.update(foo, List.of("foo.com", "bar.com"));
        reg.update(bar, List.of("bar.com", "baz.com"));
    }

    @Test
    public void all_hosts_are_returned() {
        HostRegistry reg = new HostRegistry();
        reg.update(foo, List.of("foo.com", "bar.com"));
        reg.update(bar, List.of("baz.com", "quux.com"));
        assertEquals(4, reg.getAllHosts().size());
    }

    @Test
    public void ensure_that_collection_is_copied() {
        HostRegistry reg = new HostRegistry();
        List<String> hosts = new ArrayList<>(List.of("foo.com", "bar.com", "baz.com"));
        reg.update(foo, hosts);
        assertEquals(3, reg.getHosts(foo).size());
        hosts.remove(2);
        assertEquals(3, reg.getHosts(foo).size());
    }

    @Test
    public void ensure_that_underlying_hosts_do_not_change() {
        HostRegistry reg = new HostRegistry();
        reg.update(foo, List.of("foo.com", "bar.com", "baz.com"));
        Collection<String> hosts = reg.getAllHosts();
        assertEquals(3, hosts.size());
        reg.update(foo, List.of("foo.com"));
        assertEquals(3, hosts.size());
    }

    private void assertGetKey(HostRegistry reg, String host, ApplicationId expectedKey) {
        assertNotNull(reg.getApplicationId(host));
        assertEquals(expectedKey, reg.getApplicationId(host));
    }

}