/*
 * 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.Constantes;
import agilpizza.modelo.Pedido;

public class EnderecoServletTest {

	private ClienteServlet servlet;
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private HttpSession session;
	private Object mocks[];

	@Before
	public void setUp() {
		servlet = new ClienteServlet();
		req = createNiceMock(HttpServletRequest.class);
		resp = createStrictMock(HttpServletResponse.class);
		session = createNiceMock(HttpSession.class);
		mocks = new Object[] {req, resp, session};
	}
	
	@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 testeCEPinvalidoRetornaPaginaDeErro() throws Exception {
		expect(req.getParameter("rua")).andReturn("Av. Paulista");
		expect(req.getParameter("numero")).andReturn("1");
		expect(req.getParameter("bairro")).andReturn("Jardins");
		expect(req.getParameter("cep")).andReturn("011-11111");
		expectError();
	}

	@Test
	public void testeInsereEnderecoCorretoNaSessaoERedirecionaParaTelaDeSucesso() throws Exception {
		expect(req.getParameter("nome")).andReturn("Mari");
		expect(req.getParameter("telefone")).andReturn("555");
		expect(req.getParameter("email")).andReturn("Mari@AgilCoop");
		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.getSession()).andReturn(session).times(3);
		session.setAttribute(eq(Constantes.PEDIDO), isA(Pedido.class));
		expect(session.getAttribute("fechandoPedido")).andReturn(null);

		resp.sendRedirect(eq("mostraEndereco.jsp"));

		replay(mocks);
		servlet.doPost(req, resp);
		verify(mocks);
	}

	private void expectError() throws IOException, ServletException {
		expect(req.getParameter("nome")).andReturn("Mari");
		expect(req.getParameter("telefone")).andReturn("555");
		expect(req.getParameter("email")).andReturn("Mari@AgilCoop");
		resp.sendError(eq(404), eq("Esse endereço não foi encontrado!"));
		
		replay(mocks);
		servlet.doPost(req, resp);
		verify(mocks);
	}
}
