Skip to content

Instantly share code, notes, and snippets.

@gimbimloki
Created February 8, 2018 01:25
Show Gist options
  • Save gimbimloki/bea0bbeae22c2b5ac24b8f073542c865 to your computer and use it in GitHub Desktop.
Save gimbimloki/bea0bbeae22c2b5ac24b8f073542c865 to your computer and use it in GitHub Desktop.
public class ModelMappersTest {
private static ModelMapper modelMapper;
@BeforeClass
public static void beforeClass() {
modelMapper = new ModelMapper();
modelMapper.addConverter(new AbstractConverter<A, B>() {
@Override
protected B convert(A a) {
return B
.builder()
.num(a.getNum())
.buttons(mapCollection(modelMapper, a.getButtonsJson(), Button.class))
.build();
}
});
}
/**
* Util Method
*/
private static <S, D> Collection<D> mapCollection(ModelMapper modelMapper, Collection<S> sources, Class<D> destination) {
if (CollectionUtils.isEmpty(sources) == true) {
return Collections.emptyList();
}
final Collection<D> destinations = new LinkedList<>();
for (S source : sources) {
destinations.add(modelMapper.map(source, destination));
}
return destinations;
}
/**
* Util Method
*/
private static <D> Collection<D> mapCollection(ModelMapper modelMapper, String sources, Class<D> destinations) {
try {
return Jsons.convertJsonToList(sources, destinations);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public static class S1 {
private int num;
private String name;
private String option;
}
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public static class S2 {
private int num;
private String name;
}
@Test
public void test_modelmapper_simple() {
// Given
final S1 s1 = new S1(1, "name", "option");
// When
final S2 s2 = modelMapper.map(s1, S2.class);
// Then
Assert.assertEquals(s1.getNum(), s2.getNum());
Assert.assertEquals(s1.getName(), s2.getName());
}
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public static class A {
private int num = 1;
private String buttonsJson = "[{\"name\":\"A\"}]";
}
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public static class Button {
private String name;
}
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public static class B {
private int num = 1;
private Collection<Button> buttons;
}
@Test
public void test_modelmapper() {
// Given
final A a = new A(10, "[{\"name\":\"A\"}]");
// When
final B b = modelMapper.map(a, B.class);
// Then
Assert.assertEquals(10, b.getNum());
Assert.assertEquals("A", b.getButtons().iterator().next().getName());
}
@Test
public void test_modelmapper_collection() {
// Given
final Collection<A> as = Arrays.asList(new A(1, "[{\"name\":\"A\"}]"),
new A(2, "[{\"name\":\"B\"}]"));
// When
final Collection<B> bs = mapCollection(modelMapper, as, B.class);
// Then
final Iterator<B> bIterator = bs.iterator();
final B firstB = bIterator.next();
Assert.assertEquals(1, firstB.getNum());
Assert.assertEquals("A", firstB.getButtons().iterator().next().getName());
final B secondB = bIterator.next();
Assert.assertEquals(2, secondB.getNum());
Assert.assertEquals("B", secondB.getButtons().iterator().next().getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment