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