Merge "Bug 9092: revert to org.json temporarily"
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / restful / 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
9 package org.opendaylight.restconf.restful.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.Mockito.doReturn;
15
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.Futures;
18 import java.util.Collection;
19 import java.util.Collections;
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.controller.md.sal.dom.api.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
28 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
30 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
32 import org.opendaylight.restconf.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(Futures.immediateCheckedFuture(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(Futures.immediateFailedCheckedFuture(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<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.fromNullable(rpcService)).when(moutPoint).getService(DOMRpcService.class);
80         final DOMRpcResult mockResult = new DefaultDOMRpcResult(DATA.output, Collections.emptyList());
81         doReturn(Futures.immediateCheckedFuture(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.absent()).when(moutPoint).getService(DOMRpcService.class);
91         final DOMRpcResult mockResult = new DefaultDOMRpcResult(DATA.output, Collections.emptyList());
92         doReturn(Futures.immediateCheckedFuture(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(Futures.immediateCheckedFuture(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
107 }