diff --git a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamSenderHelper.java b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamSenderHelper.java index 8ab5c376b..ed63a24f4 100644 --- a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamSenderHelper.java +++ b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamSenderHelper.java @@ -69,8 +69,11 @@ protected Optional getReportId(String rsResponseBody) { try { Map 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); } diff --git a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamSenderHelperTest.groovy b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamSenderHelperTest.groovy index 8c0231398..58c1b3573 100644 --- a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamSenderHelperTest.groovy +++ b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamSenderHelperTest.groovy @@ -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") + } }