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<>();
        String input = "{\"op\":\"add\",\"s\":\"http://example.com/a\",\"p\":\"http://example.com/title\",\"o\":{\"value\":\"Housea\"}}";
        Type patchType = new TypeToken<List<PatchObject>>() {}.getType();
        Gson gson = new GsonBuilder().registerTypeAdapter(patchType, new PatchObjectTypeAdapter()).create();
        list.addAll(gson.fromJson(input, patchType));
        assertEquals(1, list.size());
    }

    @Test
    public void test_it_handles_multiple_objects(){
        List<PatchObject> list = new ArrayList<>();
        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 patchType = new TypeToken<List<PatchObject>>() {}.getType();
        Gson gson = new GsonBuilder().registerTypeAdapter(patchType, new PatchObjectTypeAdapter()).create();
        list.addAll(gson.fromJson(input, patchType));
        assertEquals(2, list.size());
    }

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

@@ -634,32 +634,33 @@
  ArrayList%3C%3E();%0A
+%0A
         String i
@@ -668,31 +668,77 @@
 put = %22%7B
-%5C%22op%5C%22:%5C%22add%5C%22,
+%22%0A                + %22  %5C%22op%5C%22:%5C%22add%5C%22,%22%0A                + %22  
 %5C%22s%5C%22:%5C%22
@@ -752,32 +752,55 @@
 example.com/a%5C%22,
+%22%0A                + %22  
 %5C%22p%5C%22:%5C%22http://e
@@ -810,32 +810,55 @@
 ple.com/title%5C%22,
+%22%0A                + %22  
 %5C%22o%5C%22:%7B%5C%22value%5C%22
@@ -873,12 +873,34 @@
 a%5C%22%7D
+%22%0A                + %22
 %7D%22;%0A
+%0A
     
@@ -1316,32 +1316,120 @@
 put = %22%5B
-%7B%5C%22op%5C%22:%5C%22add%5C%22,
+%22%0A                     + %22  %7B%22%0A                     + %22    %5C%22op%5C%22:%5C%22add%5C%22,%22%0A                     + %22    
 %5C%22s%5C%22:%5C%22
@@ -1443,32 +1443,62 @@
 example.com/a%5C%22,
+%22%0A                     + %22    
 %5C%22p%5C%22:%5C%22http://e
@@ -1520,36 +1520,94 @@
 e%5C%22,
-%5C%22o%5C%22:%7B%5C%22value%5C%22:%5C%22Housea%5C%22%7D
+%22%0A                     + %22    %5C%22o%5C%22:%7B%5C%22value%5C%22:%5C%22Housea%5C%22%7D%22%0A                     + %22  
 %7D,%22%0A
@@ -1626,27 +1626,94 @@
     
+   
+  
 + %22
-%7B%5C%22op%5C%22:%5C%22add%5C%22,
+  %7B%22%0A                     + %22    %5C%22op%5C%22:%5C%22add%5C%22,%22%0A                     + %22    
 %5C%22s%5C
@@ -1739,16 +1739,46 @@
 com/a%5C%22,
+%22%0A                     + %22    
 %5C%22p%5C%22:%5C%22
@@ -1808,37 +1808,121 @@
 e%5C%22,
-%5C%22o%5C%22:%7B%5C%22value%5C%22:%5C%22Houseb%5C%22%7D%7D
+%22%0A                     + %22    %5C%22o%5C%22:%7B%5C%22value%5C%22:%5C%22Houseb%5C%22%7D%22%0A                     + %22  %7D%22%0A                     + %22
 %5D%22;%0A

Format test data for readability