Adjust to mdsal DOM read/exists FluentFuture change
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / modification / MutableCompositeModificationTest.java
1 /*
2  * Copyright (c) 2014, 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.controller.cluster.datastore.modification;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.util.Optional;
15 import org.apache.commons.lang.SerializationUtils;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
18 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
24
25 public class MutableCompositeModificationTest extends AbstractModificationTest {
26
27     @Test
28     public void testApply() throws Exception {
29
30         MutableCompositeModification compositeModification = new MutableCompositeModification();
31         compositeModification.addModification(new WriteModification(TestModel.TEST_PATH,
32             ImmutableNodes.containerNode(TestModel.TEST_QNAME)));
33
34         DOMStoreReadWriteTransaction transaction = store.newReadWriteTransaction();
35         compositeModification.apply(transaction);
36         commitTransaction(transaction);
37
38         Optional<NormalizedNode<?, ?>> data = readData(TestModel.TEST_PATH);
39
40         assertNotNull(data.get());
41         assertEquals(TestModel.TEST_QNAME, data.get().getNodeType());
42     }
43
44     @Test
45     public void testSerialization() {
46         YangInstanceIdentifier writePath = TestModel.TEST_PATH;
47         NormalizedNode<?, ?> writeData = ImmutableContainerNodeBuilder.create()
48                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
49                 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
50
51         YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
52         NormalizedNode<?, ?> mergeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
53                 new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
54
55         YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
56
57         MutableCompositeModification compositeModification = new MutableCompositeModification();
58         compositeModification.addModification(new WriteModification(writePath, writeData));
59         compositeModification.addModification(new MergeModification(mergePath, mergeData));
60         compositeModification.addModification(new DeleteModification(deletePath));
61
62         MutableCompositeModification clone = (MutableCompositeModification)
63                 SerializationUtils.clone(compositeModification);
64
65         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, clone.getVersion());
66
67         assertEquals("getModifications size", 3, clone.getModifications().size());
68
69         WriteModification write = (WriteModification)clone.getModifications().get(0);
70         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, write.getVersion());
71         assertEquals("getPath", writePath, write.getPath());
72         assertEquals("getData", writeData, write.getData());
73
74         MergeModification merge = (MergeModification)clone.getModifications().get(1);
75         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, merge.getVersion());
76         assertEquals("getPath", mergePath, merge.getPath());
77         assertEquals("getData", mergeData, merge.getData());
78
79         DeleteModification delete = (DeleteModification)clone.getModifications().get(2);
80         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, delete.getVersion());
81         assertEquals("getPath", deletePath, delete.getPath());
82     }
83 }