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.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertSame;
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.doCallRealMethod;
22 import static org.mockito.Mockito.doNothing;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
27 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
28 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
29 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
31 import com.google.common.collect.ImmutableClassToInstanceMap;
32 import com.google.common.io.Resources;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.nio.charset.StandardCharsets;
36 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.DOMActionService;
48 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
49 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
50 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
51 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
52 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
53 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
54 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
55 import org.opendaylight.mdsal.dom.api.DOMRpcException;
56 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
57 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
58 import org.opendaylight.mdsal.dom.api.DOMRpcService;
59 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
60 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
61 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
62 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
63 import org.opendaylight.restconf.nb.rfc8040.handlers.ActionServiceHandler;
64 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
65 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
66 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
67 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
68 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
69 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
70 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServicesWrapper;
71 import org.opendaylight.yangtools.yang.common.OperationFailedException;
72 import org.opendaylight.yangtools.yang.common.QName;
73 import org.opendaylight.yangtools.yang.common.Uint32;
74 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
75 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
76 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
77 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
78 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
79 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
80 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
81 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
82 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
83 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
84 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
85 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
86 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
87 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
88 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
91 * Unit tests for JSONRestconfServiceDraft18.
93 * @author Thomas Pantelis
95 public class JSONRestconfServiceRfc8040ImplTest {
96 static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
97 static final String IETF_INTERFACES_VERSION = "2013-07-04";
98 static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
99 static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
100 static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
101 static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
102 static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
103 static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
105 static final String TEST_MODULE_NS = "test:module";
106 static final String TEST_MODULE_VERSION = "2014-01-09";
107 static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
108 static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
109 static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
110 static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
112 static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
113 static final String TOASTER_MODULE_VERSION = "2009-11-20";
114 static final QName TOASTER_DONENESS_QNAME =
115 QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
116 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
117 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
118 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
119 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
120 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
121 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
123 private static EffectiveModelContext schemaContext;
126 private DOMTransactionChain mockTxChain;
129 private DOMDataTreeReadWriteTransaction mockReadWriteTx;
132 private DOMDataTreeReadTransaction mockReadOnlyTx;
135 private DOMDataTreeWriteTransaction mockWriteTx;
138 private DOMMountPointService mockMountPointService;
141 private DOMDataBroker mockDOMDataBroker;
144 private DOMRpcService mockRpcService;
147 private DOMActionService mockActionService;
150 private DOMSchemaService domSchemaService;
152 private JSONRestconfServiceRfc8040Impl service;
154 private final SchemaContextHandler schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
157 public static void init() throws IOException {
158 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
161 @SuppressWarnings("resource")
163 public void setup() throws Exception {
164 MockitoAnnotations.initMocks(this);
166 doReturn(ImmutableClassToInstanceMap.of()).when(domSchemaService).getExtensions();
168 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadOnlyTx).read(
169 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
171 doNothing().when(mockWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
172 any(NormalizedNode.class));
173 doNothing().when(mockWriteTx).merge(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
174 any(NormalizedNode.class));
175 doNothing().when(mockWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
176 doReturn(CommitInfo.emptyFluentFuture()).when(mockWriteTx).commit();
178 doNothing().when(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
179 any(NormalizedNode.class));
180 doReturn(CommitInfo.emptyFluentFuture()).when(mockReadWriteTx).commit();
181 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadWriteTx).read(
182 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
183 doReturn(immediateFalseFluentFuture()).when(mockReadWriteTx).exists(
184 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
186 doReturn(mockReadOnlyTx).when(mockTxChain).newReadOnlyTransaction();
187 doReturn(mockReadWriteTx).when(mockTxChain).newReadWriteTransaction();
188 doReturn(mockWriteTx).when(mockTxChain).newWriteOnlyTransaction();
190 doReturn(mockTxChain).when(mockDOMDataBroker).createTransactionChain(any());
192 final TransactionChainHandler txChainHandler = new TransactionChainHandler(mockDOMDataBroker);
194 final DOMMountPointServiceHandler mountPointServiceHandler =
195 new DOMMountPointServiceHandler(mockMountPointService);
197 final DOMNotificationService mockNotificationService = mock(DOMNotificationService.class);
198 final ServicesWrapper servicesWrapper = ServicesWrapper.newInstance(schemaContextHandler,
199 mountPointServiceHandler, txChainHandler, new DOMDataBrokerHandler(mockDOMDataBroker),
200 new RpcServiceHandler(mockRpcService), new ActionServiceHandler(mockActionService),
201 new NotificationServiceHandler(mockNotificationService), domSchemaService);
203 service = new JSONRestconfServiceRfc8040Impl(servicesWrapper, mountPointServiceHandler,
204 schemaContextHandler);
207 private static String loadData(final String path) throws IOException {
208 return Resources.asCharSource(JSONRestconfServiceRfc8040ImplTest.class.getResource(path),
209 StandardCharsets.UTF_8).read();
212 @SuppressWarnings("rawtypes")
214 public void testPut() throws Exception {
215 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
216 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
218 this.service.put(uriPath, payload);
220 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
221 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
222 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
224 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
225 capturedNode.capture());
227 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
228 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
230 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
231 capturedNode.getValue() instanceof MapEntryNode);
232 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
233 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
234 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
235 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
236 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
237 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
240 @SuppressWarnings("rawtypes")
242 public void testPutBehindMountPoint() throws Exception {
243 setupTestMountPoint();
245 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
246 final String payload = loadData("/full-versions/testCont1Data.json");
248 this.service.put(uriPath, payload);
250 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
251 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
252 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
254 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
255 capturedNode.capture());
257 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
259 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
260 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
261 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
262 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
263 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
266 @Test(expected = OperationFailedException.class)
267 @SuppressWarnings("checkstyle:IllegalThrows")
268 public void testPutFailure() throws Throwable {
269 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
270 .when(mockReadWriteTx).commit();
272 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
273 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
275 this.service.put(uriPath, payload);
278 @SuppressWarnings("rawtypes")
280 public void testPost() throws Exception {
281 final String uriPath = null;
282 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
284 this.service.post(uriPath, payload);
286 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
287 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
288 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
290 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
291 capturedNode.capture());
293 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
295 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
296 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
297 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
299 final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
300 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
301 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
302 final MapNode mapNode = (MapNode)mapChild.get();
304 final NodeIdentifierWithPredicates entryNodeID = NodeIdentifierWithPredicates.of(
305 INTERFACE_QNAME, NAME_QNAME, "eth0");
306 final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
307 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
308 final MapEntryNode entryNode = entryChild.get();
309 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
310 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
311 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
312 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
315 @SuppressWarnings("rawtypes")
317 public void testPostBehindMountPoint() throws Exception {
318 setupTestMountPoint();
320 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
321 final String payload = loadData("/full-versions/testCont1Data.json");
323 this.service.post(uriPath, payload);
325 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
326 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
327 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
329 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
330 capturedNode.capture());
332 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
334 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
335 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
336 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
337 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
338 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
342 public void testPostFailure() throws IOException {
343 final Exception failure = new TransactionCommitFailedException("mock");
344 doReturn(immediateFailedFluentFuture(failure)).when(mockReadWriteTx).commit();
346 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
348 this.service.post(null, payload);
350 } catch (final OperationFailedException e) {
351 final Throwable cause = e.getCause();
352 assertNotNull(cause);
353 assertSame(failure, cause.getCause());
357 @SuppressWarnings("rawtypes")
359 public void testPatch() throws Exception {
360 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
361 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
363 final Optional<String> patchResult = this.service.patch(uriPath, payload);
365 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
366 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
367 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
369 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
370 capturedNode.capture());
372 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
373 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
375 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
376 capturedNode.getValue() instanceof MapEntryNode);
377 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
378 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
379 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
380 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
381 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
382 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
383 assertTrue(patchResult.get().contains("\"ok\":[null]"));
386 @SuppressWarnings("rawtypes")
388 public void testPatchBehindMountPoint() throws Exception {
389 setupTestMountPoint();
391 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
392 final String payload = loadData("/full-versions/testCont1DataPatch.json");
394 final Optional<String> patchResult = this.service.patch(uriPath, payload);
396 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
397 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
398 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
400 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
401 capturedNode.capture());
403 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
405 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
406 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
407 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
408 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
409 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
410 assertTrue(patchResult.get().contains("\"ok\":[null]"));
414 @SuppressWarnings("checkstyle:IllegalThrows")
415 public void testPatchFailure() throws Throwable {
416 doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("mock")))
417 .when(mockReadWriteTx).commit();
419 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
421 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
423 final Optional<String> patchResult = this.service.patch(uriPath, payload);
424 assertTrue("Patch output is not null", patchResult.isPresent());
425 String patch = patchResult.get();
426 assertTrue(patch.contains("mock"));
430 public void testDelete() throws Exception {
431 doReturn(immediateTrueFluentFuture()).when(mockReadWriteTx).exists(
432 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
434 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
436 this.service.delete(uriPath);
438 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
439 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
441 verify(mockReadWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture());
443 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
444 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
447 @Test(expected = OperationFailedException.class)
448 public void testDeleteFailure() throws Exception {
449 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
451 this.service.delete(invalidUriPath);
455 public void testGetConfig() throws Exception {
456 testGet(LogicalDatastoreType.CONFIGURATION);
460 public void testGetOperational() throws Exception {
461 testGet(LogicalDatastoreType.OPERATIONAL);
465 public void testGetWithNoData() throws OperationFailedException {
466 final String uriPath = "ietf-interfaces:interfaces";
467 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
470 @Test(expected = OperationFailedException.class)
471 public void testGetFailure() throws Exception {
472 final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
473 this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
476 @SuppressWarnings("rawtypes")
478 public void testInvokeRpcWithInput() throws Exception {
479 final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
481 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
482 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), any(NormalizedNode.class));
484 final String uriPath = "toaster:make-toast";
485 final String input = loadData("/full-versions/make-toast-rpc-input.json");
487 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
489 assertEquals("Output present", false, output.isPresent());
491 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
492 verify(mockRpcService).invokeRpc(eq(path), capturedNode.capture());
494 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
495 capturedNode.getValue() instanceof ContainerNode);
496 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
497 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Uint32.valueOf(10L));
498 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
502 public void testInvokeRpcWithNoInput() throws Exception {
503 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
505 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
506 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
508 final String uriPath = "toaster:cancel-toast";
510 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
512 assertEquals("Output present", false, output.isPresent());
514 verify(mockRpcService).invokeRpc(eq(path), isNull());
518 public void testInvokeRpcWithOutput() throws Exception {
519 final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
521 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
522 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
523 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
524 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
525 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(path), isNull());
527 final String uriPath = "toaster:testOutput";
529 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
531 assertEquals("Output present", true, output.isPresent());
532 assertNotNull("Returned null response", output.get());
533 assertThat("Output element is missing namespace", output.get(), containsString("\"toaster:output\""));
534 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
536 verify(mockRpcService).invokeRpc(eq(path), isNull());
539 @Test(expected = OperationFailedException.class)
540 public void testInvokeRpcFailure() throws Exception {
541 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
542 doReturn(immediateFailedFluentFuture(exception)).when(mockRpcService).invokeRpc(any(SchemaPath.class),
543 any(NormalizedNode.class));
545 final String uriPath = "toaster:cancel-toast";
547 this.service.invokeRpc(uriPath, Optional.empty());
550 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
551 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
552 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
553 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
554 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
555 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
558 doReturn(immediateFluentFuture(Optional.of(entryNode))).when(mockReadOnlyTx).read(
559 eq(datastoreType), any(YangInstanceIdentifier.class));
561 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
563 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
564 assertEquals("Response present", true, optionalResp.isPresent());
565 final String jsonResp = optionalResp.get();
567 assertNotNull("Returned null response", jsonResp);
568 assertThat("Top level module has incorrect format", jsonResp, containsString("\"ietf-interfaces:interface\""));
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 {
583 final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
584 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
585 doReturn(schemaContextTestModule).when(mockMountPoint).getEffectiveModelContext();
586 doCallRealMethod().when(mockMountPoint).getSchemaContext();
588 doReturn(Optional.of(mockDOMDataBroker)).when(mockMountPoint).getService(DOMDataBroker.class);
590 doReturn(Optional.of(mockMountPoint)).when(mockMountPointService).getMountPoint(notNull());
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());