A java implementation of the Binance API Specification
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
8.9 KiB

package com.sigmaflare.binancej;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sigmaflare.binancej.entities.ServiceError;
import com.sigmaflare.binancej.entities.TickerPrice;
import com.sigmaflare.binancej.exceptions.BinanceServiceException;
import com.sigmaflare.binancej.exceptions.BinanceServiceUnreachableException;
import com.sigmaflare.binancej.exceptions.MalformedRequestException;
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.nio.charset.StandardCharsets;
import java.util.List;
import static com.sigmaflare.binancej.Constant.BASE_ENDPOINT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TickerPriceTests {
private static final ObjectMapper mapper = HttpRequests.objectMapperBuilder();
private final String url = String.format("%s%s", BASE_ENDPOINT, "/api/v3/ticker/price");
private final String specificUrl = String.format("%s%s", url, "?symbol=TEST");
private String singleTickerPriceJson;
private String multipleTickerPricesJson;
private TickerPrice tickerPrice;
private List<TickerPrice> tickerPrices;
@Before
public void setUp() throws IOException {
ClassLoader classLoader = ExchangeInfoMethodTests.class.getClassLoader();
File singleTickerPriceFile =
new File(classLoader.getResource("single_ticker_price_response.json").getFile());
File multipleTickerPriceFile =
new File(classLoader.getResource("multiple_ticker_prices_response.json").getFile());
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, TickerPrice.class);
singleTickerPriceJson = FileUtils.readFileToString(singleTickerPriceFile, "UTF-8");
multipleTickerPricesJson = FileUtils.readFileToString(multipleTickerPriceFile, "UTF-8");
tickerPrice = mapper.readValue(singleTickerPriceJson, TickerPrice.class);
tickerPrices = mapper.readValue(multipleTickerPricesJson, type);
}
@Test
public void testTickerPriceForSymbolReturnsSuccessfully() throws Exception {
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(specificUrl, "1234"))))
.thenReturn(mockedCloseableHttpResponse);
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(new ByteArrayInputStream(singleTickerPriceJson.getBytes(StandardCharsets.UTF_8)));
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity);
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
TickerPrice res = marketData.getTickerPriceForSymbol("TEST");
assertEquals(tickerPrice, res);
}
@Test
public void testTickerPricesSuccessfully() throws Exception {
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();
httpEntity.setContent(new ByteArrayInputStream(multipleTickerPricesJson.getBytes(StandardCharsets.UTF_8)));
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity);
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
List<TickerPrice> res = marketData.getTickerPrices();
assertEquals(tickerPrices, res);
}
@Test(expected = BinanceServiceUnreachableException.class)
public void testTickerPriceForSymbolWithoutHttpEntityThrows() throws Exception {
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(specificUrl, "1234"))))
.thenReturn(mockedCloseableHttpResponse);
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
marketData.getTickerPriceForSymbol("TEST");
}
@Test(expected = BinanceServiceUnreachableException.class)
public void testTickerPricesWithoutHttpEntityThrows() throws Exception {
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);
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
marketData.getTickerPrices();
}
@Test
public void testTickerPriceForSymbolWithBadApiKeyReturns400() throws Exception {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
when(mockedCloseableHttpResponse.getStatusLine())
.thenReturn(new BasicStatusLine(new ProtocolVersion("TEST", 1, 0),
400, "test"));
when(mockedCloseableHttpClient.execute(argThat(new GetMatcher(specificUrl, "1234"))))
.thenReturn(mockedCloseableHttpResponse);
ServiceError serviceError = ServiceError.builder().code(400).message("Bad API Key").build();
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(
new ByteArrayInputStream(mapper.writeValueAsString(serviceError).getBytes(StandardCharsets.UTF_8)));
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity);
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
try {
marketData.getTickerPriceForSymbol("TEST");
fail();
} catch (BinanceServiceException e) {
if (!(e instanceof MalformedRequestException)) {
fail();
}
assertEquals(400, ((MalformedRequestException) e).getCode());
assertEquals("Bad API Key", e.getMessage());
}
}
@Test
public void testTickerPricesBadApiKeyReturns400() throws Exception {
CloseableHttpClient mockedCloseableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse mockedCloseableHttpResponse = mock(CloseableHttpResponse.class);
when(mockedCloseableHttpResponse.getStatusLine())
.thenReturn(new BasicStatusLine(new ProtocolVersion("TEST", 1, 0),
400, "test"));
when(mockedCloseableHttpClient.execute(argThat(new GetMatcher(url, "1234"))))
.thenReturn(mockedCloseableHttpResponse);
ServiceError serviceError = ServiceError.builder().code(400).message("Bad API Key").build();
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(
new ByteArrayInputStream(mapper.writeValueAsString(serviceError).getBytes(StandardCharsets.UTF_8)));
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity);
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient);
try {
marketData.getTickerPrices();
} catch (BinanceServiceException e) {
if (!(e instanceof MalformedRequestException)) {
fail();
}
assertEquals(400, ((MalformedRequestException) e).getCode());
assertEquals("Bad API Key", e.getMessage());
}
}
}