Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: (2.40) Enrollment/Event notification in capture app/ new tracker api [DHIS2-18633] #19530

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@
*/
package org.hisp.dhis.tracker.job;

import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.hisp.dhis.artemis.MessageManager;
import org.hisp.dhis.artemis.Topics;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.AsyncTaskExecutor;
import org.hisp.dhis.render.RenderService;
import org.hisp.dhis.scheduling.JobConfiguration;
import org.hisp.dhis.scheduling.JobType;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
Expand All @@ -46,29 +40,12 @@
* @author Zubair Asghar
*/
@Component
public class TrackerNotificationMessageManager extends BaseMessageManager {
@RequiredArgsConstructor
public class TrackerNotificationMessageManager {
private final ObjectFactory<TrackerNotificationThread> trackerNotificationThreadObjectFactory;
private final AsyncTaskExecutor taskExecutor;

public TrackerNotificationMessageManager(
MessageManager messageManager,
AsyncTaskExecutor taskExecutor,
RenderService renderService,
ObjectFactory<TrackerNotificationThread> trackerNotificationThreadObjectFactory) {
super(messageManager, taskExecutor, renderService);
this.trackerNotificationThreadObjectFactory = trackerNotificationThreadObjectFactory;
}

@Override
public String getTopic() {
return Topics.TRACKER_IMPORT_NOTIFICATION_TOPIC_NAME;
}

@JmsListener(
destination = Topics.TRACKER_IMPORT_NOTIFICATION_TOPIC_NAME,
containerFactory = "jmsQueueListenerContainerFactory")
public void consume(TextMessage message) throws JMSException, IOException {
TrackerSideEffectDataBundle bundle = toBundle(message);

public void sendNotifications(TrackerSideEffectDataBundle bundle) {
if (bundle == null) {
return;
}
Expand All @@ -84,6 +61,6 @@ public void consume(TextMessage message) throws JMSException, IOException {

notificationThread.setSideEffectDataBundle(bundle);

executeJob(notificationThread);
taskExecutor.executeTask(notificationThread);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@
*/
package org.hisp.dhis.tracker.job;

import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.hisp.dhis.artemis.MessageManager;
import org.hisp.dhis.artemis.Topics;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.AsyncTaskExecutor;
import org.hisp.dhis.render.RenderService;
import org.hisp.dhis.scheduling.JobConfiguration;
import org.hisp.dhis.scheduling.JobType;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
Expand All @@ -46,29 +40,12 @@
* @author Zubair Asghar
*/
@Component
public class TrackerRuleEngineMessageManager extends BaseMessageManager {
@RequiredArgsConstructor
public class TrackerRuleEngineMessageManager {
private final ObjectFactory<TrackerRuleEngineThread> trackerRuleEngineThreadObjectFactory;
private final AsyncTaskExecutor taskExecutor;

public TrackerRuleEngineMessageManager(
MessageManager messageManager,
AsyncTaskExecutor taskExecutor,
RenderService renderService,
ObjectFactory<TrackerRuleEngineThread> trackerRuleEngineThreadObjectFactory) {
super(messageManager, taskExecutor, renderService);
this.trackerRuleEngineThreadObjectFactory = trackerRuleEngineThreadObjectFactory;
}

@Override
public String getTopic() {
return Topics.TRACKER_IMPORT_RULE_ENGINE_TOPIC_NAME;
}

@JmsListener(
destination = Topics.TRACKER_IMPORT_RULE_ENGINE_TOPIC_NAME,
containerFactory = "jmsQueueListenerContainerFactory")
public void consume(TextMessage message) throws JMSException, IOException {
TrackerSideEffectDataBundle bundle = toBundle(message);

public void sendRuleEngineNotifications(TrackerSideEffectDataBundle bundle) {
if (bundle == null) {
return;
}
Expand All @@ -83,6 +60,6 @@ public void consume(TextMessage message) throws JMSException, IOException {

notificationThread.setSideEffectDataBundle(bundle);

executeJob(notificationThread);
taskExecutor.executeTask(notificationThread);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class NotificationSideEffectHandlerService implements SideEffectHandlerSe

@Override
public void handleSideEffect(TrackerSideEffectDataBundle sideEffectDataBundle) {
notificationMessageManager.addJob(sideEffectDataBundle);
notificationMessageManager.sendNotifications(sideEffectDataBundle);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class RuleEngineSideEffectHandlerService implements SideEffectHandlerServ

@Override
public void handleSideEffect(TrackerSideEffectDataBundle sideEffectDataBundle) {
ruleEngineMessageManager.addJob(sideEffectDataBundle);
ruleEngineMessageManager.sendRuleEngineNotifications(sideEffectDataBundle);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,15 @@
*/
package org.hisp.dhis.tracker.job;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import java.io.IOException;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.hisp.dhis.artemis.MessageManager;
import org.hisp.dhis.artemis.Topics;
import org.hisp.dhis.common.AsyncTaskExecutor;
import org.hisp.dhis.render.RenderService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.ObjectFactory;

Expand All @@ -60,63 +44,28 @@
*/
@ExtendWith(MockitoExtension.class)
class TrackerNotificationMessageManagerTest {

@Mock private ObjectFactory<TrackerNotificationThread> objectFactory;

@Mock private MessageManager messageManager;

@Mock private RenderService renderService;

@Mock private TextMessage textMessage;

@Mock private AsyncTaskExecutor taskExecutor;

@Mock private TrackerNotificationThread trackerNotificationThread;

@InjectMocks private TrackerNotificationMessageManager trackerNotificationMessageManager;

@Captor private ArgumentCaptor<String> topicCaptor;

@Captor private ArgumentCaptor<TrackerSideEffectDataBundle> bundleArgumentCaptor;
@Mock private ObjectFactory<TrackerNotificationThread> objectFactory;

@Captor private ArgumentCaptor<Runnable> runnableCaptor;
@InjectMocks private TrackerNotificationMessageManager trackerNotificationMessageManager;

@Test
void test_add_job() {
doNothing().when(messageManager).sendQueue(anyString(), any(TrackerSideEffectDataBundle.class));

void shouldSendNotification() {
when(objectFactory.getObject()).thenReturn(trackerNotificationThread);
TrackerSideEffectDataBundle dataBundle = TrackerSideEffectDataBundle.builder().build();

trackerNotificationMessageManager.addJob(dataBundle);
trackerNotificationMessageManager.sendNotifications(dataBundle);

Mockito.verify(messageManager).sendQueue(topicCaptor.capture(), bundleArgumentCaptor.capture());

assertEquals(Topics.TRACKER_IMPORT_NOTIFICATION_TOPIC_NAME, topicCaptor.getValue());
assertEquals(dataBundle, bundleArgumentCaptor.getValue());
verify(taskExecutor).executeTask(trackerNotificationThread);
}

@Test
void test_message_consumer() throws JMSException, IOException {
TrackerSideEffectDataBundle bundle =
TrackerSideEffectDataBundle.builder().accessedBy("test-user").build();

when(textMessage.getText()).thenReturn("text");
when(objectFactory.getObject()).thenReturn(trackerNotificationThread);
doNothing().when(taskExecutor).executeTask(any(Runnable.class));

when(renderService.fromJson(anyString(), eq(TrackerSideEffectDataBundle.class)))
.thenReturn(null);
trackerNotificationMessageManager.consume(textMessage);

verify(taskExecutor, times(0)).executeTask(any(Runnable.class));

doReturn(bundle)
.when(renderService)
.fromJson(anyString(), eq(TrackerSideEffectDataBundle.class));
trackerNotificationMessageManager.consume(textMessage);

Mockito.verify(taskExecutor).executeTask(runnableCaptor.capture());
void testSendNotifications_WithNullBundle() {
trackerNotificationMessageManager.sendNotifications(null);

assertTrue(runnableCaptor.getValue() instanceof TrackerNotificationThread);
verifyNoInteractions(taskExecutor);
}
}
Loading
Loading