zerocode
zerocode copied to clipboard
[KAFKA] Assert record content when multiple records collected
Sometimes when I run consumer tests more than one record is picked up by the KafkaConsumer together with the one I'm looking for but AssertionProcessor only tries to match the first record collected.
Here is my consumer configuration :
{
"scenarioName": "Check phoneNumbersChange even is generated",
"steps": [
{
"name": "consume_step",
"url": "kafka-topic:t1.party.customer.test.v1",
"operation": "consume",
"request": {
"consumerLocalConfigs": {
"recordType": "JSON",
"commitSync": true,
"showRecordsConsumed": true,
"maxNoOfRetryPollsOrTimeouts": 10
}
},
"assertions": {
"records": [
{
"key" : "37_party_customer_7C8012AC-0122-4911-8808-54AA687C0002",
"value": {
"metadata": {
"bankNr": 37,
"eventName":"phoneNumbersChanged"
},
"payload": {
"phoneNumberList": [
{
"description" : "Mobil",
"isMobilePhone" : true,
"phoneNumber" : "${SYSTEM.PROPERTY:37_phoneChange}"
}
]
}
}
}
]
}
}
]
}
KafkaConsumer then properly pick's up the records including mine with
but when then it continues to assertion phase it checks only for the first records
How can I fix this?
Hi @eugenenosenko , can you put here the request+response log here? You can copy it from the console or the target/logs
folder after running the test scenario.
attached logs : zerocode_rest_bdd_logs.log
Please observe the above screenshot of the actual response... The actual record is the 279th record, indexed as 278(due to 0 counted as 1). You may not be able to assert this deterministically as the index might change everytime you pull from the topic, hence you need to use JSON Path expression for this kind of matching scenarios.
Matcher Explaination
Trying to understand... The below looks alright to me in terms of the framework's matcher behaviour. The expected response is as below.
---------> Expected Response: <----------
Assumed Payload:
{
"records" : [ {
"key" : "37_party_customer_7C8012AC-0122-4911-8808-54AA687C0002",
"value" : {
"metadata" : {
"bankNr" : 37,
"eventName" : "phoneNumbersChanged"
},
"payload" : {
"phoneNumberList" : [ {
"description" : "Mobil",
"isMobilePhone" : true,
"phoneNumber" : "63099875"
} ]
}
}
} ]
}
Which means this step will pass only when the 1st(very first one) record matches this. Actual 1st record is as below:
{
"key" : "32_party_customer_749412AC-0122-4E00-8F28-51A0182A0004",
"jsonKey" : null,
"value" : {
"metadata" : {
"bankNr" : 32,
"producedTime" : 1602516860781,
"aggregate" : "customer",
"aggregateVersion" : 1,
"connectTms" : 1602516860904,
"correlationId" : "",
"traceId" : "589C02AC-15A0-4605-800A-04A052000006",
"spanId" : "589C02AC-15A0-4605-800A-04A052000006",
"eventName" : "customerAdvisorRelationsChanged",
"businessDomain" : "party"
},
"payload" : {
"customerUniqueness" : {
"bankNumber" : "00032",
"partyUuid" : "749412AC-0122-4E00-8F28-51A0182A0004"
},
"customerAdvisorRelationList" : [ {
"agentId" : "0609",
"agentType" : "PRIMARY_ADVISOR"
} ]
}
}
}
Matcher errors are:
2020-10-13 09:57:02,826 [main] ERROR org.jsmart.zerocode.core.runner.StepNotificationHandler - Assertion failed for :-
[Check phoneNumbersChange even is generated]
|
|
+---Step --> [consume_step]
Failures:
---------
Assertion jsonPath '$.records[0].value.metadata.bankNr' with actual value '32' did not match the expected value '37'
----------------------------------------------------------------------------------------------------------------------------------
Assertion jsonPath '$.records[0].value.payload.phoneNumberList[0].phoneNumber' with actual value 'null' did not match the expected value '63099875'
----------------------------------------------------------------------------------------------------------------------------------
Assertion jsonPath '$.records[0].value.metadata.eventName' with actual value 'customerAdvisorRelationsChanged' did not match the expected value 'phoneNumbersChanged'
----------------------------------------------------------------------------------------------------------------------------------
Assertion jsonPath '$.records[0].value.payload.phoneNumberList[0].description' with actual value 'null' did not match the expected value 'Mobil'
----------------------------------------------------------------------------------------------------------------------------------
Assertion jsonPath '$.records[0].value.payload.phoneNumberList[0].isMobilePhone' with actual value 'null' did not match the expected value 'true'
----------------------------------------------------------------------------------------------------------------------------------
Assertion jsonPath '$.records[0].key' with actual value '32_party_customer_749412AC-0122-4E00-8F28-51A0182A0004' did not match the expected value '37_party_customer_7C8012AC-0122-4911-8808-54AA687C0002'
(See below 'Actual Vs Expected' to learn why this step failed)
Hey @eugenenosenko . Is your issue resolved? Do you have other questions?
hello @eugenenosenko ; you can use validators
block for this need and test only your targeted record with JSON path matchers :
{
"field": "$.size",
"value": 2
},
{
"field": "$.records[?(@.key== '37_party_customer_7C8012AC-0122-4911-8808-54AA687C0002')]",
"value": [
{
// your assertions here
}
]
}```
Hope this help !