7df1c2f045d98fcdfa87f37c71165462d0e2d5eb
[controller.git] / opendaylight / md-sal / cds-access-client / src / test / java / org / opendaylight / controller / cluster / access / client / ClientActorContextTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.controller.cluster.access.client;
9
10 import static org.junit.Assert.assertSame;
11
12 import akka.actor.ActorSystem;
13 import akka.testkit.TestProbe;
14 import akka.testkit.javadsl.TestKit;
15 import com.google.common.base.Ticker;
16 import java.util.concurrent.TimeUnit;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
23 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
24 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
25 import org.opendaylight.controller.cluster.access.concepts.MemberName;
26 import scala.concurrent.duration.FiniteDuration;
27
28 public class ClientActorContextTest {
29     private static final MemberName MEMBER_NAME = MemberName.forName("member-1");
30     private static final FrontendType FRONTEND_TYPE =
31             FrontendType.forName(ClientActorContextTest.class.getSimpleName());
32     private static final FrontendIdentifier FRONTEND_ID = FrontendIdentifier.create(MEMBER_NAME, FRONTEND_TYPE);
33     private static final ClientIdentifier CLIENT_ID = ClientIdentifier.create(FRONTEND_ID, 0);
34     private static final String PERSISTENCE_ID = ClientActorContextTest.class.getSimpleName();
35
36     @Mock
37     private InternalCommand<? extends BackendInfo> command;
38     private ActorSystem system;
39     private TestProbe probe;
40     private ClientActorContext ctx;
41
42     @Before
43     public void setup() {
44         MockitoAnnotations.initMocks(this);
45         system = ActorSystem.apply();
46         probe = new TestProbe(system);
47         ctx = new ClientActorContext(probe.ref(), PERSISTENCE_ID, system,
48                 CLIENT_ID, AccessClientUtil.newMockClientActorConfig());
49     }
50
51     @Test
52     public void testMockingControl() {
53         assertSame(CLIENT_ID, ctx.getIdentifier());
54         assertSame(PERSISTENCE_ID, ctx.persistenceId());
55         assertSame(probe.ref(), ctx.self());
56     }
57
58     @Test
59     public void testTicker() {
60         assertSame(Ticker.systemTicker(), ctx.ticker());
61     }
62
63     @Test
64     public void testExecuteInActor() {
65         ctx.executeInActor(command);
66         probe.expectMsg(command);
67     }
68
69     @Test
70     public void testExecuteInActorScheduled() {
71         ctx.executeInActor(command, FiniteDuration.create(1, TimeUnit.SECONDS));
72         probe.expectMsg(command);
73     }
74
75     @After
76     public void tearDown() {
77         TestKit.shutdownActorSystem(system);
78     }
79 }