67234164a7b81f18c292364401e7c9282a36f4d9
[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 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.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
32 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
33
34 public class Bug2690FixTest {
35     private static final String ODL_DATASTORE_TEST_YANG = "/odl-datastore-test.yang";
36     private SchemaContext schemaContext;
37
38     private InMemoryDataTree inMemoryDataTree;
39
40     @Before
41     public void prepare() throws IOException, YangSyntaxErrorException {
42         schemaContext = createTestContext();
43         assertNotNull("Schema context must not be null.", schemaContext);
44         inMemoryDataTree =  (InMemoryDataTree) InMemoryDataTreeFactory.getInstance().create();
45         inMemoryDataTree.setSchemaContext(schemaContext);
46     }
47
48     private static ByteSource getInputStream() {
49         return Resources.asByteSource(TestModel.class.getResource(ODL_DATASTORE_TEST_YANG));
50     }
51
52     public static SchemaContext createTestContext() throws IOException, YangSyntaxErrorException {
53         final YangParserImpl parser = new YangParserImpl();
54         return parser.parseSources(Collections.singletonList(getInputStream()));
55     }
56
57     @Test
58     public void testWriteMerge1() throws DataValidationFailedException {
59         final MapEntryNode fooEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1);
60         final MapEntryNode barEntryNode = ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2);
61         final MapNode mapNode1 = ImmutableNodes.mapNodeBuilder()
62                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
63                 .withChild(fooEntryNode).build();
64         final MapNode mapNode2 = ImmutableNodes.mapNodeBuilder()
65                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME))
66                 .withChild(barEntryNode).build();
67
68         final ContainerNode cont1 = Builders.containerBuilder()
69                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
70                 .withChild(mapNode1).build();
71
72         final ContainerNode cont2 = Builders.containerBuilder()
73                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
74                 .withChild(mapNode2).build();
75
76         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
77         modificationTree.write(TestModel.TEST_PATH, cont1);
78         modificationTree.merge(TestModel.TEST_PATH, cont2);
79         modificationTree.ready();
80
81         inMemoryDataTree.validate(modificationTree);
82         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
83         inMemoryDataTree.commit(prepare);
84
85         final InMemoryDataTreeSnapshot snapshotAfterTx = inMemoryDataTree.takeSnapshot();
86         final InMemoryDataTreeModification modificationAfterTx = snapshotAfterTx.newModification();
87         final Optional<NormalizedNode<?, ?>> readNode = modificationAfterTx.readNode(TestModel.OUTER_LIST_PATH);
88         assertTrue(readNode.isPresent());
89         assertEquals(2, ((NormalizedNodeContainer<?,?,?>)readNode.get()).getValue().size());
90
91     }
92
93
94 }