blob: 89a33507bc0964bcafc30187fd57542bd17652f1 [file] [log] [blame]
Jordan Halterman0a2bd452018-06-13 17:24:58 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.mastership;
17
18import java.util.Optional;
19
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.Lists;
22import org.junit.Test;
23import org.onosproject.cluster.NodeId;
24import org.onosproject.net.MastershipRole;
25
26import static org.junit.Assert.assertEquals;
27import static org.junit.Assert.assertNotEquals;
28
29/**
30 * Mastership info test.
31 */
32public class MastershipInfoTest {
33 private final NodeId node1 = new NodeId("1");
34 private final NodeId node2 = new NodeId("2");
35 private final NodeId node3 = new NodeId("3");
36 private final NodeId node4 = new NodeId("4");
37
38 private final MastershipInfo mastershipInfo = new MastershipInfo(
39 1,
40 Optional.of(node1),
41 ImmutableMap.<NodeId, MastershipRole>builder()
42 .put(node1, MastershipRole.MASTER)
43 .put(node2, MastershipRole.STANDBY)
44 .put(node3, MastershipRole.STANDBY)
45 .put(node4, MastershipRole.NONE)
46 .build());
47
48 @Test
49 public void testMastershipInfo() throws Exception {
50 assertEquals(1, mastershipInfo.term());
51 assertEquals(node1, mastershipInfo.master().get());
52 assertEquals(Lists.newArrayList(node1), mastershipInfo.getRoles(MastershipRole.MASTER));
53 assertEquals(Lists.newArrayList(node2, node3), mastershipInfo.backups());
54 assertEquals(Lists.newArrayList(node2, node3), mastershipInfo.getRoles(MastershipRole.STANDBY));
55 assertEquals(Lists.newArrayList(node4), mastershipInfo.getRoles(MastershipRole.NONE));
56 }
57
58 @Test
59 public void testEquals() throws Exception {
60 assertEquals(mastershipInfo, mastershipInfo);
61 assertNotEquals(mastershipInfo, new MastershipInfo(1, Optional.of(node1), ImmutableMap.of()));
62 }
63}