Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage and edge case fixes on ReportStreamSenderHelper #1180

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}