04d889fbe0f61eaa822b9b286c9527ad1b1e5447
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / CompositeModificationPayloadTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Test;
6 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
7 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
8 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
9 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
10 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
11 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
12 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
13 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
14 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
15
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 public class CompositeModificationPayloadTest {
24
25
26     private static final String SERIALIZE_OUT = "serialize.out";
27
28     @After
29     public void shutDown(){
30         File f = new File(SERIALIZE_OUT);
31         if(f.exists()){
32             f.delete();
33         }
34     }
35
36     @Test
37     public void testBasic() throws IOException {
38
39         List<ReplicatedLogEntry> entries = new ArrayList<>();
40
41         entries.add(0, new ReplicatedLogEntry() {
42             @Override public Payload getData() {
43                 WriteModification writeModification =
44                     new WriteModification(TestModel.TEST_PATH, ImmutableNodes
45                         .containerNode(TestModel.TEST_QNAME),
46                         TestModel.createTestContext());
47
48                 MutableCompositeModification compositeModification =
49                     new MutableCompositeModification();
50
51                 compositeModification.addModification(writeModification);
52
53                 return new CompositeModificationPayload(compositeModification.toSerializable());
54             }
55
56             @Override public long getTerm() {
57                 return 1;
58             }
59
60             @Override public long getIndex() {
61                 return 1;
62             }
63         });
64
65         AppendEntries appendEntries =
66             new AppendEntries(1, "member-1", 0, 100, entries, 1);
67
68         AppendEntriesMessages.AppendEntries o = (AppendEntriesMessages.AppendEntries) appendEntries.toSerializable();
69
70         o.writeDelimitedTo(new FileOutputStream(SERIALIZE_OUT));
71
72         AppendEntriesMessages.AppendEntries appendEntries2 =
73             AppendEntriesMessages.AppendEntries
74                 .parseDelimitedFrom(new FileInputStream(SERIALIZE_OUT));
75
76         AppendEntries appendEntries1 = AppendEntries.fromSerializable(appendEntries2);
77
78         Payload data = appendEntries1.getEntries().get(0).getData();
79
80
81         Assert.assertTrue(((CompositeModificationPayload) data).getModification().toString().contains(TestModel.TEST_QNAME.getNamespace().toString()));
82
83     }
84
85 }