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.Matchers.any;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.opendaylight.restconf.common.patch.PatchEditOperation.CREATE;
20 import static org.opendaylight.restconf.common.patch.PatchEditOperation.DELETE;
21 import static org.opendaylight.restconf.common.patch.PatchEditOperation.REMOVE;
22 import static org.opendaylight.restconf.common.patch.PatchEditOperation.REPLACE;
24 import com.google.common.base.Optional;
25 import com.google.common.util.concurrent.CheckedFuture;
26 import com.google.common.util.concurrent.Futures;
27 import java.lang.reflect.Field;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashSet;
31 import java.util.List;
32 import javax.ws.rs.core.MultivaluedHashMap;
33 import javax.ws.rs.core.MultivaluedMap;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.UriBuilder;
36 import javax.ws.rs.core.UriInfo;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.MockitoAnnotations;
42 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
43 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
44 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
45 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
46 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
47 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
48 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
49 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
50 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
51 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
52 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
53 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
54 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
55 import org.opendaylight.restconf.common.patch.PatchContext;
56 import org.opendaylight.restconf.common.patch.PatchEntity;
57 import org.opendaylight.restconf.common.patch.PatchStatusContext;
58 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
59 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
60 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
61 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
62 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
63 import org.opendaylight.restconf.nb.rfc8040.references.SchemaContextRef;
64 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
65 import org.opendaylight.yangtools.yang.common.QName;
66 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
67 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
69 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
70 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
71 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
72 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
73 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
74 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
75 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
76 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
78 public class RestconfDataServiceImplTest {
80 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
82 private ContainerNode buildBaseCont;
83 private ContainerNode buildBaseContConfig;
84 private ContainerNode buildBaseContOperational;
85 private SchemaContextRef contextRef;
86 private YangInstanceIdentifier iidBase;
87 private DataSchemaNode schemaNode;
88 private RestconfDataServiceImpl dataService;
89 private QName baseQName;
90 private QName containerPlayerQname;
91 private QName leafQname;
92 private ContainerNode buildPlayerCont;
93 private ContainerNode buildLibraryCont;
94 private MapNode buildPlaylistList;
97 private TransactionChainHandler transactionChainHandler;
99 private DOMTransactionChain domTransactionChain;
101 private UriInfo uriInfo;
103 private DOMDataReadWriteTransaction readWrite;
105 private DOMDataReadOnlyTransaction read;
107 private DOMDataWriteTransaction write;
109 private DOMMountPointServiceHandler mountPointServiceHandler;
111 private DOMMountPointService mountPointService;
113 private DOMMountPoint mountPoint;
115 private DOMDataBroker mountDataBroker;
117 private DOMTransactionChain transactionChain;
119 private RestconfStreamsSubscriptionService delegRestconfSubscrService;
122 public void setUp() throws Exception {
123 MockitoAnnotations.initMocks(this);
125 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
126 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
127 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(value);
129 this.baseQName = QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
130 this.containerPlayerQname = QName.create(this.baseQName, "player");
131 this.leafQname = QName.create(this.baseQName, "gap");
133 final QName containerLibraryQName = QName.create(this.baseQName, "library");
134 final QName listPlaylistQName = QName.create(this.baseQName, "playlist");
136 final LeafNode buildLeaf = Builders.leafBuilder()
137 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.leafQname))
141 this.buildPlayerCont = Builders.containerBuilder()
142 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.containerPlayerQname))
143 .withChild(buildLeaf)
146 this.buildLibraryCont = Builders.containerBuilder()
147 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(containerLibraryQName))
150 this.buildPlaylistList = Builders.mapBuilder()
151 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listPlaylistQName))
154 this.buildBaseCont = Builders.containerBuilder()
155 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
156 .withChild(this.buildPlayerCont)
159 // config contains one child the same as in operational and one additional
160 this.buildBaseContConfig = Builders.containerBuilder()
161 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
162 .withChild(this.buildPlayerCont)
163 .withChild(this.buildLibraryCont)
166 // operational contains one child the same as in config and one additional
167 this.buildBaseContOperational = Builders.containerBuilder()
168 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
169 .withChild(this.buildPlayerCont)
170 .withChild(this.buildPlaylistList)
173 this.iidBase = YangInstanceIdentifier.builder()
174 .node(this.baseQName)
177 this.contextRef = new SchemaContextRef(
178 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)));
179 this.schemaNode = DataSchemaContextTree.from(this.contextRef.get()).getChild(this.iidBase).getDataSchemaNode();
181 final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
182 final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
183 Mockito.when(txHandler.get()).thenReturn(domTx);
184 final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
185 Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
186 final CheckedFuture<Void, TransactionCommitFailedException> checked = Mockito.mock(CheckedFuture.class);
187 Mockito.when(wTx.submit()).thenReturn(checked);
188 Mockito.when(checked.checkedGet()).thenReturn(null);
189 final SchemaContextHandler schemaContextHandler = new SchemaContextHandler(txHandler);
191 schemaContextHandler.onGlobalContextUpdated(this.contextRef.get());
192 this.dataService = new RestconfDataServiceImpl(schemaContextHandler, this.transactionChainHandler,
193 this.mountPointServiceHandler, this.delegRestconfSubscrService);
194 doReturn(this.domTransactionChain).when(this.transactionChainHandler).get();
195 doReturn(this.read).when(this.domTransactionChain).newReadOnlyTransaction();
196 doReturn(this.readWrite).when(this.domTransactionChain).newReadWriteTransaction();
197 doReturn(this.write).when(this.domTransactionChain).newWriteOnlyTransaction();
198 doReturn(this.mountPointService).when(this.mountPointServiceHandler).get();
199 doReturn(Optional.of(this.mountPoint)).when(this.mountPointService)
200 .getMountPoint(any(YangInstanceIdentifier.class));
201 doReturn(this.contextRef.get()).when(this.mountPoint).getSchemaContext();
202 doReturn(Optional.of(this.mountDataBroker)).when(this.mountPoint).getService(DOMDataBroker.class);
203 doReturn(this.transactionChain).when(this.mountDataBroker)
204 .createTransactionChain(any(TransactionChainListener.class));
205 doReturn(this.read).when(this.transactionChain).newReadOnlyTransaction();
206 doReturn(this.readWrite).when(this.transactionChain).newReadWriteTransaction();
210 public void testReadData() {
211 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
212 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
213 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
214 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
215 .when(this.read).read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
216 final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
217 assertNotNull(response);
218 assertEquals(200, response.getStatus());
219 assertEquals(this.buildBaseCont, ((NormalizedNodeContext) response.getEntity()).getData());
223 * Test read data from mount point when both {@link LogicalDatastoreType#CONFIGURATION} and
224 * {@link LogicalDatastoreType#OPERATIONAL} contains the same data and some additional data to be merged.
227 public void testReadDataMountPoint() {
228 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
229 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
230 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
231 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
232 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
234 final Response response = this.dataService.readData(
235 "example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", this.uriInfo);
237 assertNotNull(response);
238 assertEquals(200, response.getStatus());
240 // response must contain all child nodes from config and operational containers merged in one container
241 final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
242 assertTrue(data instanceof ContainerNode);
243 assertEquals(3, ((ContainerNode) data).getValue().size());
244 assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
245 assertTrue(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
246 assertTrue(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
249 @Test(expected = RestconfDocumentedException.class)
250 public void testReadDataNoData() {
251 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
252 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
253 .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
254 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
255 .when(this.read).read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
256 this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
260 * Read data from config datastore according to content parameter.
263 public void testReadDataConfigTest() {
264 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
265 parameters.put("content", Collections.singletonList("config"));
267 doReturn(parameters).when(this.uriInfo).getQueryParameters();
268 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
269 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
270 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
271 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
273 final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
275 assertNotNull(response);
276 assertEquals(200, response.getStatus());
278 // response must contain only config data
279 final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
281 // config data present
282 assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
283 assertTrue(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
286 assertFalse(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
290 * Read data from operational datastore according to content parameter.
293 public void testReadDataOperationalTest() {
294 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
295 parameters.put("content", Collections.singletonList("nonconfig"));
297 doReturn(parameters).when(this.uriInfo).getQueryParameters();
298 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
299 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
300 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
301 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
303 final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
305 assertNotNull(response);
306 assertEquals(200, response.getStatus());
308 // response must contain only operational data
309 final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
311 // state data present
312 assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
313 assertTrue(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
315 // config data absent
316 assertFalse(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
320 public void testPutData() {
321 final InstanceIdentifierContext<DataSchemaNode> iidContext =
322 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
323 final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
325 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
326 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
327 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
328 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
329 final Response response = this.dataService.putData(null, payload, this.uriInfo);
330 assertNotNull(response);
331 assertEquals(200, response.getStatus());
335 public void testPutDataWithMountPoint() {
336 final DOMDataBroker dataBroker = Mockito.mock(DOMDataBroker.class);
337 doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
338 doReturn(this.transactionChainHandler.get()).when(dataBroker)
339 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER);
340 final InstanceIdentifierContext<DataSchemaNode> iidContext =
341 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, mountPoint, this.contextRef.get());
342 final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
344 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
345 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
346 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
347 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
348 final Response response = this.dataService.putData(null, payload, this.uriInfo);
349 assertNotNull(response);
350 assertEquals(200, response.getStatus());
354 public void testPostData() {
355 final QName listQname = QName.create(this.baseQName, "playlist");
356 final QName listKeyQname = QName.create(this.baseQName, "name");
357 final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeWithKey =
358 new YangInstanceIdentifier.NodeIdentifierWithPredicates(listQname, listKeyQname, "name of band");
359 final LeafNode<Object> content = Builders.leafBuilder()
360 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "name")))
361 .withValue("name of band")
363 final LeafNode<Object> content2 = Builders.leafBuilder()
364 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "description")))
365 .withValue("band description")
367 final MapEntryNode mapEntryNode = Builders.mapEntryBuilder()
368 .withNodeIdentifier(nodeWithKey)
372 final MapNode buildList = Builders.mapBuilder()
373 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listQname))
374 .withChild(mapEntryNode)
377 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
378 final InstanceIdentifierContext<? extends SchemaNode> iidContext =
379 new InstanceIdentifierContext<>(this.iidBase, null, null, this.contextRef.get());
380 final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, buildList);
381 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
382 .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
383 final MapNode data = (MapNode) payload.getData();
384 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifier =
385 data.getValue().iterator().next().getIdentifier();
386 final YangInstanceIdentifier node =
387 payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
388 doReturn(Futures.immediateCheckedFuture(false))
389 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
390 doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, payload.getData());
391 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
392 doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(this.uriInfo).getBaseUriBuilder();
394 final Response response = this.dataService.postData(null, payload, this.uriInfo);
395 assertEquals(201, response.getStatus());
399 public void testDeleteData() {
400 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
401 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
402 doReturn(Futures.immediateCheckedFuture(true))
403 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
404 final Response response = this.dataService.deleteData("example-jukebox:jukebox");
405 assertNotNull(response);
406 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
410 * Test of deleting data on mount point.
413 public void testDeleteDataMountPoint() {
414 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
415 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
416 doReturn(Futures.immediateCheckedFuture(true))
417 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
418 final Response response =
419 this.dataService.deleteData("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox");
420 assertNotNull(response);
421 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
425 public void testPatchData() throws Exception {
426 final InstanceIdentifierContext<? extends SchemaNode> iidContext =
427 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
428 final List<PatchEntity> entity = new ArrayList<>();
429 final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
430 .node(this.containerPlayerQname)
431 .node(this.leafQname)
433 entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
434 entity.add(new PatchEntity("replace data", REPLACE, this.iidBase, this.buildBaseCont));
435 entity.add(new PatchEntity("delete data", DELETE, iidleaf));
436 final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
438 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
439 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
440 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
441 doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
442 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
443 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
444 doReturn(Futures.immediateCheckedFuture(false))
445 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
446 doReturn(Futures.immediateCheckedFuture(true))
447 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
448 final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
449 assertTrue(status.isOk());
450 assertEquals(3, status.getEditCollection().size());
451 assertEquals("replace data", status.getEditCollection().get(1).getEditId());
455 public void testPatchDataMountPoint() throws Exception {
456 final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(
457 this.iidBase, this.schemaNode, this.mountPoint, this.contextRef.get());
458 final List<PatchEntity> entity = new ArrayList<>();
459 final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
460 .node(this.containerPlayerQname)
461 .node(this.leafQname)
463 entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
464 entity.add(new PatchEntity("replace data", REPLACE, this.iidBase, this.buildBaseCont));
465 entity.add(new PatchEntity("delete data", DELETE, iidleaf));
466 final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
468 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
469 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
470 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
471 doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
472 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
473 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
474 doReturn(Futures.immediateCheckedFuture(false))
475 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
476 doReturn(Futures.immediateCheckedFuture(true))
477 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
479 final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
480 assertTrue(status.isOk());
481 assertEquals(3, status.getEditCollection().size());
482 assertNull(status.getGlobalErrors());
486 public void testPatchDataDeleteNotExist() throws Exception {
487 final Field handler = RestConnectorProvider.class.getDeclaredField("transactionChainHandler");
488 final Field broker = RestConnectorProvider.class.getDeclaredField("dataBroker");
490 handler.setAccessible(true);
491 handler.set(RestConnectorProvider.class, mock(TransactionChainHandler.class));
493 broker.setAccessible(true);
494 broker.set(RestConnectorProvider.class, mock(DOMDataBroker.class));
495 final InstanceIdentifierContext<? extends SchemaNode> iidContext =
496 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
497 final List<PatchEntity> entity = new ArrayList<>();
498 final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
499 .node(this.containerPlayerQname)
500 .node(this.leafQname)
502 entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
503 entity.add(new PatchEntity("remove data", REMOVE, iidleaf));
504 entity.add(new PatchEntity("delete data", DELETE, iidleaf));
505 final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
507 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
508 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
509 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
510 doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
511 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
512 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
513 doReturn(Futures.immediateCheckedFuture(false))
514 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
515 doReturn(Futures.immediateCheckedFuture(false))
516 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
517 doReturn(true).when(this.readWrite).cancel();
518 final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
520 handler.set(RestConnectorProvider.class, null);
521 handler.setAccessible(false);
523 broker.set(RestConnectorProvider.class, null);
524 broker.setAccessible(false);
526 assertFalse(status.isOk());
527 assertEquals(3, status.getEditCollection().size());
528 assertTrue(status.getEditCollection().get(0).isOk());
529 assertTrue(status.getEditCollection().get(1).isOk());
530 assertFalse(status.getEditCollection().get(2).isOk());
531 assertFalse(status.getEditCollection().get(2).getEditErrors().isEmpty());
532 final String errorMessage = status.getEditCollection().get(2).getEditErrors().get(0).getErrorMessage();
533 assertEquals("Data does not exist", errorMessage);