PageRenderTime 19ms CodeModel.GetById 2ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/server/bootstrap/ControllerTestCase.java

http://sigma-h.googlecode.com/
Java | 140 lines | 105 code | 28 blank | 7 comment | 0 complexity | 59f11308cee9f52736ea87d63de9b5a8 MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.server.bootstrap;
  7
  8import com.google.inject.Injector;
  9import org.easymock.IAnswer;
 10import org.junit.Before;
 11import org.sigmah.server.auth.Authenticator;
 12import org.sigmah.server.bootstrap.model.ConfirmInvitePageModel;
 13import org.sigmah.server.bootstrap.model.HostPageModel;
 14import org.sigmah.server.bootstrap.model.LoginPageModel;
 15import org.sigmah.server.bootstrap.model.PageModel;
 16import org.sigmah.server.dao.AuthenticationDAO;
 17import org.sigmah.shared.dao.UserDAO;
 18import org.sigmah.server.domain.Authentication;
 19import org.sigmah.shared.domain.User;
 20import org.sigmah.server.mock.MockHttpServletRequest;
 21import org.sigmah.server.mock.MockHttpServletResponse;
 22import org.sigmah.server.mock.MockTemplateConfiguration;
 23
 24import javax.persistence.NoResultException;
 25import javax.servlet.ServletException;
 26import java.io.IOException;
 27
 28import static org.easymock.EasyMock.*;
 29import static org.junit.Assert.assertEquals;
 30
 31/**
 32 * @author Alex Bertram
 33 */
 34public abstract class ControllerTestCase {
 35    protected MockHttpServletRequest req;
 36    protected MockHttpServletResponse resp;
 37    protected Injector injector;
 38    protected MockTemplateConfiguration templateCfg;
 39    protected AbstractController controller;
 40
 41    protected static final String USER_EMAIL = "alex@bertram.com";
 42    protected static final String CORRECT_USER_PASS = "mypass";
 43    protected static final String WRONG_USER_PASS = "notmypass";
 44
 45    protected static final String NEW_AUTH_TOKEN = "XYZ123";
 46
 47    protected static final String NEW_USER_KEY = "ABC456";
 48    protected static final String NEW_USER_EMAIL = "bart@bart.nl";
 49    protected static final String BAD_KEY = "muwahaha";
 50    protected User newUser;
 51    protected static final String NEW_USER_NAME = "Henry";
 52    protected static final String NEW_USER_CHOSEN_LOCALE = "fr";
 53
 54    protected static final String GOOD_AUTH_TOKEN = "BXD556";
 55    protected static final String BAD_AUTH_TOKEN = "NONSENSE";
 56
 57    public ControllerTestCase() {
 58        newUser = new User();
 59    }
 60
 61    @Before
 62    public final void setUpDependencies()  {
 63        req = new MockHttpServletRequest();
 64        resp = new MockHttpServletResponse();
 65
 66        User user = new User();
 67        user.setEmail(USER_EMAIL);
 68
 69        newUser.setEmail(NEW_USER_EMAIL);
 70        newUser.setNewUser(true);
 71        newUser.setChangePasswordKey(NEW_USER_KEY);
 72
 73        UserDAO userDAO = createMock(UserDAO.class);
 74        expect(userDAO.findUserByEmail(USER_EMAIL)).andReturn(user);
 75        expect(userDAO.findUserByEmail(not(eq(USER_EMAIL)))).andThrow(new NoResultException());
 76
 77        expect(userDAO.findUserByChangePasswordKey(NEW_USER_KEY)).andReturn(newUser);
 78        expect(userDAO.findUserByChangePasswordKey(BAD_KEY)).andThrow(new NoResultException());
 79        replay(userDAO);
 80
 81        Authentication auth = new Authentication(user);
 82        auth.setId(GOOD_AUTH_TOKEN);
 83
 84        AuthenticationDAO authDAO = createNiceMock(AuthenticationDAO.class);
 85        expect(authDAO.findById(eq(GOOD_AUTH_TOKEN))).andReturn(auth);
 86        authDAO.persist(isA(Authentication.class));
 87        expectLastCall().andAnswer(new IAnswer<Object>() {
 88            @Override
 89            public Object answer() throws Throwable {
 90                Authentication auth = (Authentication) getCurrentArguments()[0];
 91                auth.setId(NEW_AUTH_TOKEN);
 92                return null;
 93            }
 94        });
 95        replay(authDAO);
 96
 97        Authenticator authenticator = createMock(Authenticator.class);
 98        expect(authenticator.check(eq(user), eq(WRONG_USER_PASS))).andReturn(false);
 99        expect(authenticator.check(eq(user), eq(CORRECT_USER_PASS))).andReturn(true);
100        replay(authenticator);
101
102        injector = createNiceMock(Injector.class);
103        expect(injector.getInstance(AuthenticationDAO.class)).andReturn(authDAO);
104        expect(injector.getInstance(Authenticator.class)).andReturn(authenticator);
105        expect(injector.getInstance(UserDAO.class)).andReturn(userDAO);
106        replay(injector);
107
108        templateCfg = new MockTemplateConfiguration();
109    }
110
111    public void get() throws IOException, ServletException {
112        controller.callGet(req, resp);
113    }
114
115    protected void get(String url) throws IOException, ServletException {
116        req.setRequestURL(url);
117        get();
118    }
119
120    protected void post() throws IOException, ServletException {
121        controller.callPost(req, resp);
122    }
123
124
125    protected <T extends PageModel> void assertTemplateUsed(Class<T> modelclass) {
126        assertEquals(PageModel.getTemplateName(modelclass), templateCfg.lastTemplateName);
127    }
128
129    private HostPageModel lastHostPageModel() {
130        return ((HostPageModel) templateCfg.lastModel);
131    }
132
133    protected LoginPageModel lastLoginPageModel() {
134        return (LoginPageModel) templateCfg.lastModel;
135    }
136
137    protected ConfirmInvitePageModel lastNewUserPageModel() {
138        return (ConfirmInvitePageModel) templateCfg.lastModel;
139    }
140}