Migrate restconf to MD-SAL APIs
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / RestconfInvokeOperationsUtilTest.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.restconf.nb.rfc8040.rests.utils;
9
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;
16
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;
34
35 public class RestconfInvokeOperationsUtilTest {
36
37     private static final TestData DATA = new TestData();
38
39     private RpcServiceHandler serviceHandler;
40     @Mock
41     private DOMRpcService rpcService;
42     @Mock
43     private DOMMountPoint moutPoint;
44
45     @Before
46     public void setUp() {
47         MockitoAnnotations.initMocks(this);
48         serviceHandler = new RpcServiceHandler(rpcService);
49     }
50
51     @Test
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());
58     }
59
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);
75     }
76
77     @Test
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());
86     }
87
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);
95     }
96
97     @Test
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));
105     }
106 }