2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.tree.impl;
10 import static org.junit.jupiter.api.Assertions.assertThrows;
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
14 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.data.tree.api.SchemaValidationFailedException;
25 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
27 // TODO: expand these tests to catch some more obscure cases
28 class ConfigStatementValidationTest extends AbstractTestModelTest {
29 private static final Short ONE_ID = 1;
30 private static final Short TWO_ID = 2;
32 private static final YangInstanceIdentifier OUTER_LIST_1_PATH = YangInstanceIdentifier
33 .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
36 private static final YangInstanceIdentifier OUTER_LIST_2_PATH = YangInstanceIdentifier
37 .builder(TestModel.OUTER_LIST_PATH).nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
40 private static final MapEntryNode INNER_FOO_ENTRY_NODE = mapEntry(TestModel.INNER_LIST_QNAME,
41 TestModel.NAME_QNAME, "foo");
43 private static final MapEntryNode INNER_BAR_ENTRY_NODE =
44 mapEntryBuilder(QName.create(TestModel.TEST_QNAME, "inner-list2"), TestModel.NAME_QNAME, "foo")
45 .withChild(leafNode(TestModel.VALUE_QNAME, "value")).build();
47 private static final MapEntryNode FOO_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID)
48 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_FOO_ENTRY_NODE)
52 private static final MapEntryNode BAR_NODE = mapEntryBuilder(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, TWO_ID)
53 .withChild(mapNodeBuilder(TestModel.INNER_LIST_QNAME).withChild(INNER_BAR_ENTRY_NODE)
57 private static ContainerNode createFooTestContainerNode() {
58 return Builders.containerBuilder()
59 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
60 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(FOO_NODE).build()).build();
63 private static ContainerNode createBarTestContainerNode() {
64 return Builders.containerBuilder()
65 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
66 .withChild(mapNodeBuilder(TestModel.OUTER_LIST_QNAME).withChild(BAR_NODE).build()).build();
70 void testOnPathFail() {
71 assertThrows(SchemaValidationFailedException.class, () -> {
72 final var inMemoryDataTree = new InMemoryDataTreeFactory().create(
73 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
74 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
75 final var ii = OUTER_LIST_1_PATH.node(new NodeIdentifier(TestModel.INNER_LIST_QNAME))
76 .node(INNER_FOO_ENTRY_NODE.name());
77 modificationTree.write(ii, INNER_FOO_ENTRY_NODE);
79 inMemoryDataTree.validate(modificationTree);
80 final var prepare = inMemoryDataTree.prepare(modificationTree);
81 inMemoryDataTree.commit(prepare);
86 void testOnDataFail() {
87 assertThrows(SchemaValidationFailedException.class, () -> {
88 final var inMemoryDataTree = new InMemoryDataTreeFactory().create(
89 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
90 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
91 modificationTree.write(TestModel.TEST_PATH, createFooTestContainerNode());
92 modificationTree.ready();
93 inMemoryDataTree.validate(modificationTree);
94 final var prepare = inMemoryDataTree.prepare(modificationTree);
95 inMemoryDataTree.commit(prepare);
100 void testOnDataLeafFail() {
101 assertThrows(SchemaValidationFailedException.class, () -> {
102 final var inMemoryDataTree = new InMemoryDataTreeFactory().create(
103 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
104 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
105 modificationTree.write(TestModel.TEST_PATH, createBarTestContainerNode());
106 modificationTree.ready();
107 inMemoryDataTree.validate(modificationTree);
108 final var prepare = inMemoryDataTree.prepare(modificationTree);
109 inMemoryDataTree.commit(prepare);
114 void testOnPathCaseLeafFail() {
115 assertThrows(SchemaValidationFailedException.class, () -> {
116 final var inMemoryDataTree = new InMemoryDataTreeFactory().create(
117 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
118 final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
119 final var case2ContId = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "case2-cont"));
120 final var ii = TestModel.TEST_PATH.node(choice1Id).node(case2ContId);
121 final var case2Cont = Builders.containerBuilder().withNodeIdentifier(case2ContId)
122 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case2-leaf1"), "leaf-value")).build();
124 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
125 modificationTree.write(ii, case2Cont);
126 modificationTree.ready();
131 void testOnDataCaseLeafFail() {
132 assertThrows(SchemaValidationFailedException.class, () -> {
133 final var inMemoryDataTree = new InMemoryDataTreeFactory().create(
134 DataTreeConfiguration.DEFAULT_CONFIGURATION, SCHEMA_CONTEXT);
135 final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
136 final var ii = TestModel.TEST_PATH.node(choice1Id);
137 final var choice1 = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
138 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value")).build();
140 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
141 modificationTree.write(ii, choice1);
143 modificationTree.ready();