Bug-2397:Provide a mechanism for stakeholders to get notifications on Raft state...
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / MessageCollectorActor.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 akka.actor.ActorRef;
12 import akka.actor.UntypedActor;
13
14 import akka.pattern.Patterns;
15 import akka.util.Timeout;
16 import com.google.common.collect.Lists;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.concurrent.TimeUnit;
20 import scala.concurrent.Await;
21 import scala.concurrent.Future;
22 import scala.concurrent.duration.Duration;
23 import scala.concurrent.duration.FiniteDuration;
24
25 /**
26  * MessageCollectorActor collects messages as it receives them. It can send
27  * those collected messages to any sender which sends it the "messages" message
28  * <p>
29  *     This class would be useful as a mock to test whether messages were sent
30  *     to a remote actor or not.
31  * </p>
32  */
33 public class MessageCollectorActor extends UntypedActor {
34     private List<Object> messages = new ArrayList<>();
35
36     @Override public void onReceive(Object message) throws Exception {
37         if(message instanceof String){
38             if("messages".equals(message)){
39                 getSender().tell(new ArrayList(messages), getSelf());
40             }
41         } else {
42             messages.add(message);
43         }
44     }
45
46     public static List<Object> getAllMessages(ActorRef actor) throws Exception {
47         FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
48         Timeout operationTimeout = new Timeout(operationDuration);
49         Future<Object> future = Patterns.ask(actor, "messages", operationTimeout);
50
51         try {
52             return (List<Object>) Await.result(future, operationDuration);
53         } catch (Exception e) {
54             throw e;
55         }
56     }
57
58     /**
59      * Get the first message that matches the specified class
60      * @param actor
61      * @param clazz
62      * @return
63      */
64     public static Object getFirstMatching(ActorRef actor, Class<?> clazz) throws Exception {
65         List<Object> allMessages = getAllMessages(actor);
66
67         for(Object message : allMessages){
68             if(message.getClass().equals(clazz)){
69                 return message;
70             }
71         }
72
73         return null;
74     }
75
76     public static List<Object> getAllMatching(ActorRef actor, Class<?> clazz) throws Exception {
77         List<Object> allMessages = getAllMessages(actor);
78
79         List<Object> output = Lists.newArrayList();
80
81         for(Object message : allMessages){
82             if(message.getClass().equals(clazz)){
83                 output.add(message);
84             }
85         }
86
87         return output;
88     }
89
90 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.