package com.box.boxjavalibv2.dao;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import junit.framework.Assert;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import com.box.boxjavalibv2.testutils.TestUtils;
import com.box.restclientv2.exceptions.BoxRestException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class BoxFileTest {

    // @Test
    public void testParcelRoundTrip() throws BoxRestException, IOException {
        String fileJson = FileUtils.readFileToString(new File("testdata/file.json"));
        BoxFile filev2 = (BoxFile) TestUtils.getFromJSON(fileJson, BoxFile.class);

        TestParcel parcel = new TestParcel();
        filev2.writeToParcel(parcel, 0);
        BoxFile fromParcel = new BoxFile(parcel);

        Assert.assertEquals("file", fromParcel.getType());
        Assert.assertEquals("testfileid", fromParcel.getId());
        Assert.assertEquals("testsha1", fromParcel.getSha1());
        Assert.assertEquals("2", fromParcel.getVersionNumber());
        Assert.assertEquals(2, (int) fromParcel.getCommentCount());

    }

    @Test
    public void foo() throws IOException {
        String fileJson = FileUtils.readFileToString(new File("testdata/file2.json"));
        Gson gson = (new GsonBuilder()).registerTypeHierarchyAdapter(BoxFile.class, new FileDeserializer())
            .registerTypeHierarchyAdapter(BoxFile.class, new BFileSerializer()).registerTypeHierarchyAdapter(BoxFolder.class, new FolderDeserializer())
            .create();
        BoxFile f = gson.fromJson(fileJson, BoxFile.class);
        gson.toJson(f);
    }

    private static class FileDeserializer implements JsonDeserializer<BoxFile> {

        @Override
        public BoxFile deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
            JsonObject j = (JsonObject) element;

            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("type", "file");
            map.put("id", j.get("id").getAsString());
            JsonElement obj = j.get("parent");
            if (obj != null) {
                map.put("parent", context.deserialize(obj, BoxFolder.class));
            }
            BoxFile f = new BoxFile(map);
            return f;
        }
    }

    private static class BFileSerializer implements JsonSerializer<BoxFile> {

        @Override
        public JsonElement serialize(BoxFile f, Type type, JsonSerializationContext context) {
            JsonObject j = new JsonObject();
            j.add("type", new JsonPrimitive(f.getType()));
            return j;
        }

    }

    private static class FolderDeserializer implements JsonDeserializer<BoxFolder> {

        @Override
        public BoxFolder deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
            JsonObject j = (JsonObject) element;

            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("type", "folders");
            map.put("id", j.get("id").getAsString());
            JsonElement obj = j.get("parent");
            if (obj != null) {
                map.put("parent", context.deserialize(obj, BoxFolder.class));
            }
            BoxFolder f = new BoxFolder(map);
            return f;
        }
    }
}

@@ -80,65 +80,8 @@
 ion;
-%0Aimport java.lang.reflect.Type;%0Aimport java.util.HashMap;
 %0A%0Aim
@@ -283,405 +283,8 @@
 ion;
-%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;%0Aimport com.google.gson.JsonDeserializationContext;%0Aimport com.google.gson.JsonDeserializer;%0Aimport com.google.gson.JsonElement;%0Aimport com.google.gson.JsonObject;%0Aimport com.google.gson.JsonParseException;%0Aimport com.google.gson.JsonPrimitive;%0Aimport com.google.gson.JsonSerializationContext;%0Aimport com.google.gson.JsonSerializer;
 %0A%0Apu
@@ -316,11 +316,8 @@
 %0A   
- //
  @Te
@@ -1032,2262 +1032,7 @@
    %7D
-%0A%0A    @Test%0A    public void foo() throws IOException %7B%0A        String fileJson = FileUtils.readFileToString(new File(%22testdata/file2.json%22));%0A        Gson gson = (new GsonBuilder()).registerTypeHierarchyAdapter(BoxFile.class, new FileDeserializer())%0A            .registerTypeHierarchyAdapter(BoxFile.class, new BFileSerializer()).registerTypeHierarchyAdapter(BoxFolder.class, new FolderDeserializer())%0A            .create();%0A        BoxFile f = gson.fromJson(fileJson, BoxFile.class);%0A        gson.toJson(f);%0A    %7D%0A%0A    private static class FileDeserializer implements JsonDeserializer%3CBoxFile%3E %7B%0A%0A        @Override%0A        public BoxFile deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException %7B%0A            JsonObject j = (JsonObject) element;%0A%0A            HashMap%3CString, Object%3E map = new HashMap%3CString, Object%3E();%0A            map.put(%22type%22, %22file%22);%0A            map.put(%22id%22, j.get(%22id%22).getAsString());%0A            JsonElement obj = j.get(%22parent%22);%0A            if (obj != null) %7B%0A                map.put(%22parent%22, context.deserialize(obj, BoxFolder.class));%0A            %7D%0A            BoxFile f = new BoxFile(map);%0A            return f;%0A        %7D%0A    %7D%0A%0A    private static class BFileSerializer implements JsonSerializer%3CBoxFile%3E %7B%0A%0A        @Override%0A        public JsonElement serialize(BoxFile f, Type type, JsonSerializationContext context) %7B%0A            JsonObject j = new JsonObject();%0A            j.add(%22type%22, new JsonPrimitive(f.getType()));%0A            return j;%0A        %7D%0A%0A    %7D%0A%0A    private static class FolderDeserializer implements JsonDeserializer%3CBoxFolder%3E %7B%0A%0A        @Override%0A        public BoxFolder deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException %7B%0A            JsonObject j = (JsonObject) element;%0A%0A            HashMap%3CString, Object%3E map = new HashMap%3CString, Object%3E();%0A            map.put(%22type%22, %22folders%22);%0A            map.put(%22id%22, j.get(%22id%22).getAsString());%0A            JsonElement obj = j.get(%22parent%22);%0A            if (obj != null) %7B%0A                map.put(%22parent%22, context.deserialize(obj, BoxFolder.class));%0A            %7D%0A            BoxFolder f = new BoxFolder(map);%0A            return f;%0A        %7D%0A    %7D
 %0A%7D%0A

remove test testcase