Skip to content
Advertisement

Lombok returns null as a value of response

I’ve got a problem with my Api tests.

When i’m trying to get data from api, lombok returns null as an acceptance value, but there are values with real numbers in api.

Screenchot: https://prnt.sc/w98nt2

My DTO for the responce:

 @Data
 @Builder
 @EqualsAndHashCode
 @NoArgsConstructor
 @AllArgsConstructor
 @JsonIgnoreProperties(ignoreUnknown = true) 
public class PositionStatResponceDto {
private Integer keywordTasksCount;
private Integer doneKeywordTasksCount;
private Integer tasksCount;
private Integer doneTasks;
}

My steps that extacts body and send post request public class PositionSteps {

PositionsController positionsController = new PositionsController();

@Step("Post body with url: http://prod.position.bmp.rocks/api/aparser/get-statistic")
public PositionStatResponceDto postBody(PositionStatDto positionStatDto) {
    return positionsController
            .getStatistic(positionStatDto)
            .statusCode(200)
            .extract().body().as(PositionStatResponceDto.class);
}


}

Api json responce getting properly. it means that the request working right:

  {
"period": {
    "20201224": {
        "startTime": "2020-12-24 00:00:19",
        "endTime": "2020-12-24 06:39:30",
        "totalRequestsCount": 0,
        "totalQueriesCount": 161887,
        "totalQueriesDoneCount": 161887,
        "totalFailCount": 161,
        "successfulQueries": 161726,
        "proxiesUsedCount": 6.49,
        "retriesUsedCount": 0,
        "avgSpeed": 13.74,
        "tasksCount": 1537,
        "doneTasks": 1537,
        "keywordTasksCount": 725,
        "doneKeywordTasksCount": 725,
        "runTime": "06:39:11",
        "avgTimePerKeyword": 0.15,
        "keywordsLost": 0.1
    }
},
"avg": {
    "totalRequestsCount": 0,
    "totalQueriesCount": 161887,
    "totalQueriesDoneCount": 161887,
    "totalFailCount": 161
}
}

I made post request in the similar way to the api:

           {
"success": 1,
"data": {
    "45.90.34.87:59219": [
        "http"
    ],
    "217.172.179.54:39492": [
        "http"
    ],
    "144.76.108.82:35279": [
        "http"
    ],
    "5.9.72.48:43210": [
        "http"
    ],
    "144.76.108.82:47165": [
        "http"
    ],
    "45.90.34.87:57145": [
        "http"
    ],
    "144.76.108.82:53108": [
        "http"
    ], 
         ... 
            } }

And it works correctly with dto:

        @Data
        @Builder
        @EqualsAndHashCode(exclude = "success")
        @NoArgsConstructor
        @AllArgsConstructor
        @JsonIgnoreProperties(ignoreUnknown = true)
        public class AparsersResponceDto {
        private Integer success;
        private Map<String, List<String>> data;
            }

Help me please. I can’t understand what’s wrong with the first example. Each of the Dto values returs ‘null’.

Advertisement

Answer

Your DTO does not match the structure of the response you are parsing. You have a nested structure where on DTO you are expecting only to receive primitive values. On upper level you have a structure with two fields.

{
   "period": {...},
   "avg": {...}
}

From the example I would assume that period is a key-value pair of dates as a key and your PositionStatResponceDto as a value.

{
  "period" : {
     "20201224": { <-- nested key with a value matching your DTO
         PositionStatResponceDto 
      }
   ...
}

So this means only single item in the key-value pairs match the DTO you have defined but is ignoring all other nested structure elements. For this it would make sense to introduce new wrapper DTO to handle the nested structure. e.g.

public class StatDTO {
    private Map<String,PositionStatResponceDto> period;
    //add avg if needed
}

Advertisement