@Test public void testGetUserDetailsNullResult() { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((UserDataStore.UserDetailsCallback) invocation.getArguments()[1]).onUserEntityLoaded( mockUserEntity); return null; } }).when(cloudUserDataStore).getUserEntityDetails(anyInt(), any(UserDataStore.UserDetailsCallback.class)); given(mockUserEntityDataMapper.transform(mockUserEntity)).willReturn(null); doNothing().when(mockUserDetailsRepositoryCallback).onError(any(RepositoryErrorBundle.class)); userDataRepository.getUserById(FAKE_USER_ID, mockUserDetailsRepositoryCallback); verify(mockUserEntityDataMapper).transform(mockUserEntity); verify(mockUserDetailsRepositoryCallback).onError(any(RepositoryErrorBundle.class)); }
@Override public void getUserById(final int userId, final UserDetailsCallback userCallback) { UserDataStore userDataStore; if (!this.userCache.isExpired() && this.userCache.isCached(userId)) { userDataStore = diskUserDataStore; } else { userDataStore = cloudUserDataStore; } userDataStore.getUserEntityDetails(userId, new UserDataStore.UserDetailsCallback() { @Override public void onUserEntityLoaded(UserEntity userEntity) { UserDataRepository.this.putUserEntityInCache(userEntity); User user = UserDataRepository.this.userEntityDataMapper.transform(userEntity); if (user != null) { userCallback.onUserLoaded(user); } else { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UserNotFound)); } } @Override public void onError(Exception exception) { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UnexpectedException, exception)); } }); }
UserDataRepository implements UserRepository { @Override public void getUserById(final int userId, final UserDetailsCallback userCallback) { UserDataStore userDataStore; if (!this.userCache.isExpired() && this.userCache.isCached(userId)) { userDataStore = diskUserDataStore; } else { userDataStore = cloudUserDataStore; } userDataStore.getUserEntityDetails(userId, new UserDataStore.UserDetailsCallback() { @Override public void onUserEntityLoaded(UserEntity userEntity) { UserDataRepository.this.putUserEntityInCache(userEntity); User user = UserDataRepository.this.userEntityDataMapper.transform(userEntity); if (user != null) { userCallback.onUserLoaded(user); } else { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UserNotFound)); } } @Override public void onError(Exception exception) { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UnexpectedException, exception)); } }); } }
UserDataRepository implements UserRepository { @Override public void getUserById(final int userId, final UserDetailsCallback userCallback) { UserDataStore userDataStore; if (!this.userCache.isExpired() && this.userCache.isCached(userId)) { userDataStore = diskUserDataStore; } else { userDataStore = cloudUserDataStore; } userDataStore.getUserEntityDetails(userId, new UserDataStore.UserDetailsCallback() { @Override public void onUserEntityLoaded(UserEntity userEntity) { UserDataRepository.this.putUserEntityInCache(userEntity); User user = UserDataRepository.this.userEntityDataMapper.transform(userEntity); if (user != null) { userCallback.onUserLoaded(user); } else { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UserNotFound)); } } @Override public void onError(Exception exception) { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UnexpectedException, exception)); } }); }  }
UserDataRepository implements UserRepository { @Override public void getUserById(final int userId, final UserDetailsCallback userCallback) { UserDataStore userDataStore; if (!this.userCache.isExpired() && this.userCache.isCached(userId)) { userDataStore = diskUserDataStore; } else { userDataStore = cloudUserDataStore; } userDataStore.getUserEntityDetails(userId, new UserDataStore.UserDetailsCallback() { @Override public void onUserEntityLoaded(UserEntity userEntity) { UserDataRepository.this.putUserEntityInCache(userEntity); User user = UserDataRepository.this.userEntityDataMapper.transform(userEntity); if (user != null) { userCallback.onUserLoaded(user); } else { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UserNotFound)); } } @Override public void onError(Exception exception) { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UnexpectedException, exception)); } }); }  @Override void getUserList(final UserListCallback userListCallback); @Override void getUserById(final int userId, final UserDetailsCallback userCallback); }
UserDataRepository implements UserRepository { @Override public void getUserById(final int userId, final UserDetailsCallback userCallback) { UserDataStore userDataStore; if (!this.userCache.isExpired() && this.userCache.isCached(userId)) { userDataStore = diskUserDataStore; } else { userDataStore = cloudUserDataStore; } userDataStore.getUserEntityDetails(userId, new UserDataStore.UserDetailsCallback() { @Override public void onUserEntityLoaded(UserEntity userEntity) { UserDataRepository.this.putUserEntityInCache(userEntity); User user = UserDataRepository.this.userEntityDataMapper.transform(userEntity); if (user != null) { userCallback.onUserLoaded(user); } else { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UserNotFound)); } } @Override public void onError(Exception exception) { userCallback.onError(new RepositoryErrorBundle(ErrorBundle.Error.UnexpectedException, exception)); } }); }  @Override void getUserList(final UserListCallback userListCallback); @Override void getUserById(final int userId, final UserDetailsCallback userCallback);  }