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.restconf.nb.rfc8040.rests.utils;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Mockito.doReturn;
14 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Optional;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMRpcException;
27 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
28 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
29 import org.opendaylight.mdsal.dom.api.DOMRpcService;
30 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
31 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
32 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
33 import org.opendaylight.yangtools.yang.common.RpcError;
35 public class RestconfInvokeOperationsUtilTest {
37 private static final TestData DATA = new TestData();
39 private RpcServiceHandler serviceHandler;
41 private DOMRpcService rpcService;
43 private DOMMountPoint moutPoint;
47 MockitoAnnotations.initMocks(this);
48 serviceHandler = new RpcServiceHandler(rpcService);
52 public void invokeRpcTest() {
53 final DOMRpcResult mockResult = new DefaultDOMRpcResult(DATA.output, Collections.emptyList());
54 doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(DATA.rpc, DATA.input);
55 final DOMRpcResult rpcResult = RestconfInvokeOperationsUtil.invokeRpc(DATA.input, DATA.rpc, serviceHandler);
56 Assert.assertTrue(rpcResult.getErrors().isEmpty());
57 assertEquals(DATA.output, rpcResult.getResult());
60 @Test(expected = RestconfDocumentedException.class)
61 public void invokeRpcErrorsAndCheckTestTest() {
62 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException(
63 "No implementation of RPC " + DATA.errorRpc.toString() + " available.");
64 doReturn(immediateFailedFluentFuture(exception)).when(rpcService).invokeRpc(DATA.errorRpc, DATA.input);
65 final DOMRpcResult rpcResult =
66 RestconfInvokeOperationsUtil.invokeRpc(DATA.input, DATA.errorRpc, serviceHandler);
67 assertNull(rpcResult.getResult());
68 final Collection<? extends RpcError> errorList = rpcResult.getErrors();
69 assertEquals(1, errorList.size());
70 final RpcError actual = errorList.iterator().next();
71 assertEquals("No implementation of RPC " + DATA.errorRpc.toString() + " available.", actual.getMessage());
72 assertEquals("operation-failed", actual.getTag());
73 assertEquals(RpcError.ErrorType.RPC, actual.getErrorType());
74 RestconfInvokeOperationsUtil.checkResponse(rpcResult);
78 public void invokeRpcViaMountPointTest() {
79 doReturn(Optional.ofNullable(rpcService)).when(moutPoint).getService(DOMRpcService.class);
80 final DOMRpcResult mockResult = new DefaultDOMRpcResult(DATA.output, Collections.emptyList());
81 doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(DATA.rpc, DATA.input);
82 final DOMRpcResult rpcResult =
83 RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(moutPoint, DATA.input, DATA.rpc);
84 Assert.assertTrue(rpcResult.getErrors().isEmpty());
85 assertEquals(DATA.output, rpcResult.getResult());
88 @Test(expected = RestconfDocumentedException.class)
89 public void invokeRpcMissingMountPointServiceTest() {
90 doReturn(Optional.empty()).when(moutPoint).getService(DOMRpcService.class);
91 final DOMRpcResult mockResult = new DefaultDOMRpcResult(DATA.output, Collections.emptyList());
92 doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(DATA.rpc, DATA.input);
93 final DOMRpcResult rpcResult =
94 RestconfInvokeOperationsUtil.invokeRpcViaMountPoint(moutPoint, DATA.input, DATA.rpc);
98 public void checkResponseTest() {
99 final DOMRpcResult mockResult = new DefaultDOMRpcResult(DATA.output, Collections.emptyList());
100 doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(DATA.rpc, DATA.input);
101 final DOMRpcResult rpcResult = RestconfInvokeOperationsUtil.invokeRpc(DATA.input, DATA.rpc, serviceHandler);
102 Assert.assertTrue(rpcResult.getErrors().isEmpty());
103 assertEquals(DATA.output, rpcResult.getResult());
104 assertNotNull(RestconfInvokeOperationsUtil.checkResponse(rpcResult));