Skip to content

Commit

Permalink
Adding unit testing for service
Browse files Browse the repository at this point in the history
  • Loading branch information
chris.ditcher authored and chris.ditcher committed Aug 28, 2024
1 parent cc07256 commit 3e948ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.EducGradTraxApiApplication;
import ca.bc.gov.educ.api.trax.mapper.EventHistoryMapperImpl;
import ca.bc.gov.educ.api.trax.messaging.NatsConnection;
import ca.bc.gov.educ.api.trax.messaging.jetstream.Publisher;
import ca.bc.gov.educ.api.trax.messaging.jetstream.Subscriber;
Expand All @@ -20,7 +21,7 @@
import redis.clients.jedis.JedisCluster;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {EducGradTraxApiApplication.class})
@SpringBootTest(classes = {EducGradTraxApiApplication.class, EventHistoryMapperImpl.class, EventHistoryMapperImpl.class})
@ActiveProfiles("test")
@AutoConfigureMockMvc
public abstract class BaseReplicationServiceTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package ca.bc.gov.educ.api.trax.service;

import ca.bc.gov.educ.api.trax.exception.TraxAPIRuntimeException;
import ca.bc.gov.educ.api.trax.model.entity.EventEntity;
import ca.bc.gov.educ.api.trax.model.entity.EventHistoryEntity;
import ca.bc.gov.educ.api.trax.repository.EventHistoryRepository;
import ca.bc.gov.educ.api.trax.repository.EventRepository;
import ca.bc.gov.educ.api.trax.support.TestUtils;
import ca.bc.gov.educ.api.trax.util.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class EventHistoryServiceTest extends BaseReplicationServiceTest {
Expand Down Expand Up @@ -61,4 +67,18 @@ public void purgeOldEventAndEventHistoryRecords_givenNoExceptionAndNewRecord_sho
Optional<EventHistoryEntity> eventHistoryThatShouldNotBePurgedAlso = eventHistoryRepository.findById(eventHistory.getId());
Assert.assertTrue(eventHistoryThatShouldNotBePurgedAlso.isPresent() && eventThatShouldNotBePurgedOptional.isPresent());
}

@Test
public void setSpecificationAndSortCriteria_givenValidData_shouldReturnOk() throws TraxAPIRuntimeException{
String sort = "{ \"schoolNumber\": \"ASC\" }";
String searchParams = "[{\"condition\":null,\"searchCriteriaList\":[{\"key\":\"openedDate\",\"operation\":\"lte\",\"value\":\"2024-08-26T09:05:51.782\",\"valueType\":\"DATE_TIME\",\"condition\":\"AND\"},{\"key\":\"closedDate\",\"operation\":\"eq\",\"value\":null,\"valueType\":\"STRING\",\"condition\":\"AND\"}]}]";
Specification<EventHistoryEntity> eventHistorySpecs = eventHistoryService.setSpecificationAndSortCriteria(sort, searchParams, JsonUtil.mapper, new ArrayList<>());
Assert.assertNotNull(eventHistorySpecs);
}

@Test
public void setSpecificationAndSortCriteria_givenInvalisData_shouldThrowTraxAPIRuntimeException() {
final List<Sort.Order> sorts = new ArrayList<>();
Assert.assertThrows(TraxAPIRuntimeException.class, () -> eventHistoryService.setSpecificationAndSortCriteria(null, "{ \"bunkjunk\": \"ASC\" }", JsonUtil.mapper, sorts));
}
}

0 comments on commit 3e948ca

Please sign in to comment.