From a83e78c4b918580520262b721a67ac23c617a34d Mon Sep 17 00:00:00 2001 From: zubaira Date: Thu, 19 Dec 2024 10:04:00 +0100 Subject: [PATCH] fix unit test --- ...TrackerNotificationMessageManagerTest.java | 71 +++---------------- .../TrackerRuleEngineMessageManagerTest.java | 68 +++--------------- 2 files changed, 18 insertions(+), 121 deletions(-) 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); } }