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

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

Introduction

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

Prototype

public void addParameter(String name, String... values) 

Source Link

Document

Add an array of values for the specified HTTP parameter.

Usage

From source file:org.araneaframework.tests.FormTest.java

/**
 * Testing reading from request with a grouped constraint set.
 *//* www. j  a v  a2  s . c o  m*/
public void testFormActiveGroupedConstraintValidates() throws Exception {

    FormWidget testForm = makeUsualForm();

    MockHttpServletRequest notMandatoryMissingRequest = new MockHttpServletRequest();

    notMandatoryMissingRequest.addParameter("testForm.__present", "true");
    notMandatoryMissingRequest.addParameter("testForm.myCheckBox", "true");
    notMandatoryMissingRequest.addParameter("testForm.myLongText", "108");
    notMandatoryMissingRequest.addParameter("testForm.myDateTime.date", "11.10.2015");
    notMandatoryMissingRequest.addParameter("testForm.myDateTime.time", "01:01");
    notMandatoryMissingRequest.addParameter("testForm.hierarchyTest.myTextarea", "blah");
    notMandatoryMissingRequest.addParameter("testForm.hierarchyTest.mySelect", "2");

    // create helper
    ConstraintGroupHelper groupHelper = new ConstraintGroupHelper();
    testForm.getElement("myDateTime")
            .setConstraint(groupHelper.createGroupedConstraint(new NotEmptyConstraint(), "active"));

    StandardServletInputData input = new StandardServletInputData(notMandatoryMissingRequest);
    input.pushScope("testForm");
    testForm._getWidget().update(input);

    groupHelper.setActiveGroup("active");
    assertTrue("Test form must be valid after reading from request", testForm.convertAndValidate());
}

From source file:org.araneaframework.tests.FormConstraintTest.java

/**
 * Testing reading from request with a grouped constraint set.
 *///ww  w  . j a  v a2s  .c om
public void testFormActiveGroupedConstraintValidates() throws Exception {

    FormWidget testForm = makeUsualForm();

    MockHttpServletRequest request = new MockHttpServletRequest();

    request.addParameter("testForm.__present", "true");
    request.addParameter("testForm.myCheckBox", "true");
    request.addParameter("testForm.myLongText", "108");
    request.addParameter("testForm.myDateTime.date", "11.10.2015");
    request.addParameter("testForm.myDateTime.time", "01:01");

    // create helper
    ConstraintGroupHelper groupHelper = new ConstraintGroupHelper();
    testForm.getElement("myDateTime")
            .setConstraint(groupHelper.createGroupedConstraint(new NotEmptyConstraint(), "active"));

    StandardServletInputData input = new StandardServletInputData(request);
    input.pushScope("testForm");
    testForm._getWidget().update(input);

    groupHelper.setActiveGroup("active");
    assertTrue("Test form must be valid after reading from request", testForm.convertAndValidate());
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.PersonControllerTest.java

/**
 * Tests if voided names are not shown in full representation. Adds a name and shows full
 * representation. Then voids the name and sees decrease in number of names in full
 * representation./*ww  w.  j a  v  a 2s .  com*/
 * 
 * @see PersonController#getProperty(Person,String)
 * @verifies do not show voided names in full representation
 * @throws Exception
 */
@Test
public void shouldNotShowVoidedNamesInFullRepresentation() throws Exception {
    Person p = new PersonResource().getByUniqueId("5946f880-b197-400b-9caa-a3c661d23041");
    PersonName pn = new PersonName("SUNNY", "TEST", "PURKAYASTHA");
    p.addName(pn);
    Context.getPersonService().savePerson(p);
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);
    Object result = new PersonController().retrieve("5946f880-b197-400b-9caa-a3c661d23041", req);
    Assert.assertEquals(2,
            StringUtils.countMatches(PropertyUtils.getProperty(result, "names").toString(), "givenName"));
    pn.setVoided(true);
    Context.getPersonService().savePerson(p);
    result = new PersonController().retrieve("5946f880-b197-400b-9caa-a3c661d23041", req);
    Assert.assertEquals(1,
            StringUtils.countMatches(PropertyUtils.getProperty(result, "names").toString(), "givenName"));
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.PersonControllerTest.java

/**
 * Tests if voided attributes are not shown in representation. Voids all the person attributes
 * and checks if they are not shown//w ww.j  ava 2s .  c om
 * 
 * @see PersonController#getProperty(Person,String)
 * @verifies do not show voided attributes
 * @throws Exception
 */
@Test
public void shouldNotShowVoidedAttributesInRepresentation() throws Exception {
    Person p = new PersonResource().getByUniqueId("5946f880-b197-400b-9caa-a3c661d23041");
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);
    Object result = new PersonController().retrieve("5946f880-b197-400b-9caa-a3c661d23041", req);
    Assert.assertEquals(3, ((Collection<?>) PropertyUtils.getProperty(result, "attributes")).size());
    Set<PersonAttribute> attributes = p.getAttributes();
    for (PersonAttribute pa : attributes) {
        pa.setVoided(true);
    }
    p.setAttributes(attributes);
    Context.getPersonService().savePerson(p);
    result = new PersonController().retrieve("5946f880-b197-400b-9caa-a3c661d23041", req);
    Assert.assertEquals(0,
            StringUtils.countMatches(PropertyUtils.getProperty(result, "attributes").toString(), "value"));
}

From source file:com.iflytek.edu.cloud.frame.web.filter.CheckOpenServiceFilterTest.java

/**
 * ?method?/*from w w w . j  a  v a 2 s  .  co  m*/
 */
@Test
@Ignore
public void testMethodIsNull() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setCharacterEncoding("UTF-8");

    try {
        request.setMethod("POST");
        request.addParameter("version", "1.0.0");
        filter.doFilter(request, response, null);

        Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_BAD_REQUEST);
        Assert.assertEquals(MainErrorType.MISSING_METHOD.value(), ErrorMsgParser.getErrorCode(response));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ServletException e) {
        e.printStackTrace();
    }
}

From source file:com.iflytek.edu.cloud.frame.web.filter.CheckOpenServiceFilterTest.java

/**
 * ?version?/*w w  w.  j a  v  a 2 s.  c o  m*/
 */
@Test
@Ignore
public void testVersionIsNull() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setCharacterEncoding("UTF-8");

    try {
        request.setMethod("POST");
        request.addParameter("method", "user.get");
        filter.doFilter(request, response, null);

        Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_BAD_REQUEST);
        Assert.assertEquals(MainErrorType.MISSING_VERSION.value(), ErrorMsgParser.getErrorCode(response));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ServletException e) {
        e.printStackTrace();
    }
}

From source file:com.iflytek.edu.cloud.frame.web.filter.CheckOpenServiceFilterTest.java

/**
 * ??//www  . j a va  2s  .  c o m
 */
@Test
@Ignore
public void testMethodNotExist() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setCharacterEncoding("UTF-8");

    try {
        request.setMethod("POST");
        request.addParameter("method", "user.add");
        request.addParameter("version", "1.0.0");
        filter.doFilter(request, response, null);

        Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_BAD_REQUEST);
        Assert.assertEquals(MainErrorType.INVALID_METHOD.value(), ErrorMsgParser.getErrorCode(response));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ServletException e) {
        e.printStackTrace();
    }
}

From source file:com.iflytek.edu.cloud.frame.web.filter.CheckOpenServiceFilterTest.java

/**
 * ?version?/*from  w w  w.ja v a2  s .c  o m*/
 */
@Test
@Ignore
public void testVersionNotExist() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setCharacterEncoding("UTF-8");

    try {
        request.setMethod("POST");
        request.addParameter("method", "user.get");
        request.addParameter("version", "1.0.1");
        filter.doFilter(request, response, null);

        Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_BAD_REQUEST);
        Assert.assertEquals(MainErrorType.UNSUPPORTED_VERSION.value(), ErrorMsgParser.getErrorCode(response));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ServletException e) {
        e.printStackTrace();
    }
}

From source file:com.iflytek.edu.cloud.frame.web.filter.CheckOpenServiceFilterTest.java

/**
 * ?access_token?/* w ww .j a v a2  s.  c  o m*/
 */
@Test
@Ignore
public void testAccessTokenIsNull() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setCharacterEncoding("UTF-8");

    try {
        request.setMethod("POST");
        request.addParameter("method", "user.get");
        request.addParameter("version", "1.0.0");
        filter.doFilter(request, response, null);

        Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_BAD_REQUEST);
        Assert.assertEquals(MainErrorType.MISSING_ACCESS_TOKEN.value(), ErrorMsgParser.getErrorCode(response));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ServletException e) {
        e.printStackTrace();
    }
}

From source file:org.araneaframework.tests.FormControlTest.java

/**
 * Tests that {@link TextControl} uses the min/max length parameters
 * for validation. /* ww w  . j  a  va  2  s . c o  m*/
 */
public void testTextboxControlMinMaxValidation() throws Exception {
    //Basic
    MockHttpServletRequest correctValueRequest = new MockHttpServletRequest();
    correctValueRequest.addParameter("myTextBox", "i love me");

    TextControl tc = new TextControl();
    tc._getComponent().init(new MockEnviroment());

    tc.setMinLength(new Long(5));
    tc.setMaxLength(new Long(20));

    MockUiLibUtil.emulateHandleRequest(tc, "myTextBox", correctValueRequest);
    tc.convertAndValidate();

    assertTrue("Textbox control must be valid.", tc.isValid());
    assertTrue("Textbox control value must be 'i love me'.", ((String) tc.getRawValue()).equals("i love me"));

    //Too short

    MockHttpServletRequest tooShortValueRequest = new MockHttpServletRequest();
    tooShortValueRequest.addParameter("myTextBox", "boo");

    MockUiLibUtil.emulateHandleRequest(tc, "myTextBox", tooShortValueRequest);
    tc.convertAndValidate();

    assertTrue("Textbox control mustn't be valid.", !tc.isValid());

    //Too long

    MockHttpServletRequest tooLongValueRequest = new MockHttpServletRequest();
    tooLongValueRequest.addParameter("myTextBox", "i love myself and others very very much");

    MockUiLibUtil.emulateHandleRequest(tc, "myTextBox", tooLongValueRequest);
    tc.convertAndValidate();

    assertTrue("Textbox control mustn't be valid.", !tc.isValid());

    //min=max correct

    tc.setMinLength(new Long(10));
    tc.setMaxLength(new Long(10));

    correctValueRequest = new MockHttpServletRequest();
    correctValueRequest.addParameter("myTextBox", "1234567890");

    MockUiLibUtil.emulateHandleRequest(tc, "myTextBox", correctValueRequest);
    tc.convertAndValidate();

    assertTrue("Textbox control must be valid.", tc.isValid());
    assertTrue("Textbox control value must be '1234567890'.", ((String) tc.getRawValue()).equals("1234567890"));

    //min=max too short

    tooShortValueRequest.addParameter("myTextBox", "123456789");

    MockUiLibUtil.emulateHandleRequest(tc, "myTextBox", tooShortValueRequest);
    tc.convertAndValidate();

    assertTrue("Textbox control mustn't be valid.", !tc.isValid());

    //min=max too long

    tooShortValueRequest.addParameter("myTextBox", "12345678901");

    MockUiLibUtil.emulateHandleRequest(tc, "myTextBox", tooShortValueRequest);
    tc.convertAndValidate();

    assertTrue("Textbox control mustn't be valid.", !tc.isValid());

    tc._getComponent().destroy();
}