c1ccfebfcb8e6dc3b83729c3cb01883951ea0d39
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / DataTreeCandidatesTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13
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;
32
33 public class DataTreeCandidatesTest {
34
35     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
36
37     private static final SchemaContext SCHEMA_CONTEXT;
38
39     static {
40         try {
41             SCHEMA_CONTEXT = TestModel.createTestContext();
42         } catch (ReactorException e) {
43             throw new ExceptionInInitializerError(e);
44         }
45     }
46
47     private DataTree dataTree;
48
49     @Before
50     public void setUp() throws Exception {
51         dataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
52         dataTree.setSchemaContext(SCHEMA_CONTEXT);
53
54         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
55                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
56                 .withChild(ImmutableContainerNodeBuilder.create()
57                         .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
58                         .build())
59                 .build();
60
61         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot().newModification();
62         final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
63         cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
64         modification.ready();
65
66         dataTree.validate(modification);
67         final DataTreeCandidate candidate = dataTree.prepare(modification);
68         dataTree.commit(candidate);
69     }
70
71     @Test
72     public void testRootedCandidate() throws Exception {
73         final DataTree innerDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
74         innerDataTree.setSchemaContext(SCHEMA_CONTEXT);
75
76         final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
77                 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
78                 .withValue("testing-value")
79                 .build();
80
81         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot().newModification();
82         modification.write(TestModel.VALUE_PATH, leaf);
83
84         modification.ready();
85         dataTree.validate(modification);
86         final DataTreeCandidate candidate = dataTree.prepare(modification);
87         dataTree.commit(candidate);
88
89         final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
90         final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH, candidate.getRootNode());
91
92         try {
93             // lets see if getting the identifier of the root node throws an exception
94             newCandidate.getRootNode().getIdentifier();
95             fail();
96         } catch (IllegalStateException e) {
97             LOG.debug("Cannot get identifier of root node candidate which is correct", e);
98         }
99
100         // lets see if we can apply this rooted candidate to a new dataTree
101         DataTreeCandidates.applyToModification(newModification,
102                 newCandidate);
103
104         final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
105         assertEquals(readLeaf, leaf);
106     }
107 }