Skip to content

Commit

Permalink
Merge pull request #967 from CDCgov/story/650/results-metadata
Browse files Browse the repository at this point in the history
Added metadata
  • Loading branch information
jherrflexion authored Mar 22, 2024
2 parents a6e879b + 3b163e6 commit 810d50f
Show file tree
Hide file tree
Showing 27 changed files with 507 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,47 @@ class MetadataTest extends Specification {
}
}

def "a metadata response is returned from the ETOR metadata endpoint for results"() {
given:
def expectedStatusCode = 200
def inboundSubmissionId = UUID.randomUUID().toString()
def outboundSubmissionId = "1234567890"
def resultClient = new EndpointClient("/v1/etor/results")
def labResult = Files.readString(Path.of("../examples/Test/Results/001_ORU_R01.fhir"))

when:
def resultResponse = resultClient.submit(labResult, inboundSubmissionId, true)

then:
resultResponse.getCode() == expectedStatusCode

when:
def inboundMetadataResponse = metadataClient.get(inboundSubmissionId, true)
def outboundMetadataResponse = metadataClient.get(outboundSubmissionId, true)
def inboundParsedJsonBody = JsonParsing.parseContent(inboundMetadataResponse)
def outboundParsedJsonBody = JsonParsing.parseContent(outboundMetadataResponse)

then:
inboundMetadataResponse.getCode() == expectedStatusCode
outboundMetadataResponse.getCode() == expectedStatusCode
inboundParsedJsonBody.get("id") == inboundSubmissionId
outboundParsedJsonBody.get("id") == outboundSubmissionId

[
"sender name",
"receiver name",
"result ingestion",
"payload hash",
"delivery status",
"status message"
].each { String metadataKey ->
def issue = (inboundParsedJsonBody.issue as List).find( {issue -> issue.details.text == metadataKey })
assert issue != null
assert issue.diagnostics != null
assert !issue.diagnostics.isEmpty()
}
}

def "a 404 is returned when there is no metadata for a given ID"() {
when:
def metadataResponse = metadataClient.get(UUID.randomUUID().toString(), true)
Expand Down
32 changes: 32 additions & 0 deletions etor/databaseMigrations/metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,35 @@ databaseChangeLog:
- column:
name: failure_reason
type: varchar(1000)

- changeSet:
id: 3
author: james.herr
labels: create-type
context: metadata
comment: create message type
changes:
- sql:
sql: CREATE TYPE message_type AS ENUM ('RESULT', 'ORDER')
rollback:
- sql:
sql: DROP TYPE message_type

- changeSet:
id: 4
author: samuel.aquino
labels: update-metadata-table
context: metadata
comment: update partner metadata table
changes:
- renameColumn:
columnDataType: varchar(1000)
oldColumnName: hash_of_order
newColumnName: hash_of_message
tableName: metadata
- addColumn:
tableName: metadata
columns:
- column:
name: message_type
type: message_type
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import gov.hhs.cdc.trustedintermediary.etor.demographics.PatientDemographicsController;
import gov.hhs.cdc.trustedintermediary.etor.demographics.PatientDemographicsResponse;
import gov.hhs.cdc.trustedintermediary.etor.messages.MessageRequestHandler;
import gov.hhs.cdc.trustedintermediary.etor.messages.SendMessageHelper;
import gov.hhs.cdc.trustedintermediary.etor.messages.UnableToSendMessageException;
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadata;
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataException;
Expand Down Expand Up @@ -120,6 +121,8 @@ public Map<HttpEndpoint, Function<DomainRequest, DomainResponse>> domainRegistra
ApplicationContext.register(
PartnerMetadataOrchestrator.class, PartnerMetadataOrchestrator.getInstance());

ApplicationContext.register(SendMessageHelper.class, SendMessageHelper.getInstance());

if (ApplicationContext.getProperty("DB_URL") != null) {
ApplicationContext.register(DbDao.class, PostgresDao.getInstance());
ApplicationContext.register(
Expand Down Expand Up @@ -187,7 +190,7 @@ DomainResponse handleResults(DomainRequest request) {
request,
receivedSubmissionId -> {
Result<?> results = resultController.parseResults(request);
sendResultUseCase.convertAndSend(results);
sendResultUseCase.convertAndSend(results, receivedSubmissionId);
return domainResponseHelper.constructOkResponse(new ResultResponse(results));
},
"results",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gov.hhs.cdc.trustedintermediary.etor.demographics;

import gov.hhs.cdc.trustedintermediary.etor.messages.SendMessageUseCase;
import gov.hhs.cdc.trustedintermediary.etor.messages.UnableToSendMessageException;
import gov.hhs.cdc.trustedintermediary.etor.orders.Order;
import gov.hhs.cdc.trustedintermediary.etor.orders.OrderConverter;
Expand All @@ -11,7 +10,7 @@
* The overall logic that handles receiving patient demographics, converting it to a lab order, and
* sending it on its way.
*/
public class ConvertAndSendDemographicsUsecase implements SendMessageUseCase<Demographics<?>> {
public class ConvertAndSendDemographicsUsecase {

private static final ConvertAndSendDemographicsUsecase INSTANCE =
new ConvertAndSendDemographicsUsecase();
Expand All @@ -26,7 +25,6 @@ public static ConvertAndSendDemographicsUsecase getInstance() {

private ConvertAndSendDemographicsUsecase() {}

@Override
public void convertAndSend(Demographics<?> demographics) throws UnableToSendMessageException {
Order<?> order = converter.convertToOrder(demographics);
sender.send(order);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package gov.hhs.cdc.trustedintermediary.etor.messages;

import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataException;
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataMessageType;
import gov.hhs.cdc.trustedintermediary.etor.metadata.partner.PartnerMetadataOrchestrator;
import gov.hhs.cdc.trustedintermediary.wrappers.Logger;
import javax.inject.Inject;

public class SendMessageHelper {
private static final SendMessageHelper INSTANCE = new SendMessageHelper();

public static SendMessageHelper getInstance() {
return INSTANCE;
}

@Inject Logger logger;

@Inject PartnerMetadataOrchestrator partnerMetadataOrchestrator;

private SendMessageHelper() {}

public void savePartnerMetadataForReceivedMessage(
String receivedSubmissionId, int messageHash, PartnerMetadataMessageType messageType) {
if (receivedSubmissionId == null) {
logger.logWarning(
"Received submissionId is null so not saving metadata for received message");
return;
}
try {
String stringMessageHash = String.valueOf(messageHash);

partnerMetadataOrchestrator.updateMetadataForReceivedMessage(
receivedSubmissionId, stringMessageHash, messageType);
} catch (PartnerMetadataException e) {
logger.logError(
"Unable to save metadata for receivedSubmissionId " + receivedSubmissionId, e);
}
}

public void saveSentMessageSubmissionId(String receivedSubmissionId, String sentSubmissionId) {
if (sentSubmissionId == null || receivedSubmissionId == null) {
logger.logWarning(
"Received and/or sent submissionId is null so not saving metadata for sent result");
return;
}

try {
partnerMetadataOrchestrator.updateMetadataForSentMessage(
receivedSubmissionId, sentSubmissionId);
} catch (PartnerMetadataException e) {
logger.logError(
"Unable to update metadata for received submissionId "
+ receivedSubmissionId
+ " and sent submissionId "
+ sentSubmissionId,
e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
*/
public interface SendMessageUseCase<T> {

void convertAndSend(final T message) throws UnableToSendMessageException;
void convertAndSend(final T message, final String submissionId)
throws UnableToSendMessageException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public record PartnerMetadata(
Instant timeDelivered,
String hash,
PartnerMetadataStatus deliveryStatus,
String failureReason) {
String failureReason,
PartnerMetadataMessageType messageType) {

// Below is for defaulting status when null
public PartnerMetadata {
Expand All @@ -38,7 +39,8 @@ public PartnerMetadata(
Instant timeReceived,
Instant timeDelivered,
String hash,
PartnerMetadataStatus deliveryStatus) {
PartnerMetadataStatus deliveryStatus,
PartnerMetadataMessageType messageType) {
this(
receivedSubmissionId,
null,
Expand All @@ -48,15 +50,38 @@ public PartnerMetadata(
timeDelivered,
hash,
deliveryStatus,
null);
null,
messageType);
}

public PartnerMetadata(String receivedSubmissionId, String hash) {
this(receivedSubmissionId, null, null, null, null, null, hash, null, null);
this(receivedSubmissionId, null, null, null, null, null, hash, null, null, null);
}

public PartnerMetadata(
String receivedSubmissionId, String hash, PartnerMetadataMessageType messageType) {
this(receivedSubmissionId, null, null, null, null, null, hash, null, null, messageType);
}

public PartnerMetadata(String receivedSubmissionId, PartnerMetadataStatus deliveryStatus) {
this(receivedSubmissionId, null, null, null, null, null, null, deliveryStatus, null);
this(receivedSubmissionId, null, null, null, null, null, null, deliveryStatus, null, null);
}

public PartnerMetadata(
String receivedSubmissionId,
PartnerMetadataStatus deliveryStatus,
PartnerMetadataMessageType messageType) {
this(
receivedSubmissionId,
null,
null,
null,
null,
null,
null,
deliveryStatus,
null,
messageType);
}

public PartnerMetadata withSentSubmissionId(String sentSubmissionId) {
Expand All @@ -69,7 +94,8 @@ public PartnerMetadata withSentSubmissionId(String sentSubmissionId) {
this.timeDelivered,
this.hash,
this.deliveryStatus,
this.failureReason);
this.failureReason,
this.messageType);
}

public PartnerMetadata withReceiver(String receiver) {
Expand All @@ -82,7 +108,8 @@ public PartnerMetadata withReceiver(String receiver) {
this.timeDelivered,
this.hash,
this.deliveryStatus,
this.failureReason);
this.failureReason,
this.messageType);
}

public PartnerMetadata withTimeDelivered(Instant timeDelivered) {
Expand All @@ -95,7 +122,8 @@ public PartnerMetadata withTimeDelivered(Instant timeDelivered) {
timeDelivered,
this.hash,
this.deliveryStatus,
this.failureReason);
this.failureReason,
this.messageType);
}

public PartnerMetadata withDeliveryStatus(PartnerMetadataStatus deliveryStatus) {
Expand All @@ -108,7 +136,8 @@ public PartnerMetadata withDeliveryStatus(PartnerMetadataStatus deliveryStatus)
this.timeDelivered,
this.hash,
deliveryStatus,
this.failureReason);
this.failureReason,
this.messageType);
}

public PartnerMetadata withFailureMessage(String failureMessage) {
Expand All @@ -121,6 +150,7 @@ public PartnerMetadata withFailureMessage(String failureMessage) {
this.timeDelivered,
this.hash,
this.deliveryStatus,
failureMessage);
failureMessage,
this.messageType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gov.hhs.cdc.trustedintermediary.etor.metadata.partner;

/** Enum for tracking the message type */
public enum PartnerMetadataMessageType {
RESULT,
ORDER
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public static PartnerMetadataOrchestrator getInstance() {

private PartnerMetadataOrchestrator() {}

public void updateMetadataForReceivedOrder(String receivedSubmissionId, String orderHash)
public void updateMetadataForReceivedMessage(
String receivedSubmissionId, String messageHash, PartnerMetadataMessageType messageType)
throws PartnerMetadataException {
// currently blocked by: https://github.com/CDCgov/prime-reportstream/issues/12624
// once we get the right receivedSubmissionId from RS, this method should work
Expand All @@ -61,7 +62,8 @@ public void updateMetadataForReceivedOrder(String receivedSubmissionId, String o
logger.logWarning(
"Unable to retrieve metadata from RS history API, but writing basic metadata entry anyway for received submission ID {}",
receivedSubmissionId);
PartnerMetadata partnerMetadata = new PartnerMetadata(receivedSubmissionId, orderHash);
PartnerMetadata partnerMetadata =
new PartnerMetadata(receivedSubmissionId, messageHash, messageType);
partnerMetadataStorage.saveMetadata(partnerMetadata);

throw new PartnerMetadataException(
Expand All @@ -78,12 +80,13 @@ public void updateMetadataForReceivedOrder(String receivedSubmissionId, String o
sender,
timeReceived,
null,
orderHash,
PartnerMetadataStatus.PENDING);
messageHash,
PartnerMetadataStatus.PENDING,
messageType);
partnerMetadataStorage.saveMetadata(partnerMetadata);
}

public void updateMetadataForSentOrder(String receivedSubmissionId, String sentSubmissionId)
public void updateMetadataForSentMessage(String receivedSubmissionId, String sentSubmissionId)
throws PartnerMetadataException {

if (sentSubmissionId == null) {
Expand Down
Loading

0 comments on commit 810d50f

Please sign in to comment.