Created
June 27, 2017 07:51
-
-
Save chhsiao90/8fb758f2044e87426d1faa99d17523e9 to your computer and use it in GitHub Desktop.
ModelMapper - SimpleNestedListMapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.modelmapper; | |
import org.testng.annotations.BeforeMethod; | |
import org.testng.annotations.Test; | |
import java.util.Arrays; | |
import java.util.List; | |
import static org.testng.Assert.assertEquals; | |
@Test | |
public class SimpleNestedListMapping { | |
static class PoHead { | |
private String docNo; | |
private List<PoItem> items; | |
public String getDocNo() { | |
return docNo; | |
} | |
public void setDocNo(String docNo) { | |
this.docNo = docNo; | |
} | |
public List<PoItem> getItems() { | |
return items; | |
} | |
public void setItems(List<PoItem> items) { | |
this.items = items; | |
} | |
} | |
static class PoItem { | |
private String itemNo; | |
private String brand; | |
private String vendor; | |
public PoItem() { | |
} | |
public PoItem(String itemNo, String brand, String vendor) { | |
this.itemNo = itemNo; | |
this.brand = brand; | |
this.vendor = vendor; | |
} | |
public String getItemNo() { | |
return itemNo; | |
} | |
public void setItemNo(String itemNo) { | |
this.itemNo = itemNo; | |
} | |
public String getBrand() { | |
return brand; | |
} | |
public void setBrand(String brand) { | |
this.brand = brand; | |
} | |
public String getVendor() { | |
return vendor; | |
} | |
public void setVendor(String vendor) { | |
this.vendor = vendor; | |
} | |
} | |
static class TiPoHead { | |
private String docNo; | |
private List<TiPoItem> items; | |
public String getDocNo() { | |
return docNo; | |
} | |
public void setDocNo(String docNo) { | |
this.docNo = docNo; | |
} | |
public List<TiPoItem> getItems() { | |
return items; | |
} | |
public void setItems(List<TiPoItem> items) { | |
this.items = items; | |
} | |
} | |
static class TiPoItem { | |
private String itemNo; | |
private String brand; | |
private String vendor; | |
public String getItemNo() { | |
return itemNo; | |
} | |
public void setItemNo(String itemNo) { | |
this.itemNo = itemNo; | |
} | |
public String getBrand() { | |
return brand; | |
} | |
public void setBrand(String brand) { | |
this.brand = brand; | |
} | |
public String getVendor() { | |
return vendor; | |
} | |
public void setVendor(String vendor) { | |
this.vendor = vendor; | |
} | |
} | |
private ModelMapper modelMapper; | |
@BeforeMethod | |
public void setUp() { | |
modelMapper = new ModelMapper(); | |
} | |
public void shouldMap() throws Exception { | |
PoHead poHead = new PoHead(); | |
poHead.setDocNo("docNo"); | |
poHead.setItems(Arrays.asList( | |
new PoItem("0001", "b1", "v1"), | |
new PoItem("0002", "b2", "v2"), | |
new PoItem("0003", "b3", "v3"))); | |
TiPoHead tiPoHead = modelMapper.map(poHead, TiPoHead.class); | |
assertEquals(tiPoHead.getDocNo(), "docNo"); | |
assertEquals(3, tiPoHead.getItems().size()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment