Skip to content

Commit

Permalink
611: Better E2E Test for Metadata (#792)
Browse files Browse the repository at this point in the history
* 611: The mock RS Endpoint returns better history details and the e2e test tests for more data

* 611: write a unit test to test that we do not save the error messages if the overallStatus is not a failure
  • Loading branch information
halprin authored Jan 19, 2024
1 parent d71692e commit cfc6286
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ public Optional<PartnerMetadata> 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);
}

Expand Down Expand Up @@ -198,6 +199,9 @@ String[] getDataFromReportStream(String responseBody) throws FormatterProcessing
// ...
// } ],
// ...
// "errors": [{
// "message": "some error message"
// }]
// }

Map<String, Object> responseObject =
Expand Down Expand Up @@ -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()};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
}]
}""";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ class PartnerMetadataOrchestratorTest extends Specification {
overallStatus: "Delivered",
destinations: [
[organization_id: "org", service: "service"]
]]
],
errors: []
]

when:
Optional<PartnerMetadata> result = PartnerMetadataOrchestrator.getInstance().getMetadata(receivedSubmissionId)
Expand Down Expand Up @@ -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<PartnerMetadata> 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<PartnerMetadata> result = PartnerMetadataOrchestrator.getInstance().getMetadata(receivedSubmissionId)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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)

Expand All @@ -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"
Expand Down Expand Up @@ -401,15 +441,15 @@ class PartnerMetadataOrchestratorTest extends Specification {

when:

def jsonWithEmptyDestinations = "{\"destinations\": []}"
def jsonWithEmptyDestinations = """{"destinations": [], "errors": []}"""
def parsedData = PartnerMetadataOrchestrator.getInstance().getDataFromReportStream(jsonWithEmptyDestinations)

then:
parsedData[0] == null

when:

def jsonWithNoStatus = "{\"destinations\": []}"
def jsonWithNoStatus = """{"destinations": [], "errors": []}"""
parsedData = PartnerMetadataOrchestrator.getInstance().getDataFromReportStream(jsonWithNoStatus)

then:
Expand Down

0 comments on commit cfc6286

Please sign in to comment.