/*
 * 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.ServletContext;
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;
import agilpizza.modelo.Endereco;

public class EnderecoServletTest {

	private EnderecoServlet servlet;
	private ServletContext context;
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private HttpSession session;
	private Object mocks[];

	@Before
	public void setUp() {
		context = createNiceMock(ServletContext.class);
		servlet = new EnderecoServlet(){
			@Override
			public ServletContext getServletContext() {
				return context;
			}
		};
		req = createNiceMock(HttpServletRequest.class);
		resp = createStrictMock(HttpServletResponse.class);
		session = createNiceMock(HttpSession.class);
		mocks = new Object[] {context, req, resp, session};
		expect(req.getSession()).andReturn(session).anyTimes();
		expect(session.getAttribute("telefone")).andReturn("334");
		expect(session.getAttribute("email")).andReturn("asas@asas.po");
	}
	
	@Test
	public void testeRuaVaziaRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn("");
		expect(req.getParameter("numero")).andReturn("1");
		expect(req.getParameter("bairro")).andReturn("Jardins");
		expectError();
	}

	@Test
	public void testeRuaSoEspacosRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn("  ");
		expect(req.getParameter("numero")).andReturn("1");
		expect(req.getParameter("bairro")).andReturn("Jardins");
		expectError();
	}

	@Test
	public void testeRuaNulaRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn(null);
		expect(req.getParameter("numero")).andReturn("1");
		expect(req.getParameter("bairro")).andReturn("Jardins");
		expectError();
	}
	
	@Test
	public void testeNumeroVazioRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn("Av. Paulista");
		expect(req.getParameter("numero")).andReturn("");
		expect(req.getParameter("bairro")).andReturn("Jardins");
		expectError();
	}

	@Test
	public void testeNumeroSoEspacosRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn("Av. Paulista");
		expect(req.getParameter("numero")).andReturn("  ");
		expect(req.getParameter("bairro")).andReturn("Jardins");
		expectError();
	}

	@Test
	public void testeNumeroNuloRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn("Av. Paulista");
		expect(req.getParameter("numero")).andReturn(null);
		expect(req.getParameter("bairro")).andReturn("Jardins");
		expectError();
	}
	
	@Test
	public void testeBairroVazioRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn("Av. Paulista");
		expect(req.getParameter("numero")).andReturn("1");
		expect(req.getParameter("bairro")).andReturn("");
		expectError();
	}

	@Test
	public void testeBairroSoEspacosRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn("Av. Paulista");
		expect(req.getParameter("numero")).andReturn("1");
		expect(req.getParameter("bairro")).andReturn("   ");
		expectError();
	}

	@Test
	public void testeBairroNuloRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn("Av. Paulista");
		expect(req.getParameter("numero")).andReturn("1");
		expect(req.getParameter("bairro")).andReturn(null);
		expectError();
	}
	
	@Test
	public void testeInsereEnderecoCorretoNaSessaoERedirecionaParaTelaDeSucesso() throws Exception {
		expect(context.getAttribute("clienteDao")).andReturn(new ClienteDAO());
		expect(req.getParameter("nome")).andReturn("Alexandre");
		expect(req.getParameter("rua")).andReturn("Av. Paulista");
		expect(req.getParameter("numero")).andReturn("1");
		expect(req.getParameter("bairro")).andReturn("Jardins");
		expect(req.getParameter("complemento")).andReturn("2 andar");
		expect(req.getParameter("cep")).andReturn("01111-111");
		expect(req.getParameter("cpf")).andReturn("123456");
		
		session.setAttribute(eq("cliente"), isA(Cliente.class));
		
		resp.sendRedirect(eq("mostraEndereco.jsp"));
		
		replay(mocks);
		servlet.doPost(req, resp);
		verify(mocks);
	}

	private void expectError() throws IOException, ServletException {
		session.setAttribute(eq("mensagem"), isA(String.class));
		
		resp.sendRedirect(eq("erro.jsp"));
		
		replay(mocks);
		servlet.doPost(req, resp);
		verify(mocks);
	}
	
}
