package br.com.caelum.brutal.validators;

import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;

import br.com.caelum.brutal.dao.UserDAO;
import br.com.caelum.brutal.model.User;
import br.com.caelum.vraptor.Validator;
import br.com.caelum.vraptor.util.test.JSR303MockValidator;

public class UserValidatorTest {

    private UserDAO users;
    private Validator validator;
    private UserValidator userValidator;
    
    @Before
    public void setup() {
        users = mock(UserDAO.class);
        validator = new JSR303MockValidator();
        userValidator = new UserValidator(validator, users);
    }

    @Test
    public void should_verify_email() {
        when(users.existsWithEmail("used@gmail.com")).thenReturn(true);
        User user = new User("nome", "used@gmail.com", "123");
        boolean valid = userValidator.validate(user, "123", "123");
        
        assertFalse(valid);
    }
    
    @Test
    public void should_verify_passwords() throws Exception {
        when(users.existsWithEmail("valid@gmail.com")).thenReturn(false);
        User user = new User("nome", "valid@gmail.com", "123");
        boolean valid = userValidator.validate(user, "123", "1234");
        
        assertFalse(valid);
    }
    
    @Test
    public void should_verify_null() throws Exception {
        boolean valid = userValidator.validate(null, "123", "1234");
        
        assertFalse(valid);
    }

}

@@ -79,16 +79,59 @@
 tFalse;%0A
+import static org.junit.Assert.assertTrue;%0A
 import s
@@ -1568,12 +1568,328 @@
 );%0A    %7D
+%0A    %0A    @Test%0A    public void should_valid_user() throws Exception %7B%0A        when(users.existsWithEmail(%22used@gmail.com%22)).thenReturn(false);%0A        User user = new User(%22nome%22, %22used@gmail.com%22, %22123%22);%0A        boolean valid = userValidator.validate(user, %22123%22, %22123%22);%0A        %0A        assertTrue(valid);%0A    %7D
 %0A%0A%7D%0A

teste pra usuario valido