1bc05d629ea756be516442c54de54041728e649d
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfInvokeOperationsServiceImplTest.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.services.impl;
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.assertNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertThrows;
16 import static org.junit.Assert.assertTrue;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.verify;
20 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
21 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
22
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.concurrent.ExecutionException;
27 import javax.ws.rs.WebApplicationException;
28 import javax.ws.rs.container.AsyncResponse;
29 import javax.ws.rs.core.Response.Status;
30 import javax.ws.rs.core.UriInfo;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
39 import org.opendaylight.mdsal.dom.api.DOMRpcException;
40 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
41 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
42 import org.opendaylight.mdsal.dom.api.DOMRpcService;
43 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
44 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
45 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
46 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
47 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
48 import org.opendaylight.yangtools.yang.common.ErrorTag;
49 import org.opendaylight.yangtools.yang.common.ErrorType;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.common.RpcError;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
54 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
55 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
56 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
57 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
58 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
59
60 @RunWith(MockitoJUnitRunner.StrictStubs.class)
61 public class RestconfInvokeOperationsServiceImplTest {
62     private static final QName RPC = QName.create("ns", "2015-02-28", "test-rpc");
63     private static final ContainerNode INPUT = Builders.containerBuilder()
64         .withNodeIdentifier(new NodeIdentifier(QName.create(RPC, "input")))
65         .withChild(ImmutableNodes.leafNode(QName.create(RPC, "content"), "test"))
66         .build();
67     private static final ContainerNode OUTPUT = Builders.containerBuilder()
68         .withNodeIdentifier(new NodeIdentifier(QName.create(RPC, "output")))
69         .withChild(ImmutableNodes.leafNode(QName.create(RPC, "content"), "operation result"))
70         .build();
71
72     private static EffectiveModelContext CONTEXT;
73
74     @Mock
75     private DOMRpcService rpcService;
76     @Mock
77     private DOMMountPoint mountPoint;
78     private RestconfInvokeOperationsServiceImpl invokeOperationsService;
79
80     @BeforeClass
81     public static void beforeClass() throws Exception {
82         CONTEXT = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/invoke-rpc"));
83     }
84
85     @Before
86     public void setup() {
87         invokeOperationsService = new RestconfInvokeOperationsServiceImpl(rpcService);
88     }
89
90     @Test
91     public void testInvokeRpcWithNonEmptyOutput() {
92         final ContainerNode result = mock(ContainerNode.class);
93         doReturn(false).when(result).isEmpty();
94
95         final AsyncResponse ar = mock(AsyncResponse.class);
96         final ArgumentCaptor<NormalizedNodePayload> response = ArgumentCaptor.forClass(NormalizedNodePayload.class);
97         invokeOperationsService.invokeRpc("invoke-rpc-module:rpcTest", prepNNC(result), mock(UriInfo.class), ar);
98         verify(ar).resume(response.capture());
99
100         assertSame(result, response.getValue().getData());
101     }
102
103     @Test
104     public void testInvokeRpcWithEmptyOutput() {
105         final ContainerNode result = mock(ContainerNode.class);
106         doReturn(true).when(result).isEmpty();
107
108         final AsyncResponse ar = mock(AsyncResponse.class);
109         final ArgumentCaptor<Throwable> response = ArgumentCaptor.forClass(Throwable.class);
110         invokeOperationsService.invokeRpc("invoke-rpc-module:rpcTest", prepNNC(result), mock(UriInfo.class), ar);
111         verify(ar).resume(response.capture());
112
113         final Throwable failure = response.getValue();
114         assertThat(failure, instanceOf(WebApplicationException.class));
115         assertEquals(Status.NO_CONTENT.getStatusCode(), ((WebApplicationException) failure).getResponse().getStatus());
116     }
117
118     @Test
119     public void invokeRpcTest() throws InterruptedException, ExecutionException {
120         final DOMRpcResult mockResult = new DefaultDOMRpcResult(OUTPUT, List.of());
121         doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(RPC, INPUT);
122         final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, rpcService).get();
123         assertTrue(rpcResult.getErrors().isEmpty());
124         assertEquals(OUTPUT, rpcResult.getResult());
125     }
126
127     @Test
128     public void invokeRpcErrorsAndCheckTestTest() throws InterruptedException, ExecutionException {
129         final QName errorRpc = QName.create(RPC, "error-rpc");
130         final DOMRpcException exception = new DOMRpcImplementationNotAvailableException(
131                 "No implementation of RPC " + errorRpc + " available.");
132         doReturn(immediateFailedFluentFuture(exception)).when(rpcService).invokeRpc(errorRpc, INPUT);
133         final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, errorRpc, rpcService).get();
134         assertNull(rpcResult.getResult());
135         final Collection<? extends RpcError> errorList = rpcResult.getErrors();
136         assertEquals(1, errorList.size());
137         final RpcError actual = errorList.iterator().next();
138         assertEquals("No implementation of RPC " + errorRpc + " available.", actual.getMessage());
139         assertEquals(ErrorTag.OPERATION_FAILED, actual.getTag());
140         assertEquals(ErrorType.RPC, actual.getErrorType());
141     }
142
143     @Test
144     public void invokeRpcViaMountPointTest() throws InterruptedException, ExecutionException {
145         doReturn(Optional.ofNullable(rpcService)).when(mountPoint).getService(DOMRpcService.class);
146         final DOMRpcResult mockResult = new DefaultDOMRpcResult(OUTPUT, List.of());
147         doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(RPC, INPUT);
148         final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, mountPoint).get();
149         assertTrue(rpcResult.getErrors().isEmpty());
150         assertEquals(OUTPUT, rpcResult.getResult());
151     }
152
153     @Test
154     public void invokeRpcMissingMountPointServiceTest() {
155         doReturn(Optional.empty()).when(mountPoint).getService(DOMRpcService.class);
156         assertThrows(RestconfDocumentedException.class,
157             () -> RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, mountPoint));
158     }
159
160     @Test
161     public void checkResponseTest() throws InterruptedException, ExecutionException {
162         doReturn(immediateFluentFuture(new DefaultDOMRpcResult(OUTPUT, List.of())))
163             .when(rpcService).invokeRpc(RPC, INPUT);
164         final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, rpcService).get();
165         assertTrue(rpcResult.getErrors().isEmpty());
166         assertEquals(OUTPUT, rpcResult.getResult());
167     }
168
169     private NormalizedNodePayload prepNNC(final ContainerNode result) {
170         final QName qname = QName.create("invoke:rpc:module", "2013-12-03", "rpc-test");
171         final RpcDefinition schemaNode = CONTEXT.getOperations().stream().filter(rpc -> rpc.getQName().equals(qname))
172             .findFirst()
173             .orElseThrow();
174
175         final ContainerNode data = mock(ContainerNode.class);
176         final DOMRpcResult domRpcResult = mock(DOMRpcResult.class);
177         doReturn(immediateFluentFuture(domRpcResult)).when(rpcService).invokeRpc(qname, data);
178         doReturn(result).when(domRpcResult).getResult();
179         return NormalizedNodePayload.of(
180             InstanceIdentifierContext.ofRpcInput(CONTEXT, schemaNode, null), data);
181     }
182 }