InterversionConverter { public <T> T convertToLowerVersion(String targetVersion, Object source) { Class<?> sourceType = source.getClass(); SupportedVersion supportedVersion = sourceType.getAnnotation(SupportedVersion.class); if (supportedVersion == null) { throw new IllegalVersionException(targetVersion); } if (targetVersion.equals(supportedVersion.version())) { return (T)source; } if (supportedVersion.previous() == Object.class) { throw new IllegalVersionException(targetVersion); } return convertToLowerVersion(targetVersion, map(source, supportedVersion.previous(), 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 convertAddressFromV2ToV1() { AddressV1 address = converter.convertToLowerVersion("v1", new AddressV2("Samplestreet 1", " ", new CityV2("12345", "Samplecity"))); assertThat(address.getAddressLine1(), is("Samplestreet 1")); assertThat(address.getAddressLine2(), is(" ")); assertThat(address.getZipCode(), is("12345")); assertThat(address.getCityName(), is("Samplecity")); assertThat(address.getLocation().getZipCode(), is("12345")); assertThat(address.getLocation().getCityName(), is("Samplecity")); }
@Test public void convertAddressFromV3ToV2() { AddressV2 address = converter.convertToLowerVersion("v2", new AddressV3(new CityV3("12345", "Samplecity"), "Samplestreet 1", " ")); assertThat(address.getAddressLines().size(), is(2)); assertThat(address.getAddressLine1(), is("Samplestreet 1")); assertThat(address.getAddressLine2(), is(" ")); assertThat(address.getCity().getZipCode(), is("12345")); assertThat(address.getCity().getCityName(), is("Samplecity")); assertThat(address.getLocation().getZipCode(), is("12345")); assertThat(address.getLocation().getCityName(), is("Samplecity")); }
@Test public void convertAddressFromV3ToV1() { AddressV1 address = converter.convertToLowerVersion("v1", new AddressV3("Samplestreet 1", " ", new CityV3("12345", "Samplecity"))); assertThat(address.getStreet().getName(), is("Samplestreet")); assertThat(address.getStreet().getNumber(), is("1")); assertThat(address.getAddressLine1(), is("Samplestreet 1")); assertThat(address.getAddressLine2(), is(" ")); assertThat(address.getCity(), is("12345 Samplecity")); }
@Test public void convertCustomerFromV2ToV1() { CustomerV1 customer = converter.convertToLowerVersion("v1", new CustomerV2("customer", new Date(0), new AddressV2("Samplestreet 1", " ", new CityV2("12345", "Samplecity")))); assertThat(customer.getName(), is("customer")); assertThat(customer.getDateOfBirth(), is("Thu Jan 01 00:00:00 GMT 1970")); assertThat(customer.getAddresses().size(), is(1)); assertThat(customer.getAddresses(), hasItem(v10())); }
@Test(expected = IllegalVersionException.class) public void convertToLowerVersionThrowsExceptionForUnversionedObject() { converter.convertToLowerVersion("v1", new LocationV1("12345", "Samplecity")); }
@Test(expected = IllegalVersionException.class) public void convertToLowerVersionThrowsExceptionForUnsupportedVersion() { converter.convertToLowerVersion("v0", new AddressV1("Samplestreet 1", " ", new LocationV1("12345", "Samplecity"))); }