Skip to content

Commit

Permalink
chore: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanta2018 committed May 2, 2024
1 parent 4cffc94 commit fdb3ba1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 61 deletions.
101 changes: 41 additions & 60 deletions src/test/java/uk/nhs/tis/sync/job/PostFundingSyncJobTemplateTest.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
package uk.nhs.tis.sync.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.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.format.DateTimeFormatter;
import java.math.BigInteger;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.math.BigInteger;
import java.time.LocalDate;
import java.util.*;
import org.springframework.context.ApplicationEventPublisher;
import uk.nhs.tis.sync.event.JobExecutionEvent;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.mockito.ArgumentCaptor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class PostFundingSyncJobTemplateTest {

@Mock
Expand All @@ -46,43 +47,30 @@ public class PostFundingSyncJobTemplateTest {
@Mock
private Query query;

@InjectMocks
private ConcretePostFundingSyncJobTemplate postFundingSyncJobTemplate;

private static final String BASE_QUERY = "SELECT DISTINCT p.id FROM Post p "
+ " JOIN ( "
+ " SELECT postId "
+ " FROM PostFunding "
+ " WHERE postId > :lastPostId "
+ " AND startDate IS NOT NULL "
+ " AND (endDate = ':endDate' OR endDate IS NULL) "
+ " GROUP BY postId "
+ " ) pf ON p.id = pf.postId "
+ " ORDER BY p.id LIMIT :pageSize ";

@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
postFundingSyncJobTemplate = new ConcretePostFundingSyncJobTemplate();
postFundingSyncJobTemplate.entityManagerFactory = entityManagerFactory;

when(entityManagerFactory.createEntityManager()).thenReturn(entityManager);
when(entityManager.getTransaction()).thenReturn(transaction);
when(transaction.isActive()).thenReturn(false);
}

@Test
public void testGetFailureMessage() {
String errorMessage = "Failed!";
Throwable exception = new RuntimeException(errorMessage);
assertEquals("<!channel> Sync [TestJob] failed with exception [" + errorMessage + "].", postFundingSyncJobTemplate.getFailureMessage(Optional.of("TestJob"), exception));
assertEquals("<!channel> Sync [PostFundingSyncJobTemplate] failed with exception [" + errorMessage + "].", postFundingSyncJobTemplate.getFailureMessage(Optional.empty(), exception));
assertEquals("<!channel> Sync [TestJob] failed with exception [" + errorMessage + "].",
postFundingSyncJobTemplate.getFailureMessage(Optional.of("TestJob"), exception));
assertEquals("<!channel> Sync [ConcretePostFundingSyncJobTemplate] failed with exception ["
+ errorMessage + "].",
postFundingSyncJobTemplate.getFailureMessage(Optional.empty(), exception));
}

@Test
public void testDoDataSync() throws ExecutionException, InterruptedException, NoSuchMethodException, InvocationTargetException, IllegalAccessException{

String dateOption = "2025-05-01";
String dateOption = "2024-05-01";
LocalDate dateOfChange = LocalDate.parse(dateOption);
String queryString = "SELECT DISTINCT p.id FROM Post p "
+ " JOIN ( "
Expand All @@ -98,20 +86,25 @@ public void testDoDataSync() throws ExecutionException, InterruptedException, No
when(entityManagerFactory.createEntityManager()).thenReturn(entityManager);
when(entityManager.getTransaction()).thenReturn(transaction);
when(transaction.isActive()).thenReturn(false);
when(entityManager.createQuery(queryString)).thenReturn(query);
when(query.setParameter("date", dateOfChange)).thenReturn(query);
when(entityManager.createNativeQuery(any())).thenReturn(query);
when(query.setParameter(eq("lastPostId"), any())).thenReturn(query);
when(query.getResultList())
.thenReturn(Arrays.asList(BigInteger.ONE, BigInteger.valueOf(2), BigInteger.valueOf(3)))
.thenReturn(new ArrayList<>()); // no more results
.thenReturn(
Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3)))
.thenReturn(new ArrayList<>());

// Use reflection to invoke the private method
Method method = PostFundingSyncJobTemplate.class.getDeclaredMethod("doDataSync", String.class);
method.setAccessible(true);
method.invoke(postFundingSyncJobTemplate, dateOption);

verify(query, times(2)).getResultList();
verify(entityManagerFactory, times(2)).createEntityManager();
verify(entityManager, times(2)).getTransaction();
verify(transaction, times(2)).begin();
verify(query, times(1)).setParameter("lastPostId", 0L);
verify(query, times(2)).getResultList(); // Ensure getResultList is invoked twice
verify(query, times(0)).setParameter("date", dateOfChange);
}

@Test
Expand All @@ -130,30 +123,18 @@ public void testCollectData() {
}

private static class ConcretePostFundingSyncJobTemplate extends PostFundingSyncJobTemplate<Object> {

@Override
protected int convertData(Set<Object> entitiesToSave, List<Long> entityData,
EntityManager entityManager) {
return 0;
}

@Override
protected String buildQueryForDate(LocalDate dateOfChange) {
return null;
}


@Override
protected void handleData(Set<Object> dataToSave, EntityManager entityManager) {

}

protected void handleData(Set<Object> dataToSave, EntityManager entityManager) { }
@Override
public void run(@Nullable String params) {

}
public void run(@Nullable String params) { }
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class PostFundingSyncJobTest {
private PostFundingSyncJob postFundingSyncJob;

@BeforeEach
public void setUp() {
void setUp() {
postFundingSyncJob = new PostFundingSyncJob();
}

Expand Down

0 comments on commit fdb3ba1

Please sign in to comment.