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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
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;
34 public class DataTreeCandidatesTest {
36 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidatesTest.class);
38 private static final SchemaContext SCHEMA_CONTEXT = TestModel.createTestContext();
40 private DataTree dataTree;
43 public void setUp() throws Exception {
44 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
46 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
47 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
48 .withChild(ImmutableContainerNodeBuilder.create()
49 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
53 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot()
55 final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
56 cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
59 dataTree.validate(modification);
60 final DataTreeCandidate candidate = dataTree.prepare(modification);
61 dataTree.commit(candidate);
65 public void testRootedCandidate() throws DataValidationFailedException {
66 final DataTree innerDataTree = new InMemoryDataTreeFactory().create(
67 new DataTreeConfiguration.Builder(TreeType.OPERATIONAL)
68 .setMandatoryNodesValidation(true)
69 .setRootPath(TestModel.INNER_CONTAINER_PATH)
70 .setUniqueIndexes(true).build(), SCHEMA_CONTEXT);
72 final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
73 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
74 .withValue("testing-value")
77 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot()
79 modification.write(TestModel.VALUE_PATH, leaf);
82 dataTree.validate(modification);
83 final DataTreeCandidate candidate = dataTree.prepare(modification);
84 dataTree.commit(candidate);
86 final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
87 final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
88 candidate.getRootNode());
91 // lets see if getting the identifier of the root node throws an exception
92 newCandidate.getRootNode().getIdentifier();
94 } catch (IllegalStateException e) {
95 LOG.debug("Cannot get identifier of root node candidate which is correct", e);
98 // lets see if we can apply this rooted candidate to a new dataTree
99 DataTreeCandidates.applyToModification(newModification,
102 final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
103 assertEquals(readLeaf, leaf);