Skip to content

Commit

Permalink
Fix: after connect call, login must wait for connection state
Browse files Browse the repository at this point in the history
  • Loading branch information
juxeii authored and juxeii committed Feb 16, 2017
1 parent b64da70 commit 714e4da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/jforex/programming/client/ClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public ClientUtil(final IClient client,

this.client = client;
pinCaptcha = new PinCaptcha(client);
authentification = new Authentification(client, loginStatePublisher);
authentification = new Authentification(client,
observeConnectionState(),
loginStatePublisher);

initCacheDirectory(cacheDirectory);
client.setSystemListener(jfSystemListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
import com.jforex.programming.rx.JFHotPublisher;

import io.reactivex.Completable;
import io.reactivex.Observable;

public class Authentification {

private final IClient client;
private final Observable<ConnectionState> connectionStateObservable;
private final JFHotPublisher<LoginState> loginStatePublisher;

public Authentification(final IClient client,
final Observable<ConnectionState> connectionStateObservable,
final JFHotPublisher<LoginState> loginStatePublisher) {
this.client = client;
this.connectionStateObservable = connectionStateObservable;
this.loginStatePublisher = loginStatePublisher;
}

Expand All @@ -37,6 +41,14 @@ public Completable login(final LoginCredentials loginCredentials) {
: () -> client.connect(jnlpAddress,
username,
password))
.andThen(connectionStateObservable
.take(1)
.map(state -> {
if (state == ConnectionState.CONNECTED)
return state;
throw new ConnectionLostException("Login failed! Connection state: " + state);
}))
.ignoreElements()
.doOnComplete(() -> loginStatePublisher.onNext(LoginState.LOGGED_IN));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,55 @@
import org.junit.runner.RunWith;

import com.jforex.programming.connection.Authentification;
import com.jforex.programming.connection.ConnectionLostException;
import com.jforex.programming.connection.ConnectionState;
import com.jforex.programming.connection.LoginCredentials;
import com.jforex.programming.connection.LoginState;
import com.jforex.programming.rx.JFHotPublisher;
import com.jforex.programming.test.common.CommonUtilForTest;

import de.bechte.junit.runners.context.HierarchicalContextRunner;
import io.reactivex.observers.TestObserver;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

@RunWith(HierarchicalContextRunner.class)
public class AuthentificationTest extends CommonUtilForTest {

private Authentification authentification;

private final JFHotPublisher<LoginState> loginStatePublisher = new JFHotPublisher<>();
private final Subject<ConnectionState> connectionStateSubject = PublishSubject.create();
private TestObserver<LoginState> loginStateSubscriber;

@Before
public void setUp() {
authentification = new Authentification(clientMock, loginStatePublisher);
authentification = new Authentification(clientMock,
connectionStateSubject,
loginStatePublisher);
}

private void login(final LoginCredentials credentials) {
private void loginWithConnectState(final LoginCredentials credentials) {
authentification
.login(credentials)
.subscribe(() -> {},
t -> {});
connectionStateSubject.onNext(ConnectionState.CONNECTED);
}

@Test
public void connectionLostExceptionWhenStateIsNotConnected() {
loginStateSubscriber = loginStatePublisher
.observable()
.test();

final TestObserver<Void> testObserver = authentification
.login(loginCredentials)
.test();
connectionStateSubject.onNext(ConnectionState.DISCONNECTED);

testObserver.assertError(ConnectionLostException.class);
loginStateSubscriber.assertNoValues();
}

@Test
Expand All @@ -42,7 +65,7 @@ public void logoutDoesNotCallsDisconnectOnClientWhenNotSubscribed() {

@Test
public void loginWithPinCallsConnectClientWithPin() throws Exception {
login(loginCredentialsWithPin);
loginWithConnectState(loginCredentialsWithPin);

verify(clientMock).connect(loginCredentialsWithPin.jnlpAddress(),
loginCredentialsWithPin.username(),
Expand All @@ -62,7 +85,7 @@ public void loginWithExecpetionDoesNotPublishLoginStatss() throws Exception {
.observable()
.test();

login(loginCredentialsWithPin);
loginWithConnectState(loginCredentialsWithPin);

loginStateSubscriber.assertNoValues();
}
Expand All @@ -75,7 +98,7 @@ public void setUp() {
.observable()
.test();

login(loginCredentials);
loginWithConnectState(loginCredentials);
}

@Test
Expand Down

0 comments on commit 714e4da

Please sign in to comment.