NetworkAddressModelDataMapper { public NetworkAddressModel transform(NetworkAddress networkAddress) { if (networkAddress == null) { throw new IllegalArgumentException("Cannot transform a null NetworkAddress object."); } return NetworkAddressModel.builder() .ip(networkAddress.ip()) .port(networkAddress.port()) .build(); } @Inject  NetworkAddressModelDataMapper(); NetworkAddressModel transform(NetworkAddress networkAddress); Collection<NetworkAddressModel> transform(Collection<NetworkAddress> networkAddresses); NetworkAddress transformInverse(NetworkAddressModel networkAddressModel); Collection<NetworkAddress> transformInverse(
      Collection<NetworkAddressModel> networkAddressModels);  }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionGivenNullNetworkAddress() throws Exception { NetworkAddress networkAddress = null; networkAddressModelDataMapper.transform(networkAddress); }
@Test public void shouldProperlyMapNetworkAddressIntoNetworkAddressModel() throws Exception { NetworkAddress networkAddress = NetworkAddress.builder() .ip("192.168.1.45") .port(9999) .build(); NetworkAddressModel networkAddressModel = networkAddressModelDataMapper.transform(networkAddress); assertThat(networkAddressModel, notNullValue()); assertThat(networkAddressModel, instanceOf(NetworkAddressModel.class)); assertThat(networkAddressModel.ip(), notNullValue()); assertThat(networkAddressModel.ip(), is("192.168.1.45")); assertThat(networkAddressModel.port(), is(9999)); }
@Test public void shouldProperlyMapNetworkAddressCollectionIntoNetworkAddressModelCollection() throws Exception { NetworkAddress networkAddress1 = NetworkAddress.builder() .ip("192.168.1.45") .port(9999) .build(); NetworkAddress networkAddress2 = NetworkAddress.builder() .ip("192.168.1.80") .port(1111) .build(); ArrayList<NetworkAddress> networkAddresses = new ArrayList<>(); networkAddresses.add(networkAddress1); networkAddresses.add(networkAddress2); Collection<NetworkAddressModel> networkAddressModels = networkAddressModelDataMapper.transform(networkAddresses); assertThat(networkAddressModels, notNullValue()); assertThat(networkAddressModels.size(), is(2)); NetworkAddressModel networkAddressModel1 = (NetworkAddressModel) networkAddressModels.toArray()[0]; assertThat(networkAddressModel1, notNullValue()); assertThat(networkAddressModel1, instanceOf(NetworkAddressModel.class)); assertThat(networkAddressModel1.ip(), notNullValue()); assertThat(networkAddressModel1.ip(), is("192.168.1.45")); assertThat(networkAddressModel1.port(), is(9999)); NetworkAddressModel networkAddressModel2 = (NetworkAddressModel) networkAddressModels.toArray()[1]; assertThat(networkAddressModel2, notNullValue()); assertThat(networkAddressModel2, instanceOf(NetworkAddressModel.class)); assertThat(networkAddressModel2.ip(), notNullValue()); assertThat(networkAddressModel2.ip(), is("192.168.1.80")); assertThat(networkAddressModel2.port(), is(1111)); }