diff --git a/e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/e2e/MetadataTest.groovy b/e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/e2e/MetadataTest.groovy index 0d71a7ac5..ce43dd7f7 100644 --- a/e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/e2e/MetadataTest.groovy +++ b/e2e/src/test/groovy/gov/hhs/cdc/trustedintermediary/e2e/MetadataTest.groovy @@ -33,6 +33,20 @@ class MetadataTest extends Specification { then: metadataResponse.getCode() == expectedStatusCode parsedJsonBody.get("id") == submissionId + + [ + "sender name", + "receiver name", + "order ingestion", + "payload hash", + "delivery status", + "status message" + ].each { String metadataKey -> + def issue = (parsedJsonBody.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"() { diff --git a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/metadata/partner/PartnerMetadataOrchestrator.java b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/metadata/partner/PartnerMetadataOrchestrator.java index e6644fc0f..a811d160a 100644 --- a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/metadata/partner/PartnerMetadataOrchestrator.java +++ b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/metadata/partner/PartnerMetadataOrchestrator.java @@ -144,11 +144,12 @@ public Optional getMetadata(String receivedSubmissionId) var ourStatus = ourStatusFromReportStreamStatus(rsStatus); logger.logInfo("Updating metadata with receiver {} and status {}", receiver, ourStatus); - partnerMetadata = - partnerMetadata - .withReceiver(receiver) - .withDeliveryStatus(ourStatus) - .withFailureMessage(rsMessage); + partnerMetadata = partnerMetadata.withReceiver(receiver).withDeliveryStatus(ourStatus); + + if (ourStatus == PartnerMetadataStatus.FAILED) { + partnerMetadata = partnerMetadata.withFailureMessage(rsMessage); + } + partnerMetadataStorage.saveMetadata(partnerMetadata); } @@ -198,6 +199,9 @@ String[] getDataFromReportStream(String responseBody) throws FormatterProcessing // ... // } ], // ... + // "errors": [{ + // "message": "some error message" + // }] // } Map responseObject = @@ -227,17 +231,15 @@ String[] getDataFromReportStream(String responseBody) throws FormatterProcessing } StringBuilder errorMessages = new StringBuilder(); - if ("Error".equalsIgnoreCase(overallStatus)) { - try { - ArrayList errors = (ArrayList) responseObject.get("errors"); - for (Object error : errors) { - Map x = (Map) error; - errorMessages.append(x.get("message").toString()).append(" / "); - } - } catch (Exception e) { - throw new FormatterProcessingException( - "Unable to extract failure reason due to unexpected format", e); + try { + ArrayList errors = (ArrayList) responseObject.get("errors"); + for (Object error : errors) { + Map x = (Map) error; + errorMessages.append(x.get("message").toString()).append(" / "); } + } catch (Exception e) { + throw new FormatterProcessingException( + "Unable to extract failure reason due to unexpected format", e); } return new String[] {receiver, overallStatus, errorMessages.toString()}; diff --git a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/localfile/MockRSEndpointClient.java b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/localfile/MockRSEndpointClient.java index 3fe338403..0cc24c8ef 100644 --- a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/localfile/MockRSEndpointClient.java +++ b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/localfile/MockRSEndpointClient.java @@ -46,9 +46,15 @@ public String requestHistoryEndpoint(String submissionId, String bearerToken) { { "timestamp" : "2020-01-01T00:00:00.000Z", "sender" : "flexion.simulated-hospital", + "overallStatus": "Not Delivering", "destinations": [{ "organization_id": "flexion", "service": "simulated-lab" + }], + "errors": [{ + "message": "The message was not good" + }, { + "message": "The DogCow couldn't Moof!" }] }"""; } diff --git a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/metadata/partner/PartnerMetadataOrchestratorTest.groovy b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/metadata/partner/PartnerMetadataOrchestratorTest.groovy index 4890618d1..cafdfc85c 100644 --- a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/metadata/partner/PartnerMetadataOrchestratorTest.groovy +++ b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/metadata/partner/PartnerMetadataOrchestratorTest.groovy @@ -255,7 +255,9 @@ class PartnerMetadataOrchestratorTest extends Specification { overallStatus: "Delivered", destinations: [ [organization_id: "org", service: "service"] - ]] + ], + errors: [] + ] when: Optional result = PartnerMetadataOrchestrator.getInstance().getMetadata(receivedSubmissionId) @@ -285,8 +287,47 @@ class PartnerMetadataOrchestratorTest extends Specification { mockFormatter.convertJsonToObject(rsHistoryApiResponse, _ as TypeReference) >> [ overallStatus: "Not Delivering", destinations: [ - [organization_id: "org", service: "service"] - ]] + [organization_id: "org", service: "service"], + ], + errors: [], + ] + + when: + Optional result = PartnerMetadataOrchestrator.getInstance().getMetadata(receivedSubmissionId) + + then: + result.isPresent() + result.get() == expectedMetadata + 1 * mockPartnerMetadataStorage.readMetadata(receivedSubmissionId) >> Optional.of(missingReceiverMetadata) + 1 * mockPartnerMetadataStorage.saveMetadata(expectedMetadata) + } + + def "getMetadata doesn't update the error messages if the status isn't FAILED when calling the RS history API"() { + given: + def receivedSubmissionId = "receivedSubmissionId" + def sentSubmissionId = "sentSubmissionId" + def sender = "senderName" + def receiver = "org.service" + def timestamp = Instant.now() + def hashCode = "123" + def bearerToken = "token" + def rsHistoryApiResponse = "whatever" + def missingReceiverMetadata = new PartnerMetadata(receivedSubmissionId, sentSubmissionId, sender, receiver, timestamp, hashCode, PartnerMetadataStatus.PENDING, null) + def expectedMetadata = new PartnerMetadata(receivedSubmissionId, sentSubmissionId, sender, receiver, timestamp, hashCode, PartnerMetadataStatus.DELIVERED, null) + + mockClient.getRsToken() >> bearerToken + mockClient.requestHistoryEndpoint(sentSubmissionId, bearerToken) >> rsHistoryApiResponse + mockFormatter.convertJsonToObject(rsHistoryApiResponse, _ as TypeReference) >> [ + overallStatus: "Delivered", + destinations: [ + [organization_id: "org", service: "service"], + ], + errors: [ + [ + message: "This is an error message" + ] + ], + ] when: Optional result = PartnerMetadataOrchestrator.getInstance().getMetadata(receivedSubmissionId) @@ -298,7 +339,7 @@ class PartnerMetadataOrchestratorTest extends Specification { 1 * mockPartnerMetadataStorage.saveMetadata(expectedMetadata) } - def "setMetadataStatusToFailed sets status to Failed"(){ + def "setMetadataStatusToFailed sets status to Failed"() { given: def submissionId = "13425" def metadataStatus = PartnerMetadataStatus.FAILED @@ -314,7 +355,7 @@ class PartnerMetadataOrchestratorTest extends Specification { } } - def "setMetadataStatusToFailed doesn't update status if status is the same"(){ + def "setMetadataStatusToFailed doesn't update status if status is the same"() { given: def submissionId = "13425" def metadataStatus = PartnerMetadataStatus.FAILED @@ -328,11 +369,10 @@ class PartnerMetadataOrchestratorTest extends Specification { 0 * mockPartnerMetadataStorage.saveMetadata(_ as PartnerMetadata) } - def "setMetadataStatus sets status to Pending when there is no metadata"(){ + def "setMetadataStatusToFailed sets status to Failed when there is no metadata"() { given: def submissionId = "13425" - def optional = Optional.empty() - mockPartnerMetadataStorage.readMetadata(submissionId) >> optional + mockPartnerMetadataStorage.readMetadata(submissionId) >> Optional.empty() when: PartnerMetadataOrchestrator.getInstance().setMetadataStatusToFailed(submissionId, "Failure") @@ -344,7 +384,7 @@ class PartnerMetadataOrchestratorTest extends Specification { } } - def "setMetadataStatus doesn't update when submissionId is null"(){ + def "setMetadataStatusToFailed doesn't update when submissionId is null"() { when: PartnerMetadataOrchestrator.getInstance().setMetadataStatusToFailed(null, null) @@ -353,7 +393,7 @@ class PartnerMetadataOrchestratorTest extends Specification { } - def "getDataFromReportStream returns correct status name and receiver name from valid JSON response"() { + def "getDataFromReportStream returns correct status, receiver name, error messages from valid JSON response"() { given: def organization = "org_id" def sender = "service_name" @@ -401,7 +441,7 @@ class PartnerMetadataOrchestratorTest extends Specification { when: - def jsonWithEmptyDestinations = "{\"destinations\": []}" + def jsonWithEmptyDestinations = """{"destinations": [], "errors": []}""" def parsedData = PartnerMetadataOrchestrator.getInstance().getDataFromReportStream(jsonWithEmptyDestinations) then: @@ -409,7 +449,7 @@ class PartnerMetadataOrchestratorTest extends Specification { when: - def jsonWithNoStatus = "{\"destinations\": []}" + def jsonWithNoStatus = """{"destinations": [], "errors": []}""" parsedData = PartnerMetadataOrchestrator.getInstance().getDataFromReportStream(jsonWithNoStatus) then: