Revert "Bug 5968: Mandatory leaf enforcement does not work in some cases"
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / DataTreeCandidatesTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13 import org.junit.Before;
14 import org.junit.Test;
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.schema.ContainerNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
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.DataTreeCandidates;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class DataTreeCandidatesTest {
33
34     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
35
36     private static SchemaContext SCHEMA_CONTEXT = null;
37
38     static {
39         try {
40             SCHEMA_CONTEXT = TestModel.createTestContext();
41         } catch (ReactorException e) {
42             LOG.error("Failed to setup test schema context", e);
43         }
44     }
45
46     private DataTree dataTree;
47
48     @Before
49     public void setUp() throws Exception {
50         dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
51         dataTree.setSchemaContext(SCHEMA_CONTEXT);
52
53         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
54                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
55                 .withChild(ImmutableContainerNodeBuilder.create()
56                         .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
57                         .build())
58                 .build();
59
60         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot().newModification();
61         final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
62         cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
63         modification.ready();
64
65         dataTree.validate(modification);
66         final DataTreeCandidate candidate = dataTree.prepare(modification);
67         dataTree.commit(candidate);
68     }
69
70     @Test
71     public void testRootedCandidate() throws Exception {
72         final DataTree innerDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
73         innerDataTree.setSchemaContext(SCHEMA_CONTEXT);
74
75         final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
76                 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
77                 .withValue("testing-value")
78                 .build();
79
80         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot().newModification();
81         modification.write(TestModel.VALUE_PATH, leaf);
82
83         modification.ready();
84         dataTree.validate(modification);
85         final DataTreeCandidate candidate = dataTree.prepare(modification);
86         dataTree.commit(candidate);
87
88         final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
89         final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH, candidate.getRootNode());
90
91         try {
92             // lets see if getting the identifier of the root node throws an exception
93             newCandidate.getRootNode().getIdentifier();
94             fail();
95         } catch (IllegalStateException e) {
96             LOG.debug("Cannot get identifier of root node candidate which is correct", e);
97         }
98
99         // lets see if we can apply this rooted candidate to a new dataTree
100         DataTreeCandidates.applyToModification(newModification,
101                 newCandidate);
102
103         final LeafNode<String> readLeaf = (LeafNode<String>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
104         assertEquals(readLeaf, leaf);
105     }
106 }