Search in sources :

Example 1 with ObjectMapperConfig

use of io.restassured.config.ObjectMapperConfig in project rest-assured by rest-assured.

the class CustomObjectMappingITest method using_default_object_mapper_type_if_specified.

@Test
public void using_default_object_mapper_type_if_specified() {
    final Message message = new Message();
    message.setMessage("A message");
    RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig(GSON));
    final Message returnedMessage = given().body(message).when().post("/reflect").as(Message.class);
    assertThat(returnedMessage.getMessage(), equalTo("A message"));
}
Also used : ObjectMapperConfig(io.restassured.config.ObjectMapperConfig) Message(io.restassured.itest.java.objects.Message) Test(org.junit.Test)

Example 2 with ObjectMapperConfig

use of io.restassured.config.ObjectMapperConfig in project rest-assured by rest-assured.

the class CustomObjectMappingITest method using_as_specified_object.

@Test
public void using_as_specified_object() {
    final Message message = new Message();
    message.setMessage("A message");
    RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig(GSON));
    final String returnedMessage = given().body(message).when().post("/reflect").as(Message.class).getMessage();
    assertThat(returnedMessage, equalTo("A message"));
}
Also used : ObjectMapperConfig(io.restassured.config.ObjectMapperConfig) Message(io.restassured.itest.java.objects.Message) Test(org.junit.Test)

Example 3 with ObjectMapperConfig

use of io.restassured.config.ObjectMapperConfig in project rest-assured by rest-assured.

the class MultiPartSpecBuilder method applyContentToSpec.

private void applyContentToSpec(MultiPartSpecificationImpl spec) {
    final Object actualContent;
    if (explicitObjectMapper != null) {
        ObjectMapperSerializationContextImpl ctx = new ObjectMapperSerializationContextImpl();
        ctx.setObject(content);
        ctx.setContentType(mimeType);
        actualContent = explicitObjectMapper.serialize(ctx);
    } else if (explicitObjectMapperType != null) {
        actualContent = ObjectMapping.serialize(content, mimeType, null, explicitObjectMapperType, new ObjectMapperConfig(), new EncoderConfig());
    } else {
        actualContent = content;
    }
    spec.setContent(actualContent);
}
Also used : ObjectMapperSerializationContextImpl(io.restassured.internal.mapping.ObjectMapperSerializationContextImpl) ObjectMapperConfig(io.restassured.config.ObjectMapperConfig) EncoderConfig(io.restassured.config.EncoderConfig)

Example 4 with ObjectMapperConfig

use of io.restassured.config.ObjectMapperConfig in project rest-assured by rest-assured.

the class TypeObjectMappingITest method shouldUseMapTypeWithObjectMappers.

@Test
public void shouldUseMapTypeWithObjectMappers() {
    String expected = "A message";
    final Message message = new Message();
    message.setMessage(expected);
    RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig(mapperType));
    Type type = new TypeToken<Map<String, String>>() {
    }.getType();
    final Map<String, String> returnedMessage = given().body(message).when().post("/reflect").as(type);
    assertThat(returnedMessage.get("message"), equalTo(expected));
}
Also used : ObjectMapperConfig(io.restassured.config.ObjectMapperConfig) Type(java.lang.reflect.Type) ObjectMapperType(io.restassured.mapper.ObjectMapperType) Message(io.restassured.itest.java.objects.Message) Map(java.util.Map) Test(org.junit.Test)

Example 5 with ObjectMapperConfig

use of io.restassured.config.ObjectMapperConfig 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

ObjectMapperConfig (io.restassured.config.ObjectMapperConfig)5 Message (io.restassured.itest.java.objects.Message)4 Test (org.junit.Test)4 EncoderConfig (io.restassured.config.EncoderConfig)1 ObjectMapperSerializationContextImpl (io.restassured.internal.mapping.ObjectMapperSerializationContextImpl)1 ObjectMapper (io.restassured.mapper.ObjectMapper)1 ObjectMapperDeserializationContext (io.restassured.mapper.ObjectMapperDeserializationContext)1 ObjectMapperSerializationContext (io.restassured.mapper.ObjectMapperSerializationContext)1 ObjectMapperType (io.restassured.mapper.ObjectMapperType)1 Type (java.lang.reflect.Type)1 Map (java.util.Map)1