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.
207 lines
9.0 KiB
207 lines
9.0 KiB
package com.sigmaflare.binancej; |
|
|
|
import com.codepoetics.ambivalence.Either; |
|
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.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.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.assertTrue; |
|
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 = Helpers.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 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(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); |
|
|
|
Either<ServiceError, TickerPrice> res = marketData.getTickerPriceForSymbol("TEST"); |
|
|
|
assertTrue(res.isRight()); |
|
assertEquals(Helpers.extractEitherValueSafely(res.right()), tickerPrice); |
|
} |
|
|
|
@Test |
|
public void testTickerPricesSuccessfully() 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(); |
|
httpEntity.setContent(new ByteArrayInputStream(multipleTickerPricesJson.getBytes(StandardCharsets.UTF_8))); |
|
|
|
|
|
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity); |
|
|
|
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient); |
|
|
|
Either<ServiceError, List<TickerPrice>> res = marketData.getTickerPrices(); |
|
|
|
assertTrue(res.isRight()); |
|
assertEquals(Helpers.extractEitherValueSafely(res.right()), tickerPrices); |
|
} |
|
|
|
@Test(expected = BinanceServiceUnreachableException.class) |
|
public void testTickerPriceForSymbolWithoutHttpEntityThrows() |
|
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(specificUrl, "1234")))) |
|
.thenReturn(mockedCloseableHttpResponse); |
|
|
|
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient); |
|
|
|
marketData.getTickerPriceForSymbol("TEST"); |
|
} |
|
|
|
@Test(expected = BinanceServiceUnreachableException.class) |
|
public void testTickerPricesWithoutHttpEntityThrows() |
|
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); |
|
|
|
MarketData marketData = new MarketData("1234", "abcd", mockedCloseableHttpClient); |
|
|
|
marketData.getTickerPrices(); |
|
} |
|
|
|
@Test |
|
public void testTickerPriceForSymbolWithBadApiKeyReturns400() |
|
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, "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); |
|
|
|
Either<ServiceError, TickerPrice> res = marketData.getTickerPriceForSymbol("TEST"); |
|
|
|
assertTrue(res.isLeft()); |
|
assertEquals(Helpers.extractEitherValueSafely(res.left()), serviceError); |
|
} |
|
|
|
@Test |
|
public void testTickerPricesBadApiKeyReturns400() 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, "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); |
|
|
|
Either<ServiceError, List<TickerPrice>> res = marketData.getTickerPrices(); |
|
|
|
assertTrue(res.isLeft()); |
|
assertEquals(Helpers.extractEitherValueSafely(res.left()), serviceError); |
|
} |
|
}
|
|
|