@Transactional
public void testRemoveExpenseSheetLine() {
int previousExpenseLines = expenseSheetLineDAO.findAll().size();
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.setCode("code-line-2");
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));
try {
ExpenseSheet expenseSheet = expenseSheetDAO.findByCode("code-expense-sheet-1");
assertNotNull(expenseSheet);
assertThat(expenseSheet.getExpenseSheetLines().size(), equalTo(2));
assertThat(expenseSheetLineDAO.findAll().size(), equalTo(previousExpenseLines + 2));
expenseSheet.remove(line2);
expenseSheetDAO.save(expenseSheet);
expenseSheetDAO.flush();
assertThat(expenseSheetDAO.getAll().size(), equalTo(previousExpenses + 1));
assertThat(expenseSheetLineDAO.findAll().size(), equalTo(previousExpenseLines + 1));