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.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
26 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 public class Bug2690Test {
33 private static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
34 private SchemaContext schemaContext;
35 private InMemoryDataTree inMemoryDataTree;
38 public void prepare() throws ReactorException {
39 schemaContext = createTestContext();
40 assertNotNull("Schema context must not be null.", schemaContext);
41 inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
42 inMemoryDataTree.setSchemaContext(schemaContext);
45 public static SchemaContext createTestContext() throws ReactorException {
46 return YangParserTestUtils.parseYangResource(ODL_DATASTORE_TEST_YANG);
50 public void testWriteMerge1() throws DataValidationFailedException {
51 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
52 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
53 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
54 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
55 .withChild(fooEntryNode).build();
56 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
57 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
58 .withChild(barEntryNode).build();
60 final ContainerNode cont1 = Builders.containerBuilder()
61 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
62 .withChild(mapNode1).build();
64 final ContainerNode cont2 = Builders.containerBuilder()
65 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
66 .withChild(mapNode2).build();
68 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
69 modificationTree.write(TestModel.TEST_PATH, cont1);
70 modificationTree.merge(TestModel.TEST_PATH, cont2);
71 modificationTree.ready();
73 inMemoryDataTree.validate(modificationTree);
74 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
75 inMemoryDataTree.commit(prepare);
77 final InMemoryDataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
78 final InMemoryDataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
79 final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
80 assertTrue(readNode.isPresent());
81 assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).getValue().size());