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