99e598315da339dcf6fa42555b7afc3b170c1544
[netconf.git] / restconf / sal-rest-connector / 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
9 package org.opendaylight.restconf.restful.services.impl;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.Matchers.any;
17 import static org.mockito.Mockito.doNothing;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.mock;
20
21 import com.google.common.base.Optional;
22 import com.google.common.util.concurrent.CheckedFuture;
23 import com.google.common.util.concurrent.Futures;
24 import java.lang.reflect.Field;
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.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.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
67 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
68 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
69 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
70 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
71 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
72
73 public class RestconfDataServiceImplTest {
74
75     private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
76
77     private ContainerNode buildBaseCont;
78     private ContainerNode buildBaseContConfig;
79     private ContainerNode buildBaseContOperational;
80     private SchemaContextRef contextRef;
81     private YangInstanceIdentifier iidBase;
82     private DataSchemaNode schemaNode;
83     private RestconfDataServiceImpl dataService;
84     private QName baseQName;
85     private QName containerPlayerQname;
86     private QName leafQname;
87     private ContainerNode buildPlayerCont;
88     private ContainerNode buildLibraryCont;
89     private MapNode buildPlaylistList;
90
91     @Mock
92     private TransactionChainHandler transactionChainHandler;
93     @Mock
94     private DOMTransactionChain domTransactionChain;
95     @Mock
96     private UriInfo uriInfo;
97     @Mock
98     private DOMDataReadWriteTransaction readWrite;
99     @Mock
100     private DOMDataReadOnlyTransaction read;
101     @Mock
102     private DOMDataWriteTransaction write;
103     @Mock
104     private DOMMountPointServiceHandler mountPointServiceHandler;
105     @Mock
106     private DOMMountPointService mountPointService;
107     @Mock
108     private DOMMountPoint mountPoint;
109     @Mock
110     private DOMDataBroker mountDataBroker;
111     @Mock
112     private DOMTransactionChain transactionChain;
113
114     @Before
115     public void setUp() throws Exception {
116         MockitoAnnotations.initMocks(this);
117
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);
121
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");
125
126         final QName containerLibraryQName = QName.create(this.baseQName, "library");
127         final QName listPlaylistQName = QName.create(this.baseQName, "playlist");
128
129         final LeafNode buildLeaf = Builders.leafBuilder()
130                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.leafQname))
131                 .withValue(0.2)
132                 .build();
133
134         this.buildPlayerCont = Builders.containerBuilder()
135                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.containerPlayerQname))
136                 .withChild(buildLeaf)
137                 .build();
138
139         this.buildLibraryCont = Builders.containerBuilder()
140                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(containerLibraryQName))
141                 .build();
142
143         this.buildPlaylistList = Builders.mapBuilder()
144                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listPlaylistQName))
145                 .build();
146
147         this.buildBaseCont = Builders.containerBuilder()
148                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(this.baseQName))
149                 .withChild(this.buildPlayerCont)
150                 .build();
151
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)
157                 .build();
158
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)
164                 .build();
165
166         this.iidBase = YangInstanceIdentifier.builder()
167                 .node(this.baseQName)
168                 .build();
169
170         this.contextRef = new SchemaContextRef(
171                 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)));
172         this.schemaNode = DataSchemaContextTree.from(this.contextRef.get()).getChild(this.iidBase).getDataSchemaNode();
173
174         final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
175         final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
176         Mockito.when(txHandler.get()).thenReturn(domTx);
177         final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
178         Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
179         final CheckedFuture checked = Mockito.mock(CheckedFuture.class);
180         Mockito.when(wTx.submit()).thenReturn(checked);
181         final Object valueObj = null;
182         Mockito.when(checked.checkedGet()).thenReturn(valueObj);
183         final SchemaContextHandler schemaContextHandler = new SchemaContextHandler(txHandler);
184
185         schemaContextHandler.onGlobalContextUpdated(this.contextRef.get());
186         this.dataService = new RestconfDataServiceImpl(schemaContextHandler, this.transactionChainHandler, this.mountPointServiceHandler);
187         doReturn(this.domTransactionChain).when(this.transactionChainHandler).get();
188         doReturn(this.read).when(this.domTransactionChain).newReadOnlyTransaction();
189         doReturn(this.readWrite).when(this.domTransactionChain).newReadWriteTransaction();
190         doReturn(this.write).when(this.domTransactionChain).newWriteOnlyTransaction();
191         doReturn(this.mountPointService).when(this.mountPointServiceHandler).get();
192         doReturn(Optional.of(this.mountPoint)).when(this.mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
193         doReturn(this.contextRef.get()).when(this.mountPoint).getSchemaContext();
194         doReturn(Optional.of(this.mountDataBroker)).when(this.mountPoint).getService(DOMDataBroker.class);
195         doReturn(this.transactionChain).when(this.mountDataBroker).createTransactionChain(any(TransactionChainListener.class));
196         doReturn(this.read).when(this.transactionChain).newReadOnlyTransaction();
197         doReturn(this.readWrite).when(this.transactionChain).newReadWriteTransaction();
198     }
199
200     @Test
201     public void testReadData() {
202         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
203         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
204                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
205         doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(this.read).read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
206         final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
207         assertNotNull(response);
208         assertEquals(200, response.getStatus());
209         assertEquals(this.buildBaseCont, ((NormalizedNodeContext) response.getEntity()).getData());
210     }
211
212     /**
213      * Test read data from mount point when both {@link LogicalDatastoreType#CONFIGURATION} and
214      * {@link LogicalDatastoreType#OPERATIONAL} contains the same data and some additional data to be merged.
215      */
216     @Test
217     public void testReadDataMountPoint() {
218         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
219         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
220                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
221         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
222                 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
223
224         final Response response = this.dataService.readData(
225                 "example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox", this.uriInfo);
226
227         assertNotNull(response);
228         assertEquals(200, response.getStatus());
229
230         // response must contain all child nodes from config and operational containers merged in one container
231         final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
232         assertTrue(data instanceof ContainerNode);
233         assertEquals(3, ((ContainerNode) data).getValue().size());
234         assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
235         assertTrue(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
236         assertTrue(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
237     }
238
239     @Test(expected = RestconfDocumentedException.class)
240     public void testReadDataNoData() {
241         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
242         doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(this.read).read(LogicalDatastoreType.CONFIGURATION,
243                 this.iidBase);
244         doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(this.read).read(LogicalDatastoreType.OPERATIONAL,
245                 this.iidBase);
246         this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
247     }
248
249     /**
250      * Read data from config datastore according to content parameter
251      */
252     @Test
253     public void testReadDataConfigTest() {
254         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
255         parameters.put("content", Collections.singletonList("config"));
256
257         doReturn(parameters).when(this.uriInfo).getQueryParameters();
258         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
259                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
260         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
261                 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
262
263         final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
264
265         assertNotNull(response);
266         assertEquals(200, response.getStatus());
267
268         // response must contain only config data
269         final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
270
271         // config data present
272         assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
273         assertTrue(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
274
275         // state data absent
276         assertFalse(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
277     }
278
279     /**
280      * Read data from operational datastore according to content parameter
281      */
282     @Test
283     public void testReadDataOperationalTest() {
284         final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
285         parameters.put("content", Collections.singletonList("nonconfig"));
286
287         doReturn(parameters).when(this.uriInfo).getQueryParameters();
288         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
289                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
290         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
291                 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
292
293         final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
294
295         assertNotNull(response);
296         assertEquals(200, response.getStatus());
297
298         // response must contain only operational data
299         final NormalizedNode<?, ?> data = ((NormalizedNodeContext) response.getEntity()).getData();
300
301         // state data present
302         assertTrue(((ContainerNode) data).getChild(this.buildPlayerCont.getIdentifier()).isPresent());
303         assertTrue(((ContainerNode) data).getChild(this.buildPlaylistList.getIdentifier()).isPresent());
304
305         // config data absent
306         assertFalse(((ContainerNode) data).getChild(this.buildLibraryCont.getIdentifier()).isPresent());
307     }
308
309     @Test
310     public void testPutData() {
311         final InstanceIdentifierContext<DataSchemaNode> iidContext = new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
312         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
313
314         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
315                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
316         doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
317         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
318         final Response response = this.dataService.putData(null, payload, this.uriInfo);
319         assertNotNull(response);
320         assertEquals(200, response.getStatus());
321     }
322
323     @Test
324     public void testPutDataWithMountPoint() {
325         final DOMDataBroker dataBroker = Mockito.mock(DOMDataBroker.class);
326         final DOMMountPoint mountPoint = Mockito.mock(DOMMountPoint.class);
327         doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
328         doReturn(this.transactionChainHandler.get()).when(dataBroker).createTransactionChain(RestConnectorProvider.transactionListener);
329         final InstanceIdentifierContext<DataSchemaNode> iidContext = new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, mountPoint, this.contextRef.get());
330         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
331
332         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
333                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
334         doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
335         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
336         final Response response = this.dataService.putData(null, payload, this.uriInfo);
337         assertNotNull(response);
338         assertEquals(200, response.getStatus());
339     }
340
341     @Test
342     public void testPostData() {
343         final QName listQname = QName.create(this.baseQName, "playlist");
344         final QName listKeyQname = QName.create(this.baseQName, "name");
345         final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeWithKey =
346                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(listQname, listKeyQname, "name of band");
347         final LeafNode<Object> content = Builders.leafBuilder()
348                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "name")))
349                 .withValue("name of band")
350                 .build();
351         final LeafNode<Object> content2 = Builders.leafBuilder()
352                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(this.baseQName, "description")))
353                 .withValue("band description")
354                 .build();
355         final MapEntryNode mapEntryNode = Builders.mapEntryBuilder()
356                 .withNodeIdentifier(nodeWithKey)
357                 .withChild(content)
358                 .withChild(content2)
359                 .build();
360         final MapNode buildList = Builders.mapBuilder()
361                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listQname))
362                 .withChild(mapEntryNode)
363                 .build();
364
365         doReturn(new MultivaluedHashMap<String, String>()).when(this.uriInfo).getQueryParameters();
366         final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(this.iidBase, null, null, this.contextRef.get());
367         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, buildList);
368         doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
369         final MapNode data = (MapNode) payload.getData();
370         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifier = data.getValue().iterator().next().getIdentifier();
371         final YangInstanceIdentifier node = payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
372         doReturn(Futures.immediateCheckedFuture(false)).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
373         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, payload.getData());
374         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
375         doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(this.uriInfo).getBaseUriBuilder();
376
377         final Response response = this.dataService.postData(null, payload, this.uriInfo);
378         assertEquals(201, response.getStatus());
379     }
380
381     @Test
382     public void testDeleteData() {
383         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
384         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
385         doReturn(Futures.immediateCheckedFuture(true)).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
386         final Response response = this.dataService.deleteData("example-jukebox:jukebox");
387         assertNotNull(response);
388         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
389     }
390
391     /**
392      * Test of deleting data on mount point
393      */
394     @Test
395     public void testDeleteDataMountPoint() {
396         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, this.iidBase);
397         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
398         doReturn(Futures.immediateCheckedFuture(true)).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
399         final Response response = this.dataService.deleteData("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox");
400         assertNotNull(response);
401         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
402     }
403
404     @Test
405     public void testPatchData() throws Exception {
406         final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
407         final List<PATCHEntity> entity = new ArrayList<>();
408         final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
409                 .node(this.containerPlayerQname)
410                 .node(this.leafQname)
411                 .build();
412         entity.add(new PATCHEntity("create data", "CREATE", this.iidBase, this.buildBaseCont));
413         entity.add(new PATCHEntity("replace data", "REPLACE", this.iidBase, this.buildBaseCont));
414         entity.add(new PATCHEntity("delete data", "DELETE", iidleaf));
415         final PATCHContext patch = new PATCHContext(iidContext, entity, "test patch id");
416
417         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
418                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
419         doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
420         doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
421         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
422         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
423         doReturn(Futures.immediateCheckedFuture(false)).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
424         doReturn(Futures.immediateCheckedFuture(true)).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
425         final PATCHStatusContext status = this.dataService.patchData(patch, this.uriInfo);
426         assertTrue(status.isOk());
427         assertEquals(3, status.getEditCollection().size());
428         assertEquals("replace data", status.getEditCollection().get(1).getEditId());
429     }
430
431     @Test
432     public void testPatchDataMountPoint() throws Exception {
433         final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(
434                 this.iidBase, this.schemaNode, this.mountPoint, this.contextRef.get());
435         final List<PATCHEntity> entity = new ArrayList<>();
436         final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
437                 .node(this.containerPlayerQname)
438                 .node(this.leafQname)
439                 .build();
440         entity.add(new PATCHEntity("create data", "CREATE", this.iidBase, this.buildBaseCont));
441         entity.add(new PATCHEntity("replace data", "REPLACE", this.iidBase, this.buildBaseCont));
442         entity.add(new PATCHEntity("delete data", "DELETE", iidleaf));
443         final PATCHContext patch = new PATCHContext(iidContext, entity, "test patch id");
444
445         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
446                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
447         doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
448         doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
449         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
450         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
451         doReturn(Futures.immediateCheckedFuture(false)).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
452         doReturn(Futures.immediateCheckedFuture(true)).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
453
454         final PATCHStatusContext status = this.dataService.patchData(patch, this.uriInfo);
455         assertTrue(status.isOk());
456         assertEquals(3, status.getEditCollection().size());
457         assertNull(status.getGlobalErrors());
458     }
459
460     @Test
461     public void testPatchDataDeleteNotExist() throws Exception {
462         final Field handler = RestConnectorProvider.class.getDeclaredField("transactionChainHandler");
463         final Field broker = RestConnectorProvider.class.getDeclaredField("dataBroker");
464
465         handler.setAccessible(true);
466         handler.set(RestConnectorProvider.class, mock(TransactionChainHandler.class));
467
468         broker.setAccessible(true);
469         broker.set(RestConnectorProvider.class, mock(DOMDataBroker.class));
470         final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef.get());
471         final List<PATCHEntity> entity = new ArrayList<>();
472         final YangInstanceIdentifier iidleaf = YangInstanceIdentifier.builder(this.iidBase)
473                 .node(this.containerPlayerQname)
474                 .node(this.leafQname)
475                 .build();
476         entity.add(new PATCHEntity("create data", "CREATE", this.iidBase, this.buildBaseCont));
477         entity.add(new PATCHEntity("remove data", "REMOVE", iidleaf));
478         entity.add(new PATCHEntity("delete data", "DELETE", iidleaf));
479         final PATCHContext patch = new PATCHContext(iidContext, entity, "test patch id");
480
481         doReturn(Futures.immediateCheckedFuture(Optional.of(this.buildBaseCont))).when(this.read)
482                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
483         doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
484         doReturn(Futures.immediateCheckedFuture(null)).when(this.write).submit();
485         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
486         doReturn(Futures.immediateCheckedFuture(null)).when(this.readWrite).submit();
487         doReturn(Futures.immediateCheckedFuture(false)).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
488         doReturn(Futures.immediateCheckedFuture(false)).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, iidleaf);
489         doReturn(true).when(this.readWrite).cancel();
490         final PATCHStatusContext status = this.dataService.patchData(patch, this.uriInfo);
491
492         handler.set(RestConnectorProvider.class, null);
493         handler.setAccessible(false);
494
495         broker.set(RestConnectorProvider.class, null);
496         broker.setAccessible(false);
497
498         assertFalse(status.isOk());
499         assertEquals(3, status.getEditCollection().size());
500         assertTrue(status.getEditCollection().get(0).isOk());
501         assertTrue(status.getEditCollection().get(1).isOk());
502         assertFalse(status.getEditCollection().get(2).isOk());
503         assertFalse(status.getEditCollection().get(2).getEditErrors().isEmpty());
504         final String errorMessage = status.getEditCollection().get(2).getEditErrors().get(0).getErrorMessage();
505         assertEquals("Data does not exist", errorMessage);
506     }
507 }