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