Properly handle RequestVote in all states
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / MockRaftActorContext.java
1 /*
2  * Copyright (c) 2014 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.raft;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.ActorSystem;
14 import akka.actor.Props;
15
16 public class MockRaftActorContext implements RaftActorContext {
17
18     private String id;
19     private ActorSystem system;
20     private ActorRef actor;
21     private long index = 0;
22     private long lastApplied = 0;
23     private final ElectionTerm electionTerm;
24     private ReplicatedLog replicatedLog;
25
26     public MockRaftActorContext(){
27         electionTerm = null;
28
29         initReplicatedLog();
30     }
31
32     public MockRaftActorContext(String id, ActorSystem system, ActorRef actor){
33         this.id = id;
34         this.system = system;
35         this.actor = actor;
36
37         electionTerm = new ElectionTermImpl(id);
38
39         initReplicatedLog();
40     }
41
42
43     public void initReplicatedLog(){
44         MockReplicatedLog mockReplicatedLog = new MockReplicatedLog();
45         this.replicatedLog = mockReplicatedLog;
46         mockReplicatedLog.setLast(new MockReplicatedLogEntry(1,1,""));
47         mockReplicatedLog.setReplicatedLogEntry(new MockReplicatedLogEntry(1,1, ""));
48     }
49
50     @Override public ActorRef actorOf(Props props) {
51         return system.actorOf(props);
52     }
53
54     @Override public ActorSelection actorSelection(String path) {
55         return system.actorSelection(path);
56     }
57
58     @Override public String getId() {
59         return id;
60     }
61
62     @Override public ActorRef getActor() {
63         return actor;
64     }
65
66     @Override public ElectionTerm getTermInformation() {
67         return electionTerm;
68     }
69
70     public void setIndex(long index){
71         this.index = index;
72     }
73
74     @Override public long getCommitIndex() {
75         return index;
76     }
77
78     @Override public void setCommitIndex(long commitIndex) {
79         this.index = commitIndex;
80     }
81
82     @Override public void setLastApplied(long lastApplied){
83         this.lastApplied = lastApplied;
84     }
85
86     @Override public long getLastApplied() {
87         return lastApplied;
88     }
89
90     public void setReplicatedLog(ReplicatedLog replicatedLog) {
91         this.replicatedLog = replicatedLog;
92     }
93
94     @Override public ReplicatedLog getReplicatedLog() {
95         return replicatedLog;
96     }
97
98     @Override public ActorSystem getActorSystem() {
99         return this.system;
100     }
101
102
103     public static class MockReplicatedLog implements ReplicatedLog {
104         private ReplicatedLogEntry replicatedLogEntry = new MockReplicatedLogEntry(0,0, "");
105         private ReplicatedLogEntry last = new MockReplicatedLogEntry(0,0, "");
106
107         @Override public ReplicatedLogEntry getReplicatedLogEntry(long index) {
108             return replicatedLogEntry;
109         }
110
111         @Override public ReplicatedLogEntry last() {
112             return last;
113         }
114
115         public void setReplicatedLogEntry(
116             ReplicatedLogEntry replicatedLogEntry) {
117             this.replicatedLogEntry = replicatedLogEntry;
118         }
119
120         public void setLast(ReplicatedLogEntry last) {
121             this.last = last;
122         }
123     }
124
125     public static class MockReplicatedLogEntry implements ReplicatedLogEntry {
126
127         private final long term;
128         private final long index;
129         private final Object data;
130
131         public MockReplicatedLogEntry(long term, long index, Object data){
132
133             this.term = term;
134             this.index = index;
135             this.data = data;
136         }
137
138         @Override public Object getData() {
139             return data;
140         }
141
142         @Override public long getTerm() {
143             return term;
144         }
145
146         @Override public long getIndex() {
147             return index;
148         }
149     }
150 }