Remove unused exceptions
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / RpcRegistrarTest.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 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;
18 import java.util.Map;
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.md.sal.dom.api.DOMRpcIdentifier;
28 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
30 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.UpdateRemoteEndpoints;
31 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.RemoteRpcEndpoint;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34
35 public class RpcRegistrarTest {
36     @Mock
37     private DOMRpcProviderService service;
38     @Mock
39     private DOMRpcImplementationRegistration<RemoteRpcImplementation> oldReg;
40     @Mock
41     private DOMRpcImplementationRegistration<RemoteRpcImplementation> newReg;
42
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;
49
50     @Before
51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53         system = ActorSystem.create("test");
54
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");
60
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")));
65
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));
69
70         Mockito.doReturn(oldReg).when(service).registerRpcImplementation(
71                 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(firstEndpoint.getRpcs()));
72
73         Mockito.doReturn(newReg).when(service).registerRpcImplementation(
74                 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(secondEndpoint.getRpcs()));
75
76         rpcRegistrar = testActorRef.underlyingActor();
77     }
78
79     @After
80     public void tearDown() {
81         TestKit.shutdownActorSystem(system, true);
82     }
83
84     @Test
85     public void testPostStop() throws Exception {
86         testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(firstEndpoint))),
87                 ActorRef.noSender());
88         testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(secondEndpoint))),
89                 ActorRef.noSender());
90
91         rpcRegistrar.postStop();
92
93         Mockito.verify(oldReg).close();
94         Mockito.verify(newReg).close();
95     }
96
97     @Test
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());
102
103         Mockito.verify(service).registerRpcImplementation(
104                 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(firstEndpoint.getRpcs()));
105         Mockito.verifyNoMoreInteractions(service, oldReg, newReg);
106     }
107
108     @Test
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);
114     }
115
116     @Test
117     public void testHandleReceiveUpdateEndpoint() {
118         final InOrder inOrder = Mockito.inOrder(service, oldReg, newReg);
119
120         testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(firstEndpoint))),
121                 ActorRef.noSender());
122
123         // first registration
124         inOrder.verify(service).registerRpcImplementation(
125                 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(firstEndpoint.getRpcs()));
126
127         testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(secondEndpoint))),
128                 ActorRef.noSender());
129
130         // second registration
131         inOrder.verify(service).registerRpcImplementation(
132                 Mockito.any(RemoteRpcImplementation.class), Mockito.eq(secondEndpoint.getRpcs()));
133
134         // verify first registration is closed
135         inOrder.verify(oldReg).close();
136
137         Mockito.verifyNoMoreInteractions(service, oldReg, newReg);
138     }
139 }