summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/LeafGroupsTest.java
blob: 418400b3fff58222790066799c5cdb9d97f3b20b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import com.yahoo.vdslib.distribution.Group;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class LeafGroupsTest {

    @Test
    public void rootGroupCountedAsLeafWhenNoChildren() {
        Group g = new Group(0, "donkeykong");

        List<Group> leaves = LeafGroups.enumerateFrom(g);
        assertThat(leaves.size(), is(1));
        assertThat(leaves.get(0).getName(), is("donkeykong"));
    }

    private Group.Distribution dummyDistribution() throws Exception {
        return new Group.Distribution("*", 1);
    }

    @Test
    public void singleLeafIsEnumerated() throws Exception {
        Group g = new Group(0, "donkeykong", dummyDistribution());
        Group child = new Group(1, "mario");
        g.addSubGroup(child);

        List<Group> leaves = LeafGroups.enumerateFrom(g);
        assertThat(leaves.size(), is(1));
        assertThat(leaves.get(0).getName(), is("mario"));
    }

    @Test
    public void singleLeafIsEnumeratedInNestedCase() throws Exception {
        Group g = new Group(0, "donkeykong", dummyDistribution());
        Group child = new Group(1, "mario", dummyDistribution());
        child.addSubGroup(new Group(2, "toad"));
        g.addSubGroup(child);

        List<Group> leaves = LeafGroups.enumerateFrom(g);
        assertThat(leaves.size(), is(1));
        assertThat(leaves.get(0).getName(), is("toad"));
    }

    @Test
    public void multipleLeafGroupsAreEnumerated() throws Exception {
        Group g = new Group(0, "donkeykong", dummyDistribution());
        Group child = new Group(1, "mario", dummyDistribution());
        child.addSubGroup(new Group(2, "toad"));
        child.addSubGroup(new Group(3, "yoshi"));
        g.addSubGroup(child);
        g.addSubGroup(new Group(4, "luigi"));

        List<Group> leaves = LeafGroups.enumerateFrom(g);
        // Ensure that output order matches insertion order.
        leaves.sort((a, b) -> Integer.compare(a.getIndex(), b.getIndex()));
        assertThat(leaves.size(), is(3));
        assertThat(leaves.get(0).getName(), is("toad"));
        assertThat(leaves.get(1).getName(), is("yoshi"));
        assertThat(leaves.get(2).getName(), is("luigi"));
    }
}