Split out yang-data-tree-impl
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / Bug4454Test.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.yangtools.yang.data.tree.impl;
9
10 import static junit.framework.TestCase.assertFalse;
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Optional;
21 import org.junit.AfterClass;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
38 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
44 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
45 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
46 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
47 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
48 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
49 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
50 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
51 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
52
53 public class Bug4454Test {
54
55     private static final QName MASTER_CONTAINER_QNAME = QName
56             .create("urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
57                     "master-container");
58     private static final QName MIN_MAX_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-list");
59     private static final QName MIN_MAX_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-leaf-list");
60     private static final QName MIN_MAX_LIST_QNAME_NO_MINMAX = QName
61             .create(MASTER_CONTAINER_QNAME, "min-max-list-no-minmax");
62     private static final QName MIN_MAX_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-key-leaf");
63     private static final QName MIN_MAX_VALUE_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-value-leaf");
64     private static final QName PRESENCE_QNAME = QName.create(MASTER_CONTAINER_QNAME, "presence");
65
66     private static final YangInstanceIdentifier MASTER_CONTAINER_PATH = YangInstanceIdentifier
67             .of(MASTER_CONTAINER_QNAME);
68     private static final YangInstanceIdentifier MIN_MAX_LIST_PATH = YangInstanceIdentifier
69             .builder(MASTER_CONTAINER_PATH)
70             .node(MIN_MAX_LIST_QNAME).build();
71     private static final YangInstanceIdentifier PRESENCE_PATH = YangInstanceIdentifier.of(PRESENCE_QNAME);
72     private static final YangInstanceIdentifier PRESENCE_MIN_MAX_LIST_PATH = PRESENCE_PATH.node(MIN_MAX_LIST_QNAME);
73     private static final YangInstanceIdentifier MIN_MAX_LIST_NO_MINMAX_PATH = YangInstanceIdentifier
74             .builder(MASTER_CONTAINER_PATH)
75             .node(MIN_MAX_LIST_QNAME_NO_MINMAX).build();
76     private static final YangInstanceIdentifier MIN_MAX_LEAF_LIST_PATH = YangInstanceIdentifier
77             .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LEAF_LIST_QNAME).build();
78
79     private final MapEntryNode fooEntryNodeWithValue = ImmutableMapEntryNodeBuilder.create().withNodeIdentifier(
80         NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo"))
81             .withChild(ImmutableNodes.leafNode(MIN_MAX_VALUE_LEAF_QNAME, "footest")).build();
82     private final MapEntryNode bazEntryNodeWithValue = ImmutableMapEntryNodeBuilder.create().withNodeIdentifier(
83         NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "baz"))
84             .withChild(ImmutableNodes.leafNode(MIN_MAX_VALUE_LEAF_QNAME, "baztest")).build();
85     private final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME,
86             "foo");
87     private final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME,
88             "bar");
89     private final MapEntryNode bazEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME,
90             "baz");
91     private final SystemMapNode mapNodeBazFuzWithNodes = ImmutableNodes.mapNodeBuilder()
92             .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
93             .withChild(bazEntryNode).withChild(bazEntryNodeWithValue).withChild(fooEntryNode)
94             .build();
95     private final SystemMapNode mapNodeFooWithNodes = ImmutableNodes.mapNodeBuilder()
96             .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
97             .withChild(fooEntryNode).withChild(fooEntryNodeWithValue).withChild(barEntryNode).withChild(bazEntryNode)
98             .build();
99     private final SystemMapNode mapNodeBar = ImmutableNodes.mapNodeBuilder()
100             .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
101             .withChild(barEntryNode).build();
102     private final SystemMapNode mapNodeBaz = ImmutableNodes.mapNodeBuilder()
103             .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
104             .withChild(bazEntryNode).build();
105
106     private static EffectiveModelContext schemaContext;
107
108     private DataTree inMemoryDataTree;
109
110     @BeforeClass
111     public static void beforeClass() {
112         schemaContext = YangParserTestUtils.parseYangResource("/bug-4454-test.yang");
113     }
114
115     @AfterClass
116     public static void afterClass() {
117         schemaContext = null;
118     }
119
120     @Before
121     public void prepare() throws DataValidationFailedException {
122         inMemoryDataTree =  new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
123             schemaContext);
124         final DataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
125         final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
126
127         modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
128         modificationTree.ready();
129         inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
130     }
131
132     @Test
133     public void minMaxListDeleteWriteTest() throws DataValidationFailedException {
134         final DataTreeModification modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
135
136         Map<QName, Object> key = new HashMap<>();
137         key.put(MIN_MAX_KEY_LEAF_QNAME, "foo");
138
139         NodeIdentifierWithPredicates mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME , key);
140
141         final YangInstanceIdentifier minMaxLeafFoo = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
142                 .node(MIN_MAX_LIST_QNAME).node(mapEntryPath2).build();
143
144         key.clear();
145         key.put(MIN_MAX_KEY_LEAF_QNAME, "NON-EXISTING-LEAF");
146
147         mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME, key);
148
149         final YangInstanceIdentifier minMaxLeafNel = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
150                 .node(MIN_MAX_LIST_QNAME).node(mapEntryPath2).build();
151
152         final Map<QName, Object> keyTemp = new HashMap<>();
153         keyTemp.put(MIN_MAX_KEY_LEAF_QNAME, "baz");
154
155         NodeIdentifierWithPredicates mapEntryPathTest = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME , keyTemp);
156
157         final YangInstanceIdentifier pathToBaz = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
158                 .node(MIN_MAX_LIST_QNAME).node(mapEntryPathTest).node(MIN_MAX_VALUE_LEAF_QNAME).build();
159
160         keyTemp.clear();
161         keyTemp.put(MIN_MAX_KEY_LEAF_QNAME, "bar");
162
163         mapEntryPathTest = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME , keyTemp);
164
165         final YangInstanceIdentifier pathToBar = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
166                 .node(MIN_MAX_LIST_QNAME).node(mapEntryPathTest).node(MIN_MAX_VALUE_LEAF_QNAME).build();
167
168         keyTemp.clear();
169         keyTemp.put(MIN_MAX_KEY_LEAF_QNAME, "foo");
170
171         final NodeIdentifierWithPredicates mapEntryPathTestKey = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME,
172             keyTemp);
173
174         final YangInstanceIdentifier pathToKeyFoo = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
175                 .node(MIN_MAX_LIST_QNAME).node(mapEntryPathTestKey).node(MIN_MAX_KEY_LEAF_QNAME).build();
176
177         final LeafNode<String> newNode = ImmutableNodes.leafNode(MIN_MAX_VALUE_LEAF_QNAME, "test");
178         final LeafNode<String> newNode1 = ImmutableNodes.leafNode(MIN_MAX_VALUE_LEAF_QNAME, "test1");
179         final LeafNode<String> newNode2 = ImmutableNodes.leafNode(MIN_MAX_VALUE_LEAF_QNAME, "test2");
180         final LeafNode<String> newNodekey = ImmutableNodes.leafNode(MIN_MAX_KEY_LEAF_QNAME, "foo");
181
182         assertFalse(inMemoryDataTree.toString().contains("list"));
183
184         DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
185         Optional<NormalizedNode> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
186         assertTrue(!minMaxListRead.isPresent());
187
188         modificationTree1.write(MIN_MAX_LIST_PATH, mapNodeFooWithNodes);
189         modificationTree1.write(MIN_MAX_LIST_PATH, mapNodeFooWithNodes);
190         modificationTree1.write(MIN_MAX_LIST_PATH, mapNodeFooWithNodes);
191         modificationTree1.merge(MIN_MAX_LIST_PATH, mapNodeBar);
192         modificationTree1.merge(MIN_MAX_LIST_PATH, mapNodeBaz);
193         modificationTree1.write(pathToKeyFoo, newNodekey);
194         modificationTree1.write(pathToBaz, newNode2);
195         modificationTree1.write(pathToBaz, newNode1);
196         modificationTree1.write(pathToBaz, newNode);
197         modificationTree1.delete(minMaxLeafFoo);
198         modificationTree1.delete(minMaxLeafNel);
199
200         modificationTree1.ready();
201         inMemoryDataTree.validate(modificationTree1);
202         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree1);
203         inMemoryDataTree.commit(prepare);
204
205         DataTreeSnapshot test = inMemoryDataTree.takeSnapshot();
206         testLoop(test, "bar", "test");
207
208         DataTreeModification tempMod = test.newModification();
209         tempMod.write(pathToBaz, newNode2);
210         tempMod.write(pathToBaz, newNode1);
211         tempMod.merge(pathToBaz, newNode2);
212         tempMod.write(pathToBaz, newNode1);
213
214         tempMod.ready();
215         inMemoryDataTree.validate(tempMod);
216         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(tempMod);
217         inMemoryDataTree.commit(prepare1);
218
219         DataTreeSnapshot test1 = inMemoryDataTree.takeSnapshot();
220         testLoop(test1, "bar", "test1");
221
222         DataTreeModification tempMod1 = test1.newModification();
223         tempMod1.write(MIN_MAX_LIST_PATH, mapNodeFooWithNodes);
224
225         tempMod1.ready();
226         inMemoryDataTree.validate(tempMod1);
227         final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(tempMod1);
228         inMemoryDataTree.commit(prepare2);
229
230         DataTreeSnapshot test2 = inMemoryDataTree.takeSnapshot();
231         minMaxListRead = test2.readNode(MIN_MAX_LIST_PATH);
232         assertTrue(minMaxListRead.isPresent());
233         assertEquals(3, ((NormalizedNodeContainer<?>) minMaxListRead.get()).size());
234
235         DataTreeModification tempMod2 = test2.newModification();
236         tempMod2.write(MIN_MAX_LIST_PATH, mapNodeBaz);
237         tempMod2.write(pathToBaz, newNode2);
238
239         tempMod2.ready();
240         inMemoryDataTree.validate(tempMod2);
241         final DataTreeCandidate prepare3 = inMemoryDataTree.prepare(tempMod2);
242         inMemoryDataTree.commit(prepare3);
243
244         DataTreeSnapshot test3 = inMemoryDataTree.takeSnapshot();
245         minMaxListRead = test3.readNode(MIN_MAX_LIST_PATH);
246         assertTrue(minMaxListRead.isPresent());
247         assertEquals(1, ((NormalizedNodeContainer<?>) minMaxListRead.get()).size());
248         assertThat(minMaxListRead.get().body().toString(), containsString("test2"));
249
250         DataTreeModification tempMod3 = test3.newModification();
251         tempMod3.merge(MIN_MAX_LIST_PATH, mapNodeBar);
252         tempMod3.merge(pathToBar, newNode1);
253
254         tempMod3.ready();
255         inMemoryDataTree.validate(tempMod3);
256         final DataTreeCandidate prepare4 = inMemoryDataTree.prepare(tempMod3);
257         inMemoryDataTree.commit(prepare4);
258
259         DataTreeSnapshot test4 = inMemoryDataTree.takeSnapshot();
260         testLoop(test4, "test1", "test2");
261     }
262
263     @Test
264     public void minMaxLeafListPass() throws DataValidationFailedException {
265         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
266
267         final NodeWithValue<?> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
268         final NodeWithValue<?> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
269
270         final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
271                 .withNodeIdentifier(barPath)
272                 .withValue("bar").build();
273         final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
274                 .withNodeIdentifier(gooPath)
275                 .withValue("goo").build();
276
277         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
278                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
279                 .withChildValue("foo")
280                 .build();
281
282         modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
283         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
284         modificationTree.ready();
285
286         inMemoryDataTree.validate(modificationTree);
287         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
288         inMemoryDataTree.commit(prepare1);
289
290         DataTreeSnapshot test1 = inMemoryDataTree.takeSnapshot();
291
292         DataTreeModification tempMod1 = test1.newModification();
293         tempMod1.write(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
294         tempMod1.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
295         tempMod1.ready();
296
297         inMemoryDataTree.validate(tempMod1);
298         final DataTreeCandidate prepare2 = inMemoryDataTree.prepare(tempMod1);
299         inMemoryDataTree.commit(prepare2);
300
301         final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
302         final Optional<NormalizedNode> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
303         assertTrue(masterContainer.isPresent());
304         final Optional<NormalizedNodeContainer<?>> leafList = ((DistinctNodeContainer) masterContainer.get())
305                 .findChildByArg(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
306         assertTrue(leafList.isPresent());
307         assertEquals(3, leafList.get().size());
308     }
309
310     @Test
311     public void minMaxListDeleteTest() throws DataValidationFailedException {
312         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
313
314
315         NodeIdentifierWithPredicates mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME,
316             MIN_MAX_KEY_LEAF_QNAME, "foo");
317
318         final YangInstanceIdentifier minMaxLeafFoo = MASTER_CONTAINER_PATH
319                 .node(MIN_MAX_LIST_QNAME).node(mapEntryPath2);
320
321         mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
322
323         final YangInstanceIdentifier minMaxLeafBar = MASTER_CONTAINER_PATH
324                 .node(MIN_MAX_LIST_QNAME).node(mapEntryPath2);
325
326         mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "baz");
327
328         final YangInstanceIdentifier minMaxLeafBaz = MASTER_CONTAINER_PATH
329                 .node(MIN_MAX_LIST_QNAME).node(mapEntryPath2);
330
331         modificationTree.write(MIN_MAX_LIST_PATH, mapNodeFooWithNodes);
332         modificationTree.merge(MIN_MAX_LIST_PATH, mapNodeBar);
333         modificationTree.merge(MIN_MAX_LIST_PATH, mapNodeBaz);
334         modificationTree.delete(minMaxLeafFoo);
335         modificationTree.delete(minMaxLeafBar);
336         modificationTree.delete(minMaxLeafBaz);
337
338         modificationTree.ready();
339
340         inMemoryDataTree.validate(modificationTree);
341         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
342         inMemoryDataTree.commit(prepare);
343
344         // Empty list should have disappeared, along with the container, as we are not enforcing root
345         final NormalizedNode data = inMemoryDataTree.takeSnapshot()
346                 .readNode(YangInstanceIdentifier.empty()).get();
347         assertTrue(data instanceof ContainerNode);
348         assertEquals(0, ((ContainerNode) data).size());
349     }
350
351     @Test
352     public void minMaxListDeleteExceptionTest() throws DataValidationFailedException {
353         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
354
355         NodeIdentifierWithPredicates mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME,
356             MIN_MAX_KEY_LEAF_QNAME, "foo");
357
358         final YangInstanceIdentifier minMaxLeafFoo = PRESENCE_PATH.node(MIN_MAX_LIST_QNAME).node(mapEntryPath2);
359
360         mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
361
362         final YangInstanceIdentifier minMaxLeafBar = PRESENCE_PATH.node(MIN_MAX_LIST_QNAME).node(mapEntryPath2);
363
364         mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "baz");
365
366         final YangInstanceIdentifier minMaxLeafBaz = PRESENCE_PATH.node(MIN_MAX_LIST_QNAME).node(mapEntryPath2);
367
368         modificationTree.write(PRESENCE_PATH, ImmutableNodes.containerNode(PRESENCE_QNAME));
369         modificationTree.write(PRESENCE_MIN_MAX_LIST_PATH, mapNodeFooWithNodes);
370         modificationTree.merge(PRESENCE_MIN_MAX_LIST_PATH, mapNodeBar);
371         modificationTree.merge(PRESENCE_MIN_MAX_LIST_PATH, mapNodeBaz);
372         modificationTree.delete(minMaxLeafFoo);
373         modificationTree.delete(minMaxLeafBar);
374         modificationTree.delete(minMaxLeafBaz);
375
376         try {
377             // Unlike minMaxListDeleteTest(), presence container enforces the list to be present
378             modificationTree.ready();
379             fail("Should have failed with IAE");
380         } catch (IllegalArgumentException e) {
381             assertEquals("Node (urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
382                     + "revision=2015-02-02)presence is missing mandatory descendant "
383                     + "/(urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model?"
384                     + "revision=2015-02-02)min-max-list", e.getMessage());
385         }
386     }
387
388     @Test
389     public void minMaxListNoMinMaxDeleteTest() throws DataValidationFailedException {
390         final MapEntryNode fooEntryNoMinMaxNode =
391                 ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME_NO_MINMAX, MIN_MAX_KEY_LEAF_QNAME, "foo");
392         final SystemMapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
393                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME_NO_MINMAX))
394                 .withChild(fooEntryNoMinMaxNode).build();
395
396         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
397
398         Map<QName, Object> key = new HashMap<>();
399         key.put(MIN_MAX_KEY_LEAF_QNAME, "foo");
400
401         NodeIdentifierWithPredicates mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME_NO_MINMAX, key);
402
403         final YangInstanceIdentifier minMaxLeafFoo = MASTER_CONTAINER_PATH
404                 .node(MIN_MAX_LIST_QNAME_NO_MINMAX).node(mapEntryPath2);
405
406         key.clear();
407         key.put(MIN_MAX_KEY_LEAF_QNAME, "non-existing-leaf");
408
409         mapEntryPath2 = NodeIdentifierWithPredicates.of(MIN_MAX_LIST_QNAME_NO_MINMAX, key);
410
411         YangInstanceIdentifier minMaxLeafNel = YangInstanceIdentifier.builder(MASTER_CONTAINER_PATH)
412                 .node(MIN_MAX_LIST_QNAME_NO_MINMAX).node(mapEntryPath2).build();
413
414         modificationTree.write(MIN_MAX_LIST_NO_MINMAX_PATH, mapNode1);
415         modificationTree.delete(minMaxLeafFoo);
416         modificationTree.delete(minMaxLeafNel);
417
418         modificationTree.ready();
419
420         inMemoryDataTree.validate(modificationTree);
421         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
422         inMemoryDataTree.commit(prepare);
423
424         final DataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
425         final Optional<NormalizedNode> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_NO_MINMAX_PATH);
426
427         // Empty list should have disappeared
428         assertFalse(minMaxListRead.isPresent());
429     }
430
431     private static void testLoop(final DataTreeSnapshot snapshot, final String first, final String second) {
432         Optional<NormalizedNode> minMaxListRead = snapshot.readNode(MIN_MAX_LIST_PATH);
433         assertTrue(minMaxListRead.isPresent());
434         assertEquals(2, ((NormalizedNodeContainer<?>) minMaxListRead.get()).size());
435
436         for (Object collectionChild : (Collection<?>) minMaxListRead.get().body()) {
437             if (collectionChild.toString().contains(first)) {
438                 assertTrue(collectionChild.toString().contains(first));
439             } else {
440                 assertTrue(collectionChild.toString().contains(second));
441             }
442         }
443     }
444 }