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.assertThrows;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.ArgumentMatchers.eq;
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.mdsal.dom.spi.FixedDOMSchemaService;
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.restconf.nb.rfc8040.streams.Configuration;
72 import org.opendaylight.yangtools.yang.common.OperationFailedException;
73 import org.opendaylight.yangtools.yang.common.QName;
74 import org.opendaylight.yangtools.yang.common.Uint32;
75 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
76 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
77 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
78 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
79 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
80 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
81 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
82 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
83 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
84 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
85 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
86 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
87 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
90 * Unit tests for JSONRestconfServiceDraft18.
92 * @author Thomas Pantelis
94 public class JSONRestconfServiceRfc8040ImplTest {
95 static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
96 static final String IETF_INTERFACES_VERSION = "2013-07-04";
97 static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
98 static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
99 static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
100 static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
101 static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
102 static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
104 static final String TEST_MODULE_NS = "test:module";
105 static final String TEST_MODULE_VERSION = "2014-01-09";
106 static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
107 static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
108 static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
109 static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
111 static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
112 static final String TOASTER_MODULE_VERSION = "2009-11-20";
113 static final QName TOASTER_DONENESS_QNAME =
114 QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
115 static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
116 static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
117 static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
118 static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
119 static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
120 static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
122 private static EffectiveModelContext schemaContext;
125 private DOMTransactionChain mockTxChain;
128 private DOMDataTreeReadWriteTransaction mockReadWriteTx;
131 private DOMDataTreeReadTransaction mockReadOnlyTx;
134 private DOMDataTreeWriteTransaction mockWriteTx;
137 private DOMMountPointService mockMountPointService;
140 private DOMDataBroker mockDOMDataBroker;
143 private DOMRpcService mockRpcService;
146 private DOMActionService mockActionService;
149 private DOMSchemaService domSchemaService;
152 private Configuration configuration;
154 private JSONRestconfServiceRfc8040Impl service;
156 private final SchemaContextHandler schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
159 public static void init() throws IOException {
160 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
163 @SuppressWarnings("resource")
165 public void setup() throws Exception {
166 MockitoAnnotations.initMocks(this);
168 doReturn(ImmutableClassToInstanceMap.of()).when(domSchemaService).getExtensions();
170 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadOnlyTx).read(
171 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
173 doNothing().when(mockWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
174 any(NormalizedNode.class));
175 doNothing().when(mockWriteTx).merge(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
176 any(NormalizedNode.class));
177 doNothing().when(mockWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
178 doReturn(CommitInfo.emptyFluentFuture()).when(mockWriteTx).commit();
180 doNothing().when(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
181 any(NormalizedNode.class));
182 doReturn(CommitInfo.emptyFluentFuture()).when(mockReadWriteTx).commit();
183 doReturn(immediateFluentFuture(Optional.empty())).when(mockReadWriteTx).read(
184 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
185 doReturn(immediateFalseFluentFuture()).when(mockReadWriteTx).exists(
186 eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
188 doReturn(mockReadOnlyTx).when(mockTxChain).newReadOnlyTransaction();
189 doReturn(mockReadWriteTx).when(mockTxChain).newReadWriteTransaction();
190 doReturn(mockWriteTx).when(mockTxChain).newWriteOnlyTransaction();
192 doReturn(mockTxChain).when(mockDOMDataBroker).createTransactionChain(any());
194 final TransactionChainHandler txChainHandler = new TransactionChainHandler(mockDOMDataBroker);
196 final DOMMountPointServiceHandler mountPointServiceHandler =
197 new DOMMountPointServiceHandler(mockMountPointService);
199 final DOMNotificationService mockNotificationService = mock(DOMNotificationService.class);
200 final ServicesWrapper servicesWrapper = ServicesWrapper.newInstance(schemaContextHandler,
201 mountPointServiceHandler, txChainHandler, new DOMDataBrokerHandler(mockDOMDataBroker),
202 new RpcServiceHandler(mockRpcService), new ActionServiceHandler(mockActionService),
203 new NotificationServiceHandler(mockNotificationService), domSchemaService, configuration);
205 service = new JSONRestconfServiceRfc8040Impl(servicesWrapper, mountPointServiceHandler,
206 schemaContextHandler);
209 private static String loadData(final String path) throws IOException {
210 return Resources.asCharSource(JSONRestconfServiceRfc8040ImplTest.class.getResource(path),
211 StandardCharsets.UTF_8).read();
215 public void testPut() throws Exception {
216 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
217 final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
219 this.service.put(uriPath, payload);
221 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
222 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
223 final ArgumentCaptor<NormalizedNode<?, ?>> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
225 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
226 capturedNode.capture());
228 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
229 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
231 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
232 capturedNode.getValue() instanceof MapEntryNode);
233 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
234 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
235 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
236 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
237 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
238 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
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 public void testPutFailure() throws IOException {
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 assertThrows(OperationFailedException.class, () -> this.service.put(uriPath, payload));
277 public void testPost() throws Exception {
278 final String uriPath = null;
279 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
281 this.service.post(uriPath, payload);
283 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
284 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
285 final ArgumentCaptor<NormalizedNode<?, ?>> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
287 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
288 capturedNode.capture());
290 verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
292 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
293 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
294 assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
296 final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
297 assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
298 assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
299 final MapNode mapNode = (MapNode)mapChild.get();
301 final NodeIdentifierWithPredicates entryNodeID = NodeIdentifierWithPredicates.of(
302 INTERFACE_QNAME, NAME_QNAME, "eth0");
303 final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
304 assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
305 final MapEntryNode entryNode = entryChild.get();
306 verifyLeafNode(entryNode, NAME_QNAME, "eth0");
307 verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
308 verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
309 verifyLeafNode(entryNode, DESC_QNAME, "some interface");
313 public void testPostBehindMountPoint() throws Exception {
314 setupTestMountPoint();
316 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
317 final String payload = loadData("/full-versions/testCont1Data.json");
319 this.service.post(uriPath, payload);
321 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
322 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
323 final ArgumentCaptor<NormalizedNode<?, ?>> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
325 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
326 capturedNode.capture());
328 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
330 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
331 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
332 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
333 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
334 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
338 public void testPostFailure() throws IOException {
339 final Exception failure = new TransactionCommitFailedException("mock");
340 doReturn(immediateFailedFluentFuture(failure)).when(mockReadWriteTx).commit();
342 final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
344 this.service.post(null, payload);
346 } catch (final OperationFailedException e) {
347 final Throwable cause = e.getCause();
348 assertNotNull(cause);
349 assertSame(failure, cause.getCause());
354 public void testPatch() throws Exception {
355 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
356 final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
358 final Optional<String> patchResult = this.service.patch(uriPath, payload);
360 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
361 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
362 final ArgumentCaptor<NormalizedNode<?, ?>> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
364 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
365 capturedNode.capture());
367 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
368 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
370 assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
371 capturedNode.getValue() instanceof MapEntryNode);
372 final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
373 assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
374 verifyLeafNode(actualNode, NAME_QNAME, "eth0");
375 verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
376 verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
377 verifyLeafNode(actualNode, DESC_QNAME, "some interface");
378 assertTrue(patchResult.get().contains("\"ok\":[null]"));
382 public void testPatchBehindMountPoint() throws Exception {
383 setupTestMountPoint();
385 final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
386 final String payload = loadData("/full-versions/testCont1DataPatch.json");
388 final Optional<String> patchResult = this.service.patch(uriPath, payload);
390 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
391 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
392 final ArgumentCaptor<NormalizedNode<?, ?>> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
394 verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
395 capturedNode.capture());
397 verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
399 assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
400 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
401 assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
402 verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
403 verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
404 assertTrue(patchResult.get().contains("\"ok\":[null]"));
408 public void testPatchFailure() throws IOException, OperationFailedException {
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 OperationFailedException {
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 public void testDeleteFailure() {
441 assertThrows(OperationFailedException.class, () -> this.service.delete("ietf-interfaces:interfaces/invalid"));
445 public void testGetConfig() throws OperationFailedException {
446 testGet(LogicalDatastoreType.CONFIGURATION);
450 public void testGetOperational() throws OperationFailedException {
451 testGet(LogicalDatastoreType.OPERATIONAL);
455 public void testGetWithNoData() throws OperationFailedException {
456 final String uriPath = "ietf-interfaces:interfaces";
457 this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
460 public void testGetFailure() {
461 assertThrows(OperationFailedException.class,
462 () -> this.service.get("/ietf-interfaces:interfaces/invalid", LogicalDatastoreType.CONFIGURATION));
466 public void testInvokeRpcWithInput() throws IOException, OperationFailedException {
467 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
468 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(MAKE_TOAST_QNAME),
469 any(NormalizedNode.class));
471 final String uriPath = "toaster:make-toast";
472 final String input = loadData("/full-versions/make-toast-rpc-input.json");
474 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
476 assertEquals("Output present", false, output.isPresent());
478 final ArgumentCaptor<NormalizedNode<?, ?>> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
479 verify(mockRpcService).invokeRpc(eq(MAKE_TOAST_QNAME), capturedNode.capture());
481 assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
482 capturedNode.getValue() instanceof ContainerNode);
483 final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
484 verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Uint32.valueOf(10L));
485 verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
489 public void testInvokeRpcWithNoInput() throws OperationFailedException {
490 final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
491 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(CANCEL_TOAST_QNAME), any());
493 final String uriPath = "toaster:cancel-toast";
495 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
497 assertEquals("Output present", false, output.isPresent());
499 verify(mockRpcService).invokeRpc(eq(CANCEL_TOAST_QNAME), any());
503 public void testInvokeRpcWithOutput() throws OperationFailedException {
504 final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
505 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
506 .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
507 final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
508 doReturn(immediateFluentFuture(expResult)).when(mockRpcService).invokeRpc(eq(TEST_OUTPUT_QNAME), any());
510 final String uriPath = "toaster:testOutput";
512 final Optional<String> output = this.service.invokeRpc(uriPath, Optional.empty());
514 assertEquals("Output present", true, output.isPresent());
515 assertNotNull("Returned null response", output.get());
516 assertThat("Output element is missing namespace", output.get(), containsString("\"toaster:output\""));
517 assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
519 verify(mockRpcService).invokeRpc(eq(TEST_OUTPUT_QNAME), any());
522 public void testInvokeRpcFailure() {
523 final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
524 doReturn(immediateFailedFluentFuture(exception)).when(mockRpcService).invokeRpc(any(QName.class),
525 any(NormalizedNode.class));
527 assertThrows(OperationFailedException.class,
528 () -> this.service.invokeRpc("toaster:cancel-toast", Optional.empty()));
531 void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
532 final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
533 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
534 .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
535 .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
536 .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
539 doReturn(immediateFluentFuture(Optional.of(entryNode))).when(mockReadOnlyTx).read(
540 eq(datastoreType), any(YangInstanceIdentifier.class));
542 final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
544 final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
545 assertEquals("Response present", true, optionalResp.isPresent());
546 final String jsonResp = optionalResp.get();
548 assertNotNull("Returned null response", jsonResp);
549 assertThat("Top level module has incorrect format", jsonResp, containsString("\"ietf-interfaces:interface\""));
550 assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
551 assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
552 assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
553 assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
555 final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
556 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
557 verify(mockReadOnlyTx).read(eq(datastoreType), capturedPath.capture());
559 verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
560 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
563 DOMMountPoint setupTestMountPoint() throws FileNotFoundException {
564 final EffectiveModelContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
565 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
566 doReturn(Optional.of(FixedDOMSchemaService.of(schemaContextTestModule))).when(mockMountPoint)
567 .getService(DOMSchemaService.class);
569 doReturn(Optional.of(mockDOMDataBroker)).when(mockMountPoint).getService(DOMDataBroker.class);
571 doReturn(Optional.of(mockMountPoint)).when(mockMountPointService).getMountPoint(notNull());
573 return mockMountPoint;
576 void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
577 final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
578 assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
579 assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
582 void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
583 final List<PathArgument> pathArgs = path.getPathArguments();
584 assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
586 for (final PathArgument actual: pathArgs) {
588 if (expArgs[index] instanceof Object[]) {
589 final Object[] listEntry = (Object[]) expArgs[index];
590 expNodeType = (QName) listEntry[0];
592 assertTrue(actual instanceof NodeIdentifierWithPredicates);
593 final NodeIdentifierWithPredicates nip = (NodeIdentifierWithPredicates)actual;
595 assertEquals(String.format("Path arg %d keyValues size", index + 1), 1, nip.size());
596 final QName expKey = (QName) listEntry[1];
597 assertEquals(String.format("Path arg %d keyValue for %s", index + 1, expKey), listEntry[2],
598 nip.getValue(expKey));
600 expNodeType = (QName) expArgs[index];
603 assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());