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);  }
@Test public void testGetUserDetailsSuccess() { 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(mockUser); userDataRepository.getUserById(FAKE_USER_ID, mockUserDetailsRepositoryCallback); verify(mockUserEntityDataMapper).transform(mockUserEntity); verify(mockUserDetailsRepositoryCallback).onUserLoaded(mockUser); verify(mockUserCache).put(mockUserEntity); }
@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)); }
@Test public void testGetUserByIdError() { doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((UserDataStore.UserDetailsCallback) invocation.getArguments()[1]).onError( any(Exception.class)); return null; } }).when(cloudUserDataStore).getUserEntityDetails(anyInt(), any(UserDataStore.UserDetailsCallback.class)); userDataRepository.getUserById(FAKE_USER_ID, mockUserDetailsRepositoryCallback); verify(mockUserDetailsRepositoryCallback).onError(any(RepositoryErrorBundle.class)); verifyZeroInteractions(mockUserEntityDataMapper); }