Fix followerDistributedDataStore tear down
[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 package org.opendaylight.controller.cluster.datastore.modification;
9
10 import static org.junit.Assert.assertEquals;
11
12 import org.apache.commons.lang3.SerializationUtils;
13 import org.junit.Test;
14 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
15 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
16 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22
23 @Deprecated(since = "9.0.0", forRemoval = true)
24 public class MutableCompositeModificationTest extends AbstractModificationTest {
25     @Test
26     public void testApply() throws Exception {
27         MutableCompositeModification compositeModification = new MutableCompositeModification();
28         compositeModification.addModification(new WriteModification(TestModel.TEST_PATH,
29             ImmutableNodes.containerNode(TestModel.TEST_QNAME)));
30
31         DOMStoreReadWriteTransaction transaction = store.newReadWriteTransaction();
32         compositeModification.apply(transaction);
33         commitTransaction(transaction);
34
35         assertEquals(TestModel.TEST_QNAME, readData(TestModel.TEST_PATH).orElseThrow().name().getNodeType());
36     }
37
38     @Test
39     public void testSerialization() {
40         YangInstanceIdentifier writePath = TestModel.TEST_PATH;
41         ContainerNode writeData = Builders.containerBuilder()
42             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
43             .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo"))
44             .build();
45
46         YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
47         ContainerNode mergeData = Builders.containerBuilder()
48             .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
49             .build();
50
51         YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
52
53         MutableCompositeModification compositeModification =
54             new MutableCompositeModification(DataStoreVersions.POTASSIUM_VERSION);
55         compositeModification.addModification(new WriteModification(writePath, writeData));
56         compositeModification.addModification(new MergeModification(mergePath, mergeData));
57         compositeModification.addModification(new DeleteModification(deletePath));
58
59         final byte[] bytes = SerializationUtils.serialize(compositeModification);
60         assertEquals(360, bytes.length);
61         MutableCompositeModification clone = (MutableCompositeModification) SerializationUtils.deserialize(bytes);
62
63         assertEquals("getVersion", DataStoreVersions.POTASSIUM_VERSION, clone.getVersion());
64
65         assertEquals("getModifications size", 3, clone.getModifications().size());
66
67         WriteModification write = (WriteModification)clone.getModifications().get(0);
68         assertEquals("getVersion", DataStoreVersions.POTASSIUM_VERSION, write.getVersion());
69         assertEquals("getPath", writePath, write.getPath());
70         assertEquals("getData", writeData, write.getData());
71
72         MergeModification merge = (MergeModification)clone.getModifications().get(1);
73         assertEquals("getVersion", DataStoreVersions.POTASSIUM_VERSION, merge.getVersion());
74         assertEquals("getPath", mergePath, merge.getPath());
75         assertEquals("getData", mergeData, merge.getData());
76
77         DeleteModification delete = (DeleteModification)clone.getModifications().get(2);
78         assertEquals("getVersion", DataStoreVersions.POTASSIUM_VERSION, delete.getVersion());
79         assertEquals("getPath", deletePath, delete.getPath());
80     }
81
82     @Test
83     public void testSerializationModern() {
84         YangInstanceIdentifier writePath = TestModel.TEST_PATH;
85         ContainerNode writeData = Builders.containerBuilder()
86             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
87             .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo"))
88             .build();
89
90         YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
91         ContainerNode mergeData = Builders.containerBuilder()
92             .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
93             .build();
94
95         YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
96
97         MutableCompositeModification compositeModification =
98             new MutableCompositeModification();
99         compositeModification.addModification(new WriteModification(writePath, writeData));
100         compositeModification.addModification(new MergeModification(mergePath, mergeData));
101         compositeModification.addModification(new DeleteModification(deletePath));
102
103         final byte[] bytes = SerializationUtils.serialize(compositeModification);
104         assertEquals(360, bytes.length);
105         MutableCompositeModification clone = (MutableCompositeModification) SerializationUtils.deserialize(bytes);
106
107         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, clone.getVersion());
108
109         assertEquals("getModifications size", 3, clone.getModifications().size());
110
111         WriteModification write = (WriteModification)clone.getModifications().get(0);
112         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, write.getVersion());
113         assertEquals("getPath", writePath, write.getPath());
114         assertEquals("getData", writeData, write.getData());
115
116         MergeModification merge = (MergeModification)clone.getModifications().get(1);
117         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, merge.getVersion());
118         assertEquals("getPath", mergePath, merge.getPath());
119         assertEquals("getData", mergeData, merge.getData());
120
121         DeleteModification delete = (DeleteModification)clone.getModifications().get(2);
122         assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, delete.getVersion());
123         assertEquals("getPath", deletePath, delete.getPath());
124     }
125 }