Example usage for org.springframework.mock.web MockHttpServletRequest getSession

List of usage examples for org.springframework.mock.web MockHttpServletRequest getSession

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest getSession.

Prototype

@Override
    @Nullable
    public HttpSession getSession() 

Source Link

Usage

From source file:ejportal.webapp.action.KonsortiumActionTest.java

/**
 * Test save./* w ww  .j  a  v  a 2s.co m*/
 * 
 * @throws Exception
 *             the exception
 */
public void testSave() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    ServletActionContext.setRequest(request);
    this.action.setKonsortiumId(1L);
    Assert.assertEquals("edit", this.action.edit());
    Assert.assertNotNull(this.action.getKonsortium());

    // update Name and save
    this.action.getKonsortium().setKonsortiumName("Updated Name");
    Assert.assertEquals("success", this.action.save());
    Assert.assertEquals("Updated Name", this.action.getKonsortium().getKonsortiumName());
    Assert.assertFalse(this.action.hasActionErrors());
    Assert.assertFalse(this.action.hasFieldErrors());
    Assert.assertNotNull(request.getSession().getAttribute("messages"));
}

From source file:org.openmrs.web.controller.ForgotPasswordFormControllerTest.java

/**
 * If a user enters the wrong secret answer, they should be kicked back to the form and not be
 * accepted even though the username is correct
 *
 * @throws Exception/*from  www. ja v a2  s . c  om*/
 */
@Test
public void shouldFailForAValidUsernameAndInvalidSecretQuestion() throws Exception {
    ForgotPasswordFormController controller = (ForgotPasswordFormController) applicationContext
            .getBean("forgotPasswordForm");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.addParameter("uname", "validuser");
    request.addParameter("secretAnswer", "invalid secret answer");

    HttpServletResponse response = new MockHttpServletResponse();
    controller.handleRequest(request, response);
    Assert.assertEquals("valid secret question", request.getAttribute("secretQuestion"));
    Assert.assertEquals("auth.answer.invalid",
            request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR));
    Assert.assertEquals("auth.question.fill", request.getSession().getAttribute(WebConstants.OPENMRS_MSG_ATTR));
    Assert.assertFalse(Context.isAuthenticated());
}

From source file:ejportal.webapp.action.JournalActionTest.java

/**
 * Test save.// ww w  .  j  a  v  a 2 s .  c o  m
 * 
 * @throws Exception
 *             the exception
 */
public void testSave() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    ServletActionContext.setRequest(request);
    this.action.setJournalId(1L);
    Assert.assertEquals("edit", this.action.edit());
    Assert.assertNotNull(this.action.getJournalBaseTO());

    // update Kurztitel and save
    this.action.getJournalBaseTO().setTitel("Updated Kurztitel");
    Assert.assertEquals("success", this.action.save());
    Assert.assertEquals("Updated Kurztitel", this.action.getJournalBaseTO().getTitel());
    Assert.assertFalse(this.action.hasActionErrors());
    Assert.assertFalse(this.action.hasFieldErrors());
    Assert.assertNotNull(request.getSession().getAttribute("messages"));
}

From source file:org.springframework.test.web.servlet.htmlunit.HtmlUnitRequestBuilderTest.java

@Test
public void buildRequestSession() throws Exception {
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);

    HttpSession newSession = actualRequest.getSession();
    assertThat(newSession).isNotNull();//from www .  j  a  v  a2  s .co  m
    assertSingleSessionCookie("JSESSIONID=" + newSession.getId() + "; Path=/test; Domain=example.com");

    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + newSession.getId());

    requestBuilder = new HtmlUnitRequestBuilder(sessions, cookieManager, webRequest);
    actualRequest = requestBuilder.buildRequest(servletContext);

    assertThat(actualRequest.getSession()).isSameAs(newSession);
}

From source file:ejportal.webapp.action.BestellerActionTest.java

/**
 * Test save./*from ww  w .ja  va2  s  .  c o m*/
 * 
 * @throws Exception
 *             the exception
 */
public void testSave() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    ServletActionContext.setRequest(request);
    this.action.setBestellerId(1L);
    Assert.assertEquals("edit", this.action.edit());
    Assert.assertNotNull(this.action.getBestellerBaseTO());

    // update Name and save
    this.action.getBestellerBaseTO().setBestellerName("Updated Name");
    Assert.assertEquals("success", this.action.save());
    Assert.assertEquals("Updated Name", this.action.getBestellerBaseTO().getBestellerName());
    Assert.assertFalse(this.action.hasActionErrors());
    Assert.assertFalse(this.action.hasFieldErrors());
    Assert.assertNotNull(request.getSession().getAttribute("messages"));
}

From source file:org.springframework.test.web.servlet.htmlunit.HtmlUnitRequestBuilderTest.java

@Test
public void buildRequestSessionWithExistingSession() throws Exception {
    String sessionId = "session-id";
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId);
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);

    HttpSession session = actualRequest.getSession();
    assertThat(session.getId()).isEqualTo(sessionId);
    assertSingleSessionCookie("JSESSIONID=" + session.getId() + "; Path=/test; Domain=example.com");

    requestBuilder = new HtmlUnitRequestBuilder(sessions, cookieManager, webRequest);
    actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getSession()).isEqualTo(session);

    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId + "NEW");
    actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getSession()).isNotEqualTo(session);
    assertSingleSessionCookie(//from  w  w  w . ja  v a 2s.co m
            "JSESSIONID=" + actualRequest.getSession().getId() + "; Path=/test; Domain=example.com");
}

From source file:ejportal.webapp.action.InteresseActionTest.java

/**
 * Test save./*w ww  . j  a va2  s .  c o  m*/
 * 
 * @throws Exception
 *             the exception
 */
public void testSave() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    ServletActionContext.setRequest(request);
    this.action.setInteresseId(1L);
    Assert.assertEquals("edit", this.action.edit());
    Assert.assertNotNull(this.action.getInteresseBaseTO());

    // update Name and save
    this.action.getInteresseBaseTO().setInteresse("Updated Interesse");
    Assert.assertEquals("success", this.action.save());
    Assert.assertEquals("Updated Interesse", this.action.getInteresseBaseTO().getInteresse());
    Assert.assertFalse(this.action.hasActionErrors());
    Assert.assertFalse(this.action.hasFieldErrors());
    Assert.assertNotNull(request.getSession().getAttribute("messages"));
}

From source file:com.ideabase.repository.test.webservice.RESTfulControllerTest.java

/**
 * Request for storing new object.// w ww.  j a  v  a 2s  .  c o  m
 * @throws Exception
 */
public void testSaveOperation() throws Exception {
    // Create dummy user object
    final Integer dummyUserId = TestCaseRepositoryHelper.fixCreateUser(mUserService, "hasan", "hasankhan");

    // Create fixture data
    final List<Integer> createdItemIdList = TestCaseRepositoryHelper.fixCreateItems(mRepositoryService, 1);
    // Set dummy user with the deletable list of items
    createdItemIdList.add(dummyUserId);

    try {
        final Subject subject = getSubjectFromASuccessfulRequest();
        // build mock request
        final MockHttpServletRequest request = new MockHttpServletRequest();
        request.setRequestURI("/service/save/item.xml");
        request.setMethod(METHOD_POST);
        request.getSession().setAttribute(WebConstants.SESSION_ATTR_USER_SUBJECT, subject);
        request.addParameter("item", fixContent());

        // Build mock response
        final MockHttpServletResponse response = new MockHttpServletResponse();

        // send request
        mRestfulController.handleRequest(request, response);

        // verify response
        final String content = response.getContentAsString();
        assertFalse("Response Content is empty", content.length() == 0);
        assertTrue("State is false.", content.indexOf("true") != -1);
        LOG.debug("Response - " + content);
    } finally {
        TestCaseRepositoryHelper.fixRemoveItems(mRepositoryService, createdItemIdList);
    }
}

From source file:ejportal.webapp.action.RechnungActionTest.java

/**
 * Test save./*w w w.j  a va2s.  com*/
 * 
 * @throws Exception
 *             the exception
 */
public void testSave() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    ServletActionContext.setRequest(request);
    this.action.setRechnungId(1L);
    Assert.assertEquals("edit", this.action.edit());
    Assert.assertNotNull(this.action.getRechnungBaseTO());

    // update bezugsform and save
    this.action.getRechnungBaseTO().setBezugsform("Updated Bezugsform");
    Assert.assertEquals("success", this.action.save());
    Assert.assertEquals("Updated Bezugsform", this.action.getRechnungBaseTO().getBezugsform());
    Assert.assertFalse(this.action.hasActionErrors());
    Assert.assertFalse(this.action.hasFieldErrors());
    Assert.assertNotNull(request.getSession().getAttribute("messages"));
}

From source file:org.springframework.test.web.servlet.htmlunit.HtmlUnitRequestBuilderTest.java

@Test
public void buildRequestSessionInvalidate() throws Exception {
    String sessionId = "session-id";
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId);

    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    HttpSession sessionToRemove = actualRequest.getSession();
    sessionToRemove.invalidate();//from  w  w  w  .  jav  a  2 s .  c  o  m

    assertThat(!sessions.containsKey(sessionToRemove.getId()));
    assertSingleSessionCookie("JSESSIONID=" + sessionToRemove.getId()
            + "; Expires=Thu, 01-Jan-1970 00:00:01 GMT; Path=/test; Domain=example.com");

    webRequest.removeAdditionalHeader("Cookie");
    requestBuilder = new HtmlUnitRequestBuilder(sessions, cookieManager, webRequest);

    actualRequest = requestBuilder.buildRequest(servletContext);

    assertThat(actualRequest.getSession().isNew()).isTrue();
    assertThat(!sessions.containsKey(sessionToRemove.getId()));
}