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_9.LocationController1_9Test.java

@Test
public void shouldUnretireALocation() throws Exception {

    Location location = service.getLocation(3);
    Assert.assertTrue(location.isRetired());

    String json = "{ \"retired\": false }";
    MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid());
    req.setContent(json.getBytes());
    handle(req);//  ww w.j a v  a 2s . c  om
    Location updatedLocation = service.getLocationByUuid(getUuid());
    Assert.assertTrue(!updatedLocation.isRetired());

}

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

@Test
public void shouldUnsetOtherPreferredIdentifiers() throws Exception {
    PatientIdentifier exisitingIdentifier = service.getPatientIdentifierByUuid(getUuid());
    long originalCount = getAllCount();
    assertTrue(exisitingIdentifier.isPreferred());
    SimpleObject patientIdentifier = new SimpleObject();
    patientIdentifier.add("identifier", "abc123ez");
    patientIdentifier.add("identifierType", "2f470aa8-1d73-43b7-81b5-01f0c0dfa53c");
    patientIdentifier.add("location", RestTestConstants1_8.LOCATION_UUID);
    patientIdentifier.add("preferred", true);

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

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

    SimpleObject newPatientIdentifier = deserialize(handle(req));
    Object uuid = PropertyUtils.getProperty(newPatientIdentifier, "uuid");
    assertNotNull(uuid);// ww  w  .ja va  2  s  . com
    assertEquals(originalCount + 1, getAllCount());

    PatientIdentifier newIdentifer = service.getPatientIdentifierByUuid(uuid.toString());
    assertFalse(exisitingIdentifier.isPreferred());
    assertTrue(newIdentifer.isPreferred());
    assertEquals(newIdentifer.getIdentifier(), "abc123ez");
}

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

@Test
public void shouldAddNameToPerson() throws Exception {
    long originalCount = getAllCount();

    SimpleObject personName = new SimpleObject();
    personName.add("givenName", "name1");

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

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

    SimpleObject newPersonName = deserialize(handle(req));

    Assert.assertNotNull(PropertyUtils.getProperty(newPersonName, "uuid"));
    Assert.assertEquals(originalCount + 1, getAllCount());
}

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

@Test
public void shouldCreateANonCodedDiagnosis() throws Exception {
    long originalCount = getAllCount();

    SimpleObject codedOrFreeText = new SimpleObject();

    codedOrFreeText.add("nonCoded", "Some condition");

    SimpleObject diagnosisSource = new SimpleObject();

    diagnosisSource.add("condition", condition.getUuid());
    diagnosisSource.add("certainty", CONFIRMED);
    diagnosisSource.add("diagnosis", codedOrFreeText);
    diagnosisSource.add("rank", 1);
    diagnosisSource.add("encounter", encounter.getUuid());
    diagnosisSource.add("voided", false);
    diagnosisSource.add("patient", patient.getUuid());

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

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

    SimpleObject newDiagnosisSource = deserialize(handle(req));

    Assert.assertNotNull(PropertyUtils.getProperty(newDiagnosisSource, "uuid"));

    Diagnosis diagnosis = diagnosisService.getDiagnosisByUuid(newDiagnosisSource.get("uuid").toString());

    LinkedHashMap condition = newDiagnosisSource.get("condition");

    LinkedHashMap nonCoded = newDiagnosisSource.get("diagnosis");

    LinkedHashMap patient = newDiagnosisSource.get("patient");

    Assert.assertNotNull(diagnosis.getEncounter().toString());
    Assert.assertEquals(diagnosis.getCondition().getUuid(), condition.get("uuid"));
    Assert.assertEquals((diagnosis.getCertainty().toString()), newDiagnosisSource.get("certainty"));
    Assert.assertEquals(diagnosis.getRank(), newDiagnosisSource.get("rank"));
    Assert.assertEquals(diagnosis.getEncounter().getUuid(), encounter.getUuid());
    Assert.assertNotNull(newDiagnosisSource.get("encounter"));
    Assert.assertEquals(diagnosis.getVoided(), newDiagnosisSource.get("voided"));
    Assert.assertEquals(diagnosis.getDiagnosis().getNonCoded(), nonCoded.get("nonCoded"));
    Assert.assertEquals(patient.get("uuid"), diagnosis.getPatient().getUuid());
    Assert.assertEquals(originalCount + 1, getAllCount());
}

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

@Test
public void shouldCreateACodedDiagnosis() throws Exception {
    long originalCount = getAllCount();

    SimpleObject codedOrFreeText = new SimpleObject();

    codedOrFreeText.add("coded", concept.getUuid());
    codedOrFreeText.add("specificName", conceptName.getUuid());

    SimpleObject diagnosisSource = new SimpleObject();

    diagnosisSource.add("condition", condition.getUuid());
    diagnosisSource.add("certainty", CONFIRMED);
    diagnosisSource.add("diagnosis", codedOrFreeText);
    diagnosisSource.add("rank", 1);
    diagnosisSource.add("encounter", encounter.getUuid());
    diagnosisSource.add("voided", false);
    diagnosisSource.add("patient", patient.getUuid());

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

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

    SimpleObject newDiagnosisSource = deserialize(handle(req));

    Assert.assertNotNull(PropertyUtils.getProperty(newDiagnosisSource, "uuid"));

    Diagnosis diagnosis = diagnosisService.getDiagnosisByUuid(newDiagnosisSource.get("uuid").toString());

    LinkedHashMap condition = newDiagnosisSource.get("condition");

    LinkedHashMap codedOrText = newDiagnosisSource.get("diagnosis");

    LinkedHashMap conceptName = (LinkedHashMap) codedOrText.get("specificName");

    LinkedHashMap concept = (LinkedHashMap) codedOrText.get("coded");

    LinkedHashMap patient = newDiagnosisSource.get("patient");

    Assert.assertNotNull(diagnosis.getEncounter().toString());
    Assert.assertEquals(diagnosis.getCondition().getUuid(), condition.get("uuid"));
    Assert.assertEquals((diagnosis.getCertainty().toString()), newDiagnosisSource.get("certainty"));
    Assert.assertEquals(diagnosis.getRank(), newDiagnosisSource.get("rank"));
    Assert.assertEquals(diagnosis.getEncounter().getUuid(), encounter.getUuid());
    Assert.assertNotNull(newDiagnosisSource.get("encounter"));
    Assert.assertEquals(diagnosis.getVoided(), newDiagnosisSource.get("voided"));
    Assert.assertEquals(diagnosis.getDiagnosis().getCoded().getUuid(), concept.get("uuid"));
    Assert.assertEquals(diagnosis.getDiagnosis().getSpecificName().getUuid(), conceptName.get("uuid"));
    Assert.assertEquals(patient.get("uuid"), diagnosis.getPatient().getUuid());
    Assert.assertEquals(originalCount + 1, getAllCount());
}

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

@Test
public void shouldUnVoidDiagnosis() throws Exception {

    Diagnosis diagnosis = diagnosisService.getDiagnosisByUuid(RestTestConstants2_2.VOIDED_DIAGNOSIS_UUID);

    Assert.assertTrue(diagnosis.isVoided());

    SimpleObject attributes = new SimpleObject();
    attributes.add("voided", false);

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

    MockHttpServletRequest req = request(RequestMethod.POST,
            getURI() + "/" + RestTestConstants2_2.VOIDED_DIAGNOSIS_UUID);
    req.setContent(json.getBytes());
    handle(req);// ww  w.  j a v a2 s. c  o  m

    diagnosis = diagnosisService.getDiagnosisByUuid(RestTestConstants2_2.VOIDED_DIAGNOSIS_UUID);

    Assert.assertFalse(diagnosis.isVoided());
    Assert.assertNull(diagnosis.getDateVoided());
    Assert.assertNull(diagnosis.getVoidedBy());
    Assert.assertNull(diagnosis.getVoidReason());

}

From source file:org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_10.ProgramResource1_10Test.java

@Test
public void shouldCreateAProgram() throws Exception {
    long originalCount = getAllCount();

    SimpleObject program = new SimpleObject();
    program.add("name", "Program name");
    program.add("description", "Program description");
    program.add("concept", RestTestConstants1_8.CONCEPT_UUID);
    program.add("outcomesConcept", RestTestConstants1_8.CONCEPT2_UUID);

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

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

    SimpleObject newProgram = deserialize(handle(req));

    Assert.assertNotNull(PropertyUtils.getProperty(newProgram, "uuid"));
    Assert.assertEquals(RestTestConstants1_8.CONCEPT2_UUID,
            ((Map) PropertyUtils.getProperty(newProgram, "outcomesConcept")).get("uuid"));
    Assert.assertEquals(originalCount + 1, getAllCount());
}

From source file:org.opennms.core.test.rest.AbstractSpringJerseyRestTestCase.java

/**
 * @param requestType// w  w  w. j  a va2 s .c  o m
 * @param contentType
 * @param url
 * @param data
 * @param statusCode
 */
protected MockHttpServletResponse sendData(String requestType, String contentType, String url, String data,
        int statusCode) throws Exception {
    MockHttpServletRequest request = createRequest(servletContext, requestType, url, getUser(), getUserRoles());
    request.setContentType(contentType);

    if (contentType.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
        request.setParameters(parseParamData(data));
        request.setContent(new byte[] {});
    } else {
        request.setContent(data.getBytes());
    }

    final MockHttpServletResponse response = createResponse();
    dispatch(request, response);

    LOG.debug("Received response: {}", stringifyResponse(response));
    assertEquals(response.getErrorMessage(), statusCode, response.getStatus());

    return response;
}

From source file:org.opennms.core.test.rest.AbstractSpringJerseyRestTestCase.java

protected void putXmlObject(final JAXBContext context, final String url, final int expectedStatus,
        final Object object) throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(object, out);/*from  w w  w  .  jav  a  2s  .  com*/
    final byte[] content = out.toByteArray();

    final MockHttpServletRequest request = createRequest(servletContext, PUT, url, getUser(), getUserRoles());
    request.setContentType(MediaType.APPLICATION_XML);
    request.setContent(content);
    final MockHttpServletResponse response = createResponse();
    dispatch(request, response);
    assertEquals(expectedStatus, response.getStatus());
}

From source file:org.opennms.web.rest.AbstractSpringJerseyRestTestCase.java

/**
 * @param requestType//  w w w .jav  a  2s . co  m
 * @param contentType
 * @param url
 * @param data
 * @param statusCode
 */
protected MockHttpServletResponse sendData(String requestType, String contentType, String url, String data,
        int statusCode) throws Exception {
    MockHttpServletRequest request = createRequest(requestType, url);
    request.setContentType(contentType);

    if (contentType.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
        request.setParameters(parseParamData(data));
        request.setContent(new byte[] {});
    } else {
        request.setContent(data.getBytes());
    }

    final MockHttpServletResponse response = createResponse();
    dispatch(request, response);

    LOG.debug("Received response: {}", stringifyResponse(response));
    assertEquals(response.getErrorMessage(), statusCode, response.getStatus());

    return response;
}