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;

public class IdentificacaoServletTest {
	
	private IdentificacaoServlet 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 IdentificacaoServlet(){
			@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();		
	}
	
	@Test
	public void testeComTelefoneEEmailNulos() throws Exception
	{
		expect(req.getParameter("telefone")).andReturn(null);
		expect(req.getParameter("email")).andReturn(null);
		expectError();
	}
	@Test
	public void testeComTelefoneEEmailVazios() throws Exception {
		expect(req.getParameter("telefone")).andReturn("");
		expect(req.getParameter("email")).andReturn("");
		expectError();
	}
	
	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);
	}

}
