6729ea1b01286d69008f7f292708bddf9148e4fa
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / JSONRestconfServiceImplTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.controller.sal.restconf.impl.test;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Matchers.isNull;
18 import static org.mockito.Matchers.notNull;
19 import static org.mockito.Matchers.same;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.reset;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 import com.google.common.base.Optional;
26 import com.google.common.io.Resources;
27 import com.google.common.util.concurrent.Futures;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.Response.Status;
36 import javax.ws.rs.core.UriBuilder;
37 import javax.ws.rs.core.UriInfo;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mockito;
43 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
44 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
45 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
46 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
47 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
48 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
49 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
50 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
51 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
52 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
53 import org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceImpl;
54 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
55 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
56 import org.opendaylight.yangtools.yang.common.OperationFailedException;
57 import org.opendaylight.yangtools.yang.common.QName;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
62 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
64 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
68 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
69 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
70 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
71 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
72 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
73
74 /**
75  * Unit tests for JSONRestconfServiceImpl.
76  *
77  * @author Thomas Pantelis
78  */
79 @Deprecated
80 public class JSONRestconfServiceImplTest {
81     static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
82     static final String IETF_INTERFACES_VERSION = "2013-07-04";
83     static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
84     static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
85     static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
86     static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
87     static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
88     static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
89
90     static final String TEST_MODULE_NS = "test:module";
91     static final String TEST_MODULE_VERSION = "2014-01-09";
92     static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
93     static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
94     static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
95     static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
96
97     static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
98     static final String TOASTER_MODULE_VERSION = "2009-11-20";
99     static final QName TOASTER_DONENESS_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
100     static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
101     static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
102     static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
103     static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
104     static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
105     static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
106
107     private static BrokerFacade brokerFacade;
108
109     private final JSONRestconfServiceImpl service = new JSONRestconfServiceImpl();
110
111     @BeforeClass
112     public static void init() throws IOException, ReactorException {
113         ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext("/full-versions/yangs"));
114         brokerFacade = mock(BrokerFacade.class);
115         RestconfImpl.getInstance().setBroker(brokerFacade);
116         RestconfImpl.getInstance().setControllerContext(ControllerContext.getInstance());
117     }
118
119     @Before
120     public void setup() {
121         reset(brokerFacade);
122     }
123
124     private static String loadData(final String path) throws IOException {
125         return Resources.asCharSource(JSONRestconfServiceImplTest.class.getResource(path), StandardCharsets.UTF_8).read();
126     }
127
128     @SuppressWarnings("rawtypes")
129     @Test
130     public void testPut() throws Exception {
131         final PutResult result = mock(PutResult.class);
132         when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
133                 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
134                 Mockito.anyString())).thenReturn(result);
135         when(result.getFutureOfPutData())
136                 .thenReturn(Futures.immediateCheckedFuture(null));
137         when(result.getStatus()).thenReturn(Status.OK);
138         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
139         final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
140         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
141         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
142         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
143         Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
144         this.service.put(uriPath, payload, uriInfo);
145
146         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
147         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
148         verify(brokerFacade).commitConfigurationDataPut(notNull(SchemaContext.class), capturedPath.capture(),
149                 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
150
151         verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
152                 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
153
154         assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
155                 capturedNode.getValue() instanceof MapEntryNode);
156         final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
157         assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
158         verifyLeafNode(actualNode, NAME_QNAME, "eth0");
159         verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
160         verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
161         verifyLeafNode(actualNode, DESC_QNAME, "some interface");
162     }
163
164     @SuppressWarnings("rawtypes")
165     @Test
166     public void testPutBehindMountPoint() throws Exception {
167         final DOMMountPoint mockMountPoint = setupTestMountPoint();
168         final PutResult result = mock(PutResult.class);
169         when(brokerFacade.commitMountPointDataPut(notNull(DOMMountPoint.class),
170                 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
171                 Mockito.anyString())).thenReturn(result);
172         when(result.getFutureOfPutData()).thenReturn(Futures.immediateCheckedFuture(null));
173         when(result.getStatus()).thenReturn(Status.OK);
174         final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
175         final String payload = loadData("/full-versions/testCont1Data.json");
176
177         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
178         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
179         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
180         Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
181         this.service.put(uriPath, payload, uriInfo);
182
183         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
184         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
185         verify(brokerFacade).commitMountPointDataPut(same(mockMountPoint), capturedPath.capture(),
186                 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
187
188         verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
189
190         assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
191         final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
192         assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
193         verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
194         verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
195     }
196
197     @Test(expected = OperationFailedException.class)
198     public void testPutFailure() throws Throwable {
199         final PutResult result = mock(PutResult.class);
200
201         when(result.getFutureOfPutData())
202                 .thenReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")));
203         when(result.getStatus()).thenReturn(Status.OK);
204         when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
205                 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
206                 Mockito.anyString())).thenReturn(result);
207
208         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
209         final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
210
211         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
212         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
213         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
214         Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
215         this.service.put(uriPath, payload, uriInfo);
216     }
217
218     @SuppressWarnings("rawtypes")
219     @Test
220     public void testPost() throws Exception {
221         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
222                 any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
223                 Mockito.anyString(), Mockito.anyString());
224
225         final String uriPath = null;
226         final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
227
228         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
229         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
230         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
231         Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
232         final UriBuilder uriBuilder = UriBuilder.fromPath("");
233         Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
234         this.service.post(uriPath, payload, uriInfo);
235
236         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
237         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
238         verify(brokerFacade).commitConfigurationDataPost(notNull(SchemaContext.class), capturedPath.capture(),
239                 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
240
241         verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
242
243         assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
244         final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
245         assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
246
247         final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
248         assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
249         assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
250         final MapNode mapNode = (MapNode)mapChild.get();
251
252         final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
253                 INTERFACE_QNAME, NAME_QNAME, "eth0");
254         final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
255         assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
256         final MapEntryNode entryNode = entryChild.get();
257         verifyLeafNode(entryNode, NAME_QNAME, "eth0");
258         verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
259         verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
260         verifyLeafNode(entryNode, DESC_QNAME, "some interface");
261     }
262
263     @SuppressWarnings("rawtypes")
264     @Test
265     public void testPostBehindMountPoint() throws Exception {
266         final DOMMountPoint mockMountPoint = setupTestMountPoint();
267         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
268                 notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class),
269                 Mockito.anyString(), Mockito.anyString());
270
271         final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
272         final String payload = loadData("/full-versions/testCont1Data.json");
273
274         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
275         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
276         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
277         Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
278         final UriBuilder uriBuilder = UriBuilder.fromPath("");
279         Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
280         this.service.post(uriPath, payload, uriInfo);
281
282         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
283         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
284         verify(brokerFacade).commitConfigurationDataPost(same(mockMountPoint), capturedPath.capture(),
285                 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
286
287         verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
288
289         assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
290         final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
291         assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
292         verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
293         verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
294     }
295
296     @Test
297     public void testPostFailure() throws Throwable {
298         doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock"))).when(brokerFacade)
299                 .commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
300                         any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString());
301
302         final String uriPath = null;
303         final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
304
305         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
306         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
307         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
308         Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
309         final UriBuilder uriBuilder = UriBuilder.fromPath("");
310         Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
311         this.service.post(uriPath, payload, uriInfo);
312     }
313
314     @Test
315     public void testDelete() throws Exception {
316         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade)
317                 .commitConfigurationDataDelete(notNull(YangInstanceIdentifier.class));
318
319         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
320
321         this.service.delete(uriPath);
322
323         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
324         verify(brokerFacade).commitConfigurationDataDelete(capturedPath.capture());
325
326         verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
327                 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
328     }
329
330     @Test(expected=OperationFailedException.class)
331     public void testDeleteFailure() throws Exception {
332         final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
333
334         this.service.delete(invalidUriPath);
335     }
336
337     @Test
338     public void testGetConfig() throws Exception {
339         testGet(LogicalDatastoreType.CONFIGURATION);
340     }
341
342     @Test
343     public void testGetOperational() throws Exception {
344         testGet(LogicalDatastoreType.OPERATIONAL);
345     }
346
347     @Test
348     public void testGetWithNoData() throws OperationFailedException {
349         doReturn(null).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
350                 Mockito.anyString());
351         final String uriPath = "ietf-interfaces:interfaces";
352         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
353         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
354         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
355         Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
356         this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
357     }
358
359     @Test(expected=OperationFailedException.class)
360     public void testGetFailure() throws Exception {
361         final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
362         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
363         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
364         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
365         Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
366         this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
367     }
368
369     @SuppressWarnings("rawtypes")
370     @Test
371     public void testInvokeRpcWithInput() throws Exception {
372         final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
373
374         final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
375         doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(eq(path),
376                 any(NormalizedNode.class));
377
378         final String uriPath = "toaster:make-toast";
379         final String input = loadData("/full-versions/make-toast-rpc-input.json");
380
381         final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
382
383         assertEquals("Output present", false, output.isPresent());
384
385         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
386         verify(brokerFacade).invokeRpc(eq(path), capturedNode.capture());
387
388         assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
389                 capturedNode.getValue() instanceof ContainerNode);
390         final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
391         verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Long.valueOf(10));
392         verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
393     }
394
395     @Test
396     public void testInvokeRpcWithNoInput() throws Exception {
397         final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
398
399         final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
400         doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
401                 any(NormalizedNode.class));
402
403         final String uriPath = "toaster:cancel-toast";
404
405         final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
406
407         assertEquals("Output present", false, output.isPresent());
408
409         verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
410     }
411
412     @Test
413     public void testInvokeRpcWithOutput() throws Exception {
414         final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
415
416         final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
417                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
418                 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
419         final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
420         doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
421                 any(NormalizedNode.class));
422
423         final String uriPath = "toaster:testOutput";
424
425         final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
426
427         assertEquals("Output present", true, output.isPresent());
428         assertNotNull("Returned null response", output.get());
429         assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
430
431         verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
432     }
433
434     @Test(expected=OperationFailedException.class)
435     public void testInvokeRpcFailure() throws Exception {
436         final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
437         doReturn(Futures.immediateFailedCheckedFuture(exception)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
438                 any(NormalizedNode.class));
439
440         final String uriPath = "toaster:cancel-toast";
441
442         this.service.invokeRpc(uriPath, Optional.<String>absent());
443     }
444
445     void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
446         final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
447                 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
448                 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
449                 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
450                 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
451                 .build();
452
453         if(datastoreType == LogicalDatastoreType.CONFIGURATION) {
454             doReturn(entryNode).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
455                     Mockito.anyString());
456         } else {
457             doReturn(entryNode).when(brokerFacade).readOperationalData(notNull(YangInstanceIdentifier.class));
458         }
459
460         final String uriPath = "/ietf-interfaces:interfaces/interface/eth0";
461         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
462         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
463         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
464         Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
465         Mockito.when(uriInfo.getQueryParameters(false)).thenReturn(value);
466         Mockito.when(value.getFirst("depth")).thenReturn("");
467
468         final Optional<String> optionalResp = this.service.get(uriPath, datastoreType, uriInfo);
469         assertEquals("Response present", true, optionalResp.isPresent());
470         final String jsonResp = optionalResp.get();
471
472         assertNotNull("Returned null response", jsonResp);
473         assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
474         assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
475         assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
476         assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
477
478         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
479         if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
480             verify(brokerFacade).readConfigurationData(capturedPath.capture(), Mockito.anyString());
481         } else {
482             verify(brokerFacade).readOperationalData(capturedPath.capture());
483         }
484
485         verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
486                 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
487     }
488
489     DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
490         final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
491         final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
492         doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
493
494         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
495         doReturn(Optional.of(mockMountPoint)).when(mockMountService).getMountPoint(notNull(YangInstanceIdentifier.class));
496
497         ControllerContext.getInstance().setMountService(mockMountService);
498         return mockMountPoint;
499     }
500
501     void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
502         final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
503         assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
504         assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
505     }
506
507     void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
508         final List<PathArgument> pathArgs = path.getPathArguments();
509         assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
510         int i = 0;
511         for(final PathArgument actual: pathArgs) {
512             QName expNodeType;
513             if(expArgs[i] instanceof Object[]) {
514                 final Object[] listEntry = (Object[]) expArgs[i];
515                 expNodeType = (QName) listEntry[0];
516
517                 assertTrue(actual instanceof NodeIdentifierWithPredicates);
518                 final Map<QName, Object> keyValues = ((NodeIdentifierWithPredicates)actual).getKeyValues();
519                 assertEquals(String.format("Path arg %d keyValues size", i + 1), 1, keyValues.size());
520                 final QName expKey = (QName) listEntry[1];
521                 assertEquals(String.format("Path arg %d keyValue for %s", i + 1, expKey), listEntry[2],
522                         keyValues.get(expKey));
523             } else {
524                 expNodeType = (QName) expArgs[i];
525             }
526
527             assertEquals(String.format("Path arg %d node type", i + 1), expNodeType, actual.getNodeType());
528             i++;
529         }
530
531     }
532 }