/*
 * Copyright 2017 MovingBlocks
 *
 * 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.terasology.persistence.typeHandling.gson;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
import java.util.Objects;

public class PolymorphicTypeAdapterFactoryTest {
    private static final Dog DOG = new Dog(1.25f);
    private static final Animal CAT = new Animal("Cat");
    private static final Cheetah CHEETAH = new Cheetah(21);

    private final Gson gson = new GsonBuilder()
            .registerTypeAdapterFactory(PolymorphicTypeAdapterFactory.of(Animal.class))
            .create();

    @Test
    public void testBaseClass() {
        Animal animal = CHEETAH;

        String json = gson.toJson(animal);

        Animal newAnimal = gson.fromJson(json, Animal.class);

        Assert.assertTrue(newAnimal instanceof Cheetah);
    }

    @Test
    public void testInnerField() {
        Capsule capsule = new Capsule(DOG);

        String json = gson.toJson(capsule);

        Capsule newCapsule = gson.fromJson(json, Capsule.class);
        Assert.assertTrue(newCapsule.animal instanceof Dog);
    }

    @Test
    public void testPolymorphicList() {
        List<Animal> animals = Lists.newArrayList(CAT, DOG, CHEETAH);

        String json = gson.toJson(animals);

        List<Animal> newAnimals = gson.fromJson(json, new TypeToken<List<Animal>>(){}.getType());

        Assert.assertTrue(newAnimals.get(1) instanceof Dog);
        Assert.assertTrue(newAnimals.get(2) instanceof Cheetah);
    }

    private static class Capsule {
        private final Animal animal;

        private Capsule(Animal animal) {
            this.animal = animal;
        }
    }

    private static class Animal {
        private final String name;

        private Animal(String name) {
            this.name = name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Animal animal = (Animal) o;
            return Objects.equals(name, animal.name);
        }
    }

    private static class Dog extends Animal {
        private final float tailLength;

        private Dog(float tailLength) {
            super("Dog");
            this.tailLength = tailLength;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            if (!super.equals(o)) return false;
            Dog dog = (Dog) o;
            return Float.compare(dog.tailLength, tailLength) == 0;
        }
    }

    private static class Cheetah extends Animal {
        private final int spotCount;

        private Cheetah(int spotCount) {
            super("Cheetah");
            this.spotCount = spotCount;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            if (!super.equals(o)) return false;
            Cheetah cheetah = (Cheetah) o;
            return spotCount == cheetah.spotCount;
        }
    }
}

@@ -1132,17 +1132,26 @@
 al Gson 
-g
+baseClassG
 son = ne
@@ -1274,24 +1274,464 @@
 .create();%0A%0A
+    private final Gson interfaceGson = new GsonBuilder()%0A            .registerTypeAdapterFactory(PolymorphicTypeAdapterFactory.of(Walker.class))%0A            .create();%0A%0A    @Test%0A    public void testInterfaceReference() %7B%0A        Walker walker = CAT;%0A%0A        String json = interfaceGson.toJson(walker);%0A%0A        Walker newAnimal = interfaceGson.fromJson(json, Walker.class);%0A%0A        Assert.assertTrue(newAnimal instanceof Animal);%0A    %7D%0A%0A
     @Test%0A  
@@ -1757,16 +1757,25 @@
 aseClass
+Reference
 () %7B%0A   
@@ -1819,33 +1819,42 @@
   String json = 
-g
+baseClassG
 son.toJson(anima
@@ -1885,17 +1885,26 @@
 nimal = 
-g
+baseClassG
 son.from
@@ -2098,33 +2098,42 @@
   String json = 
-g
+baseClassG
 son.toJson(capsu
@@ -2167,17 +2167,26 @@
 psule = 
-g
+baseClassG
 son.from
@@ -2310,27 +2310,25 @@
 oid test
-Polymorphic
+BaseClass
 List() %7B
@@ -2421,17 +2421,26 @@
  json = 
-g
+baseClassG
 son.toJs
@@ -2487,17 +2487,26 @@
 imals = 
-g
+baseClassG
 son.from
@@ -2686,32 +2686,510 @@
 heetah);%0A    %7D%0A%0A
+    @Test%0A    public void testInterfaceList() %7B%0A        List%3CWalker%3E walkers = Lists.newArrayList(CAT, DOG, CHEETAH);%0A%0A        String json = interfaceGson.toJson(walkers);%0A%0A        List%3CWalker%3E newWalkers = interfaceGson.fromJson(json, new TypeToken%3CList%3CWalker%3E%3E()%7B%7D.getType());%0A%0A        Assert.assertTrue(newWalkers.get(0) instanceof Animal);%0A        Assert.assertTrue(newWalkers.get(1) instanceof Dog);%0A        Assert.assertTrue(newWalkers.get(2) instanceof Cheetah);%0A    %7D%0A%0A
     private stat
@@ -3329,32 +3329,70 @@
        %7D%0A    %7D%0A%0A
+    private interface Walker %7B%0A    %7D%0A%0A
     private stat
@@ -3399,32 +3399,50 @@
 ic class Animal 
+implements Walker 
 %7B%0A        privat

Add PolymorphicTypeAdapterFactory tests testing interface references