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