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