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.restconf.nb.rfc8040.rests.services.impl;
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.assertSame;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.ArgumentMatchers.eq;
19 import static org.mockito.ArgumentMatchers.isNull;
20 import static org.mockito.ArgumentMatchers.notNull;
21 import static org.mockito.Mockito.doNothing;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
26 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
27 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
28 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
30 import com.google.common.collect.ImmutableClassToInstanceMap;
31 import com.google.common.io.Resources;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.nio.charset.StandardCharsets;
35 import java.util.List;
37 import java.util.Optional;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.opendaylight.mdsal.common.api.CommitInfo;
45 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
46 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
47 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
48 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
49 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
50 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
51 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
52 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
53 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
54 import org.opendaylight.mdsal.dom.api.DOMRpcException;
55 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
56 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
57 import org.opendaylight.mdsal.dom.api.DOMRpcService;
58 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
59 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
60 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
61 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
62 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
63 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
64 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
65 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
66 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
67 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
68 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServicesWrapper;
69 import org.opendaylight.yangtools.yang.common.OperationFailedException;
70 import org.opendaylight.yangtools.yang.common.QName;
71 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
72 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
73 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
74 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
75 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
76 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
77 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
78 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
79 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
81 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
82 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
83 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
84 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
85 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
88 * Unit tests for JSONRestconfServiceDraft18.
90 * @author Thomas Pantelis
92 public class JSONRestconfServiceRfc8040ImplTest {
93 static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
94 static final String IETF_INTERFACES_VERSION = "2013-07-04";
95 static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
96 static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
97 static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
98 static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
99 static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
100 static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
102 static final String TEST_MODULE_NS = "test:module";
103 static final String TEST_MODULE_VERSION = "2014-01-09";
104 static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
105 static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
106 static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
107 static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
109 static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
110 static final String TOASTER_MODULE_VERSION = "2009-11-20";
111 static final QName TOASTER_DONENESS_QNAME =
112 QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
113 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
114 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
115 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
116 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
117 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
118 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
120 private static SchemaContext schemaContext;
123 private DOMTransactionChain mockTxChain;
126 private DOMDataTreeReadWriteTransaction mockReadWriteTx;
129 private DOMDataTreeReadTransaction mockReadOnlyTx;
132 private DOMDataTreeWriteTransaction mockWriteTx;
135 private DOMMountPointService mockMountPointService;
138 private DOMDataBroker mockDOMDataBroker;
141 private DOMRpcService mockRpcService;
144 private DOMSchemaService domSchemaService;
146 private JSONRestconfServiceRfc8040Impl service;
148 private final SchemaContextHandler schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
151 public static void init() throws IOException, ReactorException {
152 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
155 @SuppressWarnings("resource")
157 public void setup() throws Exception {
158 MockitoAnnotations.initMocks(this);
160 doReturn(ImmutableClassToInstanceMap.of()).when(domSchemaService).getExtensions();
162 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadOnlyTx).read(
163 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
165 doNothing().when(mockWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
166 any(NormalizedNode.class));
167 doNothing().when(mockWriteTx).merge(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
168 any(NormalizedNode.class));
169 doNothing().when(mockWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
170 doReturn(CommitInfo.emptyFluentFuture()).when(mockWriteTx).commit();
172 doNothing().when(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
173 any(NormalizedNode.class));
174 doReturn(CommitInfo.emptyFluentFuture()).when(mockReadWriteTx).commit();
175 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadWriteTx).read(
176 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
177 doReturn(immediateFalseFluentFuture()).when(mockReadWriteTx).exists(
178 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
180 doReturn(mockReadOnlyTx).when(mockTxChain).newReadOnlyTransaction();
181 doReturn(mockReadWriteTx).when(mockTxChain).newReadWriteTransaction();
182 doReturn(mockWriteTx).when(mockTxChain).newWriteOnlyTransaction();
184 doReturn(mockTxChain).when(mockDOMDataBroker).createTransactionChain(any());
186 final TransactionChainHandler txChainHandler = new TransactionChainHandler(mockDOMDataBroker);
188 final DOMMountPointServiceHandler mountPointServiceHandler =
189 DOMMountPointServiceHandler.newInstance(mockMountPointService);
191 final DOMNotificationService mockNotificationService = mock(DOMNotificationService.class);
192 final ServicesWrapper servicesWrapper = ServicesWrapper.newInstance(schemaContextHandler,
193 mountPointServiceHandler, txChainHandler, new DOMDataBrokerHandler(mockDOMDataBroker),
194 new RpcServiceHandler(mockRpcService), new NotificationServiceHandler(mockNotificationService),
197 service = new JSONRestconfServiceRfc8040Impl(servicesWrapper, mountPointServiceHandler,
198 schemaContextHandler);
201 private static String loadData(final String path) throws IOException {
202 return Resources.asCharSource(JSONRestconfServiceRfc8040ImplTest.class.getResource(path),
203 StandardCharsets.UTF_8).read();
206 @SuppressWarnings("rawtypes")
208 public void testPut() throws Exception {
209 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
210 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
212 this.service.put(uriPath, payload);
214 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
215 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
216 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
218 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
219 capturedNode.capture());
221 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
222 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
224 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
225 capturedNode.getValue() instanceof MapEntryNode);
226 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
227 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
228 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
229 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
230 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
231 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
234 @SuppressWarnings("rawtypes")
236 public void testPutBehindMountPoint() throws Exception {
237 setupTestMountPoint();
239 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
240 final String payload = loadData("/full-versions/testCont1Data.json");
242 this.service.put(uriPath, payload);
244 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
245 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
246 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
248 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
249 capturedNode.capture());
251 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
253 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
254 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
255 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
256 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
257 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
260 @Test(expected = OperationFailedException.class)
261 @SuppressWarnings("checkstyle:IllegalThrows")
262 public void testPutFailure() throws Throwable {
263 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
264 .when(mockReadWriteTx).commit();
266 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
267 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
269 this.service.put(uriPath, payload);
272 @SuppressWarnings("rawtypes")
274 public void testPost() throws Exception {
275 final String uriPath = null;
276 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
278 this.service.post(uriPath, payload);
280 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
281 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
282 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
284 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
285 capturedNode.capture());
287 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
289 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
290 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
291 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
293 final java.util.Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(
294 new NodeIdentifier(INTERFACE_QNAME));
295 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
296 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
297 final MapNode mapNode = (MapNode)mapChild.get();
299 final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
300 INTERFACE_QNAME, NAME_QNAME, "eth0");
301 final java.util.Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
302 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
303 final MapEntryNode entryNode = entryChild.get();
304 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
305 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
306 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
307 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
310 @SuppressWarnings("rawtypes")
312 public void testPostBehindMountPoint() throws Exception {
313 setupTestMountPoint();
315 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
316 final String payload = loadData("/full-versions/testCont1Data.json");
318 this.service.post(uriPath, payload);
320 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
321 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
322 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
324 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
325 capturedNode.capture());
327 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
329 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
330 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
331 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
332 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
333 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
337 public void testPostFailure() throws IOException {
338 final Exception failure = new TransactionCommitFailedException("mock");
339 doReturn(immediateFailedFluentFuture(failure)).when(mockReadWriteTx).commit();
341 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
343 this.service.post(null, payload);
345 } catch (final OperationFailedException e) {
346 final Throwable cause = e.getCause();
347 assertNotNull(cause);
348 assertSame(failure, cause.getCause());
352 @SuppressWarnings("rawtypes")
354 public void testPatch() throws Exception {
355 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
356 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
358 final Optional<String> patchResult = this.service.patch(uriPath, payload);
360 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
361 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
362 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
364 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
365 capturedNode.capture());
367 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
368 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
370 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
371 capturedNode.getValue() instanceof MapEntryNode);
372 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
373 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
374 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
375 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
376 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
377 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
378 assertTrue(patchResult.get().contains("\"ok\":[null]"));
381 @SuppressWarnings("rawtypes")
383 public void testPatchBehindMountPoint() throws Exception {
384 setupTestMountPoint();
386 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
387 final String payload = loadData("/full-versions/testCont1DataPatch.json");
389 final Optional<String> patchResult = this.service.patch(uriPath, payload);
391 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
392 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
393 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
395 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
396 capturedNode.capture());
398 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
400 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
401 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
402 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
403 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
404 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
405 assertTrue(patchResult.get().contains("\"ok\":[null]"));
409 @SuppressWarnings("checkstyle:IllegalThrows")
410 public void testPatchFailure() throws Throwable {
411 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
412 .when(mockReadWriteTx).commit();
414 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
416 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
418 final Optional<String> patchResult = this.service.patch(uriPath, payload);
419 assertTrue("Patch output is not null", patchResult.isPresent());
420 String patch = patchResult.get();
421 assertTrue(patch.contains("mock"));
425 public void testDelete() throws Exception {
426 doReturn(immediateTrueFluentFuture()).when(mockReadWriteTx).exists(
427 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
429 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
431 this.service.delete(uriPath);
433 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
434 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
436 verify(mockReadWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture());
438 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
439 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
442 @Test(expected = OperationFailedException.class)
443 public void testDeleteFailure() throws Exception {
444 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
446 this.service.delete(invalidUriPath);
450 public void testGetConfig() throws Exception {
451 testGet(LogicalDatastoreType.CONFIGURATION);
455 public void testGetOperational() throws Exception {
456 testGet(LogicalDatastoreType.OPERATIONAL);
460 public void testGetWithNoData() throws OperationFailedException {
461 final String uriPath = "ietf-interfaces:interfaces";
462 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
465 @Test(expected = OperationFailedException.class)
466 public void testGetFailure() throws Exception {
467 final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
468 this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
471 @SuppressWarnings("rawtypes")
473 public void testInvokeRpcWithInput() throws Exception {
474 final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
476 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
477 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), any(NormalizedNode.class));
479 final String uriPath = "toaster:make-toast";
480 final String input = loadData("/full-versions/make-toast-rpc-input.json");
482 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
484 assertEquals("Output present", false, output.isPresent());
486 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
487 verify(mockRpcService).invokeRpc(eq(path), capturedNode.capture());
489 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
490 capturedNode.getValue() instanceof ContainerNode);
491 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
492 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, 10L);
493 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
497 public void testInvokeRpcWithNoInput() throws Exception {
498 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
500 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
501 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
503 final String uriPath = "toaster:cancel-toast";
505 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
507 assertEquals("Output present", false, output.isPresent());
509 verify(mockRpcService).invokeRpc(eq(path), isNull());
513 public void testInvokeRpcWithOutput() throws Exception {
514 final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
516 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
517 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
518 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
519 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
520 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
522 final String uriPath = "toaster:testOutput";
524 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
526 assertEquals("Output present", true, output.isPresent());
527 assertNotNull("Returned null response", output.get());
528 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
530 verify(mockRpcService).invokeRpc(eq(path), isNull());
533 @Test(expected = OperationFailedException.class)
534 public void testInvokeRpcFailure() throws Exception {
535 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
536 doReturn(immediateFailedFluentFuture(exception)).when(mockRpcService).invokeRpc(any(SchemaPath.class),
537 any(NormalizedNode.class));
539 final String uriPath = "toaster:cancel-toast";
541 this.service.invokeRpc(uriPath, Optional.empty());
544 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
545 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
546 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
547 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
548 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
549 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
552 doReturn(immediateFluentFuture(Optional.of(entryNode))).when(mockReadOnlyTx).read(
553 eq(datastoreType), any(YangInstanceIdentifier.class));
555 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
557 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
558 assertEquals("Response present", true, optionalResp.isPresent());
559 final String jsonResp = optionalResp.get();
561 assertNotNull("Returned null response", jsonResp);
562 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
563 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
564 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
565 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
567 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
568 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
569 verify(mockReadOnlyTx).read(eq(datastoreType), capturedPath.capture());
571 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
572 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
575 DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
576 final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
577 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
578 doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
580 doReturn(Optional.of(mockDOMDataBroker)).when(mockMountPoint).getService(DOMDataBroker.class);
582 doReturn(Optional.of(mockMountPoint))
583 .when(mockMountPointService).getMountPoint(notNull(YangInstanceIdentifier.class));
585 return mockMountPoint;
588 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
589 final java.util.Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
590 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
591 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
594 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
595 final List<PathArgument> pathArgs = path.getPathArguments();
596 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
598 for (final PathArgument actual: pathArgs) {
600 if (expArgs[index] instanceof Object[]) {
601 final Object[] listEntry = (Object[]) expArgs[index];
602 expNodeType = (QName) listEntry[0];
604 assertTrue(actual instanceof NodeIdentifierWithPredicates);
605 final Map<QName, Object> keyValues = ((NodeIdentifierWithPredicates)actual).getKeyValues();
606 assertEquals(String.format("Path arg %d keyValues size", index + 1), 1, keyValues.size());
607 final QName expKey = (QName) listEntry[1];
608 assertEquals(String.format("Path arg %d keyValue for %s", index + 1, expKey), listEntry[2],
609 keyValues.get(expKey));
611 expNodeType = (QName) expArgs[index];
614 assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());