84504d3ae47b7f589425f5c72b8dd52d1f748ae5
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / shardmanager / ShardPeerAddressResolverTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.shardmanager;
9
10 import static org.junit.Assert.assertEquals;
11 import akka.actor.Address;
12 import com.google.common.collect.Sets;
13 import java.util.Collection;
14 import org.junit.Test;
15 import org.opendaylight.controller.cluster.access.concepts.MemberName;
16 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
17
18 /**
19  * Unit tests for ShardPeerAddressResolver.
20  *
21  * @author Thomas Pantelis
22  */
23 public class ShardPeerAddressResolverTest {
24     private static final MemberName MEMBER_1 = MemberName.forName("member-1");
25     private static final MemberName MEMBER_2 = MemberName.forName("member-2");
26     private static final MemberName MEMBER_3 = MemberName.forName("member-3");
27
28     @Test
29     public void testGetShardActorAddress() {
30         ShardPeerAddressResolver resolver = new ShardPeerAddressResolver("config", MEMBER_1);
31
32         assertEquals("getShardActorAddress", null, resolver.getShardActorAddress("default", MEMBER_2));
33
34         Address address2 = new Address("tcp", "system2");
35         resolver.addPeerAddress(MEMBER_2, address2);
36         assertEquals("getPeerAddress", address2, resolver.getPeerAddress(MEMBER_2));
37
38         Address address3 = new Address("tcp", "system3");
39         resolver.addPeerAddress(MEMBER_3, address3);
40         assertEquals("getPeerAddress", address3, resolver.getPeerAddress(MEMBER_3));
41
42         assertEquals("getShardActorAddress", address2.toString() +
43                 "/user/shardmanager-config/member-2-shard-default-config",
44                 resolver.getShardActorAddress("default", MEMBER_2));
45
46         assertEquals("getShardActorAddress", address3.toString() +
47                 "/user/shardmanager-config/member-3-shard-default-config",
48                 resolver.getShardActorAddress("default", MEMBER_3));
49
50         assertEquals("getShardActorAddress", address2.toString() +
51                 "/user/shardmanager-config/member-2-shard-topology-config",
52                 resolver.getShardActorAddress("topology", MEMBER_2));
53
54         resolver.removePeerAddress(MEMBER_2);
55         assertEquals("getShardActorAddress", null, resolver.getShardActorAddress("default", MEMBER_2));
56         assertEquals("getShardActorAddress", null, resolver.getShardActorAddress("topology", MEMBER_2));
57         assertEquals("getShardActorAddress", address3.toString() +
58                 "/user/shardmanager-config/member-3-shard-default-config",
59                 resolver.getShardActorAddress("default", MEMBER_3));
60     }
61
62     @Test
63     public void testResolve() {
64         String type = "config";
65         ShardPeerAddressResolver resolver = new ShardPeerAddressResolver(type, MEMBER_1);
66
67         MemberName memberName = MEMBER_2;
68         String peerId = ShardIdentifier.builder().memberName(memberName).shardName("default").
69                 type(type).build().toString();
70
71         assertEquals("resolve", null, resolver.resolve(peerId));
72
73         Address address = new Address("tcp", "system");
74         resolver.addPeerAddress(memberName, address);
75
76         String shardAddress = resolver.getShardActorAddress("default", memberName);
77         assertEquals("getShardActorAddress", address.toString() +
78                 "/user/shardmanager-" + type + "/" + memberName.getName() + "-shard-default-" + type, shardAddress);
79
80         assertEquals("resolve", shardAddress, resolver.resolve(peerId));
81     }
82
83     @Test
84     public void testGetShardManagerPeerActorAddresses() {
85         ShardPeerAddressResolver resolver = new ShardPeerAddressResolver("config", MEMBER_1);
86
87         resolver.addPeerAddress(MEMBER_1, new Address("tcp", "system1"));
88
89         Address address2 = new Address("tcp", "system2");
90         resolver.addPeerAddress(MEMBER_2, address2);
91
92         Address address3 = new Address("tcp", "system3");
93         resolver.addPeerAddress(MEMBER_3, address3);
94
95         Collection<String> peerAddresses = resolver.getShardManagerPeerActorAddresses();
96         assertEquals("getShardManagerPeerActorAddresses", Sets.newHashSet(
97                 address2.toString() + "/user/shardmanager-config",
98                 address3.toString() + "/user/shardmanager-config"), Sets.newHashSet(peerAddresses));
99     }
100 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.