I have a REST
endpoint as
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getVariables(@QueryParam("_activeonly") @DefaultValue("no") @Nonnull final Active active) {
switch(active){
case yes:
return Response.ok(VariablePresentation.getPresentationVariables(variableManager.getActiveVariables())).build();
case no:
return Response.ok(VariablePresentation.getPresentationVariables(variableManager.getVariables())).build();
}
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
Which returns JSON
of List
of VariablePresentation
. The VariablePresentaion
looks like
@XmlRootElement
public class VariablePresentation {
private final UUID id;
private final String name;
private final VariableType type;
public VariablePresentation(@Nonnull final Variable variable) {
id = variable.getId();
name = variable.getName();
type = variable.getType();
}
public String getId() {
return id.toString();
}
@Nonnull
public String getName() {
return name;
}
@Nonnull
public VariableType getType() {
return type;
}
annotated with JAXB
's XmlRoot
to return JSON
.
My integration test is as following
@Test
public void testGetAllVariablesWithoutQueryParamPass() throws Exception {
final ClientRequest clientCreateRequest = new ClientRequest("http://localhost:9090/variables");
final MultivaluedMap<String, String> formParameters = clientCreateRequest.getFormParameters();
final String name = "testGetAllVariablesWithoutQueryParamPass";
formParameters.putSingle("name", name);
formParameters.putSingle("type", "String");
formParameters.putSingle("units", "units");
formParameters.putSingle("description", "description");
formParameters.putSingle("core", "true");
final ClientResponse<String> clientCreateResponse = clientCreateRequest.post(String.class);
assertEquals(201, clientCreateResponse.getStatus());
}
I want to test the request body which returns the List<VariablePresentation>
as String. How can I convert the response body (String) as VariablePresentation
Object?
Update
After adding the following
final GenericType<List<VariablePresentation>> typeToken = new GenericType<List<VariablePresentation>>() {
};
final ClientResponse<List<VariablePresentation>> clientCreateResponse = clientCreateRequest.post(typeToken);
assertEquals(201, clientCreateResponse.getStatus());
final List<VariablePresentation> variables = clientCreateResponse.getEntity();
assertNotNull(variables);
assertEquals(1, variables.size());
Its fails with different Error
testGetAllVariablesWithoutQueryParamPass(com.myorg.project.market.integration.TestVariable): Unable to find a MessageBodyReader of content-type application/json and type java.util.List<com.myorg.project.service.presentation.VariablePresentation>
How do I resolve this?