-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Bahmni/BAH-3124
BAH-3124 | added Encounter create/update events
- Loading branch information
Showing
4 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
api/src/main/java/org/bahmni/module/events/api/listener/EncounterAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.bahmni.module.events.api.listener; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.bahmni.module.events.api.model.BahmniEventType; | ||
import org.bahmni.module.events.api.model.Event; | ||
import org.openmrs.Encounter; | ||
import org.openmrs.module.webservices.rest.web.ConversionUtil; | ||
import org.openmrs.module.webservices.rest.web.representation.Representation; | ||
import org.springframework.aop.AfterReturningAdvice; | ||
import org.springframework.aop.MethodBeforeAdvice; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.ApplicationEventPublisherAware; | ||
import org.springframework.lang.NonNull; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.bahmni.module.events.api.model.BahmniEventType.*; | ||
|
||
public class EncounterAdvice implements AfterReturningAdvice, ApplicationEventPublisherAware, MethodBeforeAdvice { | ||
|
||
private final Logger log = LogManager.getLogger(EncounterAdvice.class); | ||
|
||
private ApplicationEventPublisher eventPublisher; | ||
|
||
private final ThreadLocal<Map<String,Integer>> threadLocal = new ThreadLocal<>(); | ||
private final String ENCOUNTER_ID_KEY = "encounterId"; | ||
|
||
|
||
@Override | ||
public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) { | ||
Map<String, Integer> encounterInfo = threadLocal.get(); | ||
// TODO: This is a workaround to avoid publishing duplicate events because currently the event is getting called twice. Need to find out the reason and resolve it. | ||
if (encounterInfo != null) { | ||
BahmniEventType eventType = encounterInfo.get(ENCOUNTER_ID_KEY) == null ? BAHMNI_ENCOUNTER_CREATED : BAHMNI_ENCOUNTER_UPDATED; | ||
threadLocal.remove(); | ||
Encounter encounter = (Encounter) returnValue; | ||
|
||
Object representation = ConversionUtil.convertToRepresentation(encounter, Representation.FULL); | ||
Event event = new Event(eventType, representation, encounter.getUuid()); | ||
eventPublisher.publishEvent(event); | ||
|
||
System.out.println("Successfully published event with uuid : " + encounter.getUuid()); | ||
} | ||
} | ||
|
||
@Override | ||
public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) { | ||
this.eventPublisher = applicationEventPublisher; | ||
} | ||
|
||
@Override | ||
public void before(Method method, Object[] objects, Object o) { | ||
Encounter encounter = (Encounter) objects[0]; | ||
Map<String, Integer> encounterInfo = new HashMap<>(1); | ||
encounterInfo.put(ENCOUNTER_ID_KEY, encounter.getId()); | ||
threadLocal.set(encounterInfo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
api/src/test/java/org/bahmni/module/events/api/listener/EncounterAdviceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.bahmni.module.events.api.listener; | ||
|
||
import org.bahmni.module.events.api.model.BahmniEventType; | ||
import org.bahmni.module.events.api.model.Event; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.openmrs.Encounter; | ||
import org.openmrs.module.webservices.rest.web.ConversionUtil; | ||
import org.openmrs.module.webservices.rest.web.representation.Representation; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PowerMockIgnore; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
@PowerMockIgnore("javax.management.*") | ||
@PrepareForTest({ ConversionUtil.class }) | ||
@RunWith(PowerMockRunner.class) | ||
public class EncounterAdviceTest { | ||
|
||
private final EncounterAdvice encounterAdvice = new EncounterAdvice(); | ||
|
||
private ApplicationEventPublisher applicationEventPublisher; | ||
|
||
@Before | ||
public void setUp() { | ||
applicationEventPublisher = mock(ApplicationEventPublisher.class); | ||
encounterAdvice.setApplicationEventPublisher(applicationEventPublisher); | ||
} | ||
|
||
@Test | ||
public void shouldVerifyBahmniEventPublishIsCalledGivenEncounterIsCreated() { | ||
Encounter newEncounter = getEncounter(); | ||
PowerMockito.mockStatic(ConversionUtil.class); | ||
Object[] args = {newEncounter}; | ||
newEncounter.setId(null); | ||
encounterAdvice.before(null, args, null); | ||
PowerMockito.when(ConversionUtil.convertToRepresentation(getEncounter(), Representation.FULL)).thenReturn(newEncounter); | ||
|
||
encounterAdvice.afterReturning(getEncounter(), null, null, null); | ||
|
||
verify(applicationEventPublisher, times(1)).publishEvent(any(Event.class)); | ||
} | ||
|
||
@Test | ||
public void shouldPublishCreateEventGivenEncounterIsCreated() { | ||
Encounter newEncounter = getEncounter(); | ||
|
||
PowerMockito.mockStatic(ConversionUtil.class); | ||
PowerMockito.when(ConversionUtil.convertToRepresentation(getEncounter(), Representation.FULL)).thenReturn(newEncounter); | ||
|
||
Object[] args = {newEncounter}; | ||
newEncounter.setId(null); | ||
encounterAdvice.before(null, args, null); | ||
encounterAdvice.afterReturning(newEncounter, null, null, null); | ||
|
||
ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); | ||
verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); | ||
|
||
Event event = eventArgumentCaptor.getValue(); | ||
assertEquals(BahmniEventType.BAHMNI_ENCOUNTER_CREATED, event.eventType); | ||
} | ||
|
||
@Test | ||
public void shouldPublishUpdateEventGivenEncounterIsUpdated() { | ||
Encounter newEncounter = getEncounter(); | ||
|
||
PowerMockito.mockStatic(ConversionUtil.class); | ||
PowerMockito.when(ConversionUtil.convertToRepresentation(getEncounter(), Representation.FULL)).thenReturn(newEncounter); | ||
|
||
Object[] args = {newEncounter}; | ||
encounterAdvice.before(null, args, null); | ||
encounterAdvice.afterReturning(newEncounter, null, null, null); | ||
|
||
ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); | ||
verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); | ||
|
||
Event event = eventArgumentCaptor.getValue(); | ||
assertEquals(BahmniEventType.BAHMNI_ENCOUNTER_UPDATED, event.eventType); | ||
} | ||
|
||
@Test | ||
public void shouldVerifyPublishedContentForAEncounter() { | ||
Encounter newEncounter = getEncounter(); | ||
|
||
PowerMockito.mockStatic(ConversionUtil.class); | ||
PowerMockito.when(ConversionUtil.convertToRepresentation(getEncounter(), Representation.FULL)).thenReturn(newEncounter); | ||
|
||
Object[] args = {newEncounter}; | ||
encounterAdvice.before(null, args, null); | ||
encounterAdvice.afterReturning(newEncounter, null, null, null); | ||
|
||
ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); | ||
verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); | ||
|
||
Event event = eventArgumentCaptor.getValue(); | ||
assertEquals(BahmniEventType.BAHMNI_ENCOUNTER_UPDATED, event.eventType); | ||
assertEquals(newEncounter.getUuid(), event.payloadId); | ||
} | ||
|
||
private Encounter getEncounter() { | ||
Encounter encounter = new Encounter(); | ||
encounter.setUuid(UUID.randomUUID().toString()); | ||
encounter.setId(123); | ||
return encounter; | ||
} | ||
} |