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