/*
 * Copyright © 2017 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package co.cask.wrangler.steps.writer;

import co.cask.wrangler.api.Record;
import co.cask.wrangler.steps.PipelineTest;
import com.google.gson.Gson;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * Tests {@link WriteAsJsonMap}
 */
public class WriteAsJsonMapTest {

  private static final Gson GSON = new Gson();

  @Test
  public void testWriteToJson() throws Exception {
    String[] directives = new String[] {
      "write-as-json-map test",
      "keep test"
    };

    JSONObject o = new JSONObject();
    o.put("a", 1);
    o.put("b", "2");
    String url = "http://www.yahoo.com?a=b c&b=ab&xyz=1";
    List<Record> records = Arrays.asList(
      new Record().add("int", 1).add("string", "this is string"),
      new Record("url", url)
      .add("o", o)
      .add("i1", 1)
      .add("i2", (double) 1.8f)
    );
    records = PipelineTest.execute(directives, records);

    Assert.assertTrue(records.size() == 2);
    Assert.assertEquals(new Record("test", "{\"string\":\"this is string\",\"int\":1}"),
                        records.get(0));
    Assert.assertEquals(new Record("test", "{\"i1\":1,\"i2\":1.7999999523162842,\"url\":"
      + GSON.toJson(url) + ",\"o\":{\"map\":{\"a\":1,\"b\":\"2\"}}}"),
                        records.get(1));
  }
}

@@ -631,16 +631,70 @@
 riter;%0A%0A
+import co.cask.cdap.internal.guava.reflect.TypeToken;%0A
 import c
@@ -787,17 +787,134 @@
 gle.
-gson.Gson
+common.collect.ImmutableMap;%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.JsonObject;%0Aimport com.google.gson.JsonParser
 ;%0Aim
@@ -988,16 +988,47 @@
 .Test;%0A%0A
+import java.lang.reflect.Type;%0A
 import j
@@ -1066,16 +1066,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -1831,61 +1831,245 @@
     
-Assert.assertEquals(new Record
+Type stringStringMapType = new TypeToken%3CMap%3CString, String%3E%3E() %7B %7D.getType();%0A    Map%3CString, String%3E map = GSON.fromJson((String) records.get(0).getValue
 (%22test%22
+)
 , 
-%22%7B%5C
+stringStringMapType);%0A    Assert.assertEquals(ImmutableMap.of(
 %22string
-%5C%22:%5C
+%22, 
 %22thi
@@ -2083,264 +2083,414 @@
 ring
-%5C
 %22,
-%5C
+ 
 %22int
-%5C%22:1%7D%22),%0A                        records.get(0));%0A    Assert.assertEquals(new Record(%22test%22, %22%7B%5C%22i1%5C%22:1,%5C%22i2%5C%22:1.7999999523162842,%5C%22url%5C%22:%22%0A      + GSON.toJson(url) + %22,%5C%22o%5C%22:%7B%5C%22map%5C%22:%7B%5C%22a%5C%22:1,%5C%22b%5C%22:%5C%222%5C%22%7D%7D%7D%22),%0A                        records.get(1
+%22, %221%22), map);%0A%0A    JsonObject jsonObject = new JsonParser().parse((String) records.get(1).getValue(%22test%22)).getAsJsonObject();%0A    Assert.assertEquals(1, jsonObject.get(%22i1%22).getAsInt());%0A%0A    Assert.assertEquals(1.8f, jsonObject.get(%22i2%22).getAsFloat(), 0.001);%0A    Assert.assertEquals(url, jsonObject.get(%22url%22).getAsString());%0A    Assert.assertEquals(GSON.toJson(o), jsonObject.get(%22o%22).toString(
 ));%0A

Improve WriteAsJsonMapTest.