2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import static org.junit.Assert.assertEquals;
11 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.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;
32 public class DataTreeCandidatesTest extends AbstractTestModelTest {
34 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidatesTest.class);
36 private DataTree dataTree;
39 public void setUp() throws Exception {
40 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
42 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
43 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
44 .withChild(ImmutableContainerNodeBuilder.create()
45 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
49 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot()
51 final DataTreeModificationCursor cursor = modification.openCursor();
52 cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
55 dataTree.validate(modification);
56 final DataTreeCandidate candidate = dataTree.prepare(modification);
57 dataTree.commit(candidate);
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);
68 final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
69 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
70 .withValue("testing-value")
73 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot()
75 modification.write(TestModel.VALUE_PATH, leaf);
78 dataTree.validate(modification);
79 final DataTreeCandidate candidate = dataTree.prepare(modification);
80 dataTree.commit(candidate);
82 final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
83 final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
84 candidate.getRootNode());
87 // lets see if getting the identifier of the root node throws an exception
88 newCandidate.getRootNode().getIdentifier();
90 } catch (IllegalStateException e) {
91 LOG.debug("Cannot get identifier of root node candidate which is correct", e);
94 // lets see if we can apply this rooted candidate to a new dataTree
95 DataTreeCandidates.applyToModification(newModification,
98 final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
99 assertEquals(readLeaf, leaf);