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.reset;
23 import static org.mockito.Mockito.verify;
24 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.HashSet;
32 import java.util.List;
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.Response.Status;
36 import javax.ws.rs.core.UriBuilder;
37 import javax.ws.rs.core.UriInfo;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mockito;
43 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
44 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
45 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
46 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
47 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
48 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
49 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
50 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
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.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 = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
100 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
101 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
102 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
103 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
104 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
105 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
107 private static BrokerFacade brokerFacade;
109 private final JSONRestconfServiceImpl service = new JSONRestconfServiceImpl();
112 public static void init() throws IOException, ReactorException {
113 ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext("/full-versions/yangs"));
114 brokerFacade = mock(BrokerFacade.class);
115 RestconfImpl.getInstance().setBroker(brokerFacade);
116 RestconfImpl.getInstance().setControllerContext(ControllerContext.getInstance());
120 public void setup() {
124 private static String loadData(final String path) throws IOException {
125 return Resources.asCharSource(JSONRestconfServiceImplTest.class.getResource(path), StandardCharsets.UTF_8).read();
128 @SuppressWarnings("rawtypes")
130 public void testPut() throws Exception {
131 final PutResult result = mock(PutResult.class);
132 when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
133 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
134 Mockito.anyString())).thenReturn(result);
135 when(result.getFutureOfPutData())
136 .thenReturn(Futures.immediateCheckedFuture(null));
137 when(result.getStatus()).thenReturn(Status.OK);
138 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
139 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
140 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
141 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
142 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
143 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
144 this.service.put(uriPath, payload, uriInfo);
146 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
147 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
148 verify(brokerFacade).commitConfigurationDataPut(notNull(SchemaContext.class), capturedPath.capture(),
149 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
151 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
152 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
154 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
155 capturedNode.getValue() instanceof MapEntryNode);
156 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
157 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
158 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
159 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
160 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
161 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
164 @SuppressWarnings("rawtypes")
166 public void testPutBehindMountPoint() throws Exception {
167 final DOMMountPoint mockMountPoint = setupTestMountPoint();
168 final PutResult result = mock(PutResult.class);
169 when(brokerFacade.commitMountPointDataPut(notNull(DOMMountPoint.class),
170 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
171 Mockito.anyString())).thenReturn(result);
172 when(result.getFutureOfPutData()).thenReturn(Futures.immediateCheckedFuture(null));
173 when(result.getStatus()).thenReturn(Status.OK);
174 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
175 final String payload = loadData("/full-versions/testCont1Data.json");
177 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
178 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
179 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
180 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
181 this.service.put(uriPath, payload, uriInfo);
183 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
184 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
185 verify(brokerFacade).commitMountPointDataPut(same(mockMountPoint), capturedPath.capture(),
186 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
188 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
190 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
191 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
192 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
193 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
194 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
197 @Test(expected = OperationFailedException.class)
198 public void testPutFailure() throws Throwable {
199 final PutResult result = mock(PutResult.class);
201 when(result.getFutureOfPutData())
202 .thenReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")));
203 when(result.getStatus()).thenReturn(Status.OK);
204 when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
205 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
206 Mockito.anyString())).thenReturn(result);
208 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
209 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
211 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
212 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
213 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
214 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
215 this.service.put(uriPath, payload, uriInfo);
218 @SuppressWarnings("rawtypes")
220 public void testPost() throws Exception {
221 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
222 any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
223 Mockito.anyString(), Mockito.anyString());
225 final String uriPath = null;
226 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
228 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
229 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
230 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
231 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
232 final UriBuilder uriBuilder = UriBuilder.fromPath("");
233 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
234 this.service.post(uriPath, payload, uriInfo);
236 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
237 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
238 verify(brokerFacade).commitConfigurationDataPost(notNull(SchemaContext.class), capturedPath.capture(),
239 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
241 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
243 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
244 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
245 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
247 final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
248 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
249 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
250 final MapNode mapNode = (MapNode)mapChild.get();
252 final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
253 INTERFACE_QNAME, NAME_QNAME, "eth0");
254 final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
255 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
256 final MapEntryNode entryNode = entryChild.get();
257 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
258 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
259 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
260 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
263 @SuppressWarnings("rawtypes")
265 public void testPostBehindMountPoint() throws Exception {
266 final DOMMountPoint mockMountPoint = setupTestMountPoint();
267 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
268 notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class),
269 Mockito.anyString(), Mockito.anyString());
271 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
272 final String payload = loadData("/full-versions/testCont1Data.json");
274 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
275 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
276 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
277 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
278 final UriBuilder uriBuilder = UriBuilder.fromPath("");
279 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
280 this.service.post(uriPath, payload, uriInfo);
282 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
283 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
284 verify(brokerFacade).commitConfigurationDataPost(same(mockMountPoint), capturedPath.capture(),
285 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
287 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
289 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
290 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
291 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
292 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
293 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
297 public void testPostFailure() throws Throwable {
298 doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock"))).when(brokerFacade)
299 .commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
300 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString());
302 final String uriPath = null;
303 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
305 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
306 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
307 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
308 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
309 final UriBuilder uriBuilder = UriBuilder.fromPath("");
310 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
311 this.service.post(uriPath, payload, uriInfo);
315 public void testDelete() throws Exception {
316 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade)
317 .commitConfigurationDataDelete(notNull(YangInstanceIdentifier.class));
319 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
321 this.service.delete(uriPath);
323 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
324 verify(brokerFacade).commitConfigurationDataDelete(capturedPath.capture());
326 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
327 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
330 @Test(expected=OperationFailedException.class)
331 public void testDeleteFailure() throws Exception {
332 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
334 this.service.delete(invalidUriPath);
338 public void testGetConfig() throws Exception {
339 testGet(LogicalDatastoreType.CONFIGURATION);
343 public void testGetOperational() throws Exception {
344 testGet(LogicalDatastoreType.OPERATIONAL);
348 public void testGetWithNoData() throws OperationFailedException {
349 doReturn(null).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
350 Mockito.anyString());
351 final String uriPath = "ietf-interfaces:interfaces";
352 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
353 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
354 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
355 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
356 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
359 @Test(expected=OperationFailedException.class)
360 public void testGetFailure() throws Exception {
361 final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
362 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
363 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
364 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
365 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
366 this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
369 @SuppressWarnings("rawtypes")
371 public void testInvokeRpcWithInput() throws Exception {
372 final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
374 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
375 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(eq(path),
376 any(NormalizedNode.class));
378 final String uriPath = "toaster:make-toast";
379 final String input = loadData("/full-versions/make-toast-rpc-input.json");
381 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
383 assertEquals("Output present", false, output.isPresent());
385 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
386 verify(brokerFacade).invokeRpc(eq(path), capturedNode.capture());
388 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
389 capturedNode.getValue() instanceof ContainerNode);
390 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
391 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Long.valueOf(10));
392 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
396 public void testInvokeRpcWithNoInput() throws Exception {
397 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
399 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
400 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
401 any(NormalizedNode.class));
403 final String uriPath = "toaster:cancel-toast";
405 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
407 assertEquals("Output present", false, output.isPresent());
409 verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
413 public void testInvokeRpcWithOutput() throws Exception {
414 final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
416 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
417 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
418 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
419 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
420 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
421 any(NormalizedNode.class));
423 final String uriPath = "toaster:testOutput";
425 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
427 assertEquals("Output present", true, output.isPresent());
428 assertNotNull("Returned null response", output.get());
429 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
431 verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
434 @Test(expected=OperationFailedException.class)
435 public void testInvokeRpcFailure() throws Exception {
436 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
437 doReturn(Futures.immediateFailedCheckedFuture(exception)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
438 any(NormalizedNode.class));
440 final String uriPath = "toaster:cancel-toast";
442 this.service.invokeRpc(uriPath, Optional.<String>absent());
445 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
446 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
447 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
448 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
449 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
450 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
453 if(datastoreType == LogicalDatastoreType.CONFIGURATION) {
454 doReturn(entryNode).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
455 Mockito.anyString());
457 doReturn(entryNode).when(brokerFacade).readOperationalData(notNull(YangInstanceIdentifier.class));
460 final String uriPath = "/ietf-interfaces:interfaces/interface/eth0";
461 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
462 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
463 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
464 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
465 Mockito.when(uriInfo.getQueryParameters(false)).thenReturn(value);
466 Mockito.when(value.getFirst("depth")).thenReturn("");
468 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType, uriInfo);
469 assertEquals("Response present", true, optionalResp.isPresent());
470 final String jsonResp = optionalResp.get();
472 assertNotNull("Returned null response", jsonResp);
473 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
474 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
475 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
476 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
478 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
479 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
480 verify(brokerFacade).readConfigurationData(capturedPath.capture(), Mockito.anyString());
482 verify(brokerFacade).readOperationalData(capturedPath.capture());
485 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
486 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
489 DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
490 final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
491 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
492 doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
494 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
495 doReturn(Optional.of(mockMountPoint)).when(mockMountService).getMountPoint(notNull(YangInstanceIdentifier.class));
497 ControllerContext.getInstance().setMountService(mockMountService);
498 return mockMountPoint;
501 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
502 final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
503 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
504 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
507 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
508 final List<PathArgument> pathArgs = path.getPathArguments();
509 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
511 for(final PathArgument actual: pathArgs) {
513 if(expArgs[i] instanceof Object[]) {
514 final Object[] listEntry = (Object[]) expArgs[i];
515 expNodeType = (QName) listEntry[0];
517 assertTrue(actual instanceof NodeIdentifierWithPredicates);
518 final Map<QName, Object> keyValues = ((NodeIdentifierWithPredicates)actual).getKeyValues();
519 assertEquals(String.format("Path arg %d keyValues size", i + 1), 1, keyValues.size());
520 final QName expKey = (QName) listEntry[1];
521 assertEquals(String.format("Path arg %d keyValue for %s", i + 1, expKey), listEntry[2],
522 keyValues.get(expKey));
524 expNodeType = (QName) expArgs[i];
527 assertEquals(String.format("Path arg %d node type", i + 1), expNodeType, actual.getNodeType());