AddressServiceImpl extends BaseGenericServiceImpl<Address> implements AddressService { @Override public List<Address> getAddressesByCustomerId(final long customerId) { return getGenericDao().findByNamedQuery("ADDRESSES.BY.CUSTOMER", customerId); } AddressServiceImpl(final GenericDAO<Address, Long> genericDao,
                              final OrderAddressFormatter orderAddressFormatter); @Override List<Address> getAddressesByCustomerId(final long customerId); @Override List<Address> getAddressesByCustomerId(final long customerId, final String addressType); @Override @CacheEvict(value = { "customerService-customerByEmail" }, allEntries = true) Address create(final Address instance); @Override @CacheEvict(value = { "customerService-customerByEmail" }, allEntries = true) Address updateSetDefault(final Address instance); @Override @CacheEvict(value = { "customerService-customerByEmail" }, allEntries = true) Address update(final Address instance); @Override @CacheEvict(value = { "customerService-customerByEmail" }, allEntries = true) void delete(final Address instance); @Override String formatAddressFor(final Address address, final Shop shop, final Customer customer, final String lang);  }
@Test public void testGetAddressesByCustomerId() { Customer customer = customerService.getGenericDao().getEntityFactory().getByIface(Customer.class); customer.setEmail("bender@domain.com"); customer.setFirstname("Bender"); customer.setLastname("Rodriguez"); customer.setPassword("rawpassword"); customer = customerService.create(customer, shopService.getById(10L)); assertTrue(customer.getCustomerId() > 0); Address address = addressService.getGenericDao().getEntityFactory().getByIface(Address.class); address.setFirstname("Bender"); address.setLastname("Rodriguez"); address.setCity("LA"); address.setAddrline1("line1"); address.setCountryCode("US"); address.setAddressType(Address.ADDR_TYPE_BILLING); address.setCustomer(customer); addressService.create(address); address = addressService.getGenericDao().getEntityFactory().getByIface(Address.class); address.setFirstname("Bender"); address.setLastname("Rodriguez"); address.setCity("New-Vasyki"); address.setAddrline1("line0"); address.setCountryCode("ZH"); address.setAddressType(Address.ADDR_TYPE_SHIPPING); address.setCustomer(customer); addressService.create(address); customer = customerService.findById(customer.getCustomerId()); assertEquals(2, customer.getAddress().size()); final List<Address> addresses = addressService.getAddressesByCustomerId(customer.getCustomerId()); assertEquals(2, addresses.size()); assertTrue(Arrays.asList("US", "ZH").contains(addresses.get(0).getCountryCode())); final List<Address> shipping = addressService.getAddressesByCustomerId(customer.getCustomerId(), Address.ADDR_TYPE_SHIPPING); assertEquals(1, shipping.size()); assertEquals("ZH", shipping.get(0).getCountryCode()); final List<Address> billing = addressService.getAddressesByCustomerId(customer.getCustomerId(), Address.ADDR_TYPE_BILLING); assertEquals(1, billing.size()); assertEquals("US", billing.get(0).getCountryCode()); }