@Override public List<Contact> obtain() { return localGateway.obtainContacts(); } };
@Override public void store(List<Contact> contacts) { localGateway.persist(contacts); } };
@Test public void update_cache_when_hit_network() throws Exception { when(localGateway.obtainContacts()).thenReturn(EMPTY_LIST); when(networkGateway.obtainContacts()).thenReturn(CONTACTS); interactor.call(); verify(localGateway).persist(CONTACTS); }
@Override public InteractorResponse<Contact> call() { try { return new InteractorResponse<>(localGateway.obtain(contactMd5)); } catch (LocalException e) { return new InteractorResponse<>(new GetContactError()); } } }
@Test public void hit_network_when_local_is_empty() { when(localGateway.obtainContacts()).thenReturn(EMPTY_LIST); interactor.call(); verify(networkGateway).obtainContacts(); }
@Test public void return_local_contacts_when_local_storage_is_populated() { when(localGateway.obtainContacts()).thenReturn(CONTACTS); InteractorResponse<List<Contact>> response = interactor.call(); assertThatResponseHasResult(response); }
@Test public void return_networks_contacts_when_network_success() { when(localGateway.obtainContacts()).thenReturn(EMPTY_LIST); when(networkGateway.obtainContacts()).thenReturn(CONTACTS); InteractorResponse<List<Contact>> response = interactor.call(); assertThatResponseHasResult(response); }
@Test public void hit_network_when_local_fails() { when(localGateway.obtainContacts()).thenThrow(new LocalException()); interactor.call(); verify(networkGateway).obtainContacts(); }
@Test public void return_an_error_when_network_fails() { when(localGateway.obtainContacts()).thenReturn(EMPTY_LIST); when(networkGateway.obtainContacts()).thenThrow(new NetworkException()); InteractorResponse<List<Contact>> result = interactor.call(); assertThatResponseHasError(result); }