package com.ctrip.ops.sysdev.test;

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

import com.ctrip.ops.sysdev.filters.Add;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.Yaml;

public class TestAddFilter {
    @Test
    public void testAddFilter() {
        String c = String.format("%s\n%s\n%s\n%s\n%s\n%s",
                "        fields:",
                "            nick: '${name}-badboy'",
                "            gender: 'male'",
                "            '[metric][value1]': '10'",
                "            '[metric][value2]': ${name}",
                "            '[metric][value3]': '[extra][value]'"
        );
        Yaml yaml = new Yaml();
        Map config = (Map) yaml.load(c);
        Assert.assertNotNull(config);

        Add addFilter = new Add(config);

        // Match
        Map event = new HashMap();
        event.put("name", "liujia");
        event.put("nothing", "11.11");
        event.put("extra", new HashMap() {{
            this.put("value", 10);
        }});

        event = addFilter.process(event);
        Assert.assertEquals(event.get("name"), "liujia");
        Assert.assertEquals(event.get("nick"), "liujia-badboy");
        Assert.assertEquals(event.get("gender"), "male");
        Assert.assertEquals(((Map) event.get("metric")).get("value1"), "10");
        Assert.assertEquals(((Map) event.get("metric")).get("value2"), "liujia");
        Assert.assertEquals(((Map) event.get("metric")).get("value3"), 10);
        Assert.assertNull(event.get("tags"));

    }
}

@@ -343,16 +343,20 @@
 %5Cn%25s%5Cn%25s
+%5Cn%25s
 %22,%0A     
@@ -666,16 +666,85 @@
 value%5D'%22
+,%0A                %22            '%5Bmetric%5D%5Bvalue4%5D': '%5Bextra%5D%5Bvalue2%5D'%22
 %0A       
@@ -1607,16 +1607,23 @@
 ertNull(
+((Map) 
 event.ge
@@ -1629,18 +1629,34 @@
 et(%22
-tags
+metric%22)).get(%22value4
 %22));%0A
-%0A
     

add new testcase