HONEYCOMB-424: bump ODL dependencies to Oxygen
[honeycomb.git] / infra / rpc / impl / src / test / java / io / fd / honeycomb / rpc / RpcRegistryBuilderTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.rpc;
18
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21
22 import java.net.URI;
23 import java.util.concurrent.ExecutionException;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.ArgumentMatchers;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33
34 public class RpcRegistryBuilderTest {
35
36     private RpcRegistry registry;
37     @Mock
38     private RpcService service1;
39     @Mock
40     private RpcService service2;
41
42     private static final URI namespace = URI.create("urn:foo");
43     private static final SchemaPath ID1 = SchemaPath.ROOT.createChild(QName.create(namespace, "a"));
44     private static final SchemaPath ID2 = SchemaPath.ROOT.createChild(QName.create(namespace, "b"));
45
46     @Before
47     public void setUp() {
48         service1 = Mockito.mock(RpcService.class);
49         Mockito.when(service1.getManagedNode()).thenReturn(ID1);
50
51         service2 = Mockito.mock(RpcService.class);
52         Mockito.when(service2.getManagedNode()).thenReturn(ID2);
53
54         final RpcRegistryBuilder builder = new RpcRegistryBuilder();
55         builder.addService(service1);
56         builder.addService(service2);
57         registry = builder.build();
58     }
59
60     @Test
61     public void testInvokeService() {
62         final DataObject request = Mockito.mock(DataObject.class);
63
64         registry.invoke(ID2, request);
65
66         Mockito.verify(service2).invoke(request);
67         Mockito.verify(service1, Mockito.never()).invoke(ArgumentMatchers.any());
68     }
69
70     @Test
71     public void testServiceNotFound() throws ExecutionException, InterruptedException {
72         final SchemaPath id = SchemaPath.ROOT.createChild(QName.create(namespace, "c"));
73         final DataObject request = Mockito.mock(DataObject.class);
74
75         try {
76             registry.invoke(id, request).toCompletableFuture().get();
77         } catch (Exception e) {
78             assertTrue(e.getCause() instanceof DOMRpcImplementationNotAvailableException);
79             return;
80         }
81         fail("Exception expected");
82     }
83 }