Bug 2690 - Yang-Data-Impl: write and then merge on same list in
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / Bug2690FixTest.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 import com.google.common.base.Optional;
14 import java.io.InputStream;
15 import java.util.Collections;
16 import java.util.Set;
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.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
32
33 public class Bug2690FixTest {
34     private static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
35     private SchemaContext schemaContext;
36     private RootModificationApplyOperation rootOper;
37
38     private InMemoryDataTree inMemoryDataTree;
39
40     @Before
41     public void prepare() {
42         schemaContext = createTestContext();
43         assertNotNull("Schema context must not be null.", schemaContext);
44         rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext));
45         inMemoryDataTree =  (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create();
46         inMemoryDataTree.setSchemaContext(schemaContext);
47     }
48
49     public static final InputStream getDatastoreTestInputStream() {
50         return getInputStream(ODL_DATASTORE_TEST_YANG);
51     }
52
53     private static InputStream getInputStream(final String resourceName) {
54         return TestModel.class.getResourceAsStream(ODL_DATASTORE_TEST_YANG);
55     }
56
57     public static SchemaContext createTestContext() {
58         final YangParserImpl parser = new YangParserImpl();
59         final Set<Module> modules = parser.parseYangModelsFromStreams(Collections.singletonList(getDatastoreTestInputStream()));
60         return parser.resolveSchemaContext(modules);
61     }
62
63     @Test
64     public void testWriteMerge1() throws DataValidationFailedException {
65         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
66         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
67         final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
68                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
69                 .withChild(fooEntryNode).build();
70         final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
71                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
72                 .withChild(barEntryNode).build();
73
74         final ContainerNode cont1 = Builders.containerBuilder()
75                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
76                 .withChild(mapNode1).build();
77
78         final ContainerNode cont2 = Builders.containerBuilder()
79                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
80                 .withChild(mapNode2).build();
81
82         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
83         modificationTree.write(TestModel.TEST_PATH, cont1);
84         modificationTree.merge(TestModel.TEST_PATH, cont2);
85
86         inMemoryDataTree.validate(modificationTree);
87         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
88         inMemoryDataTree.commit(prepare);
89
90         final InMemoryDataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
91         final InMemoryDataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
92         final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
93         assertTrue(readNode.isPresent());
94         assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).getValue().size());
95
96     }
97
98
99 }