/xchange-mercadobitcoin/src/test/java/org/knowm/xchange/mercadobitcoin/MercadoBitcoinAdapterTest.java

http://github.com/timmolter/XChange · Java · 169 lines · 124 code · 28 blank · 17 comment · 2 complexity · 47dc982b079a65648b762c70a5af8bd1 MD5 · raw file

  1. package org.knowm.xchange.mercadobitcoin;
  2. import static org.assertj.core.api.Assertions.assertThat;
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.math.BigDecimal;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import org.junit.Test;
  13. import org.knowm.xchange.currency.Currency;
  14. import org.knowm.xchange.currency.CurrencyPair;
  15. import org.knowm.xchange.dto.Order.OrderType;
  16. import org.knowm.xchange.dto.account.AccountInfo;
  17. import org.knowm.xchange.dto.marketdata.OrderBook;
  18. import org.knowm.xchange.dto.marketdata.Ticker;
  19. import org.knowm.xchange.dto.marketdata.Trades;
  20. import org.knowm.xchange.dto.trade.LimitOrder;
  21. import org.knowm.xchange.mercadobitcoin.dto.MercadoBitcoinBaseTradeApiResult;
  22. import org.knowm.xchange.mercadobitcoin.dto.account.MercadoBitcoinAccountInfo;
  23. import org.knowm.xchange.mercadobitcoin.dto.marketdata.MercadoBitcoinOrderBook;
  24. import org.knowm.xchange.mercadobitcoin.dto.marketdata.MercadoBitcoinTicker;
  25. import org.knowm.xchange.mercadobitcoin.dto.marketdata.MercadoBitcoinTransaction;
  26. import org.knowm.xchange.mercadobitcoin.dto.trade.MercadoBitcoinUserOrders;
  27. /**
  28. * Tests the MercadoBitcoinAdapter class
  29. *
  30. * @author Felipe Micaroni Lalli
  31. */
  32. public class MercadoBitcoinAdapterTest {
  33. @Test
  34. public void testOrderBookAdapter() throws IOException {
  35. // Read in the JSON from the example resources
  36. InputStream is =
  37. MercadoBitcoinAdapterTest.class.getResourceAsStream(
  38. "/org/knowm/xchange/mercadobitcoin/dto/marketdata/example-full-depth-data.json");
  39. // Use Jackson to parse it
  40. ObjectMapper mapper = new ObjectMapper();
  41. MercadoBitcoinOrderBook mercadoBitcoinOrderBook =
  42. mapper.readValue(is, MercadoBitcoinOrderBook.class);
  43. OrderBook orderBook =
  44. MercadoBitcoinAdapters.adaptOrderBook(mercadoBitcoinOrderBook, CurrencyPair.BTC_BRL);
  45. assertThat(orderBook.getBids().size()).isEqualTo(127);
  46. // verify all fields filled
  47. assertThat(orderBook.getBids().get(0).getLimitPrice().toString()).isEqualTo("1004.16826");
  48. assertThat(orderBook.getBids().get(0).getType()).isEqualTo(OrderType.BID);
  49. assertThat(orderBook.getBids().get(0).getOriginalAmount()).isEqualTo(new BigDecimal("0.16614"));
  50. assertThat(orderBook.getBids().get(0).getCurrencyPair()).isEqualTo(CurrencyPair.BTC_BRL);
  51. }
  52. @Test
  53. public void testTradesAdapter() throws IOException {
  54. // Read in the JSON from the example resources
  55. InputStream is =
  56. MercadoBitcoinAdapterTest.class.getResourceAsStream(
  57. "/org/knowm/xchange/mercadobitcoin/dto/marketdata/example-trades-data.json");
  58. // Use Jackson to parse it
  59. ObjectMapper mapper = new ObjectMapper();
  60. MercadoBitcoinTransaction[] transactions =
  61. mapper.readValue(is, MercadoBitcoinTransaction[].class);
  62. Trades trades = MercadoBitcoinAdapters.adaptTrades(transactions, CurrencyPair.BTC_BRL);
  63. assertThat(trades.getTrades().size()).isEqualTo(1000);
  64. assertThat(trades.getlastID()).isEqualTo(99518);
  65. // verify all fields filled
  66. assertThat(trades.getTrades().get(0).getId()).isEqualTo("98519");
  67. assertThat(trades.getTrades().get(0).getPrice().toString()).isEqualTo("1015");
  68. assertThat(trades.getTrades().get(0).getType() == OrderType.BID);
  69. assertThat(trades.getTrades().get(0).getOriginalAmount()).isEqualTo(new BigDecimal("1"));
  70. assertThat(trades.getTrades().get(0).getCurrencyPair()).isEqualTo(CurrencyPair.BTC_BRL);
  71. }
  72. @Test
  73. public void testTickerAdapter() throws IOException {
  74. // Read in the JSON from the example resources
  75. InputStream is =
  76. MercadoBitcoinAdapterTest.class.getResourceAsStream(
  77. "/org/knowm/xchange/mercadobitcoin/dto/marketdata/example-ticker-data.json");
  78. // Use Jackson to parse it
  79. ObjectMapper mapper = new ObjectMapper();
  80. MercadoBitcoinTicker mercadoBitcoinTicker = mapper.readValue(is, MercadoBitcoinTicker.class);
  81. Ticker ticker = MercadoBitcoinAdapters.adaptTicker(mercadoBitcoinTicker, CurrencyPair.BTC_BRL);
  82. assertThat(ticker.getLast().toString()).isEqualTo("1019.99999");
  83. assertThat(ticker.getBid().toString()).isEqualTo("1019.99999");
  84. assertThat(ticker.getAsk().toString()).isEqualTo("1020");
  85. assertThat(ticker.getVolume()).isEqualTo(new BigDecimal("6.90157391"));
  86. assertThat(ticker.getTimestamp()).isEqualTo(new Date(1417226432L * 1000L));
  87. }
  88. @Test
  89. public void testAccountInfoAdapter() throws IOException {
  90. // Read in the JSON from the example resources
  91. InputStream is =
  92. MercadoBitcoinAdapterTest.class.getResourceAsStream(
  93. "/org/knowm/xchange/mercadobitcoin/dto/account/example-accountinfo-data.json");
  94. // Use Jackson to parse it
  95. ObjectMapper mapper = new ObjectMapper();
  96. MercadoBitcoinBaseTradeApiResult<MercadoBitcoinAccountInfo> mercadoBitcoinAccountInfo =
  97. mapper.readValue(
  98. is,
  99. new TypeReference<MercadoBitcoinBaseTradeApiResult<MercadoBitcoinAccountInfo>>() {});
  100. AccountInfo accountInfo =
  101. MercadoBitcoinAdapters.adaptAccountInfo(mercadoBitcoinAccountInfo, "Nina Tufão & Bit");
  102. assertThat(accountInfo.getUsername()).isEqualTo("Nina Tufão & Bit");
  103. assertThat(accountInfo.getWallet().getBalances()).hasSize(3);
  104. assertThat(accountInfo.getWallet().getBalance(Currency.BRL).getCurrency())
  105. .isEqualTo(Currency.BRL);
  106. assertThat(accountInfo.getWallet().getBalance(Currency.BRL).getTotal())
  107. .isEqualTo(new BigDecimal("248.29516"));
  108. assertThat(accountInfo.getWallet().getBalance(Currency.BTC).getCurrency())
  109. .isEqualTo(Currency.BTC);
  110. assertThat(accountInfo.getWallet().getBalance(Currency.BTC).getTotal())
  111. .isEqualTo(new BigDecimal("0.25000000"));
  112. assertThat(accountInfo.getWallet().getBalance(Currency.LTC).getCurrency())
  113. .isEqualTo(Currency.LTC);
  114. assertThat(accountInfo.getWallet().getBalance(Currency.LTC).getTotal())
  115. .isEqualTo(new BigDecimal("0.00000000"));
  116. }
  117. @Test
  118. public void testOrdersAdapter() throws IOException {
  119. // Read in the JSON from the example resources
  120. InputStream is =
  121. MercadoBitcoinAdapterTest.class.getResourceAsStream(
  122. "/org/knowm/xchange/mercadobitcoin/dto/trade/example-userorders.json");
  123. // Use Jackson to parse it
  124. ObjectMapper mapper = new ObjectMapper();
  125. MercadoBitcoinBaseTradeApiResult<MercadoBitcoinUserOrders> apiResult =
  126. mapper.readValue(
  127. is, new TypeReference<MercadoBitcoinBaseTradeApiResult<MercadoBitcoinUserOrders>>() {});
  128. List<LimitOrder> orders =
  129. MercadoBitcoinAdapters.adaptOrders(new CurrencyPair(Currency.LTC, Currency.BRL), apiResult);
  130. Map<String, LimitOrder> orderById = new HashMap<>();
  131. for (LimitOrder order : orders) {
  132. orderById.put(order.getId(), order);
  133. }
  134. assertThat(orderById.get("1212").getType()).isEqualTo(OrderType.ASK);
  135. assertThat(orderById.get("1212").getTimestamp()).isEqualTo(new Date(1378929161000L));
  136. assertThat(orderById.get("1212").getLimitPrice()).isEqualTo(new BigDecimal("6.00000"));
  137. assertThat(orderById.get("1212").getOriginalAmount()).isEqualTo(new BigDecimal("165.47309607"));
  138. assertThat(orderById.get("1212").getCurrencyPair())
  139. .isEqualTo(new CurrencyPair(Currency.LTC, Currency.BRL));
  140. }
  141. }