package com.xeiam.xchange.btcchina.service.streaming;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.math.BigDecimal;

import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import com.xeiam.xchange.btcchina.dto.trade.BTCChinaOrderStatus;
import com.xeiam.xchange.btcchina.dto.trade.streaming.BTCChinaBalance;
import com.xeiam.xchange.btcchina.dto.trade.streaming.BTCChinaOrder;
import com.xeiam.xchange.currency.CurrencyPair;
import com.xeiam.xchange.dto.Order.OrderType;
import com.xeiam.xchange.dto.marketdata.Ticker;
import com.xeiam.xchange.dto.marketdata.Trade;

public class BTCChinaJSONObjectAdaptersTest {

  @Test
  public void testAdaptTicker() throws JSONException, IOException {

    JSONObject jsonObject = new JSONObject(IOUtils.toString(getClass().getResource("ticker.json")));
    Ticker ticker = BTCChinaJSONObjectAdapters.adaptTicker(jsonObject);
    assertEquals(new BigDecimal("90604.96"), ticker.getVolume());
    assertEquals(new BigDecimal("25.96"), ticker.getLast());
    assertEquals(new BigDecimal("26.1"), ticker.getAsk());
    assertEquals(new BigDecimal("25.94"), ticker.getBid());
    assertEquals(new BigDecimal("27.41"), ticker.getHigh());
    assertEquals(1411924391000L, ticker.getTimestamp().getTime());
    assertEquals(new BigDecimal("25.8"), ticker.getLow());
    assertEquals(CurrencyPair.LTC_CNY, ticker.getCurrencyPair());
  }

  @Test
  public void testAdaptTrade() throws JSONException, IOException {

    JSONObject jsonObject = new JSONObject(IOUtils.toString(getClass().getResource("trade.json")));
    Trade trade = BTCChinaJSONObjectAdapters.adaptTrade(jsonObject);
    assertEquals(new BigDecimal("0.79000000"), trade.getTradableAmount());
    assertEquals(CurrencyPair.BTC_CNY, trade.getCurrencyPair());
    assertEquals(new BigDecimal("2350.25"), trade.getPrice());
    assertEquals("8948160", trade.getId());
    assertEquals(1411924375000L, trade.getTimestamp().getTime());
    assertEquals(OrderType.ASK, trade.getType());
  }

  @Test
  public void testAdaptOrder() throws JSONException, IOException {

    JSONObject jsonObject = new JSONObject(IOUtils.toString(getClass().getResource("order.json")));
    BTCChinaOrder order = BTCChinaJSONObjectAdapters.adaptOrder(jsonObject);
    assertEquals(new BigDecimal("0.01"), order.getTradableAmount());
    assertEquals("26821399", order.getId());
    assertEquals(new BigDecimal("3605.23"), order.getLimitPrice());
    assertEquals(CurrencyPair.BTC_CNY, order.getCurrencyPair());
    assertEquals(BTCChinaOrderStatus.CANCELLED, order.getStatus());
    assertEquals(1411924393000L, order.getTimestamp().getTime());
    assertEquals(OrderType.ASK, order.getType());
    assertEquals(new BigDecimal("0.01"), order.getAmountOriginal());
  }

  @Test
  public void testAdaptBalanceBTC() throws JSONException, IOException {

    JSONObject jsonObject = new JSONObject(IOUtils.toString(getClass().getResource("account_info-balance-BTC.json")));
    BTCChinaBalance balance = BTCChinaJSONObjectAdapters.adaptBalance(jsonObject);
    assertEquals(new BigDecimal("196441900"), balance.getAmountInteger());
    assertEquals(new BigDecimal("1.964419"), balance.getAmount());
    assertEquals("฿", balance.getSymbol());
    assertEquals(8, balance.getAmountDecimal());
    assertEquals("BTC", balance.getCurrency());
  }

  @Test
  public void testAdaptBalanceCNY() throws JSONException, IOException {

    JSONObject jsonObject = new JSONObject(IOUtils.toString(getClass().getResource("account_info-balance-CNY.json")));
    BTCChinaBalance balance = BTCChinaJSONObjectAdapters.adaptBalance(jsonObject);
    assertEquals(new BigDecimal("4.2609492000000005E9"), balance.getAmountInteger());
    assertEquals(new BigDecimal("42.609492"), balance.getAmount());
    assertEquals("¥", balance.getSymbol());
    assertEquals(8, balance.getAmountDecimal());
    assertEquals("CNY", balance.getCurrency());
  }

}

@@ -152,16 +152,55 @@
 cimal;%0A%0A
+import org.apache.commons.io.Charsets;%0A
 import o
@@ -229,16 +229,16 @@
 OUtils;%0A
-
 import o
@@ -920,32 +920,48 @@
 e(%22ticker.json%22)
+, Charsets.UTF_8
 ));%0A    Ticker t
@@ -1692,24 +1692,40 @@
 trade.json%22)
+, Charsets.UTF_8
 ));%0A    Trad
@@ -2321,24 +2321,40 @@
 order.json%22)
+, Charsets.UTF_8
 ));%0A    BTCC
@@ -3119,24 +3119,40 @@
 e-BTC.json%22)
+, Charsets.UTF_8
 ));%0A    BTCC
@@ -3587,32 +3587,32 @@
  IOException %7B%0A%0A
-
     JSONObject j
@@ -3710,16 +3710,32 @@
 Y.json%22)
+, Charsets.UTF_8
 ));%0A    

Fix test case BTCChinaJSONObjectAdaptersTest failure in non-UTF-8 environment.