package org.javersion.object;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;

import java.util.List;
import java.util.Map;

import org.javersion.object.PolymorphismTest.Cat;
import org.javersion.object.PolymorphismTest.Dog;
import org.javersion.object.PolymorphismTest.Pet;
import org.javersion.path.PropertyPath;
import org.junit.Test;

import com.google.common.collect.Lists;

public class ListTest {

    @Versionable
    public static class Owner {
        private List<Pet> pets;
    }

    private ValueTypes valueTypes = ValueTypes.builder()
            .withClass(Pet.class)
            .havingSubClasses(Dog.class, Cat.class)
            .build();
    
    private final ObjectSerializer<Owner> serializer = new ObjectSerializer<>(Owner.class, valueTypes);

    @Test
    public void Write_And_Read_Owner_With_Cats_And_Dogs() {
        Owner owner = new Owner();
        owner.pets = Lists.newArrayList(new Cat("Mirri"), new Dog("Musti"));
        Map<PropertyPath, Object> map = serializer.write(owner);
        
        owner = serializer.read(map);
        assertThat(owner.pets, hasSize(2));
        
        assertThat(owner.pets.get(0), instanceOf(Cat.class));
        assertThat(owner.pets.get(0).name, equalTo("Mirri"));
        assertThat(((Cat) owner.pets.get(0)).meow, equalTo(true));
        
        assertThat(owner.pets.get(1), instanceOf(Dog.class));
        assertThat(owner.pets.get(1).name, equalTo("Musti"));
        assertThat(((Dog) owner.pets.get(1)).bark, equalTo(true));
    }

}

@@ -162,16 +162,63 @@
 anceOf;%0A
+import static org.hamcrest.Matchers.nullValue;%0A
 import s
@@ -1113,32 +1113,38 @@
 new Dog(%22Musti%22)
+, null
 );%0A        Map%3CP
@@ -1285,9 +1285,9 @@
 ize(
-2
+3
 ));%0A
@@ -1682,21 +1682,82 @@
 lTo(true));%0A
+        %0A        assertThat(owner.pets.get(2), nullValue());%0A
     %7D%0A%0A%7D%0A

Test null value in a list