Update MRI projects for Aluminium
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
15
16 import java.util.Collections;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.Response;
19 import javax.ws.rs.core.UriInfo;
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.common.api.CommitInfo;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
27 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
28 import org.opendaylight.mdsal.dom.api.DOMRpcService;
29 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
30 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
31 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
32 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
33 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
34 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
36 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
37 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45
46 public class RestconfInvokeOperationsServiceImplTest {
47
48     private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/invoke-rpc";
49
50     private RestconfInvokeOperationsServiceImpl invokeOperationsService;
51
52     @Mock
53     private RpcServiceHandler rpcServiceHandler;
54
55     @Mock
56     private DOMRpcService rpcService;
57
58     @Before
59     public void setup() throws Exception {
60         MockitoAnnotations.initMocks(this);
61         final SchemaContextRef contextRef = new SchemaContextRef(
62                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)));
63         final TransactionChainHandler txHandler = mock(TransactionChainHandler.class);
64         final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
65         when(txHandler.get()).thenReturn(domTx);
66         final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
67         when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
68         doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
69         final SchemaContextHandler schemaContextHandler = SchemaContextHandler.newInstance(txHandler,
70             mock(DOMSchemaService.class));
71         schemaContextHandler.onModelContextUpdated(contextRef.get());
72         this.invokeOperationsService =
73                 new RestconfInvokeOperationsServiceImpl(this.rpcServiceHandler, schemaContextHandler);
74         when(this.rpcServiceHandler.get()).thenReturn(this.rpcService);
75     }
76
77     @Test
78     public void testInvokeRpcWithNonEmptyOutput() {
79         final String identifier = "invoke-rpc-module:rpcTest";
80         final ContainerNode result = mock(ContainerNode.class);
81         final LeafNode outputChild = mock(LeafNode.class);
82         when(result.getValue()).thenReturn(Collections.singleton(outputChild));
83
84         final NormalizedNodeContext payload = prepNNC(result);
85         final UriInfo uriInfo = mock(UriInfo.class);
86
87         final NormalizedNodeContext rpc = this.invokeOperationsService.invokeRpc(identifier, payload, uriInfo);
88         assertEquals(result, rpc.getData());
89     }
90
91     @Test
92     public void testInvokeRpcWithEmptyOutput() {
93         final String identifier = "invoke-rpc-module:rpcTest";
94         final ContainerNode result = mock(ContainerNode.class);
95
96         final NormalizedNodeContext payload = prepNNC(result);
97         final UriInfo uriInfo = mock(UriInfo.class);
98
99         WebApplicationException exceptionToBeThrown = null;
100         try {
101             this.invokeOperationsService.invokeRpc(identifier, payload, uriInfo);
102         } catch (final WebApplicationException exception) {
103             exceptionToBeThrown = exception;
104
105         }
106         Assert.assertNotNull("WebApplicationException with status code 204 is expected.", exceptionToBeThrown);
107         Assert.assertEquals(Response.Status.NO_CONTENT.getStatusCode(), exceptionToBeThrown.getResponse().getStatus());
108     }
109
110     private NormalizedNodeContext prepNNC(final NormalizedNode<?, ?> result) {
111         final InstanceIdentifierContext<?> context = mock(InstanceIdentifierContext.class);
112         final RpcDefinition schemaNode = mock(RpcDefinition.class);
113         final QName qname = QName.create("invoke:rpc:module", "2013-12-03", "rpcTest");
114         final SchemaPath schemaPath = SchemaPath.create(true, qname);
115         when(schemaNode.getPath()).thenReturn(schemaPath);
116         when(schemaNode.getQName()).thenReturn(qname);
117         doReturn(schemaNode).when(context).getSchemaNode();
118         final NormalizedNode<?, ?> data = mock(NormalizedNode.class);
119         final DOMRpcResult domRpcResult = mock(DOMRpcResult.class);
120         doReturn(immediateFluentFuture(domRpcResult)).when(this.rpcService).invokeRpc(schemaPath, data);
121         doReturn(result).when(domRpcResult).getResult();
122         return new NormalizedNodeContext(context, data);
123     }
124 }