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