2 * Copyright (c) 2016 Cisco 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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.opendaylight.restconf.common.patch.PatchEditOperation.CREATE;
19 import static org.opendaylight.restconf.common.patch.PatchEditOperation.DELETE;
20 import static org.opendaylight.restconf.common.patch.PatchEditOperation.REMOVE;
21 import static org.opendaylight.restconf.common.patch.PatchEditOperation.REPLACE;
23 import com.google.common.base.Optional;
24 import com.google.common.util.concurrent.Futures;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.List;
29 import javax.ws.rs.core.MultivaluedHashMap;
30 import javax.ws.rs.core.MultivaluedMap;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.UriBuilder;
33 import javax.ws.rs.core.UriInfo;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.MockitoAnnotations;
39 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
40 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
41 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
42 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
43 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
44 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
45 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
46 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
47 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
48 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
49 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
50 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
51 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
52 import org.opendaylight.restconf.common.patch.PatchContext;
53 import org.opendaylight.restconf.common.patch.PatchEntity;
54 import org.opendaylight.restconf.common.patch.PatchStatusContext;
55 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
56 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
57 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
58 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
59 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
60 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
61 import org.opendaylight.yangtools.yang.common.QName;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
63 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
68 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
69 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
70 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
71 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
72 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
74 public class RestconfDataServiceImplTest {
76 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
78 private ContainerNode buildBaseCont;
79 private ContainerNode buildBaseContConfig;
80 private ContainerNode buildBaseContOperational;
81 private SchemaContextRef contextRef;
82 private YangInstanceIdentifier iidBase;
83 private DataSchemaNode schemaNode;
84 private RestconfDataServiceImpl dataService;
85 private QName baseQName;
86 private QName containerPlayerQname;
87 private QName leafQname;
88 private ContainerNode buildPlayerCont;
89 private ContainerNode buildLibraryCont;
90 private MapNode buildPlaylistList;
91 private TransactionChainHandler transactionChainHandler;
94 private DOMTransactionChain domTransactionChain;
96 private UriInfo uriInfo;
98 private DOMDataReadWriteTransaction readWrite;
100 private DOMDataReadOnlyTransaction read;
102 private DOMDataWriteTransaction write;
104 private DOMMountPointService mountPointService;
106 private DOMMountPoint mountPoint;
108 private DOMDataBroker mountDataBroker;
110 private DOMTransactionChain mountTransactionChain;
112 private RestconfStreamsSubscriptionService delegRestconfSubscrService;
115 public void setUp() throws Exception {
116 MockitoAnnotations.initMocks(this);
118 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
119 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
120 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(value);
122 this.baseQName = QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
123 this.containerPlayerQname = QName.create(this.baseQName, "player");
124 this.leafQname = QName.create(this.baseQName, "gap");
126 final QName containerLibraryQName = QName.create(this.baseQName, "library");
127 final QName listPlaylistQName = QName.create(this.baseQName, "playlist");
129 final LeafNode buildLeaf = Builders.leafBuilder()
130 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.leafQname))
134 this.buildPlayerCont = Builders.containerBuilder()
135 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.containerPlayerQname))
136 .withChild(buildLeaf)
139 this.buildLibraryCont = Builders.containerBuilder()
140 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(containerLibraryQName))
143 this.buildPlaylistList = Builders.mapBuilder()
144 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listPlaylistQName))
147 this.buildBaseCont = Builders.containerBuilder()
148 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
149 .withChild(this.buildPlayerCont)
152 // config contains one child the same as in operational and one additional
153 this.buildBaseContConfig = Builders.containerBuilder()
154 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
155 .withChild(this.buildPlayerCont)
156 .withChild(this.buildLibraryCont)
159 // operational contains one child the same as in config and one additional
160 this.buildBaseContOperational = Builders.containerBuilder()
161 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
162 .withChild(this.buildPlayerCont)
163 .withChild(this.buildPlaylistList)
166 this.iidBase = YangInstanceIdentifier.builder()
167 .node(this.baseQName)
170 this.contextRef = new SchemaContextRef(
171 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)));
172 this.schemaNode = DataSchemaContextTree.from(this.contextRef.get()).getChild(this.iidBase).getDataSchemaNode();
174 doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
175 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
177 doReturn(this.read).when(domTransactionChain).newReadOnlyTransaction();
178 doReturn(this.readWrite).when(domTransactionChain).newReadWriteTransaction();
179 doReturn(this.write).when(domTransactionChain).newWriteOnlyTransaction();
181 DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class);
182 Mockito.doReturn(domTransactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
184 transactionChainHandler = new TransactionChainHandler(mockDataBroker);
186 final SchemaContextHandler schemaContextHandler = SchemaContextHandler.newInstance(transactionChainHandler,
187 Mockito.mock(DOMSchemaService.class));
189 schemaContextHandler.onGlobalContextUpdated(this.contextRef.get());
190 this.dataService = new RestconfDataServiceImpl(schemaContextHandler, this.transactionChainHandler,
191 DOMMountPointServiceHandler.newInstance(mountPointService), this.delegRestconfSubscrService);
192 doReturn(Optional.of(this.mountPoint)).when(this.mountPointService)
193 .getMountPoint(any(YangInstanceIdentifier.class));
194 doReturn(this.contextRef.get()).when(this.mountPoint).getSchemaContext();
195 doReturn(Optional.of(this.mountDataBroker)).when(this.mountPoint).getService(DOMDataBroker.class);
196 doReturn(this.mountTransactionChain).when(this.mountDataBroker)
197 .createTransactionChain(any(TransactionChainListener.class));
198 doReturn(this.read).when(this.mountTransactionChain).newReadOnlyTransaction();
199 doReturn(this.readWrite).when(this.mountTransactionChain).newReadWriteTransaction();
203 public void testReadData() {
204 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
205 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
206 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
207 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
208 .when(this.read).read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
209 final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
210 assertNotNull(response);
211 assertEquals(200, response.getStatus());
212 assertEquals(this.buildBaseCont, ((NormalizedNodeContext) response.getEntity()).getData());
216 * Test read data from mount point when both {@link LogicalDatastoreType#CONFIGURATION} and
217 * {@link LogicalDatastoreType#OPERATIONAL} contains the same data and some additional data to be merged.
220 public void testReadDataMountPoint() {
221 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
222 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
223 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
224 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
225 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
227 final Response response = this.dataService.readData(
228 "example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", this.uriInfo);
230 assertNotNull(response);
231 assertEquals(200, response.getStatus());
233 // response must contain all child nodes from config and operational containers merged in one container
234 final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
235 assertTrue(data instanceof ContainerNode);
236 assertEquals(3, ((ContainerNode) data).getValue().size());
237 assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
238 assertTrue(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
239 assertTrue(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
242 @Test(expected = RestconfDocumentedException.class)
243 public void testReadDataNoData() {
244 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
245 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
246 .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
247 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
248 .when(this.read).read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
249 this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
253 * Read data from config datastore according to content parameter.
256 public void testReadDataConfigTest() {
257 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
258 parameters.put("content", Collections.singletonList("config"));
260 doReturn(parameters).when(this.uriInfo).getQueryParameters();
261 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
262 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
263 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
264 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
266 final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
268 assertNotNull(response);
269 assertEquals(200, response.getStatus());
271 // response must contain only config data
272 final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
274 // config data present
275 assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
276 assertTrue(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
279 assertFalse(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
283 * Read data from operational datastore according to content parameter.
286 public void testReadDataOperationalTest() {
287 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
288 parameters.put("content", Collections.singletonList("nonconfig"));
290 doReturn(parameters).when(this.uriInfo).getQueryParameters();
291 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
292 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
293 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
294 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
296 final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
298 assertNotNull(response);
299 assertEquals(200, response.getStatus());
301 // response must contain only operational data
302 final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
304 // state data present
305 assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
306 assertTrue(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
308 // config data absent
309 assertFalse(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
313 public void testPutData() {
314 final InstanceIdentifierContext<DataSchemaNode> iidContext =
315 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
316 final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
318 doReturn(Futures.immediateCheckedFuture(Boolean.TRUE)).when(this.readWrite)
319 .exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
320 doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
321 final Response response = this.dataService.putData(null, payload, this.uriInfo);
322 assertNotNull(response);
323 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
327 public void testPutDataWithMountPoint() {
328 // final DOMDataBroker dataBroker = Mockito.mock(DOMDataBroker.class);
329 // doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
330 // doReturn(this.transactionChainHandler.get()).when(dataBroker)
331 // .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER);
332 final InstanceIdentifierContext<DataSchemaNode> iidContext =
333 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, mountPoint, this.contextRef.get());
334 final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
336 doReturn(Futures.immediateCheckedFuture(Boolean.TRUE)).when(this.readWrite)
337 .exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
338 doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
339 final Response response = this.dataService.putData(null, payload, this.uriInfo);
340 assertNotNull(response);
341 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
345 public void testPostData() {
346 final QName listQname = QName.create(this.baseQName, "playlist");
347 final QName listKeyQname = QName.create(this.baseQName, "name");
348 final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeWithKey =
349 new YangInstanceIdentifier.NodeIdentifierWithPredicates(listQname, listKeyQname, "name of band");
350 final LeafNode<Object> content = Builders.leafBuilder()
351 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "name")))
352 .withValue("name of band")
354 final LeafNode<Object> content2 = Builders.leafBuilder()
355 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "description")))
356 .withValue("band description")
358 final MapEntryNode mapEntryNode = Builders.mapEntryBuilder()
359 .withNodeIdentifier(nodeWithKey)
363 final MapNode buildList = Builders.mapBuilder()
364 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listQname))
365 .withChild(mapEntryNode)
368 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
369 final InstanceIdentifierContext<? extends SchemaNode> iidContext =
370 new InstanceIdentifierContext<>(this.iidBase, null, null, this.contextRef.get());
371 final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, buildList);
372 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
373 .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
374 final MapNode data = (MapNode) payload.getData();
375 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifier =
376 data.getValue().iterator().next().getIdentifier();
377 final YangInstanceIdentifier node =
378 payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
379 doReturn(Futures.immediateCheckedFuture(false))
380 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
381 doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, payload.getData());
382 doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(this.uriInfo).getBaseUriBuilder();
384 final Response response = this.dataService.postData(null, payload, this.uriInfo);
385 assertEquals(201, response.getStatus());
389 public void testDeleteData() {
390 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
391 doReturn(Futures.immediateCheckedFuture(true))
392 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
393 final Response response = this.dataService.deleteData("example-jukebox:jukebox");
394 assertNotNull(response);
395 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
399 * Test of deleting data on mount point.
402 public void testDeleteDataMountPoint() {
403 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
404 doReturn(Futures.immediateCheckedFuture(true))
405 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
406 final Response response =
407 this.dataService.deleteData("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox");
408 assertNotNull(response);
409 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
413 public void testPatchData() throws Exception {
414 final InstanceIdentifierContext<? extends SchemaNode> iidContext =
415 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
416 final List<PatchEntity> entity = new ArrayList<>();
417 final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
418 .node(this.containerPlayerQname)
419 .node(this.leafQname)
421 entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
422 entity.add(new PatchEntity("replace data", REPLACE, this.iidBase, this.buildBaseCont));
423 entity.add(new PatchEntity("delete data", DELETE, iidleaf));
424 final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
426 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
427 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
428 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
429 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
430 doReturn(Futures.immediateCheckedFuture(false))
431 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
432 doReturn(Futures.immediateCheckedFuture(true))
433 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
434 final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
435 assertTrue(status.isOk());
436 assertEquals(3, status.getEditCollection().size());
437 assertEquals("replace data", status.getEditCollection().get(1).getEditId());
441 public void testPatchDataMountPoint() throws Exception {
442 final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(
443 this.iidBase, this.schemaNode, this.mountPoint, this.contextRef.get());
444 final List<PatchEntity> entity = new ArrayList<>();
445 final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
446 .node(this.containerPlayerQname)
447 .node(this.leafQname)
449 entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
450 entity.add(new PatchEntity("replace data", REPLACE, this.iidBase, this.buildBaseCont));
451 entity.add(new PatchEntity("delete data", DELETE, iidleaf));
452 final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
454 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
455 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
456 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
457 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
458 doReturn(Futures.immediateCheckedFuture(false))
459 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
460 doReturn(Futures.immediateCheckedFuture(true))
461 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
463 final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
464 assertTrue(status.isOk());
465 assertEquals(3, status.getEditCollection().size());
466 assertNull(status.getGlobalErrors());
470 public void testPatchDataDeleteNotExist() throws Exception {
471 final InstanceIdentifierContext<? extends SchemaNode> iidContext =
472 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
473 final List<PatchEntity> entity = new ArrayList<>();
474 final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
475 .node(this.containerPlayerQname)
476 .node(this.leafQname)
478 entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
479 entity.add(new PatchEntity("remove data", REMOVE, iidleaf));
480 entity.add(new PatchEntity("delete data", DELETE, iidleaf));
481 final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
483 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
484 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
485 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
486 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
487 doReturn(Futures.immediateCheckedFuture(false))
488 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
489 doReturn(Futures.immediateCheckedFuture(false))
490 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
491 doReturn(true).when(this.readWrite).cancel();
492 final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
494 assertFalse(status.isOk());
495 assertEquals(3, status.getEditCollection().size());
496 assertTrue(status.getEditCollection().get(0).isOk());
497 assertTrue(status.getEditCollection().get(1).isOk());
498 assertFalse(status.getEditCollection().get(2).isOk());
499 assertFalse(status.getEditCollection().get(2).getEditErrors().isEmpty());
500 final String errorMessage = status.getEditCollection().get(2).getEditErrors().get(0).getErrorMessage();
501 assertEquals("Data does not exist", errorMessage);