b749f82116d667c19bca18d3f5ed88ff53c16b86
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / RoutedDOMRpcRoutingTableEntryTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.mdsal.dom.broker;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertThrows;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.verify;
18
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.util.concurrent.Futures;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.concurrent.ExecutionException;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
31 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
32 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39
40 @RunWith(MockitoJUnitRunner.StrictStubs.class)
41 public class RoutedDOMRpcRoutingTableEntryTest {
42     public static final YangInstanceIdentifier CTX_IN_INPUT =
43         YangInstanceIdentifier.of(new NodeIdentifier(Rpcs.CTX));
44     public static final YangInstanceIdentifier ONE_PATH = YangInstanceIdentifier.of(
45         new NodeIdentifier(Rpcs.BAZ), NodeIdentifierWithPredicates.of(Rpcs.BAZ, Rpcs.NAME, "one"));
46     public static final YangInstanceIdentifier TWO_PATH = YangInstanceIdentifier.of(
47         new NodeIdentifier(Rpcs.BAZ), NodeIdentifierWithPredicates.of(Rpcs.BAZ, Rpcs.NAME, "two"));
48
49     public static final ContainerNode ONE_INPUT = Builders.containerBuilder()
50         .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
51         .withChild(ImmutableNodes.leafNode(Rpcs.CTX, ONE_PATH))
52         .build();
53     public static final ContainerNode TWO_INPUT = Builders.containerBuilder()
54         .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
55         .withChild(ImmutableNodes.leafNode(Rpcs.CTX, TWO_PATH))
56         .build();
57     public static final ContainerNode GLOBAL_INPUT = Builders.containerBuilder()
58         .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
59         // This not covered by schema
60         .withChild(ImmutableNodes.leafNode(Rpcs.NAME, "name"))
61         .build();
62
63     @Mock
64     public DOMRpcImplementation impl;
65     @Mock
66     public DOMRpcResult result;
67     public RoutedDOMRpcRoutingTableEntry entry;
68
69     @Before
70     public void before() {
71         // Note: ImmutableMap.of() allows get(null), Map.of() does not
72         entry = new RoutedDOMRpcRoutingTableEntry(Rpcs.BAR, CTX_IN_INPUT, ImmutableMap.of());
73     }
74
75     @Test
76     public void testNewInstance() {
77         final RoutedDOMRpcRoutingTableEntry instance = entry.newInstance(Map.of());
78         assertEquals(Rpcs.BAR, entry.getType());
79         assertEquals(Map.of(), instance.getImplementations());
80     }
81
82     @Test
83     public void testUnregistered()  {
84         assertRpcUnavailable(ONE_INPUT);
85         assertRpcUnavailable(TWO_INPUT);
86         assertRpcUnavailable(GLOBAL_INPUT);
87     }
88
89     @Test
90     public void testRegisteredGlobal() {
91         setPaths((YangInstanceIdentifier) null);
92         assertRpcAvailable(GLOBAL_INPUT);
93     }
94
95     @Test
96     public void testRegisteredGlobalOne() {
97         setPaths((YangInstanceIdentifier) null);
98         assertRpcAvailable(ONE_INPUT);
99     }
100
101     @Test
102     public void testRegisteredGlobalTwo() {
103         setPaths((YangInstanceIdentifier) null);
104         assertRpcAvailable(TWO_INPUT);
105     }
106
107     @Test
108     public void testRegisteredOne() {
109         setPaths(ONE_PATH);
110         assertRpcUnavailable(TWO_INPUT);
111         assertRpcAvailable(ONE_INPUT);
112     }
113
114     @Test
115     public void testRegisteredTwo() {
116         setPaths(TWO_PATH);
117         assertRpcUnavailable(ONE_INPUT);
118         assertRpcAvailable(TWO_INPUT);
119     }
120
121     @Test
122     public void testRemote() {
123         setPaths(YangInstanceIdentifier.of());
124         assertRpcAvailable(ONE_INPUT);
125     }
126
127     @Test
128     public void testWrongContext() {
129         assertRpcUnavailable(Builders.containerBuilder()
130             .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
131             .withChild(ImmutableNodes.leafNode(Rpcs.CTX, "bad type"))
132             .build());
133     }
134
135     private void setPaths(final YangInstanceIdentifier... paths) {
136         final var map = new HashMap<YangInstanceIdentifier, List<DOMRpcImplementation>>();
137         for (var path : paths) {
138             map.put(path, List.of(impl));
139         }
140         entry = entry.newInstance(map);
141     }
142
143     private void assertRpcAvailable(final ContainerNode input) {
144         doReturn(Futures.immediateFuture(result)).when(impl).invokeRpc(any(), any());
145
146         final var future = OperationInvocation.invoke(entry, input);
147         try {
148             assertSame(result, Futures.getDone(future));
149         } catch (ExecutionException e) {
150             throw new AssertionError(e);
151         }
152
153         verify(impl).invokeRpc(any(), any());
154     }
155
156     private void assertRpcUnavailable(final ContainerNode input) {
157         final var future = OperationInvocation.invoke(entry, input);
158         final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
159         assertThat(cause, instanceOf(DOMRpcImplementationNotAvailableException.class));
160         assertEquals("No implementation of RPC (rpcs)bar available", cause.getMessage());
161     }
162 }