Cleanup use of Guava library
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ListConstraintsValidation.java
1 /*
2  * Copyright (c) 2015 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.schema.tree;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Optional;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
28 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41
42 public class ListConstraintsValidation {
43     private static final String CONSTRAINTS_VALIDATION_TEST_YANG = "/list-constraints-validation-test-model.yang";
44     private static final QName MASTER_CONTAINER_QNAME = QName.create(
45             "urn:opendaylight:params:xml:ns:yang:list-constraints-validation-test-model", "2015-02-02",
46             "master-container");
47     private static final QName MIN_MAX_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-list");
48     private static final QName MIN_MAX_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-key-leaf");
49     private static final QName UNBOUNDED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-list");
50     private static final QName UNBOUNDED_KEY_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-key-leaf");
51     private static final QName MIN_MAX_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "min-max-leaf-list");
52     private static final QName UNBOUNDED_LEAF_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unbounded-leaf-list");
53     private static final QName UNKEYED_LIST_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-list");
54     private static final QName UNKEYED_LEAF_QNAME = QName.create(MASTER_CONTAINER_QNAME, "unkeyed-leaf");
55
56     private static final YangInstanceIdentifier MASTER_CONTAINER_PATH = YangInstanceIdentifier
57             .of(MASTER_CONTAINER_QNAME);
58     private static final YangInstanceIdentifier MIN_MAX_LIST_PATH = YangInstanceIdentifier
59             .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LIST_QNAME).build();
60     private static final YangInstanceIdentifier UNBOUNDED_LIST_PATH = YangInstanceIdentifier
61             .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LIST_QNAME).build();
62     private static final YangInstanceIdentifier MIN_MAX_LEAF_LIST_PATH = YangInstanceIdentifier
63             .builder(MASTER_CONTAINER_PATH).node(MIN_MAX_LEAF_LIST_QNAME).build();
64     private static final YangInstanceIdentifier UNBOUNDED_LEAF_LIST_PATH = YangInstanceIdentifier
65             .builder(MASTER_CONTAINER_PATH).node(UNBOUNDED_LEAF_LIST_QNAME).build();
66     private static final YangInstanceIdentifier UNKEYED_LIST_PATH = YangInstanceIdentifier
67             .builder(MASTER_CONTAINER_PATH).node(UNKEYED_LIST_QNAME).build();
68
69     private SchemaContext schemaContext;
70     private InMemoryDataTree inMemoryDataTree;
71
72     @Before
73     public void prepare() {
74         schemaContext = createTestContext();
75         assertNotNull("Schema context must not be null.", schemaContext);
76         inMemoryDataTree =  (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
77         inMemoryDataTree.setSchemaContext(schemaContext);
78         final InMemoryDataTreeSnapshot initialDataTreeSnapshot = inMemoryDataTree.takeSnapshot();
79         final DataTreeModification modificationTree = initialDataTreeSnapshot.newModification();
80
81         modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
82         modificationTree.ready();
83         inMemoryDataTree.commit(inMemoryDataTree.prepare(modificationTree));
84     }
85
86     public static SchemaContext createTestContext() {
87         return YangParserTestUtils.parseYangResource(CONSTRAINTS_VALIDATION_TEST_YANG);
88     }
89
90     @Test
91     public void minMaxListTestPass() throws DataValidationFailedException {
92
93         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
94         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
95         final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
96                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
97                 .withChild(fooEntryNode).build();
98         final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
99                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
100                 .withChild(barEntryNode).build();
101
102         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
103         modificationTree.write(MIN_MAX_LIST_PATH, mapNode1);
104         modificationTree.merge(MIN_MAX_LIST_PATH, mapNode2);
105         // TODO: check why write and then merge on list commits only "bar" child
106         modificationTree.ready();
107
108         inMemoryDataTree.validate(modificationTree);
109         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
110         inMemoryDataTree.commit(prepare);
111
112         final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
113         final Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
114         assertTrue(minMaxListRead.isPresent());
115         assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
116     }
117
118     @Test(expected = DataValidationFailedException.class)
119     public void minMaxListFail() throws DataValidationFailedException {
120         InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
121
122         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "foo");
123         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "bar");
124         final MapEntryNode gooEntryNode = ImmutableNodes.mapEntry(MIN_MAX_LIST_QNAME, MIN_MAX_KEY_LEAF_QNAME, "goo");
125         final MapNode mapNode = ImmutableNodes.mapNodeBuilder()
126                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LIST_QNAME))
127                 .withChild(fooEntryNode).build();
128
129         final YangInstanceIdentifier fooPath = MIN_MAX_LIST_PATH.node(fooEntryNode.getIdentifier());
130         final YangInstanceIdentifier barPath = MIN_MAX_LIST_PATH.node(barEntryNode.getIdentifier());
131         final YangInstanceIdentifier gooPath = MIN_MAX_LIST_PATH.node(gooEntryNode.getIdentifier());
132
133         modificationTree.write(MIN_MAX_LIST_PATH, mapNode);
134         modificationTree.merge(barPath, barEntryNode);
135         modificationTree.write(gooPath, gooEntryNode);
136         modificationTree.delete(gooPath);
137         modificationTree.ready();
138
139         inMemoryDataTree.validate(modificationTree);
140         DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
141         inMemoryDataTree.commit(prepare1);
142
143         InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
144         Optional<NormalizedNode<?, ?>> minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
145         assertTrue(minMaxListRead.isPresent());
146         assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 2);
147
148         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
149         modificationTree.write(gooPath, gooEntryNode);
150         modificationTree.ready();
151
152         inMemoryDataTree.validate(modificationTree);
153         prepare1 = inMemoryDataTree.prepare(modificationTree);
154         inMemoryDataTree.commit(prepare1);
155
156         snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
157         minMaxListRead = snapshotAfterCommit.readNode(MIN_MAX_LIST_PATH);
158         assertTrue(minMaxListRead.isPresent());
159         assertTrue(((NormalizedNodeContainer<?, ?, ?>) minMaxListRead.get()).getValue().size() == 3);
160
161         modificationTree = inMemoryDataTree.takeSnapshot().newModification();
162
163         modificationTree.delete(gooPath);
164         modificationTree.delete(fooPath);
165         modificationTree.ready();
166
167         inMemoryDataTree.validate(modificationTree);
168     }
169
170     @Test
171     public void minMaxLeafListPass() throws DataValidationFailedException {
172         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
173
174         final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
175         final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
176
177         final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
178                 .withNodeIdentifier(barPath)
179                 .withValue("bar").build();
180         final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
181                 .withNodeIdentifier(gooPath)
182                 .withValue("goo").build();
183
184         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
185                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
186                 .withChildValue("foo").build();
187
188         modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
189         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
190         modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
191         modificationTree.delete(MIN_MAX_LEAF_LIST_PATH.node(gooPath));
192         modificationTree.ready();
193
194         inMemoryDataTree.validate(modificationTree);
195         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
196         inMemoryDataTree.commit(prepare1);
197
198         final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
199         final Optional<NormalizedNode<?, ?>> masterContainer = snapshotAfterCommit.readNode(MASTER_CONTAINER_PATH);
200         assertTrue(masterContainer.isPresent());
201         final Optional<NormalizedNodeContainer<?, ?, ?>> leafList = ((NormalizedNodeContainer) masterContainer.get())
202                 .getChild(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME));
203         assertTrue(leafList.isPresent());
204         assertTrue(leafList.get().getValue().size() == 2);
205     }
206
207     @Test(expected = DataValidationFailedException.class)
208     public void minMaxLeafListFail() throws DataValidationFailedException {
209         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
210
211         final NodeWithValue<Object> fooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "foo");
212         final NodeWithValue<Object> barPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "bar");
213         final NodeWithValue<Object> gooPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "goo");
214         final NodeWithValue<Object> fuuPath = new NodeWithValue<>(MIN_MAX_LIST_QNAME, "fuu");
215
216         final LeafSetEntryNode<Object> barLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
217                 .withNodeIdentifier(barPath)
218                 .withValue("bar").build();
219         final LeafSetEntryNode<Object> gooLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
220                 .withNodeIdentifier(gooPath)
221                 .withValue("goo").build();
222         final LeafSetEntryNode<Object> fuuLeafSetEntry = ImmutableLeafSetEntryNodeBuilder.create()
223                 .withNodeIdentifier(fuuPath)
224                 .withValue("fuu").build();
225
226         final LeafSetNode<Object> fooLeafSetNode = ImmutableLeafSetNodeBuilder.create()
227                 .withNodeIdentifier(new NodeIdentifier(MIN_MAX_LEAF_LIST_QNAME))
228                 .withChildValue("foo").build();
229
230         modificationTree.write(MIN_MAX_LEAF_LIST_PATH, fooLeafSetNode);
231         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(barPath), barLeafSetEntry);
232         modificationTree.merge(MIN_MAX_LEAF_LIST_PATH.node(gooPath), gooLeafSetEntry);
233         modificationTree.write(MIN_MAX_LEAF_LIST_PATH.node(fuuPath), fuuLeafSetEntry);
234         modificationTree.ready();
235
236         inMemoryDataTree.validate(modificationTree);
237     }
238
239     @Test
240     public void unkeyedListTestPass() throws DataValidationFailedException {
241         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
242
243         final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
244                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
245                 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
246         final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
247         unkeyedEntries.add(foo);
248         final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
249                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
250                 .withValue(unkeyedEntries).build();
251
252         modificationTree.write(MASTER_CONTAINER_PATH, ImmutableNodes.containerNode(MASTER_CONTAINER_QNAME));
253         modificationTree.merge(UNKEYED_LIST_PATH, unkeyedListNode);
254         modificationTree.ready();
255
256         inMemoryDataTree.validate(modificationTree);
257         final DataTreeCandidate prepare1 = inMemoryDataTree.prepare(modificationTree);
258         inMemoryDataTree.commit(prepare1);
259
260         final InMemoryDataTreeSnapshot snapshotAfterCommit = inMemoryDataTree.takeSnapshot();
261         final Optional<NormalizedNode<?, ?>> unkeyedListRead = snapshotAfterCommit.readNode(UNKEYED_LIST_PATH);
262         assertTrue(unkeyedListRead.isPresent());
263         assertTrue(((UnkeyedListNode) unkeyedListRead.get()).getSize() == 1);
264     }
265
266     @Test(expected = DataValidationFailedException.class)
267     public void unkeyedListTestFail() throws DataValidationFailedException {
268         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
269
270         final UnkeyedListEntryNode foo = ImmutableUnkeyedListEntryNodeBuilder.create()
271                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
272                 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "foo")).build();
273         final UnkeyedListEntryNode bar = ImmutableUnkeyedListEntryNodeBuilder.create()
274                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LEAF_QNAME))
275                 .withChild(ImmutableNodes.leafNode(UNKEYED_LEAF_QNAME, "bar")).build();
276         final List<UnkeyedListEntryNode> unkeyedEntries = new ArrayList<>();
277         unkeyedEntries.add(foo);
278         unkeyedEntries.add(bar);
279         final UnkeyedListNode unkeyedListNode = ImmutableUnkeyedListNodeBuilder.create()
280                 .withNodeIdentifier(new NodeIdentifier(UNKEYED_LIST_QNAME))
281                 .withValue(unkeyedEntries).build();
282
283         modificationTree.write(UNKEYED_LIST_PATH, unkeyedListNode);
284         modificationTree.ready();
285
286         inMemoryDataTree.validate(modificationTree);
287     }
288 }