@Test
public void testFromDtoWithTwoLevelsOfReferences() throws Exception {
/**
* Create a customer dto with orders and orderlines to be translated to a DO.
*/
CustomerWithOrdersAndOrderLines dto = createCustomerDto(new String[] { "1", "2" },
new String[] { "3", "4" });
/**
* Save it and check.
*/
Long customerId = customerServiceModelService.createCustomer(dto);
CustomerWithOrdersAndOrderLines created = customerServiceModelService
.readCustomerAsCustomerWithOrdersAndOrderLines(customerId);
checkCustomer(created, new String[] { "1", "2" }, new String[] { "3", "4" });
/**
* Delete one orderline from each order and check.
*/
List<OrderWithOrderLinesDto> orderList = new ArrayList<OrderWithOrderLinesDto>(created
.getOrders());
Collections.sort(orderList, new OrderDtoComparator());
orderList.get(0).removeFromOrderLines(0);
orderList.get(1).removeFromOrderLines(0);
customerServiceModelService.updateCustomer(created);
CustomerWithOrdersAndOrderLines updated = customerServiceModelService
.readCustomerAsCustomerWithOrdersAndOrderLines(customerId);
checkCustomer(updated, new String[] { "1", "2" }, new String[] { "4" });
/**
* Add a new orderline to each order and check.
*/
orderList = new ArrayList<OrderWithOrderLinesDto>(updated.getOrders());
orderList.get(0).addToOrderLines(createOrderLine(5, "5"));
orderList.get(1).addToOrderLines(createOrderLine(5, "5"));
customerServiceModelService.updateCustomer(updated);
updated = customerServiceModelService
.readCustomerAsCustomerWithOrdersAndOrderLines(customerId);
checkCustomer(updated, new String[] { "1", "2" }, new String[] { "4", "5" });
/**
* Update the productnumber in the orderlines of both orders from 4 to 6. So leave the one
* with productnumber 5 alone.
*/
orderList = new ArrayList<OrderWithOrderLinesDto>(updated.getOrders());
assertEquals("4", orderList.get(0).getOrderLines().get(0).getProduct().getProductNumber());
orderList.get(0).getOrderLines().get(0).getProduct().setProductNumber("6");
assertEquals("4", orderList.get(1).getOrderLines().get(0).getProduct().getProductNumber());
orderList.get(1).getOrderLines().get(0).getProduct().setProductNumber("6");
customerServiceModelService.updateCustomer(updated);