@Test
@Transactional
public void validExpenseSheetData() {
// check out the expenseSheet without lines
int previousExpenses = expenseSheetDAO.getAll().size();
ExpenseSheet expense = ExpenseSheet.create();
expense.setCode("code-expense-sheet-1");
// check out the order element
String orderLineCode = "order-line-code";
OrderElement orderElement = givenSavedOrderElement(orderLineCode);
// check out the resource
Worker worker = givenSavedValidWorker();
// add line 1 with a resource
LocalDate today = new LocalDate();
ExpenseSheetLine line1 = ExpenseSheetLine.create(BigDecimal.ONE, "concept 1", today,
orderElement);
line1.setResource(worker);
line1.setExpenseSheet(expense);
expense.add(line1);
// add line 2 without resource
LocalDate tomorrow = new LocalDate().plusDays(1);
ExpenseSheetLine line2 = ExpenseSheetLine.create(BigDecimal.ONE, "concept 2", tomorrow,
orderElement);
line2.setExpenseSheet(expense);
expense.add(line2);
try {
expenseSheetDAO.save(expense);
expenseSheetDAO.flush();
} catch (ValidationException e) {
fail("it must not throw a exception");
}
assertThat(expenseSheetDAO.getAll().size(), equalTo(previousExpenses + 1));
// check out the expense sheet
ExpenseSheet expenseSheet = expenseSheetDAO.getAll().get(previousExpenses);
assertNotNull(expenseSheet.getCode());
assertThat(expenseSheet.getCode(), equalTo("code-expense-sheet-1"));
assertThat(expenseSheet.getTotal(), equalTo(new BigDecimal(2)));
assertThat(expenseSheet.getFirstExpense(), equalTo(today));
assertThat(expenseSheet.getLastExpense(), equalTo(tomorrow));
assertThat(expenseSheet.getExpenseSheetLines().size(), equalTo(2));
// check out the expense sheet lines
ExpenseSheetLine savedLine1 = expenseSheet.getExpenseSheetLines().last();
ExpenseSheetLine savedLine2 = expenseSheet.getExpenseSheetLines().first();
assertThat(savedLine1.getConcept(), equalTo("concept 1"));
assertThat(savedLine1.getDate(), equalTo(today));
assertThat(savedLine1.getValue(), equalTo(BigDecimal.ONE));
assertNotNull(savedLine1.getOrderElement());