1

I am testing endpoint with path para and query para and updating the request with some parameters. When I send the request content type is json and request looks good with the data that i am trying to change however when response is received

content type is text and i get 400 error bad request as status code and error message

javax.ws.rs.ProcessingException: RESTEASY008200: JSON Binding deserialization error: javax.json.bind.JsonbException: Can't infer a type for unmarshalling into: java.util.List

my code

@Given("^api is set up with required details$")
public void api_is_set_up_with_user_data() throws Throwable {
    loadProp();
    base_url = prop.getProperty("baseurl");
    RestAssured.baseURI = base_url;

}

@When("^a user retrieves the preferences by userid \"([^\"]*)\" and type \"([^\"]*)\"$")
public void a_user_retrieves_the_preferences_by_userid_and_type(String userid, String parameter) throws Throwable {
    request = given().pathParam("user_id", userid).queryParam("type", parameter);
    System.out.println(request);
    ENDPOINT_GET_USER_BY_ID = base_url + "{user_id}/preferences";
    response = request.when().get(ENDPOINT_GET_USER_BY_ID);
    System.out.println("response: " + response.prettyPrint());
}

@Then("^updates the value \"([^\"]*)\" of name \"([^\"]*)\"$")
public void updates_the_value_of_name(String value, String displaytext) throws Throwable {
    HashMap<String,String> post = new HashMap<String,String>();
    post.put("displaytext",displaytext);
    post.put("value",value);
    response = request.contentType("application/json").accept("*/*").body(post).put(ENDPOINT_GET_USER_BY_ID);
//        response = request.header("Content-Type", "application/json").body(post).put(ENDPOINT_GET_USER_BY_ID);

    System.out.println("Response : " + response.asString());
    System.out.println("Statuscode : " +response.getStatusCode());

}

enter image description here enter image description here

Ricky
  • 73
  • 1
  • 12
  • Can you show the said `PUT` endpoint, and its definition? How is it declared in the controller maybe? It seems there is a `List` definition in its RQ body, but you are not sending one, your RQ body in test must be incorrect. Maybe rather than `{}`, you have to send `[ {} ]`, so a list in the root? – buræquete Jun 28 '19 at 00:40
  • endpoint is http://localhost:8080/users/variable1/preferences?type=somevariable request = given().pathParam("variable1", variabble1).queryParam("somepara", parameter); – Ricky Jun 28 '19 at 03:25
  • [ { "displayText": "", "preferences": [ { "category": "", "displaytext": "", } ], "priority": "20" }, { "mandatory":, "name": "", “preferences”: [ { "category": "", "displaytext": "", }, { "category": "", "value": }, ], "priority": "30" } ] – Ricky Jun 28 '19 at 03:30

1 Answers1

2

As you have shared in your comment there, the endpoint is expecting a list of objects, not a singular object as you are sending... Just try wrapping that with a list, and you'd get past 400 error.

What you are sending;

{
    "displayText": "Warrants", 
    "value": "true"  // I don't know about this value field here
}

What is expected, as you've shared;

[ 
    {
        "displayText": "", 
        "preferences": [ { "category": "", "displaytext": "", } ], 
        "priority": "20" 
    }
] 

One issue is that you have to send the object in a list, also passing objects as map is also somewhat counter productive, better to use the same object used in the RQ.

public class Request {

    private String displayText;
    private List<Preference> preferences;
    private Integer priority;

    //getter, setter,etc
}

& use it in your body in the rest assured test;

List<Request> requestList = new ArrayList<>();
Request request = new Request();
request.setDisplayText("etc");
... // set other stuff
requestList.add(request);
response = request.contentType("application/json").accept("*/*").body(requestList).put(ENDPOINT_GET_USER_BY_ID);
buræquete
  • 14,226
  • 4
  • 44
  • 89
  • I tried this JsonArray arr = new JsonArray(); JsonObject obj = new JsonObject(); JsonObject requestparams = new JsonObject(); requestparams.addProperty("displayText", ""); requestparams.addProperty("name", "Products"); obj.addProperty("displaytext",displaytext); obj.addProperty("value",value); arr.add(obj); requestparams.add("preferences", obj); however again i had same issue.. thanks for your reply...will try your solution and let you know – Ricky Jun 28 '19 at 04:14
  • Not yet.. its a bit long request so creating getter setter for them.. thats what I understand.. is it possible to read json file... change it to string and replace the node value and use it to hit endpoint??? – Ricky Jun 28 '19 at 05:37
  • not exactly.. will post the exact solution tomorrow in the morning once i reach my desk... thanks for your help and following up....... your solution helped me drill down further and find exact solution. thanks – Ricky Jul 07 '19 at 12:43
  • JSONArray responseJson = new JSONArray(response.asString()); for (int idx = 0; idx < responseJson.length(); idx++) { JSONObject subCat = responseJson.getJSONObject(idx); JSONArray prefs = subCat.getJSONArray("preferences"); for (int prefidx =0; prefidx < prefs.length(); prefidx++) { JSONObject pref = prefs.getJSONObject(prefidx); if (pref.getString("name").equals(displaytext)) { // Match, update value pref.put("value", value); } } } – Ricky Jul 09 '19 at 04:12