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.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Matchers.isNull;
18 import static org.mockito.Matchers.notNull;
19 import static org.mockito.Matchers.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;
25 import com.google.common.base.Optional;
26 import com.google.common.io.Resources;
27 import com.google.common.util.concurrent.Futures;
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;
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.common.api.data.LogicalDatastoreType;
41 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
42 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
43 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
44 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
45 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
46 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
47 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
48 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
49 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
50 import org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceImpl;
51 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
52 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
53 import org.opendaylight.restconf.common.patch.PatchContext;
54 import org.opendaylight.restconf.common.patch.PatchStatusContext;
55 import org.opendaylight.restconf.common.patch.PatchStatusEntity;
56 import org.opendaylight.yangtools.yang.common.OperationFailedException;
57 import org.opendaylight.yangtools.yang.common.QName;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
62 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
64 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
68 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
69 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
70 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
71 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
72 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
75 * Unit tests for JSONRestconfServiceImpl.
77 * @author Thomas Pantelis
80 public class JSONRestconfServiceImplTest {
81 static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
82 static final String IETF_INTERFACES_VERSION = "2013-07-04";
83 static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
84 static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
85 static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
86 static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
87 static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
88 static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
90 static final String TEST_MODULE_NS = "test:module";
91 static final String TEST_MODULE_VERSION = "2014-01-09";
92 static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
93 static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
94 static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
95 static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
97 static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
98 static final String TOASTER_MODULE_VERSION = "2009-11-20";
99 static final QName TOASTER_DONENESS_QNAME =
100 QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
101 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
102 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
103 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
104 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
105 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
106 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
108 private static SchemaContext schemaContext;
110 private final BrokerFacade brokerFacade = mock(BrokerFacade.class);
111 private final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
112 private JSONRestconfServiceImpl service;
115 public static void init() throws IOException, ReactorException {
116 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
120 public void setup() throws FileNotFoundException {
121 final SchemaContext mountPointSchemaContext = TestUtils.loadSchemaContext("/full-versions/test-module");
122 final ControllerContext controllerContext =
123 TestRestconfUtils.newControllerContext(schemaContext, mockMountPoint);
124 doReturn(mountPointSchemaContext).when(mockMountPoint).getSchemaContext();
126 service = new JSONRestconfServiceImpl(controllerContext);
127 RestconfImpl.getInstance().setBroker(brokerFacade);
128 RestconfImpl.getInstance().setControllerContext(controllerContext);
131 private static String loadData(final String path) throws IOException {
132 return Resources.asCharSource(JSONRestconfServiceImplTest.class.getResource(path),
133 StandardCharsets.UTF_8).read();
136 @SuppressWarnings("rawtypes")
138 public void testPut() throws Exception {
139 final PutResult result = mock(PutResult.class);
140 when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
141 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
142 Mockito.anyString())).thenReturn(result);
143 when(result.getFutureOfPutData())
144 .thenReturn(Futures.immediateCheckedFuture(null));
145 when(result.getStatus()).thenReturn(Status.OK);
146 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
147 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
148 this.service.put(uriPath, payload);
150 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
151 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
152 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
153 verify(brokerFacade).commitConfigurationDataPut(notNull(SchemaContext.class), capturedPath.capture(),
154 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
156 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
157 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
159 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
160 capturedNode.getValue() instanceof MapEntryNode);
161 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
162 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
163 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
164 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
165 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
166 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
169 @SuppressWarnings("rawtypes")
171 public void testPutBehindMountPoint() throws Exception {
172 final PutResult result = mock(PutResult.class);
173 when(brokerFacade.commitMountPointDataPut(notNull(DOMMountPoint.class),
174 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
175 Mockito.anyString())).thenReturn(result);
176 when(result.getFutureOfPutData()).thenReturn(Futures.immediateCheckedFuture(null));
177 when(result.getStatus()).thenReturn(Status.OK);
178 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
179 final String payload = loadData("/full-versions/testCont1Data.json");
181 this.service.put(uriPath, payload);
183 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
184 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
185 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
186 verify(brokerFacade).commitMountPointDataPut(same(mockMountPoint), capturedPath.capture(),
187 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
189 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
191 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
192 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
193 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
194 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
195 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
198 @Test(expected = OperationFailedException.class)
199 @SuppressWarnings("checkstyle:IllegalThrows")
200 public void testPutFailure() throws Throwable {
201 final PutResult result = mock(PutResult.class);
203 when(result.getFutureOfPutData())
204 .thenReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")));
205 when(result.getStatus()).thenReturn(Status.OK);
206 when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
207 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
208 Mockito.anyString())).thenReturn(result);
210 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
211 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
213 this.service.put(uriPath, payload);
216 @SuppressWarnings("rawtypes")
218 public void testPost() throws Exception {
219 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
220 any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
221 Mockito.anyString(), Mockito.anyString());
223 final String uriPath = null;
224 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
226 this.service.post(uriPath, payload);
228 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
229 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
230 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
231 verify(brokerFacade).commitConfigurationDataPost(notNull(SchemaContext.class), capturedPath.capture(),
232 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
234 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
236 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
237 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
238 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
240 final java.util.Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(
241 new NodeIdentifier(INTERFACE_QNAME));
242 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
243 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
244 final MapNode mapNode = (MapNode)mapChild.get();
246 final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
247 INTERFACE_QNAME, NAME_QNAME, "eth0");
248 final java.util.Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
249 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
250 final MapEntryNode entryNode = entryChild.get();
251 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
252 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
253 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
254 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
257 @SuppressWarnings("rawtypes")
259 public void testPostBehindMountPoint() throws Exception {
260 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
261 notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class),
262 Mockito.anyString(), Mockito.anyString());
264 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
265 final String payload = loadData("/full-versions/testCont1Data.json");
267 this.service.post(uriPath, payload);
269 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
270 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
271 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
272 verify(brokerFacade).commitConfigurationDataPost(same(mockMountPoint), capturedPath.capture(),
273 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
275 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
277 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
278 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
279 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
280 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
281 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
284 @Test(expected = TransactionCommitFailedException.class)
285 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
286 public void testPostFailure() throws Throwable {
287 doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock"))).when(brokerFacade)
288 .commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
289 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString());
291 final String uriPath = null;
292 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
295 this.service.post(uriPath, payload);
296 } catch (final OperationFailedException e) {
297 assertNotNull(e.getCause());
302 @SuppressWarnings("rawtypes")
304 public void testPatch() throws Exception {
305 final PatchStatusContext result = mock(PatchStatusContext.class);
306 when(brokerFacade.patchConfigurationDataWithinTransaction(notNull(PatchContext.class)))
309 List<PatchStatusEntity> patchSTatus = new ArrayList<>();
311 PatchStatusEntity entity = new PatchStatusEntity("edit1", true, null);
313 patchSTatus.add(entity);
315 when(result.getEditCollection())
316 .thenReturn(patchSTatus);
317 when(result.getGlobalErrors()).thenReturn(new ArrayList<>());
318 when(result.getPatchId()).thenReturn("1");
319 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
320 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
321 final Optional<String> patchResult = this.service.patch(uriPath, payload);
323 assertTrue(patchResult.get().contains("\"ok\":[null]"));
326 @SuppressWarnings("rawtypes")
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(Futures.immediateCheckedFuture(null)).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 SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
422 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
423 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(eq(path),
424 any(NormalizedNode.class));
426 final String uriPath = "toaster:make-toast";
427 final String input = loadData("/full-versions/make-toast-rpc-input.json");
429 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
431 assertEquals("Output present", false, output.isPresent());
433 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
434 verify(brokerFacade).invokeRpc(eq(path), capturedNode.capture());
436 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
437 capturedNode.getValue() instanceof ContainerNode);
438 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
439 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Long.valueOf(10));
440 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
444 public void testInvokeRpcWithNoInput() throws Exception {
445 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
447 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
448 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
449 any(NormalizedNode.class));
451 final String uriPath = "toaster:cancel-toast";
453 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
455 assertEquals("Output present", false, output.isPresent());
457 verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
461 public void testInvokeRpcWithOutput() throws Exception {
462 final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
464 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
465 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
466 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
467 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
468 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
469 any(NormalizedNode.class));
471 final String uriPath = "toaster:testOutput";
473 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
475 assertEquals("Output present", true, output.isPresent());
476 assertNotNull("Returned null response", output.get());
477 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
479 verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
482 @Test(expected = OperationFailedException.class)
483 public void testInvokeRpcFailure() throws Exception {
484 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
485 doReturn(Futures.immediateFailedCheckedFuture(exception)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
486 any(NormalizedNode.class));
488 final String uriPath = "toaster:cancel-toast";
490 this.service.invokeRpc(uriPath, Optional.<String>absent());
493 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
494 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
495 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
496 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
497 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
498 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
501 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
502 doReturn(entryNode).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
503 Mockito.anyString());
505 doReturn(entryNode).when(brokerFacade).readOperationalData(notNull(YangInstanceIdentifier.class));
508 final String uriPath = "/ietf-interfaces:interfaces/interface/eth0";
510 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
511 assertEquals("Response present", true, optionalResp.isPresent());
512 final String jsonResp = optionalResp.get();
514 assertNotNull("Returned null response", jsonResp);
515 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
516 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
517 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
518 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
520 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
521 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
522 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
523 verify(brokerFacade).readConfigurationData(capturedPath.capture(), Mockito.anyString());
525 verify(brokerFacade).readOperationalData(capturedPath.capture());
528 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
529 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
532 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
533 final java.util.Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
534 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
535 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
538 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
539 final List<PathArgument> pathArgs = path.getPathArguments();
540 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
542 for (final PathArgument actual: pathArgs) {
544 if (expArgs[index] instanceof Object[]) {
545 final Object[] listEntry = (Object[]) expArgs[index];
546 expNodeType = (QName) listEntry[0];
548 assertTrue(actual instanceof NodeIdentifierWithPredicates);
549 final Map<QName, Object> keyValues = ((NodeIdentifierWithPredicates)actual).getKeyValues();
550 assertEquals(String.format("Path arg %d keyValues size", index + 1), 1, keyValues.size());
551 final QName expKey = (QName) listEntry[1];
552 assertEquals(String.format("Path arg %d keyValue for %s", index + 1, expKey), listEntry[2],
553 keyValues.get(expKey));
555 expNodeType = (QName) expArgs[index];
558 assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());