2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
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;
27 import com.google.common.io.Resources;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Optional;
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;
78 * Unit tests for JSONRestconfServiceImpl.
80 * @author Thomas Pantelis
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");
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");
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");
111 private static EffectiveModelContext schemaContext;
113 private final BrokerFacade brokerFacade = mock(BrokerFacade.class);
114 private final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
115 private JSONRestconfServiceImpl service;
118 public static void init() throws IOException, ReactorException {
119 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
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));
130 service = new JSONRestconfServiceImpl(controllerContext,
131 RestconfImpl.newInstance(brokerFacade, controllerContext));
134 private static String loadData(final String path) throws IOException {
135 return Resources.asCharSource(JSONRestconfServiceImplTest.class.getResource(path),
136 StandardCharsets.UTF_8).read();
139 @SuppressWarnings("rawtypes")
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()))
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);
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());
158 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
159 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
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.getIdentifier().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");
171 @SuppressWarnings("rawtypes")
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()))
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");
183 this.service.put(uriPath, payload);
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());
191 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
193 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
194 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
195 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getIdentifier().getNodeType());
196 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
197 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
200 @Test(expected = OperationFailedException.class)
201 @SuppressWarnings("checkstyle:IllegalThrows")
202 public void testPutFailure() throws Throwable {
203 final PutResult result = mock(PutResult.class);
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);
212 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
213 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
215 this.service.put(uriPath, payload);
218 @SuppressWarnings("rawtypes")
220 public void testPost() throws Exception {
221 doReturn(CommitInfo.emptyFluentFuture()).when(brokerFacade).commitConfigurationDataPost(
222 any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
225 final String uriPath = null;
226 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
228 this.service.post(uriPath, payload);
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());
236 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
238 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
239 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
240 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getIdentifier().getNodeType());
242 final java.util.Optional<DataContainerChild> mapChild = actualNode.findChildByArg(
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();
248 final NodeIdentifierWithPredicates entryNodeID = NodeIdentifierWithPredicates.of(
249 INTERFACE_QNAME, NAME_QNAME, "eth0");
250 final java.util.Optional<MapEntryNode> entryChild = mapNode.findChildByArg(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");
259 @SuppressWarnings("rawtypes")
261 public void testPostBehindMountPoint() throws Exception {
262 doReturn(CommitInfo.emptyFluentFuture()).when(brokerFacade).commitConfigurationDataPost(
263 notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class),
266 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
267 final String payload = loadData("/full-versions/testCont1Data.json");
269 this.service.post(uriPath, payload);
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());
277 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
279 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
280 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
281 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getIdentifier().getNodeType());
282 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
283 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
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());
293 final String uriPath = null;
294 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
297 this.service.post(uriPath, payload);
298 } catch (final OperationFailedException e) {
299 assertNotNull(e.getCause());
305 public void testPatch() throws Exception {
306 final PatchStatusContext result = mock(PatchStatusContext.class);
307 when(brokerFacade.patchConfigurationDataWithinTransaction(notNull(PatchContext.class)))
310 List<PatchStatusEntity> patchSTatus = new ArrayList<>();
312 PatchStatusEntity entity = new PatchStatusEntity("edit1", true, null);
314 patchSTatus.add(entity);
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);
324 assertTrue(patchResult.get().contains("\"ok\":[null]"));
328 public void testPatchBehindMountPoint() throws Exception {
329 final PatchStatusContext result = mock(PatchStatusContext.class);
330 when(brokerFacade.patchConfigurationDataWithinTransaction(notNull(PatchContext.class)))
333 List<PatchStatusEntity> patchSTatus = new ArrayList<>();
335 PatchStatusEntity entity = new PatchStatusEntity("edit1", true, null);
337 patchSTatus.add(entity);
339 when(result.getEditCollection())
340 .thenReturn(patchSTatus);
341 when(result.getGlobalErrors()).thenReturn(new ArrayList<>());
342 when(result.getPatchId()).thenReturn("1");
344 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
345 final String payload = loadData("/full-versions/testCont1DataPatch.json");
347 final Optional<String> patchResult = this.service.patch(uriPath, payload);
349 assertTrue(patchResult.get().contains("\"ok\":[null]"));
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"));
359 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
360 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
362 final Optional<String> patchResult = this.service.patch(uriPath, payload);
364 assertTrue("Patch output is not null", patchResult.isPresent());
365 String patch = patchResult.get();
366 assertTrue(patch.contains("TransactionCommitFailedException"));
370 public void testDelete() throws Exception {
371 doReturn(CommitInfo.emptyFluentFuture()).when(brokerFacade)
372 .commitConfigurationDataDelete(notNull(YangInstanceIdentifier.class));
374 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
376 this.service.delete(uriPath);
378 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
379 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
380 verify(brokerFacade).commitConfigurationDataDelete(capturedPath.capture());
382 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
383 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
386 @Test(expected = OperationFailedException.class)
387 public void testDeleteFailure() throws Exception {
388 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
390 this.service.delete(invalidUriPath);
394 public void testGetConfig() throws Exception {
395 testGet(LogicalDatastoreType.CONFIGURATION);
399 public void testGetOperational() throws Exception {
400 testGet(LogicalDatastoreType.OPERATIONAL);
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);
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);
417 @SuppressWarnings("rawtypes")
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));
424 final String uriPath = "toaster:make-toast";
425 final String input = loadData("/full-versions/make-toast-rpc-input.json");
427 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
429 assertEquals("Output present", false, output.isPresent());
431 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
432 verify(brokerFacade).invokeRpc(eq(MAKE_TOAST_QNAME), capturedNode.capture());
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);
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());
446 final String uriPath = "toaster:cancel-toast";
448 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
450 assertEquals("Output present", false, output.isPresent());
452 verify(brokerFacade).invokeRpc(eq(CANCEL_TOAST_QNAME), any());
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());
463 final String uriPath = "toaster:testOutput";
465 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
467 assertEquals("Output present", true, output.isPresent());
468 assertNotNull("Returned null response", output.get());
469 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
471 verify(brokerFacade).invokeRpc(eq(TEST_OUTPUT_QNAME), any());
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));
480 final String uriPath = "toaster:cancel-toast";
482 this.service.invokeRpc(uriPath, Optional.empty());
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"))
493 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
494 doReturn(entryNode).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
497 doReturn(entryNode).when(brokerFacade).readOperationalData(notNull(YangInstanceIdentifier.class));
500 final String uriPath = "/ietf-interfaces:interfaces/interface/eth0";
502 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
503 assertEquals("Response present", true, optionalResp.isPresent());
504 final String jsonResp = optionalResp.get();
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\""));
512 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
513 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
514 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
515 verify(brokerFacade).readConfigurationData(capturedPath.capture(), isNull());
517 verify(brokerFacade).readOperationalData(capturedPath.capture());
520 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
521 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
524 void verifyLeafNode(final DataContainerNode parent, final QName leafType, final Object leafValue) {
525 final java.util.Optional<DataContainerChild> leafChild = parent.findChildByArg(new NodeIdentifier(leafType));
526 assertTrue(leafType.toString() + " present", leafChild.isPresent());
527 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().body());
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());
534 for (final PathArgument actual: pathArgs) {
536 if (expArgs[index] instanceof Object[]) {
537 final Object[] listEntry = (Object[]) expArgs[index];
538 expNodeType = (QName) listEntry[0];
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));
547 expNodeType = (QName) expArgs[index];
550 assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());