import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
-import static org.opendaylight.mdsal.dom.broker.TestUtils.TEST_CHILD;
+import static org.mockito.Mockito.verify;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
+import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
import org.opendaylight.mdsal.dom.broker.DOMRpcRouter.OperationInvocation;
-import org.opendaylight.mdsal.dom.broker.util.TestModel;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class RoutedDOMRpcRoutingTableEntryTest {
- @Mock
- public RpcDefinition rpcDefinition;
+ public static final YangInstanceIdentifier CTX_IN_INPUT =
+ YangInstanceIdentifier.create(new NodeIdentifier(Rpcs.CTX));
+ public static final YangInstanceIdentifier ONE_PATH = YangInstanceIdentifier.create(
+ new NodeIdentifier(Rpcs.BAZ), NodeIdentifierWithPredicates.of(Rpcs.BAZ, Rpcs.NAME, "one"));
+ public static final YangInstanceIdentifier TWO_PATH = YangInstanceIdentifier.create(
+ new NodeIdentifier(Rpcs.BAZ), NodeIdentifierWithPredicates.of(Rpcs.BAZ, Rpcs.NAME, "two"));
+
+ public static final ContainerNode ONE_INPUT = Builders.containerBuilder()
+ .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
+ .withChild(ImmutableNodes.leafNode(Rpcs.CTX, ONE_PATH))
+ .build();
+ public static final ContainerNode TWO_INPUT = Builders.containerBuilder()
+ .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
+ .withChild(ImmutableNodes.leafNode(Rpcs.CTX, TWO_PATH))
+ .build();
+ public static final ContainerNode GLOBAL_INPUT = Builders.containerBuilder()
+ .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
+ // This not covered by schema
+ .withChild(ImmutableNodes.leafNode(Rpcs.NAME, "name"))
+ .build();
- private RoutedDOMRpcRoutingTableEntry entry;
+ @Mock
+ public RpcDefinition definition;
+ @Mock
+ public DOMRpcImplementation impl;
+ @Mock
+ public DOMRpcResult result;
+ public RoutedDOMRpcRoutingTableEntry entry;
@Before
public void before() {
- doReturn(TestModel.TEST2_QNAME).when(rpcDefinition).getQName();
+ doReturn(Rpcs.BAR).when(definition).getQName();
// Note: ImmutableMap.of() allows get(null), Map.of() does not
- entry = new RoutedDOMRpcRoutingTableEntry(rpcDefinition, TestModel.TEST_PATH, ImmutableMap.of());
+ entry = new RoutedDOMRpcRoutingTableEntry(definition, CTX_IN_INPUT, ImmutableMap.of());
}
@Test
public void testNewInstance() {
final RoutedDOMRpcRoutingTableEntry instance = entry.newInstance(Map.of());
- assertEquals(TestModel.TEST2_QNAME, entry.getType());
+ assertEquals(Rpcs.BAR, entry.getType());
assertEquals(Map.of(), instance.getImplementations());
}
@Test
public void testUnregistered() {
- final ListenableFuture<?> future = OperationInvocation.invoke(entry, TEST_CHILD);
- final Throwable cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
+ assertRpcUnavailable(ONE_INPUT);
+ assertRpcUnavailable(TWO_INPUT);
+ assertRpcUnavailable(GLOBAL_INPUT);
+ }
+
+ @Test
+ public void testRegisteredGlobal() {
+ setPaths((YangInstanceIdentifier) null);
+ assertRpcAvailable(GLOBAL_INPUT);
+ }
+
+ @Test
+ public void testRegisteredGlobalOne() {
+ setPaths((YangInstanceIdentifier) null);
+ assertRpcAvailable(ONE_INPUT);
+ }
+
+ @Test
+ public void testRegisteredGlobalTwo() {
+ setPaths((YangInstanceIdentifier) null);
+ assertRpcAvailable(TWO_INPUT);
+ }
+
+ @Test
+ public void testRegisteredOne() {
+ setPaths(ONE_PATH);
+ assertRpcUnavailable(TWO_INPUT);
+ assertRpcAvailable(ONE_INPUT);
+ }
+
+ @Test
+ public void testRegisteredTwo() {
+ setPaths(TWO_PATH);
+ assertRpcUnavailable(ONE_INPUT);
+ assertRpcAvailable(TWO_INPUT);
+ }
+
+ @Test
+ public void testRemote() {
+ setPaths(YangInstanceIdentifier.empty());
+ assertRpcAvailable(ONE_INPUT);
+ }
+
+ @Test
+ public void testWrongContext() {
+ assertRpcUnavailable(Builders.containerBuilder()
+ .withNodeIdentifier(new NodeIdentifier(Rpcs.INPUT))
+ .withChild(ImmutableNodes.leafNode(Rpcs.CTX, "bad type"))
+ .build());
+ }
+
+ private void setPaths(final YangInstanceIdentifier... paths) {
+ final var map = new HashMap<YangInstanceIdentifier, List<DOMRpcImplementation>>();
+ for (var path : paths) {
+ map.put(path, List.of(impl));
+ }
+ entry = entry.newInstance(map);
+ }
+
+ private void assertRpcAvailable(final NormalizedNode input) {
+ doReturn(Futures.immediateFuture(result)).when(impl).invokeRpc(any(), any());
+
+ final var future = OperationInvocation.invoke(entry, input);
+ try {
+ assertSame(result, Futures.getDone(future));
+ } catch (ExecutionException e) {
+ throw new AssertionError(e);
+ }
+
+ verify(impl).invokeRpc(any(), any());
+ }
+
+ private void assertRpcUnavailable(final NormalizedNode input) {
+ final var future = OperationInvocation.invoke(entry, input);
+ final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
assertThat(cause, instanceOf(DOMRpcImplementationNotAvailableException.class));
- assertEquals("No implementation of RPC "
- + "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test2 "
- + "available",
- cause.getMessage());
+ assertEquals("No implementation of RPC (rpcs)bar available", cause.getMessage());
}
}