package de.jpaw.bonaparte.core.tests;

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

import org.testng.Assert;
import org.testng.annotations.Test;

import de.jpaw.bonaparte.core.BonaPortable;
import de.jpaw.bonaparte.core.MapParser;
import de.jpaw.bonaparte.core.StaticMeta;
import de.jpaw.bonaparte.util.ToStringHelper;
import de.jpaw.json.JsonParser;

public class MapToBonaPortableTest {
    
    /** Demonstrates how a JSON String can be converted back to a BonaPortable. This is effectively a 2 step process. */
    @Test
    public void testJsonToBonaPortable() throws Exception {
        String json = "{ \"$PQON\": \"meta.ParsedFoldingComponent\", \"fieldname\":\"foo¸\"   ,  \"index\": 42 }";
        Map<String,Object> testMap = new JsonParser(json, false).parseObject();
        BonaPortable test = MapParser.asBonaPortable(testMap, StaticMeta.OUTER_BONAPORTABLE_FOR_JSON);
        Assert.assertNotNull(test);
        Assert.assertEquals(test.getClass().getSimpleName(), "ParsedFoldingComponent");
    }

    private void testNumber(Map<String,Object> testMap, String expected) {
        Assert.assertEquals(testMap.get("i").getClass().getSimpleName(), "Integer");
        Assert.assertEquals(testMap.get("l").getClass().getSimpleName(), "Long");
        Assert.assertEquals(testMap.get("d").getClass().getSimpleName(), expected);
        
    }
    
    /** Demonstrates how the parser converts numeric types, with choice on fractional numbers. */
    @Test
    public void testJsonToNumber() throws Exception {
        String json = "{ \"i\": -565656,   \"l\": 62626262628818818, \"d\":3.14 }";
        
        testNumber(new JsonParser(json, false).parseObject(), "BigDecimal");
        testNumber(new JsonParser(json, true).parseObject(), "Double");
    }

    /** Demonstrates how a Map (parsed JSON) can be converted back to a BonaPortable. */
    @Test
    public void testMapToBonaPortable() throws Exception {
        Map<String,Object> testMap = new HashMap<String,Object>(10);
        
        testMap.put("$PQON",     "meta.ParsedFoldingComponent");
        testMap.put("fieldname", "\"foo¸\"");
        testMap.put("index",     "42");
        
        BonaPortable test = MapParser.asBonaPortable(testMap, StaticMeta.OUTER_BONAPORTABLE_FOR_JSON);
        Assert.assertNotNull(test);
        Assert.assertEquals(test.getClass().getSimpleName(), "ParsedFoldingComponent");
    }
    
    /** Demonstrates how a nested Map (parsed JSON) can be converted back to a BonaPortable. */
    @Test
    public void testMapsToBonaPortable() throws Exception {
        Map<String,Object> testMap = new HashMap<String,Object>(10);
        Map<String,Object> testMap2 = new HashMap<String,Object>(10);
        
        testMap2.put("$PQON",     "meta.ParsedFoldingComponent");
        testMap2.put("fieldname", "\"bar¸\"");
        testMap2.put("index",     "999");
        
        testMap.put("$PQON",     "meta.ParsedFoldingComponent");
        testMap.put("fieldname", "\"foo¸\"");
        testMap.put("index",     "42");
        testMap.put("alphaIndex", "\"EUR\"");
        testMap.put("component", testMap2);
        
        System.out.println(ToStringHelper.toStringML(testMap));
        
        BonaPortable test = MapParser.asBonaPortable(testMap, StaticMeta.OUTER_BONAPORTABLE_FOR_JSON);
        Assert.assertNotNull(test);
        Assert.assertEquals(test.getClass().getSimpleName(), "ParsedFoldingComponent");
        System.out.println(ToStringHelper.toStringML(test));
    }
}

@@ -671,17 +671,16 @@
 %5C%22:%5C%22foo
-%C2%B8
 %5C%22   ,  
@@ -1000,24 +1000,158 @@
 omponent%22);%0A
+        Assert.assertEquals(testMap.get(%22fieldname%22), %22foo%22);%0A        Assert.assertEquals(testMap.get(%22index%22), Integer.valueOf(42));%0A
     %7D%0A%0A    p
@@ -2232,33 +2232,28 @@
 fieldname%22, 
-%22%5C
 %22foo
-%C2%B8%5C%22
 %22);%0A        
@@ -2275,32 +2275,104 @@
 dex%22,     %2242%22);
+     // show that converter also maps Strings to Integer where required!
 %0A        %0A      
@@ -3021,17 +3021,12 @@
 e%22, 
-%22%5C
 %22bar
-%C2%B8%5C%22
 %22);%0A
@@ -3063,13 +3063,11 @@
     
-%22
 999
-%22
 );%0A 
@@ -3176,17 +3176,12 @@
 e%22, 
-%22%5C
 %22foo
-%C2%B8%5C%22
 %22);%0A
@@ -3213,20 +3213,18 @@
 x%22,     
-%22
 42
-%22
 );%0A     
@@ -3256,16 +3256,12 @@
 x%22, 
-%22%5C
 %22EUR
-%5C%22
 %22);%0A

fix testcase