e57ae8fcedf4fd449c2027dd5740d1b022b51523
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / AbstractTest.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.datastore;
9
10 import akka.actor.ActorSystem;
11 import akka.testkit.javadsl.TestKit;
12 import com.typesafe.config.ConfigFactory;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.concurrent.CompletionStage;
16 import java.util.concurrent.atomic.AtomicLong;
17 import org.junit.After;
18 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
19 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
21 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
22 import org.opendaylight.controller.cluster.access.concepts.MemberName;
23 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
24 import scala.compat.java8.FutureConverters;
25 import scala.concurrent.Await;
26 import scala.concurrent.duration.Duration;
27
28 public abstract class AbstractTest {
29     protected static final MemberName MEMBER_NAME = MemberName.forName("member-1");
30     protected static final MemberName MEMBER_2_NAME = MemberName.forName("member-2");
31
32     private static final FrontendType FRONTEND_TYPE = FrontendType.forName(ShardTransactionTest.class.getSimpleName());
33
34     protected static final FrontendIdentifier FRONTEND_ID = FrontendIdentifier.create(MEMBER_NAME, FRONTEND_TYPE);
35
36     private static final ClientIdentifier CLIENT_ID = ClientIdentifier.create(FRONTEND_ID, 0);
37     private static final LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_ID, 0);
38     private static final AtomicLong HISTORY_COUNTER = new AtomicLong();
39     private static final AtomicLong TX_COUNTER = new AtomicLong();
40
41     private final Collection<ActorSystem> actorSystems = new ArrayList<>();
42
43     protected static void setUpStatic() {
44         HISTORY_COUNTER.set(1L);
45         TX_COUNTER.set(1L);
46     }
47
48     protected static TransactionIdentifier nextTransactionId() {
49         return new TransactionIdentifier(HISTORY_ID, TX_COUNTER.getAndIncrement());
50     }
51
52     protected static LocalHistoryIdentifier nextHistoryId() {
53         return new LocalHistoryIdentifier(CLIENT_ID, HISTORY_COUNTER.incrementAndGet());
54     }
55
56     protected static <T> T waitOnAsyncTask(final CompletionStage<T> completionStage, final Duration timeout)
57             throws Exception {
58         return Await.result(FutureConverters.toScala(completionStage), timeout);
59     }
60
61     @After
62     public void actorSystemCleanup() {
63         for (final ActorSystem system : actorSystems) {
64             TestKit.shutdownActorSystem(system, Boolean.TRUE);
65         }
66     }
67
68     protected ActorSystem newActorSystem(final String name, final String config) {
69         ActorSystem system = ActorSystem.create(name, ConfigFactory.load().getConfig(config));
70         actorSystems.add(system);
71         return system;
72     }
73 }