3de42a5592ea7b8e7674e1d55ca4dab787ac4223
[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.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;
33
34 public class DataTreeCandidatesTest {
35
36     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidatesTest.class);
37
38     private static final SchemaContext SCHEMA_CONTEXT = TestModel.createTestContext();
39
40     private DataTree dataTree;
41
42     @Before
43     public void setUp() throws Exception {
44         dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
45
46         final ContainerNode testContainer = ImmutableContainerNodeBuilder.create()
47                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
48                 .withChild(ImmutableContainerNodeBuilder.create()
49                         .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
50                         .build())
51                 .build();
52
53         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot()
54                 .newModification();
55         final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY);
56         cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer);
57         modification.ready();
58
59         dataTree.validate(modification);
60         final DataTreeCandidate candidate = dataTree.prepare(modification);
61         dataTree.commit(candidate);
62     }
63
64     @Test
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);
71
72         final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
73                 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
74                 .withValue("testing-value")
75                 .build();
76
77         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot()
78                 .newModification();
79         modification.write(TestModel.VALUE_PATH, leaf);
80
81         modification.ready();
82         dataTree.validate(modification);
83         final DataTreeCandidate candidate = dataTree.prepare(modification);
84         dataTree.commit(candidate);
85
86         final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
87         final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
88             candidate.getRootNode());
89
90         try {
91             // lets see if getting the identifier of the root node throws an exception
92             newCandidate.getRootNode().getIdentifier();
93             fail();
94         } catch (IllegalStateException e) {
95             LOG.debug("Cannot get identifier of root node candidate which is correct", e);
96         }
97
98         // lets see if we can apply this rooted candidate to a new dataTree
99         DataTreeCandidates.applyToModification(newModification,
100                 newCandidate);
101
102         final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
103         assertEquals(readLeaf, leaf);
104     }
105 }