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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import com.google.common.base.Optional;
15 import java.io.InputStream;
16 import java.util.Collections;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
28 import org.opendaylight.yangtools.yang.data.impl.RetestUtils;
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;
34 public class Retest_Bug2690Fix {
35 private static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
36 private SchemaContext schemaContext;
37 private InMemoryDataTree inMemoryDataTree;
40 public void prepare() throws ReactorException {
41 schemaContext = createTestContext();
42 assertNotNull("Schema context must not be null.", schemaContext);
43 inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
44 inMemoryDataTree.setSchemaContext(schemaContext);
47 public static InputStream getDatastoreTestInputStream() {
48 return getInputStream(ODL_DATASTORE_TEST_YANG);
51 private static InputStream getInputStream(final String resourceName) {
52 return TestModel.class.getResourceAsStream(ODL_DATASTORE_TEST_YANG);
55 public static SchemaContext createTestContext() throws ReactorException {
56 return RetestUtils.parseYangStreams(Collections.singletonList(getDatastoreTestInputStream()));
60 public void testWriteMerge1() throws DataValidationFailedException {
61 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
62 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
63 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
64 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
65 .withChild(fooEntryNode).build();
66 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
67 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
68 .withChild(barEntryNode).build();
70 final ContainerNode cont1 = Builders.containerBuilder()
71 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
72 .withChild(mapNode1).build();
74 final ContainerNode cont2 = Builders.containerBuilder()
75 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
76 .withChild(mapNode2).build();
78 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
79 modificationTree.write(TestModel.TEST_PATH, cont1);
80 modificationTree.merge(TestModel.TEST_PATH, cont2);
81 modificationTree.ready();
83 inMemoryDataTree.validate(modificationTree);
84 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
85 inMemoryDataTree.commit(prepare);
87 final InMemoryDataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
88 final InMemoryDataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
89 final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
90 assertTrue(readNode.isPresent());
91 assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).getValue().size());