package com.metamx.common.parsers;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import junit.framework.Assert;
import org.junit.Test;

import java.util.Map;

public class JSONParserTest
{
  private static final String json = "{\"one\": \"foo\", \"two\" : [\"bar\", \"baz\"], \"three\" : \"qux\", \"four\" : null}";
  private static final String numbersJson = "{\"five\" : 5.0, \"six\" : 6, \"many\" : 1234567878900, \"toomany\" : 1234567890000000000000}";
  private static final String whackyCharacterJson = "{\"one\": \"foo\\uD900\"}";

  @Test
  public void testSimple()
  {
    final Parser<String, Object> jsonParser = new JSONParser();
    final Map<String, Object> jsonMap = jsonParser.parse(json);
    Assert.assertEquals(
        "jsonMap",
        ImmutableMap.of("one", "foo", "two", ImmutableList.of("bar", "baz"), "three", "qux"),
        jsonMap
    );
  }

  @Test
  public void testWithWhackyCharacters()
  {
    final Parser<String, Object> jsonParser = new JSONParser();
    final Map<String, Object> jsonMap = jsonParser.parse(whackyCharacterJson);
    Assert.assertEquals(
        "jsonMap",
        ImmutableMap.of("one", "foo?"),
        jsonMap
    );
  }

  @Test
  public void testWithFields()
  {
    final Parser<String, Object> jsonParser = new JSONParser();
    jsonParser.setFieldNames(ImmutableList.of("two", "three", "five"));
    final Map<String, Object> jsonMap = jsonParser.parse(json);
    Assert.assertEquals(
        "jsonMap",
        ImmutableMap.of("two", ImmutableList.of("bar", "baz"), "three", "qux"),
        jsonMap
    );
  }

  @Test
  public void testWithNumbers()
  {
    final Parser<String, Object> jsonParser = new JSONParser();
    final Map<String, Object> jsonMap = jsonParser.parse(numbersJson);
    Assert.assertEquals(
        "jsonMap",
        ImmutableMap.of("five", 5.0, "six", 6L, "many", 1234567878900L, "toomany", 1.23456789E21),
        jsonMap
    );
  }
}

@@ -29,16 +29,68 @@
 rsers;%0A%0A
+import com.fasterxml.jackson.databind.ObjectMapper;%0A
 import c
@@ -176,16 +176,56 @@
 bleMap;%0A
+import com.google.common.collect.Lists;%0A
 import j
@@ -999,32 +999,390 @@
 Map%0A    );%0A  %7D%0A%0A
+  @Test%0A  public void testSimpleWithFields()%0A  %7B%0A    final Parser%3CString, Object%3E jsonParser = new JSONParser(new ObjectMapper(), Lists.newArrayList(%22two%22));%0A    final Map%3CString, Object%3E jsonMap = jsonParser.parse(json);%0A    Assert.assertEquals(%0A        %22jsonMap%22,%0A        ImmutableMap.of(%22two%22, ImmutableList.of(%22bar%22, %22baz%22)),%0A        jsonMap%0A    );%0A  %7D%0A%0A
   @Test%0A  public

add test with fields