diff --git a/api/src/main/java/org/bahmni/module/events/api/configuration/BahmniEventPublisherConfiguration.java b/api/src/main/java/org/bahmni/module/events/api/configuration/BahmniEventPublisherConfiguration.java new file mode 100644 index 0000000..792e481 --- /dev/null +++ b/api/src/main/java/org/bahmni/module/events/api/configuration/BahmniEventPublisherConfiguration.java @@ -0,0 +1,14 @@ +package org.bahmni.module.events.api.configuration; + +import org.bahmni.module.events.api.publisher.BahmniEventPublisher; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class BahmniEventPublisherConfiguration { + + @Bean + public BahmniEventPublisher bahmniEventPublisher() { + return new BahmniEventPublisher(); + } +} \ No newline at end of file diff --git a/api/src/main/java/org/bahmni/module/events/api/configuration/EventConfiguration.java b/api/src/main/java/org/bahmni/module/events/api/configuration/EventConfiguration.java deleted file mode 100644 index 8fe357b..0000000 --- a/api/src/main/java/org/bahmni/module/events/api/configuration/EventConfiguration.java +++ /dev/null @@ -1,114 +0,0 @@ -package org.bahmni.module.events.api.configuration; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.bahmni.module.events.api.listener.AppointmentAdvice; -import org.bahmni.module.events.api.listener.EncounterAdvice; -import org.bahmni.module.events.api.listener.PatientAdvice; -import org.bahmni.module.events.api.publisher.EventPublisher; -import org.springframework.aop.aspectj.AspectJExpressionPointcut; -import org.springframework.aop.support.DefaultPointcutAdvisor; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Conditional; -import org.springframework.context.annotation.Configuration; -import org.springframework.jms.core.JmsTemplate; -import org.springframework.jms.support.destination.DynamicDestinationResolver; -import org.springframework.jndi.JndiObjectFactoryBean; - -import javax.jms.ConnectionFactory; - -@Conditional(EventPublishingToggleCondition.class) -@Configuration -public class EventConfiguration { - - @Bean - public JndiObjectFactoryBean eventJndiObjectFactoryBean() { - JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); - - String jndiJMSResourceName = "jmsConnectionFactory"; - jndiObjectFactoryBean.setJndiName("java:comp/env/" + jndiJMSResourceName); - jndiObjectFactoryBean.setProxyInterface(ConnectionFactory.class); - jndiObjectFactoryBean.setLookupOnStartup(true); - - return jndiObjectFactoryBean; - } - - @Bean - public DynamicDestinationResolver eventDestinationResolver() { - return new DynamicDestinationResolver(); - } - - @Bean - public JmsTemplate jmsTemplate(JndiObjectFactoryBean eventJndiObjectFactoryBean, DynamicDestinationResolver eventDestinationResolver) { - JmsTemplate jmsTemplate = new JmsTemplate(); - jmsTemplate.setConnectionFactory((ConnectionFactory) eventJndiObjectFactoryBean.getObject()); - jmsTemplate.setDestinationResolver(eventDestinationResolver); - jmsTemplate.setPubSubDomain(true); - return jmsTemplate; - } - - @Bean - public PatientAdvice patientEventAdvice() { - return new PatientAdvice(); - } - - @Bean - public AspectJExpressionPointcut patientEventAdvicePointcut() { - AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); - pointcut.setExpression("execution(* org.openmrs.api.PatientService.savePatient(..))"); - return pointcut; - } - - @Bean - public DefaultPointcutAdvisor patientAdviceAdvisor(AspectJExpressionPointcut patientEventAdvicePointcut, PatientAdvice patientEventAdvice) { - DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(); - advisor.setPointcut(patientEventAdvicePointcut); - advisor.setAdvice(patientEventAdvice); - return advisor; - } - - @Bean - public AppointmentAdvice appointmentEventAdvice(ApplicationContext applicationContext) { - return new AppointmentAdvice(applicationContext); - } - - @Bean - public AspectJExpressionPointcut appointmentEventAdvicePointcut() { - AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); - pointcut.setExpression("execution(* org.openmrs.module.appointments.service.AppointmentsService.validateAndSave(..))"); - return pointcut; - } - - @Bean - public DefaultPointcutAdvisor appointmentAdviceAdvisor(AspectJExpressionPointcut appointmentEventAdvicePointcut, AppointmentAdvice appointmentEventAdvice) { - DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(); - advisor.setPointcut(appointmentEventAdvicePointcut); - advisor.setAdvice(appointmentEventAdvice); - return advisor; - } - - @Bean - public EncounterAdvice encounterEventAdvice() { - return new EncounterAdvice(); - } - - @Bean - public AspectJExpressionPointcut encounterEventAdvicePointcut() { - AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); - pointcut.setExpression("execution(* org.openmrs.api.EncounterService.saveEncounter(..))"); - return pointcut; - } - - @Bean - public DefaultPointcutAdvisor encounterAdviceAdvisor(AspectJExpressionPointcut encounterEventAdvicePointcut, EncounterAdvice encounterEventAdvice) { - DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(); - advisor.setPointcut(encounterEventAdvicePointcut); - advisor.setAdvice(encounterEventAdvice); - return advisor; - } - - @Bean - public EventPublisher eventPublisher(JmsTemplate jmsTemplate) { - return new EventPublisher(jmsTemplate, new ObjectMapper()); - } -} \ No newline at end of file diff --git a/api/src/main/java/org/bahmni/module/events/api/configuration/JMSEventPublisherConfiguration.java b/api/src/main/java/org/bahmni/module/events/api/configuration/JMSEventPublisherConfiguration.java new file mode 100644 index 0000000..6d015f9 --- /dev/null +++ b/api/src/main/java/org/bahmni/module/events/api/configuration/JMSEventPublisherConfiguration.java @@ -0,0 +1,48 @@ +package org.bahmni.module.events.api.configuration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.bahmni.module.events.api.publisher.JMSEventPublisher; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.jms.core.JmsTemplate; +import org.springframework.jms.support.destination.DynamicDestinationResolver; +import org.springframework.jndi.JndiObjectFactoryBean; + +import javax.jms.ConnectionFactory; + +@Conditional(JMSEventPublishingToggleCondition.class) +@Configuration +public class JMSEventPublisherConfiguration { + + @Bean + public JndiObjectFactoryBean eventJndiObjectFactoryBean() { + JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); + + String jndiJMSResourceName = "jmsConnectionFactory"; + jndiObjectFactoryBean.setJndiName("java:comp/env/" + jndiJMSResourceName); + jndiObjectFactoryBean.setProxyInterface(ConnectionFactory.class); + jndiObjectFactoryBean.setLookupOnStartup(true); + + return jndiObjectFactoryBean; + } + + @Bean + public DynamicDestinationResolver eventDestinationResolver() { + return new DynamicDestinationResolver(); + } + + @Bean + public JmsTemplate jmsTemplate(JndiObjectFactoryBean eventJndiObjectFactoryBean, DynamicDestinationResolver eventDestinationResolver) { + JmsTemplate jmsTemplate = new JmsTemplate(); + jmsTemplate.setConnectionFactory((ConnectionFactory) eventJndiObjectFactoryBean.getObject()); + jmsTemplate.setDestinationResolver(eventDestinationResolver); + jmsTemplate.setPubSubDomain(true); + return jmsTemplate; + } + + @Bean + public JMSEventPublisher jmsEventPublisher(JmsTemplate jmsTemplate) { + return new JMSEventPublisher(jmsTemplate, new ObjectMapper()); + } +} \ No newline at end of file diff --git a/api/src/main/java/org/bahmni/module/events/api/configuration/EventPublishingToggleCondition.java b/api/src/main/java/org/bahmni/module/events/api/configuration/JMSEventPublishingToggleCondition.java similarity index 89% rename from api/src/main/java/org/bahmni/module/events/api/configuration/EventPublishingToggleCondition.java rename to api/src/main/java/org/bahmni/module/events/api/configuration/JMSEventPublishingToggleCondition.java index 073d00e..f305dde 100644 --- a/api/src/main/java/org/bahmni/module/events/api/configuration/EventPublishingToggleCondition.java +++ b/api/src/main/java/org/bahmni/module/events/api/configuration/JMSEventPublishingToggleCondition.java @@ -6,7 +6,7 @@ import org.springframework.stereotype.Component; @Component -public class EventPublishingToggleCondition implements Condition { +public class JMSEventPublishingToggleCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { diff --git a/api/src/main/java/org/bahmni/module/events/api/listener/AppointmentAdvice.java b/api/src/main/java/org/bahmni/module/events/api/listener/AppointmentAdvice.java index d5bb7f5..a297dc7 100644 --- a/api/src/main/java/org/bahmni/module/events/api/listener/AppointmentAdvice.java +++ b/api/src/main/java/org/bahmni/module/events/api/listener/AppointmentAdvice.java @@ -1,62 +1,66 @@ package org.bahmni.module.events.api.listener; +import com.google.common.collect.Sets; import org.bahmni.module.events.api.model.BahmniEventType; import org.bahmni.module.events.api.model.Event; +import org.bahmni.module.events.api.publisher.BahmniEventPublisher; +import org.openmrs.api.context.Context; import org.openmrs.module.appointments.model.Appointment; import org.openmrs.module.appointments.web.mapper.AppointmentMapper; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; -import org.springframework.context.ApplicationContext; -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 java.util.Set; import java.util.function.Supplier; -import static org.bahmni.module.events.api.model.BahmniEventType.*; +import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_APPOINTMENT_CREATED; +import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_APPOINTMENT_UPDATED; -public class AppointmentAdvice implements AfterReturningAdvice, ApplicationEventPublisherAware, MethodBeforeAdvice { - - private ApplicationEventPublisher eventPublisher; +public class AppointmentAdvice implements AfterReturningAdvice, MethodBeforeAdvice { + private final BahmniEventPublisher eventPublisher; private final ThreadLocal> threadLocal = new ThreadLocal<>(); private final String APPOINTMENT_ID_KEY = "appointmentId"; + private final AppointmentMapper appointmentMapper; + private final Set adviceMethodNames = Sets.newHashSet("validateAndSave"); - private final ApplicationContext applicationContext; + public AppointmentAdvice() { + this.eventPublisher = Context.getRegisteredComponent("bahmniEventPublisher", BahmniEventPublisher.class); + this.appointmentMapper = Context.getRegisteredComponent("appointmentMapper", AppointmentMapper.class); + } - public AppointmentAdvice(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; + public AppointmentAdvice(BahmniEventPublisher bahmniEventPublisher, AppointmentMapper appointmentMapper) { + this.eventPublisher = bahmniEventPublisher; + this.appointmentMapper = appointmentMapper; } @Override public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) { - Map appointmentInfo = 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 (appointmentInfo != null) { - BahmniEventType eventType = appointmentInfo.get(APPOINTMENT_ID_KEY) == null ? BAHMNI_APPOINTMENT_CREATED : BAHMNI_APPOINTMENT_UPDATED; - threadLocal.remove(); - Appointment appointment = (Appointment) returnValue; - AppointmentMapper appointmentMapper = applicationContext.getBean(AppointmentMapper.class); - Object representation = appointmentMapper.constructResponse(appointment); - Event event = new Event(eventType, representation, appointment.getUuid()); - eventPublisher.publishEvent(event); - System.out.println("Successfully published event with uuid : " + appointment.getUuid()); + if (adviceMethodNames.contains(method.getName())) { + Map appointmentInfo = 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 (appointmentInfo != null) { + BahmniEventType eventType = appointmentInfo.get(APPOINTMENT_ID_KEY) == null ? BAHMNI_APPOINTMENT_CREATED : BAHMNI_APPOINTMENT_UPDATED; + threadLocal.remove(); + Appointment appointment = (Appointment) returnValue; + Object representation = appointmentMapper.constructResponse(appointment); + Event event = new Event(eventType, representation, appointment.getUuid()); + eventPublisher.publishEvent(event); + System.out.println("Successfully published event with uuid : " + appointment.getUuid()); + } } } - @Override - public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) { - this.eventPublisher = applicationEventPublisher; - } - @Override public void before(Method method, Object[] objects, Object o) { - Appointment appointment = ((Supplier) objects[0]).get(); - Map appointmentInfo = new HashMap<>(1); - appointmentInfo.put(APPOINTMENT_ID_KEY, appointment.getId()); - threadLocal.set(appointmentInfo); + if (adviceMethodNames.contains(method.getName())) { + Appointment appointment = ((Supplier) objects[0]).get(); + Map appointmentInfo = new HashMap<>(1); + appointmentInfo.put(APPOINTMENT_ID_KEY, appointment.getId()); + threadLocal.set(appointmentInfo); + } } } \ No newline at end of file diff --git a/api/src/main/java/org/bahmni/module/events/api/listener/EncounterAdvice.java b/api/src/main/java/org/bahmni/module/events/api/listener/EncounterAdvice.java index 31a2449..45ccd43 100644 --- a/api/src/main/java/org/bahmni/module/events/api/listener/EncounterAdvice.java +++ b/api/src/main/java/org/bahmni/module/events/api/listener/EncounterAdvice.java @@ -1,10 +1,14 @@ package org.bahmni.module.events.api.listener; +import com.google.common.collect.Sets; 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.bahmni.module.events.api.publisher.BahmniEventPublisher; import org.openmrs.Encounter; +import org.openmrs.api.context.Context; +import org.openmrs.module.appointments.web.mapper.AppointmentMapper; import org.openmrs.module.webservices.rest.web.ConversionUtil; import org.openmrs.module.webservices.rest.web.representation.Representation; import org.springframework.aop.AfterReturningAdvice; @@ -16,46 +20,53 @@ import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; +import java.util.Set; import static org.bahmni.module.events.api.model.BahmniEventType.*; -public class EncounterAdvice implements AfterReturningAdvice, ApplicationEventPublisherAware, MethodBeforeAdvice { +public class EncounterAdvice implements AfterReturningAdvice, MethodBeforeAdvice { private final Logger log = LogManager.getLogger(EncounterAdvice.class); - private ApplicationEventPublisher eventPublisher; - + private final BahmniEventPublisher eventPublisher; private final ThreadLocal> threadLocal = new ThreadLocal<>(); private final String ENCOUNTER_ID_KEY = "encounterId"; + private final Set adviceMethodNames = Sets.newHashSet("saveEncounter"); + public EncounterAdvice() { + this.eventPublisher = Context.getRegisteredComponent("bahmniEventPublisher", BahmniEventPublisher.class); + } - @Override - public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) { - Map 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()); - } + public EncounterAdvice(BahmniEventPublisher bahmniEventPublisher) { + this.eventPublisher = bahmniEventPublisher; } @Override - public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) { - this.eventPublisher = applicationEventPublisher; + public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) { + if (adviceMethodNames.contains(method.getName())) { + Map 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 before(Method method, Object[] objects, Object o) { - Encounter encounter = (Encounter) objects[0]; - Map encounterInfo = new HashMap<>(1); - encounterInfo.put(ENCOUNTER_ID_KEY, encounter.getId()); - threadLocal.set(encounterInfo); + if (adviceMethodNames.contains(method.getName())) { + Encounter encounter = (Encounter) objects[0]; + Map encounterInfo = new HashMap<>(1); + encounterInfo.put(ENCOUNTER_ID_KEY, encounter.getId()); + threadLocal.set(encounterInfo); + } } } diff --git a/api/src/main/java/org/bahmni/module/events/api/listener/PatientAdvice.java b/api/src/main/java/org/bahmni/module/events/api/listener/PatientAdvice.java index 76afe4e..4a65cfe 100644 --- a/api/src/main/java/org/bahmni/module/events/api/listener/PatientAdvice.java +++ b/api/src/main/java/org/bahmni/module/events/api/listener/PatientAdvice.java @@ -1,65 +1,68 @@ package org.bahmni.module.events.api.listener; +import com.google.common.collect.Sets; 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.bahmni.module.events.api.publisher.BahmniEventPublisher; import org.openmrs.Patient; +import org.openmrs.api.context.Context; 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 java.util.Set; import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_PATIENT_CREATED; import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_PATIENT_UPDATED; -public class PatientAdvice implements AfterReturningAdvice, ApplicationEventPublisherAware, MethodBeforeAdvice { +public class PatientAdvice implements AfterReturningAdvice, MethodBeforeAdvice { private final Logger log = LogManager.getLogger(PatientAdvice.class); - - private ApplicationEventPublisher eventPublisher; - + private final BahmniEventPublisher eventPublisher; private final ThreadLocal> threadLocal = new ThreadLocal<>(); private final String PATIENT_ID_KEY = "patientId"; + private final Set adviceMethodNames = Sets.newHashSet("savePatient"); + + public PatientAdvice() { + this.eventPublisher = Context.getRegisteredComponent("bahmniEventPublisher", BahmniEventPublisher.class); + } + public PatientAdvice(BahmniEventPublisher bahmniEventPublisher) { + this.eventPublisher = bahmniEventPublisher; + } @Override public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) { + if (adviceMethodNames.contains(method.getName())) { + Map patientInfo = threadLocal.get(); + if (patientInfo != null) { + BahmniEventType eventType = patientInfo.get(PATIENT_ID_KEY) == null ? BAHMNI_PATIENT_CREATED : BAHMNI_PATIENT_UPDATED; + threadLocal.remove(); - Map patientInfo = 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 (patientInfo != null) { - BahmniEventType eventType = patientInfo != null && patientInfo.get(PATIENT_ID_KEY) == null ? BAHMNI_PATIENT_CREATED: BAHMNI_PATIENT_UPDATED; - threadLocal.remove(); - - Patient patient = (Patient) returnValue; + Patient patient = (Patient) returnValue; - Object representation = ConversionUtil.convertToRepresentation(patient, Representation.FULL); - Event event = new Event(eventType, representation, patient.getUuid()); - eventPublisher.publishEvent(event); + Object representation = ConversionUtil.convertToRepresentation(patient, Representation.FULL); + Event event = new Event(eventType, representation, patient.getUuid()); + eventPublisher.publishEvent(event); - log.info("Successfully published event with uuid : " + patient.getUuid()); + log.info("Successfully published event with uuid : " + patient.getUuid()); + } } } - - @Override - public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) { - this.eventPublisher = applicationEventPublisher; - } - @Override public void before(Method method, Object[] objects, Object o) { - Patient patient = (Patient) objects[0]; + if (adviceMethodNames.contains(method.getName())) { + Patient patient = (Patient) objects[0]; - Map patientInfo = new HashMap<>(1); - patientInfo.put(PATIENT_ID_KEY, patient.getId()); - threadLocal.set(patientInfo); + Map patientInfo = new HashMap<>(1); + patientInfo.put(PATIENT_ID_KEY, patient.getId()); + threadLocal.set(patientInfo); + } } } diff --git a/api/src/main/java/org/bahmni/module/events/api/publisher/BahmniEventPublisher.java b/api/src/main/java/org/bahmni/module/events/api/publisher/BahmniEventPublisher.java new file mode 100644 index 0000000..f494725 --- /dev/null +++ b/api/src/main/java/org/bahmni/module/events/api/publisher/BahmniEventPublisher.java @@ -0,0 +1,20 @@ +package org.bahmni.module.events.api.publisher; + +import org.bahmni.module.events.api.model.Event; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.lang.NonNull; + +public class BahmniEventPublisher implements ApplicationEventPublisherAware { + + private ApplicationEventPublisher eventPublisher; + + @Override + public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) { + this.eventPublisher = applicationEventPublisher; + } + + public void publishEvent(Event event) { + this.eventPublisher.publishEvent(event); + } +} \ No newline at end of file diff --git a/api/src/main/java/org/bahmni/module/events/api/publisher/JMSEventPublisher.java b/api/src/main/java/org/bahmni/module/events/api/publisher/JMSEventPublisher.java new file mode 100644 index 0000000..33bfa88 --- /dev/null +++ b/api/src/main/java/org/bahmni/module/events/api/publisher/JMSEventPublisher.java @@ -0,0 +1,28 @@ +package org.bahmni.module.events.api.publisher; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bahmni.module.events.api.model.Event; +import org.springframework.context.event.EventListener; +import org.springframework.jms.core.JmsTemplate; + +public class JMSEventPublisher { + + private static final Logger log = LogManager.getLogger(JMSEventPublisher.class); + + private final JmsTemplate jmsTemplate; + + private final ObjectMapper objectMapper; + + public JMSEventPublisher(JmsTemplate jmsTemplate, ObjectMapper objectMapper) { + this.jmsTemplate = jmsTemplate; + this.objectMapper = objectMapper; + } + + @EventListener + public void onApplicationEvent(Event event) { + jmsTemplate.send(event.eventType.topic(), new JMSMessageCreator(objectMapper, event)); + log.info("Published Message with id : " + event.payloadId); + } +} diff --git a/api/src/test/java/org/bahmni/module/events/api/configuration/EventPublishingToggleConditionTest.java b/api/src/test/java/org/bahmni/module/events/api/configuration/JMSEventPublishingToggleConditionTest.java similarity index 50% rename from api/src/test/java/org/bahmni/module/events/api/configuration/EventPublishingToggleConditionTest.java rename to api/src/test/java/org/bahmni/module/events/api/configuration/JMSEventPublishingToggleConditionTest.java index f5f4aee..f8e71d3 100644 --- a/api/src/test/java/org/bahmni/module/events/api/configuration/EventPublishingToggleConditionTest.java +++ b/api/src/test/java/org/bahmni/module/events/api/configuration/JMSEventPublishingToggleConditionTest.java @@ -3,12 +3,12 @@ import org.junit.Test; import org.junit.jupiter.api.Assertions; -public class EventPublishingToggleConditionTest { +public class JMSEventPublishingToggleConditionTest { @Test public void shouldReturnFalseGivenEventPublishingTogglePropertyNotFound() { - EventPublishingToggleCondition eventPublishingToggleCondition = new EventPublishingToggleCondition(); - boolean matches = eventPublishingToggleCondition.matches(null, null); + JMSEventPublishingToggleCondition JMSEventPublishingToggleCondition = new JMSEventPublishingToggleCondition(); + boolean matches = JMSEventPublishingToggleCondition.matches(null, null); Assertions.assertFalse(matches); } } \ No newline at end of file diff --git a/api/src/test/java/org/bahmni/module/events/api/listener/AppointmentAdviceTest.java b/api/src/test/java/org/bahmni/module/events/api/listener/AppointmentAdviceTest.java index b1df00a..2c5f909 100644 --- a/api/src/test/java/org/bahmni/module/events/api/listener/AppointmentAdviceTest.java +++ b/api/src/test/java/org/bahmni/module/events/api/listener/AppointmentAdviceTest.java @@ -2,11 +2,15 @@ import org.bahmni.module.events.api.model.BahmniEventType; import org.bahmni.module.events.api.model.Event; +import org.bahmni.module.events.api.publisher.BahmniEventPublisher; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; +import org.openmrs.Patient; +import org.openmrs.api.PatientService; import org.openmrs.module.appointments.model.Appointment; +import org.openmrs.module.appointments.service.AppointmentsService; import org.openmrs.module.appointments.web.contract.AppointmentDefaultResponse; import org.openmrs.module.appointments.web.mapper.AppointmentMapper; import org.openmrs.module.webservices.rest.web.ConversionUtil; @@ -16,6 +20,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEventPublisher; +import java.lang.reflect.Method; import java.util.UUID; import java.util.function.Supplier; @@ -30,102 +35,99 @@ public class AppointmentAdviceTest { private AppointmentAdvice appointmentAdviceAdvice; - - private ApplicationEventPublisher applicationEventPublisher; - - private ApplicationContext applicationContext; + private BahmniEventPublisher bahmniEventPublisher; private AppointmentMapper appointmentMapper; - + + private final String SAVE_APPOINTMENT_METHOD_NAME = "validateAndSave"; + @Before public void setUp() { - applicationEventPublisher = mock(ApplicationEventPublisher.class); - applicationContext = mock(ApplicationContext.class); - appointmentAdviceAdvice = new AppointmentAdvice(applicationContext); - appointmentAdviceAdvice.setApplicationEventPublisher(applicationEventPublisher); appointmentMapper = mock(AppointmentMapper.class); - } + bahmniEventPublisher = mock(BahmniEventPublisher.class); + appointmentAdviceAdvice = new AppointmentAdvice(bahmniEventPublisher, appointmentMapper); + } @Test - public void shouldVerifyBahmniEventPublishIsCalledGivenAppointmentIsCreated() { + public void shouldVerifyBahmniEventPublishIsCalledGivenAppointmentIsCreated() throws NoSuchMethodException { + Method saveAppointmentMethod = AppointmentsService.class.getMethod(SAVE_APPOINTMENT_METHOD_NAME, Appointment.class); Supplier appointmentSupplier = getAppointmentSupplier(); Object[] args = {appointmentSupplier}; Appointment newAppointment = appointmentSupplier.get(); newAppointment.setId(null); - appointmentAdviceAdvice.before(null, args, null); + appointmentAdviceAdvice.before(saveAppointmentMethod, args, null); AppointmentDefaultResponse defaultResponse = new AppointmentDefaultResponse(); defaultResponse.setUuid(newAppointment.getUuid()); - when(applicationContext.getBean(AppointmentMapper.class)).thenReturn(appointmentMapper); when(appointmentMapper.constructResponse(newAppointment)).thenReturn(defaultResponse); - appointmentAdviceAdvice.afterReturning(appointmentSupplier.get(), null, null, null); + appointmentAdviceAdvice.afterReturning(appointmentSupplier.get(), saveAppointmentMethod, null, null); - verify(applicationEventPublisher, times(1)).publishEvent(any(Event.class)); + verify(bahmniEventPublisher, times(1)).publishEvent(any(Event.class)); } @Test - public void shouldPublishCreateEventGivenAppointmentIsCreated() { + public void shouldPublishCreateEventGivenAppointmentIsCreated() throws NoSuchMethodException { + Method saveAppointmentMethod = AppointmentsService.class.getMethod(SAVE_APPOINTMENT_METHOD_NAME, Appointment.class); Supplier appointmentSupplier = getAppointmentSupplier(); Object[] args = {appointmentSupplier}; Appointment newAppointment = appointmentSupplier.get(); newAppointment.setId(null); - appointmentAdviceAdvice.before(null, args, null); + appointmentAdviceAdvice.before(saveAppointmentMethod, args, null); AppointmentDefaultResponse defaultResponse = new AppointmentDefaultResponse(); defaultResponse.setUuid(newAppointment.getUuid()); - when(applicationContext.getBean(AppointmentMapper.class)).thenReturn(appointmentMapper); when(appointmentMapper.constructResponse(newAppointment)).thenReturn(defaultResponse); - appointmentAdviceAdvice.afterReturning(appointmentSupplier.get(), null, null, null); + appointmentAdviceAdvice.afterReturning(appointmentSupplier.get(), saveAppointmentMethod, null, null); ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); - verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); + verify(bahmniEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); Event event = eventArgumentCaptor.getValue(); assertEquals(BahmniEventType.BAHMNI_APPOINTMENT_CREATED, event.eventType); } @Test - public void shouldPublishUpdateEventGivenAppointmentIsUpdated() { + public void shouldPublishUpdateEventGivenAppointmentIsUpdated() throws NoSuchMethodException { + Method saveAppointmentMethod = AppointmentsService.class.getMethod(SAVE_APPOINTMENT_METHOD_NAME, Appointment.class); Supplier appointmentSupplier = getAppointmentSupplier(); Object[] args = {appointmentSupplier}; Appointment appointment = appointmentSupplier.get(); - appointmentAdviceAdvice.before(null, args, null); + appointmentAdviceAdvice.before(saveAppointmentMethod, args, null); AppointmentDefaultResponse defaultResponse = new AppointmentDefaultResponse(); defaultResponse.setUuid(appointment.getUuid()); - when(applicationContext.getBean(AppointmentMapper.class)).thenReturn(appointmentMapper); when(appointmentMapper.constructResponse(appointment)).thenReturn(defaultResponse); - appointmentAdviceAdvice.afterReturning(appointmentSupplier.get(), null, null, null); + appointmentAdviceAdvice.afterReturning(appointmentSupplier.get(), saveAppointmentMethod, null, null); ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); - verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); + verify(bahmniEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); Event event = eventArgumentCaptor.getValue(); assertEquals(BahmniEventType.BAHMNI_APPOINTMENT_UPDATED, event.eventType); } @Test - public void shouldVerifyPublishedContentForAppointment() { + public void shouldVerifyPublishedContentForAppointment() throws NoSuchMethodException { + Method saveAppointmentMethod = AppointmentsService.class.getMethod(SAVE_APPOINTMENT_METHOD_NAME, Appointment.class); Supplier appointmentSupplier = getAppointmentSupplier(); Object[] args = {appointmentSupplier}; Appointment appointment = appointmentSupplier.get(); - appointmentAdviceAdvice.before(null, args, null); + appointmentAdviceAdvice.before(saveAppointmentMethod, args, null); AppointmentDefaultResponse defaultResponse = new AppointmentDefaultResponse(); defaultResponse.setUuid(appointment.getUuid()); - when(applicationContext.getBean(AppointmentMapper.class)).thenReturn(appointmentMapper); when(appointmentMapper.constructResponse(appointment)).thenReturn(defaultResponse); - appointmentAdviceAdvice.afterReturning(appointmentSupplier.get(), null, null, null); + appointmentAdviceAdvice.afterReturning(appointmentSupplier.get(), saveAppointmentMethod, null, null); ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); - verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); + verify(bahmniEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); Event event = eventArgumentCaptor.getValue(); assertEquals(BahmniEventType.BAHMNI_APPOINTMENT_UPDATED, event.eventType); @@ -137,7 +139,6 @@ private Supplier getAppointmentSupplier() { appointment.setId(123); appointment.setUuid(UUID.randomUUID().toString()); - Supplier appointmentSupplier = () -> appointment; - return appointmentSupplier; + return () -> appointment; } } diff --git a/api/src/test/java/org/bahmni/module/events/api/listener/EncounterAdviceTest.java b/api/src/test/java/org/bahmni/module/events/api/listener/EncounterAdviceTest.java index 70e26f5..1f08246 100644 --- a/api/src/test/java/org/bahmni/module/events/api/listener/EncounterAdviceTest.java +++ b/api/src/test/java/org/bahmni/module/events/api/listener/EncounterAdviceTest.java @@ -2,19 +2,21 @@ import org.bahmni.module.events.api.model.BahmniEventType; import org.bahmni.module.events.api.model.Event; +import org.bahmni.module.events.api.publisher.BahmniEventPublisher; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.openmrs.Encounter; +import org.openmrs.api.EncounterService; 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.lang.reflect.Method; import java.util.UUID; import static org.junit.Assert.assertEquals; @@ -26,32 +28,35 @@ @RunWith(PowerMockRunner.class) public class EncounterAdviceTest { - private final EncounterAdvice encounterAdvice = new EncounterAdvice(); + private EncounterAdvice encounterAdvice; + private BahmniEventPublisher bahmniEventPublisher; - private ApplicationEventPublisher applicationEventPublisher; + private final String ENCOUNTER_SAVE_METHOD_NAME = "saveEncounter"; @Before public void setUp() { - applicationEventPublisher = mock(ApplicationEventPublisher.class); - encounterAdvice.setApplicationEventPublisher(applicationEventPublisher); + bahmniEventPublisher = mock(BahmniEventPublisher.class); + encounterAdvice = new EncounterAdvice(bahmniEventPublisher); } @Test - public void shouldVerifyBahmniEventPublishIsCalledGivenEncounterIsCreated() { + public void shouldVerifyBahmniEventPublishIsCalledGivenEncounterIsCreated() throws NoSuchMethodException { + Method saveEncounterMethod = EncounterService.class.getMethod(ENCOUNTER_SAVE_METHOD_NAME, Encounter.class); Encounter newEncounter = getEncounter(); PowerMockito.mockStatic(ConversionUtil.class); Object[] args = {newEncounter}; newEncounter.setId(null); - encounterAdvice.before(null, args, null); + encounterAdvice.before(saveEncounterMethod, args, null); PowerMockito.when(ConversionUtil.convertToRepresentation(getEncounter(), Representation.FULL)).thenReturn(newEncounter); - encounterAdvice.afterReturning(getEncounter(), null, null, null); + encounterAdvice.afterReturning(getEncounter(), saveEncounterMethod, null, null); - verify(applicationEventPublisher, times(1)).publishEvent(any(Event.class)); + verify(bahmniEventPublisher, times(1)).publishEvent(any(Event.class)); } @Test - public void shouldPublishCreateEventGivenEncounterIsCreated() { + public void shouldPublishCreateEventGivenEncounterIsCreated() throws NoSuchMethodException { + Method saveEncounterMethod = EncounterService.class.getMethod(ENCOUNTER_SAVE_METHOD_NAME, Encounter.class); Encounter newEncounter = getEncounter(); PowerMockito.mockStatic(ConversionUtil.class); @@ -59,47 +64,49 @@ public void shouldPublishCreateEventGivenEncounterIsCreated() { Object[] args = {newEncounter}; newEncounter.setId(null); - encounterAdvice.before(null, args, null); - encounterAdvice.afterReturning(newEncounter, null, null, null); + encounterAdvice.before(saveEncounterMethod, args, null); + encounterAdvice.afterReturning(newEncounter, saveEncounterMethod, null, null); ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); - verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); + verify(bahmniEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); Event event = eventArgumentCaptor.getValue(); assertEquals(BahmniEventType.BAHMNI_ENCOUNTER_CREATED, event.eventType); } @Test - public void shouldPublishUpdateEventGivenEncounterIsUpdated() { + public void shouldPublishUpdateEventGivenEncounterIsUpdated() throws NoSuchMethodException { + Method saveEncounterMethod = EncounterService.class.getMethod(ENCOUNTER_SAVE_METHOD_NAME, Encounter.class); 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); + encounterAdvice.before(saveEncounterMethod, args, null); + encounterAdvice.afterReturning(newEncounter, saveEncounterMethod, null, null); ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); - verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); + verify(bahmniEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); Event event = eventArgumentCaptor.getValue(); assertEquals(BahmniEventType.BAHMNI_ENCOUNTER_UPDATED, event.eventType); } @Test - public void shouldVerifyPublishedContentForAEncounter() { + public void shouldVerifyPublishedContentForAEncounter() throws NoSuchMethodException { + Method saveEncounterMethod = EncounterService.class.getMethod(ENCOUNTER_SAVE_METHOD_NAME, Encounter.class); 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); + encounterAdvice.before(saveEncounterMethod, args, null); + encounterAdvice.afterReturning(newEncounter, saveEncounterMethod, null, null); ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); - verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); + verify(bahmniEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); Event event = eventArgumentCaptor.getValue(); assertEquals(BahmniEventType.BAHMNI_ENCOUNTER_UPDATED, event.eventType); diff --git a/api/src/test/java/org/bahmni/module/events/api/listener/PatientAdviceTest.java b/api/src/test/java/org/bahmni/module/events/api/listener/PatientAdviceTest.java index 79e312e..0dad4aa 100644 --- a/api/src/test/java/org/bahmni/module/events/api/listener/PatientAdviceTest.java +++ b/api/src/test/java/org/bahmni/module/events/api/listener/PatientAdviceTest.java @@ -2,6 +2,7 @@ import org.bahmni.module.events.api.model.BahmniEventType; import org.bahmni.module.events.api.model.Event; +import org.bahmni.module.events.api.publisher.BahmniEventPublisher; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -9,14 +10,15 @@ import org.openmrs.Patient; import org.openmrs.Person; import org.openmrs.PersonName; +import org.openmrs.api.PatientService; 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.lang.reflect.Method; import java.util.Date; import java.util.HashSet; import java.util.Set; @@ -30,88 +32,110 @@ @PrepareForTest({ ConversionUtil.class }) @RunWith(PowerMockRunner.class) public class PatientAdviceTest { - - private final PatientAdvice patientAdvice = new PatientAdvice(); - - private ApplicationEventPublisher applicationEventPublisher; - - @Before - public void setUp() { - applicationEventPublisher = mock(ApplicationEventPublisher.class); - patientAdvice.setApplicationEventPublisher(applicationEventPublisher); - } - - @Test - public void shouldVerifyBahmniEventPublishIsCalledGivenPatientIsCreated() { - Patient newPatient = getPatient(); - PowerMockito.mockStatic(ConversionUtil.class); + + private PatientAdvice patientAdvice; + + private BahmniEventPublisher bahmniEventPublisher; + + private final String SAVE_PATIENT_METHOD_NAME = "savePatient"; + private final String PURGE_PATIENT_METHOD_NAME = "purgePatient"; + + @Before + public void setUp() { + bahmniEventPublisher = mock(BahmniEventPublisher.class); + patientAdvice = new PatientAdvice(bahmniEventPublisher); + } + + @Test + public void shouldVerifyTheEventPublishedIsNotGettingTriggeredGivenPatientNeitherCreatedNorUpdated() throws NoSuchMethodException { + Method savePatientMethod = PatientService.class.getMethod(PURGE_PATIENT_METHOD_NAME, Patient.class); + Patient newPatient = getPatient(); + PowerMockito.mockStatic(ConversionUtil.class); + Object[] args = {newPatient}; + newPatient.setId(null); + patientAdvice.before(savePatientMethod, args, null); + PowerMockito.when(ConversionUtil.convertToRepresentation(getPatient(), Representation.FULL)).thenReturn(newPatient); + + patientAdvice.afterReturning(getPatient(), savePatientMethod, null, null); + + verify(bahmniEventPublisher, times(0)).publishEvent(any(Event.class)); + } + + @Test + public void shouldVerifyTheEventPublishedIsGettingTriggeredGivenPatientIsCreated() throws NoSuchMethodException { + Method savePatientMethod = PatientService.class.getMethod(SAVE_PATIENT_METHOD_NAME, Patient.class); + Patient newPatient = getPatient(); + PowerMockito.mockStatic(ConversionUtil.class); Object[] args = {newPatient}; newPatient.setId(null); - patientAdvice.before(null, args, null); - PowerMockito.when(ConversionUtil.convertToRepresentation(getPatient(), Representation.FULL)).thenReturn(newPatient); - - patientAdvice.afterReturning(getPatient(), null, null, null); - - verify(applicationEventPublisher, times(1)).publishEvent(any(Event.class)); - } + patientAdvice.before(savePatientMethod, args, null); + PowerMockito.when(ConversionUtil.convertToRepresentation(getPatient(), Representation.FULL)).thenReturn(newPatient); + + patientAdvice.afterReturning(getPatient(), savePatientMethod, null, null); + + verify(bahmniEventPublisher, times(1)).publishEvent(any(Event.class)); + } @Test - public void shouldPublishCreateEventGivenPatientIsCreated() { + public void shouldPublishCreateEventGivenPatientIsCreated() throws NoSuchMethodException { Patient newPatient = getPatient(); + Method savePatientMethod = PatientService.class.getMethod(SAVE_PATIENT_METHOD_NAME, Patient.class); PowerMockito.mockStatic(ConversionUtil.class); PowerMockito.when(ConversionUtil.convertToRepresentation(getPatient(), Representation.FULL)).thenReturn(newPatient); Object[] args = {newPatient}; newPatient.setId(null); - patientAdvice.before(null, args, null); - patientAdvice.afterReturning(newPatient, null, null, null); + patientAdvice.before(savePatientMethod, args, null); + patientAdvice.afterReturning(newPatient, savePatientMethod, null, null); ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); - verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); + verify(bahmniEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); Event event = eventArgumentCaptor.getValue(); assertEquals(BahmniEventType.BAHMNI_PATIENT_CREATED, event.eventType); } @Test - public void shouldPublishUpdateEventGivenPatientIsUpdated() { + public void shouldPublishUpdateEventGivenPatientIsUpdated() throws NoSuchMethodException { Patient newPatient = getPatient(); + Method savePatientMethod = PatientService.class.getMethod(SAVE_PATIENT_METHOD_NAME, Patient.class); PowerMockito.mockStatic(ConversionUtil.class); PowerMockito.when(ConversionUtil.convertToRepresentation(getPatient(), Representation.FULL)).thenReturn(newPatient); Object[] args = {newPatient}; - patientAdvice.before(null, args, null); - patientAdvice.afterReturning(newPatient, null, null, null); + patientAdvice.before(savePatientMethod, args, null); + patientAdvice.afterReturning(newPatient, savePatientMethod, null, null); ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); - verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); + verify(bahmniEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); Event event = eventArgumentCaptor.getValue(); assertEquals(BahmniEventType.BAHMNI_PATIENT_UPDATED, event.eventType); } - - @Test - public void shouldVerifyPublishedContentForAPatient() { - Patient newPatient = getPatient(); - - PowerMockito.mockStatic(ConversionUtil.class); - PowerMockito.when(ConversionUtil.convertToRepresentation(getPatient(), Representation.FULL)).thenReturn(newPatient); + + @Test + public void shouldVerifyEventPublishedContentGivenPatientIsCreated() throws NoSuchMethodException { + Patient newPatient = getPatient(); + Method savePatientMethod = PatientService.class.getMethod(SAVE_PATIENT_METHOD_NAME, Patient.class); + + PowerMockito.mockStatic(ConversionUtil.class); + PowerMockito.when(ConversionUtil.convertToRepresentation(getPatient(), Representation.FULL)).thenReturn(newPatient); Object[] args = {newPatient}; - patientAdvice.before(null, args, null); - patientAdvice.afterReturning(newPatient, null, null, null); - - ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); - verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); - - Event event = eventArgumentCaptor.getValue(); - assertEquals(BahmniEventType.BAHMNI_PATIENT_UPDATED, event.eventType); - assertEquals(newPatient.getUuid(), event.payloadId); - } - - private Patient getPatient() { + patientAdvice.before(savePatientMethod, args, null); + patientAdvice.afterReturning(newPatient, savePatientMethod, null, null); + + ArgumentCaptor eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); + verify(bahmniEventPublisher, times(1)).publishEvent(eventArgumentCaptor.capture()); + + Event event = eventArgumentCaptor.getValue(); + assertEquals(BahmniEventType.BAHMNI_PATIENT_UPDATED, event.eventType); + assertEquals(newPatient.getUuid(), event.payloadId); + } + + private Patient getPatient() { PersonName name = new PersonName(); name.setGivenName("firstname"); name.setFamilyName("lastname"); diff --git a/api/src/test/java/org/bahmni/module/events/api/publisher/BahmniEventPublisherTest.java b/api/src/test/java/org/bahmni/module/events/api/publisher/BahmniEventPublisherTest.java new file mode 100644 index 0000000..261ceb7 --- /dev/null +++ b/api/src/test/java/org/bahmni/module/events/api/publisher/BahmniEventPublisherTest.java @@ -0,0 +1,49 @@ +package org.bahmni.module.events.api.publisher; + +import org.bahmni.module.events.api.listener.PatientAdvice; +import org.bahmni.module.events.api.model.Event; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +import org.openmrs.Person; +import org.springframework.context.ApplicationEventPublisher; + +import java.util.Date; + +import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_PATIENT_CREATED; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class BahmniEventPublisherTest { + + private final BahmniEventPublisher bahmniEventPublisher = new BahmniEventPublisher(); + + private ApplicationEventPublisher applicationEventPublisher; + + @Before + public void setUp() { + applicationEventPublisher = mock(ApplicationEventPublisher.class); + bahmniEventPublisher.setApplicationEventPublisher(applicationEventPublisher); + } + + @Test + public void shouldVerifyEventGetsPublished() { + Person person = getPerson(); + Event event = new Event(BAHMNI_PATIENT_CREATED, person, person.getUuid()); + + bahmniEventPublisher.publishEvent(event); + + verify(applicationEventPublisher, times(1)).publishEvent(event); + } + + private Person getPerson() { + Person person = new Person(); + person.setId(123); + person.setGender("M"); + person.setBirthdate(new Date(694224000000L)); + person.setUuid("bce786c0-aa57-480d-be6a-23692590086b"); + + return person; + } +} \ No newline at end of file diff --git a/api/src/test/java/org/bahmni/module/events/api/publisher/JMSEventPublisherTest.java b/api/src/test/java/org/bahmni/module/events/api/publisher/JMSEventPublisherTest.java new file mode 100644 index 0000000..0c91cd5 --- /dev/null +++ b/api/src/test/java/org/bahmni/module/events/api/publisher/JMSEventPublisherTest.java @@ -0,0 +1,43 @@ +package org.bahmni.module.events.api.publisher; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.bahmni.module.events.api.model.Event; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +import org.openmrs.Person; +import org.springframework.jms.core.JmsTemplate; + +import java.util.Date; + +import static org.bahmni.module.events.api.model.BahmniEventType.BAHMNI_PATIENT_CREATED; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class JMSEventPublisherTest { + + @Test + public void shouldPublishEventOnTopicNameAssociateWithEventType() { + JMSEventPublisher eventPublisher; + JmsTemplate jmsTemplate = mock(JmsTemplate.class); + ObjectMapper objectMapper = new ObjectMapper(); + eventPublisher = new JMSEventPublisher(jmsTemplate, objectMapper); + + Person person = getPerson(); + Event event = new Event(BAHMNI_PATIENT_CREATED, person, person.getUuid()); + + eventPublisher.onApplicationEvent(event); + + verify(jmsTemplate, times(1)).send(anyString(), any(JMSMessageCreator.class)); + } + + private Person getPerson() { + Person person = new Person(); + person.setId(123); + person.setGender("M"); + person.setBirthdate(new Date(694224000000L)); + person.setUuid("bce786c0-aa57-480d-be6a-23692590086b"); + + return person; + } +} diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index a565026..9c982f3 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -15,6 +15,21 @@ org.bahmni.module.appointments + + org.openmrs.api.PatientService + org.bahmni.module.events.api.listener.PatientAdvice + + + + org.openmrs.module.appointments.service.AppointmentsService + org.bahmni.module.events.api.listener.AppointmentAdvice + + + + org.openmrs.api.EncounterService + org.bahmni.module.events.api.listener.EncounterAdvice + + org.bahmni.module.events.api.EventsActivator