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 com.typesafe.config.ConfigFactory;
25 import java.util.Collections;
26 import java.util.List;
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;
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");
44 private ActorSystem system;
45 private TestActorRef<RpcRegistry> testActor;
46 private List<DOMRpcIdentifier> buckets;
47 private RemoteRpcRegistryMXBeanImpl mxBean;
51 system = ActorSystem.create("test", ConfigFactory.load().getConfig("unit-test"));
53 final DOMRpcIdentifier emptyRpcIdentifier = DOMRpcIdentifier.create(
54 REMOTE_QNAME, YangInstanceIdentifier.of());
55 final DOMRpcIdentifier localRpcIdentifier = DOMRpcIdentifier.create(
56 LOCAL_QNAME, YangInstanceIdentifier.of(LOCAL_QNAME));
58 buckets = Lists.newArrayList(emptyRpcIdentifier, localRpcIdentifier);
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");
68 final Timeout timeout = Timeout.apply(10, TimeUnit.SECONDS);
69 mxBean = new RemoteRpcRegistryMXBeanImpl(new BucketStoreAccess(testActor, system.dispatcher(), timeout),
74 public void tearDown() {
75 TestKit.shutdownActorSystem(system, Boolean.TRUE);
79 public void testGetGlobalRpcEmptyBuckets() {
80 final Set<String> globalRpc = mxBean.getGlobalRpc();
82 assertNotNull(globalRpc);
83 assertTrue(globalRpc.isEmpty());
87 public void testGetGlobalRpc() {
88 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
89 final Set<String> globalRpc = mxBean.getGlobalRpc();
91 assertNotNull(globalRpc);
92 assertEquals(1, globalRpc.size());
94 final String rpc = globalRpc.iterator().next();
95 assertEquals(REMOTE_QNAME.toString(), rpc);
99 public void testGetLocalRegisteredRoutedRpcEmptyBuckets() {
100 final Set<String> localRegisteredRoutedRpc = mxBean.getLocalRegisteredRoutedRpc();
102 assertNotNull(localRegisteredRoutedRpc);
103 assertTrue(localRegisteredRoutedRpc.isEmpty());
107 public void testGetLocalRegisteredRoutedRpc() {
108 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
109 final Set<String> localRegisteredRoutedRpc = mxBean.getLocalRegisteredRoutedRpc();
111 assertNotNull(localRegisteredRoutedRpc);
112 assertEquals(1, localRegisteredRoutedRpc.size());
114 final String localRpc = localRegisteredRoutedRpc.iterator().next();
115 assertThat(localRpc, containsString(LOCAL_QNAME.toString()));
119 public void testFindRpcByNameEmptyBuckets() {
120 final Map<String, String> rpcByName = mxBean.findRpcByName("");
122 assertNotNull(rpcByName);
123 assertTrue(rpcByName.isEmpty());
127 public void testFindRpcByName() {
128 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
129 final Map<String, String> rpcByName = mxBean.findRpcByName("");
131 assertNotNull(rpcByName);
132 assertEquals(1, rpcByName.size());
133 assertTrue(rpcByName.containsValue(LOCAL_QNAME.getLocalName()));
137 public void testFindRpcByRouteEmptyBuckets() {
138 final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
140 assertNotNull(rpcByRoute);
141 assertTrue(rpcByRoute.isEmpty());
145 public void testFindRpcByRoute() {
146 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
147 final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
149 assertNotNull(rpcByRoute);
150 assertEquals(1, rpcByRoute.size());
151 assertTrue(rpcByRoute.containsValue(LOCAL_QNAME.getLocalName()));
155 public void testGetBucketVersionsEmptyBuckets() {
156 final String bucketVersions = mxBean.getBucketVersions();
157 assertEquals(Collections.emptyMap().toString(), bucketVersions);
161 public void testGetBucketVersions() {
162 testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
163 final String bucketVersions = mxBean.getBucketVersions();
165 assertTrue(bucketVersions.contains(testActor.provider().getDefaultAddress().toString()));