Remove redundant modifiers
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / Retest_Bug2690Fix.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
9 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10
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;
33
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;
38
39     @Before
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);
45     }
46
47     public static InputStream getDatastoreTestInputStream() {
48         return getInputStream(ODL_DATASTORE_TEST_YANG);
49     }
50
51     private static InputStream getInputStream(final String resourceName) {
52         return TestModel.class.getResourceAsStream(ODL_DATASTORE_TEST_YANG);
53     }
54
55     public static SchemaContext createTestContext() throws ReactorException {
56         return RetestUtils.parseYangStreams(Collections.singletonList(getDatastoreTestInputStream()));
57     }
58
59     @Test
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();
69
70         final ContainerNode cont1 = Builders.containerBuilder()
71                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
72                 .withChild(mapNode1).build();
73
74         final ContainerNode cont2 = Builders.containerBuilder()
75                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
76                 .withChild(mapNode2).build();
77
78         final InMemoryDataTreeModification modificationTree = inMemoryDataTree.takeSnapshot().newModification();
79         modificationTree.write(TestModel.TEST_PATH, cont1);
80         modificationTree.merge(TestModel.TEST_PATH, cont2);
81         modificationTree.ready();
82
83         inMemoryDataTree.validate(modificationTree);
84         final DataTreeCandidate prepare = inMemoryDataTree.prepare(modificationTree);
85         inMemoryDataTree.commit(prepare);
86
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());
92
93     }
94
95 }