Remove JournalWriter.getLastEntry()
[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.FiniteDuration;
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 newTransactionId(final long txId) {
49         return new TransactionIdentifier(HISTORY_ID, txId);
50     }
51
52     protected static TransactionIdentifier nextTransactionId() {
53         return newTransactionId(TX_COUNTER.getAndIncrement());
54     }
55
56     protected static LocalHistoryIdentifier newHistoryId(final long historyId) {
57         return new LocalHistoryIdentifier(CLIENT_ID, historyId);
58     }
59
60     protected static LocalHistoryIdentifier nextHistoryId() {
61         return newHistoryId(HISTORY_COUNTER.incrementAndGet());
62     }
63
64     protected static <T> T waitOnAsyncTask(final CompletionStage<T> completionStage, final FiniteDuration timeout)
65             throws Exception {
66         return Await.result(FutureConverters.toScala(completionStage), timeout);
67     }
68
69     @After
70     public void actorSystemCleanup() {
71         for (final ActorSystem system : actorSystems) {
72             TestKit.shutdownActorSystem(system, true);
73         }
74     }
75
76     protected ActorSystem newActorSystem(final String name, final String config) {
77         ActorSystem system = ActorSystem.create(name, ConfigFactory.load().getConfig(config));
78         actorSystems.add(system);
79         return system;
80     }
81 }