Migrate from YangInstanceIdentifier.EMPTY
[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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSystem;
16 import akka.actor.Props;
17 import akka.dispatch.Dispatchers;
18 import akka.testkit.TestActorRef;
19 import akka.testkit.javadsl.TestKit;
20 import akka.util.Timeout;
21 import com.google.common.collect.Lists;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.opendaylight.controller.remote.rpc.RemoteOpsProviderConfig;
31 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
32 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
33 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37
38 public class RemoteRpcRegistryMXBeanImplTest {
39
40     private static final QName LOCAL_QNAME = QName.create("base", "local");
41     private static final SchemaPath EMPTY_SCHEMA_PATH = SchemaPath.ROOT;
42     private static final SchemaPath LOCAL_SCHEMA_PATH = SchemaPath.create(true, LOCAL_QNAME);
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");
52
53         final DOMRpcIdentifier emptyRpcIdentifier = DOMRpcIdentifier.create(
54                 EMPTY_SCHEMA_PATH, YangInstanceIdentifier.empty());
55         final DOMRpcIdentifier localRpcIdentifier = DOMRpcIdentifier.create(
56                 LOCAL_SCHEMA_PATH, 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(EMPTY_SCHEMA_PATH.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         assertTrue(localRpc.contains(LOCAL_QNAME.toString()));
116         assertTrue(localRpc.contains(LOCAL_SCHEMA_PATH.toString()));
117     }
118
119     @Test
120     public void testFindRpcByNameEmptyBuckets() {
121         final Map<String, String> rpcByName = mxBean.findRpcByName("");
122
123         assertNotNull(rpcByName);
124         assertTrue(rpcByName.isEmpty());
125     }
126
127     @Test
128     public void testFindRpcByName() {
129         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
130         final Map<String, String> rpcByName = mxBean.findRpcByName("");
131
132         assertNotNull(rpcByName);
133         assertEquals(1, rpcByName.size());
134         assertTrue(rpcByName.containsValue(LOCAL_QNAME.getLocalName()));
135     }
136
137     @Test
138     public void testFindRpcByRouteEmptyBuckets() {
139         final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
140
141         assertNotNull(rpcByRoute);
142         assertTrue(rpcByRoute.isEmpty());
143     }
144
145     @Test
146     public void testFindRpcByRoute() {
147         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
148         final Map<String, String> rpcByRoute = mxBean.findRpcByRoute("");
149
150         assertNotNull(rpcByRoute);
151         assertEquals(1, rpcByRoute.size());
152         assertTrue(rpcByRoute.containsValue(LOCAL_QNAME.getLocalName()));
153     }
154
155     @Test
156     public void testGetBucketVersionsEmptyBuckets() {
157         final String bucketVersions = mxBean.getBucketVersions();
158         assertEquals(Collections.emptyMap().toString(), bucketVersions);
159     }
160
161     @Test
162     public void testGetBucketVersions() {
163         testActor.tell(new RpcRegistry.Messages.AddOrUpdateRoutes(Lists.newArrayList(buckets)), ActorRef.noSender());
164         final String bucketVersions = mxBean.getBucketVersions();
165
166         assertTrue(bucketVersions.contains(testActor.provider().getDefaultAddress().toString()));
167     }
168 }