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(DataTreeCandidates.class);
38 private static final SchemaContext SCHEMA_CONTEXT = TestModel.createTestContext();
40 private DataTree dataTree;
43 public void setUp() throws Exception {
44 dataTree = InMemoryDataTreeFactory.getInstance().create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
45 dataTree.setSchemaContext(SCHEMA_CONTEXT);
47 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
48 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
49 .withChild(ImmutableContainerNodeBuilder.create()
50 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
54 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot()
56 final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
57 cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
60 dataTree.validate(modification);
61 final DataTreeCandidate candidate = dataTree.prepare(modification);
62 dataTree.commit(candidate);
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);
73 final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
74 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
75 .withValue("testing-value")
78 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot()
80 modification.write(TestModel.VALUE_PATH, leaf);
83 dataTree.validate(modification);
84 final DataTreeCandidate candidate = dataTree.prepare(modification);
85 dataTree.commit(candidate);
87 final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
88 final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
89 candidate.getRootNode());
92 // lets see if getting the identifier of the root node throws an exception
93 newCandidate.getRootNode().getIdentifier();
95 } catch (IllegalStateException e) {
96 LOG.debug("Cannot get identifier of root node candidate which is correct", e);
99 // lets see if we can apply this rooted candidate to a new dataTree
100 DataTreeCandidates.applyToModification(newModification,
103 final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
104 assertEquals(readLeaf, leaf);