akka.actor.provider set to 'cluster'
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / registry / mbeans / RemoteRpcRegistryMXBeanImplTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.remote.rpc.registry.mbeans;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import akka.actor.ActorRef;
17 import akka.actor.ActorSystem;
18 import akka.actor.Props;
19 import akka.dispatch.Dispatchers;
20 import akka.testkit.TestActorRef;
21 import akka.testkit.javadsl.TestKit;
22 import akka.util.Timeout;
23 import com.google.common.collect.Lists;
24 import com.typesafe.config.ConfigFactory;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.concurrent.TimeUnit;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.opendaylight.controller.remote.rpc.RemoteOpsProviderConfig;
34 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
35 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
36 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39
40 public class RemoteRpcRegistryMXBeanImplTest {
41     private static final QName LOCAL_QNAME = QName.create("base", "local");
42     private static final QName REMOTE_QNAME = QName.create("base", "remote");
43
44     private ActorSystem system;
45     private TestActorRef<RpcRegistry> testActor;
46     private List<DOMRpcIdentifier> buckets;
47     private RemoteRpcRegistryMXBeanImpl mxBean;
48
49     @Before
50     public void setUp() {
51         system = ActorSystem.create("test", ConfigFactory.load().getConfig("unit-test"));
52
53         final DOMRpcIdentifier emptyRpcIdentifier = DOMRpcIdentifier.create(
54                 REMOTE_QNAME, YangInstanceIdentifier.empty());
55         final DOMRpcIdentifier localRpcIdentifier = DOMRpcIdentifier.create(
56                 LOCAL_QNAME, YangInstanceIdentifier.of(LOCAL_QNAME));
57
58         buckets = Lists.newArrayList(emptyRpcIdentifier, localRpcIdentifier);
59
60         final RemoteOpsProviderConfig config = new RemoteOpsProviderConfig.Builder("system").build();
61         final TestKit invoker = new TestKit(system);
62         final TestKit registrar = new TestKit(system);
63         final TestKit supervisor = new TestKit(system);
64         final Props props = RpcRegistry.props(config, invoker.getRef(), registrar.getRef())
65                 .withDispatcher(Dispatchers.DefaultDispatcherId());
66         testActor = new TestActorRef<>(system, props, supervisor.getRef(), "testActor");
67
68         final Timeout timeout = Timeout.apply(10, TimeUnit.SECONDS);
69         mxBean = new RemoteRpcRegistryMXBeanImpl(new BucketStoreAccess(testActor, system.dispatcher(), timeout),
70                 timeout);
71     }
72
73     @After
74     public void tearDown() {
75         TestKit.shutdownActorSystem(system, Boolean.TRUE);
76     }
77
78     @Test
79     public void testGetGlobalRpcEmptyBuckets() {
80         final Set<String> globalRpc = mxBean.getGlobalRpc();
81
82         assertNotNull(globalRpc);
83         assertTrue(globalRpc.isEmpty());
84     }
85
86     @Test
87     public void testGetGlobalRpc() {
88         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
89         final Set<String> globalRpc = mxBean.getGlobalRpc();
90
91         assertNotNull(globalRpc);
92         assertEquals(1, globalRpc.size());
93
94         final String rpc = globalRpc.iterator().next();
95         assertEquals(REMOTE_QNAME.toString(), rpc);
96     }
97
98     @Test
99     public void testGetLocalRegisteredRoutedRpcEmptyBuckets() {
100         final Set<String> localRegisteredRoutedRpc = mxBean.getLocalRegisteredRoutedRpc();
101
102         assertNotNull(localRegisteredRoutedRpc);
103         assertTrue(localRegisteredRoutedRpc.isEmpty());
104     }
105
106     @Test
107     public void testGetLocalRegisteredRoutedRpc() {
108         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
109         final Set<String> localRegisteredRoutedRpc = mxBean.getLocalRegisteredRoutedRpc();
110
111         assertNotNull(localRegisteredRoutedRpc);
112         assertEquals(1, localRegisteredRoutedRpc.size());
113
114         final String localRpc = localRegisteredRoutedRpc.iterator().next();
115         assertThat(localRpc, containsString(LOCAL_QNAME.toString()));
116     }
117
118     @Test
119     public void testFindRpcByNameEmptyBuckets() {
120         final Map<String, String> rpcByName = mxBean.findRpcByName("");
121
122         assertNotNull(rpcByName);
123         assertTrue(rpcByName.isEmpty());
124     }
125
126     @Test
127     public void testFindRpcByName() {
128         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
129         final Map<String, String> rpcByName = mxBean.findRpcByName("");
130
131         assertNotNull(rpcByName);
132         assertEquals(1, rpcByName.size());
133         assertTrue(rpcByName.containsValue(LOCAL_QNAME.getLocalName()));
134     }
135
136     @Test
137     public void testFindRpcByRouteEmptyBuckets() {
138         final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
139
140         assertNotNull(rpcByRoute);
141         assertTrue(rpcByRoute.isEmpty());
142     }
143
144     @Test
145     public void testFindRpcByRoute() {
146         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
147         final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
148
149         assertNotNull(rpcByRoute);
150         assertEquals(1, rpcByRoute.size());
151         assertTrue(rpcByRoute.containsValue(LOCAL_QNAME.getLocalName()));
152     }
153
154     @Test
155     public void testGetBucketVersionsEmptyBuckets() {
156         final String bucketVersions = mxBean.getBucketVersions();
157         assertEquals(Collections.emptyMap().toString(), bucketVersions);
158     }
159
160     @Test
161     public void testGetBucketVersions() {
162         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
163         final String bucketVersions = mxBean.getBucketVersions();
164
165         assertTrue(bucketVersions.contains(testActor.provider().getDefaultAddress().toString()));
166     }
167 }