/*
 * 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;

import org.jongo.model.Animal;
import org.jongo.model.Fox;
import org.jongo.util.JongoTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

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

public class PolymorphismTest extends JongoTestCase {

    private MongoCollection collection;

    @Before
    public void setUp() throws Exception {
        collection = createEmptyCollection("friends");
    }

    @After
    public void tearDown() throws Exception {
        dropCollection("friends");
    }

    @Test
    public void canFindInheritedEntity() throws IOException {
        collection.save(new Fox("fantastic", "roux"));

        Animal animal = collection.findOne("{name:'fantastic'}").as(Animal.class);

        assertThat(animal).isInstanceOf(Fox.class);
        assertThat(animal.getId()).isNotNull();
    }

    @Test
    public void shouldUpdateIdFieldOnSuperType() throws IOException {

        Fox fox = new Fox("fantastic", "roux");

        collection.save(fox);

        Animal result = collection.findOne().as(Fox.class);
        assertThat(fox.getId()).isNotNull();
        assertThat(result.getId()).isEqualTo(fox.getId());
    }


}

@@ -699,16 +699,124 @@
 jongo;%0A%0A
+import com.fasterxml.jackson.annotation.JsonSubTypes;%0Aimport com.fasterxml.jackson.annotation.JsonTypeInfo;%0A
 import o
@@ -838,16 +838,16 @@
 Animal;%0A
-
 import o
@@ -1724,14 +1724,11 @@
 oid 
-should
+can
 Upda
@@ -1953,24 +1953,24 @@
 sNotNull();%0A
-
         asse
@@ -2023,11 +2023,894 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void canFindInheritedEntityByDiscriminator() throws IOException %7B%0A%0A        collection.insert(%22%7Bname:'piou piout', discriminator:'L'%7D%22);%0A        collection.insert(%22%7Bname:'hunter', discriminator:'B'%7D%22);%0A%0A        Dog dog = collection.findOne(%22%7Bname:'hunter'%7D%22).as(Dog.class);%0A%0A        assertThat(dog).isInstanceOf(Beagle.class);%0A        assertThat(dog.name).isEqualTo(%22hunter%22);%0A    %7D%0A%0A    @JsonTypeInfo(%0A            use = JsonTypeInfo.Id.NAME,%0A            include = JsonTypeInfo.As.PROPERTY,%0A            property = %22discriminator%22)%0A    @JsonSubTypes(%7B%0A            @JsonSubTypes.Type(value = Beagle.class, name = %22B%22),%0A            @JsonSubTypes.Type(value = Loulou.class, name = %22L%22)%0A    %7D)%0A    private static abstract class Dog %7B%0A        String name;%0A    %7D%0A%0A    private static class Beagle extends Dog %7B%0A    %7D%0A%0A    private static class Loulou extends Dog %7B%0A    %7D%0A%0A
 %0A%7D%0A

Add polymorphism with discriminator test