Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zubaira committed Dec 19, 2024
1 parent bc33461 commit a83e78c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 121 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -64,61 +47,26 @@ class TrackerRuleEngineMessageManagerTest {

@Mock private ObjectFactory<TrackerRuleEngineThread> 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<String> topicCaptor;

@Captor private ArgumentCaptor<TrackerSideEffectDataBundle> bundleArgumentCaptor;

@Captor private ArgumentCaptor<Runnable> 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);
}
}

0 comments on commit a83e78c

Please sign in to comment.