-
-
Notifications
You must be signed in to change notification settings - Fork 481
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
Check JSON Body contains key objects and key arrays #500
Comments
Tried with below one
Still error java.lang.UnsupportedOperationException: can't call closeArray on an Object |
I think the problem is that .eachKeyMappedToAnArrayLike("name")
.stringType("id")
.closeObject) // This is probably missing
.closeArray() |
Thanks for the reply. I implemented that and when I am trying to build the i am getting below error |
The consumerTest2 code is below one `package product_demo; import static junit.framework.TestCase.assertEquals; import java.io.File; import org.json.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import au.com.dius.pact.consumer.ConsumerPactTest; /**
PactDslJsonBody dslbody = new PactDslJsonBody();
} |
Just clean out your old pacts and try running it again. |
You need to provide the stack trace of the exception, which will indicate what the cause is. |
Please see the stack trace for the code
//compare the Map - use pact instead of traditional diff - need to check any other logic 10:10:02,254 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] . ____ _ __ _ _ 2017-08-21 10:12:47 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: groovy/json/JsonSlurper] with root cause |
|
Groovy JAR is available in dependencies. Please see the attached POM file and below image. |
It may be displayed in your IDE, but it is not available when your spring boot app is running. You need to check the classpath that the app is running with. |
Im getting an error in .eachKeyMappedToAnArrayLike() method, IDE says "Cannot resolve method 'eachKeyMappedToAnArrayLike(java.lang.String)'" public static DslPart getPactBodyFormulaEvaluation()
Do you know if this method is deprecated or some idea why can't I use it? Help!! |
@doinglivingtest Check you have the pact-jvm versions 3.3.3 or later (3.5.7 is the latest current version) |
Can someone please tell me how to match more than one object inside an array. For instance, below is the code :
Now, my response may contain the objects shown above in random order. How do I verify the objects generically? |
@habhilash in theory you need to use the But looking at your example, the three objects are identical in structure, it's just the |
Hi @uglyog , I tried using eachLike() function and got the follwoing error. Below is the snippet and error :
So, I don't think the object is supported here. Could you point me to an example to use 'or' |
You don't need the |
@uglyog, I have multiple objects to be verified as shown below.
|
From what I can see, your multiple objects are all the same. They have the same structure, so you could use one definition for all of them. |
Dynamic objects and Arrays with same structure never going to be the same. |
@sureshmaruthirao Pact is for testing structure, so for Pact things with the same structure will always be the same. |
@uglyog : I am trying to test the structure only using PACT. But I am testing the structure of value as well using Regular Expressions. And regular expression will differ for each and every object. In that case, verification of structure for each object should be supported. Also, the order of objects should not matter and currently, PACT looks for the order of objects as well. How can we verify all the objects without order? Also, you mentioned about OR operator. Can you please explain how to use OR operator and how to verify the structure of objects without order when I dont use eachLike() ?. |
Hi,
I am just exploring PACT_JVM to validate incoming JSON file against certain rules.
Sample Incoming JSON is below:
{
"customerOrderNumber": "DNUPTD01171429617",
"createdDate": 1500335319208,
"submittedDate": 1500335319208,
"orderType": "CREATE",
"orderStatus": {
"status": "RECEIVED",
"subStatus": "RECEIVED"
},
"productGroups": {
"group": [{
"id": "GROUP_01",
"name": "SHIRTS_01",
"type": "MENS",
"sequence": 1,
"price": [{
"amount": 8.75,
"currencyType": "USD",
"priceType": "DueToday",
"taxDetail": {
"amount": 0.0
},
"total": 8.75
},
{
"amount": 0.0,
"currencyType": "USD",
"total": 0.0
},
{
"amount": 0.0,
"currencyType": "USD",
"total": 0.0
},
{
"amount": 0.0,
"currencyType": "USD",
"priceType": "DUENOW",
"total": 0.0
}],
"promotionReferences": {
"promotionReference": ["PROMOTION_1"]
},
"characteristics": {
"losgCharacteristics": {
"losgReferenceId": "losg315710616",
"sequence": 1,
"losgType": "ACC",
"productCategory": "ONLINE",
"primaryIndicator": false,
"dealerCode": "Z0066",
"market": "TNK",
"subMarket": "415",
"serviceArea": "006772002695",
"priceCode": "415",
"accountReference": "ACCOUNT_01",
"wLOSCharacteristics": {
"billingSystemId": "T"
},
"fulfillmentMethod": "DF",
"shippingDetailReference": "SHIPPING_INFO_01"
}
}
}]
},
"lineItems": {
"lineItem": [{
"id": "111817034145",
"sequence": 1,
"productCode": "prod8720378",
"productSKU": "4797G",
"billingCode": "4797G",
"productType": "HARDGOOD",
"displayName": "U S POLA - V NECK T-SHIRT",
"systemName": "CAS SPK PRESIDIO MARGE CLR",
"action": "ADD",
"price": {
"amount": 159.0,
"msrp": 159.0,
"priceType": "DueToday",
"taxDetail": {
"preCalculatedTax": {
"taxableCost": 126.0,
"taxableMSRP": 159.0,
"taxableUnitPrice": 159.0,
"orderTaxAreaId": 390950000,
"shipFromTaxAreaId": 431570560,
"shipToTaxAreaId": 180091237
},
"lineItemTaxes": [{
"taxLineId": 1,
"taxCode": "SALESTAX",
"memo": "SALESTAX",
"printName": "Sales Tax",
"taxable": "N",
"skuSpecific": "false",
"jurisdictionLevel": "Sales Tax",
"jurisdictionName": "Sales Tax",
"taxAmount": 9.54,
"taxRate": 0.06,
"taxDate": "2017-06-05"
}]
},
"total": 168.54
},
"locationId": "K006",
"payments": {
"payment": [{
"paymentOptionReference": "PAYMENT_OPTION_01"
}]
},
"quantity": 1,
"promotionReferences": {
"promotionReference": ["PROMOTION_1"]
},
"groupReferences": {
"groupReference": ["GROUP_01"]
},
"characteristics": {
"wirelessLineItemCharacteristics": {
"nciEligibleIndicator": false
}
},
"hardGood": {
"hardGoodType": "ACCESSORY"
}
},
{
"id": "88878ShippingFee",
"sequence": 2,
"productCode": "NOT_AVAILABLE",
"productSKU": "88878",
"billingCode": "88878",
"productType": "MISC_CHARGE",
"displayName": "ShippingFee",
"systemName": "SHIPPING_FEE",
"price": {
"amount": 0.0,
"priceType": "DueToday",
"taxDetail": {
"preCalculatedTax": {
"taxableCost": 0.0,
"taxableMSRP": 14.95,
"taxableUnitPrice": 0.0,
"orderTaxAreaId": 390950000,
"shipFromTaxAreaId": 431570560,
"shipToTaxAreaId": 180091237
},
"lineItemTaxes": [{
"taxLineId": 2,
"taxCode": "SALESTAX",
"memo": "SALESTAX",
"printName": "Sales Tax",
"taxable": "N",
"skuSpecific": "false",
"jurisdictionLevel": "Sales Tax",
"jurisdictionName": "Sales Tax",
"taxAmount": 0.0,
"taxRate": 0.0,
"taxDate": "2017-06-05"
}]
},
"total": 14.95
},
"payments": {
"payment": [{
"paymentOptionReference": "PAYMENT_OPTION_01"
}]
},
"quantity": 1,
"groupReferences": {
"groupReference": ["GROUP_01"]
}
}]
},
"names": {
"name": [{
"id": "NAME_01",
"firstName": "ABC",
"lastName": "QWE",
"emailAddress": "XXXXXXXX@GMAIL.COM",
"primaryContactPhones": [{
"phoneNumber": "1111108029",
"contactPhoneType": "HOME_PHONE"
},
{
"phoneNumber": "1111111111",
"contactPhoneType": "WORK_PHONE"
}]
}]
},
"addresses": {
"address": [{
"id": "ADDRESS_01",
"unparsedAddress": {
"addressLine1": "1000 W SPRINGA VLLY Dr",
"city": "CAVE CITY",
"state": "KY",
"zip": "75001",
"country": "US"
},
"additionalDetails": {
"additionalDetail": [{
"code": "ValidatedAddress",
"value": "true"
}]
}
},
{
"id": "ADDRESS_02",
"unparsedAddress": {
"addressLine1": "1000 W SPRINGA VLLY Dr",
"city": "CAVE CITY",
"state": "KY",
"zip": "75001",
"country": "US"
},
"additionalDetails": {
"additionalDetail": [{
"code": "ValidatedAddress",
"value": "true"
}]
}
},
{
"id": "ADDRESS_03",
"unparsedAddress": {
"addressLine1": "1000 W SPRINGA VLLY Dr",
"city": "CAVE CITY",
"state": "KY",
"zip": "75001",
"country": "US"
},
"additionalDetails": {
"additionalDetail": [{
"code": "ValidatedAddress",
"value": "true"
}]
}
},
{
"id": "ADDRESS_04",
"unparsedAddress": {
"addressLine1": "1000 W SPRINGA VLLY Dr",
"addressLine2": "",
"city": "CHICAGO",
"state": "IL",
"zip": "75001",
"county": "COOK",
"country": "US"
},
"parsedAddress": {
"addressStreetLine": "350 N ORLEANS ST STE 1270",
"houseNumber": "350",
"streetName": "ORLEANS",
"streetType": "ST",
"city": "CHICAGO",
"state": "IL",
"zip": "75001",
"zipCodeExtension": "2148",
"county": "COOK",
"country": "COOK"
},
"additionalDetails": {
"additionalDetail": [{
"code": "ValidatedAddress",
"value": "true"
}]
}
}]
},
"accounts": {
"account": [{
"id": "ACCOUNT_01",
"sequence": 1,
"accountCategory": "MOBILITY_ACCOUNT",
"accountSubCategory": "NEW",
"paymentArrangement": "PAYPAL",
"billingAccountNumber": "992863227376",
"billingDetail": [{
"nameReference": "NAME_01",
"addressReference": "ADDRESS_03",
"authentication": {
"dob": "DQS~-Lr-=.",
"driversLicense": {
}
In the above JSON falloutHistory is optional and it comes only if there any validation error while checking on the other system.
Using my plain blank mind after reading - I have to just check only the key elements present or not
like below I specified the DslPart
DslPart body = new PactDslJsonBody()
.stringType("customerOrderNumber")
.stringType("orderType")
.object("productGroups")
.eachKeyMappedToAnArrayLike("group")
.stringType("id")
.eachKeyMappedToAnArrayLike("price")
//.closeArray()
.closeObject()
.eachLike("lineItems")
.eachKeyMappedToAnArrayLike("lineItem")
.numberType("quantity")
.object("price")
.object("taxDetail")
.closeObject()
.closeObject()
.closeArray()
.object("payments")
.eachKeyMappedToAnArrayLike("payment")
.closeArray()
.closeObject()
.object("promotionReferences")
.closeObject()
.object("characteristics")
.closeObject()
.closeObject()
.object("names")
.eachKeyMappedToAnArrayLike("name")
.eachKeyMappedToAnArrayLike("primaryContactPhones")
.closeArray()
.closeArray()
.closeObject()
.object("addresses")
.eachKeyMappedToAnArrayLike("address")
.closeArray()
.closeObject()
.object("accounts")
.eachKeyMappedToAnArrayLike("account")
.eachKeyMappedToAnArrayLike("billingDetail")
.closeArray()
.closeArray()
.closeObject()
.object("paymentOptions")
.eachKeyMappedToAnArrayLike("paymentOption")
.closeArray()
.closeObject()
.object("promotions")
.eachKeyMappedToAnArrayLike("promotion")
.closeArray()
.closeObject()
.object("shippingDetails")
.eachKeyMappedToAnArrayLike("shippingDetail")
.closeArray()
.closeObject()
.object("b2bs")
.eachKeyMappedToAnArrayLike("b2b")
.closeArray()
.closeObject()
.object("totalPrice")
.eachKeyMappedToAnArrayLike("price")
.closeArray()
.closeObject()
.object("termsAndConditions")
.eachKeyMappedToAnArrayLike("termsAndConditionAccepted")
.closeArray()
.closeObject()
.object("orderSource")
.stringType("clientType")
.closeObject()
;
Basically I am checking objects within the JSON with available in array and some key elements
When i write it for first object I got the below JSON pact, when I started adding other object check it is giving me error.
Generated PACT JSON and error below:
PACT JSON
{
"provider": {
"name": "product-provider-demo"
},
"consumer": {
"name": "product-consumer-demo"
},
"interactions": [
{
"description": "ConsumerDemoTest interaction",
"request": {
"method": "GET",
"path": "/product"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json;charset=UTF-8"
},
"body": {
"customerOrderNumber": "BFxFcnuvlTtXcjPmeIuf",
"orderType": "rbLluporifHQkAbkGbmo",
"productGroups": {
"group": [
{
"id": "TJtIEnClGyZRgPCZRoUX",
"price": [
{
}
Error:
java.lang.UnsupportedOperationException: use the eachLike() form
at au.com.dius.pact.consumer.dsl.PactDslJsonArray.eachLike(PactDslJsonArray.java:68)
at product_demo.ConsumerDemoTest3.createFragment(ConsumerDemoTest3.java:59)
at au.com.dius.pact.consumer.ConsumerPactTest.testPact(ConsumerPactTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
I tried various ways of defining and closing but not able to find the correct or easier way to check.
ConsumerDemoTest3.txt
The text was updated successfully, but these errors were encountered: