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