when(getDaoFacade().getEntryDAO().read(TEST_ENTRY_ID)).thenReturn(testEntry);
when(getDaoFacade().getBranchDAO().read(TEST_SUBBRANCH1_ID)).thenReturn(testSubBranch1);
when(getDaoFacade().getBranchDAO().read(TEST_SUBBRANCH2_ID)).thenReturn(testSubBranch2);
when(getDaoFacade().getBranchDAO().read(TEST_SUBBRANCH11_ID)).thenReturn(testSubBranch11);
final BranchDAO mockBranchDAO = getDaoFacade().getBranchDAO();
when(mockBranchDAO.readAllByParent(Constant.ROOT_ID)).thenAnswer(new Answer<List<Branch>>() {
@Override
public List<Branch> answer(final InvocationOnMock invocation) throws Throwable
{
final List<Branch> retval = new ArrayList<Branch>();
if (Long.valueOf(Constant.ROOT_ID).equals(invocation.getArguments()[0])) {
retval.add(testSubBranch1);
retval.add(testSubBranch2);
} else if (Long.valueOf(TEST_SUBBRANCH1_ID).equals(invocation.getArguments()[0])) {
retval.add(testSubBranch11);
}
return retval;
}
});
doAnswer(new Answer<Object>() {
/** Simulate an exception if rename conflicted with a sibling. */
public Object answer(final InvocationOnMock invocation) throws Throwable
{
final Branch param = (Branch) invocation.getArguments()[0];
final Branch parent = param.getParent();
if (parent != null) {//not Root.
final List<Branch> siblings = mockBranchDAO.readAllByParent(parent.getBranchId());
for (final Branch branch : siblings) {
if (branch.getBranchId() != param.getBranchId() && branch.getName().equals(param.getName())) {
throw new JournalException(JournalTestCommon.TEST_JRNL_ERRORMSG);
}
}