2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
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;
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;
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);
48 public static SchemaContext createTestContext() throws ReactorException {
49 return YangParserTestUtils.parseYangResource(ODL_DATASTORE_TEST_YANG);
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();
63 final ContainerNode cont1 = Builders.containerBuilder()
64 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
65 .withChild(mapNode1).build();
67 final ContainerNode cont2 = Builders.containerBuilder()
68 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
69 .withChild(mapNode2).build();
71 final DataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
72 modificationTree.write(TestModel.TEST_PATH, cont1);
73 modificationTree.merge(TestModel.TEST_PATH, cont2);
74 modificationTree.ready();
76 inMemoryDataTree.validate(modificationTree);
77 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
78 inMemoryDataTree.commit(prepare);
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());