|
|
|
package com.sigmaflare.binancej;
|
|
|
|
|
|
|
|
import com.codepoetics.ambivalence.Either;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.sigmaflare.binancej.entities.OrderBookDepth;
|
|
|
|
import com.sigmaflare.binancej.entities.OrderBookPricing;
|
|
|
|
import com.sigmaflare.binancej.entities.ServiceError;
|
|
|
|
import com.sigmaflare.binancej.exceptions.BinanceServiceUnreachableException;
|
|
|
|
import com.sigmaflare.binancej.matchers.GetMatcher;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.apache.http.ProtocolVersion;
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
import org.apache.http.entity.BasicHttpEntity;
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.message.BasicStatusLine;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
import static com.sigmaflare.binancej.Constant.BASE_ENDPOINT;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import static org.mockito.ArgumentMatchers.argThat;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
public class OrderBookDepthTests {
|
|
|
|
private static ObjectMapper mapper = Helpers.objectMapperBuilder();
|
|
|
|
private final String url = String.format("%s%s", BASE_ENDPOINT, "/api/v1/depth?symbol=ETHBTC&limit=100");
|
|
|
|
private final String urlWithLimit = String.format("%s%s", BASE_ENDPOINT, "/api/v1/depth?symbol=ETHBTC&limit=1000");
|
|
|
|
|
|
|
|
private OrderBookDepth orderBookDepth;
|
|
|
|
private String orderBookDepthJson;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() throws IOException {
|
|
|
|
ClassLoader classLoader = ExchangeInfoMethodTests.class.getClassLoader();
|
|
|
|
File file = new File(classLoader.getResource("orderbook_depth_sample.json").getFile());
|
|
|
|
|
|
|
|
orderBookDepthJson = FileUtils.readFileToString(file, "UTF-8");
|
|
|
|
|
|
|
|
orderBookDepth =
|
|
|
|
mapper.readValue(orderBookDepthJson, OrderBookDepth.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is only seen in some test suites - typically ones with custom deserializers
|
|
|
|
@Test
|
|
|
|
public void testValuesAreDeserializedCorrectly() {
|
|
|
|
OrderBookPricing bidPricing = OrderBookPricing
|
|
|
|
.builder()
|
|
|
|
.price(new BigDecimal("4.00000000"))
|
|
|
|
.quantity(new BigDecimal("431.00000000"))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
OrderBookPricing askPricing = OrderBookPricing
|
|
|
|
.builder()
|
|
|
|
.price(new BigDecimal("4.00000200"))
|
|
|
|
.quantity(new BigDecimal("12.00000000"))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
assertEquals(1027024, orderBookDepth.getLastUpdateId());
|
|
|
|
assertEquals(bidPricing, orderBookDepth.getBids().get(0));
|
|
|
|
assertEquals(askPricing, orderBookDepth.getAsks().get(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOrderBookDepthReturnsSuccessfully() throws IOException, BinanceServiceUnreachableException {
|
|
|
|
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
|
|
|
|
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
|
|
|
|
|
|
|
|
when(mockedCloseableHttpResponse.getStatusLine())
|
|
|
|
.thenReturn(new BasicStatusLine(new ProtocolVersion("TEST", 1, 0),
|
|
|
|
200, "test"));
|
|
|
|
|
|
|
|
when(mockedCloseableHttpClient.execute(argThat(new GetMatcher(url, "1234"))))
|
|
|
|
.thenReturn(mockedCloseableHttpResponse);
|
|
|
|
|
|
|
|
BasicHttpEntity httpEntity = new BasicHttpEntity();
|
|
|
|
|
|
|
|
// This changes from the usual way we do this because we do post-processing in the deserializer and the JSON
|
|
|
|
// will no longer match the stringified OrderBookDepth.
|
|
|
|
httpEntity.setContent(new ByteArrayInputStream(orderBookDepthJson.getBytes()));
|
|
|
|
|
|
|
|
|
|
|
|
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity);
|
|
|
|
|
|
|
|
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
|
|
|
|
|
|
|
|
Either<ServiceError, OrderBookDepth> res = marketData.getOrderBookDepth("ETHBTC");
|
|
|
|
|
|
|
|
assertTrue(res.isRight());
|
|
|
|
assertEquals(orderBookDepth, Helpers.extractEitherValueSafely(res.right()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BinanceServiceUnreachableException.class)
|
|
|
|
public void testOrderBookDepthWithNoHttpEntityFails() throws IOException, BinanceServiceUnreachableException {
|
|
|
|
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
|
|
|
|
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
|
|
|
|
|
|
|
|
when(mockedCloseableHttpResponse.getStatusLine())
|
|
|
|
.thenReturn(new BasicStatusLine(new ProtocolVersion("TEST", 1, 0),
|
|
|
|
200, "test"));
|
|
|
|
|
|
|
|
when(mockedCloseableHttpClient.execute(argThat(new GetMatcher(urlWithLimit, "1234"))))
|
|
|
|
.thenReturn(mockedCloseableHttpResponse);
|
|
|
|
|
|
|
|
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
|
|
|
|
|
|
|
|
marketData.getOrderBookDepth("ETHBTC", 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOrderBookDepthWithBadApiKeyReturns400() throws IOException, BinanceServiceUnreachableException {
|
|
|
|
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
|
|
|
|
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
|
|
|
|
|
|
|
|
when(mockedCloseableHttpResponse.getStatusLine())
|
|
|
|
.thenReturn(new BasicStatusLine(new ProtocolVersion("TEST", 1, 0),
|
|
|
|
400, "Bad API Key"));
|
|
|
|
|
|
|
|
when(mockedCloseableHttpClient.execute(argThat(new GetMatcher(urlWithLimit, "1234"))))
|
|
|
|
.thenReturn(mockedCloseableHttpResponse);
|
|
|
|
|
|
|
|
ServiceError serviceError = ServiceError.builder().code(400).message("Bad API Key").build();
|
|
|
|
|
|
|
|
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
|
|
|
|
|
|
|
|
BasicHttpEntity httpEntity = new BasicHttpEntity();
|
|
|
|
httpEntity.setContent(new ByteArrayInputStream(
|
|
|
|
mapper.writeValueAsString(serviceError).getBytes(StandardCharsets.UTF_8)));
|
|
|
|
|
|
|
|
|
|
|
|
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity);
|
|
|
|
|
|
|
|
Either<ServiceError, OrderBookDepth> res = marketData.getOrderBookDepth("ETHBTC", 1000);
|
|
|
|
|
|
|
|
assertTrue(res.isLeft());
|
|
|
|
assertEquals(serviceError, Helpers.extractEitherValueSafely(res.left()));
|
|
|
|
}
|
|
|
|
}
|