81b6bccaf08ea0f35b5d4ed8c8a0fea8f3524796
[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 import com.google.common.base.Optional;
16
17 public class MockActorContext extends ActorContext {
18
19     private volatile Object executeShardOperationResponse;
20     private volatile Object executeRemoteOperationResponse;
21     private volatile Object executeLocalOperationResponse;
22     private volatile Object executeLocalShardOperationResponse;
23     private volatile Exception executeRemoteOperationFailure;
24     private volatile Object inputMessage;
25
26     public MockActorContext(ActorSystem actorSystem) {
27         super(actorSystem, null, new MockClusterWrapper(), new MockConfiguration());
28     }
29
30     public MockActorContext(ActorSystem actorSystem, ActorRef shardManager) {
31         super(actorSystem, shardManager, new MockClusterWrapper(), new MockConfiguration());
32     }
33
34     @Override public Object executeOperation(ActorSelection actor,
35                                              Object message) {
36         return executeRemoteOperationResponse;
37     }
38
39     @Override public Optional<ActorSelection> findPrimaryShard(String shardName) {
40         return Optional.absent();
41     }
42
43     public void setExecuteShardOperationResponse(Object response){
44         executeShardOperationResponse = response;
45     }
46
47     public void setExecuteRemoteOperationResponse(Object response){
48         executeRemoteOperationResponse = response;
49     }
50
51     public void setExecuteRemoteOperationFailure(Exception executeRemoteOperationFailure) {
52         this.executeRemoteOperationFailure = executeRemoteOperationFailure;
53     }
54
55     public void setExecuteLocalOperationResponse(
56         Object executeLocalOperationResponse) {
57         this.executeLocalOperationResponse = executeLocalOperationResponse;
58     }
59
60     public void setExecuteLocalShardOperationResponse(
61         Object executeLocalShardOperationResponse) {
62         this.executeLocalShardOperationResponse = executeLocalShardOperationResponse;
63     }
64
65     @SuppressWarnings("unchecked")
66     public <T> T getInputMessage(Class<T> expType) throws Exception {
67         assertNotNull("Input message was null", inputMessage);
68         return (T) expType.getMethod("fromSerializable", Object.class).invoke(null, inputMessage);
69     }
70
71     @Override
72     public Object executeOperation(ActorRef actor,
73                                    Object message) {
74         return this.executeLocalOperationResponse;
75     }
76
77 }