Add FundWalletScenarioTest

Some refactoring was done to reduce some of the boilerplate.
This commit is contained in:
ghubstan 2020-07-16 20:53:11 -03:00
parent 6edab1a2cd
commit 687bcf1d8f
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
4 changed files with 143 additions and 3 deletions

View File

@ -20,6 +20,7 @@ package bisq.apitest.method;
import java.io.IOException;
import static java.lang.String.format;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
@ -39,7 +40,9 @@ public final class BitcoinCliHelper {
public String getNewBtcAddress() {
try {
return new BitcoinCli(config, "getnewaddress").run().getOutput();
String newAddress = new BitcoinCli(config, "getnewaddress").run().getOutput();
assertNotNull(newAddress);
return newAddress;
} catch (IOException | InterruptedException ex) {
fail(ex.getMessage());
return null;
@ -50,8 +53,9 @@ public final class BitcoinCliHelper {
try {
String generateToAddressCmd = format("generatetoaddress %d \"%s\"", blocks, address);
BitcoinCli generateToAddress = new BitcoinCli(config, generateToAddressCmd).run();
// Return an array of transaction ids.
return generateToAddress.getOutputValueAsStringArray();
String[] txids = generateToAddress.getOutputValueAsStringArray();
assertNotNull(txids);
return txids;
} catch (IOException | InterruptedException ex) {
fail(ex.getMessage());
return null;
@ -61,4 +65,22 @@ public final class BitcoinCliHelper {
public void generateBlocks(int blocks) {
generateToAddress(blocks, getNewBtcAddress());
}
public String sendToAddress(String address, double amount) {
// sendtoaddress "address" amount \
// ( "comment" "comment_to" subtractfeefromamount \
// replaceable conf_target "estimate_mode" )
// returns a transaction id
try {
String sendToAddressCmd = format("sendtoaddress \"%s\" %s \"\" \"\" true",
address, amount);
BitcoinCli sendToAddress = new BitcoinCli(config, sendToAddressCmd).run();
String txid = sendToAddress.getOutput();
assertNotNull(txid);
return txid;
} catch (IOException | InterruptedException ex) {
fail(ex.getMessage());
return null;
}
}
}

View File

@ -18,6 +18,7 @@
package bisq.apitest.method;
import bisq.proto.grpc.GetBalanceRequest;
import bisq.proto.grpc.GetFundingAddressesRequest;
import bisq.proto.grpc.LockWalletRequest;
import bisq.proto.grpc.RemoveWalletPasswordRequest;
import bisq.proto.grpc.SetWalletPasswordRequest;
@ -55,6 +56,10 @@ public class MethodTest extends ApiTestCase {
return LockWalletRequest.newBuilder().build();
}
protected final GetFundingAddressesRequest createGetFundingAddressesRequest() {
return GetFundingAddressesRequest.newBuilder().build();
}
// Convenience methods for calling frequently used & thoroughly tested gRPC services.
protected final long getBalance() {
@ -62,10 +67,23 @@ public class MethodTest extends ApiTestCase {
}
protected final void unlockWallet(String password, long timeout) {
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.unlockWallet(createUnlockWalletRequest(password, timeout));
}
protected final void lockWallet() {
//noinspection ResultOfMethodCallIgnored
grpcStubs.walletsService.lockWallet(createLockWalletRequest());
}
protected final String getUnusedBtcAddress() {
return grpcStubs.walletsService.getFundingAddresses(createGetFundingAddressesRequest())
.getAddressBalanceInfoList()
.stream()
.filter(a -> a.getBalance() == 0 && a.getNumConfirmations() == 0)
.findFirst()
.get()
.getAddress();
}
}

View File

@ -0,0 +1,78 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.apitest.scenario;
import lombok.extern.slf4j.Slf4j;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import static java.lang.Double.parseDouble;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import bisq.apitest.OrderedRunner;
import bisq.apitest.annotation.Order;
@Slf4j
@RunWith(OrderedRunner.class)
public class FundWalletScenarioTest extends ScenarioTest {
@BeforeClass
public static void setUp() {
try {
setUpScaffold("bitcoind,seednode,alicedaemon");
bitcoinCli.generateBlocks(1);
MILLISECONDS.sleep(1500);
} catch (InterruptedException ex) {
fail(ex.getMessage());
}
}
@Test
@Order(1)
public void testFundWallet() {
long balance = getBalance(); // bisq wallet was initialized with 10 btc
assertEquals(1000000000, balance);
String unusedAddress = getUnusedBtcAddress();
// Given the default tx fee rate, we want to send 2.5 + the fee,
// so the new wallet balance will be exactly 12.5 btc.
// We should calculate the fee based on the fee rate and tx size
// instead of hard coding the fee amount.
double btc = parseDouble("2.5") + parseDouble("0.0000336");
bitcoinCli.sendToAddress(unusedAddress, btc);
bitcoinCli.generateBlocks(1);
sleep(1500);
balance = getBalance();
assertEquals(1250000000L, balance); // new balance is 12.5 btc
}
@AfterClass
public static void tearDown() {
tearDownScaffold();
}
}

View File

@ -1,6 +1,28 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.apitest.scenario;
import lombok.extern.slf4j.Slf4j;
import bisq.apitest.method.MethodTest;
@Slf4j
public class ScenarioTest extends MethodTest {
}