Cleanup DataTree interfaces and InMemmoryDataTreeFactory
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / Bug2690Test.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.Optional;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33
34 public class Bug2690Test {
35     private static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
36     private SchemaContext schemaContext;
37     private DataTree inMemoryDataTree;
38
39     @Before
40     public void prepare() {
41         schemaContext = createTestContext();
42         assertNotNull("Schema context must not be null.", schemaContext);
43         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
44             schemaContext);
45     }
46
47     public static SchemaContext createTestContext() {
48         return YangParserTestUtils.parseYangResource(ODL_DATASTORE_TEST_YANG);
49     }
50
51     @Test
52     public void testWriteMerge1() throws DataValidationFailedException {
53         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
54         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
55         final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
56                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
57                 .withChild(fooEntryNode).build();
58         final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
59                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
60                 .withChild(barEntryNode).build();
61
62         final ContainerNode cont1 = Builders.containerBuilder()
63                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
64                 .withChild(mapNode1).build();
65
66         final ContainerNode cont2 = Builders.containerBuilder()
67                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
68                 .withChild(mapNode2).build();
69
70         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
71         modificationTree.write(TestModel.TEST_PATH, cont1);
72         modificationTree.merge(TestModel.TEST_PATH, cont2);
73         modificationTree.ready();
74
75         inMemoryDataTree.validate(modificationTree);
76         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
77         inMemoryDataTree.commit(prepare);
78
79         final DataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
80         final DataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
81         final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
82         assertTrue(readNode.isPresent());
83         assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).getValue().size());
84     }
85 }