Bug 1608: Make ActorContext ASK_DURATION configurable
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / MockActorContext.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.datastore.utils;
10
11 import static org.junit.Assert.assertNotNull;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.actor.ActorSystem;
15
16 public class MockActorContext extends ActorContext {
17
18     private volatile Object executeShardOperationResponse;
19     private volatile Object executeRemoteOperationResponse;
20     private volatile Object executeLocalOperationResponse;
21     private volatile Object executeLocalShardOperationResponse;
22     private volatile Exception executeRemoteOperationFailure;
23     private volatile Object inputMessage;
24
25     public MockActorContext(ActorSystem actorSystem) {
26         super(actorSystem, null, new MockClusterWrapper(), new MockConfiguration());
27     }
28
29     public MockActorContext(ActorSystem actorSystem, ActorRef shardManager) {
30         super(actorSystem, shardManager, new MockClusterWrapper(), new MockConfiguration());
31     }
32
33
34     @Override public Object executeShardOperation(String shardName,
35         Object message) {
36         return executeShardOperationResponse;
37     }
38
39     @Override public Object executeRemoteOperation(ActorSelection actor,
40         Object message) {
41         return executeRemoteOperationResponse;
42     }
43
44     @Override public ActorSelection findPrimary(String shardName) {
45         return null;
46     }
47
48     public void setExecuteShardOperationResponse(Object response){
49         executeShardOperationResponse = response;
50     }
51
52     public void setExecuteRemoteOperationResponse(Object response){
53         executeRemoteOperationResponse = response;
54     }
55
56     public void setExecuteRemoteOperationFailure(Exception executeRemoteOperationFailure) {
57         this.executeRemoteOperationFailure = executeRemoteOperationFailure;
58     }
59
60     public void setExecuteLocalOperationResponse(
61         Object executeLocalOperationResponse) {
62         this.executeLocalOperationResponse = executeLocalOperationResponse;
63     }
64
65     public void setExecuteLocalShardOperationResponse(
66         Object executeLocalShardOperationResponse) {
67         this.executeLocalShardOperationResponse = executeLocalShardOperationResponse;
68     }
69
70     @SuppressWarnings("unchecked")
71     public <T> T getInputMessage(Class<T> expType) throws Exception {
72         assertNotNull("Input message was null", inputMessage);
73         return (T) expType.getMethod("fromSerializable", Object.class).invoke(null, inputMessage);
74     }
75
76     @Override
77     public Object executeLocalOperation(ActorRef actor,
78         Object message) {
79         return this.executeLocalOperationResponse;
80     }
81
82     @Override
83     public Object executeLocalShardOperation(String shardName,
84         Object message) {
85         return this.executeLocalShardOperationResponse;
86     }
87 }