package test.com.fp.common;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import test.com.fp.json.TestingBean;

import com.fp.common.messages.ELEval;
import com.fp.dto.save.SaveBean;
import com.fp.dto.save.SaveRequest;

public class ELTest {
    private Map<String, Object> maia = new HashMap<String, Object>();

    public ELTest() throws Exception {
        SaveRequest sr = new SaveRequest();
        sr.setUser("GF");
        sr.setTransactionCode(150);
        sr.put("OfficeName", "Prueba");
        sr.put("val", new BigDecimal("129983.89"));
        sr.put("date", new Date(1323961406815L));
        SaveBean sb = new SaveBean("test.com.fp.json.TestingBean");
        TestingBean tb = new TestingBean();
        tb.setEntero(1200);
        sb.addModifiedObject(tb);
        sr.putSaveBean("aliasSaveBean", sb);
        this.maia.put("request", sr);
    }

    @Test
    public void name() throws Exception {
        Assert.assertEquals("User", "GF", ELEval.eval(this.maia, "request.user"));
        Assert.assertEquals("User", String.class, ELEval.eval(this.maia, "request.user").getClass());
    }

    @Test
    public void control() throws Exception {
        Assert.assertEquals("Control", "Prueba", ELEval.eval(this.maia, "request.OfficeName"));
        Assert.assertEquals("Control", String.class, ELEval.eval(this.maia, "request.OfficeName").getClass());
    }

    @Test
    public void bigDecimal() throws Exception {
        Assert.assertEquals("BigDecimal", new BigDecimal("129983.89"), ELEval.eval(this.maia, "request.val"));
        Assert.assertEquals("BigDecimal", BigDecimal.class, ELEval.eval(this.maia, "request.val").getClass());
    }

    @Test
    public void date() throws Exception {
        Assert.assertEquals("Date", new Date(1323961406815L), ELEval.eval(this.maia, "request.date"));
        Assert.assertEquals("Date", Date.class, ELEval.eval(this.maia, "request.date").getClass());
    }

    @Test
    public void bean() throws Exception {
        Assert.assertEquals("Bean", SaveBean.class, ELEval.eval(this.maia, "request.aliasSaveBean").getClass());
        Assert.assertEquals("Bean", TestingBean.class, ELEval.eval(this.maia, "request.aliasSaveBean.modifiedRecords[0]").getClass());
        Assert.assertEquals("Bean", 1200, ELEval.eval(this.maia, "request.aliasSaveBean.modifiedRecords[0].entero"));
    }
}
