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.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_8.UserController1_8Test.java

/**
 * @see UserController#findUsers(String,WebRequest,HttpServletResponse)
 * @throws Exception //  w w  w.  j a v  a  2 s  . c  o  m
 * @verifies return no results if there are no matching users
 */
@Test
public void findUsers_shouldReturnNoResultsIfThereAreNoMatchingUsers() throws Exception {

    MockHttpServletRequest req = request(RequestMethod.GET, getURI());
    req.addParameter("q", "foo-bar-baz");

    SimpleObject result = deserialize(handle(req));
    Assert.assertNotNull(result);

    List<User> hits = (List<User>) result.get("results");
    Assert.assertEquals(0, hits.size());
}

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

public MockHttpServletRequest newRequest(RequestMethod method, String requestURI,
        MainResourceControllerTest.Parameter... parameters) {
    MockHttpServletRequest request = request(method, requestURI);
    for (MainResourceControllerTest.Parameter parameter : parameters) {
        request.addParameter(parameter.name, parameter.value);
    }/*  w  ww  . j  av  a2s .  c o  m*/
    return request;
}

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

/**
 * @see OptionsFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 * @verifies accept 2 characters as username
 *///from  w  ww  .  j a v  a 2  s  . c  o m
@Test
public void onSubmit_shouldAccept2CharactersAsUsername() throws Exception {
    //given
    MockHttpServletRequest post = testHelper.newPOST("/options.form");
    post.addParameter("username", "ab");

    //when
    testHelper.handle(post);

    //then
    Assert.assertThat("ab", is(Context.getAuthenticatedUser().getUsername()));
}

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

/**
 * @see OptionsFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 * @verifies reject 1 character as username
 *///from ww w.  j a  va  2s  . c  om
@Test
public void onSubmit_shouldReject1CharacterAsUsername() throws Exception {
    //given
    MockHttpServletRequest post = testHelper.newPOST("/options.form");
    post.addParameter("username", "a");

    //when
    testHelper.handle(post);

    //then
    Assert.assertThat("a", is(not(Context.getAuthenticatedUser().getUsername())));
}

From source file:nl.surfnet.coin.teams.control.EditTeamControllerTest.java

@Test
public void testEditTeamHappyFlow() throws Exception {

    ListAppender auditAppender = getAuditLogAppender();
    auditAppender.list.clear();//  w  ww.  j a va 2  s. c  om

    MockHttpServletRequest request = getRequest();
    String token = TokenUtil.generateSessionToken();
    // Add the teamId, team name, description & token
    request.addParameter("teamName", "Another name");
    request.addParameter("team", "team-1");
    request.addParameter("description", "description");
    request.addParameter("token", token);

    GrouperTeamService grouperTeamService = mock(GrouperTeamService.class);
    when(grouperTeamService.findTeamById("team-1")).thenReturn(mockTeam);
    when(grouperTeamService.findMember("team-1", "member-1")).thenReturn(mockAdminMember);
    autoWireMock(editTeamController, grouperTeamService, GrouperTeamService.class);
    autoWireMock(editTeamController, new Returns(true), ControllerUtil.class);
    autoWireRemainingResources(editTeamController);

    RedirectView result = editTeamController.editTeam(getModelMap(), request, token, token,
            new SimpleSessionStatus());

    assertEquals("detailteam.shtml?team=team-1&view=app", result.getUrl());

    assertEquals("An audit event should be appended to audit log", 1, auditAppender.list.size());
    LoggingEvent auditEvent = (LoggingEvent) auditAppender.list.get(0);
    assertTrue("Audit event should contain old team name", auditEvent.getFormattedMessage().contains("Team 1"));
}

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

/**
 * @see UserController#findUsers(String,WebRequest,HttpServletResponse)
 * @throws Exception /*from   w w w. j  a  v  a2  s  .co  m*/
 * @verifies find matching users
 */
@Test
public void findUsers_shouldFindMatchingUsers() throws Exception {

    MockHttpServletRequest req = request(RequestMethod.GET, getURI());
    req.addParameter("q", "but");

    SimpleObject result = deserialize(handle(req));
    Assert.assertNotNull(result);

    List<Object> hits = (List<Object>) result.get("results");
    Assert.assertEquals(1, hits.size());

    Util.log("Found " + hits.size() + " user(s)", result);
    Assert.assertEquals(service.getUserByUuid(getUuid()).getUuid(),
            PropertyUtils.getProperty(hits.get(0), "uuid"));
}

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

/**
 * @see OptionsFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 * @verifies accept email address as username if enabled
 *//*from w  w  w . j a v a2  s  . c  o m*/
@Test
public void onSubmit_shouldAcceptEmailAddressAsUsernameIfEnabled() throws Exception {
    //given
    Context.getAdministrationService().saveGlobalProperty(
            new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "true"));
    MockHttpServletRequest post = testHelper.newPOST("/options.form");
    post.addParameter("username", "ab@gmail.com");

    //when
    testHelper.handle(post);

    //then
    Assert.assertThat("ab@gmail.com", is(Context.getAuthenticatedUser().getUsername()));
}

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

/**
 * @see OptionsFormController#onSubmit(HttpServletRequest,HttpServletResponse,Object,BindException)
 * @verifies reject invalid email address as username if enabled
 *///from ww w.  j  a v  a 2s  .  c  o  m
@Test
public void onSubmit_shouldRejectInvalidEmailAddressAsUsernameIfEnabled() throws Exception {
    //given
    Context.getAdministrationService().saveGlobalProperty(
            new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "true"));
    MockHttpServletRequest post = testHelper.newPOST("/options.form");
    post.addParameter("username", "ab@");

    //when
    testHelper.handle(post);

    //then
    Assert.assertThat("ab@", is(not(Context.getAuthenticatedUser().getUsername())));
}

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

/**
 * @see ObsController#getObs(String,WebRequest)
 * @verifies get a full representation of a obs
 *//*from ww  w.j a  va2s.com*/
@Test
public void getObs_shouldGetAFullRepresentationOfAObs() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL);
    Object result = new ObsController().retrieve("39fb7f47-e80a-4056-9285-bd798be13c63", req);
    Assert.assertNotNull(result);
    Util.log("Obs fetched (default)", result);
    Assert.assertEquals("39fb7f47-e80a-4056-9285-bd798be13c63", PropertyUtils.getProperty(result, "uuid"));
    Assert.assertNotNull(PropertyUtils.getProperty(result, "links"));
    Assert.assertNotNull(PropertyUtils.getProperty(result, "person"));
    Assert.assertNotNull(PropertyUtils.getProperty(result, "concept"));
    Assert.assertNotNull(PropertyUtils.getProperty(result, "auditInfo"));
}

From source file:nl.surfnet.coin.teams.control.EditTeamControllerTest.java

@Test(expected = SecurityException.class)
public void testEditTeamWrongToken() throws Exception {
    MockHttpServletRequest request = getRequest();
    String token = TokenUtil.generateSessionToken();

    // do NOT add the team id, but do add the team name, team description & token
    request.addParameter("team", "Team 1");
    request.addParameter("description", "description");
    request.addParameter("token", token);

    autoWireMock(editTeamController, new Returns(mockTeam), GrouperTeamService.class);
    autoWireMock(editTeamController, new Returns(true), ControllerUtil.class);
    autoWireRemainingResources(editTeamController);

    editTeamController.editTeam(getModelMap(), request, token, "asfjkhsdjkfhsd", new SimpleSessionStatus());
}