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.encounter.EncounterRoleFormControllerTest.java

/**
 * @verifies purge an existing encounter
 * @see EncounterRoleFormController#purge(javax.servlet.http.HttpSession, org.openmrs.EncounterRole, org.springframework.validation.BindingResult)
 *//*ww  w .  ja va  2s.  c  om*/
@Test
public void purge_shouldPurgeAnExistingEncounter() throws Exception {
    executeDataSet(ENC_INITIAL_DATA_XML);
    EncounterRoleFormController controller = new EncounterRoleFormController();
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = request.getSession();
    EncounterRole encounterRole = Context.getEncounterService().getEncounterRole(1);
    BindException errors = new BindException(encounterRole, "encounterRole");
    controller.purge(session, encounterRole, errors);
    Assert.assertEquals("EncounterRole.purgedSuccessfully",
            session.getAttribute(WebConstants.OPENMRS_MSG_ATTR));
}

From source file:org.openmrs.web.controller.encounter.EncounterRoleFormControllerTest.java

/**
 * @verifies raise an error if retire reason is not filled
 * @see EncounterRoleFormController#retire(javax.servlet.http.HttpSession, org.openmrs.EncounterRole, org.springframework.validation.BindingResult)
 *///from  w w w.  j a v a 2  s  .  co  m
@Test
public void retire_shouldRaiseAnErrorIfRetireReasonIsNotFilled() throws Exception {
    executeDataSet(ENC_INITIAL_DATA_XML);
    EncounterRoleFormController controller = new EncounterRoleFormController();
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = request.getSession();
    EncounterRole encounterRole = Context.getEncounterService().getEncounterRole(1);
    encounterRole.setRetireReason(""); //setting empty retire reason so that it will raise an error.
    BindException errors = new BindException(encounterRole, "encounterRole");
    controller.retire(session, encounterRole, errors);
    Assert.assertEquals(1, errors.getErrorCount());
}

From source file:org.openmrs.web.controller.encounter.EncounterRoleFormControllerTest.java

/**
 * @verifies save a new encounter role object
 * @see EncounterRoleFormController#save(javax.servlet.http.HttpSession, org.openmrs.EncounterRole, org.springframework.validation.BindingResult)
 *//*from   w w  w  .ja  v  a2  s.c  om*/
@Test
public void saveEncounterRole_shouldSaveANewEncounterRoleObject() throws Exception {
    EncounterRoleFormController controller = new EncounterRoleFormController();
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = request.getSession();
    EncounterRole encounterRole = new EncounterRole();
    encounterRole.setName("attending physician");
    encounterRole.setDescription("person in charge");
    BindException errors = new BindException(encounterRole, "encounterRole");
    controller.save(session, encounterRole, errors);
    Assert.assertNotNull(encounterRole.getId());

}

From source file:org.openmrs.web.controller.encounter.EncounterRoleFormControllerTest.java

/**
 * @verifies retire an existing encounter
 * @see EncounterRoleFormController#retire(javax.servlet.http.HttpSession, org.openmrs.EncounterRole, org.springframework.validation.BindingResult)
 *//*from w  ww.java 2 s .c  o m*/
@Test
public void retire_shouldRetireAnExistingEncounter() throws Exception {
    executeDataSet(ENC_INITIAL_DATA_XML);
    EncounterRoleFormController controller = new EncounterRoleFormController();
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = request.getSession();
    EncounterRole encounterRole = Context.getEncounterService().getEncounterRole(1);
    encounterRole.setRetireReason("this role is no more existing");
    BindException errors = new BindException(encounterRole, "encounterRole");
    controller.retire(session, encounterRole, errors);
    Assert.assertNotNull(encounterRole.getId());
    Assert.assertTrue(encounterRole.isRetired());
    Assert.assertEquals("EncounterRole.retiredSuccessfully",
            session.getAttribute(WebConstants.OPENMRS_MSG_ATTR));
}

From source file:org.openmrs.web.controller.concept.ConceptStopWordListControllerTest.java

/**
 * @see ConceptStopWordListController#showForm(javax.servlet.http.HttpSession)
 *//*from  w ww  .j av a2 s  .com*/
@SuppressWarnings("unchecked")
@Test
@Verifies(value = "should add all ConceptStopWords in session attribute", method = "showForm(HttpSession)")
public void showForm_shouldAddAllConceptStopWordsInSessionAttribute() throws Exception {
    ConceptStopWordListController controller = (ConceptStopWordListController) applicationContext
            .getBean("conceptStopWordListController");

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setMethod("POST");

    controller.showForm(mockRequest.getSession());

    List<ConceptStopWord> conceptStopWordList = (List<ConceptStopWord>) mockRequest.getSession()
            .getAttribute("conceptStopWordList");
    Assert.assertNotNull(conceptStopWordList);
    Assert.assertEquals(4, conceptStopWordList.size());
}

From source file:org.openmrs.web.controller.encounter.EncounterRoleFormControllerTest.java

/**
 * @verifies edit and save an existing encounter
 * @see EncounterRoleFormController#save(javax.servlet.http.HttpSession, org.openmrs.EncounterRole, org.springframework.validation.BindingResult)
 *///from   w ww .  jav a  2  s .c o  m
@Test
public void saveEncounterRole_shouldEditAndSaveAnExistingEncounter() throws Exception {
    executeDataSet(ENC_INITIAL_DATA_XML);
    EncounterRoleFormController controller = new EncounterRoleFormController();
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = request.getSession();
    EncounterRole encounterRole = Context.getEncounterService().getEncounterRole(2);
    String roleName = "surgeon";
    String description = "person who did the operation";
    encounterRole.setName(roleName);
    encounterRole.setDescription(description);
    BindException errors = new BindException(encounterRole, "encounterRole");
    controller.save(session, encounterRole, errors);
    Assert.assertNotNull(encounterRole.getId());
    Assert.assertEquals(roleName, encounterRole.getName());
    Assert.assertEquals(description, encounterRole.getDescription());
}

From source file:org.openmrs.web.controller.concept.ConceptStopWordListControllerTest.java

/**
 * @see ConceptStopWordListController#showForm(javax.servlet.http.HttpSession)
 *//*from w ww.ja v  a  2s  .c om*/
@Test
@Verifies(value = "should return Concept Stop Word List View", method = "showForm(HttpSession)")
public void showForm_shouldReturnConceptStopWordListView() throws Exception {
    ConceptStopWordListController controller = (ConceptStopWordListController) applicationContext
            .getBean("conceptStopWordListController");

    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setMethod("POST");

    String showFormResult = controller.showForm(mockRequest.getSession());

    Assert.assertNotNull(showFormResult);
    Assert.assertEquals("admin/concepts/conceptStopWordList", showFormResult);
}

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

/**
 * Convenience method to get the "model" from the controller's handleRequest method
 * /*from  w ww .ja  va  2  s . co  m*/
 * @param patientId the patient id to fetch
 * @return the Map from string to object of everything in the generated "model"
 * @throws Exception
 */
private Map<String, Object> getModelFromController(Integer patientId) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
    HttpServletResponse response = new MockHttpServletResponse();

    request.setAttribute(WebConstants.INIT_REQ_UNIQUE_ID, "1");
    request.getSession().setAttribute(WebConstants.OPENMRS_PORTLET_LAST_REQ_ID, "0");
    request.setAttribute("javax.servlet.include.servlet_path", "testPortlet");
    request.setAttribute("org.openmrs.portlet.parameters", new HashMap());
    request.setAttribute("org.openmrs.portlet.patientId", patientId);

    ModelAndView modelAndView = new PortletController().handleRequest(request, response);

    return (Map<String, Object>) modelAndView.getModel().get("model");
}

From source file:fi.okm.mpass.idp.authn.impl.OpenIdConnectIdentityTest.java

@Test
public void failsAuthenticationStateMismatchGetSubject() throws Exception {
    MockHttpServletRequest mockHttpServletRequest = getRequest();
    mockHttpServletRequest.setQueryString("code=1234abcd&state=1234abcd");
    mockHttpServletRequest.getSession().setAttribute("fi.okm.mpass.state", new State("234abcd"));
    openIdConnectIdentity.setTokenEndpoint(token_endpoint);
    openIdConnectIdentity.setClientId("oauth2ClientId");
    openIdConnectIdentity.setClientSecret("oauth2ClientSecret");
    try {//from   w  w w  .j av a2 s  .  c om
        openIdConnectIdentity.getSubject(mockHttpServletRequest);
    } catch (SocialUserAuthenticationException e) {
        Assert.assertEquals(e.getMessage(), "State parameter not satisfied");
    }
}

From source file:org.hdiv.web.servlet.tags.form.AbstractHtmlElementTagTests.java

protected void initDataComposer() {

    String[] files = { "/org/hdiv/config/hdiv-core-applicationContext.xml", "/hdiv-config.xml",
            "/hdiv-validations.xml", };

    if (this.hdivContext == null) {
        this.hdivContext = new ClassPathXmlApplicationContext(files);
    }//from   w ww  .j  a  v a  2  s.c om

    //API mock de Servlet
    //      HttpServletRequest request = (MockHttpServletRequest) this.hdivContext.getBean("mockRequest");
    MockHttpServletRequest request = (MockHttpServletRequest) pageContext.getRequest();
    HttpSession httpSession = request.getSession();
    ServletContext servletContext = httpSession.getServletContext();
    HDIVUtil.setHttpServletRequest(request);

    //inicializar StateCache en session
    this.initStateCache(httpSession);

    //inicializar HDIVConfig en ServletContext
    HDIVConfig hdivConfig = (HDIVConfig) this.hdivContext.getBean("config");
    HDIVUtil.setHDIVConfig(hdivConfig, servletContext);

    //inicializar IApplication en ServletContext
    IApplication application = (IApplication) this.hdivContext.getBean("application");
    HDIVUtil.setApplication(application, servletContext);

    //inicializar MessageSource en ServletContext
    MessageSource messageSource = (MessageSource) this.hdivContext;
    HDIVUtil.setMessageSource(messageSource, servletContext);

    //inicializar el datacomposer
    DataComposerFactory dataComposerFactory = (DataComposerFactory) this.hdivContext
            .getBean("dataComposerFactory");
    IDataComposer dataComposer = dataComposerFactory.newInstance();
    dataComposer.beginRequest("/testFormTag.do");
    HDIVUtil.setDataComposer(dataComposer, request);
}