Reduce use of getChildByName()
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / context / DataTreeCandidateValidatorTest.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.yangtools.yang.data.impl.leafref.context;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.Set;
16 import org.apache.log4j.BasicConfigurator;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
37 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
38 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefDataValidationFailedException;
39 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidation;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
47 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.Module;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class DataTreeCandidateValidatorTest {
56
57     private static SchemaContext context;
58     private static Module valModule;
59     private static QNameModule valModuleQname;
60     private static LeafRefContext rootLeafRefContext;
61     public static DataTree inMemoryDataTree;
62
63     private static QName odl;
64     private static QName project;
65     private static QName name;
66     private static QName desc;
67     private static QName lead;
68     private static QName owner;
69     private static QName odlContributor;
70     private static QName contributor;
71     private static QName odlProjectName;
72     private static QName odlProjectDesc;
73     private static QName login;
74     private static QName contributorName;
75     private static QName l1;
76     private static QName l2;
77     private static QName con1;
78     private static QName ch1;
79     private static QName ch2;
80     private static QName leafrefInChoice;
81     private static QName listInChoice;
82
83     private static QName leafrefInChoiceToChoice;
84     private static QName con3;
85     private static QName list3InChoice;
86     private static QName l3;
87     private static QName choiceInCon3;
88
89     private static QName listInChoiceKey;
90     private static QName k;
91
92     private static QName leafrefLeafList;
93
94     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest.class);
95     private static final String NEW_LINE = System.getProperty("line.separator");
96
97     static {
98         BasicConfigurator.configure();
99     }
100
101     @BeforeClass
102     public static void init()  {
103         context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
104
105         final Set<Module> modules = context.getModules();
106         for (final Module module : modules) {
107             if (module.getName().equals("leafref-validation")) {
108                 valModule = module;
109             }
110         }
111
112         valModuleQname = valModule.getQNameModule();
113         rootLeafRefContext = LeafRefContext.create(context);
114
115         odl = QName.create(valModuleQname, "odl-project");
116         project = QName.create(valModuleQname, "project");
117         name = QName.create(valModuleQname, "name");
118         desc = QName.create(valModuleQname, "desc");
119         lead = QName.create(valModuleQname, "project-lead");
120         owner = QName.create(valModuleQname, "project-owner");
121
122         odlContributor = QName.create(valModuleQname, "odl-contributor");
123         contributor = QName.create(valModuleQname, "contributor");
124         odlProjectName = QName.create(valModuleQname, "odl-project-name");
125         login = QName.create(valModuleQname, "login");
126         contributorName = QName.create(valModuleQname, "contributor-name");
127
128         con1 = QName.create(valModuleQname, "con1");
129         l1 = QName.create(valModuleQname, "l1");
130         l2 = QName.create(valModuleQname, "l2");
131         odlProjectDesc = QName.create(valModuleQname, "odl-project-desc");
132
133         ch1 = QName.create(valModuleQname, "ch1");
134         ch2 = QName.create(valModuleQname, "ch2");
135         leafrefInChoice = QName.create(valModuleQname, "leafref-in-choice");
136         listInChoice = QName.create(valModuleQname, "list-in-choice");
137
138         leafrefInChoiceToChoice = QName.create(valModuleQname,
139                 "leafref-in-choice-to-choice");
140         con3 = QName.create(valModuleQname, "con3");
141         list3InChoice = QName.create(valModuleQname, "list3-in-choice");
142         l3 = QName.create(valModuleQname, "l3");
143         choiceInCon3 = QName.create(valModuleQname, "choice-in-con3");
144
145         listInChoiceKey = QName.create(valModuleQname, "list-in-choice-key");
146         k = QName.create(valModuleQname, "k");
147
148         leafrefLeafList = QName.create(valModuleQname, "leafref-leaf-list");
149
150         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
151
152         final DataTreeModification initialDataTreeModification = inMemoryDataTree
153                 .takeSnapshot().newModification();
154
155         final ContainerSchemaNode odlProjContSchemaNode = (ContainerSchemaNode) valModule.findDataChildByName(odl)
156                 .get();
157
158         final ContainerNode odlProjectContainer = createOdlContainer(odlProjContSchemaNode);
159
160         final YangInstanceIdentifier path = YangInstanceIdentifier.of(odl);
161         initialDataTreeModification.write(path, odlProjectContainer);
162         initialDataTreeModification.ready();
163
164         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
165                 .prepare(initialDataTreeModification);
166         inMemoryDataTree.commit(writeContributorsCandidate);
167     }
168
169     @AfterClass
170     public static void cleanup() {
171         inMemoryDataTree = null;
172         rootLeafRefContext = null;
173         valModule = null;
174         context = null;
175     }
176
177     @Test
178     public void dataTreeCanditateValidationTest() {
179         write();
180
181         write2();
182
183         delete();
184
185         writeContributors();
186
187         writeMapEntry();
188
189         writeIntoMapEntry();
190     }
191
192     private static void writeContributors() {
193
194         final ContainerSchemaNode contributorContSchemaNode = (ContainerSchemaNode) valModule
195                 .findDataChildByName(odlContributor).get();
196
197         final ContainerNode contributorContainer = createBasicContributorContainer(contributorContSchemaNode);
198
199         final YangInstanceIdentifier contributorPath = YangInstanceIdentifier
200                 .of(odlContributor);
201         final DataTreeModification writeModification = inMemoryDataTree
202                 .takeSnapshot().newModification();
203         writeModification.write(contributorPath, contributorContainer);
204         writeModification.ready();
205
206         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
207                 .prepare(writeModification);
208
209         LOG.debug("*************************");
210         LOG.debug("Before write of contributors: ");
211         LOG.debug("*************************");
212         LOG.debug(inMemoryDataTree.toString());
213
214         boolean exception = false;
215         try {
216             LeafRefValidation.validate(writeContributorsCandidate,
217                     rootLeafRefContext);
218         } catch (final LeafRefDataValidationFailedException e) {
219             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
220             assertEquals(3, e.getValidationsErrorsCount());
221             exception = true;
222         }
223
224         inMemoryDataTree.commit(writeContributorsCandidate);
225
226         LOG.debug("*************************");
227         LOG.debug("After write of contributors: ");
228         LOG.debug("*************************");
229         LOG.debug(inMemoryDataTree.toString());
230
231         assertTrue(exception);
232
233     }
234
235     private static void writeIntoMapEntry() {
236
237         final Map<QName, Object> keys = new HashMap<>();
238         keys.put(name, "New Project");
239         final NodeIdentifierWithPredicates mapEntryPath = new NodeIdentifierWithPredicates(
240                 project, keys);
241
242         final YangInstanceIdentifier leaderPath = YangInstanceIdentifier
243                 .of(odl).node(project).node(mapEntryPath).node(lead);
244
245         final LeafNode<String> leader = ImmutableNodes.leafNode(lead,
246                 "Updated leader");
247
248         final DataTreeModification writeModification = inMemoryDataTree
249                 .takeSnapshot().newModification();
250         writeModification.write(leaderPath, leader);
251         writeModification.ready();
252
253         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
254                 .prepare(writeModification);
255
256         LOG.debug("*************************");
257         LOG.debug("Before write into map entry (update of leader name): ");
258         LOG.debug("*************************");
259         LOG.debug(inMemoryDataTree.toString());
260
261         boolean exception = false;
262         try {
263             LeafRefValidation.validate(writeContributorsCandidate,
264                     rootLeafRefContext);
265         } catch (final LeafRefDataValidationFailedException e) {
266             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
267             assertEquals(1, e.getValidationsErrorsCount());
268             exception = true;
269         }
270
271         inMemoryDataTree.commit(writeContributorsCandidate);
272
273         LOG.debug("*************************");
274         LOG.debug("After write into map entry (update of leader name): ");
275         LOG.debug("*************************");
276         LOG.debug(inMemoryDataTree.toString());
277
278         assertTrue(exception);
279
280     }
281
282     private static void writeMapEntry() {
283
284         final Map<QName, Object> keys = new HashMap<>();
285         keys.put(name, "New Project");
286         final NodeIdentifierWithPredicates mapEntryPath = new NodeIdentifierWithPredicates(
287                 project, keys);
288
289         final YangInstanceIdentifier newOdlProjectMapEntryPath = YangInstanceIdentifier
290                 .of(odl).node(project).node(mapEntryPath);
291
292         final ContainerSchemaNode odlProjContSchemaNode = (ContainerSchemaNode) valModule.findDataChildByName(odl)
293                 .get();
294         final ListSchemaNode projListSchemaNode = (ListSchemaNode) odlProjContSchemaNode.findDataChildByName(project)
295                 .get();
296         final MapEntryNode newProjectMapEntry = createProjectListEntry(
297                 "New Project", "New Project description ...",
298                 "Leader of New Project", "Owner of New Project",
299                 projListSchemaNode);
300
301         final DataTreeModification writeModification = inMemoryDataTree
302                 .takeSnapshot().newModification();
303         writeModification.write(newOdlProjectMapEntryPath, newProjectMapEntry);
304         writeModification.ready();
305
306         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
307                 .prepare(writeModification);
308
309         LOG.debug("*************************");
310         LOG.debug("Before map entry write: ");
311         LOG.debug("*************************");
312         LOG.debug(inMemoryDataTree.toString());
313
314         boolean exception = false;
315         try {
316             LeafRefValidation.validate(writeContributorsCandidate,
317                     rootLeafRefContext);
318         } catch (final LeafRefDataValidationFailedException e) {
319             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
320             assertEquals(2, e.getValidationsErrorsCount());
321             exception = true;
322         }
323
324         inMemoryDataTree.commit(writeContributorsCandidate);
325
326         LOG.debug("*************************");
327         LOG.debug("After map entry write: ");
328         LOG.debug("*************************");
329         LOG.debug(inMemoryDataTree.toString());
330
331         assertTrue(exception);
332
333     }
334
335     private static void write() {
336
337         final ContainerSchemaNode contributorContSchemaNode = (ContainerSchemaNode) valModule
338                 .findDataChildByName(odlContributor).get();
339
340         final ContainerNode contributorContainer = createContributorContainer(contributorContSchemaNode);
341
342         final YangInstanceIdentifier contributorPath = YangInstanceIdentifier
343                 .of(odlContributor);
344         final DataTreeModification writeModification = inMemoryDataTree
345                 .takeSnapshot().newModification();
346         writeModification.write(contributorPath, contributorContainer);
347
348         writeModification.write(YangInstanceIdentifier.of(l1),
349                 ImmutableNodes.leafNode(l1, "Leafref l1 under the root"));
350         writeModification
351                 .write(YangInstanceIdentifier.of(l2), ImmutableNodes.leafNode(
352                         l2, "Leafref target l2 under the root"));
353
354         writeModification.ready();
355         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
356                 .prepare(writeModification);
357
358         LOG.debug("*************************");
359         LOG.debug("Before write: ");
360         LOG.debug("*************************");
361         LOG.debug(inMemoryDataTree.toString());
362
363         boolean exception = false;
364         try {
365             LeafRefValidation.validate(writeContributorsCandidate,
366                     rootLeafRefContext);
367         } catch (final LeafRefDataValidationFailedException e) {
368             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
369             assertEquals(12, e.getValidationsErrorsCount());
370             exception = true;
371         }
372
373         inMemoryDataTree.commit(writeContributorsCandidate);
374
375         LOG.debug("*************************");
376         LOG.debug("After write: ");
377         LOG.debug("*************************");
378         LOG.debug(inMemoryDataTree.toString());
379
380         assertTrue(exception);
381     }
382
383     private static void write2() {
384
385         final ContainerSchemaNode odlCon = (ContainerSchemaNode) valModule.findDataChildByName(odl).get();
386         final ContainerSchemaNode con1Con = (ContainerSchemaNode) odlCon.findDataChildByName(con1).get();
387         final LeafNode<String> l1Leaf = ImmutableNodes.leafNode(l1, "l1 value");
388         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders
389                 .containerBuilder(con1Con);
390         containerBuilder.addChild(l1Leaf);
391         final ContainerNode con1Node = containerBuilder.build();
392
393         final YangInstanceIdentifier con1Path = YangInstanceIdentifier.of(odl)
394                 .node(con1);
395         final DataTreeModification writeModification = inMemoryDataTree
396                 .takeSnapshot().newModification();
397         writeModification.write(con1Path, con1Node);
398
399         final ChoiceNode choiceNode = createChoiceNode();
400         final YangInstanceIdentifier choicePath = YangInstanceIdentifier
401                 .of(odl).node(ch1);
402         writeModification.write(choicePath, choiceNode);
403
404         final ContainerNode con3Node = createCon3Node();
405         final YangInstanceIdentifier con3Path = YangInstanceIdentifier.of(odl)
406                 .node(con3);
407         writeModification.write(con3Path, con3Node);
408
409         final LeafSetNode<?> leafListNode = createLeafRefLeafListNode();
410         final YangInstanceIdentifier leafListPath = YangInstanceIdentifier.of(
411                 odl).node(leafrefLeafList);
412         writeModification.write(leafListPath, leafListNode);
413         writeModification.ready();
414
415         final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree
416                 .prepare(writeModification);
417
418         LOG.debug("*************************");
419         LOG.debug("Before write2: ");
420         LOG.debug("*************************");
421         LOG.debug(inMemoryDataTree.toString());
422
423         boolean exception = false;
424         try {
425             LeafRefValidation.validate(writeContributorsCandidate,
426                     rootLeafRefContext);
427         } catch (final LeafRefDataValidationFailedException e) {
428             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
429             assertEquals(6, e.getValidationsErrorsCount());
430             exception = true;
431         }
432
433         assertTrue(exception);
434
435         inMemoryDataTree.commit(writeContributorsCandidate);
436
437         LOG.debug("*************************");
438         LOG.debug("After write2: ");
439         LOG.debug("*************************");
440         LOG.debug(inMemoryDataTree.toString());
441
442     }
443
444     private static LeafSetNode<?> createLeafRefLeafListNode() {
445
446         final ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders
447                 .leafSetBuilder();
448         leafSetBuilder.withNodeIdentifier(new NodeIdentifier(leafrefLeafList));
449
450         leafSetBuilder.addChild(createLeafSetEntry(leafrefLeafList, "k1"));
451         leafSetBuilder.addChild(createLeafSetEntry(leafrefLeafList, "k2"));
452         leafSetBuilder.addChild(createLeafSetEntry(leafrefLeafList, "k3"));
453
454         return leafSetBuilder.build();
455     }
456
457     private static ContainerNode createCon3Node() {
458
459         final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders
460                 .mapBuilder();
461         mapBuilder.withNodeIdentifier(new NodeIdentifier(list3InChoice));
462
463         mapBuilder.addChild(createList3Entry("k1", "val1", "valA", "valX"));
464         mapBuilder.addChild(createList3Entry("k2", "val2", "valB", "valY"));
465
466         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choiceBuilder = Builders
467                 .choiceBuilder();
468         choiceBuilder.withNodeIdentifier(new NodeIdentifier(choiceInCon3));
469
470         choiceBuilder.addChild(mapBuilder.build());
471
472         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders
473                 .containerBuilder();
474         containerBuilder.withNodeIdentifier(new NodeIdentifier(con3));
475
476         containerBuilder.addChild(choiceBuilder.build());
477
478         return containerBuilder.build();
479     }
480
481     private static MapEntryNode createList3Entry(final String keyVal,
482             final String l3Val1, final String l3Val2, final String l3Val3) {
483         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = Builders
484                 .mapEntryBuilder();
485         mapEntryBuilder.withNodeIdentifier(new NodeIdentifierWithPredicates(
486                 list3InChoice, k, keyVal));
487
488         final ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder = Builders
489                 .leafSetBuilder();
490         leafSetBuilder.withNodeIdentifier(new NodeIdentifier(l3));
491
492         leafSetBuilder.addChild(createLeafSetEntry(l3, l3Val1));
493         leafSetBuilder.addChild(createLeafSetEntry(l3, l3Val2));
494         leafSetBuilder.addChild(createLeafSetEntry(l3, l3Val3));
495
496         mapEntryBuilder.addChild(ImmutableNodes.leafNode(k, keyVal));
497         mapEntryBuilder.addChild(leafSetBuilder.build());
498
499         return mapEntryBuilder.build();
500     }
501
502     private static LeafSetEntryNode<Object> createLeafSetEntry(
503             final QName qname, final String val) {
504         final NormalizedNodeBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder = Builders
505                 .leafSetEntryBuilder();
506         leafSetEntryBuilder.withNodeIdentifier(new NodeWithValue<>(qname, val));
507         leafSetEntryBuilder.withValue(val);
508         return leafSetEntryBuilder.build();
509     }
510
511     private static ChoiceNode createChoiceNode() {
512
513         final CollectionNodeBuilder<MapEntryNode, MapNode> listInChoiceBuilder = Builders
514                 .mapBuilder();
515         listInChoiceBuilder
516                 .withNodeIdentifier(new NodeIdentifier(listInChoice));
517
518         listInChoiceBuilder.addChild(createListInChoiceEntry("key1",
519                 "leafref-in-choice value", "val1"));
520         listInChoiceBuilder.addChild(createListInChoiceEntry("key2",
521                 "l1 value", "val2"));
522         listInChoiceBuilder.addChild(createListInChoiceEntry("key3",
523                 "l1 value", "val3"));
524
525         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice2Builder = Builders
526                 .choiceBuilder();
527         choice2Builder.withNodeIdentifier(new NodeIdentifier(ch2));
528
529         choice2Builder.addChild(listInChoiceBuilder.build());
530
531         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choiceBuilder = Builders
532                 .choiceBuilder();
533         choiceBuilder.withNodeIdentifier(new NodeIdentifier(ch1));
534         choiceBuilder.addChild(choice2Builder.build());
535
536         return choiceBuilder.build();
537     }
538
539     private static MapEntryNode createListInChoiceEntry(final String keyVal,
540             final String leafrefInChoiceVal,
541             final String leafrefInChoiceToChoiceVal) {
542
543         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder = Builders
544                 .mapEntryBuilder();
545
546         mapEntryBuilder.withNodeIdentifier(new NodeIdentifierWithPredicates(
547                 listInChoice, listInChoiceKey, keyVal));
548
549         mapEntryBuilder.addChild(ImmutableNodes.leafNode(listInChoiceKey,
550                 keyVal));
551         mapEntryBuilder.addChild(ImmutableNodes.leafNode(leafrefInChoice,
552                 leafrefInChoiceVal));
553         mapEntryBuilder.addChild(ImmutableNodes.leafNode(
554                 leafrefInChoiceToChoice, leafrefInChoiceToChoiceVal));
555
556         return mapEntryBuilder.build();
557     }
558
559     private static void delete() {
560
561         final YangInstanceIdentifier contributorPath = YangInstanceIdentifier
562                 .of(odlContributor);
563         final DataTreeModification delete = inMemoryDataTree.takeSnapshot()
564                 .newModification();
565         delete.delete(contributorPath);
566         delete.ready();
567
568         final DataTreeCandidate deleteContributorsCanditate = inMemoryDataTree
569                 .prepare(delete);
570
571         LOG.debug("*************************");
572         LOG.debug("Before delete: ");
573         LOG.debug("*************************");
574         LOG.debug(inMemoryDataTree.toString());
575
576         boolean exception = false;
577         try {
578             LeafRefValidation.validate(deleteContributorsCanditate,
579                     rootLeafRefContext);
580         } catch (final LeafRefDataValidationFailedException e) {
581             LOG.debug("All validation errors:{}{}", NEW_LINE, e.getMessage());
582             assertEquals(6, e.getValidationsErrorsCount());
583             exception = true;
584         }
585
586         assertTrue(exception);
587
588         inMemoryDataTree.commit(deleteContributorsCanditate);
589
590         LOG.debug("*************************");
591         LOG.debug("After delete: ");
592         LOG.debug("*************************");
593         LOG.debug(inMemoryDataTree.toString());
594
595     }
596
597     private static ContainerNode createContributorContainer(
598             final ContainerSchemaNode contributorContSchemaNode) {
599
600         final ListSchemaNode contributorListSchemaNode = (ListSchemaNode) contributorContSchemaNode
601                 .findDataChildByName(contributor).get();
602
603         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contributorContainerBldr = Builders
604                 .containerBuilder(contributorContSchemaNode);
605
606         final MapNode contributorMap = createContributorList(contributorListSchemaNode);
607         contributorContainerBldr.addChild(contributorMap);
608
609         final ContainerNode contributorContainer = contributorContainerBldr
610                 .build();
611
612         return contributorContainer;
613
614     }
615
616     private static MapNode createContributorList(
617             final ListSchemaNode contributorListSchemaNode) {
618
619         final CollectionNodeBuilder<MapEntryNode, MapNode> contributorMapBldr = Builders
620                 .mapBuilder(contributorListSchemaNode);
621
622         final MapEntryNode contributorMapEntry1 = createContributorListEntry(
623                 "Leader of Yangtools", "Yangtools Leader name", "Yangtools",
624                 "Yangtools description ...", contributorListSchemaNode);
625         final MapEntryNode contributorMapEntry2 = createContributorListEntry(
626                 "Leader of MD-SAL", "MD-SAL Leader name", "MD-SAL",
627                 "MD-SAL description ...", contributorListSchemaNode);
628         final MapEntryNode contributorMapEntry3 = createContributorListEntry(
629                 "Leader of Controller", "Controller Leader name", "Controller",
630                 "Controller description ...", contributorListSchemaNode);
631
632         final MapEntryNode contributorMapEntry4 = createContributorListEntry(
633                 "jdoe", "John Doe", "MD-SAL", "Yangtools description ...",
634                 contributorListSchemaNode);
635
636         final MapEntryNode contributorMapEntry5 = createContributorListEntry(
637                 "foo", "foo name", "Controller", "MD-SAL description ...",
638                 contributorListSchemaNode);
639
640         final MapEntryNode contributorMapEntry6 = createContributorListEntry(
641                 "bar", "bar name", "Yangtools", "Controller description ...",
642                 contributorListSchemaNode);
643
644         final MapEntryNode contributorMapEntry7 = createContributorListEntry(
645                 "baz", "baz name", "Unknown Project",
646                 "Unknown Project description ...", contributorListSchemaNode);
647
648         final MapEntryNode contributorMapEntry8 = createContributorListEntry(
649                 "pk", "pk name", "Unknown Project 2",
650                 "Controller description ...", contributorListSchemaNode);
651
652         contributorMapBldr.addChild(contributorMapEntry1);
653         contributorMapBldr.addChild(contributorMapEntry2);
654         contributorMapBldr.addChild(contributorMapEntry3);
655         contributorMapBldr.addChild(contributorMapEntry4);
656         contributorMapBldr.addChild(contributorMapEntry5);
657         contributorMapBldr.addChild(contributorMapEntry6);
658         contributorMapBldr.addChild(contributorMapEntry7);
659         contributorMapBldr.addChild(contributorMapEntry8);
660
661         final MapNode contributorMap = contributorMapBldr.build();
662
663         return contributorMap;
664
665     }
666
667     private static MapEntryNode createContributorListEntry(
668             final String loginVal, final String contributorNameVal,
669             final String odlProjectNameVal, final String odlProjectDescVal,
670             final ListSchemaNode contributorListSchemaNode) {
671
672         final LeafNode<String> loginLeaf = ImmutableNodes.leafNode(login, loginVal);
673         final LeafNode<String> contributorNameLeaf = ImmutableNodes.leafNode(contributorName, contributorNameVal);
674         final LeafNode<String> odlProjectNameLeafRef = ImmutableNodes.leafNode(odlProjectName, odlProjectNameVal);
675         final LeafNode<String> odlProjectDescLeafRef = ImmutableNodes.leafNode(odlProjectDesc, odlProjectDescVal);
676
677         return Builders.mapEntryBuilder(contributorListSchemaNode)
678                 .addChild(loginLeaf)
679                 .addChild(contributorNameLeaf)
680                 .addChild(odlProjectNameLeafRef)
681                 .addChild(odlProjectDescLeafRef)
682                 .build();
683     }
684
685     private static ContainerNode createOdlContainer(
686             final ContainerSchemaNode container) {
687
688         final ListSchemaNode projListSchemaNode = (ListSchemaNode) container.findDataChildByName(project).get();
689
690         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> odlProjectContainerBldr = Builders
691                 .containerBuilder(container);
692
693         final MapNode projectMap = createProjectList(projListSchemaNode);
694         odlProjectContainerBldr.addChild(projectMap);
695
696         final ContainerNode odlProjectContainer = odlProjectContainerBldr
697                 .build();
698
699         return odlProjectContainer;
700     }
701
702     private static MapNode createProjectList(
703             final ListSchemaNode projListSchemaNode) {
704
705         final CollectionNodeBuilder<MapEntryNode, MapNode> projectMapBldr = Builders
706                 .mapBuilder(projListSchemaNode);
707
708         final MapEntryNode projMapEntry1 = createProjectListEntry("Yangtools",
709                 "Yangtools description ...", "Leader of Yangtools",
710                 "Owner of Yangtools", projListSchemaNode);
711         final MapEntryNode projMapEntry2 = createProjectListEntry("MD-SAL",
712                 "MD-SAL description ...", "Leader of MD-SAL",
713                 "Owner of MD-SAL", projListSchemaNode);
714         final MapEntryNode projMapEntry3 = createProjectListEntry("Controller",
715                 "Controller description ...", "Leader of Controller",
716                 "Owner of Controller", projListSchemaNode);
717
718         projectMapBldr.addChild(projMapEntry1);
719         projectMapBldr.addChild(projMapEntry2);
720         projectMapBldr.addChild(projMapEntry3);
721
722         final MapNode projectMap = projectMapBldr.build();
723
724         return projectMap;
725     }
726
727     private static MapEntryNode createProjectListEntry(final String nameVal,
728             final String descVal, final String leadVal, final String ownerVal,
729             final ListSchemaNode projListSchemaNode) {
730
731         final LeafNode<String> nameLeaf = ImmutableNodes
732                 .leafNode(name, nameVal);
733         final LeafNode<String> descLeaf = ImmutableNodes
734                 .leafNode(desc, descVal);
735         final LeafNode<String> leadLeafRef = ImmutableNodes.leafNode(lead,
736                 leadVal);
737         final LeafNode<String> ownerLeafRef = ImmutableNodes.leafNode(owner,
738                 ownerVal);
739
740         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> projMapEntryBldr = Builders
741                 .mapEntryBuilder(projListSchemaNode);
742
743         projMapEntryBldr.addChild(nameLeaf);
744         projMapEntryBldr.addChild(descLeaf);
745         projMapEntryBldr.addChild(leadLeafRef);
746         projMapEntryBldr.addChild(ownerLeafRef);
747         final MapEntryNode projMapEntry = projMapEntryBldr.build();
748
749         return projMapEntry;
750     }
751
752     private static ContainerNode createBasicContributorContainer(
753             final ContainerSchemaNode contributorContSchemaNode) {
754
755         final ListSchemaNode contributorListSchemaNode = (ListSchemaNode) contributorContSchemaNode
756                 .findDataChildByName(contributor).get();
757
758         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contributorContainerBldr = Builders
759                 .containerBuilder(contributorContSchemaNode);
760
761         final MapNode contributorMap = createBasicContributorList(contributorListSchemaNode);
762         contributorContainerBldr.addChild(contributorMap);
763
764         final ContainerNode contributorContainer = contributorContainerBldr
765                 .build();
766
767         return contributorContainer;
768
769     }
770
771     private static MapNode createBasicContributorList(
772             final ListSchemaNode contributorListSchemaNode) {
773
774         final CollectionNodeBuilder<MapEntryNode, MapNode> contributorMapBldr = Builders
775                 .mapBuilder(contributorListSchemaNode);
776
777         final MapEntryNode contributorMapEntry1 = createContributorListEntry(
778                 "Leader of Yangtools", "Yangtools Leader name", "Yangtools",
779                 "Yangtools description ...", contributorListSchemaNode);
780         final MapEntryNode contributorMapEntry2 = createContributorListEntry(
781                 "Leader of MD-SAL", "MD-SAL Leader name", "MD-SAL",
782                 "MD-SAL description ...", contributorListSchemaNode);
783         final MapEntryNode contributorMapEntry3 = createContributorListEntry(
784                 "Leader of Controller", "Controller Leader name", "Controller",
785                 "Controller description ...", contributorListSchemaNode);
786
787         contributorMapBldr.addChild(contributorMapEntry1);
788         contributorMapBldr.addChild(contributorMapEntry2);
789         contributorMapBldr.addChild(contributorMapEntry3);
790
791         final MapNode contributorMap = contributorMapBldr.build();
792
793         return contributorMap;
794     }
795 }