summaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/test/java/com/yahoo/config/provision/ClusterMembershipTest.java
blob: 7cf0abecc2d545098144007ecc18f9f2d3e06f02 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;

import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import java.util.Optional;

import static org.junit.Assert.assertEquals;

/**
 * @author bratseth
 */
public class ClusterMembershipTest {

    @Test
    public void testContainerServiceInstance() {
        ClusterSpec cluster = ClusterSpec.request(ClusterSpec.Type.container, ClusterSpec.Id.from("id1"), Optional.empty());
        assertContainerService(ClusterMembership.from(cluster, 3));
    }

    @Test
    public void testContainerServiceInstanceFromString() {
        assertContainerService(ClusterMembership.from("container/id1/3", Optional.empty()));
    }

    @Test
    public void testServiceInstance() {
        ClusterSpec cluster = ClusterSpec.request(ClusterSpec.Type.content, ClusterSpec.Id.from("id1"), Optional.empty());
        assertContentService(ClusterMembership.from(cluster, 37));
    }

    @Test
    public void testServiceInstanceFromString() {
        assertContentService(ClusterMembership.from("content/id1/37", Optional.empty()));
    }

    @Test
    public void testServiceInstanceWithGroup() {
        ClusterSpec cluster = ClusterSpec.from(ClusterSpec.Type.content, ClusterSpec.Id.from("id1"),
                                               ClusterSpec.Group.from(4), Optional.empty());
        assertContentServiceWithGroup(ClusterMembership.from(cluster, 37));
    }

    @Test
    public void testServiceInstanceWithGroupFromString() {
        assertContentServiceWithGroup(ClusterMembership.from("content/id1/4/37", Optional.empty()));
    }

    @Test
    public void testServiceInstanceWithRetire() {
        ClusterSpec cluster = ClusterSpec.request(ClusterSpec.Type.content, ClusterSpec.Id.from("id1"), Optional.empty());
        assertContentServiceWithRetire(ClusterMembership.retiredFrom(cluster, 37));
    }

    @Test
    public void testServiceInstanceWithRetireFromString() {
        assertContentServiceWithRetire(ClusterMembership.from("content/id1/37/retired", Optional.empty()));
    }

    @Test
    public void testServiceInstanceWithGroupAndRetire() {
        ClusterSpec cluster = ClusterSpec.from(ClusterSpec.Type.content, ClusterSpec.Id.from("id1"),
                                               ClusterSpec.Group.from(4), Optional.empty());
        assertContentServiceWithGroupAndRetire(ClusterMembership.retiredFrom(cluster, 37));
    }

    @Test
    public void testServiceInstanceWithGroupAndRetireFromString() {
        assertContentServiceWithGroupAndRetire(ClusterMembership.from("content/id1/4/37/retired", Optional.empty()));
    }

    private void assertContainerService(ClusterMembership instance) {
        assertEquals(ClusterSpec.Type.container, instance.cluster().type());
        assertEquals("id1", instance.cluster().id().value());
        assertEquals(Optional.<ClusterSpec.Group>empty(), instance.cluster().group());
        assertEquals(3, instance.index());
        assertEquals("container/id1/3", instance.stringValue());
    }

    private void assertContentService(ClusterMembership instance) {
        assertEquals(ClusterSpec.Type.content, instance.cluster().type());
        assertEquals("id1", instance.cluster().id().value());
        assertFalse("gr4", instance.cluster().group().isPresent());
        assertEquals(37, instance.index());
        assertFalse(instance.retired());
        assertEquals("content/id1/37", instance.stringValue());
    }

    private void assertContentServiceWithGroup(ClusterMembership instance) {
        assertEquals(ClusterSpec.Type.content, instance.cluster().type());
        assertEquals("id1", instance.cluster().id().value());
        assertEquals(4, instance.cluster().group().get().index());
        assertEquals(37, instance.index());
        assertFalse(instance.retired());
        assertEquals("content/id1/4/37", instance.stringValue());
    }

    private void assertContentServiceWithRetire(ClusterMembership instance) {
        assertEquals(ClusterSpec.Type.content, instance.cluster().type());
        assertEquals("id1", instance.cluster().id().value());
        assertEquals(37, instance.index());
        assertTrue(instance.retired());
        assertEquals("content/id1/37/retired", instance.stringValue());
    }

    private void assertContentServiceWithGroupAndRetire(ClusterMembership instance) {
        assertEquals(ClusterSpec.Type.content, instance.cluster().type());
        assertEquals("id1", instance.cluster().id().value());
        assertEquals(4, instance.cluster().group().get().index());
        assertEquals(37, instance.index());
        assertTrue(instance.retired());
        assertEquals("content/id1/4/37/retired", instance.stringValue());
    }

}