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