Skip to content

Commit

Permalink
fixing unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ndduc01 committed Feb 3, 2025
1 parent 976e50f commit 8c58b2f
Show file tree
Hide file tree
Showing 23 changed files with 144 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public class KafkaConsumerService {
@Value("${kafka.xml-conversion.topic}")
private String convertedToXmlTopic = "xml_converted";

@Value("${kafka.rti.topic}")
private String rtiTopic = "dp_elr_unprocessed";

@Value("${kafka.raw.topic}")
private String rawTopic = "elr_raw";

Expand Down Expand Up @@ -262,7 +265,7 @@ public void handleMessageForElrXml(String message,
iReportStatusRepository.save(reportStatusIdData);

if (dataProcessingApplied) {
kafkaProducerService.sendMessageAfterConvertedToXml(String.valueOf(nbsInterfaceModel.getNbsInterfaceUid()), "dp_elr_unprocessed", 0);
kafkaProducerService.sendMessageAfterConvertedToXml(String.valueOf(nbsInterfaceModel.getNbsInterfaceUid()), rtiTopic, 0);
}
else {
kafkaProducerService.sendMessageAfterConvertedToXml(nbsInterfaceModel.getNbsInterfaceUid().toString(), convertedToXmlTopic, 0);
Expand Down Expand Up @@ -538,7 +541,7 @@ public void xmlConversionHandlerProcessing(String message, String operation, Str
}

if (dataProcessingApplied) {
kafkaProducerService.sendMessageAfterConvertedToXml(nbsInterfaceModel.getNbsInterfaceUid().toString(), "dp_elr_unprocessed", 0); //NOSONAR
kafkaProducerService.sendMessageAfterConvertedToXml(nbsInterfaceModel.getNbsInterfaceUid().toString(), rtiTopic, 0); //NOSONAR
} else {
kafkaProducerService.sendMessageAfterConvertedToXml(nbsInterfaceModel.getNbsInterfaceUid().toString(), convertedToXmlTopic, 0);
}
Expand Down
2 changes: 2 additions & 0 deletions data-ingestion-service/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ kafka:
topic: xml_prep
elr-duplicate:
topic: elr_duplicate
rti:
topic: dp_elr_unprocessed
topics: elr_raw,elr_validated
retry:
suffix: _retry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import gov.cdc.dataprocessing.repository.nbs.odse.repos.act.ActRelationshipRepository;
import gov.cdc.dataprocessing.service.model.auth_user.AuthUserProfileInfo;
import gov.cdc.dataprocessing.utilities.auth.AuthUtil;
import gov.cdc.dataprocessing.utilities.component.jdbc.DataModifierReposJdbc;
import gov.cdc.dataprocessing.utilities.time.TimeStampUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -27,6 +28,8 @@
class ActRelationshipServiceTest {
@Mock
private ActRelationshipRepository actRelationshipRepository;
@Mock
private DataModifierReposJdbc dataModifierReposJdbc;
@InjectMocks
private ActRelationshipService actRelationshipService;
@Mock
Expand All @@ -46,7 +49,7 @@ void setUp() {

@AfterEach
void tearDown() {
Mockito.reset(actRelationshipRepository, authUtil);
Mockito.reset(dataModifierReposJdbc, actRelationshipRepository, authUtil);
}

@Test
Expand Down Expand Up @@ -199,7 +202,7 @@ void saveActRelationship_Success_Delete() throws DataProcessingException {

actRelationshipService.saveActRelationship(dto);

verify(actRelationshipRepository, times(1)).deleteActRelationshipByPk(any(), any(), any());
verify(dataModifierReposJdbc, times(1)).deleteActRelationshipByPk(any(), any(), any());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ void storePageAnswer_Exception() {
.thenThrow(new RuntimeException("TEST"));


DataProcessingException thrown = assertThrows(DataProcessingException.class, () -> {
RuntimeException thrown = assertThrows(RuntimeException.class, () -> {
answerService.storePageAnswer(pageContainer, observationDto);
});

Expand Down Expand Up @@ -518,11 +518,11 @@ void getNbsAnswerAndAssociation_Exception() {

when(nbsActEntityRepository.getNbsActEntitiesByActUid(any())).thenThrow(new RuntimeException("TEST"));

DataProcessingException thrown = assertThrows(DataProcessingException.class, () -> {
RuntimeException thrown = assertThrows(RuntimeException.class, () -> {
answerService.getNbsAnswerAndAssociation(null);
});

assertEquals("InterviewAnswerRootDAOImpl:answerCollection- could not be returned",thrown.getMessage());
assertEquals("TEST",thrown.getMessage());

}

Expand Down Expand Up @@ -566,7 +566,7 @@ void storeAnswerDTCollection_Test_Exp() {
when(nbsAnswerRepository.save(any())).thenThrow(new RuntimeException());


DataProcessingException thrown = assertThrows(DataProcessingException.class, () -> {
RuntimeException thrown = assertThrows(RuntimeException.class, () -> {
answerService.storeAnswerDTCollection(answerDTColl, interfaceDt);
});

Expand All @@ -577,7 +577,7 @@ void storeAnswerDTCollection_Test_Exp() {
void delete_Exp() {
ObservationDto observationDto = new ObservationDto();
when(nbsAnswerRepository.getPageAnswerByActUid(any())).thenThrow(new RuntimeException());
DataProcessingException thrown = assertThrows(DataProcessingException.class, () -> {
RuntimeException thrown = assertThrows(RuntimeException.class, () -> {
answerService.delete(observationDto);
});

Expand All @@ -595,7 +595,7 @@ void insertAnswerHistoryDTCollection_Test_1() {
when(nbsAnswerHistRepository.save(any())).thenThrow(new RuntimeException());


DataProcessingException thrown = assertThrows(DataProcessingException.class, () -> {
RuntimeException thrown = assertThrows(RuntimeException.class, () -> {
answerService.insertAnswerHistoryDTCollection(anCol);
});

Expand Down Expand Up @@ -629,7 +629,7 @@ void insertPageEntityHistoryDTCollection_Exp() {

when(nbsActEntityHistRepository.save(any())).thenThrow(new RuntimeException());

DataProcessingException thrown = assertThrows(DataProcessingException.class, () -> {
RuntimeException thrown = assertThrows(RuntimeException.class, () -> {
answerService.insertPageEntityHistoryDTCollection(nbsCaseEntityDTColl, oldrootDTInterface);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

class AuthUserServiceTest {
@Mock
private AuthUserRepository authUserRepository;
@Mock
private CustomAuthUserRepository customAuthUserRepository;
@Mock
private JdbcTemplate jdbcTemplateOdse;
@InjectMocks
private AuthUserService authUserService;
@Mock
Expand All @@ -51,13 +58,18 @@ void tearDown() {

@Test
void getAuthUserInfo_Success() throws DataProcessingException {

String authUserId = "Test";
var authUser = new AuthUser();
when(authUserRepository.findAuthUserByUserId(authUserId)).thenReturn(Optional.of(authUser));
var roleCol = new ArrayList<AuthUserRealizedRole>();
var role = new AuthUserRealizedRole();
roleCol.add(role);
when(customAuthUserRepository.getAuthUserRealizedRole(authUserId)).thenReturn(roleCol);
when(jdbcTemplateOdse.query(anyString(), any(Object[].class), (ResultSetExtractor<Object>) any()))
.thenAnswer(invocation -> {
return Optional.of(authUser);
});

var test = authUserService.getAuthUserInfo(authUserId);

Expand All @@ -69,11 +81,15 @@ void getAuthUserInfo_Success() throws DataProcessingException {
void getAuthUserInfo_Exception() {
String authUserId = "Test";
when(authUserRepository.findAuthUserByUserId(authUserId)).thenReturn(Optional.empty());
when(jdbcTemplateOdse.query(anyString(), any(Object[].class), (ResultSetExtractor<Object>) any()))
.thenAnswer(invocation -> {
return Optional.of(Optional.empty());
});

DataProcessingException thrown = assertThrows(DataProcessingException.class, () -> {
ClassCastException thrown = assertThrows(ClassCastException.class, () -> {
authUserService.getAuthUserInfo(authUserId);
});
assertEquals("Auth User Not Found", thrown.getMessage());
assertNotNull(thrown.getMessage());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import gov.cdc.dataprocessing.service.interfaces.uid_generator.IOdseIdGeneratorWCacheService;
import gov.cdc.dataprocessing.service.model.auth_user.AuthUserProfileInfo;
import gov.cdc.dataprocessing.utilities.auth.AuthUtil;
import gov.cdc.dataprocessing.utilities.component.jdbc.DataModifierReposJdbc;
import gov.cdc.dataprocessing.utilities.time.TimeStampUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -35,6 +36,7 @@
import java.util.*;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

Expand All @@ -46,6 +48,8 @@ class EntityLocatorParticipationServiceTest {
@Mock
private PostalLocatorRepository postalLocatorRepository;
@Mock
private DataModifierReposJdbc dataModifierReposJdbc;
@Mock
private PhysicalLocatorRepository physicalLocatorRepository;
@Mock
private IOdseIdGeneratorWCacheService iOdseIdGeneratorWCacheService;
Expand Down Expand Up @@ -77,7 +81,7 @@ void setUp() {
@AfterEach
void tearDown() {
Mockito.reset(entityLocatorParticipationRepository, teleLocatorRepository, postalLocatorRepository, physicalLocatorRepository,
iOdseIdGeneratorWCacheService, authUtil, personRepository);
iOdseIdGeneratorWCacheService, authUtil, personRepository, dataModifierReposJdbc);
}

@Test
Expand Down Expand Up @@ -373,7 +377,7 @@ void testDeleteEntityLocatorParticipationWhenPersonResIsEmpty() {

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, 1L);

verify(postalLocatorRepository).deletePostalLocatorById(any());
verify(dataModifierReposJdbc).deletePostalLocatorById(any());

}

Expand All @@ -387,7 +391,7 @@ void testDeleteEntityLocatorParticipationWhenPostalRevisionIsEmpty() {

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, 1L);

verify(postalLocatorRepository).deletePostalLocatorById(any());
verify(dataModifierReposJdbc).deletePostalLocatorById(any());

}

Expand All @@ -402,7 +406,7 @@ void testDeleteEntityLocatorParticipationWhenEntityMprEntityResIsEmpty() {

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, 1L);

verify(postalLocatorRepository).deletePostalLocatorById(any());
verify(dataModifierReposJdbc).deletePostalLocatorById(any());
}

@Test
Expand All @@ -417,7 +421,7 @@ void testDeleteEntityLocatorParticipationWhenEntityMprResIsEmpty() {

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, 1L);

verify(postalLocatorRepository).deletePostalLocatorById(any());
verify(dataModifierReposJdbc).deletePostalLocatorById(any());
}
private EntityLocatorParticipationDto createEntityLocatorParticipationDto(String classCd, boolean itDelete, String useCd) {
EntityLocatorParticipationDto dto = new EntityLocatorParticipationDto();
Expand All @@ -442,8 +446,8 @@ void testDeleteEntityLocatorParticipationWhenConditionsAreMet() {

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, 1L);

verify(postalLocatorRepository).deletePostalLocatorById(any());
verify(entityLocatorParticipationRepository).deleteLocatorById(any(), any());
verify(dataModifierReposJdbc).deletePostalLocatorById(any());
verify(dataModifierReposJdbc).deleteLocatorById(any(), any());
}

@Test
Expand All @@ -457,14 +461,14 @@ void testDeleteEntityLocatorParticipationWhenExceptionThrown() {
dto.setClassCd(NEDSSConstant.POSTAL);
dto.setItDelete(true);
dto.setUseCd("BIR");
dto.setLocatorUid(1L);
locatorCollectionMock.add(dto);

doThrow(new RuntimeException()).when(postalLocatorRepository).deletePostalLocatorById(any());
doThrow(new RuntimeException()).when(dataModifierReposJdbc).deletePostalLocatorById(any());

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, 1L);
assertThrows(RuntimeException.class, () -> entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, 1L));

verify(postalLocatorRepository).deletePostalLocatorById(any());
verify(entityLocatorParticipationRepository, never()).deleteLocatorById(any(), any());
verify(dataModifierReposJdbc, never()).deleteLocatorById(any(), any());
}

@Test
Expand Down Expand Up @@ -505,8 +509,8 @@ void testDeleteEntityLocatorParticipationWithSpecificScenario() {

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, patientUid);

verify(postalLocatorRepository, times(2)).deletePostalLocatorById(1L);
verify(entityLocatorParticipationRepository).deleteLocatorById(parentUid, 1L);
verify(dataModifierReposJdbc, times(2)).deletePostalLocatorById(1L);
verify(dataModifierReposJdbc).deleteLocatorById(parentUid, 1L);
}

@Test
Expand Down Expand Up @@ -543,11 +547,11 @@ void testDeleteEntityLocatorParticipationWhenExceptionThrownInDeleteOperations()
when(entityLocatorParticipationRepository.findLocatorUidsByEntityUid(parentUid)).thenReturn(Optional.of(List.of(1L)));
when(postalLocatorRepository.findByPostalLocatorUids(anyList())).thenReturn(Optional.of(List.of(postalLocator)));

doThrow(new RuntimeException()).when(postalLocatorRepository).deletePostalLocatorById(anyLong());
doThrow(new RuntimeException()).when(dataModifierReposJdbc).deletePostalLocatorById(anyLong());

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, patientUid);
assertThrows(RuntimeException.class, () -> entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, patientUid));

verify(entityLocatorParticipationRepository, never()).deleteLocatorById(parentUid, 1L);
verify(dataModifierReposJdbc, never()).deleteLocatorById(parentUid, 1L);
}

@Test
Expand Down Expand Up @@ -586,7 +590,7 @@ void testDeleteEntityLocatorParticipationWhenBirCheckIsEmpty() {

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, patientUid);

verify(postalLocatorRepository).deletePostalLocatorById(any());
verify(dataModifierReposJdbc).deletePostalLocatorById(any());
}

@Test
Expand Down Expand Up @@ -628,6 +632,6 @@ void testDeleteEntityLocatorParticipationWhenElseConditionIsMet() {

entityLocatorParticipationService.deleteEntityLocatorParticipation(locatorCollectionMock, patientUid);

verify(postalLocatorRepository, times(0)).updatePostalStatus(any(), any());
verify(postalLocatorRepository, times(0)).save(any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void testProgAndJurisdictionAggregationAsync_Exception() throws DataProcessingEx
// Mock behavior of services to throw exception
doThrow(new DataProcessingException("Error")).when(programAreaService).getProgramArea(any(), any(), any());

assertThrows(RuntimeException.class, () -> managerAggregationService.progAndJurisdictionAggregation(labResult, edxLabInformationDto, personAggContainer, organizationContainer));
assertThrows(DataProcessingException.class, () -> managerAggregationService.progAndJurisdictionAggregation(labResult, edxLabInformationDto, personAggContainer, organizationContainer));

}

Expand Down
Loading

0 comments on commit 8c58b2f

Please sign in to comment.