Complete Candidate behavior implementation
[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 import java.util.concurrent.atomic.AtomicLong;
17
18 public class MockRaftActorContext implements RaftActorContext {
19
20     private String id;
21     private ActorSystem system;
22     private ActorRef actor;
23     private AtomicLong index = new AtomicLong(0);
24     private AtomicLong lastApplied = new AtomicLong(0);
25     private final ElectionTerm electionTerm;
26
27     public MockRaftActorContext(){
28         electionTerm = null;
29     }
30
31     public MockRaftActorContext(String id, ActorSystem system, ActorRef actor){
32         this.id = id;
33         this.system = system;
34         this.actor = actor;
35
36         electionTerm = new ElectionTermImpl(id);
37     }
38
39     @Override public ActorRef actorOf(Props props) {
40         return system.actorOf(props);
41     }
42
43     @Override public ActorSelection actorSelection(String path) {
44         return system.actorSelection(path);
45     }
46
47     @Override public String getId() {
48         return id;
49     }
50
51     @Override public ActorRef getActor() {
52         return actor;
53     }
54
55     @Override public ElectionTerm getTermInformation() {
56         return electionTerm;
57     }
58
59     public void setIndex(AtomicLong index){
60         this.index = index;
61     }
62
63     @Override public AtomicLong getCommitIndex() {
64         return index;
65     }
66
67     public void setLastApplied(AtomicLong lastApplied){
68         this.lastApplied = lastApplied;
69     }
70
71     @Override public AtomicLong getLastApplied() {
72         return lastApplied;
73     }
74
75     @Override public ReplicatedLog getReplicatedLog() {
76         return new ReplicatedLog(){
77
78             @Override public ReplicatedLogEntry getReplicatedLogEntry(
79                 long index) {
80                 throw new UnsupportedOperationException(
81                     "getReplicatedLogEntry");
82             }
83
84             @Override public ReplicatedLogEntry last() {
85                 return new ReplicatedLogEntry() {
86                     @Override public Object getData() {
87                         return null;
88                     }
89
90                     @Override public long getTerm() {
91                         return 1;
92                     }
93
94                     @Override public long getIndex() {
95                         return 1;
96                     }
97                 };
98             }
99         };
100     }
101
102     @Override public ActorSystem getActorSystem() {
103         return this.system;
104     }
105 }