1f772060f851db3905914db0caca6aea08178c92
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / leafref / YT821Test.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 com.google.common.collect.ImmutableSet;
11 import org.junit.AfterClass;
12 import org.junit.Before;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30
31 public class YT821Test {
32     private static final QName ROOT = QName.create("urn:opendaylight:params:xml:ns:yang:foo", "2018-07-18", "root");
33     private static final QName FOO = QName.create(ROOT, "foo");
34     private static final QName BAR = QName.create(ROOT, "bar");
35     private static final QName NAME = QName.create(ROOT, "name");
36     private static final QName CONTAINER_IN_LIST = QName.create(ROOT, "container-in-list");
37     private static final QName REF_FROM_AUG = QName.create(ROOT, "ref-from-aug");
38     private static final QName CONTAINER_FROM_AUG = QName.create(ROOT, "container-from-aug");
39     private static final QName REF_IN_CONTAINER = QName.create(ROOT, "ref-in-container");
40     private static final YangInstanceIdentifier ROOT_ID = YangInstanceIdentifier.of(ROOT);
41
42     private static SchemaContext schemaContext;
43     private static LeafRefContext leafRefContext;
44
45     private DataTree dataTree;
46
47     @BeforeClass
48     public static void beforeClass() {
49         schemaContext = YangParserTestUtils.parseYangResource("/yt821.yang");
50         leafRefContext = LeafRefContext.create(schemaContext);
51     }
52
53     @AfterClass
54     public static void afterClass() {
55         schemaContext = null;
56         leafRefContext = null;
57     }
58
59     @Before
60     public void before() {
61         dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
62     }
63
64     @Test
65     public void testValidRefFromAugmentation() throws Exception {
66         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
67         writeModification.write(ROOT_ID, refFromAug("foo1"));
68         writeModification.ready();
69         final DataTreeCandidate writeContributorsCandidate = dataTree.prepare(writeModification);
70
71         LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
72         dataTree.commit(writeContributorsCandidate);
73     }
74
75     @Test(expected = LeafRefDataValidationFailedException.class)
76     public void testInvalidRefFromAugmentation() throws Exception {
77         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
78         writeModification.write(ROOT_ID, refFromAug("foo2"));
79         writeModification.ready();
80         final DataTreeCandidate writeContributorsCandidate = dataTree.prepare(writeModification);
81
82         LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
83     }
84
85     @Test
86     public void testValidRefInContainerFromAugmentation() throws Exception {
87         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
88         writeModification.write(ROOT_ID, refInContainer("foo1"));
89         writeModification.ready();
90         final DataTreeCandidate writeContributorsCandidate = dataTree.prepare(writeModification);
91
92         LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
93         dataTree.commit(writeContributorsCandidate);
94     }
95
96     @Test(expected = LeafRefDataValidationFailedException.class)
97     public void testInvalidRefInContainerFromAugmentation() throws Exception {
98         final DataTreeModification writeModification = dataTree.takeSnapshot().newModification();
99         writeModification.write(ROOT_ID, refInContainer("foo2"));
100         writeModification.ready();
101         final DataTreeCandidate writeContributorsCandidate = dataTree.prepare(writeModification);
102
103         LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
104     }
105
106     private static ContainerNode refFromAug(final String refValue) {
107         return Builders.containerBuilder()
108                 .withNodeIdentifier(new NodeIdentifier(ROOT))
109                 .withChild(Builders.mapBuilder()
110                     .withNodeIdentifier(new NodeIdentifier(FOO))
111                     .withChild(Builders.mapEntryBuilder()
112                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(FOO, NAME, "foo1"))
113                         .withChild(ImmutableNodes.leafNode(NAME, "foo1"))
114                         .build())
115                     .build())
116                 .withChild(Builders.mapBuilder()
117                     .withNodeIdentifier(new NodeIdentifier(BAR))
118                     .withChild(Builders.mapEntryBuilder()
119                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(BAR, NAME, "bar1"))
120                         .withChild(ImmutableNodes.leafNode(NAME, "bar1"))
121                         .withChild(Builders.containerBuilder()
122                             .withNodeIdentifier(new NodeIdentifier(CONTAINER_IN_LIST))
123                             .withChild(Builders.augmentationBuilder()
124                                 .withNodeIdentifier(AugmentationIdentifier.create(ImmutableSet.of(REF_FROM_AUG)))
125                                 .withChild(ImmutableNodes.leafNode(REF_FROM_AUG, refValue))
126                                 .build())
127                             .build())
128                         .build())
129                     .build())
130                 .build();
131     }
132
133     private static ContainerNode refInContainer(final String refValue) {
134         return Builders.containerBuilder()
135                 .withNodeIdentifier(new NodeIdentifier(ROOT))
136                 .withChild(Builders.mapBuilder()
137                     .withNodeIdentifier(new NodeIdentifier(FOO))
138                     .withChild(Builders.mapEntryBuilder()
139                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(FOO, NAME, "foo1"))
140                         .withChild(ImmutableNodes.leafNode(NAME, "foo1"))
141                         .build())
142                     .build())
143                 .withChild(Builders.mapBuilder()
144                     .withNodeIdentifier(new NodeIdentifier(BAR))
145                     .withChild(Builders.mapEntryBuilder()
146                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(BAR, NAME, "bar1"))
147                         .withChild(ImmutableNodes.leafNode(NAME, "bar1"))
148                         .withChild(Builders.augmentationBuilder()
149                             .withNodeIdentifier(AugmentationIdentifier.create(ImmutableSet.of(CONTAINER_FROM_AUG)))
150                             .withChild(Builders.containerBuilder()
151                                 .withNodeIdentifier(new NodeIdentifier(CONTAINER_FROM_AUG))
152                                 .withChild(ImmutableNodes.leafNode(REF_IN_CONTAINER, refValue))
153                                 .build())
154                             .build())
155                         .build())
156                     .build())
157                 .build();
158     }
159 }