Add default subscriber. Remove un-meaningful tests.

This commit is contained in:
Fernando Cejas
2015-04-19 14:09:15 +02:00
parent a69552dcc2
commit 33b23eac7c
6 changed files with 130 additions and 94 deletions

View File

@ -27,18 +27,12 @@ public class CloudUserDataStoreTest extends ApplicationTestCase {
private CloudUserDataStore cloudUserDataStore;
@Mock
private RestApi mockRestApi;
@Mock
private UserCache mockUserCache;
@Mock
private UserDataStore.UserListCallback mockUserListDataStoreCallback;
@Mock
private UserDataStore.UserDetailsCallback mockUserDetailsDataStoreCallback;
@Captor
private ArgumentCaptor<RestApi.UserListCallback> restApiUserListCallbackArgumentCaptor;
@Captor
private ArgumentCaptor<RestApi.UserDetailsCallback> restApiUserDetailsCallbackArgumentCaptor;
@Mock private RestApi mockRestApi;
@Mock private UserCache mockUserCache;
@Mock private UserDataStore.UserListCallback mockUserListDataStoreCallback;
@Mock private UserDataStore.UserDetailsCallback mockUserDetailsDataStoreCallback;
@Captor private ArgumentCaptor<RestApi.UserListCallback> restApiUserListCallbackArgumentCaptor;
@Captor private ArgumentCaptor<RestApi.UserDetailsCallback> restApiUserDetailsCallbackArgumentCaptor;
@Before
public void setUp() {
@ -75,32 +69,9 @@ public class CloudUserDataStoreTest extends ApplicationTestCase {
verifyZeroInteractions(mockUserCache);
}
//@Test
//@SuppressWarnings("unchecked")
//public void testGetUserEntityListSuccessfully() {
// Collection<UserEntity> mockUserEntityCollection = (Collection<UserEntity>)mock(Collection.class);
//
// cloudUserDataStore.getUsersEntityList(mockUserListDataStoreCallback);
//
// verify(mockRestApi).getUserList(restApiUserListCallbackArgumentCaptor.capture());
// verifyZeroInteractions(mockUserListDataStoreCallback);
// verifyZeroInteractions(mockUserCache);
//
// restApiUserListCallbackArgumentCaptor.getValue().onUserListLoaded(mockUserEntityCollection);
//
// verify(mockUserListDataStoreCallback).onUserListLoaded(mockUserEntityCollection);
//}
//
//@Test
//public void testGetUserEntityListError() {
// cloudUserDataStore.getUsersEntityList(mockUserListDataStoreCallback);
//
// verify(mockRestApi).getUserList(restApiUserListCallbackArgumentCaptor.capture());
// verifyZeroInteractions(mockUserListDataStoreCallback);
//
// restApiUserListCallbackArgumentCaptor.getValue().onError(any(Exception.class));
//
// verify(mockUserListDataStoreCallback).onError(any(Exception.class));
// verifyZeroInteractions(mockUserCache);
//}
@Test
public void testGetUserEntityListFromApi() {
cloudUserDataStore.getUserEntityList();
verify(mockRestApi).getUserEntityList();
}
}

View File

@ -0,0 +1,24 @@
/**
* Copyright (C) 2015 android10.org. All rights reserved.
* @author Fernando Cejas (the android10 coder)
*/
package com.fernandocejas.android10.sample.domain;
import rx.Subscriber;
/**
* Default subscriber base class to be used whenever you want default error handling
*/
public class DefaultSubscriber<T> extends Subscriber<T> {
@Override public void onCompleted() {
// no-op by default.
}
@Override public void onError(Throwable e) {
//todo: default error handling, use a bus here?
}
@Override public void onNext(T t) {
// no-op by default.
}
}

View File

@ -0,0 +1,29 @@
/**
* Copyright (C) 2015 android10.org. All rights reserved.
* @author Fernando Cejas (the android10 coder)
*/
package com.fernandocejas.android10.sample.domain.exception;
/**
* Wrapper around Exceptions used to manage default errors.
*/
public class DefaultErrorBundle implements ErrorBundle {
private final Exception exception;
public DefaultErrorBundle(Exception exception) {
this.exception = exception;
}
public Exception getException() {
return exception;
}
public String getErrorMessage() {
String message = "";
if (this.exception != null) {
this.exception.getMessage();
}
return message;
}
}

View File

@ -4,15 +4,14 @@
*/
package com.fernandocejas.android10.sample.domain.interactor;
import com.fernandocejas.android10.sample.domain.DefaultSubscriber;
import com.fernandocejas.android10.sample.domain.User;
import com.fernandocejas.android10.sample.domain.exception.ErrorBundle;
import com.fernandocejas.android10.sample.domain.exception.DefaultErrorBundle;
import com.fernandocejas.android10.sample.domain.executor.PostExecutionThread;
import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor;
import com.fernandocejas.android10.sample.domain.repository.UserRepository;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject;
import rx.Observer;
/**
* This class is an implementation of {@link GetUserListUseCase} that represents a use case for
@ -52,38 +51,34 @@ public class GetUserListUseCaseImpl implements GetUserListUseCase {
}
@Override public void run() {
//todo clean this: for now is the first step to move to a reactive approach
//For now this is being executed in a separate thread but should be change for schedulers.
this.userRepository.getUsers().subscribe(new Observer<List<User>>() {
@Override public void onCompleted() {
//todo
//empty for now.
}
@Override public void onError(Throwable e) {
//todo rethink error handling
notifyError(null);
}
@Override public void onNext(List<User> users) {
notifyGetUserListSuccessfully(users);
}
});
this.userRepository.getUsers()
.subscribe(new GetUserListSubscriber(callback, postExecutionThread));
}
private void notifyGetUserListSuccessfully(final Collection<User> usersCollection) {
this.postExecutionThread.post(new Runnable() {
@Override public void run() {
callback.onUserListLoaded(usersCollection);
}
});
}
private static class GetUserListSubscriber extends DefaultSubscriber<List<User>> {
private final Callback callback;
private final PostExecutionThread postExecutionThread;
private void notifyError(final ErrorBundle errorBundle) {
this.postExecutionThread.post(new Runnable() {
@Override public void run() {
callback.onError(errorBundle);
}
});
public GetUserListSubscriber(Callback callback, PostExecutionThread postExecutionThread) {
this.callback = callback;
this.postExecutionThread = postExecutionThread;
}
@Override public void onError(final Throwable e) {
this.postExecutionThread.post(new Runnable() {
@Override public void run() {
callback.onError(new DefaultErrorBundle((Exception)e));
}
});
}
@Override public void onNext(final List<User> users) {
this.postExecutionThread.post(new Runnable() {
@Override public void run() {
callback.onUserListLoaded(users);
}
});
}
}
}

View File

@ -0,0 +1,28 @@
package com.fernandocejas.android10.sample.domain.exception;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.verify;
public class DefaultErrorBundleTest {
private DefaultErrorBundle defaultErrorBundle;
@Mock
private Exception mockException;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
defaultErrorBundle = new DefaultErrorBundle(mockException);
}
@Test
public void testGetErrorMessageInteraction() {
defaultErrorBundle.getErrorMessage();
verify(mockException).getMessage();
}
}

View File

@ -18,7 +18,6 @@ import rx.Observable;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
@ -27,12 +26,10 @@ public class GetUserListUseCaseTest {
private GetUserListUseCase getUserListUseCase;
@Mock
private ThreadExecutor mockThreadExecutor;
@Mock
private PostExecutionThread mockPostExecutionThread;
@Mock
private UserRepository mockUserRepository;
@Mock private ThreadExecutor mockThreadExecutor;
@Mock private PostExecutionThread mockPostExecutionThread;
@Mock private UserRepository mockUserRepository;
@Mock private GetUserListUseCase.Callback mockGetUserListCallback;
@Before
public void setUp() {
@ -43,8 +40,6 @@ public class GetUserListUseCaseTest {
@Test
public void testGetUserListUseCaseExecution() {
GetUserListUseCase.Callback mockGetUserListCallback = mock(GetUserListUseCase.Callback.class);
getUserListUseCase.execute(mockGetUserListCallback);
verify(mockThreadExecutor).execute(any(Interactor.class));
@ -55,8 +50,7 @@ public class GetUserListUseCaseTest {
@Test
public void testGetUserListUseCaseInteractorRun() {
given(mockUserRepository.getUsers()).willReturn(buildUserListTestObservable());
GetUserListUseCase.Callback mockGetUserListCallback = mock(GetUserListUseCase.Callback.class);
given(mockUserRepository.getUsers()).willReturn(Observable.just(buildTestUserList()));
getUserListUseCase.execute(mockGetUserListCallback);
getUserListUseCase.run();
@ -67,17 +61,12 @@ public class GetUserListUseCaseTest {
verifyNoMoreInteractions(mockThreadExecutor);
}
private Observable<List<User>> buildUserListTestObservable() {
private List<User> buildTestUserList() {
List<User> userList = new ArrayList<>();
return Observable.just(userList);
}
userList.add(new User(1));
userList.add(new User(2));
userList.add(new User(3));
//@Test
//@SuppressWarnings("unchecked")
//public void testUserListUseCaseCallbackSuccessful() {
//}
//
//@Test
//public void testUserListUseCaseCallbackError() {
//}
return userList;
}
}