InterversionConverter { public <T> T convertToHigherVersion(Class<T> targetType, Object source, String sourceVersion) { SupportedVersion supportedVersion = targetType.getAnnotation(SupportedVersion.class); if (supportedVersion == null) { throw new IllegalVersionException(sourceVersion); } if (supportedVersion.version().equals(sourceVersion)) { mapper.map(source); return targetType.cast(source); } if (supportedVersion.previous() == Object.class) { throw new IllegalVersionException(sourceVersion); } Object previousVersion = convertToHigherVersion(supportedVersion.previous(), source, sourceVersion); return map(previousVersion, targetType, new DefaultVersionContext()); } InterversionConverter(VersionTypeFactory factory, CompatibilityMapper mapper); T convertToLowerVersion(String targetVersion, Object source); T convertToHigherVersion(Class<T> targetType, Object source, String sourceVersion);  }
@Test public void convertAddressFromV1ToV2() { AddressV2 address = converter.convertToHigherVersion(AddressV2.class, new AddressV1("Samplestreet 1", " ", new LocationV1("12345", "Samplecity")), "v1"); assertThat(address.getAddressLines().get(0), is("Samplestreet 1")); assertThat(address.getAddressLines().get(1), is(" ")); assertThat(address.getLocation().getZipCode(), is("12345")); assertThat(address.getLocation().getCityName(), is("Samplecity")); }
@Test public void convertAddressFromV2ToV3() { AddressV3 address = converter.convertToHigherVersion(AddressV3.class, new AddressV2("Samplestreet 1", " ", new CityV2("12345", "Samplecity")), "v2"); assertThat(address.getAddressLine1(), is("Samplestreet 1")); assertThat(address.getAddressLine2(), is(" ")); assertThat(address.getAddressLines().size(), is(2)); assertThat(address.getAddressLines(), hasItem("Samplestreet 1")); assertThat(address.getAddressLines(), hasItem(" ")); assertThat(address.getCity().getZipCode(), is("12345")); assertThat(address.getCity().getCityName(), is("Samplecity")); }
@Test public void convertAddressFromV1ToV3() { AddressV3 address = converter.convertToHigherVersion(AddressV3.class, new AddressV1(new StreetV1("Samplestreet", "1"), "12345 Samplecity"), "v1"); assertThat(address.getAddressLine1(), is("Samplestreet 1")); assertThat(address.getAddressLine2(), is(" ")); assertThat(address.getAddressLines().size(), is(2)); assertThat(address.getAddressLines(), hasItem("Samplestreet 1")); assertThat(address.getAddressLines(), hasItem(" ")); assertThat(address.getCity().getZipCode(), is("12345")); assertThat(address.getCity().getCityName(), is("Samplecity")); }
@Test public void convertCustomerFromV1ToV2() { CustomerV2 customer = converter.convertToHigherVersion(CustomerV2.class, new CustomerV1() {{ name = "customer"; dateOfBirth = "01/01/1970"; addresses = Arrays.asList(new AddressV1(new StreetV1("Samplestreet", "1"), "12345 Samplecity")); }}, "v1"); assertThat(customer.getName(), is("customer")); assertThat(customer.getDateOfBirth(), is(new Date(0))); assertThat(customer.getAddresses().size(), is(1)); assertThat(customer.getAddresses(), hasItem(v2())); }
@Test public void convertWithNullList() { CustomerV2 customer = converter.convertToHigherVersion(CustomerV2.class, new CustomerV1() {{ name = "customer"; dateOfBirth = "01/01/1970"; addresses = null; }}, "v1"); assertThat(customer.getName(), is("customer")); assertThat(customer.getDateOfBirth(), is(new Date(0))); assertThat(customer.getAddresses(), is(nullValue())); }
@Test(expected = IllegalVersionException.class) public void convertToHigherVersionThrowsExceptionForUnversionedObject() { converter.convertToHigherVersion(LocationV1.class, new LocationV1("12345", "Samplecity"), "v1"); }
@Test(expected = IllegalVersionException.class) public void convertToHigherVersionThrowsExceptionForUnsupportedVersion() { converter.convertToHigherVersion(AddressV1.class, new AddressV1("Samplestreet 1", " ", new LocationV1("12345", "Samplecity")), "v0"); }