Eliminate use of DataTreeFactory.create(TreeType)
[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.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class DataTreeCandidatesTest {
35
36     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
37
38     private static final SchemaContext SCHEMA_CONTEXT = TestModel.createTestContext();
39
40     private DataTree dataTree;
41
42     @Before
43     public void setUp() throws Exception {
44         dataTree = InMemoryDataTreeFactory.getInstance().create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
45         dataTree.setSchemaContext(SCHEMA_CONTEXT);
46
47         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
48                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
49                 .withChild(ImmutableContainerNodeBuilder.create()
50                         .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
51                         .build())
52                 .build();
53
54         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot()
55                 .newModification();
56         final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
57         cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
58         modification.ready();
59
60         dataTree.validate(modification);
61         final DataTreeCandidate candidate = dataTree.prepare(modification);
62         dataTree.commit(candidate);
63     }
64
65     @Test
66     public void testRootedCandidate() throws DataValidationFailedException {
67         final DataTree innerDataTree = InMemoryDataTreeFactory.getInstance().create(
68             new DataTreeConfiguration.Builder(TreeType.OPERATIONAL)
69             .setMandatoryNodesValidation(true)
70             .setRootPath(TestModel.INNER_CONTAINER_PATH)
71             .setUniqueIndexes(true).build(), SCHEMA_CONTEXT);
72
73         final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
74                 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
75                 .withValue("testing-value")
76                 .build();
77
78         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot()
79                 .newModification();
80         modification.write(TestModel.VALUE_PATH, leaf);
81
82         modification.ready();
83         dataTree.validate(modification);
84         final DataTreeCandidate candidate = dataTree.prepare(modification);
85         dataTree.commit(candidate);
86
87         final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
88         final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
89             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<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
104         assertEquals(readLeaf, leaf);
105     }
106 }