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;
36 import java.util.Optional;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.opendaylight.mdsal.common.api.CommitInfo;
44 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
45 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
46 import org.opendaylight.mdsal.dom.api.DOMActionService;
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.ActionServiceHandler;
63 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
64 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
65 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
66 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
67 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
68 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
69 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServicesWrapper;
70 import org.opendaylight.yangtools.yang.common.OperationFailedException;
71 import org.opendaylight.yangtools.yang.common.QName;
72 import org.opendaylight.yangtools.yang.common.Uint32;
73 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
74 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
75 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
76 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
77 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
78 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
79 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
81 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
82 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
83 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
84 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
85 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
86 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
87 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
90 * Unit tests for JSONRestconfServiceDraft18.
92 * @author Thomas Pantelis
94 public class JSONRestconfServiceRfc8040ImplTest {
95 static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
96 static final String IETF_INTERFACES_VERSION = "2013-07-04";
97 static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
98 static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
99 static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
100 static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
101 static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
102 static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
104 static final String TEST_MODULE_NS = "test:module";
105 static final String TEST_MODULE_VERSION = "2014-01-09";
106 static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
107 static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
108 static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
109 static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
111 static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
112 static final String TOASTER_MODULE_VERSION = "2009-11-20";
113 static final QName TOASTER_DONENESS_QNAME =
114 QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
115 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
116 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
117 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
118 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
119 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
120 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
122 private static SchemaContext schemaContext;
125 private DOMTransactionChain mockTxChain;
128 private DOMDataTreeReadWriteTransaction mockReadWriteTx;
131 private DOMDataTreeReadTransaction mockReadOnlyTx;
134 private DOMDataTreeWriteTransaction mockWriteTx;
137 private DOMMountPointService mockMountPointService;
140 private DOMDataBroker mockDOMDataBroker;
143 private DOMRpcService mockRpcService;
146 private DOMActionService mockActionService;
149 private DOMSchemaService domSchemaService;
151 private JSONRestconfServiceRfc8040Impl service;
153 private final SchemaContextHandler schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
156 public static void init() throws IOException, ReactorException {
157 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
160 @SuppressWarnings("resource")
162 public void setup() throws Exception {
163 MockitoAnnotations.initMocks(this);
165 doReturn(ImmutableClassToInstanceMap.of()).when(domSchemaService).getExtensions();
167 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadOnlyTx).read(
168 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
170 doNothing().when(mockWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
171 any(NormalizedNode.class));
172 doNothing().when(mockWriteTx).merge(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
173 any(NormalizedNode.class));
174 doNothing().when(mockWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
175 doReturn(CommitInfo.emptyFluentFuture()).when(mockWriteTx).commit();
177 doNothing().when(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
178 any(NormalizedNode.class));
179 doReturn(CommitInfo.emptyFluentFuture()).when(mockReadWriteTx).commit();
180 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadWriteTx).read(
181 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
182 doReturn(immediateFalseFluentFuture()).when(mockReadWriteTx).exists(
183 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
185 doReturn(mockReadOnlyTx).when(mockTxChain).newReadOnlyTransaction();
186 doReturn(mockReadWriteTx).when(mockTxChain).newReadWriteTransaction();
187 doReturn(mockWriteTx).when(mockTxChain).newWriteOnlyTransaction();
189 doReturn(mockTxChain).when(mockDOMDataBroker).createTransactionChain(any());
191 final TransactionChainHandler txChainHandler = new TransactionChainHandler(mockDOMDataBroker);
193 final DOMMountPointServiceHandler mountPointServiceHandler =
194 DOMMountPointServiceHandler.newInstance(mockMountPointService);
196 final DOMNotificationService mockNotificationService = mock(DOMNotificationService.class);
197 final ServicesWrapper servicesWrapper = ServicesWrapper.newInstance(schemaContextHandler,
198 mountPointServiceHandler, txChainHandler, new DOMDataBrokerHandler(mockDOMDataBroker),
199 new RpcServiceHandler(mockRpcService), new ActionServiceHandler(mockActionService),
200 new NotificationServiceHandler(mockNotificationService), domSchemaService);
202 service = new JSONRestconfServiceRfc8040Impl(servicesWrapper, mountPointServiceHandler,
203 schemaContextHandler);
206 private static String loadData(final String path) throws IOException {
207 return Resources.asCharSource(JSONRestconfServiceRfc8040ImplTest.class.getResource(path),
208 StandardCharsets.UTF_8).read();
211 @SuppressWarnings("rawtypes")
213 public void testPut() throws Exception {
214 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
215 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
217 this.service.put(uriPath, payload);
219 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
220 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
221 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
223 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
224 capturedNode.capture());
226 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
227 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
229 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
230 capturedNode.getValue() instanceof MapEntryNode);
231 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
232 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
233 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
234 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
235 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
236 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
239 @SuppressWarnings("rawtypes")
241 public void testPutBehindMountPoint() throws Exception {
242 setupTestMountPoint();
244 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
245 final String payload = loadData("/full-versions/testCont1Data.json");
247 this.service.put(uriPath, payload);
249 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
250 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
251 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
253 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
254 capturedNode.capture());
256 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
258 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
259 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
260 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
261 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
262 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
265 @Test(expected = OperationFailedException.class)
266 @SuppressWarnings("checkstyle:IllegalThrows")
267 public void testPutFailure() throws Throwable {
268 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
269 .when(mockReadWriteTx).commit();
271 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
272 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
274 this.service.put(uriPath, payload);
277 @SuppressWarnings("rawtypes")
279 public void testPost() throws Exception {
280 final String uriPath = null;
281 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
283 this.service.post(uriPath, payload);
285 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
286 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
287 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
289 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
290 capturedNode.capture());
292 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
294 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
295 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
296 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
298 final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
299 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
300 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
301 final MapNode mapNode = (MapNode)mapChild.get();
303 final NodeIdentifierWithPredicates entryNodeID = NodeIdentifierWithPredicates.of(
304 INTERFACE_QNAME, NAME_QNAME, "eth0");
305 final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
306 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
307 final MapEntryNode entryNode = entryChild.get();
308 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
309 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
310 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
311 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
314 @SuppressWarnings("rawtypes")
316 public void testPostBehindMountPoint() throws Exception {
317 setupTestMountPoint();
319 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
320 final String payload = loadData("/full-versions/testCont1Data.json");
322 this.service.post(uriPath, payload);
324 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
325 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
326 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
328 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
329 capturedNode.capture());
331 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
333 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
334 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
335 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
336 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
337 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
341 public void testPostFailure() throws IOException {
342 final Exception failure = new TransactionCommitFailedException("mock");
343 doReturn(immediateFailedFluentFuture(failure)).when(mockReadWriteTx).commit();
345 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
347 this.service.post(null, payload);
349 } catch (final OperationFailedException e) {
350 final Throwable cause = e.getCause();
351 assertNotNull(cause);
352 assertSame(failure, cause.getCause());
356 @SuppressWarnings("rawtypes")
358 public void testPatch() throws Exception {
359 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
360 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
362 final Optional<String> patchResult = this.service.patch(uriPath, payload);
364 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
365 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
366 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
368 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
369 capturedNode.capture());
371 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
372 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
374 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
375 capturedNode.getValue() instanceof MapEntryNode);
376 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
377 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
378 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
379 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
380 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
381 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
382 assertTrue(patchResult.get().contains("\"ok\":[null]"));
385 @SuppressWarnings("rawtypes")
387 public void testPatchBehindMountPoint() throws Exception {
388 setupTestMountPoint();
390 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
391 final String payload = loadData("/full-versions/testCont1DataPatch.json");
393 final Optional<String> patchResult = this.service.patch(uriPath, payload);
395 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
396 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
397 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
399 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
400 capturedNode.capture());
402 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
404 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
405 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
406 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
407 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
408 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
409 assertTrue(patchResult.get().contains("\"ok\":[null]"));
413 @SuppressWarnings("checkstyle:IllegalThrows")
414 public void testPatchFailure() throws Throwable {
415 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
416 .when(mockReadWriteTx).commit();
418 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
420 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
422 final Optional<String> patchResult = this.service.patch(uriPath, payload);
423 assertTrue("Patch output is not null", patchResult.isPresent());
424 String patch = patchResult.get();
425 assertTrue(patch.contains("mock"));
429 public void testDelete() throws Exception {
430 doReturn(immediateTrueFluentFuture()).when(mockReadWriteTx).exists(
431 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
433 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
435 this.service.delete(uriPath);
437 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
438 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
440 verify(mockReadWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture());
442 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
443 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
446 @Test(expected = OperationFailedException.class)
447 public void testDeleteFailure() throws Exception {
448 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
450 this.service.delete(invalidUriPath);
454 public void testGetConfig() throws Exception {
455 testGet(LogicalDatastoreType.CONFIGURATION);
459 public void testGetOperational() throws Exception {
460 testGet(LogicalDatastoreType.OPERATIONAL);
464 public void testGetWithNoData() throws OperationFailedException {
465 final String uriPath = "ietf-interfaces:interfaces";
466 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
469 @Test(expected = OperationFailedException.class)
470 public void testGetFailure() throws Exception {
471 final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
472 this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
475 @SuppressWarnings("rawtypes")
477 public void testInvokeRpcWithInput() throws Exception {
478 final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
480 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
481 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), any(NormalizedNode.class));
483 final String uriPath = "toaster:make-toast";
484 final String input = loadData("/full-versions/make-toast-rpc-input.json");
486 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
488 assertEquals("Output present", false, output.isPresent());
490 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
491 verify(mockRpcService).invokeRpc(eq(path), capturedNode.capture());
493 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
494 capturedNode.getValue() instanceof ContainerNode);
495 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
496 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Uint32.valueOf(10L));
497 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
501 public void testInvokeRpcWithNoInput() throws Exception {
502 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
504 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
505 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
507 final String uriPath = "toaster:cancel-toast";
509 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
511 assertEquals("Output present", false, output.isPresent());
513 verify(mockRpcService).invokeRpc(eq(path), isNull());
517 public void testInvokeRpcWithOutput() throws Exception {
518 final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
520 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
521 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
522 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
523 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
524 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
526 final String uriPath = "toaster:testOutput";
528 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
530 assertEquals("Output present", true, output.isPresent());
531 assertNotNull("Returned null response", output.get());
532 assertThat("Output element is missing namespace", output.get(), containsString("\"toaster:output\""));
533 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
535 verify(mockRpcService).invokeRpc(eq(path), isNull());
538 @Test(expected = OperationFailedException.class)
539 public void testInvokeRpcFailure() throws Exception {
540 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
541 doReturn(immediateFailedFluentFuture(exception)).when(mockRpcService).invokeRpc(any(SchemaPath.class),
542 any(NormalizedNode.class));
544 final String uriPath = "toaster:cancel-toast";
546 this.service.invokeRpc(uriPath, Optional.empty());
549 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
550 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
551 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
552 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
553 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
554 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
557 doReturn(immediateFluentFuture(Optional.of(entryNode))).when(mockReadOnlyTx).read(
558 eq(datastoreType), any(YangInstanceIdentifier.class));
560 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
562 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
563 assertEquals("Response present", true, optionalResp.isPresent());
564 final String jsonResp = optionalResp.get();
566 assertNotNull("Returned null response", jsonResp);
567 assertThat("Top level module has incorrect format", jsonResp, containsString("\"ietf-interfaces:interface\""));
568 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
569 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
570 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
571 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
572 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
574 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
575 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
576 verify(mockReadOnlyTx).read(eq(datastoreType), capturedPath.capture());
578 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
579 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
582 DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
583 final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
584 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
585 doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
587 doReturn(Optional.of(mockDOMDataBroker)).when(mockMountPoint).getService(DOMDataBroker.class);
589 doReturn(Optional.of(mockMountPoint))
590 .when(mockMountPointService).getMountPoint(notNull(YangInstanceIdentifier.class));
592 return mockMountPoint;
595 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
596 final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
597 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
598 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
601 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
602 final List<PathArgument> pathArgs = path.getPathArguments();
603 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
605 for (final PathArgument actual: pathArgs) {
607 if (expArgs[index] instanceof Object[]) {
608 final Object[] listEntry = (Object[]) expArgs[index];
609 expNodeType = (QName) listEntry[0];
611 assertTrue(actual instanceof NodeIdentifierWithPredicates);
612 final NodeIdentifierWithPredicates nip = (NodeIdentifierWithPredicates)actual;
614 assertEquals(String.format("Path arg %d keyValues size", index + 1), 1, nip.size());
615 final QName expKey = (QName) listEntry[1];
616 assertEquals(String.format("Path arg %d keyValue for %s", index + 1, expKey), listEntry[2],
617 nip.getValue(expKey));
619 expNodeType = (QName) expArgs[index];
622 assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());