0

I want to deserialize Json array in the Rest-Assure respons into TodoList with Todo object as elements in the List. I have a class Todo

import java.util.Date;

public class Todo   {
    
    private Long id;
    
    private String name;
    
    private String description;
    
    private Date targetDate;
    
    private Boolean isDone;
    
    protected Todo() {
        
    }
    
    
    
    /**
        * Get the id of the bank.
        * @return id for the bank
        */
    public Long getId() {
            return id;
        }
    
    
    
    public Todo(String name, String description, Date targetDate, boolean isDone) {
        this.name = name;
        this.description = description;
        this.targetDate = targetDate;
        this.isDone = isDone;
    }



    public void setName( String name) {
         this.name = name;
    }
    
    public String getName() {
        return this.name;
    }



    public String getDescription() {
        return this.description;
    }



    public Date getTargetDate() {
        return this.targetDate;
    }



    public Boolean getIsDone() {
        return this.isDone;
    }



    public void setId(Long id) {
        this.id = id;
    }



    public void setDescription(String description) {
        this.description = description;
    }



    public void setTargetDate(Date targetDate) {
        this.targetDate = targetDate;
    }



    public void setIsDone(Boolean isDone) {
        this.isDone = isDone;
    }

}

Objects of this class is manage in a ListTodo class

package com.steinko.todo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class TodoList {
    
    private List<Todo> todos = new ArrayList<>();
    
    public void addTodo(Todo todo) {
        this.todos.add(todo);
    }
    
    public void addAllTodos(Collection<Todo> todos) {
        this.todos.addAll(todos);
    }
     
    public List<Todo> booksByAuthor(String author) {
        return todos.stream()
          .filter(book -> Objects.equals(author, book.getName()))
          .collect(Collectors.toList());
    }

}

I want that the Rest Assure query shuold deliver a TodoList. I want to convert from a Json array into TodoList How do I do that

TodoList result =  given().
                    when().
                      get("/user/stein/todos").??????
stein korsveien
  • 1,047
  • 5
  • 13
  • 35

2 Answers2

1

After get request you can extract Response. Then, you can grab JsonPath and with the help of ObjectMapper you can transform any JSON to POJO

Based on your code:

TodoList result =  given().
                    when().
                      get("/user/stein/todos")
                    then().
                    extract().
                    response(). //here's the Response object of Rest Assured
                    jsonPath(). //calling json path on response
                    getObject("$", TodoList.class);

The line

getObject("$", TodoList.class);

tells JSON Path to transform JSON into TodoList.class object starting with root of JSON. If you have response like this:

{
   "someKey": {
      "someOtherKey": {
          //here's todo list
       }

   }
}

then you can use someKey.someOtherKey instead of $. It's like telling Object Mapper where to start JSON transformation

Fenio
  • 3,528
  • 1
  • 13
  • 27
0

Try this:

TodoList result =  given()
                    .when()
                    .get("/user/stein/todos")
                    .then()
                    .extract()
                    .as(ToDoList.class);
Villa_7
  • 508
  • 4
  • 14