WorldpayAddressPopulator implements Populator<AddressModel, Address> { @Override public void populate(final AddressModel source, final Address target) { validateParameterNotNull(source, "Parameter source (AddressModel) cannot be null"); target.setAddress1(StringEscapeUtils.escapeXml(source.getLine1())); target.setAddress2(StringEscapeUtils.escapeXml(source.getLine2())); target.setCity(StringEscapeUtils.escapeXml(source.getTown())); if (source.getCountry() != null) { target.setCountryCode(source.getCountry().getIsocode()); } target.setFirstName(StringEscapeUtils.escapeXml(source.getFirstname())); target.setLastName(StringEscapeUtils.escapeXml(source.getLastname())); target.setPostalCode(StringEscapeUtils.escapeXml(source.getPostalcode())); target.setTelephoneNumber(source.getPhone1()); if (source.getRegion() != null) { target.setState(source.getRegion().getIsocode()); } }  @Override void populate(final AddressModel source, final Address target);  }
@Test public void testPopulateHappyFlow() { final Address target = new Address(); testObject.populate(sourceMock, target); assertEquals(LINE_1, target.getAddress1()); assertEquals(LINE_2, target.getAddress2()); assertEquals(TOWN, target.getCity()); assertEquals(COUNTRY_ISO_CODE, target.getCountryCode()); assertEquals(FIRSTNAME, target.getFirstName()); assertEquals(LASTNAME, target.getLastName()); assertEquals(POSTALCODE, target.getPostalCode()); assertEquals(TELEPHONE, target.getTelephoneNumber()); assertEquals(STATE, target.getState()); }
@Test public void testThatStateIsNotSetWhenRegionIsNull() { when(sourceMock.getRegion()).thenReturn(null); final Address target = new Address(); testObject.populate(sourceMock, target); assertEquals(LINE_1, target.getAddress1()); assertEquals(LINE_2, target.getAddress2()); assertEquals(TOWN, target.getCity()); assertEquals(COUNTRY_ISO_CODE, target.getCountryCode()); assertEquals(FIRSTNAME, target.getFirstName()); assertEquals(LASTNAME, target.getLastName()); assertEquals(POSTALCODE, target.getPostalCode()); assertEquals(TELEPHONE, target.getTelephoneNumber()); assertNull(target.getState()); }
@Test public void testThatCountryCodeIsNotSetWhenCountryIsNull() { when(sourceMock.getCountry()).thenReturn(null); final Address target = new Address(); testObject.populate(sourceMock, target); assertEquals(LINE_1, target.getAddress1()); assertEquals(LINE_2, target.getAddress2()); assertEquals(TOWN, target.getCity()); assertNull(target.getCountryCode()); assertEquals(FIRSTNAME, target.getFirstName()); assertEquals(LASTNAME, target.getLastName()); assertEquals(POSTALCODE, target.getPostalCode()); assertEquals(TELEPHONE, target.getTelephoneNumber()); assertEquals(STATE, target.getState()); }
@Test public void testEscapeCharacters() { when(sourceMock.getLine1()).thenReturn("áéíóú"); when(sourceMock.getLine2()).thenReturn("üöë"); when(sourceMock.getTown()).thenReturn("ñÑçÇ"); when(sourceMock.getPostalcode()).thenReturn("abcÑñç"); final Address target = new Address(); testObject.populate(sourceMock, target); assertEquals(StringEscapeUtils.escapeXml("áéíóú"), target.getAddress1()); assertEquals(StringEscapeUtils.escapeXml("üöë"), target.getAddress2()); assertEquals(StringEscapeUtils.escapeXml("ñÑçÇ"), target.getCity()); assertEquals(StringEscapeUtils.escapeXml("abcÑñç"), target.getPostalCode()); }
@Test(expected = IllegalArgumentException.class) public void populateThrowsExceptionIfSourceIsNull() { final Address target = new Address(); final AddressModel source = null; testObject.populate(source, target); }