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