2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.remote.rpc;
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.Address;
13 import akka.actor.Props;
14 import akka.testkit.TestActorRef;
15 import akka.testkit.javadsl.TestKit;
16 import com.google.common.collect.ImmutableMap;
17 import java.util.Collections;
19 import java.util.Optional;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.InOrder;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.UpdateRemoteEndpoints;
28 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.RemoteRpcEndpoint;
29 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
30 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationRegistration;
31 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 public class RpcRegistrarTest {
37 private DOMRpcProviderService service;
39 private DOMRpcImplementationRegistration<RemoteRpcImplementation> oldReg;
41 private DOMRpcImplementationRegistration<RemoteRpcImplementation> newReg;
43 private ActorSystem system;
44 private TestActorRef<RpcRegistrar> testActorRef;
45 private Address endpointAddress;
46 private RemoteRpcEndpoint firstEndpoint;
47 private RemoteRpcEndpoint secondEndpoint;
48 private RpcRegistrar rpcRegistrar;
52 MockitoAnnotations.initMocks(this);
53 system = ActorSystem.create("test");
55 final TestKit testKit = new TestKit(system);
56 final RemoteRpcProviderConfig config = new RemoteRpcProviderConfig.Builder("system").build();
57 final Props props = RpcRegistrar.props(config, service);
58 testActorRef = new TestActorRef<>(system, props, testKit.getRef(), "actorRef");
59 endpointAddress = new Address("http", "local");
61 final DOMRpcIdentifier firstEndpointId = DOMRpcIdentifier.create(
62 SchemaPath.create(true, QName.create("first:identifier", "foo")));
63 final DOMRpcIdentifier secondEndpointId = DOMRpcIdentifier.create(
64 SchemaPath.create(true, QName.create("second:identifier", "bar")));
66 final TestKit senderKit = new TestKit(system);
67 firstEndpoint = new RemoteRpcEndpoint(senderKit.getRef(), Collections.singletonList(firstEndpointId));
68 secondEndpoint = new RemoteRpcEndpoint(senderKit.getRef(), Collections.singletonList(secondEndpointId));
70 Mockito.doReturn(oldReg).when(service).registerRpcImplementation(
71 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(firstEndpoint.getRpcs()));
73 Mockito.doReturn(newReg).when(service).registerRpcImplementation(
74 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(secondEndpoint.getRpcs()));
76 rpcRegistrar = testActorRef.underlyingActor();
80 public void tearDown() {
81 TestKit.shutdownActorSystem(system, true);
85 public void testPostStop() throws Exception {
86 testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(firstEndpoint))),
88 testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(secondEndpoint))),
91 rpcRegistrar.postStop();
93 Mockito.verify(oldReg).close();
94 Mockito.verify(newReg).close();
98 public void testHandleReceiveAddEndpoint() {
99 final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = ImmutableMap.of(
100 endpointAddress, Optional.of(firstEndpoint));
101 testActorRef.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
103 Mockito.verify(service).registerRpcImplementation(
104 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(firstEndpoint.getRpcs()));
105 Mockito.verifyNoMoreInteractions(service, oldReg, newReg);
109 public void testHandleReceiveRemoveEndpoint() {
110 final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = ImmutableMap.of(
111 endpointAddress, Optional.empty());
112 testActorRef.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
113 Mockito.verifyNoMoreInteractions(service, oldReg, newReg);
117 public void testHandleReceiveUpdateEndpoint() {
118 final InOrder inOrder = Mockito.inOrder(service, oldReg, newReg);
120 testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(firstEndpoint))),
121 ActorRef.noSender());
123 // first registration
124 inOrder.verify(service).registerRpcImplementation(
125 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(firstEndpoint.getRpcs()));
127 testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(secondEndpoint))),
128 ActorRef.noSender());
130 // second registration
131 inOrder.verify(service).registerRpcImplementation(
132 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(secondEndpoint.getRpcs()));
134 // verify first registration is closed
135 inOrder.verify(oldReg).close();
137 Mockito.verifyNoMoreInteractions(service, oldReg, newReg);