2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.remote.rpc.registry.mbeans;
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;
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 java.util.Collections;
25 import java.util.List;
28 import java.util.concurrent.TimeUnit;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.opendaylight.controller.remote.rpc.RemoteOpsProviderConfig;
33 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
34 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
35 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 public class RemoteRpcRegistryMXBeanImplTest {
40 private static final QName LOCAL_QNAME = QName.create("base", "local");
41 private static final QName REMOTE_QNAME = QName.create("base", "remote");
43 private ActorSystem system;
44 private TestActorRef<RpcRegistry> testActor;
45 private List<DOMRpcIdentifier> buckets;
46 private RemoteRpcRegistryMXBeanImpl mxBean;
50 system = ActorSystem.create("test");
52 final DOMRpcIdentifier emptyRpcIdentifier = DOMRpcIdentifier.create(
53 REMOTE_QNAME, YangInstanceIdentifier.empty());
54 final DOMRpcIdentifier localRpcIdentifier = DOMRpcIdentifier.create(
55 LOCAL_QNAME, YangInstanceIdentifier.of(LOCAL_QNAME));
57 buckets = Lists.newArrayList(emptyRpcIdentifier, localRpcIdentifier);
59 final RemoteOpsProviderConfig config = new RemoteOpsProviderConfig.Builder("system").build();
60 final TestKit invoker = new TestKit(system);
61 final TestKit registrar = new TestKit(system);
62 final TestKit supervisor = new TestKit(system);
63 final Props props = RpcRegistry.props(config, invoker.getRef(), registrar.getRef())
64 .withDispatcher(Dispatchers.DefaultDispatcherId());
65 testActor = new TestActorRef<>(system, props, supervisor.getRef(), "testActor");
67 final Timeout timeout = Timeout.apply(10, TimeUnit.SECONDS);
68 mxBean = new RemoteRpcRegistryMXBeanImpl(new BucketStoreAccess(testActor, system.dispatcher(), timeout),
73 public void tearDown() {
74 TestKit.shutdownActorSystem(system, Boolean.TRUE);
78 public void testGetGlobalRpcEmptyBuckets() {
79 final Set<String> globalRpc = mxBean.getGlobalRpc();
81 assertNotNull(globalRpc);
82 assertTrue(globalRpc.isEmpty());
86 public void testGetGlobalRpc() {
87 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
88 final Set<String> globalRpc = mxBean.getGlobalRpc();
90 assertNotNull(globalRpc);
91 assertEquals(1, globalRpc.size());
93 final String rpc = globalRpc.iterator().next();
94 assertEquals(REMOTE_QNAME.toString(), rpc);
98 public void testGetLocalRegisteredRoutedRpcEmptyBuckets() {
99 final Set<String> localRegisteredRoutedRpc = mxBean.getLocalRegisteredRoutedRpc();
101 assertNotNull(localRegisteredRoutedRpc);
102 assertTrue(localRegisteredRoutedRpc.isEmpty());
106 public void testGetLocalRegisteredRoutedRpc() {
107 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
108 final Set<String> localRegisteredRoutedRpc = mxBean.getLocalRegisteredRoutedRpc();
110 assertNotNull(localRegisteredRoutedRpc);
111 assertEquals(1, localRegisteredRoutedRpc.size());
113 final String localRpc = localRegisteredRoutedRpc.iterator().next();
114 assertThat(localRpc, containsString(LOCAL_QNAME.toString()));
118 public void testFindRpcByNameEmptyBuckets() {
119 final Map<String, String> rpcByName = mxBean.findRpcByName("");
121 assertNotNull(rpcByName);
122 assertTrue(rpcByName.isEmpty());
126 public void testFindRpcByName() {
127 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
128 final Map<String, String> rpcByName = mxBean.findRpcByName("");
130 assertNotNull(rpcByName);
131 assertEquals(1, rpcByName.size());
132 assertTrue(rpcByName.containsValue(LOCAL_QNAME.getLocalName()));
136 public void testFindRpcByRouteEmptyBuckets() {
137 final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
139 assertNotNull(rpcByRoute);
140 assertTrue(rpcByRoute.isEmpty());
144 public void testFindRpcByRoute() {
145 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
146 final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
148 assertNotNull(rpcByRoute);
149 assertEquals(1, rpcByRoute.size());
150 assertTrue(rpcByRoute.containsValue(LOCAL_QNAME.getLocalName()));
154 public void testGetBucketVersionsEmptyBuckets() {
155 final String bucketVersions = mxBean.getBucketVersions();
156 assertEquals(Collections.emptyMap().toString(), bucketVersions);
160 public void testGetBucketVersions() {
161 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
162 final String bucketVersions = mxBean.getBucketVersions();
164 assertTrue(bucketVersions.contains(testActor.provider().getDefaultAddress().toString()));