Initial code for RaftActorServerConfigurationSupport
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / utils / ForwardMessageToBehaviorActor.java
1 /*
2  * Copyright (c) 2015 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.utils;
10
11 import static org.junit.Assert.assertTrue;
12 import akka.actor.Props;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
16
17 public class ForwardMessageToBehaviorActor extends MessageCollectorActor {
18     private volatile RaftActorBehavior behavior;
19     private final List<RaftActorBehavior> behaviorChanges = new ArrayList<>();
20
21     @Override
22     public void onReceive(Object message) throws Exception {
23         if(behavior != null) {
24             behaviorChanges.add(behavior.handleMessage(sender(), message));
25         }
26         super.onReceive(message);
27     }
28
29     public static Props props() {
30         return Props.create(ForwardMessageToBehaviorActor.class);
31     }
32
33     public void setBehavior(RaftActorBehavior behavior){
34         this.behavior = behavior;
35     }
36
37     public RaftActorBehavior getFirstBehaviorChange() {
38         assertTrue("no behavior changes present", behaviorChanges.size() > 0);
39         return behaviorChanges.get(0);
40     }
41
42     public RaftActorBehavior getLastBehaviorChange() {
43         assertTrue("no behavior changes present", behaviorChanges.size() > 0);
44         return behaviorChanges.get(behaviorChanges.size() - 1);
45     }
46
47     public List<RaftActorBehavior> getBehaviorChanges(){
48         return behaviorChanges;
49     }
50
51     @Override
52     public void clear() {
53         super.clear();
54         behaviorChanges.clear();
55     }
56 }
57