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;
26 import com.google.common.base.Optional;
27 import com.google.common.io.Resources;
28 import com.google.common.util.concurrent.Futures;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.HashSet;
33 import java.util.List;
35 import javax.ws.rs.core.MultivaluedMap;
36 import javax.ws.rs.core.Response.Status;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Mockito;
44 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
45 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
46 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
47 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
48 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
49 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
50 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
51 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
52 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
53 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
54 import org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceImpl;
55 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
56 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
57 import org.opendaylight.yangtools.yang.common.OperationFailedException;
58 import org.opendaylight.yangtools.yang.common.QName;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
63 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
65 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
69 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
70 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
71 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
72 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
73 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
76 * Unit tests for JSONRestconfServiceImpl.
78 * @author Thomas Pantelis
81 public class JSONRestconfServiceImplTest {
82 static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
83 static final String IETF_INTERFACES_VERSION = "2013-07-04";
84 static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
85 static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
86 static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
87 static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
88 static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
89 static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
91 static final String TEST_MODULE_NS = "test:module";
92 static final String TEST_MODULE_VERSION = "2014-01-09";
93 static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
94 static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
95 static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
96 static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
98 static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
99 static final String TOASTER_MODULE_VERSION = "2009-11-20";
100 static final QName TOASTER_DONENESS_QNAME = 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 BrokerFacade brokerFacade;
110 private final JSONRestconfServiceImpl service = new JSONRestconfServiceImpl();
113 public static void init() throws IOException, ReactorException {
114 ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext("/full-versions/yangs"));
115 brokerFacade = mock(BrokerFacade.class);
116 RestconfImpl.getInstance().setBroker(brokerFacade);
117 RestconfImpl.getInstance().setControllerContext(ControllerContext.getInstance());
121 public void setup() {
125 private static String loadData(final String path) throws IOException {
126 return Resources.asCharSource(JSONRestconfServiceImplTest.class.getResource(path), StandardCharsets.UTF_8).read();
129 @SuppressWarnings("rawtypes")
131 public void testPut() throws Exception {
132 final PutResult result = mock(PutResult.class);
133 when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
134 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
135 Mockito.anyString())).thenReturn(result);
136 when(result.getFutureOfPutData())
137 .thenReturn(Futures.immediateCheckedFuture(null));
138 when(result.getStatus()).thenReturn(Status.OK);
139 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
140 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
141 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
142 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
143 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
144 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
145 this.service.put(uriPath, payload, uriInfo);
147 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
148 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
149 verify(brokerFacade).commitConfigurationDataPut(notNull(SchemaContext.class), capturedPath.capture(),
150 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
152 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
153 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
155 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
156 capturedNode.getValue() instanceof MapEntryNode);
157 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
158 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
159 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
160 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
161 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
162 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
165 @SuppressWarnings("rawtypes")
167 public void testPutBehindMountPoint() throws Exception {
168 final DOMMountPoint mockMountPoint = setupTestMountPoint();
169 final PutResult result = mock(PutResult.class);
170 when(brokerFacade.commitMountPointDataPut(notNull(DOMMountPoint.class),
171 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
172 Mockito.anyString())).thenReturn(result);
173 when(result.getFutureOfPutData()).thenReturn(Futures.immediateCheckedFuture(null));
174 when(result.getStatus()).thenReturn(Status.OK);
175 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
176 final String payload = loadData("/full-versions/testCont1Data.json");
178 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
179 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
180 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
181 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
182 this.service.put(uriPath, payload, uriInfo);
184 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = 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 public void testPutFailure() throws Throwable {
200 final PutResult result = mock(PutResult.class);
202 when(result.getFutureOfPutData())
203 .thenReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")));
204 when(result.getStatus()).thenReturn(Status.OK);
205 when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
206 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
207 Mockito.anyString())).thenReturn(result);
209 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
210 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
212 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
213 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
214 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
215 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
216 this.service.put(uriPath, payload, uriInfo);
219 @SuppressWarnings("rawtypes")
221 public void testPost() throws Exception {
222 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
223 any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
224 Mockito.anyString(), Mockito.anyString());
226 final String uriPath = null;
227 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
229 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
230 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
231 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
232 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
233 final UriBuilder uriBuilder = UriBuilder.fromPath("");
234 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
235 this.service.post(uriPath, payload, uriInfo);
237 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
238 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
239 verify(brokerFacade).commitConfigurationDataPost(notNull(SchemaContext.class), capturedPath.capture(),
240 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
242 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
244 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
245 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
246 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
248 final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
249 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
250 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
251 final MapNode mapNode = (MapNode)mapChild.get();
253 final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
254 INTERFACE_QNAME, NAME_QNAME, "eth0");
255 final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
256 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
257 final MapEntryNode entryNode = entryChild.get();
258 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
259 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
260 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
261 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
264 @SuppressWarnings("rawtypes")
266 public void testPostBehindMountPoint() throws Exception {
267 final DOMMountPoint mockMountPoint = setupTestMountPoint();
268 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
269 notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class),
270 Mockito.anyString(), Mockito.anyString());
272 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
273 final String payload = loadData("/full-versions/testCont1Data.json");
275 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
276 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
277 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
278 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
279 final UriBuilder uriBuilder = UriBuilder.fromPath("");
280 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
281 this.service.post(uriPath, payload, uriInfo);
283 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
284 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
285 verify(brokerFacade).commitConfigurationDataPost(same(mockMountPoint), capturedPath.capture(),
286 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
288 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
290 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
291 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
292 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
293 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
294 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
297 @Test(expected = TransactionCommitFailedException.class)
298 public void testPostFailure() throws Throwable {
299 doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock"))).when(brokerFacade)
300 .commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
301 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString());
303 final String uriPath = null;
304 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
306 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
307 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
308 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
309 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
310 final UriBuilder uriBuilder = UriBuilder.fromPath("");
311 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
314 this.service.post(uriPath, payload, uriInfo);
315 } catch (final OperationFailedException e) {
316 assertNotNull(e.getCause());
322 public void testDelete() throws Exception {
323 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade)
324 .commitConfigurationDataDelete(notNull(YangInstanceIdentifier.class));
326 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
328 this.service.delete(uriPath);
330 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
331 verify(brokerFacade).commitConfigurationDataDelete(capturedPath.capture());
333 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
334 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
337 @Test(expected=OperationFailedException.class)
338 public void testDeleteFailure() throws Exception {
339 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
341 this.service.delete(invalidUriPath);
345 public void testGetConfig() throws Exception {
346 testGet(LogicalDatastoreType.CONFIGURATION);
350 public void testGetOperational() throws Exception {
351 testGet(LogicalDatastoreType.OPERATIONAL);
355 public void testGetWithNoData() throws OperationFailedException {
356 doReturn(null).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
357 Mockito.anyString());
358 final String uriPath = "ietf-interfaces:interfaces";
359 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
360 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
361 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
362 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
363 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
366 @Test(expected=OperationFailedException.class)
367 public void testGetFailure() throws Exception {
368 final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
369 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
370 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
371 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
372 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
373 this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
376 @SuppressWarnings("rawtypes")
378 public void testInvokeRpcWithInput() throws Exception {
379 final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
381 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
382 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(eq(path),
383 any(NormalizedNode.class));
385 final String uriPath = "toaster:make-toast";
386 final String input = loadData("/full-versions/make-toast-rpc-input.json");
388 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
390 assertEquals("Output present", false, output.isPresent());
392 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
393 verify(brokerFacade).invokeRpc(eq(path), capturedNode.capture());
395 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
396 capturedNode.getValue() instanceof ContainerNode);
397 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
398 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Long.valueOf(10));
399 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
403 public void testInvokeRpcWithNoInput() throws Exception {
404 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
406 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
407 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
408 any(NormalizedNode.class));
410 final String uriPath = "toaster:cancel-toast";
412 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
414 assertEquals("Output present", false, output.isPresent());
416 verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
420 public void testInvokeRpcWithOutput() throws Exception {
421 final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
423 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
424 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
425 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
426 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
427 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
428 any(NormalizedNode.class));
430 final String uriPath = "toaster:testOutput";
432 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
434 assertEquals("Output present", true, output.isPresent());
435 assertNotNull("Returned null response", output.get());
436 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
438 verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
441 @Test(expected=OperationFailedException.class)
442 public void testInvokeRpcFailure() throws Exception {
443 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
444 doReturn(Futures.immediateFailedCheckedFuture(exception)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
445 any(NormalizedNode.class));
447 final String uriPath = "toaster:cancel-toast";
449 this.service.invokeRpc(uriPath, Optional.<String>absent());
452 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
453 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
454 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
455 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
456 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
457 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
460 if(datastoreType == LogicalDatastoreType.CONFIGURATION) {
461 doReturn(entryNode).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
462 Mockito.anyString());
464 doReturn(entryNode).when(brokerFacade).readOperationalData(notNull(YangInstanceIdentifier.class));
467 final String uriPath = "/ietf-interfaces:interfaces/interface/eth0";
468 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
469 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
470 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
471 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
472 Mockito.when(uriInfo.getQueryParameters(false)).thenReturn(value);
473 Mockito.when(value.getFirst("depth")).thenReturn("");
475 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType, uriInfo);
476 assertEquals("Response present", true, optionalResp.isPresent());
477 final String jsonResp = optionalResp.get();
479 assertNotNull("Returned null response", jsonResp);
480 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
481 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
482 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
483 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
485 final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
486 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
487 verify(brokerFacade).readConfigurationData(capturedPath.capture(), Mockito.anyString());
489 verify(brokerFacade).readOperationalData(capturedPath.capture());
492 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
493 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
496 DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
497 final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
498 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
499 doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
501 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
502 doReturn(Optional.of(mockMountPoint)).when(mockMountService).getMountPoint(notNull(YangInstanceIdentifier.class));
504 ControllerContext.getInstance().setMountService(mockMountService);
505 return mockMountPoint;
508 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
509 final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
510 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
511 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
514 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
515 final List<PathArgument> pathArgs = path.getPathArguments();
516 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
518 for(final PathArgument actual: pathArgs) {
520 if(expArgs[i] instanceof Object[]) {
521 final Object[] listEntry = (Object[]) expArgs[i];
522 expNodeType = (QName) listEntry[0];
524 assertTrue(actual instanceof NodeIdentifierWithPredicates);
525 final Map<QName, Object> keyValues = ((NodeIdentifierWithPredicates)actual).getKeyValues();
526 assertEquals(String.format("Path arg %d keyValues size", i + 1), 1, keyValues.size());
527 final QName expKey = (QName) listEntry[1];
528 assertEquals(String.format("Path arg %d keyValue for %s", i + 1, expKey), listEntry[2],
529 keyValues.get(expKey));
531 expNodeType = (QName) expArgs[i];
534 assertEquals(String.format("Path arg %d node type", i + 1), expNodeType, actual.getNodeType());