public ShapeShiftCoins(JSONObject data) throws ShapeShiftException { super(data); if (!isError) { try { ImmutableList.Builder<ShapeShiftCoin> listBuilder = ImmutableList.builder(); Iterator iter = data.keys(); while (iter.hasNext()) { String k = (String) iter.next(); listBuilder.add(new ShapeShiftCoin(data.getJSONObject(k))); } coins = listBuilder.build(); ImmutableList.Builder<CoinType> typesBuilder = ImmutableList.builder(); for (ShapeShiftCoin coin : coins) { if (coin.isAvailable && CoinID.isSymbolSupported(coin.symbol)) { typesBuilder.add(CoinID.typeFromSymbol(coin.symbol)); } } availableCoinTypes = typesBuilder.build(); } catch (Exception e) { throw new ShapeShiftException("Could not parse object", e); } } else { coins = null; availableCoinTypes = null; } } }
@Test(expected = ShapeShiftException.class) public void testInvalidCoin() throws ShapeShiftException, JSONException { JSONObject json = new JSONObject( "{" + "LTC: {" + "bad: \"\"" + "}" + "}"); new ShapeShiftCoin(json); }
@Test public void testCoin() throws JSONException, ShapeShiftException { JSONObject json = new JSONObject( "{" + "name: \"Bitcoin\"," + "symbol: \"BTC\"," + "image: \"https://shapeshift.io/images/coins/bitcoin.png\"," + "status: \"available\"" + "}"); ShapeShiftCoin coin = new ShapeShiftCoin(json); assertNotNull(coin); assertFalse(coin.isError); assertEquals("Bitcoin", coin.name); assertEquals("BTC", coin.symbol); assertEquals("https://shapeshift.io/images/coins/bitcoin.png", coin.image.toString()); assertTrue(coin.isAvailable); }
@Test public void testCoinError() throws JSONException, ShapeShiftException { JSONObject json = new JSONObject("{ error: \"error\" }"); ShapeShiftCoin coin = new ShapeShiftCoin(json); assertNotNull(coin); assertTrue(coin.isError); assertEquals("error", coin.errorMessage); assertNull(coin.name); assertNull(coin.symbol); assertNull(coin.image); assertFalse(coin.isAvailable); }