package agilpizza.servlet;

import static junit.framework.Assert.assertEquals;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.isA;
import javax.servlet.ServletContext;
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.CategoriaDAO;
import agilpizza.modelo.Ingrediente;

public class PaginaAdministracaoAlteraPrecoServetTest {

	private mudaPreco servlet;
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private HttpSession session;
	private ServletContext servletContext;
	private Object mocks[];

	@Before
	public void setUp() {
		servlet = new mudaPreco();
		req = createNiceMock(HttpServletRequest.class);
		resp = createNiceMock(HttpServletResponse.class);
		session = createNiceMock(HttpSession.class);
		servletContext = createNiceMock(ServletContext.class);
		
		mocks = new Object[] {req, resp, session, servletContext};
	}

	@Test
	public void testeModificacaoDePreco() throws Exception {
		// Criar comportamento
		//Ingrediente ingrediente = new Ingrediente("Muzzarela",4.0);
		CategoriaDAO categoriaDAO = (CategoriaDAO)CategoriaDAO.getCategoriaDAO();
		
		Ingrediente muzzarela = categoriaDAO.buscaCategoria("Queijos").buscaIngrediente("Muzzarela");
		assertEquals("Preco padrao da muzzarela", 3.0, muzzarela.getPreco());
		
		expect(req.getParameter("Muzzarela")).andReturn("6");		
	
		//expect(req.getParameter("Muzzarela")).andReturn("a");
		//req.setAttribute(eq("erro"), eq("Preço invalido"));
		
		
		/*expect(session.getAttribute("clienteIdentificado")).andReturn(ingrediente);
		expect(req.getSession()).andReturn(session).anyTimes();
		expect(session.getAttribute("Muzzarela")).andReturn("4");
		resp.sendRedirect(eq("identificacaoCliente.jsp"));*/

		servletContext.setAttribute(eq("categoriasDao"), isA(CategoriaDAO.class));
		servlet.setServletContext(servletContext);
		
		// Salvar
		replay(mocks);
		
		// Executar
		servlet.doPost(req, resp);
		
		// Testar
		verify(mocks);
		assertEquals("Preco padrao da muzzarela", 6.0, muzzarela.getPreco());
	}
}
