Bump odlparent dependencies to 3.0.0
[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.parser.spi.meta.ReactorException;
33 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34
35 public class Bug2690Test {
36     private static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
37     private SchemaContext schemaContext;
38     private DataTree inMemoryDataTree;
39
40     @Before
41     public void prepare() throws ReactorException {
42         schemaContext = createTestContext();
43         assertNotNull("Schema context must not be null.", schemaContext);
44         inMemoryDataTree = InMemoryDataTreeFactory.getInstance().create(
45             DataTreeConfiguration.DEFAULT_OPERATIONAL, schemaContext);
46     }
47
48     public static SchemaContext createTestContext() throws ReactorException {
49         return YangParserTestUtils.parseYangResource(ODL_DATASTORE_TEST_YANG);
50     }
51
52     @Test
53     public void testWriteMerge1() throws DataValidationFailedException {
54         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
55         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
56         final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
57                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
58                 .withChild(fooEntryNode).build();
59         final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
60                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
61                 .withChild(barEntryNode).build();
62
63         final ContainerNode cont1 = Builders.containerBuilder()
64                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
65                 .withChild(mapNode1).build();
66
67         final ContainerNode cont2 = Builders.containerBuilder()
68                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
69                 .withChild(mapNode2).build();
70
71         final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
72         modificationTree.write(TestModel.TEST_PATH, cont1);
73         modificationTree.merge(TestModel.TEST_PATH, cont2);
74         modificationTree.ready();
75
76         inMemoryDataTree.validate(modificationTree);
77         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
78         inMemoryDataTree.commit(prepare);
79
80         final DataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
81         final DataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
82         final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
83         assertTrue(readNode.isPresent());
84         assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).getValue().size());
85     }
86 }