CrmWebserviceEndpoint { @WebMethod public ContactDto addHistoryToContact(@WebParam(name = "contactDto") ContactDto contactDto, @WebParam(name = "historyDto") HistoryDto historyDto, @WebParam(name = "returnHistory") boolean returnHistory) { Contact contact = contactDao.find(contactDto.getId(), new HashMap<String, Object>()); checkRevision(contactDto, contact); History history = new History(); obtainHistoryMapper().mapHistoryOnlyInbound(historyDto, history); contact.addHistory(history); incrementRevision(contact); contact = contactDao.merge(contact, new HashMap<String, Object>()); contactDto = new ContactDto(); if (returnHistory) { obtainContactMapper().mapContactInclHistory(contact, contactDto); } else { obtainContactMapper().mapContactWithoutHistory(contact, contactDto); } return contactDto; } CrmWebserviceEndpoint();  CrmWebserviceEndpoint(ContactDao contactDao); @Inject @WebMethod(exclude = true) void setContactDao(ContactDaoEjb contactDao); @WebMethod List<ContactDto> getContracts(@WebParam(name = "from") int from, @WebParam(name = "limit") int limit); @WebMethod ContactDto getContactIncludingHistory(@WebParam(name = "id") long id); @WebMethod ContactDto addContact(@WebParam(name = "contactDto") ContactDto contactDto); @WebMethod ContactDto updateContact(@WebParam(name = "contactDto") ContactDto contactDto); @WebMethod ContactDto addHistoryToContact(@WebParam(name = "contactDto") ContactDto contactDto, @WebParam(name = "historyDto") HistoryDto historyDto,
			@WebParam(name = "returnHistory") boolean returnHistory);  }
@SuppressWarnings("unchecked") @Test public void testAddHistoryToContact() { when(contactDao.find(any(), anyMap())).thenReturn(contactRef); when(contactDao.merge(any(Contact.class), anyMap())).thenAnswer(new Answer<Contact>() { @Override public Contact answer(InvocationOnMock invocation) throws Throwable { Contact contactRet = (Contact) invocation.getArguments()[0]; Method idSetter = contactRet.getClass().getDeclaredMethod("setId", Long.TYPE); idSetter.setAccessible(true); idSetter.invoke(contactRet, 11); return contactRet; } }); contactDtoRef.setRevision(contactRef.getRev()); ContactDto contactDtoNew = webservice.addHistoryToContact(contactDtoRef, contactDtoRef.getHistory().get(0), false); verify(contactDao).find(eq(new Long(11)), anyMap()); verify(contactDao).merge(argThat(new BaseMatcher<Contact>() { @Override public boolean matches(Object arg0) { return arg0 != null && (arg0 instanceof Contact) && ((Contact) arg0).getId() == 11 && ((Contact) arg0).getRev() == 3 && "Hans Hummel".equals(((Contact) arg0).getName()) && ((Contact) arg0).getHistory().size() == 2 && "mors, mors".equals(((Contact) arg0).getHistory().get(0).getText()) && "grrr".equals(((Contact) arg0).getHistory().get(1).getText()) && dateRef.equals(((Contact) arg0).getHistory().get(1).getTimestamp()); } @Override public void describeTo(Description arg0) { arg0.appendText("ID=11, Rev=3, Name=Hans Hummel, old hist + new hist"); } }), anyMap()); assertEquals(11, contactDtoNew.getId()); assertEquals(3, contactDtoNew.getRevision()); assertEquals("Hans Hummel", contactDtoNew.getName()); assertEquals(0, contactDtoNew.getHistory().size()); }
@SuppressWarnings("unchecked") @Test public void testAddHistoryToContactWithHist() { when(contactDao.find(any(), anyMap())).thenReturn(contactRef); when(contactDao.merge(any(Contact.class), anyMap())).thenAnswer(new Answer<Contact>() { @Override public Contact answer(InvocationOnMock invocation) throws Throwable { Contact contactRet = (Contact) invocation.getArguments()[0]; Method idSetter = contactRet.getClass().getDeclaredMethod("setId", Long.TYPE); idSetter.setAccessible(true); idSetter.invoke(contactRet, 11); return contactRet; } }); contactDtoRef.setRevision(contactRef.getRev()); ContactDto contactDtoNew = webservice.addHistoryToContact(contactDtoRef, contactDtoRef.getHistory().get(0), true); verify(contactDao).find(eq(new Long(11)), anyMap()); verify(contactDao).merge(argThat(new BaseMatcher<Contact>() { @Override public boolean matches(Object arg0) { return arg0 != null && (arg0 instanceof Contact) && ((Contact) arg0).getId() == 11 && ((Contact) arg0).getRev() == 3 && "Hans Hummel".equals(((Contact) arg0).getName()) && ((Contact) arg0).getHistory().size() == 2 && "mors, mors".equals(((Contact) arg0).getHistory().get(0).getText()) && "grrr".equals(((Contact) arg0).getHistory().get(1).getText()) && dateRef.equals(((Contact) arg0).getHistory().get(1).getTimestamp()); } @Override public void describeTo(Description arg0) { arg0.appendText("ID=11, Rev=3, Name=Hans Hummel, old hist + new hist"); } }), anyMap()); assertEquals(11, contactDtoNew.getId()); assertEquals(3, contactDtoNew.getRevision()); assertEquals("Hans Hummel", contactDtoNew.getName()); assertEquals(2, contactDtoNew.getHistory().size()); assertEquals("mors, mors", contactDtoNew.getHistory().get(0).getText()); assertEquals(dateRef, contactDtoNew.getHistory().get(0).getTimestamp()); assertEquals("grrr", contactDtoNew.getHistory().get(1).getText()); assertEquals(dateRef, contactDtoNew.getHistory().get(1).getTimestamp()); }
@SuppressWarnings("unchecked") @Test public void testAddHistoryToContactInvalidRev() { when(contactDao.find(any(), anyMap())).thenReturn(contactRef); contactDtoRef.setRevision(contactRef.getRev() + 1); Exception exc = null; try { webservice.addHistoryToContact(contactDtoRef, contactDtoRef.getHistory().get(0), false); } catch (Exception e) { exc = e; } verify(contactDao).find(eq(new Long(11)), anyMap()); verify(contactDao, never()).merge(any(Contact.class), anyMap()); assertNotNull(exc); assertTrue(exc.toString().contains("Try to work with outdated revision")); }