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.ConceptController1_8Test.java

@Test
public void shouldCreateAConcept() throws Exception {
    int originalCount = service.getAllConcepts().size();
    String json = "{ \"names\": [{\"name\":\"test concept\", \"locale\":\"en\", \"conceptNameType\":\""
            + ConceptNameType.FULLY_SPECIFIED
            + "\"}], \"datatype\":\"8d4a4c94-c2cc-11de-8d13-0010c6dffd0f\", \"conceptClass\":\"Diagnosis\" }";

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

    Object newConcept = deserialize(handle(req));
    Assert.assertNotNull(PropertyUtils.getProperty(newConcept, "uuid"));
    Assert.assertEquals(originalCount + 1, service.getAllConcepts().size());
}

From source file:org.opennms.netmgt.ncs.rest.AbstractSpringJerseyRestTestCase.java

protected void 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 {/*from  ww  w  .j  a  v a 2  s .  c o m*/
        request.setContent(data.getBytes());
    }

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

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

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

@Test
public void shouldEditFullySpecifiedNameOfAConcept() throws Exception {
    final String changedName = "TESTING NAME";
    String json = "{ \"name\":\"" + changedName + "\" }";
    MockHttpServletRequest req = request(RequestMethod.POST,
            getURI() + "/f923524a-b90c-4870-a948-4125638606fd");
    req.setContent(json.getBytes());
    handle(req);/*from  ww w .j  av  a  2  s.  com*/
    Concept updated = service.getConceptByUuid("f923524a-b90c-4870-a948-4125638606fd");
    Assert.assertNotNull(updated);
    Assert.assertEquals(changedName, updated.getFullySpecifiedName(Context.getLocale()).getName());
}

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

@Test
public void shouldEditAConcept() throws Exception {
    final String changedVersion = "1.2.3";
    String json = "{ \"version\":\"" + changedVersion + "\" }";
    MockHttpServletRequest req = request(RequestMethod.POST,
            getURI() + "/f923524a-b90c-4870-a948-4125638606fd");
    req.setContent(json.getBytes());
    handle(req);/* ww w  .  jav a 2s .c  o m*/
    Concept updated = service.getConceptByUuid("f923524a-b90c-4870-a948-4125638606fd");
    Assert.assertNotNull(updated);
    Assert.assertEquals(changedVersion, updated.getVersion());
}

From source file:fr.paris.lutece.portal.web.upload.UploadServletTest.java

private MockHttpServletRequest getMultipartRequest() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    byte[] fileContent = new byte[] { 1, 2, 3 };
    Part[] parts = new Part[] { new FilePart("file1", new ByteArrayPartSource("file1", fileContent)) };
    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts,
            new PostMethod().getParams());
    // Serialize request body
    ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
    multipartRequestEntity.writeRequest(requestContent);
    // Set request body to HTTP servlet request
    request.setContent(requestContent.toByteArray());
    // Set content type to HTTP servlet request (important, includes Mime boundary string)
    request.setContentType(multipartRequestEntity.getContentType());
    request.setMethod("POST");
    return request;
}

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

@Test
public void shouldRemoveAnswersFromConcept() throws Exception {
    String conceptWithAnswersUuid = "95312123-e0c2-466d-b6b1-cb6e990d0d65";
    String existingAnswerUuid = "b055abd8-a420-4a11-8b98-02ee170a7b54";
    String newAnswerUuid = "32d3611a-6699-4d52-823f-b4b788bac3e3";

    // sanity check to make sure this concept has the existing answer and not the other
    Concept concept = Context.getConceptService().getConceptByUuid(conceptWithAnswersUuid);
    Assert.assertTrue(hasAnswer(concept, service.getConceptByUuid(existingAnswerUuid)));
    Assert.assertFalse(hasAnswer(concept, service.getConceptByUuid(newAnswerUuid)));
    Assert.assertEquals(3, concept.getAnswers().size());

    MockHttpServletRequest request = request(RequestMethod.POST, getURI() + "/" + conceptWithAnswersUuid);
    String json = "{ \"answers\": [\"" + existingAnswerUuid + "\", \"" + newAnswerUuid + "\"] }";
    request.setContent(json.getBytes());

    handle(request);//from w w w . j av a 2 s  .  com

    // get the object again so we have the new answers (will this bork hibernate because we fetched it earlier?)
    concept = Context.getConceptService().getConceptByUuid(conceptWithAnswersUuid);
    Concept answer1 = service.getConceptByUuid(existingAnswerUuid);
    Concept answer2 = service.getConceptByUuid(newAnswerUuid);
    Assert.assertTrue(hasAnswer(concept, answer1));
    Assert.assertTrue(hasAnswer(concept, answer2));
    Assert.assertEquals(2, concept.getAnswers().size());
}

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

@Test
public void shouldAddAnswersToConcept() throws Exception {
    MockHttpServletRequest request = request(RequestMethod.POST, getURI() + "/" + getUuid());
    String json = "{ \"answers\": [\"0dde1358-7fcf-4341-a330-f119241a46e8\", \"54d2dce5-0357-4253-a91a-85ce519137f5\", \"05ec820a-d297-44e3-be6e-698531d9dd3f\"] }";
    request.setContent(json.getBytes());

    handle(request);//from w w w .  ja v a  2 s  .c  om

    Concept concept = service.getConceptByUuid(getUuid());
    Concept answer1 = service.getConceptByUuid("0dde1358-7fcf-4341-a330-f119241a46e8");
    Concept answer2 = service.getConceptByUuid("54d2dce5-0357-4253-a91a-85ce519137f5");
    Drug drug = Context.getConceptService().getDrugByUuid("05ec820a-d297-44e3-be6e-698531d9dd3f");
    Assert.assertTrue(hasAnswer(concept, answer1));
    Assert.assertTrue(hasAnswer(concept, answer2));
    Assert.assertTrue(hasAnswer(concept, drug));
}

From source file:com.baidu.jprotobuf.rpc.client.ProxyFactoryBeanTestBase.java

protected HttpServer createServer() throws Exception {

    servlet.init();//from   w w w.j a  va  2 s. c  o  m

    HttpServerProvider provider = HttpServerProvider.provider();
    HttpServer httpserver = provider.createHttpServer(new InetSocketAddress(8080), 10);

    httpserver.createContext(getPathInfo(), new HttpHandler() {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {

            MockHttpServletRequest request = new MockHttpServletRequest();
            request.setPathInfo(getPathInfo());

            String queryString = httpExchange.getRequestURI().getRawQuery();

            if (queryString != null) {
                if (queryString.indexOf(ServiceExporter.INPUT_IDL_PARAMETER) != -1) {
                    request.addParameter(ServiceExporter.INPUT_IDL_PARAMETER, "");
                }
                if (queryString.indexOf(ServiceExporter.OUTPUT_IDL_PARAMETER) != -1) {
                    request.addParameter(ServiceExporter.OUTPUT_IDL_PARAMETER, "");
                }
            }

            request.setQueryString(queryString);
            InputStream requestBody = httpExchange.getRequestBody();
            request.setContent(IOUtils.toByteArray(requestBody));

            MockHttpServletResponse response = new MockHttpServletResponse();
            response.setOutputStreamAccessAllowed(true);

            try {
                servlet.service(request, response);
            } catch (ServletException e) {
                e.printStackTrace();
            }
            httpExchange.sendResponseHeaders(200, response.getContentLength());
            OutputStream out = httpExchange.getResponseBody(); // ?
            out.write(response.getContentAsByteArray());
            out.flush();
            httpExchange.close();
        }
    });
    httpserver.setExecutor(null);
    httpserver.start();

    return httpserver;
}