Skip to content

Commit

Permalink
test: increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanta2018 committed May 14, 2024
1 parent edddd26 commit fc67e0c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 151 deletions.
6 changes: 4 additions & 2 deletions src/main/java/uk/nhs/tis/sync/job/PostFundingSyncJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ public void run(@Nullable String params) {
}

protected void runSyncJob() {
if (mainStopWatch != null) {
if (isCurrentlyRunning()) {
LOG.info("Sync job [{}] already running, exiting this execution", JOB_NAME);
return;
}
CompletableFuture.runAsync(this::doDataSync);
CompletableFuture.runAsync(() -> {
doDataSync();
});
}

private void doDataSync() {
Expand Down
30 changes: 0 additions & 30 deletions src/main/java/uk/nhs/tis/sync/job/PostFundingSyncJobTemplate.java

This file was deleted.

119 changes: 0 additions & 119 deletions src/test/java/uk/nhs/tis/sync/job/PostFundingSyncJobTemplateTest.java

This file was deleted.

72 changes: 72 additions & 0 deletions src/test/java/uk/nhs/tis/sync/job/PostFundingSyncJobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.transformuk.hee.tis.tcs.api.enumeration.Status;
import com.transformuk.hee.tis.tcs.service.model.Post;
import com.transformuk.hee.tis.tcs.service.model.PostFunding;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
Expand All @@ -18,17 +25,32 @@
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;
import uk.nhs.tis.sync.event.JobExecutionEvent;

@ExtendWith(MockitoExtension.class)
class PostFundingSyncJobTest {

@Mock
private EntityManager entityManager;
@Mock
ApplicationEventPublisher applicationEventPublisher;
@Mock
private EntityManagerFactory entityManagerFactory;
@Mock
private EntityTransaction transaction;
@Mock
private Query query;

private PostFundingSyncJob postFundingSyncJob;

Expand All @@ -37,6 +59,7 @@ class PostFundingSyncJobTest {
@BeforeEach
void setUp() {
postFundingSyncJob = new PostFundingSyncJob();
postFundingSyncJob.entityManagerFactory = entityManagerFactory;
}

@Test
Expand Down Expand Up @@ -125,4 +148,53 @@ void testShouldBeSuccessfulWithHandleDataMethod() {
verify(entityManager, times(1)).persist(post);
verify(entityManager, times(1)).flush();
}

@Test
void testDoDataSync()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
when(entityManagerFactory.createEntityManager()).thenReturn(entityManager);
when(entityManager.getTransaction()).thenReturn(transaction);
when(transaction.isActive()).thenReturn(false);
when(entityManager.createNativeQuery(ArgumentMatchers.any())).thenReturn(query);
when(query.setParameter(eq("lastPostId"), ArgumentMatchers.any())).thenReturn(query);
when(query.getResultList())
.thenReturn(Arrays.asList(BigInteger.ONE, BigInteger.valueOf(2), BigInteger.valueOf(3)))
.thenReturn(new ArrayList<>());

Method method = PostFundingSyncJob.class.getDeclaredMethod("doDataSync");
method.setAccessible(true);
method.invoke(postFundingSyncJob);

verify(query, times(2)).getResultList();
}

@Test
void testPublishJobExecutionEvent()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
postFundingSyncJob.applicationEventPublisher = applicationEventPublisher;

JobExecutionEvent event = new JobExecutionEvent(this, "Post funding sync job");

Method method = PostFundingSyncJob.class
.getDeclaredMethod("publishJobexecutionEvent", JobExecutionEvent.class);
method.setAccessible(true);
method.invoke(postFundingSyncJob, event);

ArgumentCaptor<JobExecutionEvent> eventCaptor = ArgumentCaptor
.forClass(JobExecutionEvent.class);
verify(applicationEventPublisher, times(1)).publishEvent(eventCaptor.capture());

JobExecutionEvent capturedEvent = eventCaptor.getValue();
assertThat(capturedEvent.getMessage(), containsString("Post funding sync job"));
}

@Test
public void testRun() {
PostFundingSyncJob spyJob = spy(postFundingSyncJob);
doNothing().when(spyJob).postFundingSyncJob();

spyJob.run(null);

verify(spyJob).postFundingSyncJob();
}
}

0 comments on commit fc67e0c

Please sign in to comment.