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;
13 import com.google.common.base.Optional;
14 import com.google.common.io.ByteSource;
15 import com.google.common.io.Resources;
16 import java.io.IOException;
17 import java.util.Collections;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
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.model.parser.api.YangSyntaxErrorException;
33 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
35 public class Bug2690FixTest {
36 private static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
37 private SchemaContext schemaContext;
39 private InMemoryDataTree inMemoryDataTree;
42 public void prepare() throws IOException, YangSyntaxErrorException {
43 schemaContext = createTestContext();
44 assertNotNull("Schema context must not be null.", schemaContext);
45 inMemoryDataTree = (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create(TreeType.OPERATIONAL);
46 inMemoryDataTree.setSchemaContext(schemaContext);
49 private static ByteSource getInputStream() {
50 return Resources.asByteSource(TestModel.class.getResource(ODL_DATASTORE_TEST_YANG));
53 public static SchemaContext createTestContext() throws IOException, YangSyntaxErrorException {
54 final YangParserImpl parser = new YangParserImpl();
55 return parser.parseSources(Collections.singletonList(getInputStream()));
59 public void testWriteMerge1() throws DataValidationFailedException {
60 final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
61 final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
62 final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
63 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
64 .withChild(fooEntryNode).build();
65 final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
66 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
67 .withChild(barEntryNode).build();
69 final ContainerNode cont1 = Builders.containerBuilder()
70 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
71 .withChild(mapNode1).build();
73 final ContainerNode cont2 = Builders.containerBuilder()
74 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
75 .withChild(mapNode2).build();
77 final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
78 modificationTree.write(TestModel.TEST_PATH, cont1);
79 modificationTree.merge(TestModel.TEST_PATH, cont2);
80 modificationTree.ready();
82 inMemoryDataTree.validate(modificationTree);
83 final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
84 inMemoryDataTree.commit(prepare);
86 final InMemoryDataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
87 final InMemoryDataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
88 final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
89 assertTrue(readNode.isPresent());
90 assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).getValue().size());