package no.deichman.services.patch;

import com.google.gson.JsonParseException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class PatchObjectTypeAdapterTest {
    @Test
    public void test_adapter_exists(){
        assertNotNull(new PatchObjectTypeAdapter());
    }

    @Test
    public void test_it_handles_single_object(){
        List<PatchObject> list = new ArrayList<PatchObject>();
        String input = "{\"op\":\"add\",\"s\":\"http://example.com/a\",\"p\":\"http://example.com/title\",\"o\":{\"value\":\"Housea\"}}";
        Type polt = new TypeToken<List<PatchObject>>() {}.getType();
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(polt, new PatchObjectTypeAdapter())
                .create();
        list.addAll(gson.fromJson(input,polt));
        assertEquals(1,list.size());
    }

    @Test
    public void test_it_handles_multiple_objects(){
        List<PatchObject> list = new ArrayList<PatchObject>();
        String input = "[{\"op\":\"add\",\"s\":\"http://example.com/a\",\"p\":\"http://example.com/title\",\"o\":{\"value\":\"Housea\"}},"
                + "{\"op\":\"add\",\"s\":\"http://example.com/a\",\"p\":\"http://example.com/title\",\"o\":{\"value\":\"Houseb\"}}]";
        Type polt = new TypeToken<List<PatchObject>>() {}.getType();
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(polt, new PatchObjectTypeAdapter())
                .create();
        list.addAll(gson.fromJson(input,polt));
        assertEquals(2,list.size());
    }

    @Test(expected = JsonParseException.class)
    public void it_throws_when_unable_to_parse() {
        List<PatchObject> list = new ArrayList<PatchObject>();
        String input = "notverygoodisit";
        Type polt = new TypeToken<List<PatchObject>>() {}.getType();
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(polt, new PatchObjectTypeAdapter())
                .create();
        gson.fromJson(input,polt);
    }
}

@@ -633,35 +633,24 @@
 w ArrayList%3C
-PatchObject
 %3E();%0A       
@@ -786,35 +786,40 @@
 ;%0A        Type p
-olt
+atchType
  = new TypeToken
@@ -891,33 +891,16 @@
 uilder()
-%0A                
 .registe
@@ -905,35 +905,40 @@
 terTypeAdapter(p
-olt
+atchType
 , new PatchObjec
@@ -952,33 +952,16 @@
 apter())
-%0A                
 .create(
@@ -995,36 +995,42 @@
 .fromJson(input,
-polt
+ patchType
 ));%0A        asse
@@ -1040,16 +1040,17 @@
 quals(1,
+ 
 list.siz
@@ -1167,35 +1167,24 @@
 w ArrayList%3C
-PatchObject
 %3E();%0A       
@@ -1455,35 +1455,40 @@
 ;%0A        Type p
-olt
+atchType
  = new TypeToken
@@ -1560,33 +1560,16 @@
 uilder()
-%0A                
 .registe
@@ -1574,35 +1574,40 @@
 terTypeAdapter(p
-olt
+atchType
 , new PatchObjec
@@ -1621,33 +1621,16 @@
 apter())
-%0A                
 .create(
@@ -1672,20 +1672,26 @@
 n(input,
-polt
+ patchType
 ));%0A    
@@ -1709,16 +1709,17 @@
 quals(2,
+ 
 list.siz
@@ -1833,71 +1833,8 @@
 ) %7B%0A
-        List%3CPatchObject%3E list = new ArrayList%3CPatchObject%3E();%0A
     
@@ -1885,19 +1885,24 @@
   Type p
-olt
+atchType
  = new T
@@ -1982,33 +1982,16 @@
 uilder()
-%0A                
 .registe
@@ -2004,19 +2004,24 @@
 dapter(p
-olt
+atchType
 , new Pa
@@ -2043,33 +2043,16 @@
 apter())
-%0A                
 .create(
@@ -2086,12 +2086,18 @@
 put,
-polt
+ patchType
 );%0A 

Improve formatting and naming.