/*
 * Created on 02/02/2007
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package agilpizza.servlet;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

import agilpizza.dao.ClienteDAO;
import agilpizza.modelo.Cliente;

public class IdentificarClienteServletTest {

	private IdentificarCliente servlet;
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private HttpSession session;
	private Object mocks[];
	
	@Before
	public void setUp() {
		servlet = new IdentificarCliente();
		req = createNiceMock(HttpServletRequest.class);
		resp = createStrictMock(HttpServletResponse.class);
		session = createNiceMock(HttpSession.class);
		
		servlet.setClienteDao(new ClienteDAO()); //Simulando Init
		
		mocks = new Object[] {req, resp, session};
	}
	
	@Test
	public void testeEmailVazio() throws Exception {
		expect(req.getParameter("email")).andReturn("");
		expectError();
	}
	
	@Test
	public void testeEmailInvalido() throws Exception {
		expect(req.getParameter("email")).andReturn("lalala");
		expectError();
	}
	
	
	@Test
	public void testeValidaEmailERedirecionaParaTelaDeSucesso() throws Exception {
		expect(req.getParameter("email")).andReturn("email@email.com");
		
		
		expect(req.getSession()).andReturn(session).anyTimes();
		session.setAttribute(eq("clienteIdentificado"), isA(Cliente.class));
		
		resp.sendRedirect(eq("cliente.jsp"));
		
		replay(mocks);
		servlet.doPost(req, resp);
		verify(mocks);
	}

	private void expectError() throws IOException, ServletException {
		resp.sendRedirect(eq("identificacaoCliente.jsp"));
		
		expect(req.getSession()).andReturn(session).anyTimes();
		
		//session.setAttribute(eq("msgErro"), eq("Campos obrigatorios (rua e número) não foram preenchidos"));
		
		replay(mocks);
		servlet.doPost(req, resp);
		verify(mocks);
	}
	
}
