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.DOMDataBroker;
47 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
48 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
49 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
50 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
51 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
52 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
53 import org.opendaylight.mdsal.dom.api.DOMRpcException;
54 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
55 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
56 import org.opendaylight.mdsal.dom.api.DOMRpcService;
57 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
58 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
59 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
60 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
61 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
62 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
63 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
64 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
65 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
66 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
67 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServicesWrapper;
68 import org.opendaylight.yangtools.yang.common.OperationFailedException;
69 import org.opendaylight.yangtools.yang.common.QName;
70 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
71 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
72 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
73 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
74 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
75 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
76 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
78 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
79 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
80 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
81 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
82 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
83 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
84 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
87 * Unit tests for JSONRestconfServiceDraft18.
89 * @author Thomas Pantelis
91 public class JSONRestconfServiceRfc8040ImplTest {
92 static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
93 static final String IETF_INTERFACES_VERSION = "2013-07-04";
94 static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
95 static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
96 static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
97 static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
98 static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
99 static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
101 static final String TEST_MODULE_NS = "test:module";
102 static final String TEST_MODULE_VERSION = "2014-01-09";
103 static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
104 static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
105 static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
106 static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
108 static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
109 static final String TOASTER_MODULE_VERSION = "2009-11-20";
110 static final QName TOASTER_DONENESS_QNAME =
111 QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
112 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
113 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
114 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
115 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
116 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
117 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
119 private static SchemaContext schemaContext;
122 private DOMTransactionChain mockTxChain;
125 private DOMDataTreeReadWriteTransaction mockReadWriteTx;
128 private DOMDataTreeReadTransaction mockReadOnlyTx;
131 private DOMDataTreeWriteTransaction mockWriteTx;
134 private DOMMountPointService mockMountPointService;
137 private DOMDataBroker mockDOMDataBroker;
140 private DOMRpcService mockRpcService;
143 private DOMSchemaService domSchemaService;
145 private JSONRestconfServiceRfc8040Impl service;
147 private final SchemaContextHandler schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
150 public static void init() throws IOException, ReactorException {
151 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
154 @SuppressWarnings("resource")
156 public void setup() throws Exception {
157 MockitoAnnotations.initMocks(this);
159 doReturn(ImmutableClassToInstanceMap.of()).when(domSchemaService).getExtensions();
161 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadOnlyTx).read(
162 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
164 doNothing().when(mockWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
165 any(NormalizedNode.class));
166 doNothing().when(mockWriteTx).merge(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
167 any(NormalizedNode.class));
168 doNothing().when(mockWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
169 doReturn(CommitInfo.emptyFluentFuture()).when(mockWriteTx).commit();
171 doNothing().when(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
172 any(NormalizedNode.class));
173 doReturn(CommitInfo.emptyFluentFuture()).when(mockReadWriteTx).commit();
174 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadWriteTx).read(
175 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
176 doReturn(immediateFalseFluentFuture()).when(mockReadWriteTx).exists(
177 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
179 doReturn(mockReadOnlyTx).when(mockTxChain).newReadOnlyTransaction();
180 doReturn(mockReadWriteTx).when(mockTxChain).newReadWriteTransaction();
181 doReturn(mockWriteTx).when(mockTxChain).newWriteOnlyTransaction();
183 doReturn(mockTxChain).when(mockDOMDataBroker).createTransactionChain(any());
185 final TransactionChainHandler txChainHandler = new TransactionChainHandler(mockDOMDataBroker);
187 final DOMMountPointServiceHandler mountPointServiceHandler =
188 DOMMountPointServiceHandler.newInstance(mockMountPointService);
190 final DOMNotificationService mockNotificationService = mock(DOMNotificationService.class);
191 final ServicesWrapper servicesWrapper = ServicesWrapper.newInstance(schemaContextHandler,
192 mountPointServiceHandler, txChainHandler, new DOMDataBrokerHandler(mockDOMDataBroker),
193 new RpcServiceHandler(mockRpcService), new NotificationServiceHandler(mockNotificationService),
196 service = new JSONRestconfServiceRfc8040Impl(servicesWrapper, mountPointServiceHandler,
197 schemaContextHandler);
200 private static String loadData(final String path) throws IOException {
201 return Resources.asCharSource(JSONRestconfServiceRfc8040ImplTest.class.getResource(path),
202 StandardCharsets.UTF_8).read();
205 @SuppressWarnings("rawtypes")
207 public void testPut() throws Exception {
208 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
209 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
211 this.service.put(uriPath, payload);
213 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
214 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
215 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
217 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
218 capturedNode.capture());
220 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
221 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
223 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
224 capturedNode.getValue() instanceof MapEntryNode);
225 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
226 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
227 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
228 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
229 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
230 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
233 @SuppressWarnings("rawtypes")
235 public void testPutBehindMountPoint() throws Exception {
236 setupTestMountPoint();
238 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
239 final String payload = loadData("/full-versions/testCont1Data.json");
241 this.service.put(uriPath, payload);
243 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
244 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
245 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
247 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
248 capturedNode.capture());
250 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
252 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
253 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
254 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
255 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
256 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
259 @Test(expected = OperationFailedException.class)
260 @SuppressWarnings("checkstyle:IllegalThrows")
261 public void testPutFailure() throws Throwable {
262 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
263 .when(mockReadWriteTx).commit();
265 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
266 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
268 this.service.put(uriPath, payload);
271 @SuppressWarnings("rawtypes")
273 public void testPost() throws Exception {
274 final String uriPath = null;
275 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
277 this.service.post(uriPath, payload);
279 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
280 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
281 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
283 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
284 capturedNode.capture());
286 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
288 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
289 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
290 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
292 final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
293 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
294 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
295 final MapNode mapNode = (MapNode)mapChild.get();
297 final NodeIdentifierWithPredicates entryNodeID = NodeIdentifierWithPredicates.of(
298 INTERFACE_QNAME, NAME_QNAME, "eth0");
299 final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
300 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
301 final MapEntryNode entryNode = entryChild.get();
302 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
303 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
304 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
305 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
308 @SuppressWarnings("rawtypes")
310 public void testPostBehindMountPoint() throws Exception {
311 setupTestMountPoint();
313 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
314 final String payload = loadData("/full-versions/testCont1Data.json");
316 this.service.post(uriPath, payload);
318 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
319 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
320 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
322 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
323 capturedNode.capture());
325 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
327 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
328 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
329 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
330 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
331 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
335 public void testPostFailure() throws IOException {
336 final Exception failure = new TransactionCommitFailedException("mock");
337 doReturn(immediateFailedFluentFuture(failure)).when(mockReadWriteTx).commit();
339 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
341 this.service.post(null, payload);
343 } catch (final OperationFailedException e) {
344 final Throwable cause = e.getCause();
345 assertNotNull(cause);
346 assertSame(failure, cause.getCause());
350 @SuppressWarnings("rawtypes")
352 public void testPatch() throws Exception {
353 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
354 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
356 final Optional<String> patchResult = this.service.patch(uriPath, payload);
358 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
359 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
360 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
362 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
363 capturedNode.capture());
365 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
366 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
368 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
369 capturedNode.getValue() instanceof MapEntryNode);
370 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
371 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
372 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
373 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
374 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
375 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
376 assertTrue(patchResult.get().contains("\"ok\":[null]"));
379 @SuppressWarnings("rawtypes")
381 public void testPatchBehindMountPoint() throws Exception {
382 setupTestMountPoint();
384 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
385 final String payload = loadData("/full-versions/testCont1DataPatch.json");
387 final Optional<String> patchResult = this.service.patch(uriPath, payload);
389 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
390 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
391 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
393 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
394 capturedNode.capture());
396 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
398 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
399 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
400 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
401 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
402 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
403 assertTrue(patchResult.get().contains("\"ok\":[null]"));
407 @SuppressWarnings("checkstyle:IllegalThrows")
408 public void testPatchFailure() throws Throwable {
409 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
410 .when(mockReadWriteTx).commit();
412 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
414 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
416 final Optional<String> patchResult = this.service.patch(uriPath, payload);
417 assertTrue("Patch output is not null", patchResult.isPresent());
418 String patch = patchResult.get();
419 assertTrue(patch.contains("mock"));
423 public void testDelete() throws Exception {
424 doReturn(immediateTrueFluentFuture()).when(mockReadWriteTx).exists(
425 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
427 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
429 this.service.delete(uriPath);
431 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
432 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
434 verify(mockReadWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture());
436 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
437 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
440 @Test(expected = OperationFailedException.class)
441 public void testDeleteFailure() throws Exception {
442 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
444 this.service.delete(invalidUriPath);
448 public void testGetConfig() throws Exception {
449 testGet(LogicalDatastoreType.CONFIGURATION);
453 public void testGetOperational() throws Exception {
454 testGet(LogicalDatastoreType.OPERATIONAL);
458 public void testGetWithNoData() throws OperationFailedException {
459 final String uriPath = "ietf-interfaces:interfaces";
460 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
463 @Test(expected = OperationFailedException.class)
464 public void testGetFailure() throws Exception {
465 final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
466 this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
469 @SuppressWarnings("rawtypes")
471 public void testInvokeRpcWithInput() throws Exception {
472 final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
474 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
475 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), any(NormalizedNode.class));
477 final String uriPath = "toaster:make-toast";
478 final String input = loadData("/full-versions/make-toast-rpc-input.json");
480 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
482 assertEquals("Output present", false, output.isPresent());
484 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
485 verify(mockRpcService).invokeRpc(eq(path), capturedNode.capture());
487 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
488 capturedNode.getValue() instanceof ContainerNode);
489 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
490 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, 10L);
491 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
495 public void testInvokeRpcWithNoInput() throws Exception {
496 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
498 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
499 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
501 final String uriPath = "toaster:cancel-toast";
503 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
505 assertEquals("Output present", false, output.isPresent());
507 verify(mockRpcService).invokeRpc(eq(path), isNull());
511 public void testInvokeRpcWithOutput() throws Exception {
512 final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
514 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
515 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
516 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
517 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
518 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
520 final String uriPath = "toaster:testOutput";
522 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
524 assertEquals("Output present", true, output.isPresent());
525 assertNotNull("Returned null response", output.get());
526 assertThat("Output element is missing namespace", output.get(), containsString("\"toaster:output\""));
527 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
529 verify(mockRpcService).invokeRpc(eq(path), isNull());
532 @Test(expected = OperationFailedException.class)
533 public void testInvokeRpcFailure() throws Exception {
534 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
535 doReturn(immediateFailedFluentFuture(exception)).when(mockRpcService).invokeRpc(any(SchemaPath.class),
536 any(NormalizedNode.class));
538 final String uriPath = "toaster:cancel-toast";
540 this.service.invokeRpc(uriPath, Optional.empty());
543 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
544 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
545 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
546 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
547 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
548 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
551 doReturn(immediateFluentFuture(Optional.of(entryNode))).when(mockReadOnlyTx).read(
552 eq(datastoreType), any(YangInstanceIdentifier.class));
554 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
556 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
557 assertEquals("Response present", true, optionalResp.isPresent());
558 final String jsonResp = optionalResp.get();
560 assertNotNull("Returned null response", jsonResp);
561 assertThat("Top level module has incorrect format", jsonResp, containsString("\"ietf-interfaces:interface\""));
562 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
563 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
564 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
565 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
566 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
568 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
569 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
570 verify(mockReadOnlyTx).read(eq(datastoreType), capturedPath.capture());
572 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
573 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
576 DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
577 final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
578 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
579 doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
581 doReturn(Optional.of(mockDOMDataBroker)).when(mockMountPoint).getService(DOMDataBroker.class);
583 doReturn(Optional.of(mockMountPoint))
584 .when(mockMountPointService).getMountPoint(notNull(YangInstanceIdentifier.class));
586 return mockMountPoint;
589 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
590 final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
591 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
592 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
595 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
596 final List<PathArgument> pathArgs = path.getPathArguments();
597 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
599 for (final PathArgument actual: pathArgs) {
601 if (expArgs[index] instanceof Object[]) {
602 final Object[] listEntry = (Object[]) expArgs[index];
603 expNodeType = (QName) listEntry[0];
605 assertTrue(actual instanceof NodeIdentifierWithPredicates);
606 final NodeIdentifierWithPredicates nip = (NodeIdentifierWithPredicates)actual;
608 assertEquals(String.format("Path arg %d keyValues size", index + 1), 1, nip.size());
609 final QName expKey = (QName) listEntry[1];
610 assertEquals(String.format("Path arg %d keyValue for %s", index + 1, expKey), listEntry[2],
611 nip.getValue(expKey));
613 expNodeType = (QName) expArgs[index];
616 assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());