Search in sources :

Example 36 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class CommandsTest method testCommandsJSON.

/**
 * Call all commands and check for excepion
 * @throws CommandException
 */
@Test
public void testCommandsJSON() throws CommandException {
    final BitfinexOrder order = BitfinexOrderBuilder.create(BitfinexCurrencyPair.BCH_USD, BitfinexOrderType.EXCHANGE_STOP, 2).build();
    final BitfinexCandlestickSymbol candleSymbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BCH_USD, Timeframe.HOUR_1);
    final OrderbookConfiguration orderbookConfiguration = new OrderbookConfiguration(BitfinexCurrencyPair.BCH_USD, OrderBookPrecision.P0, OrderBookFrequency.F0, 50);
    final RawOrderbookConfiguration rawOrderbookConfiguration = new RawOrderbookConfiguration(BitfinexCurrencyPair.BAT_BTC);
    final List<AbstractAPICommand> commands = Arrays.asList(new AuthCommand(), new CancelOrderCommand(123), new CancelOrderGroupCommand(1), new OrderCommand(order), new PingCommand(), new SubscribeCandlesCommand(candleSymbol), new SubscribeTickerCommand(new BitfinexTickerSymbol(BitfinexCurrencyPair.BCH_USD)), new SubscribeTradesCommand(new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BAT_BTC)), new SubscribeOrderbookCommand(orderbookConfiguration), new SubscribeRawOrderbookCommand(rawOrderbookConfiguration), new UnsubscribeChannelCommand(12));
    final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
    for (final AbstractAPICommand command : commands) {
        final String commandValue = command.getCommand(bitfinexApiBroker);
        Assert.assertNotNull(commandValue);
        Assert.assertTrue(commandValue.length() > 10);
    }
}
Also used : BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) CancelOrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand) OrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.OrderCommand) SubscribeOrderbookCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand) CancelOrderGroupCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderGroupCommand) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) UnsubscribeChannelCommand(com.github.jnidzwetzki.bitfinex.v2.commands.UnsubscribeChannelCommand) SubscribeTradesCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTradesCommand) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) AbstractAPICommand(com.github.jnidzwetzki.bitfinex.v2.commands.AbstractAPICommand) CancelOrderCommand(com.github.jnidzwetzki.bitfinex.v2.commands.CancelOrderCommand) PingCommand(com.github.jnidzwetzki.bitfinex.v2.commands.PingCommand) SubscribeCandlesCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) SubscribeTickerCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) BitfinexOrder(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexOrder) AuthCommand(com.github.jnidzwetzki.bitfinex.v2.commands.AuthCommand) SubscribeRawOrderbookCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand) Test(org.junit.Test)

Example 37 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class HeartbeatManagerTest method testConnectWhenDisconnected.

@Test(timeout = 30000)
public void testConnectWhenDisconnected() throws Exception {
    // Events
    // * Disconnect websocket from heartbeat
    // * Disconnect from bitfinex API reconnect method
    // * Reconnect on bitfinex api
    final CountDownLatch connectLatch = new CountDownLatch(3);
    // Count down the latch on method call
    final Answer<Void> answer = new Answer<Void>() {

        public Void answer(final InvocationOnMock invocation) throws Throwable {
            connectLatch.countDown();
            return null;
        }
    };
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    final HeartbeatThread heartbeatThreadRunnable = new HeartbeatThread(bitfinexApiBroker);
    final WebsocketClientEndpoint websocketClientEndpoint = Mockito.mock(WebsocketClientEndpoint.class);
    Mockito.when(websocketClientEndpoint.isConnected()).thenReturn(connectLatch.getCount() == 0);
    Mockito.when(bitfinexApiBroker.getWebsocketEndpoint()).thenReturn(websocketClientEndpoint);
    Mockito.doAnswer(answer).when(bitfinexApiBroker).reconnect();
    Mockito.doAnswer(answer).when(websocketClientEndpoint).close();
    final Thread heartbeatThread = new Thread(heartbeatThreadRunnable);
    try {
        heartbeatThread.start();
        connectLatch.await();
    } catch (Exception e) {
        // Should not happen
        throw e;
    } finally {
        heartbeatThread.interrupt();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HeartbeatThread(com.github.jnidzwetzki.bitfinex.v2.HeartbeatThread) WebsocketClientEndpoint(com.github.jnidzwetzki.bitfinex.v2.WebsocketClientEndpoint) CountDownLatch(java.util.concurrent.CountDownLatch) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) HeartbeatThread(com.github.jnidzwetzki.bitfinex.v2.HeartbeatThread) Test(org.junit.Test)

Example 38 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class HeartbeatManagerTest method testHeartbeatHandler.

/**
 * Test the heartbeart handler
 * @throws APIException
 */
@Test
public void testHeartbeatHandler() throws APIException {
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    final HeartbeatHandler handler = new HeartbeatHandler();
    handler.handleChannelData(bitfinexApiBroker, null);
    Mockito.verify(bitfinexApiBroker, Mockito.times(1)).updateConnectionHeartbeat();
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) HeartbeatHandler(com.github.jnidzwetzki.bitfinex.v2.callback.api.HeartbeatHandler) Test(org.junit.Test)

Example 39 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class IntegrationTest method testWalletsOnUnauthClient.

/**
 * Try to fetch wallets on an unauthenticated connection
 */
@Test
public void testWalletsOnUnauthClient() throws APIException {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    try {
        bitfinexClient.connect();
        Assert.assertFalse(bitfinexClient.isAuthenticated());
        try {
            bitfinexClient.getWalletManager().getWallets();
            // Should not happen
            Assert.assertTrue(false);
        } catch (APIException e) {
            return;
        }
    } catch (Exception e) {
        // Should not happen
        e.printStackTrace();
        Assert.assertTrue(false);
    } finally {
        bitfinexClient.close();
    }
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Test(org.junit.Test)

Example 40 with BitfinexApiBroker

use of com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class IntegrationTest method testAuthFailed.

/**
 * Test auth failed
 * @throws APIException
 */
@Test(expected = APIException.class, timeout = 10000)
public void testAuthFailed() throws APIException {
    final String KEY = "key";
    final String SECRET = "secret";
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker(KEY, SECRET);
    Assert.assertEquals(KEY, bitfinexClient.getApiKey());
    Assert.assertEquals(SECRET, bitfinexClient.getApiSecret());
    Assert.assertFalse(bitfinexClient.isAuthenticated());
    bitfinexClient.connect();
    // Should not be reached
    Assert.assertTrue(false);
    bitfinexClient.close();
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) Test(org.junit.Test)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)46 Test (org.junit.Test)36 JSONArray (org.json.JSONArray)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)15 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)15 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)13 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)12 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)12 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)11 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)11 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)9 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)9 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)9 OrderManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderManager)9 ExecutorService (java.util.concurrent.ExecutorService)9 ExecutedTrade (com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade)7 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)7 Assert (org.junit.Assert)7 ExchangeOrder (com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)6