Migrate yang-data-tree-ri to JUnit5
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / 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.tree.leafref;
9
10 import static org.junit.jupiter.api.Assertions.assertThrows;
11
12 import org.junit.jupiter.api.AfterAll;
13 import org.junit.jupiter.api.BeforeAll;
14 import org.junit.jupiter.api.BeforeEach;
15 import org.junit.jupiter.api.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.impl.schema.Builders;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28
29 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     @BeforeEach
49     void before() {
50         dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
51     }
52
53     @BeforeAll
54     static void beforeClass() {
55         schemaContext = YangParserTestUtils.parseYang("""
56             module bar {
57               namespace "urn:opendaylight:params:xml:ns:yang:bar";
58               prefix bar;
59               revision 2018-07-27;
60
61               grouping grouping-with-list {
62                 list list-in-grouping {
63                   key "name";
64                   leaf name {
65                     type leafref {
66                       path "../container-in-list/name";
67                     }
68                   }
69                   container container-in-list {
70                     leaf name {
71                       type string;
72                     }
73                   }
74                 }
75               }
76             }""", """
77             module baz {
78               namespace "urn:opendaylight:params:xml:ns:yang:baz";
79               prefix baz;
80               revision 2018-07-27;
81
82               grouping grouping-with-leafref {
83                 leaf ref {
84                   type leafref {
85                     path "/baz-top/list-in-container/name";
86                   }
87                 }
88               }
89
90               container baz-top {
91                 list list-in-container {
92                   key "name";
93                   leaf name {
94                     type string;
95                   }
96                 }
97               }
98             }""", """
99             module foo {
100               namespace "urn:opendaylight:params:xml:ns:yang:foo";
101               prefix foo;
102
103               import bar {
104                 prefix bar;
105               }
106
107               import baz {
108                 prefix baz;
109               }
110
111               revision 2018-07-27;
112
113               container foo-top {
114                 uses bar:grouping-with-list;
115                 uses baz:grouping-with-leafref;
116               }
117             }""");
118         leafRefContext = LeafRefContext.create(schemaContext);
119     }
120
121     @AfterAll
122     static void afterClass() {
123         schemaContext = null;
124         leafRefContext = null;
125     }
126
127     @Test
128     void testValid() throws Exception {
129         final var writeModification = dataTree.takeSnapshot().newModification();
130         writeModification.write(FOO_TOP_ID, fooTopWithList("name1"));
131         writeModification.ready();
132         final var writeContributorsCandidate = dataTree.prepare(writeModification);
133         LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
134         dataTree.commit(writeContributorsCandidate);
135     }
136
137     @Test
138     void testInvalid() throws Exception {
139         assertThrows(LeafRefDataValidationFailedException.class, () -> {
140             final var writeModification = dataTree.takeSnapshot().newModification();
141             writeModification.write(FOO_TOP_ID, fooTopWithList("name2"));
142             writeModification.ready();
143             LeafRefValidation.validate(dataTree.prepare(writeModification), leafRefContext);
144         });
145     }
146
147     @Test
148     void testGroupingWithLeafrefValid() throws Exception {
149         final var writeModification = dataTree.takeSnapshot().newModification();
150         writeModification.write(BAZ_TOP_ID, bazTop());
151         writeModification.write(FOO_TOP_ID, fooTopWithRef("name1"));
152         writeModification.ready();
153         LeafRefValidation.validate(dataTree.prepare(writeModification), leafRefContext);
154     }
155
156     @Test
157     void testGroupingWithLeafrefInvalid() throws Exception {
158         assertThrows(LeafRefDataValidationFailedException.class, () -> {
159             final var writeModification = dataTree.takeSnapshot().newModification();
160             writeModification.write(BAZ_TOP_ID, bazTop());
161             writeModification.write(FOO_TOP_ID, fooTopWithRef("name3"));
162             writeModification.ready();
163             LeafRefValidation.validate(dataTree.prepare(writeModification), leafRefContext);
164         });
165     }
166
167     private static ContainerNode fooTopWithList(final String refValue) {
168         return Builders.containerBuilder()
169                 .withNodeIdentifier(new NodeIdentifier(FOO_TOP))
170                 .withChild(Builders.mapBuilder()
171                     .withNodeIdentifier(new NodeIdentifier(LIST_IN_GROUPING))
172                     .withChild(Builders.mapEntryBuilder()
173                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(LIST_IN_GROUPING, NAME, "name1"))
174                         .withChild(ImmutableNodes.leafNode(NAME, "name1"))
175                         .withChild(Builders.containerBuilder()
176                             .withNodeIdentifier(new NodeIdentifier(CONTAINER_IN_LIST))
177                             .withChild(ImmutableNodes.leafNode(NAME, refValue))
178                             .build())
179                         .build())
180                     .build())
181                 .build();
182     }
183
184     private static ContainerNode fooTopWithRef(final String refValue) {
185         return Builders.containerBuilder()
186                 .withNodeIdentifier(new NodeIdentifier(FOO_TOP))
187                 .withChild(ImmutableNodes.leafNode(REF, refValue))
188                 .build();
189     }
190
191     private static ContainerNode bazTop() {
192         return Builders.containerBuilder()
193                 .withNodeIdentifier(new NodeIdentifier(BAZ_TOP))
194                 .withChild(Builders.mapBuilder()
195                     .withNodeIdentifier(new NodeIdentifier(LIST_IN_CONTAINER))
196                     .withChild(Builders.mapEntryBuilder()
197                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(LIST_IN_CONTAINER, BAZ_NAME, "name1"))
198                         .withChild(ImmutableNodes.leafNode(BAZ_NAME, "name1"))
199                         .build())
200                     .build())
201                 .build();
202     }
203 }