Skip to content

Commit

Permalink
Merge pull request #1180 from CDCgov/chores/additional-code-coverage
Browse files Browse the repository at this point in the history
Test coverage and edge case fixes on ReportStreamSenderHelper
  • Loading branch information
luis-pabon-tf authored Jul 17, 2024
2 parents fb5034d + 608a658 commit 6301516
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ protected Optional<String> getReportId(String rsResponseBody) {
try {
Map<String, Object> rsResponse =
formatter.convertJsonToObject(rsResponseBody, new TypeReference<>() {});
return Optional.ofNullable(rsResponse.get("reportId").toString());
} catch (FormatterProcessingException e) {
String reportId = rsResponse.get("reportId").toString();
if (!reportId.isEmpty()) {
return Optional.of(reportId);
}
} catch (FormatterProcessingException | NullPointerException e) {
logger.logError("Unable to get the reportId", e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,40 @@ class ReportStreamSenderHelperTest extends Specification {
then:
1 * mockLogger.logError(_ as String, exception)
}

def "sendToReportStream correctly logs errors when it lacks a ReportId or it is blank"() {
given:
def requestBody = "testBody"
def bearerToken = "fake-token"
def responseBody = """{"reportId": ""}"""
def messageType = PartnerMetadataMessageType.ORDER

def mockFormatter = Mock(Formatter)
TestApplicationContext.register(Formatter, mockFormatter)

def mockRsClient = Mock(RSEndpointClient)
TestApplicationContext.register(RSEndpointClient, mockRsClient)

def mockLogger = Mock(Logger)
TestApplicationContext.register(Logger, mockLogger)

TestApplicationContext.injectRegisteredImplementations()

mockRsClient.getRsToken() >> "fake-token"
mockRsClient.requestWatersEndpoint(requestBody, bearerToken) >> responseBody

when:
mockFormatter.convertJsonToObject(responseBody, _ as TypeReference) >> ["": ""]
ReportStreamSenderHelper.getInstance().sendToReportStream(requestBody, _ as String, messageType)

then:
1 * mockLogger.logError("Unable to retrieve ReportId from ReportStream response")

when:
mockFormatter.convertJsonToObject(responseBody, _ as TypeReference) >> ["reportId": ""]
ReportStreamSenderHelper.getInstance().sendToReportStream(requestBody, _ as String, messageType)

then:
1 * mockLogger.logError("Unable to retrieve ReportId from ReportStream response")
}
}

0 comments on commit 6301516

Please sign in to comment.