1a76aa6f9c1d8c82c4a02685a568e57afd84cb1f
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / registry / mbeans / RemoteActionRegistryMXBeanImplTest.java
1 /*
2  * Copyright (c) 2019 Nordix Foundation.  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 akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.Props;
13 import akka.dispatch.Dispatchers;
14 import akka.testkit.TestActorRef;
15 import akka.testkit.javadsl.TestKit;
16 import akka.util.Timeout;
17 import com.google.common.collect.Lists;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.TimeUnit;
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.remote.rpc.RemoteOpsProviderConfig;
28 import org.opendaylight.controller.remote.rpc.registry.ActionRegistry;
29 import org.opendaylight.controller.remote.rpc.registry.gossip.BucketStoreAccess;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
35
36 public class RemoteActionRegistryMXBeanImplTest {
37
38     private static final QName LOCAL_QNAME = QName.create("base", "local");
39     private static final QName REMOTE_QNAME = QName.create("base", "local");
40     private static final Absolute LOCAL_SCHEMA_PATH = Absolute.of(LOCAL_QNAME);
41     private static final Absolute REMOTE_SCHEMA_PATH = Absolute.of(REMOTE_QNAME);
42
43     private ActorSystem system;
44     private TestActorRef<ActionRegistry> testActor;
45     private List<DOMActionInstance> buckets;
46     private RemoteActionRegistryMXBeanImpl mxBean;
47
48     @Before
49     public void setUp() {
50         system = ActorSystem.create("test");
51
52         final DOMActionInstance emptyActionIdentifier = DOMActionInstance.of(
53                 REMOTE_SCHEMA_PATH, LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.empty());
54         final DOMActionInstance localActionIdentifier = DOMActionInstance.of(
55                 LOCAL_SCHEMA_PATH, LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.of(LOCAL_QNAME));
56
57         buckets = Lists.newArrayList(emptyActionIdentifier, localActionIdentifier);
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 = ActionRegistry.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 RemoteActionRegistryMXBeanImpl(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 testGetLocalRegisteredRoutedActionEmptyBuckets() {
79         final Set<String> localRegisteredRoutedAction = mxBean.getLocalRegisteredAction();
80
81         Assert.assertNotNull(localRegisteredRoutedAction);
82         Assert.assertTrue(localRegisteredRoutedAction.isEmpty());
83     }
84
85     @Test
86     public void testGetLocalRegisteredRoutedAction() {
87         testActor.tell(new ActionRegistry.Messages.UpdateActions(Lists.newArrayList(buckets),
88                 Collections.emptyList()), ActorRef.noSender());
89         final Set<String> localRegisteredRoutedAction = mxBean.getLocalRegisteredAction();
90
91         Assert.assertNotNull(localRegisteredRoutedAction);
92         Assert.assertEquals(1, localRegisteredRoutedAction.size());
93
94         final String localAction = localRegisteredRoutedAction.iterator().next();
95         Assert.assertTrue(localAction.contains(LOCAL_QNAME.toString()));
96         Assert.assertTrue(localAction.contains(LOCAL_SCHEMA_PATH.toString()));
97     }
98
99     @Test
100     public void testFindActionByNameEmptyBuckets() {
101         final Map<String, String> rpcByName = mxBean.findActionByName("");
102
103         Assert.assertNotNull(rpcByName);
104         Assert.assertTrue(rpcByName.isEmpty());
105     }
106
107     @Test
108     public void testFindActionByName() {
109         testActor.tell(new ActionRegistry.Messages.UpdateActions(Lists.newArrayList(buckets),
110                 Collections.emptyList()), ActorRef.noSender());
111         final Map<String, String> rpcByName = mxBean.findActionByName("");
112
113         Assert.assertNotNull(rpcByName);
114         Assert.assertEquals(1, rpcByName.size());
115         Assert.assertTrue(rpcByName.containsValue(LOCAL_QNAME.getLocalName()));
116     }
117
118     @Test
119     public void testFindActionByRouteEmptyBuckets() {
120         final Map<String, String> rpcByRoute = mxBean.findActionByRoute("");
121
122         Assert.assertNotNull(rpcByRoute);
123         Assert.assertTrue(rpcByRoute.isEmpty());
124     }
125
126     @Test
127     public void testFindActionByRoute() {
128         testActor.tell(new ActionRegistry.Messages.UpdateActions(Lists.newArrayList(buckets),
129                 Collections.emptyList()), ActorRef.noSender());
130         final Map<String, String> rpcByRoute = mxBean.findActionByRoute("");
131
132         Assert.assertNotNull(rpcByRoute);
133         Assert.assertEquals(1, rpcByRoute.size());
134         Assert.assertTrue(rpcByRoute.containsValue(LOCAL_QNAME.getLocalName()));
135     }
136
137     @Test
138     public void testGetBucketVersionsEmptyBuckets() {
139         final String bucketVersions = mxBean.getBucketVersions();
140         Assert.assertEquals(Collections.emptyMap().toString(), bucketVersions);
141     }
142
143     @Test
144     public void testGetBucketVersions() {
145         testActor.tell(new ActionRegistry.Messages.UpdateActions(Lists.newArrayList(buckets),
146                 Collections.emptyList()), ActorRef.noSender());
147         final String bucketVersions = mxBean.getBucketVersions();
148
149         Assert.assertTrue(bucketVersions.contains(testActor.provider().getDefaultAddress().toString()));
150     }
151 }