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:org.openmrs.web.controller.patient.PatientFormControllerTest.java

/**
 * @see PatientFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, Object, org.springframework.validation.BindException)
 *///from ww  w .  j a  va 2 s . c om
@Test
@Verifies(value = "not void patient when void reason is empty", method = "onSubmit(HttpServletRequest, HttpServletResponse, Object, BindException)")
public void onSubmit_shouldNotVoidPatientWhenVoidReasonIsEmpty() throws Exception {
    Patient p = Context.getPatientService().getPatient(2);

    HttpServletResponse response = new MockHttpServletResponse();

    PatientFormController controller = (PatientFormController) applicationContext.getBean("patientForm");
    controller.setApplicationContext(applicationContext);

    MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
    request.setParameter("action", "Patient.void");
    request.setParameter("voidReason", "");
    BindException errors = new BindException(p, "patient");
    ModelAndView modelAndview = controller.onSubmit(request, response, p, errors);

    Assert.assertTrue(!p.isVoided());
    String tmp = request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR).toString();
    Assert.assertEquals(tmp, "Patient.error.void.reasonEmpty");
}

From source file:org.openmrs.web.controller.patient.PatientDashboardControllerTest.java

/**
 * @see PatientDashboardController#renderDashboard(String,ModelMap,HttpServletRequest)
 * @verifies redirect to find patient page if given patient id is not an existing uuid
 *///  ww  w.jav a 2  s  . c o m
@Test
public void renderDashboard_shouldRedirectToFindPatientPageIfGivenPatientIdIsNotAnExistingUuid()
        throws Exception {

    MockHttpServletRequest request = new MockHttpServletRequest();
    ModelMap map = new ModelMap();

    String view = patientDashboardController.renderDashboard(NON_EXISTING_PATIENT_UUID, map, request);

    assertThat(view, is(PATIENT_FIND_VIEW));

    assertThat(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ATTR),
            is("patientDashboard.noPatientWithId"));
    assertThat(request.getSession().getAttribute(WebConstants.OPENMRS_ERROR_ARGS),
            is(NON_EXISTING_PATIENT_UUID));
}

From source file:nl.surfnet.coin.teams.service.interceptor.LoginInterceptorTest.java

@Test
public void testPreHandle() throws Exception {
    String remoteUser = "urn:collab:person:surfnet.nl:hansz";

    LoginInterceptor interceptor = new LoginInterceptor();

    OpenConextOAuthClient apiClient = mock(OpenConextOAuthClient.class);
    Person person = new Person();
    person.setId(remoteUser);//from  w  w  w .  j  a v  a 2  s .c  o m
    when(apiClient.getPerson(remoteUser, null)).thenReturn(person);
    MemberAttributeService memberAttributeService = mock(MemberAttributeService.class);
    when(memberAttributeService.findAttributesForMemberId(person.getId()))
            .thenReturn(new ArrayList<MemberAttribute>());
    interceptor.setMemberAttributeService(memberAttributeService);

    interceptor.setApiClient(apiClient);
    interceptor.setTeamEnvironment(new TeamEnvironment());

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("REMOTE_USER", remoteUser);
    request.addHeader("coin-user-status", "member");
    MockHttpServletResponse response = new MockHttpServletResponse();
    boolean loggedIn = interceptor.preHandle(request, response, null);
    assertTrue(loggedIn);
    Assert.assertNotNull(request.getSession().getAttribute("person"));
}

From source file:alpha.portal.webapp.filter.LocaleFilterTest.java

/**
 * Test set locale in session when session is null.
 * // w  w  w . ja v  a2s .  com
 * @throws Exception
 *             the exception
 */
public void testSetLocaleInSessionWhenSessionIsNull() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "es");

    final MockHttpServletResponse response = new MockHttpServletResponse();
    this.filter.doFilter(request, response, new MockFilterChain());

    // no session, should result in null
    Assert.assertNull(request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY));
    // thread locale should always have it, regardless of session
    Assert.assertNotNull(LocaleContextHolder.getLocale());
}

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

/**
 * Test remove.//from  w w  w.j av  a  2  s. c  o  m
 * 
 * @throws Exception
 *             the exception
 */
public void testRemove() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    ServletActionContext.setRequest(request);
    this.action.setDelete("");
    final Paket paket = new Paket();
    paket.setPaketId(2L);
    // action.setPaket(paket);
    this.action.setPaketId(2L);
    Assert.assertEquals("success", this.action.delete());
    Assert.assertNotNull(request.getSession().getAttribute("messages"));
}

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

/**
 * Test remove./*from   w  w  w.j a  v  a 2s.  co  m*/
 * 
 * @throws Exception
 *             the exception
 */
public void testRemove() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    ServletActionContext.setRequest(request);
    this.action.setDelete("");
    final Sigel sigel = new Sigel();
    sigel.setSigelId(2L);
    // action.setSigel(sigel);
    this.action.setSigelId(2L);
    Assert.assertEquals("success", this.action.delete());
    Assert.assertNotNull(request.getSession().getAttribute("messages"));
}

From source file:org.openmrs.contrib.metadatarepository.webapp.filter.LocaleFilterTest.java

public void testLocaleAndCountry() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(new MockHttpSession());
    request.addParameter("locale", "zh_TW");

    MockHttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(request, response, new MockFilterChain());

    // session not null, should result in not null
    Locale locale = (Locale) request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY);
    assertNotNull(locale);/*from www.ja v  a 2  s  . c o  m*/
    assertEquals(new Locale("zh", "TW"), locale);
}

From source file:org.openmrs.contrib.metadatarepository.webapp.filter.LocaleFilterTest.java

public void testSetLocaleInSessionWhenSessionNotNull() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "es");

    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(new MockHttpSession(null));

    filter.doFilter(request, response, new MockFilterChain());

    // session not null, should result in not null
    Locale locale = (Locale) request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY);
    assertNotNull(locale);/*w ww  .j a  va  2s . c  om*/
    assertNotNull(LocaleContextHolder.getLocale());
    assertEquals(new Locale("es"), locale);
}

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

/**
 * Test remove.//from  w  ww. ja v a2 s  .  c  om
 * 
 * @throws Exception
 *             the exception
 */
public void testRemove() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    ServletActionContext.setRequest(request);
    this.action.setDelete("");
    final Journal journal = new Journal();
    journal.setId(2L);
    // action.setJournal(journal);
    this.action.setJournalId(2L);
    Assert.assertEquals("success", this.action.delete());
    Assert.assertNotNull(request.getSession().getAttribute("messages"));
}

From source file:alpha.portal.webapp.filter.LocaleFilterTest.java

/**
 * Test jstl locale is set.//from   w w  w.  j  av a 2s. c o  m
 * 
 * @throws Exception
 *             the exception
 */
public void testJstlLocaleIsSet() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "es");

    final MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(new MockHttpSession(null));

    this.filter.doFilter(request, response, new MockFilterChain());

    Assert.assertNotNull(Config.get(request.getSession(), Config.FMT_LOCALE));
}