2 * Copyright (c) 2016 Cisco Systems, Inc. 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.mdsal.dom.broker;
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;
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;
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.mdsal.dom.broker.DOMRpcRouter.OperationInvocation;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
43 @RunWith(MockitoJUnitRunner.StrictStubs.class)
44 public class RoutedDOMRpcRoutingTableEntryTest {
45 public static final YangInstanceIdentifier CTX_IN_INPUT =
46 YangInstanceIdentifier.create(new NodeIdentifier(Rpcs.CTX));
47 public static final YangInstanceIdentifier ONE_PATH = YangInstanceIdentifier.create(
48 new NodeIdentifier(Rpcs.BAZ), NodeIdentifierWithPredicates.of(Rpcs.BAZ, Rpcs.NAME, "one"));
49 public static final YangInstanceIdentifier TWO_PATH = YangInstanceIdentifier.create(
50 new NodeIdentifier(Rpcs.BAZ), NodeIdentifierWithPredicates.of(Rpcs.BAZ, Rpcs.NAME, "two"));
52 public static final ContainerNode ONE_INPUT = Builders.containerBuilder()
53 .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
54 .withChild(ImmutableNodes.leafNode(Rpcs.CTX, ONE_PATH))
56 public static final ContainerNode TWO_INPUT = Builders.containerBuilder()
57 .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
58 .withChild(ImmutableNodes.leafNode(Rpcs.CTX, TWO_PATH))
60 public static final ContainerNode GLOBAL_INPUT = Builders.containerBuilder()
61 .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
62 // This not covered by schema
63 .withChild(ImmutableNodes.leafNode(Rpcs.NAME, "name"))
67 public RpcDefinition definition;
69 public DOMRpcImplementation impl;
71 public DOMRpcResult result;
72 public RoutedDOMRpcRoutingTableEntry entry;
75 public void before() {
76 doReturn(Rpcs.BAR).when(definition).getQName();
77 // Note: ImmutableMap.of() allows get(null), Map.of() does not
78 entry = new RoutedDOMRpcRoutingTableEntry(definition, CTX_IN_INPUT, ImmutableMap.of());
82 public void testNewInstance() {
83 final RoutedDOMRpcRoutingTableEntry instance = entry.newInstance(Map.of());
84 assertEquals(Rpcs.BAR, entry.getType());
85 assertEquals(Map.of(), instance.getImplementations());
89 public void testUnregistered() {
90 assertRpcUnavailable(ONE_INPUT);
91 assertRpcUnavailable(TWO_INPUT);
92 assertRpcUnavailable(GLOBAL_INPUT);
96 public void testRegisteredGlobal() {
97 setPaths((YangInstanceIdentifier) null);
98 assertRpcAvailable(GLOBAL_INPUT);
102 public void testRegisteredGlobalOne() {
103 setPaths((YangInstanceIdentifier) null);
104 assertRpcAvailable(ONE_INPUT);
108 public void testRegisteredGlobalTwo() {
109 setPaths((YangInstanceIdentifier) null);
110 assertRpcAvailable(TWO_INPUT);
114 public void testRegisteredOne() {
116 assertRpcUnavailable(TWO_INPUT);
117 assertRpcAvailable(ONE_INPUT);
121 public void testRegisteredTwo() {
123 assertRpcUnavailable(ONE_INPUT);
124 assertRpcAvailable(TWO_INPUT);
128 public void testRemote() {
129 setPaths(YangInstanceIdentifier.empty());
130 assertRpcAvailable(ONE_INPUT);
134 public void testWrongContext() {
135 assertRpcUnavailable(Builders.containerBuilder()
136 .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
137 .withChild(ImmutableNodes.leafNode(Rpcs.CTX, "bad type"))
141 private void setPaths(final YangInstanceIdentifier... paths) {
142 final var map = new HashMap<YangInstanceIdentifier, List<DOMRpcImplementation>>();
143 for (var path : paths) {
144 map.put(path, List.of(impl));
146 entry = entry.newInstance(map);
149 private void assertRpcAvailable(final NormalizedNode input) {
150 doReturn(Futures.immediateFuture(result)).when(impl).invokeRpc(any(), any());
152 final var future = OperationInvocation.invoke(entry, input);
154 assertSame(result, Futures.getDone(future));
155 } catch (ExecutionException e) {
156 throw new AssertionError(e);
159 verify(impl).invokeRpc(any(), any());
162 private void assertRpcUnavailable(final NormalizedNode input) {
163 final var future = OperationInvocation.invoke(entry, input);
164 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
165 assertThat(cause, instanceOf(DOMRpcImplementationNotAvailableException.class));
166 assertEquals("No implementation of RPC (rpcs)bar available", cause.getMessage());