Teach sal-remoterpc-connector to route actions
[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.SchemaPath;
35
36 public class RemoteActionRegistryMXBeanImplTest {
37
38     private static final QName LOCAL_QNAME = QName.create("base", "local");
39     private static final SchemaPath EMPTY_SCHEMA_PATH = SchemaPath.ROOT;
40     private static final SchemaPath LOCAL_SCHEMA_PATH = SchemaPath.create(true, LOCAL_QNAME);
41
42     private ActorSystem system;
43     private TestActorRef<ActionRegistry> testActor;
44     private List<DOMActionInstance> buckets;
45     private RemoteActionRegistryMXBeanImpl mxBean;
46
47     @Before
48     public void setUp() {
49         system = ActorSystem.create("test");
50
51         final DOMActionInstance emptyActionIdentifier = DOMActionInstance.of(
52                 EMPTY_SCHEMA_PATH, LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.EMPTY);
53         final DOMActionInstance localActionIdentifier = DOMActionInstance.of(
54                 LOCAL_SCHEMA_PATH, LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.of(LOCAL_QNAME));
55
56         buckets = Lists.newArrayList(emptyActionIdentifier, localActionIdentifier);
57
58         final RemoteOpsProviderConfig config = new RemoteOpsProviderConfig.Builder("system").build();
59         final TestKit invoker = new TestKit(system);
60         final TestKit registrar = new TestKit(system);
61         final TestKit supervisor = new TestKit(system);
62         final Props props = ActionRegistry.props(config, invoker.getRef(), registrar.getRef())
63                 .withDispatcher(Dispatchers.DefaultDispatcherId());
64         testActor = new TestActorRef<>(system, props, supervisor.getRef(), "testActor");
65
66         final Timeout timeout = Timeout.apply(10, TimeUnit.SECONDS);
67         mxBean = new RemoteActionRegistryMXBeanImpl(new BucketStoreAccess(testActor, system.dispatcher(), timeout),
68                 timeout);
69     }
70
71     @After
72     public void tearDown() {
73         TestKit.shutdownActorSystem(system, Boolean.TRUE);
74     }
75
76     @Test
77     public void testGetLocalRegisteredRoutedActionEmptyBuckets() {
78         final Set<String> localRegisteredRoutedAction = mxBean.getLocalRegisteredAction();
79
80         Assert.assertNotNull(localRegisteredRoutedAction);
81         Assert.assertTrue(localRegisteredRoutedAction.isEmpty());
82     }
83
84     @Test
85     public void testGetLocalRegisteredRoutedAction() {
86         testActor.tell(new ActionRegistry.Messages.UpdateActions(Lists.newArrayList(buckets),
87                 Collections.emptyList()), ActorRef.noSender());
88         final Set<String> localRegisteredRoutedAction = mxBean.getLocalRegisteredAction();
89
90         Assert.assertNotNull(localRegisteredRoutedAction);
91         Assert.assertEquals(1, localRegisteredRoutedAction.size());
92
93         final String localAction = localRegisteredRoutedAction.iterator().next();
94         Assert.assertTrue(localAction.contains(LOCAL_QNAME.toString()));
95         Assert.assertTrue(localAction.contains(LOCAL_SCHEMA_PATH.toString()));
96     }
97
98     @Test
99     public void testFindActionByNameEmptyBuckets() {
100         final Map<String, String> rpcByName = mxBean.findActionByName("");
101
102         Assert.assertNotNull(rpcByName);
103         Assert.assertTrue(rpcByName.isEmpty());
104     }
105
106     @Test
107     public void testFindActionByName() {
108         testActor.tell(new ActionRegistry.Messages.UpdateActions(Lists.newArrayList(buckets),
109                 Collections.emptyList()), ActorRef.noSender());
110         final Map<String, String> rpcByName = mxBean.findActionByName("");
111
112         Assert.assertNotNull(rpcByName);
113         Assert.assertEquals(1, rpcByName.size());
114         Assert.assertTrue(rpcByName.containsValue(LOCAL_QNAME.getLocalName()));
115     }
116
117     @Test
118     public void testFindActionByRouteEmptyBuckets() {
119         final Map<String, String> rpcByRoute = mxBean.findActionByRoute("");
120
121         Assert.assertNotNull(rpcByRoute);
122         Assert.assertTrue(rpcByRoute.isEmpty());
123     }
124
125     @Test
126     public void testFindActionByRoute() {
127         testActor.tell(new ActionRegistry.Messages.UpdateActions(Lists.newArrayList(buckets),
128                 Collections.emptyList()), ActorRef.noSender());
129         final Map<String, String> rpcByRoute = mxBean.findActionByRoute("");
130
131         Assert.assertNotNull(rpcByRoute);
132         Assert.assertEquals(1, rpcByRoute.size());
133         Assert.assertTrue(rpcByRoute.containsValue(LOCAL_QNAME.getLocalName()));
134     }
135
136     @Test
137     public void testGetBucketVersionsEmptyBuckets() {
138         final String bucketVersions = mxBean.getBucketVersions();
139         Assert.assertEquals(Collections.emptyMap().toString(), bucketVersions);
140     }
141
142     @Test
143     public void testGetBucketVersions() {
144         testActor.tell(new ActionRegistry.Messages.UpdateActions(Lists.newArrayList(buckets),
145                 Collections.emptyList()), ActorRef.noSender());
146         final String bucketVersions = mxBean.getBucketVersions();
147
148         Assert.assertTrue(bucketVersions.contains(testActor.provider().getDefaultAddress().toString()));
149     }
150 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.