/*
 * 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.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.Assert;
import org.junit.Test;
import org.terasology.math.geom.Rect2i;
import org.terasology.math.geom.Vector4f;
import org.terasology.naming.Name;
import org.terasology.persistence.typeHandling.TypeSerializationLibrary;
import org.terasology.reflection.copy.CopyStrategyLibrary;
import org.terasology.reflection.reflect.ReflectionReflectFactory;
import org.terasology.rendering.nui.Color;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class GsonTypeSerializationLibraryAdapterFactoryTest {
    private static final TestClass OBJECT = new TestClass(
            new Color(0xDEADBEEF),
            ImmutableSet.of(Vector4f.zero(), Vector4f.one()),
            ImmutableMap.of(
                    new Name("someRect"),
                    Rect2i.createFromMinAndSize(-3, -3, 10, 10)
            ),
            -0xDECAF
    );

    private static final String OBJECT_JSON = "{\"color\":[222,173,190,239],\"vector4fs\":[[0.0,0.0,0.0,0.0]," +
            "[1.0,1.0,1.0,1.0]],\"rect2iMap\":{\"someRect\":{\"min\":[-3,-3],\"size\":[10,10]}},\"i\":-912559}";

    private final ReflectionReflectFactory reflectFactory = new ReflectionReflectFactory();
    private final CopyStrategyLibrary copyStrategyLibrary = new CopyStrategyLibrary(reflectFactory);
    private final TypeSerializationLibrary typeSerializationLibrary =
            TypeSerializationLibrary.createDefaultLibrary(reflectFactory, copyStrategyLibrary);

    private final GsonTypeSerializationLibraryAdapterFactory typeAdapterFactory =
            new GsonTypeSerializationLibraryAdapterFactory(typeSerializationLibrary);

    private final Gson gson = new GsonBuilder()
            .registerTypeAdapterFactory(typeAdapterFactory)
            .create();

    @Test
    public void testSerialize() {
        String serializedObject = gson.toJson(OBJECT);

        Assert.assertEquals(OBJECT_JSON, serializedObject);
    }

    @Test
    public void testDeserialize() {
        TestClass deserializedObject = gson.fromJson(OBJECT_JSON, TestClass.class);

        Assert.assertEquals(OBJECT, deserializedObject);
    }

    private static class TestClass {
        private final Color color;
        private final Set<Vector4f> vector4fs;
        private final Map<Name, Rect2i> rect2iMap;
        private final int i;

        private TestClass(Color color, Set<Vector4f> vector4fs, Map<Name, Rect2i> rect2iMap, int i) {
            this.color = color;
            this.vector4fs = vector4fs;
            this.rect2iMap = rect2iMap;
            this.i = i;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            TestClass testClass = (TestClass) o;
            return i == testClass.i &&
                    Objects.equals(color, testClass.color) &&
                    Objects.equals(vector4fs, testClass.vector4fs) &&
                    Objects.equals(rect2iMap, testClass.rect2iMap);
        }
    }
}

@@ -892,16 +892,58 @@
 Rect2i;%0A
+import org.terasology.math.geom.Vector2i;%0A
 import o
@@ -1596,17 +1596,8 @@
     
-new Name(
 %22som
@@ -1602,17 +1602,16 @@
 omeRect%22
-)
 ,%0A      
@@ -3103,28 +3103,30 @@
 e final Map%3C
-Name
+String
 , Rect2i%3E re
@@ -3232,20 +3232,22 @@
 fs, Map%3C
-Name
+String
 , Rect2i

Use string map keys in GsonTypeSerializationLibraryAdapterFactoryTest