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

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

Introduction

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

Prototype

public void setContent(@Nullable byte[] content) 

Source Link

Document

Set the content of the request body as a byte array.

Usage

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

@Test
public void shouldEditAProgram() throws Exception {

    final String editedName = "Malaria Program Edited";
    String json = "{ \"name\":\"" + editedName + "\" }";
    MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
    req.setContent(json.getBytes());
    handle(req);//from  www . jav  a 2s  .  co m

    Program editedProgram = service.getProgramByUuid(getUuid());
    Assert.assertNotNull(editedProgram);
    Assert.assertEquals(editedName, editedProgram.getName());
}

From source file:cc.redpen.server.api.RedPenResourceTest.java

public void testRun() throws Exception {
    MockHttpServletRequest request = constructMockRequest("POST", "/document/validate", WILDCARD);
    request.setContent("document=Foobar".getBytes());
    MockHttpServletResponse response = invoke(request);
    assertEquals("HTTP status", HttpStatus.OK.getCode(), response.getStatus());
    JSONArray errors = (JSONArray) new JSONObject(response.getContentAsString()).get("errors");
    assertEquals(0, errors.length());//from   w  ww .j  av a 2s  .  c  o m
}

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

@Test
public void createCohort_shouldCreateANewCohort() throws Exception {

    SimpleObject cohort = new SimpleObject();
    cohort.add("name", "New cohort");
    cohort.add("description", "New cohort description");
    cohort.add("memberIds", new Integer[] { 2, 6 });

    String json = new ObjectMapper().writeValueAsString(cohort);

    MockHttpServletRequest req = request(RequestMethod.POST, getURI());
    req.setContent(json.getBytes());

    SimpleObject newCohort = deserialize(handle(req));
    Util.log("Created cohort", newCohort);

    // Check existence in database
    String uuid = (String) newCohort.get("uuid");
    Assert.assertNotNull(service.getCohortByUuid(uuid));
}

From source file:cc.redpen.server.api.RedPenResourceTest.java

public void testRunWithErrors() throws Exception {
    MockHttpServletRequest request = constructMockRequest("POST", "/document/validate", WILDCARD);
    request.setContent(("document=foobar.foobar").getBytes()); //NOTE: need space between periods.
    MockHttpServletResponse response = invoke(request);

    assertEquals("HTTP status", HttpStatus.OK.getCode(), response.getStatus());
    System.out.println(response.getContentAsString());
    JSONArray errors = (JSONArray) new JSONObject(response.getContentAsString()).get("errors");
    // the following will change whenever the configuration or validator functionaliy changes
    // but it doesn't indicate what particular errors are new/missing
    // assertEquals(3, errors.length());
    assertTrue(errors.get(0).toString().length() > 0);
}

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

@Test
public void shouldEditingAConceptReferenceTerm() throws Exception {
    final String newCode = "updated code";
    SimpleObject conceptReferenceTermType = new SimpleObject();
    conceptReferenceTermType.add("code", newCode);

    String json = new ObjectMapper().writeValueAsString(conceptReferenceTermType);

    MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
    req.setContent(json.getBytes());
    handle(req);//from w  w w  .  ja  v a2s .c om
    assertEquals(newCode, service.getConceptReferenceTermByUuid(getUuid()).getCode());
}

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

@Test
public void shouldEditAnOrderType() throws Exception {
    final String newName = "Updated name";
    SimpleObject conceptMapTypeType = new SimpleObject();
    conceptMapTypeType.add("name", newName);

    String json = new ObjectMapper().writeValueAsString(conceptMapTypeType);

    MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
    req.setContent(json.getBytes());
    handle(req);/*from  ww w.j a  v  a  2s  .  com*/
    assertEquals(newName, service.getOrderTypeByUuid(getUuid()).getName());
}

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

@Test
public void shouldEditAProblem() throws Exception {
    final int id = 2;
    final ProblemModifier newModifier = ProblemModifier.HISTORY_OF;
    Problem problem = patientService.getProblem(id);
    assertEquals(false, newModifier.equals(problem.getModifier()));
    SimpleObject p = new SimpleObject();
    p.add("modifier", newModifier);
    String json = new ObjectMapper().writeValueAsString(p);
    MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + problem.getUuid());
    req.setContent(json.getBytes());/* w  ww .  j  av a  2  s  . c  om*/
    handle(req);

    problem = patientService.getProblem(id);
    assertEquals(newModifier, problem.getModifier());
}

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

@Test
public void shouldEditAPatientIdentifier() throws Exception {
    final String newLocationUuid = RestTestConstants1_8.LOCATION_UUID;
    PatientIdentifier patientIdentifierType = service.getPatientIdentifierByUuid(getUuid());
    assertFalse(newLocationUuid.equals(patientIdentifierType.getLocation().getUuid()));
    SimpleObject patientIdentifier = new SimpleObject();
    patientIdentifier.add("location", newLocationUuid);

    String json = new ObjectMapper().writeValueAsString(patientIdentifier);

    MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
    req.setContent(json.getBytes());//from w w w .  j  a  v a2 s . com
    handle(req);
    assertEquals(newLocationUuid, patientIdentifierType.getLocation().getUuid());
}

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

@Test(expected = ConversionException.class)
public void updateCohort_shouldFailToOverwriteMemberIdsOnACohort() throws Exception {
    Assert.assertEquals(3, service.getCohortByUuid(getUuid()).getMemberIds().size());

    SimpleObject attributes = new SimpleObject();
    attributes.add("memberIds", new Integer[] { 2, 6 });
    String json = new ObjectMapper().writeValueAsString(attributes);

    MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
    req.setContent(json.getBytes());
    handle(req);/*  w w  w .j  a  v  a2s .  c  o  m*/

}

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

@Test
public void updateCohort_shouldChangeAPropertyOnACohort() throws Exception {

    SimpleObject attributes = new SimpleObject();
    attributes.add("name", "Updated cohort name");

    String json = new ObjectMapper().writeValueAsString(attributes);

    MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
    req.setContent(json.getBytes());
    handle(req);/*www. j av a 2  s .co m*/

    Cohort editedCohort = service.getCohortByUuid(getUuid());
    Assert.assertEquals("Updated cohort name", editedCohort.getName());
}