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