diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/BaseMessageManager.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/BaseMessageManager.java deleted file mode 100644 index 1811e90cef05..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/BaseMessageManager.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -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.common.AsyncTaskExecutor; -import org.hisp.dhis.common.CodeGenerator; -import org.hisp.dhis.render.RenderService; -import org.springframework.stereotype.Component; - -/** - * @author Zubair Asghar - */ -@Component -public abstract class BaseMessageManager { - private final MessageManager messageManager; - - private final AsyncTaskExecutor taskExecutor; - - private final RenderService renderService; - - public BaseMessageManager( - MessageManager messageManager, AsyncTaskExecutor taskExecutor, RenderService renderService) { - this.messageManager = messageManager; - this.taskExecutor = taskExecutor; - this.renderService = renderService; - } - - public String addJob(TrackerSideEffectDataBundle sideEffectDataBundle) { - String jobId = CodeGenerator.generateUid(); - sideEffectDataBundle.setJobId(jobId); - - messageManager.sendQueue(getTopic(), sideEffectDataBundle); - - return jobId; - } - - public void executeJob(Runnable runnable) { - taskExecutor.executeTask(runnable); - } - - public TrackerSideEffectDataBundle toBundle(TextMessage message) - throws JMSException, IOException { - String payload = message.getText(); - - return renderService.fromJson(payload, TrackerSideEffectDataBundle.class); - } - - public abstract String getTopic(); -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/TrackerNotificationMessageManager.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/TrackerNotificationMessageManager.java index 2c106ac80c53..3859a212ef44 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/TrackerNotificationMessageManager.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/TrackerNotificationMessageManager.java @@ -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; /** @@ -46,29 +40,12 @@ * @author Zubair Asghar */ @Component -public class TrackerNotificationMessageManager extends BaseMessageManager { +@RequiredArgsConstructor +public class TrackerNotificationMessageManager { private final ObjectFactory trackerNotificationThreadObjectFactory; + private final AsyncTaskExecutor taskExecutor; - public TrackerNotificationMessageManager( - MessageManager messageManager, - AsyncTaskExecutor taskExecutor, - RenderService renderService, - ObjectFactory 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; } @@ -84,6 +61,6 @@ public void consume(TextMessage message) throws JMSException, IOException { notificationThread.setSideEffectDataBundle(bundle); - executeJob(notificationThread); + taskExecutor.executeTask(notificationThread); } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/TrackerRuleEngineMessageManager.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/TrackerRuleEngineMessageManager.java index 27e155438d2e..245de02950fc 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/TrackerRuleEngineMessageManager.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/job/TrackerRuleEngineMessageManager.java @@ -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; /** @@ -46,29 +40,12 @@ * @author Zubair Asghar */ @Component -public class TrackerRuleEngineMessageManager extends BaseMessageManager { +@RequiredArgsConstructor +public class TrackerRuleEngineMessageManager { private final ObjectFactory trackerRuleEngineThreadObjectFactory; + private final AsyncTaskExecutor taskExecutor; - public TrackerRuleEngineMessageManager( - MessageManager messageManager, - AsyncTaskExecutor taskExecutor, - RenderService renderService, - ObjectFactory 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; } @@ -83,6 +60,6 @@ public void consume(TextMessage message) throws JMSException, IOException { notificationThread.setSideEffectDataBundle(bundle); - executeJob(notificationThread); + taskExecutor.executeTask(notificationThread); } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/sideeffect/NotificationSideEffectHandlerService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/sideeffect/NotificationSideEffectHandlerService.java index fbd10ba7c23c..4a936f78a500 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/sideeffect/NotificationSideEffectHandlerService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/sideeffect/NotificationSideEffectHandlerService.java @@ -43,7 +43,7 @@ public class NotificationSideEffectHandlerService implements SideEffectHandlerSe @Override public void handleSideEffect(TrackerSideEffectDataBundle sideEffectDataBundle) { - notificationMessageManager.addJob(sideEffectDataBundle); + notificationMessageManager.sendNotifications(sideEffectDataBundle); } @Override diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/sideeffect/RuleEngineSideEffectHandlerService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/sideeffect/RuleEngineSideEffectHandlerService.java index 9ee8a3c45e7d..30a35ccaf215 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/sideeffect/RuleEngineSideEffectHandlerService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/sideeffect/RuleEngineSideEffectHandlerService.java @@ -43,7 +43,7 @@ public class RuleEngineSideEffectHandlerService implements SideEffectHandlerServ @Override public void handleSideEffect(TrackerSideEffectDataBundle sideEffectDataBundle) { - ruleEngineMessageManager.addJob(sideEffectDataBundle); + ruleEngineMessageManager.sendRuleEngineNotifications(sideEffectDataBundle); } @Override diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/job/TrackerNotificationMessageManagerTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/job/TrackerNotificationMessageManagerTest.java index 2ce5ebc6ca45..6d10ccfbe170 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/job/TrackerNotificationMessageManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/job/TrackerNotificationMessageManagerTest.java @@ -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; @@ -60,63 +44,28 @@ */ @ExtendWith(MockitoExtension.class) class TrackerNotificationMessageManagerTest { - - @Mock private ObjectFactory 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 topicCaptor; - - @Captor private ArgumentCaptor bundleArgumentCaptor; + @Mock private ObjectFactory objectFactory; - @Captor private ArgumentCaptor 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); } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/job/TrackerRuleEngineMessageManagerTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/job/TrackerRuleEngineMessageManagerTest.java index c26e2fccd1b9..e3eaffe86ccc 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/job/TrackerRuleEngineMessageManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/job/TrackerRuleEngineMessageManagerTest.java @@ -27,32 +27,15 @@ */ package org.hisp.dhis.tracker.job; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -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; @@ -64,61 +47,26 @@ class TrackerRuleEngineMessageManagerTest { @Mock private ObjectFactory objectFactory; - @Mock private MessageManager messageManager; - - @Mock private RenderService renderService; - - @Mock private TextMessage textMessage; - @Mock private AsyncTaskExecutor taskExecutor; @Mock private TrackerRuleEngineThread trackerRuleEngineThread; @InjectMocks private TrackerRuleEngineMessageManager trackerRuleEngineMessageManager; - @Captor private ArgumentCaptor topicCaptor; - - @Captor private ArgumentCaptor bundleArgumentCaptor; - - @Captor private ArgumentCaptor runnableArgumentCaptor; - @Test - void test_add_job() { - doNothing().when(messageManager).sendQueue(anyString(), any(TrackerSideEffectDataBundle.class)); - + void shouldSendRuleEngineNotification() { + when(objectFactory.getObject()).thenReturn(trackerRuleEngineThread); TrackerSideEffectDataBundle dataBundle = TrackerSideEffectDataBundle.builder().build(); - trackerRuleEngineMessageManager.addJob(dataBundle); + trackerRuleEngineMessageManager.sendRuleEngineNotifications(dataBundle); - Mockito.verify(messageManager).sendQueue(topicCaptor.capture(), bundleArgumentCaptor.capture()); - - assertNotNull(topicCaptor.getValue()); - assertEquals(Topics.TRACKER_IMPORT_RULE_ENGINE_TOPIC_NAME, topicCaptor.getValue()); - assertEquals(dataBundle, bundleArgumentCaptor.getValue()); + verify(taskExecutor).executeTask(trackerRuleEngineThread); } @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(trackerRuleEngineThread); - doNothing().when(taskExecutor).executeTask(any(Runnable.class)); - - when(renderService.fromJson(anyString(), eq(TrackerSideEffectDataBundle.class))) - .thenReturn(null); - trackerRuleEngineMessageManager.consume(textMessage); - - verify(taskExecutor, times(0)).executeTask(any(Runnable.class)); - - doReturn(bundle) - .when(renderService) - .fromJson(anyString(), eq(TrackerSideEffectDataBundle.class)); - trackerRuleEngineMessageManager.consume(textMessage); - - Mockito.verify(taskExecutor).executeTask(runnableArgumentCaptor.capture()); + void testSendRuleEngineNotifications_WithNullBundle() { + trackerRuleEngineMessageManager.sendRuleEngineNotifications(null); - assertTrue(runnableArgumentCaptor.getValue() instanceof TrackerRuleEngineThread); + verifyNoInteractions(taskExecutor); } }