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