@Override public List<Contact> obtain() { return networkGateway.obtainContacts(); } };
@Override public void store(List<Contact> contacts) { localGateway.persist(contacts); } };
@Override public List<Contact> obtain() { return localGateway.obtainContacts(); } };
@Test public void create_an_exact_copy_of_a_domain_contact() { Contact contact = createFullContact(); PresentationContact mappedContact = contactMapper.map(contact); assertThat(contact.getMd5(), is(mappedContact.getMd5())); assertThat(contact.getCell(), is(mappedContact.getCell())); assertThat(contact.getEmail(), is(mappedContact.getEmail())); assertThat(contact.getGender(), is(mappedContact.getGender())); assertThat(contact.getName().getFirst(), is(mappedContact.getFirstName())); assertThat(contact.getName().getLast(), is(mappedContact.getLastName())); assertThat(contact.getName().getTitle(), is(mappedContact.getTitle())); assertThat(contact.getPhone(), is(mappedContact.getPhone())); assertThat(contact.getPassword(), is(mappedContact.getPassword())); assertThat(contact.getUsername(), is(mappedContact.getUsername())); assertThat(contact.getVersion(), is(mappedContact.getVersion())); assertThat(contact.getLocation().getCity(), is(mappedContact.getLocation().getCity())); assertThat(contact.getLocation().getState(), is(mappedContact.getLocation().getState())); assertThat(contact.getLocation().getStreet(), is(mappedContact.getLocation().getStreet())); assertThat(contact.getLocation().getZip(), is(mappedContact.getLocation().getZip())); assertThat(contact.getPicture().getThumbnail(), is(mappedContact.getPicture().getThumbnail())); assertThat(contact.getPicture().getLarge(), is(mappedContact.getPicture().getLarge())); assertThat(contact.getPicture().getMedium(), is(mappedContact.getPicture().getMedium())); }
private Contact createFullContact() { Contact contact = new Contact(); contact.setMd5("md5"); contact.setCell("cell"); contact.setEmail("email"); contact.setGender("gender"); contact.setPhone("phone"); contact.setPassword("password"); contact.setUsername("username"); contact.setVersion("version"); Name name = new Name(); name.setTitle("title"); name.setFirst("first"); name.setLast("last"); contact.setName(name); Location location = new Location(); location.setCity("city"); location.setState("state"); location.setStreet("street"); location.setZip("zip"); contact.setLocation(location); Picture picture = new Picture(); picture.setThumbnail("thumbnail"); picture.setLarge("large"); picture.setMedium("medium"); contact.setPicture(picture);
@Override public PresentationContact map(Contact model) { if (model == null) { return null; } PresentationContact presentationContact = new PresentationContact(); presentationContact.setMd5(model.getMd5()); presentationContact.setCell(model.getCell()); presentationContact.setEmail(model.getEmail()); presentationContact.setGender(model.getGender()); Name name = model.getName(); if (name != null) { presentationContact.setFirstName(name.getFirst()); presentationContact.setLastName(name.getLast()); presentationContact.setTitle(name.getTitle()); } presentationContact.setPhone(model.getPhone()); presentationContact.setPassword(model.getPassword()); presentationContact.setUsername(model.getUsername()); presentationContact.setVersion(model.getVersion()); presentationContact.setLocation(PRESENTATION_LOCATION_MAPPER.map(model.getLocation())); presentationContact.setPicture(PRESENTATION_PICTURE_MAPPER.map(model.getPicture())); return presentationContact; } }
@Override public PresentationLocation map(Location model) { if (model == null) { return null; } PresentationLocation location = new PresentationLocation(); location.setCity(model.getCity()); location.setState(model.getState()); location.setStreet(model.getStreet()); location.setZip(model.getZip()); return location; } }
@Override public PresentationPicture map(Picture model) { if (model == null) { return null; } PresentationPicture picture = new PresentationPicture(); picture.setLarge(model.getLarge()); picture.setMedium(model.getMedium()); picture.setThumbnail(model.getThumbnail()); return picture; } }
@Override public Picture map(BddPicture model) { if (model == null) { return null; } Picture picture = new Picture(); picture.setLarge(model.getLarge()); picture.setMedium(model.getMedium()); picture.setThumbnail(model.getThumbnail()); return picture; } }
@Test public void hit_network_when_local_fails() { when(localGateway.obtainContacts()).thenThrow(new LocalException()); interactor.call(); verify(networkGateway).obtainContacts(); }
@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); }
@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); }
@Test public void hit_network_when_local_is_empty() { when(localGateway.obtainContacts()).thenReturn(EMPTY_LIST); interactor.call(); verify(networkGateway).obtainContacts(); }
@Override public InteractorResponse<Contact> call() { try { return new InteractorResponse<>(localGateway.obtain(contactMd5)); } catch (LocalException e) { return new InteractorResponse<>(new GetContactError()); } } }
@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 return_local_contacts_when_local_storage_is_populated() { when(localGateway.obtainContacts()).thenReturn(CONTACTS); InteractorResponse<List<Contact>> response = interactor.call(); assertThatResponseHasResult(response); }