Refactor test cases to use arbitrary grpc stubs

Most test cases send requests to the alicedaemon, but new test cases
will need to be able to send requests to arbitration and bob daemons.
This commit is contained in:
ghubstan 2020-09-14 12:09:04 -03:00
parent 899bea8df5
commit 148a0f1200
No known key found for this signature in database
GPG key ID: E35592D6800A861E
6 changed files with 70 additions and 48 deletions

View file

@ -21,14 +21,16 @@ import java.net.InetAddress;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import static bisq.apitest.config.BisqAppConfig.alicedaemon;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import bisq.apitest.config.ApiTestConfig;
import bisq.apitest.config.BisqAppConfig;
import bisq.apitest.method.BitcoinCliHelper;
import bisq.cli.GrpcStubs;
@ -59,34 +61,41 @@ import bisq.cli.GrpcStubs;
*/
public class ApiTestCase {
// The gRPC service stubs are used by method & scenario tests, but not e2e tests.
protected static GrpcStubs grpcStubs;
protected static Scaffold scaffold;
protected static ApiTestConfig config;
protected static BitcoinCliHelper bitcoinCli;
// gRPC service stubs are used by method & scenario tests, but not e2e tests.
private static final Map<BisqAppConfig, GrpcStubs> grpcStubsCache = new HashMap<>();
public static void setUpScaffold(String supportingApps)
throws InterruptedException, ExecutionException, IOException {
scaffold = new Scaffold(supportingApps).setUp();
config = scaffold.config;
bitcoinCli = new BitcoinCliHelper((config));
grpcStubs = new GrpcStubs(InetAddress.getLoopbackAddress().getHostAddress(),
alicedaemon.apiPort, config.apiPassword);
}
public static void setUpScaffold(String[] params)
throws InterruptedException, ExecutionException, IOException {
scaffold = new Scaffold(params).setUp();
config = scaffold.config;
grpcStubs = new GrpcStubs(InetAddress.getLoopbackAddress().getHostAddress(),
alicedaemon.apiPort, config.apiPassword);
}
public static void tearDownScaffold() {
scaffold.tearDown();
}
protected static GrpcStubs grpcStubs(BisqAppConfig bisqAppConfig) {
if (grpcStubsCache.containsKey(bisqAppConfig)) {
return grpcStubsCache.get(bisqAppConfig);
} else {
GrpcStubs stubs = new GrpcStubs(InetAddress.getLoopbackAddress().getHostAddress(),
bisqAppConfig.apiPort, config.apiPassword);
grpcStubsCache.put(bisqAppConfig, stubs);
return stubs;
}
}
protected void sleep(long ms) {
try {
MILLISECONDS.sleep(ms);

View file

@ -27,6 +27,7 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static bisq.apitest.config.BisqAppConfig.alicedaemon;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
@ -57,7 +58,8 @@ public class GetBalanceTest extends MethodTest {
public void testGetBalance() {
// All tests depend on the DAO / regtest environment, and Alice's wallet is
// initialized with 10 BTC during the scaffolding setup.
var balance = grpcStubs.walletsService.getBalance(GetBalanceRequest.newBuilder().build()).getBalance();
var balance = grpcStubs(alicedaemon).walletsService
.getBalance(GetBalanceRequest.newBuilder().build()).getBalance();
assertEquals(1000000000, balance);
}

View file

@ -50,7 +50,8 @@ public class GetVersionTest extends MethodTest {
@Test
@Order(1)
public void testGetVersion() {
var version = grpcStubs.versionService.getVersion(GetVersionRequest.newBuilder().build()).getVersion();
var version = grpcStubs(alicedaemon).versionService
.getVersion(GetVersionRequest.newBuilder().build()).getVersion();
assertEquals(VERSION, version);
}

View file

@ -20,13 +20,17 @@ package bisq.apitest.method;
import bisq.proto.grpc.GetBalanceRequest;
import bisq.proto.grpc.GetFundingAddressesRequest;
import bisq.proto.grpc.LockWalletRequest;
import bisq.proto.grpc.RegisterDisputeAgentRequest;
import bisq.proto.grpc.RemoveWalletPasswordRequest;
import bisq.proto.grpc.SetWalletPasswordRequest;
import bisq.proto.grpc.UnlockWalletRequest;
import static bisq.common.app.DevEnv.DEV_PRIVILEGE_PRIV_KEY;
import bisq.apitest.ApiTestCase;
import bisq.apitest.config.BisqAppConfig;
public class MethodTest extends ApiTestCase {
@ -60,24 +64,31 @@ public class MethodTest extends ApiTestCase {
return GetFundingAddressesRequest.newBuilder().build();
}
protected final RegisterDisputeAgentRequest createRegisterDisputeAgentRequest(String disputeAgentType) {
return RegisterDisputeAgentRequest.newBuilder()
.setDisputeAgentType(disputeAgentType)
.setRegistrationKey(DEV_PRIVILEGE_PRIV_KEY).build();
}
// Convenience methods for calling frequently used & thoroughly tested gRPC services.
protected final long getBalance() {
return grpcStubs.walletsService.getBalance(createBalanceRequest()).getBalance();
protected final long getBalance(BisqAppConfig bisqAppConfig) {
return grpcStubs(bisqAppConfig).walletsService.getBalance(createBalanceRequest()).getBalance();
}
protected final void unlockWallet(String password, long timeout) {
protected final void unlockWallet(BisqAppConfig bisqAppConfig, String password, long timeout) {
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.unlockWallet(createUnlockWalletRequest(password, timeout));
grpcStubs(bisqAppConfig).walletsService.unlockWallet(createUnlockWalletRequest(password, timeout));
}
protected final void lockWallet() {
protected final void lockWallet(BisqAppConfig bisqAppConfig) {
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.lockWallet(createLockWalletRequest());
grpcStubs(bisqAppConfig).walletsService.lockWallet(createLockWalletRequest());
}
protected final String getUnusedBtcAddress() {
return grpcStubs.walletsService.getFundingAddresses(createGetFundingAddressesRequest())
protected final String getUnusedBtcAddress(BisqAppConfig bisqAppConfig) {
//noinspection OptionalGetWithoutIsPresent
return grpcStubs(bisqAppConfig).walletsService.getFundingAddresses(createGetFundingAddressesRequest())
.getAddressBalanceInfoList()
.stream()
.filter(a -> a.getBalance() == 0 && a.getNumConfirmations() == 0)

View file

@ -36,13 +36,13 @@ public class WalletProtectionTest extends MethodTest {
@Order(1)
public void testSetWalletPassword() {
var request = createSetWalletPasswordRequest("first-password");
grpcStubs.walletsService.setWalletPassword(request);
grpcStubs(alicedaemon).walletsService.setWalletPassword(request);
}
@Test
@Order(2)
public void testGetBalanceOnEncryptedWalletShouldThrowException() {
Throwable exception = assertThrows(StatusRuntimeException.class, this::getBalance);
Throwable exception = assertThrows(StatusRuntimeException.class, () -> getBalance(alicedaemon));
assertEquals("UNKNOWN: wallet is locked", exception.getMessage());
}
@ -50,11 +50,10 @@ public class WalletProtectionTest extends MethodTest {
@Order(3)
public void testUnlockWalletFor4Seconds() {
var request = createUnlockWalletRequest("first-password", 4);
grpcStubs.walletsService.unlockWallet(request);
getBalance(); // should not throw 'wallet locked' exception
grpcStubs(alicedaemon).walletsService.unlockWallet(request);
getBalance(alicedaemon); // should not throw 'wallet locked' exception
sleep(4500); // let unlock timeout expire
Throwable exception = assertThrows(StatusRuntimeException.class, this::getBalance);
Throwable exception = assertThrows(StatusRuntimeException.class, () -> getBalance(alicedaemon));
assertEquals("UNKNOWN: wallet is locked", exception.getMessage());
}
@ -62,20 +61,19 @@ public class WalletProtectionTest extends MethodTest {
@Order(4)
public void testGetBalanceAfterUnlockTimeExpiryShouldThrowException() {
var request = createUnlockWalletRequest("first-password", 3);
grpcStubs.walletsService.unlockWallet(request);
grpcStubs(alicedaemon).walletsService.unlockWallet(request);
sleep(4000); // let unlock timeout expire
Throwable exception = assertThrows(StatusRuntimeException.class, this::getBalance);
Throwable exception = assertThrows(StatusRuntimeException.class, () -> getBalance(alicedaemon));
assertEquals("UNKNOWN: wallet is locked", exception.getMessage());
}
@Test
@Order(5)
public void testLockWalletBeforeUnlockTimeoutExpiry() {
unlockWallet("first-password", 60);
unlockWallet(alicedaemon, "first-password", 60);
var request = createLockWalletRequest();
grpcStubs.walletsService.lockWallet(request);
Throwable exception = assertThrows(StatusRuntimeException.class, this::getBalance);
grpcStubs(alicedaemon).walletsService.lockWallet(request);
Throwable exception = assertThrows(StatusRuntimeException.class, () -> getBalance(alicedaemon));
assertEquals("UNKNOWN: wallet is locked", exception.getMessage());
}
@ -83,40 +81,39 @@ public class WalletProtectionTest extends MethodTest {
@Order(6)
public void testLockWalletWhenWalletAlreadyLockedShouldThrowException() {
var request = createLockWalletRequest();
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
grpcStubs.walletsService.lockWallet(request));
grpcStubs(alicedaemon).walletsService.lockWallet(request));
assertEquals("UNKNOWN: wallet is already locked", exception.getMessage());
}
@Test
@Order(7)
public void testUnlockWalletTimeoutOverride() {
unlockWallet("first-password", 2);
unlockWallet(alicedaemon, "first-password", 2);
sleep(500); // override unlock timeout after 0.5s
unlockWallet("first-password", 6);
unlockWallet(alicedaemon, "first-password", 6);
sleep(5000);
getBalance(); // getbalance 5s after resetting unlock timeout to 6s
getBalance(alicedaemon); // getbalance 5s after resetting unlock timeout to 6s
}
@Test
@Order(8)
public void testSetNewWalletPassword() {
var request = createSetWalletPasswordRequest("first-password", "second-password");
grpcStubs.walletsService.setWalletPassword(request);
unlockWallet("second-password", 2);
getBalance();
var request = createSetWalletPasswordRequest(
"first-password", "second-password");
grpcStubs(alicedaemon).walletsService.setWalletPassword(request);
unlockWallet(alicedaemon, "second-password", 2);
getBalance(alicedaemon);
sleep(2500); // allow time for wallet save
}
@Test
@Order(9)
public void testSetNewWalletPasswordWithIncorrectNewPasswordShouldThrowException() {
var request = createSetWalletPasswordRequest("bad old password", "irrelevant");
var request = createSetWalletPasswordRequest(
"bad old password", "irrelevant");
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
grpcStubs.walletsService.setWalletPassword(request));
grpcStubs(alicedaemon).walletsService.setWalletPassword(request));
assertEquals("UNKNOWN: incorrect old password", exception.getMessage());
}
@ -124,8 +121,8 @@ public class WalletProtectionTest extends MethodTest {
@Order(10)
public void testRemoveNewWalletPassword() {
var request = createRemoveWalletPasswordRequest("second-password");
grpcStubs.walletsService.removeWalletPassword(request);
getBalance(); // should not throw 'wallet locked' exception
grpcStubs(alicedaemon).walletsService.removeWalletPassword(request);
getBalance(alicedaemon); // should not throw 'wallet locked' exception
}
@AfterAll

View file

@ -26,6 +26,7 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static bisq.apitest.config.BisqAppConfig.alicedaemon;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
@ -48,16 +49,17 @@ public class FundWalletScenarioTest extends ScenarioTest {
@Test
@Order(1)
public void testFundWallet() {
long balance = getBalance(); // bisq wallet was initialized with 10 btc
// bisq wallet was initialized with 10 btc
long balance = getBalance(alicedaemon);
assertEquals(1000000000, balance);
String unusedAddress = getUnusedBtcAddress();
String unusedAddress = getUnusedBtcAddress(alicedaemon);
bitcoinCli.sendToAddress(unusedAddress, "2.5");
bitcoinCli.generateBlocks(1);
sleep(1500);
balance = getBalance();
balance = getBalance(alicedaemon);
assertEquals(1250000000L, balance); // new balance is 12.5 btc
}