Search in sources :

Example 1 with ObjectMapperDeserializationContext

use of io.restassured.mapper.ObjectMapperDeserializationContext in project rest-assured by rest-assured.

the class CustomObjectMappingITest method using_explicit_custom_object_mapper.

@Test
public void using_explicit_custom_object_mapper() throws Exception {
    final Message message = new Message();
    message.setMessage("A message");
    final ObjectMapper mapper = new ObjectMapper() {

        public Object deserialize(ObjectMapperDeserializationContext context) {
            final String toDeserialize = context.getDataToDeserialize().asString();
            final String unquoted = StringUtils.remove(toDeserialize, "#");
            final Message message = new Message();
            message.setMessage(unquoted);
            customDeserializationUsed.set(true);
            return message;
        }

        public Object serialize(ObjectMapperSerializationContext context) {
            final Message objectToSerialize = context.getObjectToSerializeAs(Message.class);
            final String message = objectToSerialize.getMessage();
            customSerializationUsed.set(true);
            return "##" + message + "##";
        }
    };
    final Message returnedMessage = given().body(message, mapper).when().post("/reflect").as(Message.class, mapper);
    assertThat(returnedMessage.getMessage(), equalTo("A message"));
    assertThat(customSerializationUsed.get(), is(true));
    assertThat(customDeserializationUsed.get(), is(true));
}
Also used : Message(io.restassured.itest.java.objects.Message) ObjectMapperDeserializationContext(io.restassured.mapper.ObjectMapperDeserializationContext) ObjectMapperSerializationContext(io.restassured.mapper.ObjectMapperSerializationContext) ObjectMapper(io.restassured.mapper.ObjectMapper) Test(org.junit.Test)

Example 2 with ObjectMapperDeserializationContext

use of io.restassured.mapper.ObjectMapperDeserializationContext in project rest-assured by rest-assured.

the class CustomObjectMappingITest method using_custom_object_mapper_statically.

@Test
public void using_custom_object_mapper_statically() {
    final Message message = new Message();
    message.setMessage("A message");
    final ObjectMapper mapper = new ObjectMapper() {

        public Object deserialize(ObjectMapperDeserializationContext context) {
            final String toDeserialize = context.getDataToDeserialize().asString();
            final String unquoted = StringUtils.remove(toDeserialize, "##");
            final Message message = new Message();
            message.setMessage(unquoted);
            customDeserializationUsed.set(true);
            return message;
        }

        public Object serialize(ObjectMapperSerializationContext context) {
            final Message objectToSerialize = context.getObjectToSerializeAs(Message.class);
            final String message = objectToSerialize.getMessage();
            customSerializationUsed.set(true);
            return "##" + message + "##";
        }
    };
    RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig(mapper));
    final Message returnedMessage = given().body(message).when().post("/reflect").as(Message.class);
    assertThat(returnedMessage.getMessage(), equalTo("A message"));
    assertThat(customSerializationUsed.get(), is(true));
    assertThat(customDeserializationUsed.get(), is(true));
}
Also used : ObjectMapperConfig(io.restassured.config.ObjectMapperConfig) Message(io.restassured.itest.java.objects.Message) ObjectMapperDeserializationContext(io.restassured.mapper.ObjectMapperDeserializationContext) ObjectMapperSerializationContext(io.restassured.mapper.ObjectMapperSerializationContext) ObjectMapper(io.restassured.mapper.ObjectMapper) Test(org.junit.Test)

Aggregations

Message (io.restassured.itest.java.objects.Message)2 ObjectMapper (io.restassured.mapper.ObjectMapper)2 ObjectMapperDeserializationContext (io.restassured.mapper.ObjectMapperDeserializationContext)2 ObjectMapperSerializationContext (io.restassured.mapper.ObjectMapperSerializationContext)2 Test (org.junit.Test)2 ObjectMapperConfig (io.restassured.config.ObjectMapperConfig)1