caffa698f14311949657021ac69216ad2088b7f2
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / common / actor / QuarantinedMonitorActorTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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 package org.opendaylight.controller.cluster.common.actor;
9
10 import static org.mockito.Mockito.never;
11 import static org.mockito.Mockito.timeout;
12 import static org.mockito.Mockito.verify;
13
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSystem;
16 import akka.actor.Address;
17 import akka.event.Logging;
18 import akka.japi.Effect;
19 import akka.remote.AssociationErrorEvent;
20 import akka.remote.InvalidAssociation;
21 import akka.remote.ThisActorSystemQuarantinedEvent;
22 import akka.testkit.javadsl.TestKit;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import scala.Option;
29
30 public class QuarantinedMonitorActorTest {
31
32     private static final Address LOCAL = Address.apply("http", "local");
33     private static final Address REMOTE = Address.apply("http", "remote");
34
35     @Mock
36     private Effect callback;
37     private ActorSystem system;
38     private ActorRef actor;
39
40     @Before
41     public void setUp() {
42         MockitoAnnotations.initMocks(this);
43         system = ActorSystem.apply();
44         actor = system.actorOf(QuarantinedMonitorActor.props(callback));
45     }
46
47     @After
48     public void tearDown() {
49         TestKit.shutdownActorSystem(system);
50     }
51
52     @Test
53     public void testOnReceiveQuarantined() throws Exception {
54         final Throwable t = new RuntimeException("Remote has quarantined this system");
55         final InvalidAssociation cause = InvalidAssociation.apply(LOCAL, REMOTE, t, Option.apply(null));
56         final ThisActorSystemQuarantinedEvent event = new ThisActorSystemQuarantinedEvent(LOCAL, REMOTE);
57         actor.tell(event, ActorRef.noSender());
58         verify(callback, timeout(1000)).apply();
59     }
60
61     @Test
62     public void testOnReceiveQuarantinedAsAssociation() throws Exception {
63         for (int i = 0; i < 9; i++) {
64             final Throwable t =
65                     new RuntimeException("The remote system has a UID that has been quarantined. Association aborted.");
66             final InvalidAssociation cause = InvalidAssociation.apply(LOCAL, REMOTE, t, Option.apply(null));
67             final AssociationErrorEvent event =
68                     new AssociationErrorEvent(cause, LOCAL, REMOTE, true, Logging.ErrorLevel());
69             actor.tell(event, ActorRef.noSender());
70         }
71
72         final Address local1 = Address.apply("http", "local1");
73         final Address remote1 = Address.apply("http", "remote1");
74         final Throwable t1 =
75                 new RuntimeException("The remote system has a UID that has been quarantined. Association aborted.");
76         final InvalidAssociation cause1 = InvalidAssociation.apply(local1, remote1, t1, Option.apply(null));
77         final AssociationErrorEvent event1 =
78                 new AssociationErrorEvent(cause1, local1, remote1, true, Logging.ErrorLevel());
79         actor.tell(event1, ActorRef.noSender());
80         verify(callback, timeout(1000)).apply();
81     }
82
83     @Test
84     public void testOnReceiveAnother() throws Exception {
85         final Address local = Address.apply("http", "local");
86         final Address remote = Address.apply("http", "remote");
87         final Throwable t = new RuntimeException("Another exception");
88         final InvalidAssociation cause = InvalidAssociation.apply(local, remote, t, Option.apply(null));
89         final AssociationErrorEvent event = new AssociationErrorEvent(cause, local, remote, true, Logging.ErrorLevel());
90         actor.tell(event, ActorRef.noSender());
91         verify(callback, never()).apply();
92     }
93
94 }

©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.