93babcbceb5b836b73f50ceed922ee610571a000
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / restconf / restful / services / impl / RestconfDataServiceImplTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.restful.services.impl;
9
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.netconf.sal.restconf.impl.PatchEditOperation.CREATE;
20 import static org.opendaylight.netconf.sal.restconf.impl.PatchEditOperation.DELETE;
21 import static org.opendaylight.netconf.sal.restconf.impl.PatchEditOperation.REMOVE;
22 import static org.opendaylight.netconf.sal.restconf.impl.PatchEditOperation.REPLACE;
23
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.controller.md.sal.rest.common.TestRestconfUtils;
53 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
54 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
55 import org.opendaylight.netconf.sal.restconf.impl.PatchContext;
56 import org.opendaylight.netconf.sal.restconf.impl.PatchEntity;
57 import org.opendaylight.netconf.sal.restconf.impl.PatchStatusContext;
58 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
59 import org.opendaylight.restconf.RestConnectorProvider;
60 import org.opendaylight.restconf.common.references.SchemaContextRef;
61 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
62 import org.opendaylight.restconf.handlers.SchemaContextHandler;
63 import org.opendaylight.restconf.handlers.TransactionChainHandler;
64 import org.opendaylight.restconf.restful.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;
77
78 public class RestconfDataServiceImplTest {
79
80     private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
81
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;
95
96     @Mock
97     private TransactionChainHandler transactionChainHandler;
98     @Mock
99     private DOMTransactionChain domTransactionChain;
100     @Mock
101     private UriInfo uriInfo;
102     @Mock
103     private DOMDataReadWriteTransaction readWrite;
104     @Mock
105     private DOMDataReadOnlyTransaction read;
106     @Mock
107     private DOMDataWriteTransaction write;
108     @Mock
109     private DOMMountPointServiceHandler mountPointServiceHandler;
110     @Mock
111     private DOMMountPointService mountPointService;
112     @Mock
113     private DOMMountPoint mountPoint;
114     @Mock
115     private DOMDataBroker mountDataBroker;
116     @Mock
117     private DOMTransactionChain transactionChain;
118     @Mock
119     private RestconfStreamsSubscriptionService delegRestconfSubscrService;
120
121     @Before
122     public void setUp() throws Exception {
123         MockitoAnnotations.initMocks(this);
124
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);
128
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");
132
133         final QName containerLibraryQName = QName.create(this.baseQName, "library");
134         final QName listPlaylistQName = QName.create(this.baseQName, "playlist");
135
136         final LeafNode buildLeaf = Builders.leafBuilder()
137                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.leafQname))
138                 .withValue(0.2)
139                 .build();
140
141         this.buildPlayerCont = Builders.containerBuilder()
142                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.containerPlayerQname))
143                 .withChild(buildLeaf)
144                 .build();
145
146         this.buildLibraryCont = Builders.containerBuilder()
147                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(containerLibraryQName))
148                 .build();
149
150         this.buildPlaylistList = Builders.mapBuilder()
151                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listPlaylistQName))
152                 .build();
153
154         this.buildBaseCont = Builders.containerBuilder()
155                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
156                 .withChild(this.buildPlayerCont)
157                 .build();
158
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)
164                 .build();
165
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)
171                 .build();
172
173         this.iidBase = YangInstanceIdentifier.builder()
174                 .node(this.baseQName)
175                 .build();
176
177         this.contextRef = new SchemaContextRef(
178                 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)));
179         this.schemaNode = DataSchemaContextTree.from(this.contextRef.get()).getChild(this.iidBase).getDataSchemaNode();
180
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);
190
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();
207     }
208
209     @Test
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());
220     }
221
222     /**
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.
225      */
226     @Test
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);
233
234         final Response response = this.dataService.readData(
235                 "example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", this.uriInfo);
236
237         assertNotNull(response);
238         assertEquals(200, response.getStatus());
239
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());
247     }
248
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);
257     }
258
259     /**
260      * Read data from config datastore according to content parameter.
261      */
262     @Test
263     public void testReadDataConfigTest() {
264         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
265         parameters.put("content", Collections.singletonList("config"));
266
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);
272
273         final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
274
275         assertNotNull(response);
276         assertEquals(200, response.getStatus());
277
278         // response must contain only config data
279         final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
280
281         // config data present
282         assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
283         assertTrue(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
284
285         // state data absent
286         assertFalse(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
287     }
288
289     /**
290      * Read data from operational datastore according to content parameter.
291      */
292     @Test
293     public void testReadDataOperationalTest() {
294         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
295         parameters.put("content", Collections.singletonList("nonconfig"));
296
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);
302
303         final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
304
305         assertNotNull(response);
306         assertEquals(200, response.getStatus());
307
308         // response must contain only operational data
309         final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
310
311         // state data present
312         assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
313         assertTrue(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
314
315         // config data absent
316         assertFalse(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
317     }
318
319     @Test
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);
324
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());
332     }
333
334     @Test
335     public void testPutDataWithMountPoint() {
336         final DOMDataBroker dataBroker = Mockito.mock(DOMDataBroker.class);
337         final DOMMountPoint mountPoint = Mockito.mock(DOMMountPoint.class);
338         doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
339         doReturn(this.transactionChainHandler.get()).when(dataBroker)
340                 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER);
341         final InstanceIdentifierContext<DataSchemaNode> iidContext =
342                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, mountPoint, this.contextRef.get());
343         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
344
345         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
346                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
347         doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
348         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
349         final Response response = this.dataService.putData(null, payload, this.uriInfo);
350         assertNotNull(response);
351         assertEquals(200, response.getStatus());
352     }
353
354     @Test
355     public void testPostData() {
356         final QName listQname = QName.create(this.baseQName, "playlist");
357         final QName listKeyQname = QName.create(this.baseQName, "name");
358         final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeWithKey =
359                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(listQname, listKeyQname, "name of band");
360         final LeafNode<Object> content = Builders.leafBuilder()
361                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "name")))
362                 .withValue("name of band")
363                 .build();
364         final LeafNode<Object> content2 = Builders.leafBuilder()
365             .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "description")))
366             .withValue("band description")
367             .build();
368         final MapEntryNode mapEntryNode = Builders.mapEntryBuilder()
369                 .withNodeIdentifier(nodeWithKey)
370                 .withChild(content)
371                 .withChild(content2)
372                 .build();
373         final MapNode buildList = Builders.mapBuilder()
374                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listQname))
375                 .withChild(mapEntryNode)
376                 .build();
377
378         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
379         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
380                 new InstanceIdentifierContext<>(this.iidBase, null, null, this.contextRef.get());
381         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, buildList);
382         doReturn(Futures.immediateCheckedFuture(Optional.absent()))
383                 .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
384         final MapNode data = (MapNode) payload.getData();
385         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifier =
386                 data.getValue().iterator().next().getIdentifier();
387         final YangInstanceIdentifier node =
388                 payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
389         doReturn(Futures.immediateCheckedFuture(false))
390                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
391         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, payload.getData());
392         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
393         doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(this.uriInfo).getBaseUriBuilder();
394
395         final Response response = this.dataService.postData(null, payload, this.uriInfo);
396         assertEquals(201, response.getStatus());
397     }
398
399     @Test
400     public void testDeleteData() {
401         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
402         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
403         doReturn(Futures.immediateCheckedFuture(true))
404                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
405         final Response response = this.dataService.deleteData("example-jukebox:jukebox");
406         assertNotNull(response);
407         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
408     }
409
410     /**
411      * Test of deleting data on mount point.
412      */
413     @Test
414     public void testDeleteDataMountPoint() {
415         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
416         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
417         doReturn(Futures.immediateCheckedFuture(true))
418                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
419         final Response response =
420                 this.dataService.deleteData("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox");
421         assertNotNull(response);
422         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
423     }
424
425     @Test
426     public void testPatchData() throws Exception {
427         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
428                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
429         final List<PatchEntity> entity = new ArrayList<>();
430         final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
431                 .node(this.containerPlayerQname)
432                 .node(this.leafQname)
433                 .build();
434         entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
435         entity.add(new PatchEntity("replace data", REPLACE, this.iidBase, this.buildBaseCont));
436         entity.add(new PatchEntity("delete data", DELETE, iidleaf));
437         final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
438
439         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
440                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
441         doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
442         doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
443         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
444         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
445         doReturn(Futures.immediateCheckedFuture(false))
446                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
447         doReturn(Futures.immediateCheckedFuture(true))
448                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
449         final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
450         assertTrue(status.isOk());
451         assertEquals(3, status.getEditCollection().size());
452         assertEquals("replace data", status.getEditCollection().get(1).getEditId());
453     }
454
455     @Test
456     public void testPatchDataMountPoint() throws Exception {
457         final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(
458                 this.iidBase, this.schemaNode, this.mountPoint, this.contextRef.get());
459         final List<PatchEntity> entity = new ArrayList<>();
460         final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
461                 .node(this.containerPlayerQname)
462                 .node(this.leafQname)
463                 .build();
464         entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
465         entity.add(new PatchEntity("replace data", REPLACE, this.iidBase, this.buildBaseCont));
466         entity.add(new PatchEntity("delete data", DELETE, iidleaf));
467         final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
468
469         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
470                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
471         doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
472         doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
473         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
474         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
475         doReturn(Futures.immediateCheckedFuture(false))
476                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
477         doReturn(Futures.immediateCheckedFuture(true))
478                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
479
480         final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
481         assertTrue(status.isOk());
482         assertEquals(3, status.getEditCollection().size());
483         assertNull(status.getGlobalErrors());
484     }
485
486     @Test
487     public void testPatchDataDeleteNotExist() throws Exception {
488         final Field handler = RestConnectorProvider.class.getDeclaredField("transactionChainHandler");
489         final Field broker = RestConnectorProvider.class.getDeclaredField("dataBroker");
490
491         handler.setAccessible(true);
492         handler.set(RestConnectorProvider.class, mock(TransactionChainHandler.class));
493
494         broker.setAccessible(true);
495         broker.set(RestConnectorProvider.class, mock(DOMDataBroker.class));
496         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
497                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
498         final List<PatchEntity> entity = new ArrayList<>();
499         final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
500                 .node(this.containerPlayerQname)
501                 .node(this.leafQname)
502                 .build();
503         entity.add(new PatchEntity("create data", CREATE, this.iidBase, this.buildBaseCont));
504         entity.add(new PatchEntity("remove data", REMOVE, iidleaf));
505         entity.add(new PatchEntity("delete data", DELETE, iidleaf));
506         final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
507
508         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
509                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
510         doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
511         doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
512         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
513         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
514         doReturn(Futures.immediateCheckedFuture(false))
515                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
516         doReturn(Futures.immediateCheckedFuture(false))
517                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
518         doReturn(true).when(this.readWrite).cancel();
519         final PatchStatusContext status = this.dataService.patchData(patch, this.uriInfo);
520
521         handler.set(RestConnectorProvider.class, null);
522         handler.setAccessible(false);
523
524         broker.set(RestConnectorProvider.class, null);
525         broker.setAccessible(false);
526
527         assertFalse(status.isOk());
528         assertEquals(3, status.getEditCollection().size());
529         assertTrue(status.getEditCollection().get(0).isOk());
530         assertTrue(status.getEditCollection().get(1).isOk());
531         assertFalse(status.getEditCollection().get(2).isOk());
532         assertFalse(status.getEditCollection().get(2).getEditErrors().isEmpty());
533         final String errorMessage = status.getEditCollection().get(2).getEditErrors().get(0).getErrorMessage();
534         assertEquals("Data does not exist", errorMessage);
535     }
536 }