NetworkAddressEntityDataMapper { public NetworkAddress transform(NetworkAddressEntity networkAddressEntity) { if (networkAddressEntity == null) { return null; } return NetworkAddress.builder() .ip(networkAddressEntity.ip()) .port(networkAddressEntity.port()) .build(); } @Inject  NetworkAddressEntityDataMapper(); NetworkAddress transform(NetworkAddressEntity networkAddressEntity); Collection<NetworkAddress> transform(
      Collection<NetworkAddressEntity> networkAddressEntities); NetworkAddressEntity transformInverse(NetworkAddress networkAddress); Collection<NetworkAddressEntity> transformInverse(
      Collection<NetworkAddress> networkAddresses);  }
@Test public void shouldReturnNullNetworkAddressGivenNullNetworkAddressEntity() throws Exception { NetworkAddressEntity networkAddressEntity = null; NetworkAddress networkAddress = networkAddressEntityDataMapper.transform(networkAddressEntity); assertThat(networkAddress, nullValue()); }
@Test public void shouldProperlyMapNetworkAddressEntityIntoNetworkAddress() throws Exception { NetworkAddressEntity networkAddressEntity = NetworkAddressEntity.builder() .ip("192.168.1.1") .port(8889) .build(); NetworkAddress networkAddress = networkAddressEntityDataMapper.transform(networkAddressEntity); assertThat(networkAddress, notNullValue()); assertThat(networkAddress, instanceOf(NetworkAddress.class)); assertThat(networkAddress.ip(), notNullValue()); assertThat(networkAddress.ip(), is("192.168.1.1")); assertThat(networkAddress.port(), is(8889)); }
@Test public void shouldProperlyMapNetworkAddressEntityCollectionIntoNetworkAddressCollection() throws Exception { NetworkAddressEntity networkAddressEntity1 = NetworkAddressEntity.builder() .ip("192.168.1.1") .port(8889) .build(); NetworkAddressEntity networkAddressEntity2 = NetworkAddressEntity.builder() .ip("192.168.1.2") .port(8890) .build(); ArrayList<NetworkAddressEntity> networkAddressEntities = new ArrayList<>(); networkAddressEntities.add(networkAddressEntity1); networkAddressEntities.add(networkAddressEntity2); Collection<NetworkAddress> networkAddresses = networkAddressEntityDataMapper.transform(networkAddressEntities); assertThat(networkAddresses, notNullValue()); assertThat(networkAddressEntities.size(), is(2)); NetworkAddress networkAddress1 = (NetworkAddress) networkAddresses.toArray()[0]; assertThat(networkAddress1, notNullValue()); assertThat(networkAddress1.ip(), is("192.168.1.1")); assertThat(networkAddress1.port(), is(8889)); NetworkAddress networkAddress2 = (NetworkAddress) networkAddresses.toArray()[1]; assertThat(networkAddress2, notNullValue()); assertThat(networkAddress2.ip(), is("192.168.1.2")); assertThat(networkAddress2.port(), is(8890)); }