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