Clean up sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / OpsRegistrarTest.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;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.inOrder;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.verifyNoMoreInteractions;
16
17 import akka.actor.ActorRef;
18 import akka.actor.ActorSystem;
19 import akka.actor.Address;
20 import akka.actor.Props;
21 import akka.testkit.TestActorRef;
22 import akka.testkit.javadsl.TestKit;
23 import com.google.common.collect.ImmutableMap;
24 import java.util.Collections;
25 import java.util.Map;
26 import java.util.Optional;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.InOrder;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.controller.remote.rpc.registry.ActionRegistry.Messages.UpdateRemoteActionEndpoints;
34 import org.opendaylight.controller.remote.rpc.registry.ActionRegistry.RemoteActionEndpoint;
35 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.UpdateRemoteEndpoints;
36 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.RemoteRpcEndpoint;
37 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
38 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
39 import org.opendaylight.mdsal.dom.api.DOMActionProviderService;
40 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
41 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
42 import org.opendaylight.yangtools.concepts.ObjectRegistration;
43 import org.opendaylight.yangtools.concepts.Registration;
44 import org.opendaylight.yangtools.yang.common.QName;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
47
48 public class OpsRegistrarTest {
49     @Mock
50     private DOMRpcProviderService rpcService;
51     @Mock
52     private DOMActionProviderService actionService;
53     @Mock
54     private Registration oldReg;
55     @Mock
56     private Registration newReg;
57     @Mock
58     private ObjectRegistration<RemoteActionImplementation> oldActionReg;
59     @Mock
60     private ObjectRegistration<RemoteActionImplementation> newActionReg;
61
62     private ActorSystem system;
63     private TestActorRef<OpsRegistrar> testActorRef;
64     private Address endpointAddress;
65     private RemoteRpcEndpoint firstEndpoint;
66     private RemoteRpcEndpoint secondEndpoint;
67     private RemoteActionEndpoint firstActionEndpoint;
68     private RemoteActionEndpoint secondActionEndpoint;
69     private OpsRegistrar opsRegistrar;
70
71     @Before
72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74         system = ActorSystem.create("test");
75
76         final TestKit testKit = new TestKit(system);
77         final RemoteOpsProviderConfig config = new RemoteOpsProviderConfig.Builder("system").build();
78         final Props props = OpsRegistrar.props(config, rpcService, actionService);
79         testActorRef = new TestActorRef<>(system, props, testKit.getRef(), "actorRef");
80         endpointAddress = new Address("http", "local");
81
82         final DOMRpcIdentifier firstEndpointId = DOMRpcIdentifier.create(QName.create("first:identifier", "foo"));
83         final DOMRpcIdentifier secondEndpointId = DOMRpcIdentifier.create(QName.create("second:identifier", "bar"));
84         final QName firstActionQName = QName.create("first:actionIdentifier", "fooAction");
85
86         final DOMActionInstance firstActionInstance = DOMActionInstance.of(Absolute.of(firstActionQName),
87                 LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.of(firstActionQName));
88
89         final DOMActionInstance secondActionInstance = DOMActionInstance.of(Absolute.of(firstActionQName),
90                 LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.of(firstActionQName));
91
92         final TestKit senderKit = new TestKit(system);
93         firstEndpoint = new RemoteRpcEndpoint(senderKit.getRef(), Collections.singletonList(firstEndpointId));
94         secondEndpoint = new RemoteRpcEndpoint(senderKit.getRef(), Collections.singletonList(secondEndpointId));
95         firstActionEndpoint = new RemoteActionEndpoint(senderKit.getRef(),
96                 Collections.singletonList(firstActionInstance));
97         secondActionEndpoint = new RemoteActionEndpoint(senderKit.getRef(),
98                 Collections.singletonList(secondActionInstance));
99
100         doReturn(oldReg).when(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
101             eq(firstEndpoint.getRpcs()));
102         doReturn(newReg).when(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
103             eq(secondEndpoint.getRpcs()));
104
105         doReturn(oldActionReg).when(actionService).registerActionImplementation(any(RemoteActionImplementation.class),
106             eq(secondActionEndpoint.getActions()));
107         doReturn(oldActionReg).when(actionService).registerActionImplementation(any(RemoteActionImplementation.class),
108                 eq(secondActionEndpoint.getActions()));
109
110         opsRegistrar = testActorRef.underlyingActor();
111     }
112
113     @After
114     public void tearDown() {
115         TestKit.shutdownActorSystem(system, true);
116     }
117
118     @Test
119     public void testHandleReceiveAddEndpoint() {
120         final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = ImmutableMap.of(
121                 endpointAddress, Optional.of(firstEndpoint));
122         testActorRef.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
123
124         verify(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
125             eq(firstEndpoint.getRpcs()));
126         verifyNoMoreInteractions(rpcService, oldReg, newReg);
127     }
128
129     @Test
130     public void testHandleReceiveRemoveEndpoint() {
131         final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = ImmutableMap.of(
132                 endpointAddress, Optional.empty());
133         testActorRef.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
134         verifyNoMoreInteractions(rpcService, oldReg, newReg);
135     }
136
137     @Test
138     public void testHandleReceiveUpdateRpcEndpoint() {
139         final InOrder inOrder = inOrder(rpcService, oldReg, newReg);
140
141         testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(firstEndpoint))),
142                 ActorRef.noSender());
143
144         inOrder.verify(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
145                 eq(firstEndpoint.getRpcs()));
146
147         testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(secondEndpoint))),
148                 ActorRef.noSender());
149
150         inOrder.verify(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
151                 eq(secondEndpoint.getRpcs()));
152
153         // verify first registration is closed
154         inOrder.verify(oldReg).close();
155
156         verifyNoMoreInteractions(rpcService, oldReg, newReg);
157     }
158
159     @Test
160     public void testHandleReceiveUpdateActionEndpoint() {
161         final InOrder inOrder = inOrder(actionService, oldActionReg, newActionReg);
162
163         testActorRef.tell(new UpdateRemoteActionEndpoints(ImmutableMap.of(endpointAddress,
164                 Optional.of(firstActionEndpoint))), ActorRef.noSender());
165
166         inOrder.verify(actionService).registerActionImplementation(any(RemoteActionImplementation.class),
167                 eq(firstActionEndpoint.getActions()));
168
169         testActorRef.tell(new UpdateRemoteActionEndpoints(ImmutableMap.of(endpointAddress,
170                 Optional.of(secondActionEndpoint))), ActorRef.noSender());
171
172         inOrder.verify(actionService).registerActionImplementation(any(RemoteActionImplementation.class),
173                 eq(secondActionEndpoint.getActions()));
174
175         // verify first registration is closed
176         inOrder.verify(oldActionReg).close();
177
178         verifyNoMoreInteractions(actionService, oldActionReg, newActionReg);
179     }
180 }