@Test public void firesPatientDeletingEventsWhenPatientDocumentsAreDeleted() throws Exception { XWikiDocument doc = mock(XWikiDocument.class); XWikiDocument odoc = mock(XWikiDocument.class); when(doc.getDocumentReference()).thenReturn(this.ref); DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class); when(dab.getDocument(this.ref)).thenReturn(odoc); when(odoc.getXObject(Patient.CLASS_REFERENCE)).thenReturn(mock(BaseObject.class)); PatientRepository repo = this.mocker.getInstance(PatientRepository.class); Patient p = mock(Patient.class); when(repo.load(odoc)).thenReturn(p); UserManager um = this.mocker.getInstance(UserManager.class); User u = mock(User.class); when(um.getCurrentUser()).thenReturn(u); ObservationManager om = this.mocker.getInstance(ObservationManager.class); CapturingMatcher<PatientEvent> evtCapture = new CapturingMatcher<>(); CapturingMatcher<Object> dataCapture = new CapturingMatcher<>(); Mockito.doNothing().when(om).notify(Matchers.argThat(evtCapture), Matchers.argThat(dataCapture)); this.mocker.getComponentUnderTest().onEvent(new DocumentDeletingEvent(), doc, null); Assert.assertEquals(1, evtCapture.getAllValues().size()); PatientEvent evt = evtCapture.getLastValue(); Assert.assertNotNull(evt); Assert.assertTrue(evt instanceof PatientDeletingEvent); Assert.assertSame(u, evt.getAuthor()); Assert.assertSame(p, evt.getPatient()); Assert.assertSame(odoc, dataCapture.getLastValue()); }
@Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc == null || "PatientTemplate".equals(doc.getDocumentReference().getName())) { return; } XWikiDocument odoc; try { odoc = (XWikiDocument) this.dab.getDocument(doc.getDocumentReference()); } catch (Exception e) { return; } BaseObject patientRecordObj = odoc.getXObject(Patient.CLASS_REFERENCE); if (patientRecordObj == null) { return; } Patient patient = this.repo.load(odoc); User user = this.userManager.getCurrentUser(); CancelableEvent patientEvent = new PatientDeletingEvent(patient, user); this.observationManager.notify(patientEvent, odoc); if (patientEvent.isCanceled()) { } }
PatientDeletingEventSource implements EventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc == null || "PatientTemplate".equals(doc.getDocumentReference().getName())) { return; } XWikiDocument odoc; try { odoc = (XWikiDocument) this.dab.getDocument(doc.getDocumentReference()); } catch (Exception e) { return; } BaseObject patientRecordObj = odoc.getXObject(Patient.CLASS_REFERENCE); if (patientRecordObj == null) { return; } Patient patient = this.repo.load(odoc); User user = this.userManager.getCurrentUser(); CancelableEvent patientEvent = new PatientDeletingEvent(patient, user); this.observationManager.notify(patientEvent, odoc); if (patientEvent.isCanceled()) { } } }
PatientDeletingEventSource implements EventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc == null || "PatientTemplate".equals(doc.getDocumentReference().getName())) { return; } XWikiDocument odoc; try { odoc = (XWikiDocument) this.dab.getDocument(doc.getDocumentReference()); } catch (Exception e) { return; } BaseObject patientRecordObj = odoc.getXObject(Patient.CLASS_REFERENCE); if (patientRecordObj == null) { return; } Patient patient = this.repo.load(odoc); User user = this.userManager.getCurrentUser(); CancelableEvent patientEvent = new PatientDeletingEvent(patient, user); this.observationManager.notify(patientEvent, odoc); if (patientEvent.isCanceled()) { } }  }
PatientDeletingEventSource implements EventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc == null || "PatientTemplate".equals(doc.getDocumentReference().getName())) { return; } XWikiDocument odoc; try { odoc = (XWikiDocument) this.dab.getDocument(doc.getDocumentReference()); } catch (Exception e) { return; } BaseObject patientRecordObj = odoc.getXObject(Patient.CLASS_REFERENCE); if (patientRecordObj == null) { return; } Patient patient = this.repo.load(odoc); User user = this.userManager.getCurrentUser(); CancelableEvent patientEvent = new PatientDeletingEvent(patient, user); this.observationManager.notify(patientEvent, odoc); if (patientEvent.isCanceled()) { } }  @Override String getName(); @Override List<Event> getEvents(); @Override void onEvent(Event event, Object source, Object data); }
PatientDeletingEventSource implements EventListener { @Override public void onEvent(Event event, Object source, Object data) { XWikiDocument doc = (XWikiDocument) source; if (doc == null || "PatientTemplate".equals(doc.getDocumentReference().getName())) { return; } XWikiDocument odoc; try { odoc = (XWikiDocument) this.dab.getDocument(doc.getDocumentReference()); } catch (Exception e) { return; } BaseObject patientRecordObj = odoc.getXObject(Patient.CLASS_REFERENCE); if (patientRecordObj == null) { return; } Patient patient = this.repo.load(odoc); User user = this.userManager.getCurrentUser(); CancelableEvent patientEvent = new PatientDeletingEvent(patient, user); this.observationManager.notify(patientEvent, odoc); if (patientEvent.isCanceled()) { } }  @Override String getName(); @Override List<Event> getEvents(); @Override void onEvent(Event event, Object source, Object data);  }