Example usage for org.springframework.mock.web MockHttpSession MockHttpSession

List of usage examples for org.springframework.mock.web MockHttpSession MockHttpSession

Introduction

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

Prototype

public MockHttpSession(@Nullable ServletContext servletContext) 

Source Link

Document

Create a new MockHttpSession.

Usage

From source file:org.openmrs.module.feedback.web.AddPredefinedSubjectFormControllerTest.java

/**
 * Test of formBackingObject method, of class AddPredefinedSubjectFormController.
 *//*from w  ww .j  a  va 2s  .  com*/
@Test
public void testFormBackingObject() throws Exception {
    HttpServletRequest req = null;

    authenticate();

    AddPredefinedSubjectFormController instance = new AddPredefinedSubjectFormController();

    System.out.println("formBackingObject");

    /* To execute the test case where the predefinedsubject is < 50 */
    this.request = new MockHttpServletRequest();

    ModelAndView mv = new ModelAndView();

    instance.setSuccessView("someValue");
    request.setSession(new MockHttpSession(null));
    request.setMethod("POST");
    request.addParameter("predefinedsubject", "Testing");
    mv = instance.handleRequest(request, response);

    List predefinedSubjects = service.getPredefinedSubjects();

    for (Iterator it = predefinedSubjects.iterator(); it.hasNext();) {
        PredefinedSubject s = (PredefinedSubject) it.next();

        if ("Testing".equals(s.getSubject())) {
            this.expResult = true;
        }
    }

    Assert.assertEquals(this.expResult, this.result);

    /* To execute the test case where the predefinedsubject is > 50 */
    this.request = new MockHttpServletRequest();
    this.expResult = false;
    mv = new ModelAndView();
    instance.setSuccessView("someValue");
    request.setSession(new MockHttpSession(null));
    request.setMethod("POST");
    request.addParameter("predefinedsubject",
            "TestingTestingTestingTestingTestingTestingTestingTestingTesting");
    mv = instance.handleRequest(request, response);
    predefinedSubjects = service.getPredefinedSubjects();

    for (Iterator it = predefinedSubjects.iterator(); it.hasNext();) {
        PredefinedSubject s = (PredefinedSubject) it.next();

        if ("TestingTestingTestingTestingTestingTestingTestingT".equals(s.getSubject())) {
            this.expResult = true;
        }
    }

    Assert.assertEquals(this.expResult, this.result);

    /* To execute the test case where the predefinedsubject is = 50 */
    this.request = new MockHttpServletRequest();
    this.expResult = false;
    mv = new ModelAndView();
    instance.setSuccessView("someValue");
    request.setSession(new MockHttpSession(null));
    request.setMethod("POST");
    request.addParameter("predefinedsubject", "estingTestingTestingTestingTestingTestingTestingTe");
    mv = instance.handleRequest(request, response);
    predefinedSubjects = service.getPredefinedSubjects();

    for (Iterator it = predefinedSubjects.iterator(); it.hasNext();) {
        PredefinedSubject s = (PredefinedSubject) it.next();

        if ("estingTestingTestingTestingTestingTestingTestingTe".equals(s.getSubject())) {
            this.expResult = true;
        }
    }

    Assert.assertEquals(this.expResult, this.result);

    /* To execute the test case where the predefinedsubject is = 0 */
    this.request = new MockHttpServletRequest();
    this.expResult = true;
    mv = new ModelAndView();
    instance.setSuccessView("someValue");
    request.setSession(new MockHttpSession(null));
    request.setMethod("POST");
    request.addParameter("predefinedsubject", "");
    mv = instance.handleRequest(request, response);
    predefinedSubjects = service.getPredefinedSubjects();

    for (Iterator it = predefinedSubjects.iterator(); it.hasNext();) {
        PredefinedSubject s = (PredefinedSubject) it.next();

        if ("".equals(s.getSubject())) {
            this.expResult = false;
        }
    }

    Assert.assertEquals(this.expResult, this.result);
}

From source file:org.openmrs.module.feedback.web.PredefinedSubjectFormControllerTest.java

/**
 * Test of formBackingObject method, of class PredefinedSubjectFormController.
 *///  ww  w  .j a  va  2 s .  com
@Test
public void testFormBackingObject() throws Exception {
    HttpServletRequest req = null;

    authenticate();

    PredefinedSubjectFormController instance = new PredefinedSubjectFormController();

    System.out.println("formBackingObject");

    /* To check wheather the delete works or not */
    this.request = new MockHttpServletRequest();

    ModelAndView mv = new ModelAndView();

    instance.setSuccessView("someValue");
    request.setSession(new MockHttpSession(null));
    request.setMethod("POST");
    request.setParameter("predefinedsubjectid", "1");
    request.setParameter("delete", "1");
    mv = instance.handleRequest(request, response);

    if (service.getPredefinedSubject(1) == null) {
        result = true;
        Assert.assertEquals(expResult, result);
    } else {
        result = false;
        Assert.assertEquals(expResult, result);
    }

    /* To check that anything except 1 in delete should not delete predefined subject */
    this.request = new MockHttpServletRequest();
    mv = new ModelAndView();
    instance.setSuccessView("someValue");
    request.setSession(new MockHttpSession(null));
    request.setMethod("POST");
    request.setParameter("predefinedsubjectid", "2");
    request.setParameter("delete", "0");
    mv = instance.handleRequest(request, response);

    if (service.getPredefinedSubject(2) != null) {
        result = true;
        Assert.assertEquals(expResult, result);
    } else {
        result = false;
        Assert.assertEquals(expResult, result);
    }

    /* To check wheather the save works or not */
    this.request = new MockHttpServletRequest();
    mv = new ModelAndView();
    instance.setSuccessView("someValue");
    request.setSession(new MockHttpSession(null));
    request.setMethod("POST");
    request.setParameter("predefinedsubjectid", "3");
    request.setParameter("predefinedsubject", "Testsave");
    request.setParameter("save", "1");
    mv = instance.handleRequest(request, response);

    if ("Testsave".equals(service.getPredefinedSubject(3).getSubject())) {
        result = true;
        Assert.assertEquals(expResult, result);
    } else {
        result = false;
        Assert.assertEquals(expResult, result);
    }

    /* To check wheather the save works or not with wrong value of save i.e. save != 1 */
    this.request = new MockHttpServletRequest();
    mv = new ModelAndView();
    instance.setSuccessView("someValue");
    request.setSession(new MockHttpSession(null));
    request.setMethod("POST");
    request.setParameter("predefinedsubjectid", "4");
    request.setParameter("predefinedsubject", "Testsave");
    request.setParameter("save", "0");
    mv = instance.handleRequest(request, response);

    if ("Test".equals(service.getPredefinedSubject(4).getSubject())) {
        result = true;
        Assert.assertEquals(expResult, result);
    } else {
        result = false;
        Assert.assertEquals(expResult, result);
    }
}

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

/**
 * Test set invalid locale./*from  ww w  .  j a v a2s. co m*/
 * 
 * @throws Exception
 *             the exception
 */
public void testSetInvalidLocale() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("locale", "foo");

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

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

    // a locale will get set regardless - there's no such thing as an
    // invalid one
    Assert.assertNotNull(request.getSession().getAttribute(Constants.PREFERRED_LOCALE_KEY));
}

From source file:org.openmrs.module.feedback.web.FeedbackFormControllerTest.java

/**
 *    Testing whether Status Changes works properly
 *//*from  ww w . j a v a  2 s  .com*/
@Test
public void testStatus_FormBackingObject() throws Exception {

    try {
        authenticate();
        System.out.println("testStatus");
        ModelAndView mv = new ModelAndView();

        HttpServletRequest req = null;
        FeedbackFormController instance = new FeedbackFormController();

        instance.setSuccessView("someValue");
        request.setSession(new MockHttpSession(null));
        request.setMethod("POST");
        request.addParameter("status", "TestStatus");
        request.addParameter("feedbackId", "2");

        mv = instance.handleRequest(request, response);

        String st = service.getFeedback(2).getStatus();
        if ("TestStatus".equals(st)) {
            this.expResult = true;
        }

        Assert.assertEquals(this.expResult, this.result);
    } catch (Exception e) {
    }
}

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

/**
 * Test jstl locale is set./*w w w . j a v  a2 s.  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));
}

From source file:org.openmrs.module.feedback.web.FeedbackFormControllerTest.java

/**
 *    Testing whether Assigning new users function works properly.
 *//*from w w w . j  av  a 2s.c o m*/
@Test
public void testAssignNewUser_FormBackingObject() throws Exception {

    try {
        Context.authenticate("test", "Password123");
        //            Context.authenticate("admin", "Admin123");

        authenticate();
        System.out.println("testAssignNewUser");
        ModelAndView mv = new ModelAndView();

        FeedbackFormController instance = new FeedbackFormController();

        instance.setSuccessView("someValue");
        request.setSession(new MockHttpSession(null));
        request.setMethod("POST");
        request.addParameter("addAssignedUser", "test");
        request.addParameter("feedbackId", "2");

        mv = instance.handleRequest(request, response);

        List<Feedback> feedbacks = service
                .getAssignedFeedbacks(Context.getUserService().getUserByUsername("admin"));
        if (feedbacks != null) {
            this.expResult = true;
        }

        Assert.assertEquals(this.expResult, this.result);
    } catch (Exception e) {
    }
}

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

/**
 * @see ConceptProposalFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 *//*  w w  w.java  2 s .  c  o  m*/
@Test
@Verifies(value = "should work properly for country locales", method = "onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)")
public void onSubmit_shouldWorkProperlyForCountryLocales() throws Exception {
    executeDataSet("org/openmrs/api/include/ConceptServiceTest-proposals.xml");

    ConceptService cs = Context.getConceptService();

    final Integer conceptproposalId = 5;
    ConceptProposal cp = cs.getConceptProposal(conceptproposalId);
    Concept conceptToMap = cs.getConcept(4);
    Locale locale = new Locale("en", "GB");

    Assert.assertFalse(conceptToMap.hasName(cp.getOriginalText(), locale));

    ConceptProposalFormController controller = (ConceptProposalFormController) applicationContext
            .getBean("conceptProposalForm");
    controller.setApplicationContext(applicationContext);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(new MockHttpSession(null));
    request.setMethod("POST");
    request.addParameter("conceptProposalId", conceptproposalId.toString());
    request.addParameter("finalText", cp.getOriginalText());
    request.addParameter("conceptId", conceptToMap.getConceptId().toString());
    request.addParameter("conceptNamelocale", locale.toString());
    request.addParameter("action", "");
    request.addParameter("actionToTake", "saveAsSynonym");

    HttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mav = controller.handleRequest(request, response);
    assertNotNull(mav);
    assertTrue(mav.getModel().isEmpty());

    Assert.assertEquals(cp.getOriginalText(), cp.getFinalText());
    Assert.assertTrue(conceptToMap.hasName(cp.getOriginalText(), locale));
}

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

/**
 * Test to make sure a new patient form can save a person relationship
 * /*from  w  ww .j  a  v  a 2  s.c o  m*/
 * @throws Exception
 */
@Test
public void shouldNotDeleteConceptsWhenConceptsAreLocked() throws Exception {
    // this dataset should lock the concepts
    executeDataSet("org/openmrs/web/include/ConceptFormControllerTest.xml");

    ConceptService cs = Context.getConceptService();

    // set up the controller
    ConceptFormController controller = (ConceptFormController) applicationContext.getBean("conceptForm");
    controller.setApplicationContext(applicationContext);
    controller.setSuccessView("index.htm");
    controller.setFormView("concept.form");

    // set up the request and do an initial "get" as if the user loaded the
    // page for the first time
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/dictionary/concept.form?conceptId=3");
    request.setSession(new MockHttpSession(null));
    HttpServletResponse response = new MockHttpServletResponse();
    controller.handleRequest(request, response);

    // set this to be a page submission
    request.setMethod("POST");

    request.addParameter("action", "Delete Concept"); // so that the form is processed

    // send the parameters to the controller
    ModelAndView mav = controller.handleRequest(request, response);

    Assert.assertNotSame("The purge attempt should have failed!", "index.htm", mav.getViewName());
    Assert.assertNotNull(cs.getConcept(3));

}

From source file:org.openmrs.module.feedback.web.FeedbackFormControllerTest.java

/**
 *    Testing whether Assigning new users function works properly.
 *//*from   w w  w . j  a  v a  2  s. c om*/
@Test
public void testRemoveAssignedUser_FormBackingObject() throws Exception {

    try {
        System.out.println("testRemoveAssignedUser");
        ModelAndView mv = new ModelAndView();

        FeedbackFormController instance = new FeedbackFormController();

        instance.setSuccessView("someValue");
        request.setSession(new MockHttpSession(null));
        request.setMethod("POST");
        request.addParameter("delAssignedUser", "test");
        request.addParameter("feedbackId", "2");

        mv = instance.handleRequest(request, response);

        List<Feedback> feedbacks = service
                .getAssignedFeedbacks(Context.getUserService().getUserByUsername("test"));
        if (feedbacks != null) {
            this.expResult = true;
        }

        Assert.assertEquals(this.expResult, this.result);
    } catch (Exception e) {

    }
}

From source file:org.esupportail.commons.batch.WebApplicationEnvironment.java

/**
 * @param preverseSession/*from  w  ww. j  a  v a2 s  .  c  o m*/
 */
private void init(boolean preverseSession) {
    request = new MockHttpServletRequest(context);
    if (preverseSession) {
        request.setSession(session);
    } else {
        session = new MockHttpSession(context);
    }
    response = new MockHttpServletResponse();
}