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.data.api.YangInstanceIdentifier;
73 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
74 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
75 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
76 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
77 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
78 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
79 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
81 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
82 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
83 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
84 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
85 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
86 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
89 * Unit tests for JSONRestconfServiceDraft18.
91 * @author Thomas Pantelis
93 public class JSONRestconfServiceRfc8040ImplTest {
94 static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
95 static final String IETF_INTERFACES_VERSION = "2013-07-04";
96 static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
97 static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
98 static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
99 static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
100 static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
101 static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
103 static final String TEST_MODULE_NS = "test:module";
104 static final String TEST_MODULE_VERSION = "2014-01-09";
105 static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
106 static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
107 static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
108 static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
110 static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
111 static final String TOASTER_MODULE_VERSION = "2009-11-20";
112 static final QName TOASTER_DONENESS_QNAME =
113 QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
114 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
115 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
116 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
117 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
118 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
119 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
121 private static SchemaContext schemaContext;
124 private DOMTransactionChain mockTxChain;
127 private DOMDataTreeReadWriteTransaction mockReadWriteTx;
130 private DOMDataTreeReadTransaction mockReadOnlyTx;
133 private DOMDataTreeWriteTransaction mockWriteTx;
136 private DOMMountPointService mockMountPointService;
139 private DOMDataBroker mockDOMDataBroker;
142 private DOMRpcService mockRpcService;
145 private DOMActionService mockActionService;
148 private DOMSchemaService domSchemaService;
150 private JSONRestconfServiceRfc8040Impl service;
152 private final SchemaContextHandler schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
155 public static void init() throws IOException, ReactorException {
156 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
159 @SuppressWarnings("resource")
161 public void setup() throws Exception {
162 MockitoAnnotations.initMocks(this);
164 doReturn(ImmutableClassToInstanceMap.of()).when(domSchemaService).getExtensions();
166 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadOnlyTx).read(
167 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
169 doNothing().when(mockWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
170 any(NormalizedNode.class));
171 doNothing().when(mockWriteTx).merge(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
172 any(NormalizedNode.class));
173 doNothing().when(mockWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
174 doReturn(CommitInfo.emptyFluentFuture()).when(mockWriteTx).commit();
176 doNothing().when(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
177 any(NormalizedNode.class));
178 doReturn(CommitInfo.emptyFluentFuture()).when(mockReadWriteTx).commit();
179 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadWriteTx).read(
180 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
181 doReturn(immediateFalseFluentFuture()).when(mockReadWriteTx).exists(
182 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
184 doReturn(mockReadOnlyTx).when(mockTxChain).newReadOnlyTransaction();
185 doReturn(mockReadWriteTx).when(mockTxChain).newReadWriteTransaction();
186 doReturn(mockWriteTx).when(mockTxChain).newWriteOnlyTransaction();
188 doReturn(mockTxChain).when(mockDOMDataBroker).createTransactionChain(any());
190 final TransactionChainHandler txChainHandler = new TransactionChainHandler(mockDOMDataBroker);
192 final DOMMountPointServiceHandler mountPointServiceHandler =
193 DOMMountPointServiceHandler.newInstance(mockMountPointService);
195 final DOMNotificationService mockNotificationService = mock(DOMNotificationService.class);
196 final ServicesWrapper servicesWrapper = ServicesWrapper.newInstance(schemaContextHandler,
197 mountPointServiceHandler, txChainHandler, new DOMDataBrokerHandler(mockDOMDataBroker),
198 new RpcServiceHandler(mockRpcService), new ActionServiceHandler(mockActionService),
199 new NotificationServiceHandler(mockNotificationService), domSchemaService);
201 service = new JSONRestconfServiceRfc8040Impl(servicesWrapper, mountPointServiceHandler,
202 schemaContextHandler);
205 private static String loadData(final String path) throws IOException {
206 return Resources.asCharSource(JSONRestconfServiceRfc8040ImplTest.class.getResource(path),
207 StandardCharsets.UTF_8).read();
210 @SuppressWarnings("rawtypes")
212 public void testPut() throws Exception {
213 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
214 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
216 this.service.put(uriPath, payload);
218 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
219 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
220 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
222 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
223 capturedNode.capture());
225 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
226 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
228 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
229 capturedNode.getValue() instanceof MapEntryNode);
230 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
231 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
232 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
233 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
234 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
235 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
238 @SuppressWarnings("rawtypes")
240 public void testPutBehindMountPoint() throws Exception {
241 setupTestMountPoint();
243 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
244 final String payload = loadData("/full-versions/testCont1Data.json");
246 this.service.put(uriPath, payload);
248 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
249 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
250 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
252 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
253 capturedNode.capture());
255 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
257 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
258 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
259 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
260 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
261 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
264 @Test(expected = OperationFailedException.class)
265 @SuppressWarnings("checkstyle:IllegalThrows")
266 public void testPutFailure() throws Throwable {
267 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
268 .when(mockReadWriteTx).commit();
270 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
271 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
273 this.service.put(uriPath, payload);
276 @SuppressWarnings("rawtypes")
278 public void testPost() throws Exception {
279 final String uriPath = null;
280 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
282 this.service.post(uriPath, payload);
284 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
285 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
286 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
288 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
289 capturedNode.capture());
291 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
293 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
294 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
295 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
297 final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
298 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
299 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
300 final MapNode mapNode = (MapNode)mapChild.get();
302 final NodeIdentifierWithPredicates entryNodeID = NodeIdentifierWithPredicates.of(
303 INTERFACE_QNAME, NAME_QNAME, "eth0");
304 final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
305 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
306 final MapEntryNode entryNode = entryChild.get();
307 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
308 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
309 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
310 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
313 @SuppressWarnings("rawtypes")
315 public void testPostBehindMountPoint() throws Exception {
316 setupTestMountPoint();
318 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
319 final String payload = loadData("/full-versions/testCont1Data.json");
321 this.service.post(uriPath, payload);
323 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
324 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
325 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
327 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
328 capturedNode.capture());
330 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
332 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
333 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
334 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
335 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
336 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
340 public void testPostFailure() throws IOException {
341 final Exception failure = new TransactionCommitFailedException("mock");
342 doReturn(immediateFailedFluentFuture(failure)).when(mockReadWriteTx).commit();
344 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
346 this.service.post(null, payload);
348 } catch (final OperationFailedException e) {
349 final Throwable cause = e.getCause();
350 assertNotNull(cause);
351 assertSame(failure, cause.getCause());
355 @SuppressWarnings("rawtypes")
357 public void testPatch() throws Exception {
358 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
359 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
361 final Optional<String> patchResult = this.service.patch(uriPath, payload);
363 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
364 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
365 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
367 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
368 capturedNode.capture());
370 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
371 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
373 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
374 capturedNode.getValue() instanceof MapEntryNode);
375 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
376 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
377 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
378 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
379 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
380 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
381 assertTrue(patchResult.get().contains("\"ok\":[null]"));
384 @SuppressWarnings("rawtypes")
386 public void testPatchBehindMountPoint() throws Exception {
387 setupTestMountPoint();
389 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
390 final String payload = loadData("/full-versions/testCont1DataPatch.json");
392 final Optional<String> patchResult = this.service.patch(uriPath, payload);
394 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
395 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
396 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
398 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
399 capturedNode.capture());
401 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
403 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
404 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
405 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
406 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
407 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
408 assertTrue(patchResult.get().contains("\"ok\":[null]"));
412 @SuppressWarnings("checkstyle:IllegalThrows")
413 public void testPatchFailure() throws Throwable {
414 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
415 .when(mockReadWriteTx).commit();
417 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
419 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
421 final Optional<String> patchResult = this.service.patch(uriPath, payload);
422 assertTrue("Patch output is not null", patchResult.isPresent());
423 String patch = patchResult.get();
424 assertTrue(patch.contains("mock"));
428 public void testDelete() throws Exception {
429 doReturn(immediateTrueFluentFuture()).when(mockReadWriteTx).exists(
430 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
432 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
434 this.service.delete(uriPath);
436 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
437 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
439 verify(mockReadWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture());
441 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
442 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
445 @Test(expected = OperationFailedException.class)
446 public void testDeleteFailure() throws Exception {
447 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
449 this.service.delete(invalidUriPath);
453 public void testGetConfig() throws Exception {
454 testGet(LogicalDatastoreType.CONFIGURATION);
458 public void testGetOperational() throws Exception {
459 testGet(LogicalDatastoreType.OPERATIONAL);
463 public void testGetWithNoData() throws OperationFailedException {
464 final String uriPath = "ietf-interfaces:interfaces";
465 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
468 @Test(expected = OperationFailedException.class)
469 public void testGetFailure() throws Exception {
470 final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
471 this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
474 @SuppressWarnings("rawtypes")
476 public void testInvokeRpcWithInput() throws Exception {
477 final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
479 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
480 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), any(NormalizedNode.class));
482 final String uriPath = "toaster:make-toast";
483 final String input = loadData("/full-versions/make-toast-rpc-input.json");
485 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
487 assertEquals("Output present", false, output.isPresent());
489 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
490 verify(mockRpcService).invokeRpc(eq(path), capturedNode.capture());
492 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
493 capturedNode.getValue() instanceof ContainerNode);
494 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
495 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, 10L);
496 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
500 public void testInvokeRpcWithNoInput() throws Exception {
501 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
503 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
504 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
506 final String uriPath = "toaster:cancel-toast";
508 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
510 assertEquals("Output present", false, output.isPresent());
512 verify(mockRpcService).invokeRpc(eq(path), isNull());
516 public void testInvokeRpcWithOutput() throws Exception {
517 final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
519 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
520 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
521 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
522 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
523 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
525 final String uriPath = "toaster:testOutput";
527 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
529 assertEquals("Output present", true, output.isPresent());
530 assertNotNull("Returned null response", output.get());
531 assertThat("Output element is missing namespace", output.get(), containsString("\"toaster:output\""));
532 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
534 verify(mockRpcService).invokeRpc(eq(path), isNull());
537 @Test(expected = OperationFailedException.class)
538 public void testInvokeRpcFailure() throws Exception {
539 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
540 doReturn(immediateFailedFluentFuture(exception)).when(mockRpcService).invokeRpc(any(SchemaPath.class),
541 any(NormalizedNode.class));
543 final String uriPath = "toaster:cancel-toast";
545 this.service.invokeRpc(uriPath, Optional.empty());
548 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
549 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
550 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
551 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
552 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
553 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
556 doReturn(immediateFluentFuture(Optional.of(entryNode))).when(mockReadOnlyTx).read(
557 eq(datastoreType), any(YangInstanceIdentifier.class));
559 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
561 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
562 assertEquals("Response present", true, optionalResp.isPresent());
563 final String jsonResp = optionalResp.get();
565 assertNotNull("Returned null response", jsonResp);
566 assertThat("Top level module has incorrect format", jsonResp, containsString("\"ietf-interfaces:interface\""));
567 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
568 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
569 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
570 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
571 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
573 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
574 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
575 verify(mockReadOnlyTx).read(eq(datastoreType), capturedPath.capture());
577 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
578 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
581 DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
582 final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
583 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
584 doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
586 doReturn(Optional.of(mockDOMDataBroker)).when(mockMountPoint).getService(DOMDataBroker.class);
588 doReturn(Optional.of(mockMountPoint))
589 .when(mockMountPointService).getMountPoint(notNull(YangInstanceIdentifier.class));
591 return mockMountPoint;
594 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
595 final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
596 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
597 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
600 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
601 final List<PathArgument> pathArgs = path.getPathArguments();
602 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
604 for (final PathArgument actual: pathArgs) {
606 if (expArgs[index] instanceof Object[]) {
607 final Object[] listEntry = (Object[]) expArgs[index];
608 expNodeType = (QName) listEntry[0];
610 assertTrue(actual instanceof NodeIdentifierWithPredicates);
611 final NodeIdentifierWithPredicates nip = (NodeIdentifierWithPredicates)actual;
613 assertEquals(String.format("Path arg %d keyValues size", index + 1), 1, nip.size());
614 final QName expKey = (QName) listEntry[1];
615 assertEquals(String.format("Path arg %d keyValue for %s", index + 1, expKey), listEntry[2],
616 nip.getValue(expKey));
618 expNodeType = (QName) expArgs[index];
621 assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());