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;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
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.TreeType;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class DataTreeCandidatesTest {
34 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
36 private static SchemaContext SCHEMA_CONTEXT = null;
40 SCHEMA_CONTEXT = TestModel.createTestContext();
41 } catch (ReactorException e) {
42 LOG.error("Failed to setup test schema context", e);
46 private DataTree dataTree;
49 public void setUp() throws Exception {
50 dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
51 dataTree.setSchemaContext(SCHEMA_CONTEXT);
53 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
54 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
55 .withChild(ImmutableContainerNodeBuilder.create()
56 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
60 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot().newModification();
61 final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
62 cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
65 dataTree.validate(modification);
66 final DataTreeCandidate candidate = dataTree.prepare(modification);
67 dataTree.commit(candidate);
71 public void testRootedCandidate() throws Exception {
72 final DataTree innerDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
73 innerDataTree.setSchemaContext(SCHEMA_CONTEXT);
75 final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
76 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
77 .withValue("testing-value")
80 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot().newModification();
81 modification.write(TestModel.VALUE_PATH, leaf);
84 dataTree.validate(modification);
85 final DataTreeCandidate candidate = dataTree.prepare(modification);
86 dataTree.commit(candidate);
88 final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
89 final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH, 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<String> readLeaf = (LeafNode<String>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
104 assertEquals(readLeaf, leaf);