Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test api method 'canceloffer' #4716

Merged
merged 25 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cb65de6
Block on tx-fee-request in core TakeOfferModel init
ghubstan Oct 23, 2020
ab20225
Add compiler warning suppression, remove comment
ghubstan Oct 24, 2020
63cf436
Add fields to grpc TradeInfo proto & wrapper
ghubstan Oct 24, 2020
296e4f9
Replace static TradeUtil with singleton TradeUtil
ghubstan Oct 24, 2020
ccd3c99
Fix comment typos
ghubstan Oct 24, 2020
24ba921
Refactor PendingTradesViewModel methods -> TradeUtil & OfferUtil
ghubstan Oct 25, 2020
36ad137
Remove trailing spaces for codacy
ghubstan Oct 25, 2020
95bcb1e
Refactor PendingTradesDataModel methods -> TradeUtil
ghubstan Oct 25, 2020
bbd7a31
Remove unused import
ghubstan Oct 25, 2020
161dbad
Add getRole(tradeId) to core api
ghubstan Oct 25, 2020
3379376
Refactor CLI output formatting code & add trade formatter
ghubstan Oct 25, 2020
d8bc265
Add license comment
ghubstan Oct 25, 2020
31435bb
Move semicolon up from blank line
ghubstan Oct 25, 2020
2b23704
Add 'gettrade' to api method CLI
ghubstan Oct 25, 2020
a2b2923
Add boolean 'showcontract' argument to api's 'gettrade'
ghubstan Oct 25, 2020
a8decaf
Stub out api methods 'keepfunds', 'withdrawfunds'
ghubstan Oct 26, 2020
a3631a0
Implement api methods 'keepfunds', 'withdrawfunds'
ghubstan Oct 26, 2020
f1db254
Make formatSatoshis visible for testing
ghubstan Oct 27, 2020
b8ae566
Add method for printing current jupiter test name
ghubstan Oct 27, 2020
1e25be5
Test trade closing api methods 'keepfunds' withdrawfunds'
ghubstan Oct 27, 2020
2746b27
Fix apitest dummy payment acct init bug
ghubstan Oct 27, 2020
027a7d5
Stub out canceloffer api method
ghubstan Oct 27, 2020
0f1d4f8
Fix typo
ghubstan Oct 27, 2020
b38507c
Implement api method 'canceloffer'
ghubstan Oct 27, 2020
91a2e2c
Add canceloffer test
ghubstan Oct 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apitest/src/test/java/bisq/apitest/ApiTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import org.junit.jupiter.api.TestInfo;

import static java.util.Arrays.stream;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

Expand Down Expand Up @@ -117,4 +119,10 @@ protected static void sleep(long ms) {
// empty
}
}

protected final String testName(TestInfo testInfo) {
return testInfo.getTestMethod().isPresent()
? testInfo.getTestMethod().get().getName()
: "unknown test name";
}
}
56 changes: 56 additions & 0 deletions apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package bisq.apitest.method;

import bisq.proto.grpc.CancelOfferRequest;
import bisq.proto.grpc.ConfirmPaymentReceivedRequest;
import bisq.proto.grpc.ConfirmPaymentStartedRequest;
import bisq.proto.grpc.CreatePaymentAccountRequest;
Expand All @@ -25,6 +26,7 @@
import bisq.proto.grpc.GetOfferRequest;
import bisq.proto.grpc.GetPaymentAccountsRequest;
import bisq.proto.grpc.GetTradeRequest;
import bisq.proto.grpc.KeepFundsRequest;
import bisq.proto.grpc.LockWalletRequest;
import bisq.proto.grpc.MarketPriceRequest;
import bisq.proto.grpc.OfferInfo;
Expand All @@ -34,11 +36,14 @@
import bisq.proto.grpc.TakeOfferRequest;
import bisq.proto.grpc.TradeInfo;
import bisq.proto.grpc.UnlockWalletRequest;
import bisq.proto.grpc.WithdrawFundsRequest;

import protobuf.PaymentAccount;

import java.util.stream.Collectors;

import static bisq.apitest.config.BisqAppConfig.alicedaemon;
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.Comparator.comparing;
Expand All @@ -48,13 +53,28 @@

import bisq.apitest.ApiTestCase;
import bisq.apitest.config.BisqAppConfig;
import bisq.cli.GrpcStubs;

public class MethodTest extends ApiTestCase {

protected static final String ARBITRATOR = "arbitrator";
protected static final String MEDIATOR = "mediator";
protected static final String REFUND_AGENT = "refundagent";

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);
}

// Convenience methods for building gRPC request objects

protected final GetBalanceRequest createBalanceRequest() {
Expand Down Expand Up @@ -93,6 +113,10 @@ protected final GetOfferRequest createGetOfferRequest(String offerId) {
return GetOfferRequest.newBuilder().setId(offerId).build();
}

protected final CancelOfferRequest createCancelOfferRequest(String offerId) {
return CancelOfferRequest.newBuilder().setId(offerId).build();
}

protected final TakeOfferRequest createTakeOfferRequest(String offerId, String paymentAccountId) {
return TakeOfferRequest.newBuilder().setOfferId(offerId).setPaymentAccountId(paymentAccountId).build();
}
Expand All @@ -109,6 +133,19 @@ protected final ConfirmPaymentReceivedRequest createConfirmPaymentReceivedReques
return ConfirmPaymentReceivedRequest.newBuilder().setTradeId(tradeId).build();
}

protected final KeepFundsRequest createKeepFundsRequest(String tradeId) {
return KeepFundsRequest.newBuilder()
.setTradeId(tradeId)
.build();
}

protected final WithdrawFundsRequest createWithdrawFundsRequest(String tradeId, String address) {
return WithdrawFundsRequest.newBuilder()
.setTradeId(tradeId)
.setAddress(address)
.build();
}

// Convenience methods for calling frequently used & thoroughly tested gRPC services.

protected final long getBalance(BisqAppConfig bisqAppConfig) {
Expand Down Expand Up @@ -170,21 +207,40 @@ protected final OfferInfo getOffer(BisqAppConfig bisqAppConfig, String offerId)
return grpcStubs(bisqAppConfig).offersService.getOffer(req).getOffer();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
protected final void cancelOffer(BisqAppConfig bisqAppConfig, String offerId) {
var req = createCancelOfferRequest(offerId);
grpcStubs(bisqAppConfig).offersService.cancelOffer(req);
}

protected final TradeInfo getTrade(BisqAppConfig bisqAppConfig, String tradeId) {
var req = createGetTradeRequest(tradeId);
return grpcStubs(bisqAppConfig).tradesService.getTrade(req).getTrade();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
protected final void confirmPaymentStarted(BisqAppConfig bisqAppConfig, String tradeId) {
var req = createConfirmPaymentStartedRequest(tradeId);
grpcStubs(bisqAppConfig).tradesService.confirmPaymentStarted(req);
}

@SuppressWarnings("ResultOfMethodCallIgnored")
protected final void confirmPaymentReceived(BisqAppConfig bisqAppConfig, String tradeId) {
var req = createConfirmPaymentReceivedRequest(tradeId);
grpcStubs(bisqAppConfig).tradesService.confirmPaymentReceived(req);
}

@SuppressWarnings("ResultOfMethodCallIgnored")
protected final void keepFunds(BisqAppConfig bisqAppConfig, String tradeId) {
var req = createKeepFundsRequest(tradeId);
grpcStubs(bisqAppConfig).tradesService.keepFunds(req);
}

@SuppressWarnings("ResultOfMethodCallIgnored")
protected final void withdrawFunds(BisqAppConfig bisqAppConfig, String tradeId, String address) {
var req = createWithdrawFundsRequest(tradeId, address);
grpcStubs(bisqAppConfig).tradesService.withdrawFunds(req);
}
// Static conveniences for test methods and test case fixture setups.

protected static RegisterDisputeAgentRequest createRegisterDisputeAgentRequest(String disputeAgentType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public static void setUp() {
@Test
@Order(1)
public void testRegisterArbitratorShouldThrowException() {
var req =
createRegisterDisputeAgentRequest(ARBITRATOR);
var req = createRegisterDisputeAgentRequest(ARBITRATOR);
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req));
assertEquals("INVALID_ARGUMENT: arbitrators must be registered in a Bisq UI",
Expand All @@ -67,8 +66,7 @@ public void testRegisterArbitratorShouldThrowException() {
@Test
@Order(2)
public void testInvalidDisputeAgentTypeArgShouldThrowException() {
var req =
createRegisterDisputeAgentRequest("badagent");
var req = createRegisterDisputeAgentRequest("badagent");
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req));
assertEquals("INVALID_ARGUMENT: unknown dispute agent type 'badagent'",
Expand All @@ -90,16 +88,14 @@ public void testInvalidRegistrationKeyArgShouldThrowException() {
@Test
@Order(4)
public void testRegisterMediator() {
var req =
createRegisterDisputeAgentRequest(MEDIATOR);
var req = createRegisterDisputeAgentRequest(MEDIATOR);
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req);
}

@Test
@Order(5)
public void testRegisterRefundAgent() {
var req =
createRegisterDisputeAgentRequest(REFUND_AGENT);
var req = createRegisterDisputeAgentRequest(REFUND_AGENT);
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

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 @@ -59,14 +60,16 @@
@Slf4j
public abstract class AbstractOfferTest extends MethodTest {

protected static GrpcStubs aliceStubs;
protected static GrpcStubs bobStubs;

@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"});
Expand Down Expand Up @@ -120,6 +123,11 @@ protected final OfferInfo getOffer(String offerId) {
return aliceStubs.offersService.getOffer(createGetOfferRequest(offerId)).getOffer();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
protected final void cancelOffer(GrpcStubs grpcStubs, String offerId) {
grpcStubs.offersService.cancelOffer(createCancelOfferRequest(offerId));
}

protected final OfferInfo getMostRecentOffer(GrpcStubs grpcStubs, String direction, String currencyCode) {
List<OfferInfo> offerInfoList = getOffersSortedByDate(grpcStubs, direction, currencyCode);
if (offerInfoList.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.method.offer;

import bisq.core.btc.wallet.Restrictions;

import bisq.proto.grpc.CreateOfferRequest;
import bisq.proto.grpc.OfferInfo;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import static org.junit.jupiter.api.Assertions.assertEquals;


@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class CancelOfferTest extends AbstractOfferTest {

private static final int MAX_OFFERS = 3;

@Test
@Order(1)
public void testCancelOffer() {
var req = CreateOfferRequest.newBuilder()
.setPaymentAccountId(alicesDummyAcct.getId())
.setDirection("buy")
.setCurrencyCode("cad")
.setAmount(10000000)
.setMinAmount(10000000)
.setUseMarketBasedPrice(true)
.setMarketPriceMargin(0.00)
.setPrice("0")
.setBuyerSecurityDeposit(Restrictions.getDefaultBuyerSecurityDepositAsPercent())
.build();

// Create some offers.
for (int i = 1; i <= MAX_OFFERS; i++) {
//noinspection ResultOfMethodCallIgnored
aliceStubs.offersService.createOffer(req);
// Wait for Alice's AddToOfferBook task.
// Wait times vary; my logs show >= 2 second delay.
sleep(2500);
}

List<OfferInfo> offers = getOffersSortedByDate(aliceStubs, "buy", "cad");
assertEquals(MAX_OFFERS, offers.size());

// Cancel the offers, checking the open offer count after each offer removal.
for (int i = 1; i <= MAX_OFFERS; i++) {
cancelOffer(aliceStubs, offers.remove(0).getId());
assertEquals(MAX_OFFERS - i, getOpenOffersCount(aliceStubs, "buy", "cad"));
}

sleep(1000); // wait for offer removal

offers = getOffersSortedByDate(aliceStubs, "buy", "cad");
assertEquals(0, offers.size());
}
}
Loading