Update CursorAwareDataTree{Modification,Snapshot} API
[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.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.DataTreeConfiguration;
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.DataValidationFailedException;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class DataTreeCandidatesTest {
34
35     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidatesTest.class);
36
37     private static final SchemaContext SCHEMA_CONTEXT = TestModel.createTestContext();
38
39     private DataTree dataTree;
40
41     @Before
42     public void setUp() throws Exception {
43         dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, 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()
53                 .newModification();
54         final DataTreeModificationCursor cursor = modification.openCursor();
55         cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
56         modification.ready();
57
58         dataTree.validate(modification);
59         final DataTreeCandidate candidate = dataTree.prepare(modification);
60         dataTree.commit(candidate);
61     }
62
63     @Test
64     public void testRootedCandidate() throws DataValidationFailedException {
65         final DataTree innerDataTree = new InMemoryDataTreeFactory().create(
66             new DataTreeConfiguration.Builder(TreeType.OPERATIONAL)
67             .setMandatoryNodesValidation(true)
68             .setRootPath(TestModel.INNER_CONTAINER_PATH)
69             .setUniqueIndexes(true).build(), SCHEMA_CONTEXT);
70
71         final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
72                 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
73                 .withValue("testing-value")
74                 .build();
75
76         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot()
77                 .newModification();
78         modification.write(TestModel.VALUE_PATH, leaf);
79
80         modification.ready();
81         dataTree.validate(modification);
82         final DataTreeCandidate candidate = dataTree.prepare(modification);
83         dataTree.commit(candidate);
84
85         final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
86         final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
87             candidate.getRootNode());
88
89         try {
90             // lets see if getting the identifier of the root node throws an exception
91             newCandidate.getRootNode().getIdentifier();
92             fail();
93         } catch (IllegalStateException e) {
94             LOG.debug("Cannot get identifier of root node candidate which is correct", e);
95         }
96
97         // lets see if we can apply this rooted candidate to a new dataTree
98         DataTreeCandidates.applyToModification(newModification,
99                 newCandidate);
100
101         final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
102         assertEquals(readLeaf, leaf);
103     }
104 }