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.restful.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;
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import com.google.common.util.concurrent.Futures;
23 import java.lang.reflect.Field;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import javax.ws.rs.core.MultivaluedHashMap;
29 import javax.ws.rs.core.MultivaluedMap;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.UriBuilder;
32 import javax.ws.rs.core.UriInfo;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
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.TransactionChainListener;
40 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
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.controller.md.sal.rest.common.TestRestconfUtils;
49 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
50 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
51 import org.opendaylight.netconf.sal.restconf.impl.PatchContext;
52 import org.opendaylight.netconf.sal.restconf.impl.PatchEntity;
53 import org.opendaylight.netconf.sal.restconf.impl.PatchStatusContext;
54 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
55 import org.opendaylight.restconf.RestConnectorProvider;
56 import org.opendaylight.restconf.common.references.SchemaContextRef;
57 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
58 import org.opendaylight.restconf.handlers.SchemaContextHandler;
59 import org.opendaylight.restconf.handlers.TransactionChainHandler;
60 import org.opendaylight.restconf.restful.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;
93 private TransactionChainHandler transactionChainHandler;
95 private DOMTransactionChain domTransactionChain;
97 private UriInfo uriInfo;
99 private DOMDataReadWriteTransaction readWrite;
101 private DOMDataReadOnlyTransaction read;
103 private DOMDataWriteTransaction write;
105 private DOMMountPointServiceHandler mountPointServiceHandler;
107 private DOMMountPointService mountPointService;
109 private DOMMountPoint mountPoint;
111 private DOMDataBroker mountDataBroker;
113 private DOMTransactionChain transactionChain;
115 private RestconfStreamsSubscriptionService delegRestconfSubscrService;
118 public void setUp() throws Exception {
119 MockitoAnnotations.initMocks(this);
121 final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
122 Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
123 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(value);
125 this.baseQName = QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
126 this.containerPlayerQname = QName.create(this.baseQName, "player");
127 this.leafQname = QName.create(this.baseQName, "gap");
129 final QName containerLibraryQName = QName.create(this.baseQName, "library");
130 final QName listPlaylistQName = QName.create(this.baseQName, "playlist");
132 final LeafNode buildLeaf = Builders.leafBuilder()
133 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.leafQname))
137 this.buildPlayerCont = Builders.containerBuilder()
138 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.containerPlayerQname))
139 .withChild(buildLeaf)
142 this.buildLibraryCont = Builders.containerBuilder()
143 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(containerLibraryQName))
146 this.buildPlaylistList = Builders.mapBuilder()
147 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listPlaylistQName))
150 this.buildBaseCont = Builders.containerBuilder()
151 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
152 .withChild(this.buildPlayerCont)
155 // config contains one child the same as in operational and one additional
156 this.buildBaseContConfig = Builders.containerBuilder()
157 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
158 .withChild(this.buildPlayerCont)
159 .withChild(this.buildLibraryCont)
162 // operational contains one child the same as in config and one additional
163 this.buildBaseContOperational = Builders.containerBuilder()
164 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
165 .withChild(this.buildPlayerCont)
166 .withChild(this.buildPlaylistList)
169 this.iidBase = YangInstanceIdentifier.builder()
170 .node(this.baseQName)
173 this.contextRef = new SchemaContextRef(
174 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)));
175 this.schemaNode = DataSchemaContextTree.from(this.contextRef.get()).getChild(this.iidBase).getDataSchemaNode();
177 final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
178 final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
179 Mockito.when(txHandler.get()).thenReturn(domTx);
180 final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
181 Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
182 final CheckedFuture<Void, TransactionCommitFailedException> checked = Mockito.mock(CheckedFuture.class);
183 Mockito.when(wTx.submit()).thenReturn(checked);
184 Mockito.when(checked.checkedGet()).thenReturn(null);
185 final SchemaContextHandler schemaContextHandler = new SchemaContextHandler(txHandler);
187 schemaContextHandler.onGlobalContextUpdated(this.contextRef.get());
188 this.dataService = new RestconfDataServiceImpl(schemaContextHandler, this.transactionChainHandler,
189 this.mountPointServiceHandler, this.delegRestconfSubscrService);
190 doReturn(this.domTransactionChain).when(this.transactionChainHandler).get();
191 doReturn(this.read).when(this.domTransactionChain).newReadOnlyTransaction();
192 doReturn(this.readWrite).when(this.domTransactionChain).newReadWriteTransaction();
193 doReturn(this.write).when(this.domTransactionChain).newWriteOnlyTransaction();
194 doReturn(this.mountPointService).when(this.mountPointServiceHandler).get();
195 doReturn(Optional.of(this.mountPoint)).when(this.mountPointService)
196 .getMountPoint(any(YangInstanceIdentifier.class));
197 doReturn(this.contextRef.get()).when(this.mountPoint).getSchemaContext();
198 doReturn(Optional.of(this.mountDataBroker)).when(this.mountPoint).getService(DOMDataBroker.class);
199 doReturn(this.transactionChain).when(this.mountDataBroker)
200 .createTransactionChain(any(TransactionChainListener.class));
201 doReturn(this.read).when(this.transactionChain).newReadOnlyTransaction();
202 doReturn(this.readWrite).when(this.transactionChain).newReadWriteTransaction();
206 public void testReadData() {
207 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
208 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
209 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
210 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
211 .when(this.read).read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
212 final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
213 assertNotNull(response);
214 assertEquals(200, response.getStatus());
215 assertEquals(this.buildBaseCont, ((NormalizedNodeContext) response.getEntity()).getData());
219 * Test read data from mount point when both {@link LogicalDatastoreType#CONFIGURATION} and
220 * {@link LogicalDatastoreType#OPERATIONAL} contains the same data and some additional data to be merged.
223 public void testReadDataMountPoint() {
224 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
225 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
226 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
227 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
228 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
230 final Response response = this.dataService.readData(
231 "example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", this.uriInfo);
233 assertNotNull(response);
234 assertEquals(200, response.getStatus());
236 // response must contain all child nodes from config and operational containers merged in one container
237 final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
238 assertTrue(data instanceof ContainerNode);
239 assertEquals(3, ((ContainerNode) data).getValue().size());
240 assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
241 assertTrue(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
242 assertTrue(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
245 @Test(expected = RestconfDocumentedException.class)
246 public void testReadDataNoData() {
247 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
248 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
249 .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
250 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
251 .when(this.read).read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
252 this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
256 * Read data from config datastore according to content parameter.
259 public void testReadDataConfigTest() {
260 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
261 parameters.put("content", Collections.singletonList("config"));
263 doReturn(parameters).when(this.uriInfo).getQueryParameters();
264 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
265 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
266 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
267 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
269 final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
271 assertNotNull(response);
272 assertEquals(200, response.getStatus());
274 // response must contain only config data
275 final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
277 // config data present
278 assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
279 assertTrue(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
282 assertFalse(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
286 * Read data from operational datastore according to content parameter.
289 public void testReadDataOperationalTest() {
290 final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
291 parameters.put("content", Collections.singletonList("nonconfig"));
293 doReturn(parameters).when(this.uriInfo).getQueryParameters();
294 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
295 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
296 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
297 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
299 final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
301 assertNotNull(response);
302 assertEquals(200, response.getStatus());
304 // response must contain only operational data
305 final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
307 // state data present
308 assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
309 assertTrue(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
311 // config data absent
312 assertFalse(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
316 public void testPutData() {
317 final InstanceIdentifierContext<DataSchemaNode> iidContext =
318 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
319 final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
321 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
322 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
323 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
324 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
325 final Response response = this.dataService.putData(null, payload, this.uriInfo);
326 assertNotNull(response);
327 assertEquals(200, response.getStatus());
331 public void testPutDataWithMountPoint() {
332 final DOMDataBroker dataBroker = Mockito.mock(DOMDataBroker.class);
333 final DOMMountPoint mountPoint = Mockito.mock(DOMMountPoint.class);
334 doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
335 doReturn(this.transactionChainHandler.get()).when(dataBroker)
336 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER);
337 final InstanceIdentifierContext<DataSchemaNode> iidContext =
338 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, mountPoint, this.contextRef.get());
339 final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
341 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
342 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
343 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
344 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
345 final Response response = this.dataService.putData(null, payload, this.uriInfo);
346 assertNotNull(response);
347 assertEquals(200, response.getStatus());
351 public void testPostData() {
352 final QName listQname = QName.create(this.baseQName, "playlist");
353 final QName listKeyQname = QName.create(this.baseQName, "name");
354 final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeWithKey =
355 new YangInstanceIdentifier.NodeIdentifierWithPredicates(listQname, listKeyQname, "name of band");
356 final LeafNode<Object> content = Builders.leafBuilder()
357 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "name")))
358 .withValue("name of band")
360 final LeafNode<Object> content2 = Builders.leafBuilder()
361 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "description")))
362 .withValue("band description")
364 final MapEntryNode mapEntryNode = Builders.mapEntryBuilder()
365 .withNodeIdentifier(nodeWithKey)
369 final MapNode buildList = Builders.mapBuilder()
370 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listQname))
371 .withChild(mapEntryNode)
374 doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
375 final InstanceIdentifierContext<? extends SchemaNode> iidContext =
376 new InstanceIdentifierContext<>(this.iidBase, null, null, this.contextRef.get());
377 final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, buildList);
378 doReturn(Futures.immediateCheckedFuture(Optional.absent()))
379 .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
380 final MapNode data = (MapNode) payload.getData();
381 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifier =
382 data.getValue().iterator().next().getIdentifier();
383 final YangInstanceIdentifier node =
384 payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
385 doReturn(Futures.immediateCheckedFuture(false))
386 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
387 doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, payload.getData());
388 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
389 doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(this.uriInfo).getBaseUriBuilder();
391 final Response response = this.dataService.postData(null, payload, this.uriInfo);
392 assertEquals(201, response.getStatus());
396 public void testDeleteData() {
397 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
398 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
399 doReturn(Futures.immediateCheckedFuture(true))
400 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
401 final Response response = this.dataService.deleteData("example-jukebox:jukebox");
402 assertNotNull(response);
403 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
407 * Test of deleting data on mount point.
410 public void testDeleteDataMountPoint() {
411 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
412 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
413 doReturn(Futures.immediateCheckedFuture(true))
414 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
415 final Response response =
416 this.dataService.deleteData("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox");
417 assertNotNull(response);
418 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
422 public void testPatchData() throws Exception {
423 final InstanceIdentifierContext<? extends SchemaNode> iidContext =
424 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
425 final List<PatchEntity> entity = new ArrayList<>();
426 final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
427 .node(this.containerPlayerQname)
428 .node(this.leafQname)
430 entity.add(new PatchEntity("create data", "CREATE", this.iidBase, this.buildBaseCont));
431 entity.add(new PatchEntity("replace data", "REPLACE", this.iidBase, this.buildBaseCont));
432 entity.add(new PatchEntity("delete data", "DELETE", iidleaf));
433 final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
435 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
436 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
437 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
438 doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
439 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
440 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
441 doReturn(Futures.immediateCheckedFuture(false))
442 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
443 doReturn(Futures.immediateCheckedFuture(true))
444 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
445 final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
446 assertTrue(status.isOk());
447 assertEquals(3, status.getEditCollection().size());
448 assertEquals("replace data", status.getEditCollection().get(1).getEditId());
452 public void testPatchDataMountPoint() throws Exception {
453 final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(
454 this.iidBase, this.schemaNode, this.mountPoint, this.contextRef.get());
455 final List<PatchEntity> entity = new ArrayList<>();
456 final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
457 .node(this.containerPlayerQname)
458 .node(this.leafQname)
460 entity.add(new PatchEntity("create data", "CREATE", this.iidBase, this.buildBaseCont));
461 entity.add(new PatchEntity("replace data", "REPLACE", this.iidBase, this.buildBaseCont));
462 entity.add(new PatchEntity("delete data", "DELETE", iidleaf));
463 final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
465 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
466 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
467 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
468 doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
469 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
470 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
471 doReturn(Futures.immediateCheckedFuture(false))
472 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
473 doReturn(Futures.immediateCheckedFuture(true))
474 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
476 final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
477 assertTrue(status.isOk());
478 assertEquals(3, status.getEditCollection().size());
479 assertNull(status.getGlobalErrors());
483 public void testPatchDataDeleteNotExist() throws Exception {
484 final Field handler = RestConnectorProvider.class.getDeclaredField("transactionChainHandler");
485 final Field broker = RestConnectorProvider.class.getDeclaredField("dataBroker");
487 handler.setAccessible(true);
488 handler.set(RestConnectorProvider.class, mock(TransactionChainHandler.class));
490 broker.setAccessible(true);
491 broker.set(RestConnectorProvider.class, mock(DOMDataBroker.class));
492 final InstanceIdentifierContext<? extends SchemaNode> iidContext =
493 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
494 final List<PatchEntity> entity = new ArrayList<>();
495 final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
496 .node(this.containerPlayerQname)
497 .node(this.leafQname)
499 entity.add(new PatchEntity("create data", "CREATE", this.iidBase, this.buildBaseCont));
500 entity.add(new PatchEntity("remove data", "REMOVE", iidleaf));
501 entity.add(new PatchEntity("delete data", "DELETE", iidleaf));
502 final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
504 doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
505 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
506 doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
507 doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
508 doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
509 doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
510 doReturn(Futures.immediateCheckedFuture(false))
511 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
512 doReturn(Futures.immediateCheckedFuture(false))
513 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
514 doReturn(true).when(this.readWrite).cancel();
515 final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
517 handler.set(RestConnectorProvider.class, null);
518 handler.setAccessible(false);
520 broker.set(RestConnectorProvider.class, null);
521 broker.setAccessible(false);
523 assertFalse(status.isOk());
524 assertEquals(3, status.getEditCollection().size());
525 assertTrue(status.getEditCollection().get(0).isOk());
526 assertTrue(status.getEditCollection().get(1).isOk());
527 assertFalse(status.getEditCollection().get(2).isOk());
528 assertFalse(status.getEditCollection().get(2).getEditErrors().isEmpty());
529 final String errorMessage = status.getEditCollection().get(2).getEditErrors().get(0).getErrorMessage();
530 assertEquals("Data does not exist", errorMessage);