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.services.impl;
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;
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.DOMMountPointService;
40 import org.opendaylight.mdsal.dom.api.DOMRpcException;
41 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
42 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
43 import org.opendaylight.mdsal.dom.api.DOMRpcService;
44 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
45 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
46 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
47 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
48 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
49 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
50 import org.opendaylight.yangtools.yang.common.ErrorTag;
51 import org.opendaylight.yangtools.yang.common.ErrorType;
52 import org.opendaylight.yangtools.yang.common.QName;
53 import org.opendaylight.yangtools.yang.common.RpcError;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
56 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
57 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
58 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
59 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
60 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
62 @RunWith(MockitoJUnitRunner.StrictStubs.class)
63 public class RestconfInvokeOperationsServiceImplTest {
64 private static final QName RPC = QName.create("ns", "2015-02-28", "test-rpc");
65 private static final ContainerNode INPUT = Builders.containerBuilder()
66 .withNodeIdentifier(new NodeIdentifier(QName.create(RPC, "input")))
67 .withChild(ImmutableNodes.leafNode(QName.create(RPC, "content"), "test"))
69 private static final ContainerNode OUTPUT = Builders.containerBuilder()
70 .withNodeIdentifier(new NodeIdentifier(QName.create(RPC, "output")))
71 .withChild(ImmutableNodes.leafNode(QName.create(RPC, "content"), "operation result"))
74 private static EffectiveModelContext CONTEXT;
77 private DOMRpcService rpcService;
79 private DOMMountPoint mountPoint;
81 private DOMMountPointService mountPointService;
82 private RestconfInvokeOperationsServiceImpl invokeOperationsService;
85 public static void beforeClass() throws Exception {
86 CONTEXT = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/invoke-rpc"));
91 invokeOperationsService = new RestconfInvokeOperationsServiceImpl(rpcService, mountPointService,
92 mock(Configuration.class));
96 public void testInvokeRpcWithNonEmptyOutput() {
97 final ContainerNode result = mock(ContainerNode.class);
98 doReturn(false).when(result).isEmpty();
100 final AsyncResponse ar = mock(AsyncResponse.class);
101 final ArgumentCaptor<NormalizedNodePayload> response = ArgumentCaptor.forClass(NormalizedNodePayload.class);
102 invokeOperationsService.invokeRpc("invoke-rpc-module:rpcTest", prepNNC(result), mock(UriInfo.class), ar);
103 verify(ar).resume(response.capture());
105 assertSame(result, response.getValue().getData());
109 public void testInvokeRpcWithEmptyOutput() {
110 final ContainerNode result = mock(ContainerNode.class);
111 doReturn(true).when(result).isEmpty();
113 final AsyncResponse ar = mock(AsyncResponse.class);
114 final ArgumentCaptor<Throwable> response = ArgumentCaptor.forClass(Throwable.class);
115 invokeOperationsService.invokeRpc("invoke-rpc-module:rpcTest", prepNNC(result), mock(UriInfo.class), ar);
116 verify(ar).resume(response.capture());
118 final Throwable failure = response.getValue();
119 assertThat(failure, instanceOf(WebApplicationException.class));
120 assertEquals(Status.NO_CONTENT.getStatusCode(), ((WebApplicationException) failure).getResponse().getStatus());
124 public void invokeRpcTest() throws InterruptedException, ExecutionException {
125 final DOMRpcResult mockResult = new DefaultDOMRpcResult(OUTPUT, List.of());
126 doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(RPC, INPUT);
127 final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, rpcService).get();
128 assertTrue(rpcResult.getErrors().isEmpty());
129 assertEquals(OUTPUT, rpcResult.getResult());
133 public void invokeRpcErrorsAndCheckTestTest() throws InterruptedException, ExecutionException {
134 final QName errorRpc = QName.create(RPC, "error-rpc");
135 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException(
136 "No implementation of RPC " + errorRpc + " available.");
137 doReturn(immediateFailedFluentFuture(exception)).when(rpcService).invokeRpc(errorRpc, INPUT);
138 final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, errorRpc, rpcService).get();
139 assertNull(rpcResult.getResult());
140 final Collection<? extends RpcError> errorList = rpcResult.getErrors();
141 assertEquals(1, errorList.size());
142 final RpcError actual = errorList.iterator().next();
143 assertEquals("No implementation of RPC " + errorRpc + " available.", actual.getMessage());
144 assertEquals(ErrorTag.OPERATION_FAILED, actual.getTag());
145 assertEquals(ErrorType.RPC, actual.getErrorType());
149 public void invokeRpcViaMountPointTest() throws InterruptedException, ExecutionException {
150 doReturn(Optional.ofNullable(rpcService)).when(mountPoint).getService(DOMRpcService.class);
151 final DOMRpcResult mockResult = new DefaultDOMRpcResult(OUTPUT, List.of());
152 doReturn(immediateFluentFuture(mockResult)).when(rpcService).invokeRpc(RPC, INPUT);
153 final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, mountPoint).get();
154 assertTrue(rpcResult.getErrors().isEmpty());
155 assertEquals(OUTPUT, rpcResult.getResult());
159 public void invokeRpcMissingMountPointServiceTest() {
160 doReturn(Optional.empty()).when(mountPoint).getService(DOMRpcService.class);
161 assertThrows(RestconfDocumentedException.class,
162 () -> RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, mountPoint));
166 public void checkResponseTest() throws InterruptedException, ExecutionException {
167 doReturn(immediateFluentFuture(new DefaultDOMRpcResult(OUTPUT, List.of())))
168 .when(rpcService).invokeRpc(RPC, INPUT);
169 final DOMRpcResult rpcResult = RestconfInvokeOperationsServiceImpl.invokeRpc(INPUT, RPC, rpcService).get();
170 assertTrue(rpcResult.getErrors().isEmpty());
171 assertEquals(OUTPUT, rpcResult.getResult());
174 private NormalizedNodePayload prepNNC(final ContainerNode result) {
175 final QName qname = QName.create("invoke:rpc:module", "2013-12-03", "rpc-test");
176 final RpcDefinition schemaNode = CONTEXT.getOperations().stream().filter(rpc -> rpc.getQName().equals(qname))
180 final ContainerNode data = mock(ContainerNode.class);
181 final DOMRpcResult domRpcResult = mock(DOMRpcResult.class);
182 doReturn(immediateFluentFuture(domRpcResult)).when(rpcService).invokeRpc(qname, data);
183 doReturn(result).when(domRpcResult).getResult();
184 return NormalizedNodePayload.of(
185 InstanceIdentifierContext.ofRpcInput(CONTEXT, schemaNode, null), data);