/*
 * Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
 *
 * 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.jongo.marshall.jackson;

import org.jongo.model.Fox;
import org.jongo.model.People;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.fest.assertions.Assertions.assertThat;

public class JacksonProcessorTest {

    private JacksonProcessor processor;

    @Before
    public void setUp() throws Exception {
        processor = new JacksonProcessor();
    }

    @Test
    public void canConvertEntityToJson() {
        String json = processor.marshall(new Fox("fantastic", "roux"));
        assertThat(json).isEqualTo(jsonify("{'_class':'org.jongo.model.Fox','name':'fantastic','color':'roux'}"));

        People people = processor.unmarshall(json, People.class);
        assertThat(people.getName()).isEqualTo("fantastic");
    }

    @Test
    public void canConvertJsonToEntity() throws IOException {
        String json = jsonify("{'address': '22 rue des murlins'}");

        People people = processor.unmarshall(json, People.class);

        assertThat(people.getAddress()).isEqualTo("22 rue des murlins");
    }

    @Test
    public void canConvertNestedJsonToEntities() throws IOException {
        String json = jsonify("{'address': '22 rue des murlins', 'coordinate': {'lat': 48}}");

        People people = processor.unmarshall(json, People.class);

        assertThat(people.getCoordinate().lat).isEqualTo(48);
    }

    private String jsonify(String json) {
        return json.replace("'", "\"");
    }
}

@@ -712,24 +712,76 @@
 l.jackson;%0A%0A
+import org.codehaus.jackson.annotate.JsonAnySetter;%0A
 import org.j
@@ -2130,85 +2130,737 @@
     
-private String jsonify(String json) %7B%0A        return json.replace(%22'%22, %22%5C%22%22);
+@Test%0A    public void hasAFallbackToEnsureBackwardCompatibility() throws IOException %7B%0A%0A        String json = jsonify(%22%7B'address': '22 rue des murlins', 'oldAddress': '22-rue-des-murlins'%7D%22);%0A%0A        BackwardPeople backwardPeople = processor.unmarshall(json, BackwardPeople.class);%0A%0A        assertThat(backwardPeople.getAddress()).isEqualTo(%2222-rue-des-murlins%22);%0A    %7D%0A%0A    private String jsonify(String json) %7B%0A        return json.replace(%22'%22, %22%5C%22%22);%0A    %7D%0A%0A%0A    static class BackwardPeople extends People %7B%0A%0A        @JsonAnySetter%0A        public void fallbackForBackwardCompatibility(String name, Object value) %7B%0A            if (%22oldAddress%22.equals(name)) %7B%0A                setAddress((String) value);%0A            %7D%0A        %7D
 %0A   

Add a test to handle old properties