Skip to content

Commit

Permalink
Merge pull request #4724 from ghubstan/17-fix-testfixture-init
Browse files Browse the repository at this point in the history
Refactor api test fixture setup
  • Loading branch information
sqrrm committed Nov 2, 2020
2 parents f2cf5e2 + e8e55d2 commit ec54b4a
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ public boolean hasShutdownExceptions() {

@Override
public void logExceptions(List<Throwable> exceptions, org.slf4j.Logger log) {
StringBuilder errorBuilder = new StringBuilder();
for (Throwable t : exceptions) {
log.error("", t);
errorBuilder.append(t.getMessage()).append("\n");
}
}

Expand Down
47 changes: 37 additions & 10 deletions apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@
import java.util.stream.Collectors;

import static bisq.apitest.config.BisqAppConfig.alicedaemon;
import static bisq.apitest.config.BisqAppConfig.arbdaemon;
import static bisq.apitest.config.BisqAppConfig.bobdaemon;
import static bisq.common.app.DevEnv.DEV_PRIVILEGE_PRIV_KEY;
import static bisq.core.payment.payload.PaymentMethod.PERFECT_MONEY;
import static java.util.Arrays.stream;
import static java.util.Comparator.comparing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;



Expand All @@ -64,15 +67,39 @@ public class MethodTest extends ApiTestCase {
protected static GrpcStubs aliceStubs;
protected static GrpcStubs bobStubs;

protected PaymentAccount alicesDummyAcct;
protected PaymentAccount bobsDummyAcct;

protected final void initAlicesDummyPaymentAccount() {
alicesDummyAcct = getDefaultPerfectDummyPaymentAccount(alicedaemon);
}

protected final void initBobsDummyPaymentAccount() {
bobsDummyAcct = getDefaultPerfectDummyPaymentAccount(bobdaemon);
protected static PaymentAccount alicesDummyAcct;
protected static PaymentAccount bobsDummyAcct;

public static void startSupportingApps(boolean registerDisputeAgents,
boolean generateBtcBlock,
Enum<?>... supportingApps) {
try {
// To run Bisq apps in debug mode, use the other setUpScaffold method:
// setUpScaffold(new String[]{"--supportingApps", "bitcoind,seednode,arbdaemon,alicedaemon,bobdaemon",
// "--enableBisqDebugging", "true"});
setUpScaffold(supportingApps);
if (registerDisputeAgents) {
registerDisputeAgents(arbdaemon);
}

if (stream(supportingApps).map(Enum::name).anyMatch(name -> name.equals(alicedaemon.name()))) {
aliceStubs = grpcStubs(alicedaemon);
alicesDummyAcct = getDefaultPerfectDummyPaymentAccount(alicedaemon);
}

if (stream(supportingApps).map(Enum::name).anyMatch(name -> name.equals(bobdaemon.name()))) {
bobStubs = grpcStubs(bobdaemon);
bobsDummyAcct = getDefaultPerfectDummyPaymentAccount(bobdaemon);
}

// Generate 1 regtest block for alice's and/or bob's wallet to
// show 10 BTC balance, and allow time for daemons parse the new block.
if (generateBtcBlock)
genBtcBlocksThenWait(1, 1500);

} catch (Exception ex) {
fail(ex);
}
}

// Convenience methods for building gRPC request objects
Expand Down Expand Up @@ -185,7 +212,7 @@ protected final CreatePaymentAccountRequest createCreatePerfectMoneyPaymentAccou
.build();
}

protected final PaymentAccount getDefaultPerfectDummyPaymentAccount(BisqAppConfig bisqAppConfig) {
protected static PaymentAccount getDefaultPerfectDummyPaymentAccount(BisqAppConfig bisqAppConfig) {
var req = GetPaymentAccountsRequest.newBuilder().build();
var paymentAccountsService = grpcStubs(bisqAppConfig).paymentAccountsService;
PaymentAccount paymentAccount = paymentAccountsService.getPaymentAccounts(req)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;

import static bisq.apitest.Scaffold.BitcoinCoreApp.bitcoind;
import static bisq.apitest.config.BisqAppConfig.alicedaemon;
Expand All @@ -62,28 +61,13 @@ public abstract class AbstractOfferTest extends MethodTest {

@BeforeAll
public static void setUp() {
startSupportingApps();
}

@BeforeEach
public void initDummyPaymentAccount() {
super.initAlicesDummyPaymentAccount();
}

static void startSupportingApps() {
try {
// setUpScaffold(new String[]{"--supportingApps", "bitcoind,seednode,arbdaemon,alicedaemon,bobdaemon", "--enableBisqDebugging", "true"});
setUpScaffold(bitcoind, seednode, arbdaemon, alicedaemon, bobdaemon);
registerDisputeAgents(arbdaemon);
aliceStubs = grpcStubs(alicedaemon);
bobStubs = grpcStubs(bobdaemon);

// Generate 1 regtest block for alice's wallet to show 10 BTC balance,
// and give alicedaemon time to parse the new block.
genBtcBlocksThenWait(1, 1500);
} catch (Exception ex) {
fail(ex);
}
startSupportingApps(true,
true,
bitcoind,
seednode,
arbdaemon,
alicedaemon,
bobdaemon);
}

protected final OfferInfo createAliceOffer(PaymentAccount paymentAccount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package bisq.apitest.method.trade;

import bisq.core.trade.Trade;

import bisq.proto.grpc.TradeInfo;

import org.slf4j.Logger;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;

import static bisq.cli.TradeFormat.format;
Expand All @@ -20,24 +17,16 @@

public class AbstractTradeTest extends AbstractOfferTest {

// A test fixture encapsulating expected trade protocol status.
// ExpectedProtocolStatus.init should be called before any @Test begins.
protected static final ExpectedProtocolStatus EXPECTED_PROTOCOL_STATUS = new ExpectedProtocolStatus();
public static final ExpectedProtocolStatus EXPECTED_PROTOCOL_STATUS = new ExpectedProtocolStatus();

// A Trade ID cache for use in @Test sequences.
protected static String tradeId;

@BeforeAll
public static void clearExpectedPaymentStatusFlags() {
public static void initStaticFixtures() {
EXPECTED_PROTOCOL_STATUS.init();
}

@BeforeEach
public void initDummyPaymentAccounts() {
super.initAlicesDummyPaymentAccount();
super.initBobsDummyPaymentAccount();
}

protected final TradeInfo takeAlicesOffer(String offerId, String paymentAccountId) {
return bobStubs.tradesService.takeOffer(createTakeOfferRequest(offerId, paymentAccountId)).getTrade();
}
Expand Down Expand Up @@ -68,68 +57,4 @@ protected final void logTrade(Logger log,
description.toUpperCase(),
format(trade)));
}

@SuppressWarnings("UnusedReturnValue")
static class ExpectedProtocolStatus {
Trade.State state;
Trade.Phase phase;
boolean isDepositPublished;
boolean isDepositConfirmed;
boolean isFiatSent;
boolean isFiatReceived;
boolean isPayoutPublished;
boolean isWithdrawn;

ExpectedProtocolStatus setState(Trade.State state) {
this.state = state;
return this;
}

ExpectedProtocolStatus setPhase(Trade.Phase phase) {
this.phase = phase;
return this;
}

ExpectedProtocolStatus setDepositPublished(boolean depositPublished) {
isDepositPublished = depositPublished;
return this;
}

ExpectedProtocolStatus setDepositConfirmed(boolean depositConfirmed) {
isDepositConfirmed = depositConfirmed;
return this;
}

ExpectedProtocolStatus setFiatSent(boolean fiatSent) {
isFiatSent = fiatSent;
return this;
}

ExpectedProtocolStatus setFiatReceived(boolean fiatReceived) {
isFiatReceived = fiatReceived;
return this;
}

ExpectedProtocolStatus setPayoutPublished(boolean payoutPublished) {
isPayoutPublished = payoutPublished;
return this;
}

ExpectedProtocolStatus setWithdrawn(boolean withdrawn) {
isWithdrawn = withdrawn;
return this;
}

@SuppressWarnings("unused")
void init() {
state = null;
phase = null;
isDepositPublished = false;
isDepositConfirmed = false;
isFiatSent = false;
isFiatReceived = false;
isPayoutPublished = false;
isWithdrawn = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package bisq.apitest.method.trade;

import bisq.core.trade.Trade;

/**
* A test fixture encapsulating expected trade protocol status.
* Status flags should be cleared via init() before starting a new trade protocol.
*/
public class ExpectedProtocolStatus {
Trade.State state;
Trade.Phase phase;
boolean isDepositPublished;
boolean isDepositConfirmed;
boolean isFiatSent;
boolean isFiatReceived;
boolean isPayoutPublished;
boolean isWithdrawn;

public ExpectedProtocolStatus setState(Trade.State state) {
this.state = state;
return this;
}

public ExpectedProtocolStatus setPhase(Trade.Phase phase) {
this.phase = phase;
return this;
}

public ExpectedProtocolStatus setDepositPublished(boolean depositPublished) {
isDepositPublished = depositPublished;
return this;
}

public ExpectedProtocolStatus setDepositConfirmed(boolean depositConfirmed) {
isDepositConfirmed = depositConfirmed;
return this;
}

public ExpectedProtocolStatus setFiatSent(boolean fiatSent) {
isFiatSent = fiatSent;
return this;
}

public ExpectedProtocolStatus setFiatReceived(boolean fiatReceived) {
isFiatReceived = fiatReceived;
return this;
}

public ExpectedProtocolStatus setPayoutPublished(boolean payoutPublished) {
isPayoutPublished = payoutPublished;
return this;
}

public ExpectedProtocolStatus setWithdrawn(boolean withdrawn) {
isWithdrawn = withdrawn;
return this;
}

public void init() {
state = null;
phase = null;
isDepositPublished = false;
isDepositConfirmed = false;
isFiatSent = false;
isFiatReceived = false;
isPayoutPublished = false;
isWithdrawn = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;



import bisq.apitest.method.MethodTest;

@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FundWalletScenarioTest extends ScenarioTest {
public class FundWalletScenarioTest extends MethodTest {

@BeforeAll
public static void setUp() {
Expand Down
28 changes: 0 additions & 28 deletions apitest/src/test/java/bisq/apitest/scenario/ScenarioTest.java

This file was deleted.

0 comments on commit ec54b4a

Please sign in to comment.