Cleanup checkstyle warnings and turn enforcement on in yang-data-impl
[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.TreeType;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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 = TestModel.createTestContext();
38
39     private DataTree dataTree;
40
41     @Before
42     public void setUp() throws Exception {
43         dataTree = InMemoryDataTreeFactory.getInstance().create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
44         dataTree.setSchemaContext(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 Exception {
66         final DataTree innerDataTree = InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL,
67             TestModel.INNER_CONTAINER_PATH);
68         innerDataTree.setSchemaContext(SCHEMA_CONTEXT);
69
70         final LeafNode<String> leaf = ImmutableLeafNodeBuilder.<String>create()
71                 .withNodeIdentifier(new NodeIdentifier(TestModel.VALUE_QNAME))
72                 .withValue("testing-value")
73                 .build();
74
75         final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) innerDataTree.takeSnapshot()
76                 .newModification();
77         modification.write(TestModel.VALUE_PATH, leaf);
78
79         modification.ready();
80         dataTree.validate(modification);
81         final DataTreeCandidate candidate = dataTree.prepare(modification);
82         dataTree.commit(candidate);
83
84         final DataTreeModification newModification = dataTree.takeSnapshot().newModification();
85         final DataTreeCandidate newCandidate = DataTreeCandidates.newDataTreeCandidate(TestModel.INNER_CONTAINER_PATH,
86             candidate.getRootNode());
87
88         try {
89             // lets see if getting the identifier of the root node throws an exception
90             newCandidate.getRootNode().getIdentifier();
91             fail();
92         } catch (IllegalStateException e) {
93             LOG.debug("Cannot get identifier of root node candidate which is correct", e);
94         }
95
96         // lets see if we can apply this rooted candidate to a new dataTree
97         DataTreeCandidates.applyToModification(newModification,
98                 newCandidate);
99
100         final LeafNode<?> readLeaf = (LeafNode<?>) newModification.readNode(TestModel.INNER_VALUE_PATH).get();
101         assertEquals(readLeaf, leaf);
102     }
103 }