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