Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lindberg committed Oct 11, 2017
1 parent 86300ad commit 1bf05da
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion c++/src/JReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace JData
const JNINativeMethod nativesTable[2]{ { (char*)"jcallback_BrokerError", (char*)"(Ljava/lang/String;)I", (void *)&jcallback_BrokerError },
{ (char*)"jcallback_BrokerProgress", (char*)"(I)I", (void *)&jcallback_BrokerProgress } };

const char* JVMClassPathOption = "-Djava.class.path=Plugin/ig/igplugin-0.1.jar";
const char* JVMClassPathOption = "-Djava.class.path=Plugin/ig/igplugin-0.23.jar";
const char* IgZorroBridgePath = "com/danlind/igz/ZorroBridge";
const char* ZorroPath = "com/danlind/igz/Zorro";
const char* ExcPath = "java/lang/Class";
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.danlind.igzplugin</groupId>
<artifactId>igplugin</artifactId>
<version>0.22</version>
<version>0.23</version>

<properties>
<java.version>1.8</java.version>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/danlind/igz/adapter/RestApiAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class RestApiAdapter {
@Autowired
private PluginProperties pluginProperties;


public long getServerTime() {
try {
return restApi.getEncryptionKeySessionV1(loginHandler.getConversationContext()).getTimeStamp();
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/danlind/igz/brokerapi/BrokerLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.danlind.igz.ig.api.client.rest.ConversationContextV3;
import com.danlind.igz.ig.api.client.rest.dto.session.createSessionV3.CreateSessionV3Request;
import com.danlind.igz.ig.api.client.rest.dto.session.refreshSessionV1.RefreshSessionV1Request;
import com.danlind.igz.misc.RetryWithDelay;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
Expand Down Expand Up @@ -80,7 +79,7 @@ private void refreshAccessToken(final ConversationContextV3 contextV3) {
ConversationContextV3 newContextV3 = new ConversationContextV3(restApiAdapter.refreshSessionV1(contextV3, RefreshSessionV1Request.builder().refresh_token(contextV3.getRefreshToken()).build()), contextV3.getAccountId(), contextV3.getApiKey());
authenticationContext.setConversationContext(newContextV3);
} catch (OauthTokenInvalidException e) {
logger.info("Detected invalid oauth token, attempting to reconnect");
logger.info("Detected invalid oauth token, will attempt to reconnect");
disconnect();
}
}
Expand All @@ -92,14 +91,12 @@ private void startRefreshAccessTokenScheduler() {
tokenSubscription.dispose();
}
tokenSubscription = Observable.interval(pluginProperties.getRefreshTokenInterval(), TimeUnit.MILLISECONDS, Schedulers.io())
.doOnError(e -> logger.debug("Error when refreshing session token, retrying"))
.retryWhen(new RetryWithDelay(60, 5000))
.subscribe(x -> {
ConversationContextV3 contextV3 = (ConversationContextV3) authenticationContext.getConversationContext();
refreshAccessToken(contextV3);
},
error -> {
logger.error("Exception after retrying refreshing session token, disconnecting");
logger.error("Exception after retrying refreshing session token, will attempt to reconnect");
disconnect();
}
);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/danlind/igz/ig/api/client/RestAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public AuthenticationResponseAndConversationContext createSession(CreateSessionV
}

public AuthenticationResponseAndConversationContext createSessionV3(CreateSessionV3Request authenticationRequest, String apiKey) {

return authenticationService.createSessionV3(authenticationRequest, apiKey);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/danlind/igz/misc/RetryWithDelay.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.danlind.igz.misc;

import com.danlind.igz.brokerapi.BrokerBuy;
import io.reactivex.Observable;
import io.reactivex.functions.Function;
import org.slf4j.Logger;
Expand Down
31 changes: 24 additions & 7 deletions src/test/java/com/danlind/igz/brokerapi/BrokerLoginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;

import java.util.concurrent.ScheduledFuture;
import java.nio.charset.Charset;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
Expand Down Expand Up @@ -53,9 +55,6 @@ public class BrokerLoginTest {
@Mock
ConnectionListener connectionListener;

@Mock
ScheduledFuture scheduledFuture;

@InjectMocks
RestApiAdapter restApiAdapter;

Expand All @@ -66,7 +65,7 @@ public class BrokerLoginTest {

@Before
public void setUp() throws Exception {
brokerLogin = new BrokerLogin(streamingApiAdapter, restApiAdapter, pluginProperties);
brokerLogin = Mockito.spy(new BrokerLogin(streamingApiAdapter, restApiAdapter, pluginProperties));
PowerMockito.mockStatic(Zorro.class);
PowerMockito.when(Zorro.class,"callProgress",anyInt()).thenReturn(1);
PowerMockito.doNothing().when(Zorro.class,"indicateError");
Expand All @@ -77,8 +76,6 @@ public void setUp() throws Exception {
.lightstreamerEndpoint("TestLightstreamerEndpoint")
.build();



when(restApi.createSessionV3(any(), any())).thenReturn(context);
when(streamingAPI.connect(anyString(), any(), anyString())).thenReturn(connectionListener);
when(pluginProperties.getRefreshTokenInterval()).thenReturn(30000);
Expand All @@ -103,11 +100,31 @@ public void testConnectFailedWithOtherException() throws Exception {
assertEquals(0,brokerLogin.connect("TestId", "TestPassword", "Real"));
}

//TODO: Double check if this test is doing what it should
@Test
public void testRefreshToken() throws Exception {
when(pluginProperties.getRefreshTokenInterval()).thenReturn(1);
assertEquals(1,brokerLogin.connect("TestId", "TestPassword", "Real"));
verify(restApi, atLeastOnce()).refreshSessionV1(any(), any());
brokerLogin.disconnect();
}

@Test
public void testOauthTokenInvalid() throws Exception {
when(pluginProperties.getRefreshTokenInterval()).thenReturn(1);
when(restApi.refreshSessionV1(any(), any())).thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED, "", "{\"errorCode\":\"error.security.oauth-token-invalid\"}".getBytes(), Charset.defaultCharset()));
assertEquals(1,brokerLogin.connect("TestId", "TestPassword", "Real"));
Thread.sleep(10);
verify(brokerLogin, atLeastOnce()).disconnect();
}

@Test
public void testOtherException() throws Exception {
when(pluginProperties.getRefreshTokenInterval()).thenReturn(1);
when(restApi.refreshSessionV1(any(), any())).thenThrow(new HttpServerErrorException(HttpStatus.SERVICE_UNAVAILABLE));
assertEquals(1,brokerLogin.connect("TestId", "TestPassword", "Real"));
Thread.sleep(10);
verify(brokerLogin, atLeastOnce()).disconnect();
}

@Test
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/danlind/igz/misc/MarketDataProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ public void testGetContractDetails() throws InterruptedException {
assertEquals(0.0001,contractDetails.getPipSize());
}

@Test
public void testGetContractDetailsException() throws Exception {
GetMarketDetailsV3Response newResponse = response;
newResponse.getInstrument().getCurrencies().get(0).setCode("SEK");
when(restApi.getMarketDetailsV3(any(),anyString())).thenReturn(response).thenThrow(new Exception()).thenReturn(newResponse);
marketDataProvider.updateMarketDetails(testEpic);
Thread.sleep(100);
ContractDetails contractDetails = marketDataProvider.getContractDetails(testEpic);
assertEquals("SEK", contractDetails.getCurrencyCode());
}

@Test
public void testMarketIsTradable() {
assertEquals(1,marketDataProvider.isAnySubscribedEpicTradable());
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
</encoder>
</appender>

<logger name="org.apache.http" level="INFO"/>
<logger name="org.springframework" level="INFO"/>
<logger name="com.danlind.igz.ig" level="INFO"/>
<logger name="org.hibernate" level="INFO"/>


<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
Expand Down

0 comments on commit 1bf05da

Please sign in to comment.