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.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Matchers.isNull;
18 import static org.mockito.Matchers.notNull;
19 import static org.mockito.Mockito.doNothing;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
24 import com.google.common.base.Optional;
25 import com.google.common.io.Resources;
26 import com.google.common.util.concurrent.Futures;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30 import java.util.List;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
39 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
40 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
41 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
42 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
43 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
44 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
45 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
46 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
47 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
48 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
49 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
50 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
51 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
52 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
53 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
54 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
55 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
56 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
57 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
58 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
59 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
60 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
61 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
62 import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServicesWrapperImpl;
63 import org.opendaylight.yangtools.yang.common.OperationFailedException;
64 import org.opendaylight.yangtools.yang.common.QName;
65 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
66 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
67 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
68 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
69 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
70 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
71 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
72 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
73 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
74 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
75 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
76 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
77 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
78 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
79 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
82 * Unit tests for JSONRestconfServiceDraft18.
84 * @author Thomas Pantelis
86 public class JSONRestconfServiceRfc8040ImplTest {
87 static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
88 static final String IETF_INTERFACES_VERSION = "2013-07-04";
89 static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
90 static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
91 static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
92 static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
93 static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
94 static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
96 static final String TEST_MODULE_NS = "test:module";
97 static final String TEST_MODULE_VERSION = "2014-01-09";
98 static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
99 static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
100 static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
101 static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
103 static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
104 static final String TOASTER_MODULE_VERSION = "2009-11-20";
105 static final QName TOASTER_DONENESS_QNAME =
106 QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
107 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
108 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
109 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
110 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
111 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
112 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
114 private static SchemaContext schemaContext;
117 private DOMTransactionChain mockTxChain;
120 private DOMDataReadWriteTransaction mockReadWriteTx;
123 private DOMDataReadOnlyTransaction mockReadOnlyTx;
126 private DOMDataWriteTransaction mockWriteTx;
129 private DOMMountPointService mockMountPointService;
132 private DOMDataBroker mockDOMDataBroker;
135 private DOMRpcService mockRpcService;
138 private DOMSchemaService domSchemaService;
140 private JSONRestconfServiceRfc8040Impl service;
142 private final SchemaContextHandler schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
145 public static void init() throws IOException, ReactorException {
146 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
149 @SuppressWarnings("resource")
151 public void setup() throws Exception {
152 MockitoAnnotations.initMocks(this);
154 doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(mockReadOnlyTx).read(
155 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
157 doNothing().when(mockWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
158 any(NormalizedNode.class));
159 doNothing().when(mockWriteTx).merge(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
160 any(NormalizedNode.class));
161 doNothing().when(mockWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
162 doReturn(Futures.immediateCheckedFuture(null)).when(mockWriteTx).submit();
164 doNothing().when(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
165 any(NormalizedNode.class));
166 doReturn(Futures.immediateCheckedFuture(null)).when(mockReadWriteTx).submit();
167 doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(mockReadWriteTx).read(
168 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
169 doReturn(Futures.immediateCheckedFuture(Boolean.FALSE)).when(mockReadWriteTx).exists(
170 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
172 doReturn(mockReadOnlyTx).when(mockTxChain).newReadOnlyTransaction();
173 doReturn(mockReadWriteTx).when(mockTxChain).newReadWriteTransaction();
174 doReturn(mockWriteTx).when(mockTxChain).newWriteOnlyTransaction();
176 doReturn(mockTxChain).when(mockDOMDataBroker).createTransactionChain(any());
178 final TransactionChainHandler txChainHandler = new TransactionChainHandler(mockDOMDataBroker);
180 final DOMMountPointServiceHandler mountPointServiceHandler =
181 new DOMMountPointServiceHandler(mockMountPointService);
183 final DOMNotificationService mockNotificationService = mock(DOMNotificationService.class);
184 ServicesWrapperImpl.getInstance().setHandlers(schemaContextHandler, mountPointServiceHandler,
185 txChainHandler, new DOMDataBrokerHandler(mockDOMDataBroker),
186 new RpcServiceHandler(mockRpcService),
187 new NotificationServiceHandler(mockNotificationService), domSchemaService);
189 service = new JSONRestconfServiceRfc8040Impl(ServicesWrapperImpl.getInstance(), mountPointServiceHandler,
190 schemaContextHandler);
192 new RestConnectorProvider<>(mockDOMDataBroker, domSchemaService, mockRpcService, mockNotificationService,
193 mockMountPointService, txChainHandler, schemaContextHandler, null).start();
196 private static String loadData(final String path) throws IOException {
197 return Resources.asCharSource(JSONRestconfServiceRfc8040ImplTest.class.getResource(path),
198 StandardCharsets.UTF_8).read();
201 @SuppressWarnings("rawtypes")
203 public void testPut() throws Exception {
204 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
205 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
207 this.service.put(uriPath, payload);
209 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
210 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
211 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
213 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
214 capturedNode.capture());
216 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
217 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
219 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
220 capturedNode.getValue() instanceof MapEntryNode);
221 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
222 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
223 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
224 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
225 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
226 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
229 @SuppressWarnings("rawtypes")
231 public void testPutBehindMountPoint() throws Exception {
232 setupTestMountPoint();
234 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
235 final String payload = loadData("/full-versions/testCont1Data.json");
237 this.service.put(uriPath, payload);
239 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
240 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
241 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
243 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
244 capturedNode.capture());
246 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
248 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
249 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
250 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
251 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
252 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
255 @Test(expected = OperationFailedException.class)
256 @SuppressWarnings("checkstyle:IllegalThrows")
257 public void testPutFailure() throws Throwable {
258 doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")))
259 .when(mockReadWriteTx).submit();
261 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
262 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
264 this.service.put(uriPath, payload);
267 @SuppressWarnings("rawtypes")
269 public void testPost() throws Exception {
270 final String uriPath = null;
271 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
273 this.service.post(uriPath, payload);
275 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
276 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
277 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
279 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
280 capturedNode.capture());
282 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
284 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
285 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
286 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
288 final java.util.Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(
289 new NodeIdentifier(INTERFACE_QNAME));
290 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
291 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
292 final MapNode mapNode = (MapNode)mapChild.get();
294 final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
295 INTERFACE_QNAME, NAME_QNAME, "eth0");
296 final java.util.Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
297 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
298 final MapEntryNode entryNode = entryChild.get();
299 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
300 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
301 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
302 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
305 @SuppressWarnings("rawtypes")
307 public void testPostBehindMountPoint() throws Exception {
308 setupTestMountPoint();
310 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
311 final String payload = loadData("/full-versions/testCont1Data.json");
313 this.service.post(uriPath, payload);
315 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
316 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
317 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
319 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
320 capturedNode.capture());
322 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
324 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
325 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
326 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
327 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
328 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
331 @Test(expected = TransactionCommitFailedException.class)
332 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
333 public void testPostFailure() throws Throwable {
334 doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")))
335 .when(mockReadWriteTx).submit();
337 final String uriPath = null;
338 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
341 this.service.post(uriPath, payload);
342 } catch (final OperationFailedException e) {
343 assertNotNull(e.getCause());
348 @SuppressWarnings("rawtypes")
350 public void testPatch() throws Exception {
351 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
352 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
354 final Optional<String> patchResult = this.service.patch(uriPath, payload);
356 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
357 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
358 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
360 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
361 capturedNode.capture());
363 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
364 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
366 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
367 capturedNode.getValue() instanceof MapEntryNode);
368 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
369 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
370 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
371 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
372 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
373 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
374 assertTrue(patchResult.get().contains("\"ok\":[null]"));
377 @SuppressWarnings("rawtypes")
379 public void testPatchBehindMountPoint() throws Exception {
380 setupTestMountPoint();
382 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
383 final String payload = loadData("/full-versions/testCont1DataPatch.json");
385 final Optional<String> patchResult = this.service.patch(uriPath, payload);
387 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
388 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
389 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
391 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
392 capturedNode.capture());
394 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
396 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
397 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
398 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
399 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
400 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
401 assertTrue(patchResult.get().contains("\"ok\":[null]"));
405 @SuppressWarnings("checkstyle:IllegalThrows")
406 public void testPatchFailure() throws Throwable {
407 doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")))
408 .when(mockReadWriteTx).submit();
410 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
412 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
414 final Optional<String> patchResult = this.service.patch(uriPath, payload);
415 assertTrue("Patch output is not null", patchResult.isPresent());
416 String patch = patchResult.get();
417 assertTrue(patch.contains("mock"));
421 public void testDelete() throws Exception {
422 doReturn(Futures.immediateCheckedFuture(Boolean.TRUE)).when(mockReadWriteTx).exists(
423 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
425 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
427 this.service.delete(uriPath);
429 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
430 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
432 verify(mockReadWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture());
434 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
435 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
438 @Test(expected = OperationFailedException.class)
439 public void testDeleteFailure() throws Exception {
440 final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
442 this.service.delete(invalidUriPath);
446 public void testGetConfig() throws Exception {
447 testGet(LogicalDatastoreType.CONFIGURATION);
451 public void testGetOperational() throws Exception {
452 testGet(LogicalDatastoreType.OPERATIONAL);
456 public void testGetWithNoData() throws OperationFailedException {
457 final String uriPath = "ietf-interfaces:interfaces";
458 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
461 @Test(expected = OperationFailedException.class)
462 public void testGetFailure() throws Exception {
463 final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
464 this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
467 @SuppressWarnings("rawtypes")
469 public void testInvokeRpcWithInput() throws Exception {
470 final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
472 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
473 doReturn(Futures.immediateCheckedFuture(expResult)).when(mockRpcService).invokeRpc(eq(path),
474 any(NormalizedNode.class));
476 final String uriPath = "toaster:make-toast";
477 final String input = loadData("/full-versions/make-toast-rpc-input.json");
479 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
481 assertEquals("Output present", false, output.isPresent());
483 final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
484 verify(mockRpcService).invokeRpc(eq(path), capturedNode.capture());
486 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
487 capturedNode.getValue() instanceof ContainerNode);
488 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
489 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Long.valueOf(10));
490 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
494 public void testInvokeRpcWithNoInput() throws Exception {
495 final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
497 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
498 doReturn(Futures.immediateCheckedFuture(expResult)).when(mockRpcService).invokeRpc(eq(path),
499 any(NormalizedNode.class));
501 final String uriPath = "toaster:cancel-toast";
503 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
505 assertEquals("Output present", false, output.isPresent());
507 verify(mockRpcService).invokeRpc(eq(path), isNull(NormalizedNode.class));
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(Futures.immediateCheckedFuture(expResult)).when(mockRpcService).invokeRpc(eq(path),
519 any(NormalizedNode.class));
521 final String uriPath = "toaster:testOutput";
523 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
525 assertEquals("Output present", true, output.isPresent());
526 assertNotNull("Returned null response", output.get());
527 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
529 verify(mockRpcService).invokeRpc(eq(path), isNull(NormalizedNode.class));
532 @Test(expected = OperationFailedException.class)
533 public void testInvokeRpcFailure() throws Exception {
534 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
535 doReturn(Futures.immediateFailedCheckedFuture(exception)).when(mockRpcService).invokeRpc(any(SchemaPath.class),
536 any(NormalizedNode.class));
538 final String uriPath = "toaster:cancel-toast";
540 this.service.invokeRpc(uriPath, Optional.<String>absent());
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(Futures.immediateCheckedFuture(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("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
562 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
563 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
564 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
566 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
567 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
568 verify(mockReadOnlyTx).read(eq(datastoreType), capturedPath.capture());
570 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
571 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
574 DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
575 final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
576 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
577 doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
579 doReturn(Optional.of(mockDOMDataBroker)).when(mockMountPoint).getService(DOMDataBroker.class);
581 doReturn(Optional.of(mockMountPoint))
582 .when(mockMountPointService).getMountPoint(notNull(YangInstanceIdentifier.class));
584 return mockMountPoint;
587 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
588 final java.util.Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
589 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
590 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
593 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
594 final List<PathArgument> pathArgs = path.getPathArguments();
595 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
597 for (final PathArgument actual: pathArgs) {
599 if (expArgs[index] instanceof Object[]) {
600 final Object[] listEntry = (Object[]) expArgs[index];
601 expNodeType = (QName) listEntry[0];
603 assertTrue(actual instanceof NodeIdentifierWithPredicates);
604 final Map<QName, Object> keyValues = ((NodeIdentifierWithPredicates)actual).getKeyValues();
605 assertEquals(String.format("Path arg %d keyValues size", index + 1), 1, keyValues.size());
606 final QName expKey = (QName) listEntry[1];
607 assertEquals(String.format("Path arg %d keyValue for %s", index + 1, expKey), listEntry[2],
608 keyValues.get(expKey));
610 expNodeType = (QName) expArgs[index];
613 assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());