Optimize IMDT tests
[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 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
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.DataValidationFailedException;
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 extends AbstractTestModelTest {
33
34     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidatesTest.class);
35
36     private DataTree dataTree;
37
38     @Before
39     public void setUp() throws Exception {
40         dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
41
42         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
43                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
44                 .withChild(ImmutableContainerNodeBuilder.create()
45                         .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
46                         .build())
47                 .build();
48
49         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot()
50                 .newModification();
51         final DataTreeModificationCursor cursor = modification.openCursor();
52         cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
53         modification.ready();
54
55         dataTree.validate(modification);
56         final DataTreeCandidate candidate = dataTree.prepare(modification);
57         dataTree.commit(candidate);
58     }
59
60     @Test
61     public void testRootedCandidate() throws DataValidationFailedException {
62         final DataTree innerDataTree = new InMemoryDataTreeFactory().create(
63             new DataTreeConfiguration.Builder(TreeType.OPERATIONAL)
64             .setMandatoryNodesValidation(true)
65             .setRootPath(TestModel.INNER_CONTAINER_PATH)
66             .setUniqueIndexes(true).build(), SCHEMA_CONTEXT);
67
68         final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
69                 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
70                 .withValue("testing-value")
71                 .build();
72
73         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot()
74                 .newModification();
75         modification.write(TestModel.VALUE_PATH, leaf);
76
77         modification.ready();
78         dataTree.validate(modification);
79         final DataTreeCandidate candidate = dataTree.prepare(modification);
80         dataTree.commit(candidate);
81
82         final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
83         final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
84             candidate.getRootNode());
85
86         try {
87             // lets see if getting the identifier of the root node throws an exception
88             newCandidate.getRootNode().getIdentifier();
89             fail();
90         } catch (IllegalStateException e) {
91             LOG.debug("Cannot get identifier of root node candidate which is correct", e);
92         }
93
94         // lets see if we can apply this rooted candidate to a new dataTree
95         DataTreeCandidates.applyToModification(newModification,
96                 newCandidate);
97
98         final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
99         assertEquals(readLeaf, leaf);
100     }
101 }