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.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
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.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class DataTreeCandidatesTest {
35 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
37 private static final SchemaContext SCHEMA_CONTEXT;
41 SCHEMA_CONTEXT = TestModel.createTestContext();
42 } catch (ReactorException e) {
43 throw new ExceptionInInitializerError(e);
47 private DataTree dataTree;
50 public void setUp() throws Exception {
51 dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
52 dataTree.setSchemaContext(SCHEMA_CONTEXT);
54 final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
55 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
56 .withChild(ImmutableContainerNodeBuilder.create()
57 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
61 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot().newModification();
62 final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
63 cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
66 dataTree.validate(modification);
67 final DataTreeCandidate candidate = dataTree.prepare(modification);
68 dataTree.commit(candidate);
72 public void testRootedCandidate() throws Exception {
73 final DataTree innerDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
74 innerDataTree.setSchemaContext(SCHEMA_CONTEXT);
76 final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
77 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
78 .withValue("testing-value")
81 final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot().newModification();
82 modification.write(TestModel.VALUE_PATH, leaf);
85 dataTree.validate(modification);
86 final DataTreeCandidate candidate = dataTree.prepare(modification);
87 dataTree.commit(candidate);
89 final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
90 final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH, candidate.getRootNode());
93 // lets see if getting the identifier of the root node throws an exception
94 newCandidate.getRootNode().getIdentifier();
96 } catch (IllegalStateException e) {
97 LOG.debug("Cannot get identifier of root node candidate which is correct", e);
100 // lets see if we can apply this rooted candidate to a new dataTree
101 DataTreeCandidates.applyToModification(newModification,
104 final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
105 assertEquals(readLeaf, leaf);