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