RxRocketChatClient implements ConnectListener { public Single<Token> signin(final String username, final String password) { return Single.create(new SingleOnSubscribe<Token>() { @Override public void subscribe(final SingleEmitter<Token> emitter) throws Exception { client.signin(username, password, new LoginCallback() { @Override public void onLoginSuccess(Token token) { emitter.onSuccess(token); } @Override public void onError(RocketChatException error) { emitter.onError(error); } }); } }); } RxRocketChatClient(RocketChatClient client); Single<Token> signin(final String username, final String password); Flowable<State> connect(); static Completable verifyServerVersion(final OkHttpClient client, final String baseUrl,
                                                  final RocketChatVerifier.VersionVerifier verifier); @Override void onConnect(String sessionID); @Override void onDisconnect(boolean closedByServer); @Override void onConnectError(Throwable websocketException);  }
@Test public void tesSigninShouldBeSuccessfull() { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((LoginCallback) invocation.getArguments()[2]).onLoginSuccess(token); return null; } }).when(api).signin(any(String.class), any(String.class), any(LoginCallback.class)); TestObserver<Token> testObserver = sut.signin("username", "password").test(); testObserver.assertValue(new Predicate<Token>() { @Override public boolean test(Token token) throws Exception { return token != null && token.userId().contentEquals("userId") && token.authToken().contentEquals("token"); } }); testObserver.assertComplete(); testObserver.assertNoErrors(); }
@Test public void testSigninShouldFailWithNullUsername() { doThrow(new IllegalArgumentException("username == null")) .when(api).signin((String) isNull(), any(String.class), any(LoginCallback.class)); TestObserver<Token> testObserver = sut.signin(null, "password").test(); testObserver.assertError(IllegalArgumentException.class); }
@Test public void testSigninShouldFailWithNullPassword() { doThrow(new IllegalArgumentException("password == null")) .when(api).signin(any(String.class), (String) isNull(), any(LoginCallback.class)); TestObserver<Token> testObserver = sut.signin("username", null).test(); testObserver.assertError(IllegalArgumentException.class); }
@Test public void testSigninShouldFailWithRocketChatNetworkErrorException() { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((LoginCallback) invocation.getArguments()[2]).onError(new RocketChatNetworkErrorException("network error")); return null; } }).when(api).signin(any(String.class), any(String.class), any(LoginCallback.class)); TestObserver<Token> testObserver = sut.signin("username", "password").test(); testObserver.assertError(new Predicate<Throwable>() { @Override public boolean test(Throwable throwable) throws Exception { return throwable != null && throwable instanceof RocketChatNetworkErrorException && throwable.getMessage().contentEquals("network error"); } }); testObserver.assertNoValues(); }
@Test public void testSigninShouldFailWithRocketChatAuthException() { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((LoginCallback) invocation.getArguments()[2]).onError(new RocketChatAuthException("Invalid credentials")); return null; } }).when(api).signin(any(String.class), any(String.class), any(LoginCallback.class)); TestObserver<Token> testObserver = sut.signin("username", "password").test(); testObserver.assertError(new Predicate<Throwable>() { @Override public boolean test(Throwable throwable) throws Exception { return throwable != null && throwable instanceof RocketChatAuthException && throwable.getMessage().contentEquals("Invalid credentials"); } }); testObserver.assertNoValues(); }