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 =
101 QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
102 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
103 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
104 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
105 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
106 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
107 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
109 private static BrokerFacade brokerFacade;
111 private final JSONRestconfServiceImpl service = new JSONRestconfServiceImpl();
114 public static void init() throws IOException, ReactorException {
115 ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext("/full-versions/yangs"));
116 brokerFacade = mock(BrokerFacade.class);
117 RestconfImpl.getInstance().setBroker(brokerFacade);
118 RestconfImpl.getInstance().setControllerContext(ControllerContext.getInstance());
122 public void setup() {
126 private static String loadData(final String path) throws IOException {
127 return Resources.asCharSource(JSONRestconfServiceImplTest.class.getResource(path),
128 StandardCharsets.UTF_8).read();
131 @SuppressWarnings("rawtypes")
133 public void testPut() throws Exception {
134 final PutResult result = mock(PutResult.class);
135 when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
136 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
137 Mockito.anyString())).thenReturn(result);
138 when(result.getFutureOfPutData())
139 .thenReturn(Futures.immediateCheckedFuture(null));
140 when(result.getStatus()).thenReturn(Status.OK);
141 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
142 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
143 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
144 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
145 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
146 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
147 this.service.put(uriPath, payload, uriInfo);
149 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
150 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
151 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
152 verify(brokerFacade).commitConfigurationDataPut(notNull(SchemaContext.class), capturedPath.capture(),
153 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
155 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
156 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
158 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
159 capturedNode.getValue() instanceof MapEntryNode);
160 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
161 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
162 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
163 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
164 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
165 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
168 @SuppressWarnings("rawtypes")
170 public void testPutBehindMountPoint() throws Exception {
171 final DOMMountPoint mockMountPoint = setupTestMountPoint();
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 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
182 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
183 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
184 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
185 this.service.put(uriPath, payload, uriInfo);
187 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
188 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
189 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
190 verify(brokerFacade).commitMountPointDataPut(same(mockMountPoint), capturedPath.capture(),
191 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
193 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
195 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
196 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
197 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
198 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
199 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
202 @Test(expected = OperationFailedException.class)
203 @SuppressWarnings("checkstyle:IllegalThrows")
204 public void testPutFailure() throws Throwable {
205 final PutResult result = mock(PutResult.class);
207 when(result.getFutureOfPutData())
208 .thenReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")));
209 when(result.getStatus()).thenReturn(Status.OK);
210 when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
211 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
212 Mockito.anyString())).thenReturn(result);
214 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
215 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
217 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
218 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
219 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
220 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
221 this.service.put(uriPath, payload, uriInfo);
224 @SuppressWarnings("rawtypes")
226 public void testPost() throws Exception {
227 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
228 any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
229 Mockito.anyString(), Mockito.anyString());
231 final String uriPath = null;
232 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
234 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
235 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
236 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
237 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
238 final UriBuilder uriBuilder = UriBuilder.fromPath("");
239 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
240 this.service.post(uriPath, payload, uriInfo);
242 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
243 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
244 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
245 verify(brokerFacade).commitConfigurationDataPost(notNull(SchemaContext.class), capturedPath.capture(),
246 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
248 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
250 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
251 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
252 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
254 final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
255 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
256 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
257 final MapNode mapNode = (MapNode)mapChild.get();
259 final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
260 INTERFACE_QNAME, NAME_QNAME, "eth0");
261 final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
262 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
263 final MapEntryNode entryNode = entryChild.get();
264 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
265 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
266 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
267 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
270 @SuppressWarnings("rawtypes")
272 public void testPostBehindMountPoint() throws Exception {
273 final DOMMountPoint mockMountPoint = setupTestMountPoint();
274 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
275 notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class),
276 Mockito.anyString(), Mockito.anyString());
278 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
279 final String payload = loadData("/full-versions/testCont1Data.json");
281 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
282 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
283 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
284 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
285 final UriBuilder uriBuilder = UriBuilder.fromPath("");
286 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
287 this.service.post(uriPath, payload, uriInfo);
289 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
290 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
291 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
292 verify(brokerFacade).commitConfigurationDataPost(same(mockMountPoint), capturedPath.capture(),
293 capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
295 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
297 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
298 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
299 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
300 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
301 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
304 @Test(expected = TransactionCommitFailedException.class)
305 @SuppressWarnings("checkstyle:IllegalThrows")
306 public void testPostFailure() throws Throwable {
307 doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock"))).when(brokerFacade)
308 .commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
309 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString());
311 final String uriPath = null;
312 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
314 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
315 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
316 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
317 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
318 final UriBuilder uriBuilder = UriBuilder.fromPath("");
319 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
322 this.service.post(uriPath, payload, uriInfo);
323 } catch (final OperationFailedException e) {
324 assertNotNull(e.getCause());
330 public void testDelete() throws Exception {
331 doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade)
332 .commitConfigurationDataDelete(notNull(YangInstanceIdentifier.class));
334 final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
336 this.service.delete(uriPath);
338 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
339 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
340 verify(brokerFacade).commitConfigurationDataDelete(capturedPath.capture());
342 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
343 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
346 @Test(expected = OperationFailedException.class)
347 public void testDeleteFailure() throws Exception {
348 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
350 this.service.delete(invalidUriPath);
354 public void testGetConfig() throws Exception {
355 testGet(LogicalDatastoreType.CONFIGURATION);
359 public void testGetOperational() throws Exception {
360 testGet(LogicalDatastoreType.OPERATIONAL);
364 public void testGetWithNoData() throws OperationFailedException {
365 doReturn(null).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
366 Mockito.anyString());
367 final String uriPath = "ietf-interfaces:interfaces";
368 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
369 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
370 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
371 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
372 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
375 @Test(expected = OperationFailedException.class)
376 public void testGetFailure() throws Exception {
377 final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
378 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
379 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
380 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
381 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
382 this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
385 @SuppressWarnings("rawtypes")
387 public void testInvokeRpcWithInput() throws Exception {
388 final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
390 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
391 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(eq(path),
392 any(NormalizedNode.class));
394 final String uriPath = "toaster:make-toast";
395 final String input = loadData("/full-versions/make-toast-rpc-input.json");
397 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
399 assertEquals("Output present", false, output.isPresent());
401 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
402 verify(brokerFacade).invokeRpc(eq(path), capturedNode.capture());
404 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
405 capturedNode.getValue() instanceof ContainerNode);
406 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
407 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Long.valueOf(10));
408 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
412 public void testInvokeRpcWithNoInput() throws Exception {
413 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
415 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
416 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
417 any(NormalizedNode.class));
419 final String uriPath = "toaster:cancel-toast";
421 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
423 assertEquals("Output present", false, output.isPresent());
425 verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
429 public void testInvokeRpcWithOutput() throws Exception {
430 final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
432 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
433 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
434 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
435 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
436 doReturn(Futures.immediateCheckedFuture(expResult)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
437 any(NormalizedNode.class));
439 final String uriPath = "toaster:testOutput";
441 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
443 assertEquals("Output present", true, output.isPresent());
444 assertNotNull("Returned null response", output.get());
445 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
447 verify(brokerFacade).invokeRpc(eq(path), isNull(NormalizedNode.class));
450 @Test(expected = OperationFailedException.class)
451 public void testInvokeRpcFailure() throws Exception {
452 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
453 doReturn(Futures.immediateFailedCheckedFuture(exception)).when(brokerFacade).invokeRpc(any(SchemaPath.class),
454 any(NormalizedNode.class));
456 final String uriPath = "toaster:cancel-toast";
458 this.service.invokeRpc(uriPath, Optional.<String>absent());
461 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
462 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
463 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
464 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
465 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
466 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
469 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
470 doReturn(entryNode).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
471 Mockito.anyString());
473 doReturn(entryNode).when(brokerFacade).readOperationalData(notNull(YangInstanceIdentifier.class));
476 final String uriPath = "/ietf-interfaces:interfaces/interface/eth0";
477 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
478 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
479 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
480 Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
481 Mockito.when(uriInfo.getQueryParameters(false)).thenReturn(value);
482 Mockito.when(value.getFirst("depth")).thenReturn("");
484 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType, uriInfo);
485 assertEquals("Response present", true, optionalResp.isPresent());
486 final String jsonResp = optionalResp.get();
488 assertNotNull("Returned null response", jsonResp);
489 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
490 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
491 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
492 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
494 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
495 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
496 if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
497 verify(brokerFacade).readConfigurationData(capturedPath.capture(), Mockito.anyString());
499 verify(brokerFacade).readOperationalData(capturedPath.capture());
502 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
503 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
506 DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
507 final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
508 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
509 doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
511 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
512 doReturn(Optional.of(mockMountPoint))
513 .when(mockMountService).getMountPoint(notNull(YangInstanceIdentifier.class));
515 ControllerContext.getInstance().setMountService(mockMountService);
516 return mockMountPoint;
519 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
520 final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
521 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
522 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
525 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
526 final List<PathArgument> pathArgs = path.getPathArguments();
527 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
529 for (final PathArgument actual: pathArgs) {
531 if (expArgs[index] instanceof Object[]) {
532 final Object[] listEntry = (Object[]) expArgs[index];
533 expNodeType = (QName) listEntry[0];
535 assertTrue(actual instanceof NodeIdentifierWithPredicates);
536 final Map<QName, Object> keyValues = ((NodeIdentifierWithPredicates)actual).getKeyValues();
537 assertEquals(String.format("Path arg %d keyValues size", index + 1), 1, keyValues.size());
538 final QName expKey = (QName) listEntry[1];
539 assertEquals(String.format("Path arg %d keyValue for %s", index + 1, expKey), listEntry[2],
540 keyValues.get(expKey));
542 expNodeType = (QName) expArgs[index];
545 assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());