Merge "Implement behavior common to a RaftActor in all it's 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 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
26     public MockRaftActorContext(){
27
28     }
29
30     public MockRaftActorContext(String id, ActorSystem system, ActorRef actor){
31         this.id = id;
32         this.system = system;
33         this.actor = actor;
34     }
35
36     @Override public ActorRef actorOf(Props props) {
37         return system.actorOf(props);
38     }
39
40     @Override public ActorSelection actorSelection(String path) {
41         return system.actorSelection(path);
42     }
43
44     @Override public String getId() {
45         return id;
46     }
47
48     @Override public ActorRef getActor() {
49         return actor;
50     }
51
52     @Override public ElectionTerm getTermInformation() {
53         return new ElectionTermImpl(this.id);
54     }
55
56     public void setIndex(AtomicLong index){
57         this.index = index;
58     }
59
60     @Override public AtomicLong getCommitIndex() {
61         return index;
62     }
63
64     public void setLastApplied(AtomicLong lastApplied){
65         this.lastApplied = lastApplied;
66     }
67
68     @Override public AtomicLong getLastApplied() {
69         return lastApplied;
70     }
71
72     @Override public ReplicatedLog getReplicatedLog() {
73         return new ReplicatedLog(){
74
75             @Override public ReplicatedLogEntry getReplicatedLogEntry(
76                 long index) {
77                 throw new UnsupportedOperationException(
78                     "getReplicatedLogEntry");
79             }
80
81             @Override public ReplicatedLogEntry last() {
82                 return new ReplicatedLogEntry() {
83                     @Override public Object getData() {
84                         return null;
85                     }
86
87                     @Override public long getTerm() {
88                         return 1;
89                     }
90
91                     @Override public long getIndex() {
92                         return 1;
93                     }
94                 };
95             }
96         };
97     }
98 }