Skip to content

Instantly share code, notes, and snippets.

@adammichalik
Last active August 29, 2015 14:21
Show Gist options
  • Save adammichalik/76104c6360ee446606f1 to your computer and use it in GitHub Desktop.
Save adammichalik/76104c6360ee446606f1 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class OrikaIntermediateObjectWithListTest {
private final DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
private final Source source = new Source();
@Test
public void createIntermediateObjectForSingleProperty() {
mapperFactory.registerClassMap(mapperFactory.classMap(Source.class, Parent.class)
.fieldAToB("single", "child.nestedSingle")
);
source.setSingle("SINGLE");
Parent target = mapperFactory.getMapperFacade(Source.class, Parent.class).map(source);
assertThat(target.getChild().getNestedSingle()).isEqualTo("SINGLE");
}
@Test
// Fails for Orika 1.4.5
public void createIntermediateObjectForListProperty() {
mapperFactory.registerClassMap(mapperFactory.classMap(Source.class, Parent.class)
.fieldAToB("list{}", "child.nestedList{}")
);
source.setList(Arrays.asList("A", "B"));
Parent target = mapperFactory.getMapperFacade(Source.class, Parent.class).map(source);
assertThat(target.getChild().getNestedList()).containsExactly("A", "B");
}
@Test
public void createIntermediateObjectForBothPropertiesWithNonNullSingle() {
mapperFactory.registerClassMap(mapperFactory.classMap(Source.class, Parent.class)
.fieldAToB("single", "child.nestedSingle")
.fieldAToB("list{}", "child.nestedList{}")
);
source.setSingle("SINGLE");
source.setList(Arrays.asList("A", "B"));
Parent target = mapperFactory.getMapperFacade(Source.class, Parent.class).map(source);
assertThat(target.getChild().getNestedSingle()).isEqualTo("SINGLE");
assertThat(target.getChild().getNestedList()).containsExactly("A", "B");
}
@Test
// Fails for Orika 1.4.5
public void createIntermediateObjectForBothPropertiesWithNullSingle() {
mapperFactory.registerClassMap(mapperFactory.classMap(Source.class, Parent.class)
.fieldAToB("list{}", "child.nestedList{}")
.fieldAToB("single", "child.nestedSingle")
);
source.setSingle(null);
source.setList(Arrays.asList("A", "B"));
Parent target = mapperFactory.getMapperFacade(Source.class, Parent.class).map(source);
assertThat(target.getChild().getNestedSingle()).isNull();
assertThat(target.getChild().getNestedList()).containsExactly("A", "B");
}
public static class Source {
private String single;
private List<String> list;
public String getSingle() {
return single;
}
public void setSingle(String single) {
this.single = single;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
public static class Parent {
private Child child;
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
}
public static class Child {
private String nestedSingle;
private List<String> nestedList = new ArrayList<>();
public String getNestedSingle() {
return nestedSingle;
}
public void setNestedSingle(String nestedSingle) {
this.nestedSingle = nestedSingle;
}
public List<String> getNestedList() {
return nestedList;
}
public void setNestedList(List<String> nestedList) {
this.nestedList = nestedList;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment