/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Andoni del Olmo
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package org.ado.minesync.json;

import org.ado.minesync.ClassTestCase;
import org.ado.minesync.commons.DateUtils;
import org.ado.minesync.db.SyncTypeEnum;
import org.ado.minesync.db.WorldEntity;
import org.apache.commons.io.FileUtils;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class JsonWorldsAdapterTest extends ClassTestCase<JsonWorldsAdapter> {

    @Before
    public void setUp() throws Exception {
        createUnitUnderTest();
    }

    @Test
    public void testGetWorldListToJson() throws Exception {
        JSONObject worldListToJson = unitUnderTest.getWorldListToJson(Arrays.asList(
                createWorldEntity("worldOne", "06.05.1981 00:40:22", 1000, SyncTypeEnum.AUTO),
                createWorldEntity("worldTwo", "21.12.1973 12:30:01", 2000, SyncTypeEnum.MANUAL)
        ));

        assertEquals("json world array",
                "{\"worlds\":[{\"syncType\":1,\"name\":\"worldOne\"},{\"syncType\":0,\"name\":\"worldTwo\"}]}",
                worldListToJson.toJSONString());
    }

    @Test
    public void testGetWorldList() throws Exception {
        File file = File.createTempFile("test", "json");
        FileUtils.write(file, "{\"worlds\":[{\"syncType\":1,\"name\":\"worldOne\"},{\"syncType\":0,\"name\":\"worldTwo\"}]}");

        List<JsonWorld> worldList = unitUnderTest.getWorldList(file);

        assertEquals("world one name", "worldOne", worldList.get(0).getName());
        assertEquals("world one syncType", SyncTypeEnum.AUTO, worldList.get(0).getSyncType());
        assertEquals("world two name", "worldTwo", worldList.get(1).getName());
        assertEquals("world two syncType", SyncTypeEnum.MANUAL, worldList.get(1).getSyncType());
    }

    private WorldEntity createWorldEntity(String name, String creationDate, int size, SyncTypeEnum syncType) {
        return new WorldEntity(name, DateUtils.parse(creationDate), size, syncType);
    }
}
@@ -1369,32 +1369,66 @@
 s.io.FileUtils;%0A
+import org.json.simple.JSONArray;%0A
 import org.json.
@@ -1492,82 +1492,193 @@
 nit.
-Test;%0A%0Aimport java.io.File;%0Aimport java.util.Arrays;%0Aimport java.util.List
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder;%0A%0Aimport java.io.File;%0Aimport java.util.Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Ai
@@ -1801,16 +1801,95 @@
 ter%3E %7B%0A%0A
+    @Rule%0A    public TemporaryFolder temporaryFolder = new TemporaryFolder();%0A%0A
     @Bef
@@ -2350,130 +2350,238 @@
 sert
-Equals(%22json world array%22,%0A                %22%7B%5C%22worlds%5C%22:%5B%7B%5C%22syncType%5C%22:1,%5C%22name%5C%22:%5C%22worldOne%5C%22%7D,%7B%5C%22syncType%5C%22:0,%5C
+JsonObject(new HashMap%3CString, String%3E() %7B%7B%0A            put(%22name%22, %22worldOne%22);%0A            put(%22syncType%22, %221%22);%0A        %7D%7D, worldListToJson, 0);%0A        assertJsonObject(new HashMap%3CString, String%3E() %7B%7B%0A            put(
 %22name
-%5C%22:%5C
+%22, 
 %22wor
@@ -2589,15 +2589,11 @@
 dTwo
-%5C%22%7D%5D%7D%22,
+%22);
 %0A   
@@ -2605,42 +2605,60 @@
     
-    worldListToJson.toJSONString()
+put(%22syncType%22, %220%22);%0A        %7D%7D, worldListToJson, 1
 );%0A 
@@ -2751,42 +2751,32 @@
 e = 
-File.createTempFile(%22test%22, %22json%22
+temporaryFolder.newFile(
 );%0A 
@@ -3533,10 +3533,405 @@
 );%0A    %7D
+%0A%0A    private void assertJsonObject(Map%3CString, String%3E expected, JSONObject actual, int index) %7B%0A        final JSONArray worlds = ((JSONArray) actual.get(%22worlds%22));%0A        final String%5B%5D keyArray = expected.keySet().toArray(new String%5Bexpected.size()%5D);%0A        String key = keyArray%5Bindex%5D;%0A        assertEquals(expected.get(key), ((JSONObject) worlds.get(index)).get(key).toString());%0A    %7D
 %0A%7D

make unittest jdk independent.