|
|
|
package com.sigmaflare.binancej;
|
|
|
|
|
|
|
|
|
|
|
|
import com.codepoetics.ambivalence.Either;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.sigmaflare.binancej.entities.ExchangeInfo;
|
|
|
|
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.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 ExchangeInfoMethodTests {
|
|
|
|
private static ObjectMapper mapper = Helpers.objectMapperBuilder();
|
|
|
|
private String url = String.format("%s%s", BASE_ENDPOINT, "/api/v1/exchangeInfo");
|
|
|
|
|
|
|
|
private ExchangeInfo exchangeInfo;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() throws IOException {
|
|
|
|
ClassLoader classLoader = ExchangeInfoMethodTests.class.getClassLoader();
|
|
|
|
File file = new File(classLoader.getResource("exchange_info_response_sample.json").getFile());
|
|
|
|
|
|
|
|
exchangeInfo =
|
|
|
|
mapper.readValue(FileUtils.readFileToString(file, "UTF-8"), ExchangeInfo.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testExchangeInfoServiceReturnsSuccessfully() 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(
|
|
|
|
mapper.writeValueAsString(exchangeInfo).getBytes(StandardCharsets.UTF_8)));
|
|
|
|
|
|
|
|
|
|
|
|
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity);
|
|
|
|
|
|
|
|
GeneralUtilities generalUtilities =
|
|
|
|
new GeneralUtilities("1234", "abcd", mockedCloseableHttpClient);
|
|
|
|
|
|
|
|
Either<ServiceError, ExchangeInfo> res = generalUtilities.getExchangeInfo();
|
|
|
|
|
|
|
|
assertTrue(res.isRight());
|
|
|
|
assertEquals(exchangeInfo, Helpers.extractEitherValueSafely(res.right()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = BinanceServiceUnreachableException.class)
|
|
|
|
public void testExchangeInfoServiceWithNoHttpEntityFails() 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);
|
|
|
|
|
|
|
|
GeneralUtilities generalUtilities =
|
|
|
|
new GeneralUtilities("1234", "abcd", mockedCloseableHttpClient);
|
|
|
|
|
|
|
|
generalUtilities.getExchangeInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testExchangeInfoServiceBadApiKeyReturns400() 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(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);
|
|
|
|
|
|
|
|
GeneralUtilities generalUtilities =
|
|
|
|
new GeneralUtilities("1234", "abcd", mockedCloseableHttpClient);
|
|
|
|
|
|
|
|
Either<ServiceError, ExchangeInfo> res = generalUtilities.getExchangeInfo();
|
|
|
|
|
|
|
|
assertTrue(res.isLeft());
|
|
|
|
assertEquals(serviceError, Helpers.extractEitherValueSafely(res.left()));
|
|
|
|
}
|
|
|
|
}
|