Make sure registrations are closed
[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.DOMRpcImplementationRegistration;
42 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
43 import org.opendaylight.yangtools.concepts.ObjectRegistration;
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.SchemaPath;
47
48 public class OpsRegistrarTest {
49     @Mock
50     private DOMRpcProviderService rpcService;
51     @Mock
52     private DOMActionProviderService actionService;
53     @Mock
54     private DOMRpcImplementationRegistration<RemoteRpcImplementation> oldReg;
55     @Mock
56     private DOMRpcImplementationRegistration<RemoteRpcImplementation> 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(
83                 SchemaPath.create(true, QName.create("first:identifier", "foo")));
84         final DOMRpcIdentifier secondEndpointId = DOMRpcIdentifier.create(
85                 SchemaPath.create(true, QName.create("second:identifier", "bar")));
86         final QName firstActionQName = QName.create("first:actionIdentifier", "fooAction");
87
88         final DOMActionInstance firstActionInstance = DOMActionInstance.of(
89                 SchemaPath.create(true, firstActionQName), LogicalDatastoreType.OPERATIONAL,
90                 YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(firstActionQName)));
91
92         final DOMActionInstance secondActionInstance = DOMActionInstance.of(
93                 SchemaPath.create(true, firstActionQName), LogicalDatastoreType.OPERATIONAL,
94                 YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(firstActionQName)));
95
96         final TestKit senderKit = new TestKit(system);
97         firstEndpoint = new RemoteRpcEndpoint(senderKit.getRef(), Collections.singletonList(firstEndpointId));
98         secondEndpoint = new RemoteRpcEndpoint(senderKit.getRef(), Collections.singletonList(secondEndpointId));
99         firstActionEndpoint = new RemoteActionEndpoint(senderKit.getRef(),
100                 Collections.singletonList(firstActionInstance));
101         secondActionEndpoint = new RemoteActionEndpoint(senderKit.getRef(),
102                 Collections.singletonList(secondActionInstance));
103
104         doReturn(oldReg).when(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
105             eq(firstEndpoint.getRpcs()));
106         doReturn(newReg).when(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
107             eq(secondEndpoint.getRpcs()));
108
109         doReturn(oldActionReg).when(actionService).registerActionImplementation(any(RemoteActionImplementation.class),
110             eq(secondActionEndpoint.getActions()));
111         doReturn(oldActionReg).when(actionService).registerActionImplementation(any(RemoteActionImplementation.class),
112                 eq(secondActionEndpoint.getActions()));
113
114         opsRegistrar = testActorRef.underlyingActor();
115     }
116
117     @After
118     public void tearDown() {
119         TestKit.shutdownActorSystem(system, true);
120     }
121
122     @Test
123     public void testHandleReceiveAddEndpoint() {
124         final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = ImmutableMap.of(
125                 endpointAddress, Optional.of(firstEndpoint));
126         testActorRef.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
127
128         verify(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
129             eq(firstEndpoint.getRpcs()));
130         verifyNoMoreInteractions(rpcService, oldReg, newReg);
131     }
132
133     @Test
134     public void testHandleReceiveRemoveEndpoint() {
135         final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = ImmutableMap.of(
136                 endpointAddress, Optional.empty());
137         testActorRef.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
138         verifyNoMoreInteractions(rpcService, oldReg, newReg);
139     }
140
141     @Test
142     public void testHandleReceiveUpdateRpcEndpoint() {
143         final InOrder inOrder = inOrder(rpcService, oldReg, newReg);
144
145         testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(firstEndpoint))),
146                 ActorRef.noSender());
147
148         inOrder.verify(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
149                 eq(firstEndpoint.getRpcs()));
150
151         testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(secondEndpoint))),
152                 ActorRef.noSender());
153
154         inOrder.verify(rpcService).registerRpcImplementation(any(RemoteRpcImplementation.class),
155                 eq(secondEndpoint.getRpcs()));
156
157         // verify first registration is closed
158         inOrder.verify(oldReg).close();
159
160         verifyNoMoreInteractions(rpcService, oldReg, newReg);
161     }
162
163     @Test
164     public void testHandleReceiveUpdateActionEndpoint() {
165         final InOrder inOrder = inOrder(actionService, oldActionReg, newActionReg);
166
167         testActorRef.tell(new UpdateRemoteActionEndpoints(ImmutableMap.of(endpointAddress,
168                 Optional.of(firstActionEndpoint))), ActorRef.noSender());
169
170         inOrder.verify(actionService).registerActionImplementation(any(RemoteActionImplementation.class),
171                 eq(firstActionEndpoint.getActions()));
172
173         testActorRef.tell(new UpdateRemoteActionEndpoints(ImmutableMap.of(endpointAddress,
174                 Optional.of(secondActionEndpoint))), ActorRef.noSender());
175
176         inOrder.verify(actionService).registerActionImplementation(any(RemoteActionImplementation.class),
177                 eq(secondActionEndpoint.getActions()));
178
179         // verify first registration is closed
180         inOrder.verify(oldActionReg).close();
181
182         verifyNoMoreInteractions(actionService, oldActionReg, newActionReg);
183     }
184 }

©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.