org.jddd.jackson.SimpleValueObjectSerializerModifierUnitTests.java Source code

Java tutorial

Introduction

Here is the source code for org.jddd.jackson.SimpleValueObjectSerializerModifierUnitTests.java

Source

/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jddd.jackson;

import static org.assertj.core.api.Assertions.*;

import lombok.Value;

import org.jddd.annotation.ValueObject;
import org.jddd.jackson.DddModule;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;

/**
 * @author Oliver Gierke
 */
public class SimpleValueObjectSerializerModifierUnitTests {

    @Test
    public void automaticallyUnwrapsSinglePropertyValueObjects() throws Exception {

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new DddModule());

        EmailAddress email = EmailAddress.of("foo@bar.com");
        String result = mapper.writeValueAsString(new Wrapper(email));

        System.out.println(result);

        assertThat(JsonPath.<String>read(result, "$.email")).isEqualTo("foo@bar.com");
    }

    @Value
    static class Wrapper {
        EmailAddress email;
    }

    @Value(staticConstructor = "of")
    @ValueObject
    static class EmailAddress {
        String emailAddress;
    }
}