Looking at your code, I can see you want to test that the eventDttm field is being properly amended when reading from a valid intake merchant payload. Based on the FinancialEventUtils.updateEventDttmField() method shown in Image 2, this appears to convert UTC time to EST/EDT with an offset.
Here’s how to write a test case for this:
@test
@DisplayName("Verify eventDttm is amended correctly for valid merchant intake")
void testEventDttmIsAmendedForValidMerchantIntake() throws Exception {
// Setup logger to capture logs
logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(MerchantController.class)).addAppender(logWatcher);
// Create test payload with a specific eventDttm in UTC
String originalEventDttm = "2024-01-15T10:30:00Z"; // UTC time
testPayload = readJsonObject(filePath: "valid_merchantIntake.json");
// Modify the financial event's eventDttm to a known value
ObjectNode financialEvent = (ObjectNode) testPayload
.get("financialEvents")
.get(0);
financialEvent.put("eventDttm", originalEventDttm);
// Perform the request
mockMvc.perform(
post(urlTemplate: "/fraudcheck")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getToken())
.header(name: "eventCode", value: "test")
.header(name: "channelID", value: "test")
.header(name: "TraceabilityID", value: "test")
.content(testPayload)
)
.andExpect(status().is2xxSuccessful());
// Verify the eventDttm was amended
// Check the log to see the amended value was set
assertTrue(
logWatcher.list.stream()
.anyMatch(event -> {
String message = event.getFormattedMessage();
// The log should show the request with amended eventDttm
if (message.contains("fraudCheck: FraudCheckEventRequest, Received:")) {
// Extract and verify the eventDttm was changed from UTC
return !message.contains(originalEventDttm);
}
return false;
}),
"eventDttm should be amended from original UTC value"
);
// Alternative: Capture the actual Kafka message sent and verify eventDttm there
// This would require mocking KafkaService and capturing the ProducerRecord
}
Better approach with Kafka verification:
@test
@DisplayName("Verify eventDttm is amended correctly in Kafka message")
void testEventDttmAmendedInKafkaMessage() throws Exception {
// Capture the actual Kafka message
ArgumentCaptor kafkaRecordCaptor =
ArgumentCaptor.forClass(ProducerRecord.class);
// Original UTC time
String originalEventDttm = "2024-01-15T10:30:00Z";
testPayload = readJsonObject(filePath: "valid_merchantIntake.json");
ObjectNode financialEvent = (ObjectNode) testPayload
.get("financialEvents")
.get(0);
financialEvent.put("eventDttm", originalEventDttm);
mockMvc.perform(
post("/fraudcheck")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, getToken())
.header("eventCode", "test")
.header("channelID", "test")
.header("TraceabilityID", "test")
.content(testPayload)
)
.andExpect(status().is2xxSuccessful());
// Verify Kafka message was sent
verify(kafkaService).publishMessage(kafkaRecordCaptor.capture());
// Extract the published record
ProducerRecord capturedRecord = kafkaRecordCaptor.getValue();
String publishedMessage = (String) capturedRecord.value();
// Parse the published JSON
ObjectNode publishedPayload = (ObjectNode) objectMapper.readTree(publishedMessage);
String amendedEventDttm = publishedPayload
.get("financialEvents")
.get(0)
.get("eventDttm")
.asText();
// Verify the eventDttm was changed (amended to EST/EDT)
assertNotEquals(originalEventDttm, amendedEventDttm,
"eventDttm should be amended from original UTC time");
// Verify it follows expected format and timezone conversion
// Based on the code, it should be converted to America/New_York timezone
assertTrue(amendedEventDttm.contains("-05:00") || amendedEventDttm.contains("-04:00"),
"eventDttm should be converted to EST (-05:00) or EDT (-04:00)");
}
This test:
1. Creates a payload with a known UTC eventDttm value
2. Calls the fraud check endpoint
3. Captures the Kafka message that gets published
4. Verifies that the eventDttm in the Kafka message is different from the original (has been amended)
5. Confirms it’s in the expected timezone format (EST/EDT)
Top comments (0)