6421750e0d38a19b51080d6134713d81d8cad032
[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 java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
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;
38
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");
42
43     private ActorSystem system;
44     private TestActorRef<RpcRegistry> testActor;
45     private List<DOMRpcIdentifier> buckets;
46     private RemoteRpcRegistryMXBeanImpl mxBean;
47
48     @Before
49     public void setUp() {
50         system = ActorSystem.create("test");
51
52         final DOMRpcIdentifier emptyRpcIdentifier = DOMRpcIdentifier.create(
53                 REMOTE_QNAME, YangInstanceIdentifier.empty());
54         final DOMRpcIdentifier localRpcIdentifier = DOMRpcIdentifier.create(
55                 LOCAL_QNAME, YangInstanceIdentifier.of(LOCAL_QNAME));
56
57         buckets = Lists.newArrayList(emptyRpcIdentifier, localRpcIdentifier);
58
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");
66
67         final Timeout timeout = Timeout.apply(10, TimeUnit.SECONDS);
68         mxBean = new RemoteRpcRegistryMXBeanImpl(new BucketStoreAccess(testActor, system.dispatcher(), timeout),
69                 timeout);
70     }
71
72     @After
73     public void tearDown() {
74         TestKit.shutdownActorSystem(system, Boolean.TRUE);
75     }
76
77     @Test
78     public void testGetGlobalRpcEmptyBuckets() {
79         final Set<String> globalRpc = mxBean.getGlobalRpc();
80
81         assertNotNull(globalRpc);
82         assertTrue(globalRpc.isEmpty());
83     }
84
85     @Test
86     public void testGetGlobalRpc() {
87         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
88         final Set<String> globalRpc = mxBean.getGlobalRpc();
89
90         assertNotNull(globalRpc);
91         assertEquals(1, globalRpc.size());
92
93         final String rpc = globalRpc.iterator().next();
94         assertEquals(REMOTE_QNAME.toString(), rpc);
95     }
96
97     @Test
98     public void testGetLocalRegisteredRoutedRpcEmptyBuckets() {
99         final Set<String> localRegisteredRoutedRpc = mxBean.getLocalRegisteredRoutedRpc();
100
101         assertNotNull(localRegisteredRoutedRpc);
102         assertTrue(localRegisteredRoutedRpc.isEmpty());
103     }
104
105     @Test
106     public void testGetLocalRegisteredRoutedRpc() {
107         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
108         final Set<String> localRegisteredRoutedRpc = mxBean.getLocalRegisteredRoutedRpc();
109
110         assertNotNull(localRegisteredRoutedRpc);
111         assertEquals(1, localRegisteredRoutedRpc.size());
112
113         final String localRpc = localRegisteredRoutedRpc.iterator().next();
114         assertThat(localRpc, containsString(LOCAL_QNAME.toString()));
115     }
116
117     @Test
118     public void testFindRpcByNameEmptyBuckets() {
119         final Map<String, String> rpcByName = mxBean.findRpcByName("");
120
121         assertNotNull(rpcByName);
122         assertTrue(rpcByName.isEmpty());
123     }
124
125     @Test
126     public void testFindRpcByName() {
127         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
128         final Map<String, String> rpcByName = mxBean.findRpcByName("");
129
130         assertNotNull(rpcByName);
131         assertEquals(1, rpcByName.size());
132         assertTrue(rpcByName.containsValue(LOCAL_QNAME.getLocalName()));
133     }
134
135     @Test
136     public void testFindRpcByRouteEmptyBuckets() {
137         final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
138
139         assertNotNull(rpcByRoute);
140         assertTrue(rpcByRoute.isEmpty());
141     }
142
143     @Test
144     public void testFindRpcByRoute() {
145         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
146         final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
147
148         assertNotNull(rpcByRoute);
149         assertEquals(1, rpcByRoute.size());
150         assertTrue(rpcByRoute.containsValue(LOCAL_QNAME.getLocalName()));
151     }
152
153     @Test
154     public void testGetBucketVersionsEmptyBuckets() {
155         final String bucketVersions = mxBean.getBucketVersions();
156         assertEquals(Collections.emptyMap().toString(), bucketVersions);
157     }
158
159     @Test
160     public void testGetBucketVersions() {
161         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
162         final String bucketVersions = mxBean.getBucketVersions();
163
164         assertTrue(bucketVersions.contains(testActor.provider().getDefaultAddress().toString()));
165     }
166 }