Migrate users of Builders/ImmutableNodes
[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.spi.node.ImmutableNodes;
38
39 @RunWith(MockitoJUnitRunner.StrictStubs.class)
40 public class RoutedDOMRpcRoutingTableEntryTest {
41     public static final YangInstanceIdentifier CTX_IN_INPUT =
42         YangInstanceIdentifier.of(new NodeIdentifier(Rpcs.CTX));
43     public static final YangInstanceIdentifier ONE_PATH = YangInstanceIdentifier.of(
44         new NodeIdentifier(Rpcs.BAZ), NodeIdentifierWithPredicates.of(Rpcs.BAZ, Rpcs.NAME, "one"));
45     public static final YangInstanceIdentifier TWO_PATH = YangInstanceIdentifier.of(
46         new NodeIdentifier(Rpcs.BAZ), NodeIdentifierWithPredicates.of(Rpcs.BAZ, Rpcs.NAME, "two"));
47
48     public static final ContainerNode ONE_INPUT = ImmutableNodes.newContainerBuilder()
49         .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
50         .withChild(ImmutableNodes.leafNode(Rpcs.CTX, ONE_PATH))
51         .build();
52     public static final ContainerNode TWO_INPUT = ImmutableNodes.newContainerBuilder()
53         .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
54         .withChild(ImmutableNodes.leafNode(Rpcs.CTX, TWO_PATH))
55         .build();
56     public static final ContainerNode GLOBAL_INPUT = ImmutableNodes.newContainerBuilder()
57         .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
58         // This not covered by schema
59         .withChild(ImmutableNodes.leafNode(Rpcs.NAME, "name"))
60         .build();
61
62     @Mock
63     public DOMRpcImplementation impl;
64     @Mock
65     public DOMRpcResult result;
66     public RoutedDOMRpcRoutingTableEntry entry;
67
68     @Before
69     public void before() {
70         // Note: ImmutableMap.of() allows get(null), Map.of() does not
71         entry = new RoutedDOMRpcRoutingTableEntry(Rpcs.BAR, CTX_IN_INPUT, ImmutableMap.of());
72     }
73
74     @Test
75     public void testNewInstance() {
76         final RoutedDOMRpcRoutingTableEntry instance = entry.newInstance(Map.of());
77         assertEquals(Rpcs.BAR, entry.getType());
78         assertEquals(Map.of(), instance.getImplementations());
79     }
80
81     @Test
82     public void testUnregistered()  {
83         assertRpcUnavailable(ONE_INPUT);
84         assertRpcUnavailable(TWO_INPUT);
85         assertRpcUnavailable(GLOBAL_INPUT);
86     }
87
88     @Test
89     public void testRegisteredGlobal() {
90         setPaths((YangInstanceIdentifier) null);
91         assertRpcAvailable(GLOBAL_INPUT);
92     }
93
94     @Test
95     public void testRegisteredGlobalOne() {
96         setPaths((YangInstanceIdentifier) null);
97         assertRpcAvailable(ONE_INPUT);
98     }
99
100     @Test
101     public void testRegisteredGlobalTwo() {
102         setPaths((YangInstanceIdentifier) null);
103         assertRpcAvailable(TWO_INPUT);
104     }
105
106     @Test
107     public void testRegisteredOne() {
108         setPaths(ONE_PATH);
109         assertRpcUnavailable(TWO_INPUT);
110         assertRpcAvailable(ONE_INPUT);
111     }
112
113     @Test
114     public void testRegisteredTwo() {
115         setPaths(TWO_PATH);
116         assertRpcUnavailable(ONE_INPUT);
117         assertRpcAvailable(TWO_INPUT);
118     }
119
120     @Test
121     public void testRemote() {
122         setPaths(YangInstanceIdentifier.of());
123         assertRpcAvailable(ONE_INPUT);
124     }
125
126     @Test
127     public void testWrongContext() {
128         assertRpcUnavailable(ImmutableNodes.newContainerBuilder()
129             .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
130             .withChild(ImmutableNodes.leafNode(Rpcs.CTX, "bad type"))
131             .build());
132     }
133
134     private void setPaths(final YangInstanceIdentifier... paths) {
135         final var map = new HashMap<YangInstanceIdentifier, List<DOMRpcImplementation>>();
136         for (var path : paths) {
137             map.put(path, List.of(impl));
138         }
139         entry = entry.newInstance(map);
140     }
141
142     private void assertRpcAvailable(final ContainerNode input) {
143         doReturn(Futures.immediateFuture(result)).when(impl).invokeRpc(any(), any());
144
145         final var future = OperationInvocation.invoke(entry, input);
146         try {
147             assertSame(result, Futures.getDone(future));
148         } catch (ExecutionException e) {
149             throw new AssertionError(e);
150         }
151
152         verify(impl).invokeRpc(any(), any());
153     }
154
155     private void assertRpcUnavailable(final ContainerNode input) {
156         final var future = OperationInvocation.invoke(entry, input);
157         final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
158         assertThat(cause, instanceOf(DOMRpcImplementationNotAvailableException.class));
159         assertEquals("No implementation of RPC (rpcs)bar available", cause.getMessage());
160     }
161 }