/*
 * Created on 12/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 java.util.List;

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.ProdutoDAO;

public class MontarServletTest {

	private MontarServlet 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 MontarServlet(){
			@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};
	}
	
	@Test
	public void testeSemIngredienteRetornaPaginaDeErro() throws Exception {
		String [] teste = null;
		expect(req.getParameterValues("chkIngrediente")).andReturn(teste);
		expectError();
	}

	
	@Test
	public void testeUmIngredienteRedirecionaParaTelaDeSucesso() throws Exception {

		String [] teste = new String[1];
		teste[0] = "Ovo";		
		expect(req.getParameterValues("chkIngrediente")).andReturn(teste);
		expect(req.getParameter("QuantidadeOvo")).andReturn("1");
		context.setAttribute(eq("produtos"), isA(ProdutoDAO.class));
		expect(req.getSession()).andReturn(session);
		session.setAttribute(eq("PizzaCliente"), isA(List.class));
		
		resp.sendRedirect(eq("mostraPizza.jsp"));
		
		replay(mocks);
		servlet.doPost(req, resp);
		verify(mocks);
	}

	@Test
	public void testeIngredientesRedirecionaParaTelaDeSucesso() throws Exception {

		String [] teste = new String[2];
		teste[0] = "Ovo";
		teste[1] = "Presunto";		
		expect(req.getParameterValues("chkIngrediente")).andReturn(teste);
		expect(req.getParameter("QuantidadeOvo")).andReturn("1");
		expect(req.getParameter("QuantidadePresunto")).andReturn("1");
		expect(req.getSession()).andReturn(session);
		session.setAttribute(eq("PizzaCliente"), isA(List.class));
	
		context.setAttribute(eq("produtos"), isA(ProdutoDAO.class));
		
		resp.sendRedirect(eq("mostraPizza.jsp"));
		
		replay(mocks);
		servlet.doPost(req, resp);
		verify(mocks);
	}

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