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.
109 lines
4.5 KiB
109 lines
4.5 KiB
package com.sigmaflare.binancej; |
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
import com.sigmaflare.binancej.entities.ServiceError; |
|
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.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.Test; |
|
|
|
import java.io.ByteArrayInputStream; |
|
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.fail; |
|
import static org.mockito.ArgumentMatchers.argThat; |
|
import static org.mockito.Mockito.mock; |
|
import static org.mockito.Mockito.when; |
|
|
|
public class PingMethodTests { |
|
private static ObjectMapper mapper = new ObjectMapper().findAndRegisterModules(); |
|
private final String url = String.format("%s%s", BASE_ENDPOINT, "/api/v1/ping"); |
|
|
|
@Test |
|
public void testServiceAliveReturnsSuccess() 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("{}".getBytes(StandardCharsets.UTF_8))); |
|
|
|
when(mockedCloseableHttpResponse.getEntity()).thenReturn(httpEntity); |
|
|
|
GeneralUtilities generalUtilities = |
|
new GeneralUtilities("1234", "abcd", mockedCloseableHttpClient); |
|
|
|
// Ping returns an object that is an empty Ping - all we care is that is doesn't throw |
|
generalUtilities.ping(); |
|
} |
|
|
|
@Test(expected = BinanceServiceUnreachableException.class) |
|
public void testServiceFailsToReturnHttpEntity() 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); |
|
|
|
GeneralUtilities generalUtilities = |
|
new GeneralUtilities("1234", "abcd", mockedCloseableHttpClient); |
|
|
|
generalUtilities.ping(); |
|
} |
|
|
|
@Test |
|
public void testServiceBadApiKeyReturns400() throws Exception { |
|
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); |
|
|
|
try { |
|
generalUtilities.ping(); |
|
fail(); |
|
} catch (BinanceServiceException e) { |
|
if (!(e instanceof MalformedRequestException)) { |
|
fail(); |
|
} |
|
assertEquals(400, ((MalformedRequestException) e).getCode()); |
|
assertEquals("Bad API Key", e.getMessage()); |
|
} |
|
} |
|
}
|
|
|