2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.modification;
10 import static org.junit.Assert.assertEquals;
12 import org.apache.commons.lang.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.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
22 public class MutableCompositeModificationTest extends AbstractModificationTest {
24 public void testApply() throws Exception {
25 MutableCompositeModification compositeModification = new MutableCompositeModification();
26 compositeModification.addModification(new WriteModification(TestModel.TEST_PATH,
27 ImmutableNodes.containerNode(TestModel.TEST_QNAME)));
29 DOMStoreReadWriteTransaction transaction = store.newReadWriteTransaction();
30 compositeModification.apply(transaction);
31 commitTransaction(transaction);
33 assertEquals(TestModel.TEST_QNAME, readData(TestModel.TEST_PATH).get().getIdentifier().getNodeType());
37 public void testSerialization() {
38 YangInstanceIdentifier writePath = TestModel.TEST_PATH;
39 NormalizedNode writeData = ImmutableContainerNodeBuilder.create()
40 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
41 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
43 YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
44 NormalizedNode mergeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
45 new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
47 YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
49 MutableCompositeModification compositeModification =
50 new MutableCompositeModification(DataStoreVersions.SODIUM_SR1_VERSION);
51 compositeModification.addModification(new WriteModification(writePath, writeData));
52 compositeModification.addModification(new MergeModification(mergePath, mergeData));
53 compositeModification.addModification(new DeleteModification(deletePath));
55 final byte[] bytes = SerializationUtils.serialize(compositeModification);
56 assertEquals(360, bytes.length);
57 MutableCompositeModification clone = (MutableCompositeModification) SerializationUtils.deserialize(bytes);
59 assertEquals("getVersion", DataStoreVersions.SODIUM_SR1_VERSION, clone.getVersion());
61 assertEquals("getModifications size", 3, clone.getModifications().size());
63 WriteModification write = (WriteModification)clone.getModifications().get(0);
64 assertEquals("getVersion", DataStoreVersions.SODIUM_SR1_VERSION, write.getVersion());
65 assertEquals("getPath", writePath, write.getPath());
66 assertEquals("getData", writeData, write.getData());
68 MergeModification merge = (MergeModification)clone.getModifications().get(1);
69 assertEquals("getVersion", DataStoreVersions.SODIUM_SR1_VERSION, merge.getVersion());
70 assertEquals("getPath", mergePath, merge.getPath());
71 assertEquals("getData", mergeData, merge.getData());
73 DeleteModification delete = (DeleteModification)clone.getModifications().get(2);
74 assertEquals("getVersion", DataStoreVersions.SODIUM_SR1_VERSION, delete.getVersion());
75 assertEquals("getPath", deletePath, delete.getPath());
79 public void testSerializationModern() {
80 YangInstanceIdentifier writePath = TestModel.TEST_PATH;
81 NormalizedNode writeData = ImmutableContainerNodeBuilder.create()
82 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
83 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
85 YangInstanceIdentifier mergePath = TestModel.OUTER_LIST_PATH;
86 NormalizedNode mergeData = ImmutableContainerNodeBuilder.create().withNodeIdentifier(
87 new YangInstanceIdentifier.NodeIdentifier(TestModel.OUTER_LIST_QNAME)).build();
89 YangInstanceIdentifier deletePath = TestModel.TEST_PATH;
91 MutableCompositeModification compositeModification =
92 new MutableCompositeModification();
93 compositeModification.addModification(new WriteModification(writePath, writeData));
94 compositeModification.addModification(new MergeModification(mergePath, mergeData));
95 compositeModification.addModification(new DeleteModification(deletePath));
97 final byte[] bytes = SerializationUtils.serialize(compositeModification);
98 assertEquals(360, bytes.length);
99 MutableCompositeModification clone = (MutableCompositeModification) SerializationUtils.deserialize(bytes);
101 assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, clone.getVersion());
103 assertEquals("getModifications size", 3, clone.getModifications().size());
105 WriteModification write = (WriteModification)clone.getModifications().get(0);
106 assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, write.getVersion());
107 assertEquals("getPath", writePath, write.getPath());
108 assertEquals("getData", writeData, write.getData());
110 MergeModification merge = (MergeModification)clone.getModifications().get(1);
111 assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, merge.getVersion());
112 assertEquals("getPath", mergePath, merge.getPath());
113 assertEquals("getData", mergeData, merge.getData());
115 DeleteModification delete = (DeleteModification)clone.getModifications().get(2);
116 assertEquals("getVersion", DataStoreVersions.CURRENT_VERSION, delete.getVersion());
117 assertEquals("getPath", deletePath, delete.getPath());