1 package org.oxerr.okcoin.rest;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5
6 import java.io.IOException;
7 import java.math.BigDecimal;
8 import java.util.Arrays;
9 import java.util.Map;
10
11 import org.junit.Test;
12 import org.knowm.xchange.currency.Currency;
13 import org.knowm.xchange.currency.CurrencyPair;
14 import org.knowm.xchange.dto.Order;
15 import org.knowm.xchange.dto.Order.OrderType;
16 import org.knowm.xchange.dto.account.AccountInfo;
17 import org.knowm.xchange.dto.account.Balance;
18 import org.knowm.xchange.dto.marketdata.OrderBook;
19 import org.knowm.xchange.dto.marketdata.Ticker;
20 import org.knowm.xchange.dto.marketdata.Trades;
21 import org.knowm.xchange.dto.marketdata.Trades.TradeSortType;
22 import org.knowm.xchange.dto.trade.OpenOrders;
23 import org.knowm.xchange.dto.trade.UserTrade;
24 import org.knowm.xchange.dto.trade.UserTrades;
25 import org.oxerr.okcoin.rest.dto.Depth;
26 import org.oxerr.okcoin.rest.dto.OrderHistory;
27 import org.oxerr.okcoin.rest.dto.OrderResult;
28 import org.oxerr.okcoin.rest.dto.TickerResponse;
29 import org.oxerr.okcoin.rest.dto.Trade;
30 import org.oxerr.okcoin.rest.dto.UserInfo;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.fasterxml.jackson.databind.ObjectMapper;
35
36 public class OKCoinAdaptersTest {
37
38 private final Logger log = LoggerFactory.getLogger(OKCoinAdaptersTest.class);
39
40 private ObjectMapper mapper = new ObjectMapper();
41
42 @Test
43 public void testAdaptSymbolCurrencyPair() {
44 assertEquals("btc_cny", OKCoinAdapters.adaptSymbol(CurrencyPair.BTC_CNY));
45 }
46
47 @Test
48 public void testAdaptSymbolString() {
49 assertEquals(CurrencyPair.BTC_CNY, OKCoinAdapters.adaptSymbol("btc_cny"));
50 }
51
52 @Test
53 public void testAdaptTicker() throws IOException {
54 TickerResponse tickerResponse = mapper.readValue(getClass()
55 .getResource("dto/ticker.json"),
56 TickerResponse.class);
57 Ticker ticker = OKCoinAdapters.adaptTicker(tickerResponse, CurrencyPair.BTC_CNY);
58 assertEquals(1410431279_000L, ticker.getTimestamp().getTime());
59 assertEquals(new BigDecimal("34.15"), ticker.getHigh());
60 assertEquals(new BigDecimal("32.05"), ticker.getLow());
61 assertEquals(new BigDecimal("33.15"), ticker.getBid());
62 assertEquals(new BigDecimal("33.16"), ticker.getAsk());
63 assertEquals(new BigDecimal("33.15"), ticker.getLast());
64 assertEquals(new BigDecimal("10532696.39199642"), ticker.getVolume());
65 }
66
67 @Test
68 public void testAdaptOrderBook() throws IOException {
69 Depth depth = mapper.readValue(
70 getClass().getResource("dto/depth.json"), Depth.class);
71 OrderBook orderBook = OKCoinAdapters.adaptOrderBook(depth, CurrencyPair.BTC_CNY);
72 assertEquals(5, orderBook.getAsks().size());
73
74
75
76 assertEquals(new BigDecimal("787.27"), orderBook.getAsks().get(0).getLimitPrice());
77 assertEquals(new BigDecimal("0.02"), orderBook.getAsks().get(0).getTradableAmount());
78 assertEquals(Order.OrderType.ASK, orderBook.getAsks().get(0).getType());
79 assertEquals(CurrencyPair.BTC_CNY, orderBook.getAsks().get(0).getCurrencyPair());
80
81
82 assertEquals(new BigDecimal("788.43"), orderBook.getAsks().get(1).getLimitPrice());
83 assertEquals(new BigDecimal("0.036"), orderBook.getAsks().get(1).getTradableAmount());
84 assertEquals(OrderType.ASK, orderBook.getAsks().get(1).getType());
85 assertEquals(CurrencyPair.BTC_CNY, orderBook.getAsks().get(1).getCurrencyPair());
86
87
88 assertEquals(new BigDecimal("788.99"), orderBook.getAsks().get(2).getLimitPrice());
89 assertEquals(new BigDecimal("0.042"), orderBook.getAsks().get(2).getTradableAmount());
90 assertEquals(OrderType.ASK, orderBook.getAsks().get(2).getType());
91 assertEquals(CurrencyPair.BTC_CNY, orderBook.getAsks().get(2).getCurrencyPair());
92
93
94 assertEquals(new BigDecimal("789.68"), orderBook.getAsks().get(3).getLimitPrice());
95 assertEquals(new BigDecimal("0.018"), orderBook.getAsks().get(3).getTradableAmount());
96 assertEquals(OrderType.ASK, orderBook.getAsks().get(3).getType());
97 assertEquals(CurrencyPair.BTC_CNY, orderBook.getAsks().get(3).getCurrencyPair());
98
99
100 assertEquals(new BigDecimal("792"), orderBook.getAsks().get(4).getLimitPrice());
101 assertEquals(new BigDecimal("5"), orderBook.getAsks().get(4).getTradableAmount());
102 assertEquals(OrderType.ASK, orderBook.getAsks().get(4).getType());
103 assertEquals(CurrencyPair.BTC_CNY, orderBook.getAsks().get(4).getCurrencyPair());
104
105 assertEquals(7, orderBook.getBids().size());
106
107
108
109 assertEquals(new BigDecimal("787.1"), orderBook.getBids().get(0).getLimitPrice());
110 assertEquals(new BigDecimal("0.35"), orderBook.getBids().get(0).getTradableAmount());
111 assertEquals(Order.OrderType.BID, orderBook.getBids().get(0).getType());
112 assertEquals(CurrencyPair.BTC_CNY, orderBook.getBids().get(0).getCurrencyPair());
113
114
115 assertEquals(new BigDecimal("787"), orderBook.getBids().get(1).getLimitPrice());
116 assertEquals(new BigDecimal("12.071"), orderBook.getBids().get(1).getTradableAmount());
117 assertEquals(OrderType.BID, orderBook.getBids().get(1).getType());
118 assertEquals(CurrencyPair.BTC_CNY, orderBook.getBids().get(1).getCurrencyPair());
119
120
121 assertEquals(new BigDecimal("786.5"), orderBook.getBids().get(2).getLimitPrice());
122 assertEquals(new BigDecimal("0.014"), orderBook.getBids().get(2).getTradableAmount());
123 assertEquals(OrderType.BID, orderBook.getBids().get(2).getType());
124 assertEquals(CurrencyPair.BTC_CNY, orderBook.getBids().get(2).getCurrencyPair());
125
126
127 assertEquals(new BigDecimal("786.2"), orderBook.getBids().get(3).getLimitPrice());
128 assertEquals(new BigDecimal("0.38"), orderBook.getBids().get(3).getTradableAmount());
129 assertEquals(OrderType.BID, orderBook.getBids().get(3).getType());
130 assertEquals(CurrencyPair.BTC_CNY, orderBook.getBids().get(3).getCurrencyPair());
131
132
133 assertEquals(new BigDecimal("786"), orderBook.getBids().get(4).getLimitPrice());
134 assertEquals(new BigDecimal("3.217"), orderBook.getBids().get(4).getTradableAmount());
135 assertEquals(OrderType.BID, orderBook.getBids().get(4).getType());
136 assertEquals(CurrencyPair.BTC_CNY, orderBook.getBids().get(4).getCurrencyPair());
137
138
139 assertEquals(new BigDecimal("785.3"), orderBook.getBids().get(5).getLimitPrice());
140 assertEquals(new BigDecimal("5.322"), orderBook.getBids().get(5).getTradableAmount());
141 assertEquals(OrderType.BID, orderBook.getBids().get(5).getType());
142 assertEquals(CurrencyPair.BTC_CNY, orderBook.getBids().get(5).getCurrencyPair());
143
144
145 assertEquals(new BigDecimal("785.04"), orderBook.getBids().get(6).getLimitPrice());
146 assertEquals(new BigDecimal("5.04"), orderBook.getBids().get(6).getTradableAmount());
147 assertEquals(OrderType.BID, orderBook.getBids().get(6).getType());
148 assertEquals(CurrencyPair.BTC_CNY, orderBook.getBids().get(6).getCurrencyPair());
149 }
150
151 @Test
152 public void testAdaptTrades() throws IOException {
153 Trade[] tradeArray = mapper.readValue(
154 getClass().getResource("dto/trades.json"), Trade[].class);
155 Trades trades = OKCoinAdapters.adaptTrades(tradeArray, CurrencyPair.BTC_CNY);
156 assertEquals(230512L, trades.getlastID());
157 assertEquals(TradeSortType.SortByTimestamp, trades.getTradeSortType());
158 assertEquals(4, trades.getTrades().size());
159
160 assertEquals(1367130137000L, trades.getTrades().get(0).getTimestamp().getTime());
161 assertEquals(new BigDecimal("787.71"), trades.getTrades().get(0).getPrice());
162 assertEquals(new BigDecimal("0.003"), trades.getTrades().get(0).getTradableAmount());
163 assertEquals("230433", trades.getTrades().get(0).getId());
164 assertEquals(OrderType.ASK, trades.getTrades().get(0).getType());
165
166 assertEquals(1367130137000L, trades.getTrades().get(1).getTimestamp().getTime());
167 assertEquals(new BigDecimal("787.65"), trades.getTrades().get(1).getPrice());
168 assertEquals(new BigDecimal("0.001"), trades.getTrades().get(1).getTradableAmount());
169 assertEquals("230434", trades.getTrades().get(1).getId());
170 assertEquals(OrderType.ASK, trades.getTrades().get(1).getType());
171
172 assertEquals(1367130137000L, trades.getTrades().get(2).getTimestamp().getTime());
173 assertEquals(new BigDecimal("787.5"), trades.getTrades().get(2).getPrice());
174 assertEquals(new BigDecimal("0.091"), trades.getTrades().get(2).getTradableAmount());
175 assertEquals("230435", trades.getTrades().get(2).getId());
176 assertEquals(OrderType.ASK, trades.getTrades().get(2).getType());
177
178 assertEquals(1367131526000L, trades.getTrades().get(3).getTimestamp().getTime());
179 assertEquals(new BigDecimal("787.27"), trades.getTrades().get(3).getPrice());
180 assertEquals(new BigDecimal("0.03"), trades.getTrades().get(3).getTradableAmount());
181 assertEquals("230512", trades.getTrades().get(3).getId());
182 assertEquals(OrderType.BID, trades.getTrades().get(3).getType());
183 }
184
185 @Test
186 public void testAdaptAccountInfo() throws IOException {
187 UserInfo userInfo = mapper.readValue(
188 getClass().getResource("dto/userinfo.json"), UserInfo.class);
189 AccountInfo accountInfo = OKCoinAdapters.adaptAccountInfo(userInfo);
190 for (Map.Entry<Currency, Balance> entry : accountInfo.getWallet().getBalances().entrySet()) {
191 log.debug("{}: {}", entry.getKey(), entry.getValue());
192 }
193 assertEquals(new BigDecimal("0"), accountInfo.getWallet().getBalance(Currency.CNY).getBorrowed());
194 assertEquals(new BigDecimal("1000"), accountInfo.getWallet().getBalance(Currency.CNY).getAvailable());
195 assertEquals(new BigDecimal("1000"), accountInfo.getWallet().getBalance(Currency.CNY).getFrozen());
196
197 assertEquals(new BigDecimal("0"), accountInfo.getWallet().getBalance(Currency.BTC).getBorrowed());
198 assertEquals(new BigDecimal("10"), accountInfo.getWallet().getBalance(Currency.BTC).getAvailable());
199 assertEquals(new BigDecimal("10"), accountInfo.getWallet().getBalance(Currency.BTC).getFrozen());
200
201 assertEquals(new BigDecimal("0"), accountInfo.getWallet().getBalance(Currency.LTC).getBorrowed());
202 assertEquals(new BigDecimal("0"), accountInfo.getWallet().getBalance(Currency.LTC).getAvailable());
203 assertEquals(new BigDecimal("0"), accountInfo.getWallet().getBalance(Currency.LTC).getFrozen());
204 }
205
206 @Test
207 public void testAdaptOpenOrders() throws IOException {
208 OrderResult orderResult = mapper.readValue(
209 getClass().getResource("dto/order_info.json"),
210 OrderResult.class);
211 OpenOrders openOrders = OKCoinAdapters.adaptOpenOrders(Arrays.asList(orderResult));
212 assertEquals(2, openOrders.getOpenOrders().size());
213
214 assertEquals("10000591", openOrders.getOpenOrders().get(0).getId());
215 assertEquals(CurrencyPair.BTC_CNY, openOrders.getOpenOrders().get(0).getCurrencyPair());
216 assertEquals(OrderType.ASK, openOrders.getOpenOrders().get(0).getType());
217 assertEquals(new BigDecimal("500"), openOrders.getOpenOrders().get(0).getLimitPrice());
218 assertEquals(new BigDecimal("0.1"), openOrders.getOpenOrders().get(0).getTradableAmount());
219
220 assertEquals("10000724", openOrders.getOpenOrders().get(1).getId());
221 assertEquals(CurrencyPair.BTC_CNY, openOrders.getOpenOrders().get(1).getCurrencyPair());
222 assertEquals(OrderType.BID, openOrders.getOpenOrders().get(1).getType());
223 assertEquals(new BigDecimal("0.1"), openOrders.getOpenOrders().get(1).getLimitPrice());
224 assertEquals(new BigDecimal("0.2"), openOrders.getOpenOrders().get(1).getTradableAmount());
225 }
226
227 @Test
228 public void testAdaptUserTrades() throws IOException {
229 OrderHistory orderHistory = mapper.readValue(getClass().getResource("dto/order_history_with_market_orders.json"), OrderHistory.class);
230 UserTrades userTrades = OKCoinAdapters.adaptUserTrades(orderHistory);
231
232 UserTrade trade = userTrades.getUserTrades().get(0);
233 assertEquals(new BigDecimal("0.1"), trade.getTradableAmount());
234 assertEquals(new BigDecimal("1467.94"), trade.getPrice());
235 assertEquals(1424313017000L, trade.getTimestamp().getTime());
236 assertEquals("342062526", trade.getOrderId());
237 assertNull(trade.getId());
238 assertEquals(CurrencyPair.BTC_CNY, trade.getCurrencyPair());
239 assertEquals(OrderType.ASK, trade.getType());
240
241 trade = userTrades.getUserTrades().get(7);
242 assertEquals(new BigDecimal("0.01"), trade.getTradableAmount());
243 assertEquals(new BigDecimal("1467.98"), trade.getPrice());
244 assertEquals(1424315429000L, trade.getTimestamp().getTime());
245 assertEquals("342110762", trade.getOrderId());
246 assertNull(trade.getId());
247 assertEquals(CurrencyPair.BTC_CNY, trade.getCurrencyPair());
248 assertEquals(OrderType.ASK, trade.getType());
249
250 trade = userTrades.getUserTrades().get(8);
251 assertEquals(new BigDecimal("0.01"), trade.getTradableAmount());
252 assertEquals(new BigDecimal("1467.99"), trade.getPrice());
253 assertEquals(1424315585000L, trade.getTimestamp().getTime());
254 assertEquals("342113073", trade.getOrderId());
255 assertNull(trade.getId());
256 assertEquals(CurrencyPair.BTC_CNY, trade.getCurrencyPair());
257 assertEquals(OrderType.BID, trade.getType());
258 }
259
260 }