Remove deprecated Yin/YangStatementSourceImpl
[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
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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 final SchemaContext SCHEMA_CONTEXT = TestModel.createTestContext();
37
38     private DataTree dataTree;
39
40     @Before
41     public void setUp() throws Exception {
42         dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
43         dataTree.setSchemaContext(SCHEMA_CONTEXT);
44
45         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
46                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
47                 .withChild(ImmutableContainerNodeBuilder.create()
48                         .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
49                         .build())
50                 .build();
51
52         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot().newModification();
53         final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
54         cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
55         modification.ready();
56
57         dataTree.validate(modification);
58         final DataTreeCandidate candidate = dataTree.prepare(modification);
59         dataTree.commit(candidate);
60     }
61
62     @Test
63     public void testRootedCandidate() throws Exception {
64         final DataTree innerDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
65         innerDataTree.setSchemaContext(SCHEMA_CONTEXT);
66
67         final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
68                 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
69                 .withValue("testing-value")
70                 .build();
71
72         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot().newModification();
73         modification.write(TestModel.VALUE_PATH, leaf);
74
75         modification.ready();
76         dataTree.validate(modification);
77         final DataTreeCandidate candidate = dataTree.prepare(modification);
78         dataTree.commit(candidate);
79
80         final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
81         final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH, candidate.getRootNode());
82
83         try {
84             // lets see if getting the identifier of the root node throws an exception
85             newCandidate.getRootNode().getIdentifier();
86             fail();
87         } catch (IllegalStateException e) {
88             LOG.debug("Cannot get identifier of root node candidate which is correct", e);
89         }
90
91         // lets see if we can apply this rooted candidate to a new dataTree
92         DataTreeCandidates.applyToModification(newModification,
93                 newCandidate);
94
95         final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
96         assertEquals(readLeaf, leaf);
97     }
98 }