package com.rollbar.utilities;

import org.junit.Test;

import org.junit.Assert;

import java.util.HashMap;
import java.util.Map;

public class RollbarSerializerTest {
    @Test
    public void testSerializeObject() throws Exception {
        RollbarSerializer s = new RollbarSerializer();
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("key1", 42);
        obj.put("a", "a \" b");
        obj.put("other", "some \\\" string");
        FakeData d = new FakeData(obj);
        String result = s.serialize(d);
        Assert.assertEquals(
            "should serialize object properly",
            "{\"key1\":42,\"a\":\"a \\\" b\",\"other\":\"some \\\\\\\" string\"}",
            result);
    }

    @Test
    public void testSerializeArray() throws Exception {
        RollbarSerializer s = new RollbarSerializer();
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("k", "v");
        obj.put("a", "b");
        Map<String, Object> innerObj = new HashMap<String, Object>();
        innerObj.put("x", "y");
        obj.put("inner", innerObj);
        Object[] a = {42,obj,"hello"};
        FakeData d = new FakeData(a);
        String result = s.serialize(d);
        Assert.assertEquals(
            "should serialize array properly",
            "[42,{\"a\":\"b\",\"k\":\"v\",\"inner\":{\"x\":\"y\"}},\"hello\"]",
            result);
    }

    @Test
    public void testSerializePretty() throws Exception {
        RollbarSerializer s = new RollbarSerializer(true);
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("key1", 42);
        obj.put("a", "a \" b");
        obj.put("other", "some \\\" string");
        FakeData d = new FakeData(obj);
        String result = s.serialize(d);
        Assert.assertEquals(
            "should serialize object properly",
            "{\n" +
            "  \"key1\": 42,\n" + 
            "  \"a\": \"a \\\" b\",\n" +
            "  \"other\": \"some \\\\\\\" string\"\n" +
            "}",
            result);
    }

    @Test
    public void testSerializeNull() throws Exception {
        RollbarSerializer s = new RollbarSerializer();
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("key1", null);
        FakeData d = new FakeData(obj);
        String result = s.serialize(d);
        Assert.assertEquals(
            "should serialize object properly",
            "{\"key1\":null}",
            result);
    }
}

class FakeData implements JsonSerializable {
  private Object object;

  public FakeData(Object object) {
    this.object = object;
  }

  public Object asJson() {
    return this.object;
  }
}

@@ -352,37 +352,8 @@
 ();%0A
-        obj.put(%22key1%22, 42);%0A
     
@@ -372,32 +372,32 @@
 %22a%22, %22a %5C%22 b%22);%0A
+
         obj.put(
@@ -518,103 +518,29 @@
     
-Assert.assertEquals(%0A            %22should serialize object properly%22,%0A            %22%7B%5C%22key1%5C%22:42,
+String expected1 = %22%7B
 %5C%22a%5C
@@ -590,17 +590,17 @@
 ring%5C%22%7D%22
-,
+;
 %0A       
@@ -596,34 +596,180 @@
 %7D%22;%0A        
-    result
+String expected2 = %22%7B%5C%22other%5C%22:%5C%22some %5C%5C%5C%5C%5C%5C%5C%22 string%5C%22,%5C%22a%5C%22:%5C%22a %5C%5C%5C%22 b%5C%22%7D%22;%0A        Assert.assertTrue(expected1.equals(result) %7C%7C expected2.equals(result)
 );%0A    %7D%0A%0A  
@@ -983,35 +983,8 @@
 %22);%0A
-        obj.put(%22a%22, %22b%22);%0A
     
@@ -1246,87 +1246,26 @@
     
-Assert.assertEquals(%0A            %22should serialize array properly%22,%0A           
+String expected1 =
  %22%5B4
@@ -1273,30 +1273,89 @@
 ,%7B%5C%22
-a
+k
 %5C%22:%5C%22
-b
+v
 %5C%22,%5C%22
-k%5C%22:%5C%22v%5C%22,
+inner%5C%22:%7B%5C%22x%5C%22:%5C%22y%5C%22%7D%7D,%5C%22hello%5C%22%5D%22;%0A        String expected2 = %22%5B42,%7B
 %5C%22in
@@ -1365,32 +1365,44 @@
 %5C%22:%7B%5C%22x%5C%22:%5C%22y%5C%22%7D
+,%5C%22k%5C%22:%5C%22v%5C%22
 %7D,%5C%22hello%5C%22%5D%22,%0A 
@@ -1398,17 +1398,17 @@
 ello%5C%22%5D%22
-,
+;
 %0A       
@@ -1404,35 +1404,95 @@
 %5D%22;%0A        
-    
+Assert.assertTrue(expected1.equals(result) %7C%7C expected2.equals(
 result)
+)
 ;%0A    %7D%0A%0A   
@@ -1712,86 +1712,8 @@
 2);%0A
-        obj.put(%22a%22, %22a %5C%22 b%22);%0A        obj.put(%22other%22, %22some %5C%5C%5C%22 string%22);%0A
     
@@ -1881,24 +1881,24 @@
     %22%7B%5Cn%22 +%0A
+
             
@@ -1916,107 +1916,8 @@
 : 42
-,%5Cn%22 + %0A            %22  %5C%22a%5C%22: %5C%22a %5C%5C%5C%22 b%5C%22,%5Cn%22 +%0A            %22  %5C%22other%5C%22: %5C%22some %5C%5C%5C%5C%5C%5C%5C%22 string%5C%22
 %5Cn%22 

deal with the fact that hash maps are not ordered