package me.chyxion.tigon.webmvc.test;

import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import javax.validation.Validator;
import javax.validation.ConstraintViolation;
import me.chyxion.tigon.webmvc.test.controller.SiteController.Form;
import org.hibernate.validator.constraints.NotBlank;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.validation.annotation.Validated;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Jan 27, 2015 3:27:08 PM
 */
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
    "classpath*:spring/spring-*.xml",
    "classpath*:spring/servlet-spring-*.xml"
})
@Validated
public class SiteControllerTest {

    @Autowired
    private ControllerTestTool t;
    @Autowired
    private Validator v;

    @Test
    public void testIndex() {
        t.print(t.get("/"));
    }

    @Test
    public void testV() {
        t.print(t.get("/v?v="));
    }

    @Test
    public void testValidation() {
        testValidation("");
    }

    public void testValidation(@NotBlank String foo) {

    }

    @Test
    public void testRaw() {
        t.print(t.get("/raw"));
    }

    @Test
    public void testInt() {
        t.print(t.get("/int"));
    }

    @Test
    public void testPost() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", "");
        params.put("name", "Shaun Chyxion");
        params.put("gender", "");
        t.print(t.post("/post", params));
    }

    @Test
    public void testValidate() {
        Set<ConstraintViolation<Form>> result = v.validate(new Form());
        for (ConstraintViolation<Form> vv : result) {
            System.err.println("[" + 
                vv.getPropertyPath() + "] " + vv.getMessage());
        }
    }
    
    @Test
    public void testValidate1() {
        Form form = new Form();
        form.setId("chyxion");
        form.setName("chyxion");
        form.setGender("");

        Set<ConstraintViolation<Form>> result = v.validate(form);
        for (ConstraintViolation<Form> vv : result) {
            System.err.println(vv.getMessage());
            System.err.println(vv.getPropertyPath().toString());
        }
    }
}
