Skip to content

Commit

Permalink
fixing case sensitive issue with the header (#209)
Browse files Browse the repository at this point in the history
* fixing upper case header

* clnea up
  • Loading branch information
ndduc01 authored Aug 20, 2024
1 parent aaec8f7 commit 0dc147d
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gov.cdc.dataingestion.constant;

public class MessageType {
public static final String HL7_ELR = "HL7";
public static final String XML_ELR = "HL7-XML";
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.io.StringWriter;
import java.util.Optional;

import static gov.cdc.dataingestion.constant.MessageType.XML_ELR;
import static gov.cdc.dataingestion.share.helper.TimeStampHelper.getCurrentTimeStamp;

@Service
Expand Down Expand Up @@ -251,7 +252,7 @@ public void handleMessageForElrXml(String message,
log.debug("Saved Elr xml to NBS_interface table with uid: {}", nbsInterfaceModel.getNbsInterfaceUid());

ReportStatusIdData reportStatusIdData = new ReportStatusIdData();
reportStatusIdData.setRawMessageId(messageId.replaceAll("HL7-xml_", ""));
reportStatusIdData.setRawMessageId(messageId.replaceAll(XML_ELR + "_", ""));
reportStatusIdData.setNbsInterfaceUid(nbsInterfaceModel.getNbsInterfaceUid());
reportStatusIdData.setCreatedBy("elr_raw_xml");
reportStatusIdData.setUpdatedBy("elr_raw_xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import static gov.cdc.dataingestion.constant.MessageType.HL7_ELR;
import static gov.cdc.dataingestion.constant.MessageType.XML_ELR;

@RestController
@RequiredArgsConstructor
@SecurityRequirement(name = "bearer-key")
Expand Down Expand Up @@ -62,18 +65,20 @@ public ResponseEntity<String> save(@RequestBody final String payload, @RequestHe
@RequestHeader(name = "version", defaultValue = "1") String version) {
if (type.isEmpty()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Required headers should not be null");
} else {
type = type.toUpperCase();
}

RawERLDto rawERLDto = new RawERLDto();
customMetricsBuilder.incrementMessagesProcessed();

if (type.equalsIgnoreCase("HL7")) {
if (type.equalsIgnoreCase(HL7_ELR)) {
rawERLDto.setType(type);
rawERLDto.setPayload(payload);
rawERLDto.setValidationActive(true);
return ResponseEntity.ok(rawELRService.submission(rawERLDto, version));
}
else if (type.equalsIgnoreCase("HL7-XML")) {
else if (type.equalsIgnoreCase(XML_ELR)) {
rawERLDto.setType(type);
rawERLDto.setPayload(payload);
rawERLDto.setValidationActive(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.util.UUID;

import static gov.cdc.dataingestion.constant.MessageType.HL7_ELR;
import static gov.cdc.dataingestion.constant.MessageType.XML_ELR;
import static gov.cdc.dataingestion.share.helper.TimeStampHelper.getCurrentTimeStamp;

@Service
Expand All @@ -31,7 +33,7 @@ public class RawELRService {

public String submission(RawERLDto rawERLDto, String version) {
RawERLModel created = rawELRRepository.save(convert(rawERLDto));
if(rawERLDto.getType().equalsIgnoreCase("HL7")) {
if(rawERLDto.getType().equalsIgnoreCase(HL7_ELR)) {
kafkaProducerService.sendMessageFromController(
created.getId(),
topicName,
Expand All @@ -40,7 +42,7 @@ public String submission(RawERLDto rawERLDto, String version) {
rawERLDto.getValidationActive(),
version);
}
if(rawERLDto.getType().equalsIgnoreCase("HL7-XML")) {
if(rawERLDto.getType().equalsIgnoreCase(XML_ELR)) {
kafkaProducerService.sendElrXmlMessageFromController(
created.getId(),
rawXmlTopicName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.List;
import java.util.Optional;

import static gov.cdc.dataingestion.constant.MessageType.HL7_ELR;

@Service
public class ReportStatusService {
private final IReportStatusRepository iReportStatusRepository;
Expand Down Expand Up @@ -58,7 +60,7 @@ public MessageStatus getMessageStatus(String rawMessageID) {
msgStatus.getRawInfo().setRawCreatedOn(rawMessageData.get().getCreatedOn());
msgStatus.getRawInfo().setRawPipeLineStatus(MSG_STATUS_SUCCESS);

if (rawMessageData.get().getType().equals("HL7")) {
if (rawMessageData.get().getType().equalsIgnoreCase(HL7_ELR)) {
Optional<ValidatedELRModel> validatedMessageData = iValidatedELRRepository.findByRawId(msgStatus.getRawInfo().getRawMessageId());
if (!validatedMessageData.isEmpty()) {
msgStatus.getValidatedInfo().setValidatedMessageId(validatedMessageData.get().getId());
Expand Down Expand Up @@ -107,7 +109,11 @@ private MessageStatus setDiXmlTransformationInfo(MessageStatus msgStatus) {
msgStatus.getNbsInfo().setNbsInterfacePipeLineStatus(MSG_STATUS_SUCCESS);
setNbsInfo(msgStatus);
} else {
setDltInfo(msgStatus.getValidatedInfo().getValidatedMessageId(), msgStatus, DLT_ORIGIN_VALIDATED);
if (msgStatus.getValidatedInfo().getValidatedMessageId() == null) {
setDltInfo(msgStatus.getRawInfo().getRawMessageId(), msgStatus, DLT_ORIGIN_RAW);
} else {
setDltInfo(msgStatus.getValidatedInfo().getValidatedMessageId(), msgStatus, DLT_ORIGIN_VALIDATED);
}
}
return msgStatus;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package gov.cdc.dataingestion.rawmessage.controller;
import gov.cdc.dataingestion.custommetrics.CustomMetricsBuilder;
import gov.cdc.dataingestion.hl7.helper.integration.exception.DiHL7Exception;
import gov.cdc.dataingestion.rawmessage.dto.RawERLDto;
import gov.cdc.dataingestion.rawmessage.service.RawELRService;
import gov.cdc.dataingestion.rawmessage.controller.ElrReportsController;
import gov.cdc.dataingestion.validation.services.interfaces.IHL7Service;
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 org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.server.ResponseStatusException;

import static gov.cdc.dataingestion.constant.MessageType.HL7_ELR;
import static gov.cdc.dataingestion.constant.MessageType.XML_ELR;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

public class ElrReportsControllerMockTest {
@Mock
private RawELRService rawELRService;

@Mock
private CustomMetricsBuilder customMetricsBuilder;

@Mock
private IHL7Service hl7Service;

@InjectMocks
private ElrReportsController elrReportsController;

@BeforeEach
void setUp() {
// Initializes the mocks and injects them into the controller
MockitoAnnotations.openMocks(this);
}

@Test
void testSave_HL7_ELR_Type() {
String payload = "HL7 message";
String type = HL7_ELR;
String version = "1";
String expectedResponse = "Submission successful";

when(rawELRService.submission(any(RawERLDto.class), eq(version))).thenReturn(expectedResponse);

ResponseEntity<String> response = elrReportsController.save(payload, type, version);

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(expectedResponse, response.getBody());
verify(customMetricsBuilder).incrementMessagesProcessed();
}

@Test
void testSave_XML_ELR_Type() {
String payload = "XML message";
String type = XML_ELR;
String version = "1";
String expectedResponse = "Submission successful";

when(rawELRService.submission(any(RawERLDto.class), eq(version))).thenReturn(expectedResponse);

ResponseEntity<String> response = elrReportsController.save(payload, type, version);

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(expectedResponse, response.getBody());
verify(customMetricsBuilder).incrementMessagesProcessed();
}

@Test
void testSave_Invalid_Type() {
String payload = "Invalid message";
String type = "INVALID_TYPE";
String version = "1";

assertThrows(ResponseStatusException.class, () ->
elrReportsController.save(payload, type, version));
}

@Test
void testSave_Missing_Type() {
String payload = "Message without type";
String type = ""; // Empty type
String version = "1";

assertThrows(ResponseStatusException.class, () ->
elrReportsController.save(payload, type, version));
}

@Test
void testHl7Validator_ValidPayload() throws DiHL7Exception {
String payload = "Valid HL7 message";
String expectedResponse = "Valid";

when(hl7Service.hl7Validator(payload)).thenReturn(expectedResponse);

ResponseEntity<String> response = elrReportsController.hl7Validator(payload);

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(expectedResponse, response.getBody());
}

@Test
void testHl7Validator_InvalidPayload() throws DiHL7Exception {
String payload = "Invalid HL7 message";

when(hl7Service.hl7Validator(payload)).thenThrow(new DiHL7Exception("Invalid HL7 message"));

assertThrows(DiHL7Exception.class, () -> elrReportsController.hl7Validator(payload));
}
}

0 comments on commit 0dc147d

Please sign in to comment.