Populate data/ hierarchy
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / YT891Test.java
1 /*
2  * Copyright (c) 2018 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;
9
10 import org.junit.AfterClass;
11 import org.junit.Before;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
23 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 public class YT891Test {
30     private static final QName FOO_TOP = QName.create("urn:opendaylight:params:xml:ns:yang:foo", "2018-07-27",
31         "foo-top");
32     private static final QName CONTAINER_IN_LIST = QName.create(FOO_TOP, "container-in-list");
33     private static final QName LIST_IN_GROUPING = QName.create(FOO_TOP, "list-in-grouping");
34     private static final QName NAME = QName.create(FOO_TOP, "name");
35     private static final QName REF = QName.create(FOO_TOP, "ref");
36     private static final YangInstanceIdentifier FOO_TOP_ID = YangInstanceIdentifier.of(FOO_TOP);
37     private static final QName BAZ_TOP = QName.create("urn:opendaylight:params:xml:ns:yang:baz", "2018-07-27",
38         "baz-top");
39     private static final QName BAZ_NAME = QName.create(BAZ_TOP, "name");
40     private static final QName LIST_IN_CONTAINER = QName.create(BAZ_TOP, "list-in-container");
41     private static final YangInstanceIdentifier BAZ_TOP_ID = YangInstanceIdentifier.of(BAZ_TOP);
42
43     private static EffectiveModelContext schemaContext;
44     private static LeafRefContext leafRefContext;
45
46     private DataTree dataTree;
47
48     @Before
49     public void before() {
50         dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
51     }
52
53     @BeforeClass
54     public static void beforeClass() {
55         schemaContext = YangParserTestUtils.parseYangResourceDirectory("/yt891");
56         leafRefContext = LeafRefContext.create(schemaContext);
57     }
58
59     @AfterClass
60     public static void afterClass() {
61         schemaContext = null;
62         leafRefContext = null;
63     }
64
65     @Test
66     public void testValid() throws Exception {
67         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
68         writeModification.write(FOO_TOP_ID, fooTopWithList("name1"));
69         writeModification.ready();
70         final DataTreeCandidate writeContributorsCandidate = dataTree.prepare(writeModification);
71         LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
72         dataTree.commit(writeContributorsCandidate);
73     }
74
75     @Test(expected = LeafRefDataValidationFailedException.class)
76     public void testInvalid() throws Exception {
77         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
78         writeModification.write(FOO_TOP_ID, fooTopWithList("name2"));
79         writeModification.ready();
80         LeafRefValidation.validate(dataTree.prepare(writeModification), leafRefContext);
81     }
82
83     @Test
84     public void testGroupingWithLeafrefValid() throws Exception {
85         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
86         writeModification.write(BAZ_TOP_ID, bazTop());
87         writeModification.write(FOO_TOP_ID, fooTopWithRef("name1"));
88         writeModification.ready();
89         LeafRefValidation.validate(dataTree.prepare(writeModification), leafRefContext);
90     }
91
92     @Test(expected = LeafRefDataValidationFailedException.class)
93     public void testGroupingWithLeafrefInvalid() throws Exception {
94         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
95         writeModification.write(BAZ_TOP_ID, bazTop());
96         writeModification.write(FOO_TOP_ID, fooTopWithRef("name3"));
97         writeModification.ready();
98         LeafRefValidation.validate(dataTree.prepare(writeModification), leafRefContext);
99     }
100
101     private static ContainerNode fooTopWithList(final String refValue) {
102         return Builders.containerBuilder()
103                 .withNodeIdentifier(new NodeIdentifier(FOO_TOP))
104                 .withChild(Builders.mapBuilder()
105                     .withNodeIdentifier(new NodeIdentifier(LIST_IN_GROUPING))
106                     .withChild(Builders.mapEntryBuilder()
107                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(LIST_IN_GROUPING, NAME, "name1"))
108                         .withChild(ImmutableNodes.leafNode(NAME, "name1"))
109                         .withChild(Builders.containerBuilder()
110                             .withNodeIdentifier(new NodeIdentifier(CONTAINER_IN_LIST))
111                             .withChild(ImmutableNodes.leafNode(NAME, refValue))
112                             .build())
113                         .build())
114                     .build())
115                 .build();
116     }
117
118     private static ContainerNode fooTopWithRef(final String refValue) {
119         return Builders.containerBuilder()
120                 .withNodeIdentifier(new NodeIdentifier(FOO_TOP))
121                 .withChild(ImmutableNodes.leafNode(REF, refValue))
122                 .build();
123     }
124
125     private static ContainerNode bazTop() {
126         return Builders.containerBuilder()
127                 .withNodeIdentifier(new NodeIdentifier(BAZ_TOP))
128                 .withChild(Builders.mapBuilder()
129                     .withNodeIdentifier(new NodeIdentifier(LIST_IN_CONTAINER))
130                     .withChild(Builders.mapEntryBuilder()
131                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(LIST_IN_CONTAINER, BAZ_NAME, "name1"))
132                         .withChild(ImmutableNodes.leafNode(BAZ_NAME, "name1"))
133                         .build())
134                     .build())
135                 .build();
136     }
137 }