1

I am trying to extract Json response as a list of type string in the code below-

List<String> titleTypeAssertions = response.getBody().path("content.items.type");

upon System.out.println(titleTypeAssertions); below is the result- it is clearly evident that the list is being stored in variable titleTypeAssertions

[[image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, gallery, image, image, image, image, image, image, image, image, image, image, image, image, image, image, page, page, page, page, page, page, image, article, image, image, image, article, image, image, image, image, image, augmPhysStore, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, image, article, image, page, article, video, image, image, article, article, page, video, image, image, article, page, article, image, image, image, page, page, image, page, image, image, page, page, page, article, article, page, article, page, page, image, image, page, augmentedCategory, augmentedCategory, augmentedCategory, article, video, page, article, article, gallery, gallery, gallery, gallery, gallery, article, article, article, article, article, article, article, video, page, video, page, article, image, image, image, page, image, image, image, image, page, video, image, video, image, image, image, image, video, video, video, video, video, page, page, page, page, page, page, page, page, page, page, image, image, image, image, image, image, image, image, image, page, image, video, image, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, page, video, video, page, page, page, image, image, video, article, article, page, image, article, article, page, gallery, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, image, image, image, image, image, image, image, image, image, video, video, image, image, image, image, image, image, image, article, image, article, video, video, image, image, image, image, article, image, article, page, image, image, image, image, video, video, video, video, video, video, video, video, video, video, video, video]]

but when i try this assertion-

Assert.assertTrue(titleTypeAssertions.contains("image"));

it throws the following error-

java.lang.AssertionError at com.salmon.test.step_definitions.api.coremedia.ComponentApiSteps.theApiShouldReturnsValidJsonResponseWithStatusAndContainTitleInAscendingOrder(ComponentApiSteps.java:127)

not sure what seems to be the issue here.

this is a bit of json response being shown to verify the path in(response.getBody().path("content.items.type");-

{
"contentKey": "sorting",
"version": "1.2",
"lang": "ru_RU",
"country": "ru",
"content": [
{
"type": "queryList",
"pageNumber": 1,
"items": [
{
"type": "image",
"thumbnailUrl": "http://coremedia.imagesphere.dev.product.ext.net-a-porter.com/testfolder2/6/w84.jpg",
"externalDamItemId": "imagesphere-ton:///dam/picture/testfolder2-6.jpg",
"title": "6.jpg",
"externalDamItemName": "6.jpg",
"mimeType": "image/jpg",
"imageUrl": "http://coremedia.imagesphere.dev.product.ext.net-a-porter.com/testfolder2/6/w500.jpg",
"focusPoint": {
"x": "0.5",
"y": "0.5"
},
"focusArea": {
"x1": "0.0",
"y1": "0.0",
"x2": "1.0",
"y2": "1.0"
},
"callToActionEnabled": true,
"locale": "ru-RU"
},
{
"type": "image",
"externalDamItemId": "throntestbw3:///dam/picture/c356c820-f70b-4099-877d-d645a545cd71",
"mimeType": "image/jpeg",
"imageUrl": "/ycm/resource/blob/121826/21D4C5DE5ECAB9B6C1A98FE8DD710347/hc-jpg-data.jpg",
"focusPoint": {
"x": "0.5",
"y": "0.5"
},
"focusArea": {
"x1": "0.0",
"y1": "0.0",
"x2": "1.0",
"y2": "1.0"
},
"callToActionEnabled": true,
"locale": "ru-RU"
},
{

any help would be greatly appreciated! thank you!

further to it, i changed the path and tried -

List<String> titleTypeAssertions = response.getBody().path("content.items.type[0]");

these assertions seem to work-

assertThat(titleTypeAssertions.contains("image"),is(true));
    assertThat(titleTypeAssertions.contains("gallery"),is(true));
    assertThat(titleTypeAssertions.contains("article"),is(true));
assertThat(titleTypeAssertions.contains("augmentedCategory"),is(true);
    assertThat(titleTypeAssertions.contains("video"),is(true)); 
     assertThat(titleTypeAssertions.contains("augmPhysStore"),is(true));

but not this -

assertThat(titleTypeAssertions,containsInAnyOrder("image","gallery","article","video","augmPhysStore","augmentedCategory"));

it throws the following error-

    java.lang.AssertionError:
Expected: iterable over ["image", "gallery", "article", "video", "augmPhysStore", "augmentedCategory"] in any order
     but: Not matched: "image"
  at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
Raj
  • 95
  • 1
  • 3
  • 13
  • I am wondering why are you getting two square brackets [[ around your list? Can you try to print size of list? – codeLover Nov 16 '18 at 10:50
  • Hi there,Thanks for the reply. you are right there seems to be an issue with the path. the count is returning as 1. but when i tried List titleTypeAssertions = response.getBody().path("content.items.type[0]"); the count is 270 – Raj Nov 18 '18 at 13:20

0 Answers0