Example usage for org.springframework.mock.web MockHttpServletResponse getStatus

List of usage examples for org.springframework.mock.web MockHttpServletResponse getStatus

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse getStatus.

Prototype

@Override
    public int getStatus() 

Source Link

Usage

From source file:org.apache.hadoop.chukwa.datacollection.agent.rest.TestAdaptorController.java

private String assertJSONResponse(MockHttpServletResponse response, int adaptorCount)
        throws UnsupportedEncodingException {
    String responseContent = response.getContentAsString();
    assertEquals("Unexpected response status", 200, response.getStatus());

    JSONObject json = (JSONObject) JSONValue.parse(responseContent);
    String adaptorClass = (String) json.get("adaptorClass");
    String dataType = (String) json.get("dataType");
    assertEquals("Response text doesn't include adaptor class",
            "org.apache.hadoop.chukwa.datacollection.adaptor.ChukwaTestAdaptor", adaptorClass);
    assertEquals("Response text doesn't include data type", "SomeDataType", dataType);

    return responseContent;
}

From source file:org.apache.hadoop.chukwa.datacollection.agent.rest.TestAdaptorController.java

private String assertXmlResponse(MockHttpServletResponse response, int adaptorCount)
        throws UnsupportedEncodingException {
    String responseContent = response.getContentAsString();

    // assert response
    assertEquals("Unexpected response status", 200, response.getStatus());

    //Content it correct when executed via an HTTP client, but it doesn't seem
    //to get set by the servlet
    assertOccurs("Response XML doesn't include correct adaptor_count", adaptorCount, responseContent,
            "adaptorCount>");
    assertOccurs("Response XML doesn't include adaptorClass", adaptorCount, responseContent,
            "<adaptorClass>org.apache.hadoop.chukwa.datacollection.adaptor.ChukwaTestAdaptor</adaptorClass>");
    assertOccurs("Response XML doesn't include dataType", adaptorCount, responseContent,
            "<dataType>SomeDataType</dataType>");

    return responseContent;
}

From source file:org.cloudfoundry.identity.uaa.scim.endpoints.ScimGroupEndpointsTests.java

private void validateView(View view, HttpStatus status) {
    MockHttpServletResponse response = new MockHttpServletResponse();
    try {/*w  w w  .  j  a  v a 2  s .  c  o  m*/
        view.render(new HashMap<String, Object>(), new MockHttpServletRequest(), response);
        assertNotNull(response.getContentAsString());
    } catch (Exception e) {
        fail("view should render correct status and body");
    }
    assertEquals(status.value(), response.getStatus());
}

From source file:org.cloudifysource.rest.AttributesContollerTest.java

private void testRequest(final MockHttpServletRequest reqeust, final HandlerMethod expectedHandlerMethod,
        final String expectedResponseContent) throws Exception {

    final MockHttpServletResponse response = new MockHttpServletResponse();

    final Object handler = getHandlerToRequest(reqeust);
    Assert.assertEquals("Wrong handler selected for request uri: " + reqeust.getRequestURI(),
            expectedHandlerMethod.toString(), handler.toString());

    // handle the request
    handlerAdapter.handle(reqeust, response, handler);

    // validate the response
    Assert.assertTrue("Wrong response status: " + response.getStatus(),
            response.getStatus() == HttpStatus.OK.value());
    Assert.assertEquals("Wrong content type in response: " + response.getContentType(), JSON_CONTENT_TYPE,
            response.getContentType());/*from  w  w w .  j  a  va 2s.c o  m*/
    Assert.assertEquals("Wrong response content: " + response.getContentAsString(), expectedResponseContent,
            response.getContentAsString());
}

From source file:org.cloudifysource.rest.ControllerTest.java

private MockHttpServletResponse testRequest(final MockHttpServletRequest request,
        final HandlerMethod expectedHandlerMethod) throws Exception {
    final MockHttpServletResponse response = new MockHttpServletResponse();

    final HandlerExecutionChain handlerExecutionChain = getHandlerToRequest(request);
    Object handler = handlerExecutionChain.getHandler();
    Assert.assertEquals("Wrong handler selected for request uri: " + request.getRequestURI(),
            expectedHandlerMethod.toString(), handler.toString());

    HandlerInterceptor[] interceptors = handlerExecutionChain.getInterceptors();
    // pre handle
    for (HandlerInterceptor handlerInterceptor : interceptors) {
        handlerInterceptor.preHandle(request, response, handler);
    }//  w  ww .jav a 2s  .  c  om
    // handle the request
    ModelAndView modelAndView = handlerAdapter.handle(request, response, handler);
    // post handle
    for (HandlerInterceptor handlerInterceptor : interceptors) {
        handlerInterceptor.postHandle(request, response, handler, modelAndView);
    }

    // validate the response
    Assert.assertTrue("Wrong response status: " + response.getStatus(),
            response.getStatus() == HttpStatus.OK.value());
    Assert.assertTrue(response.getContentType().contains(MediaType.APPLICATION_JSON));

    return response;
}

From source file:org.dataconservancy.deposit.status.SimpleStatusServletTest.java

private void doBasicRetrieval(String depositid, String mgrid) throws Exception {
    final String BASEURL = "http://example.org/webapp/depositServlet";
    final String MGR_ID_1 = "id_1";

    final String STATUS_CONTENT = "status content";
    final String STATUS_MIME = "text/foo";

    DepositDocumentServlet serv = new DepositStatusServlet();
    serv.setBaseURL(BASEURL);/*  w w  w  . j a v a  2s .c  om*/

    List<StatusTestDepositManager> mgrs = getTestManagers(MGR_ID_1, mgrid);
    serv.setDepositManagers(mgrs);

    MockDepositInfo status = new MockDepositInfo(depositid, mgrid);
    MockDepositDocument statusDoc = new MockDepositDocument();
    statusDoc.setInputStream(IOUtils.toInputStream(STATUS_CONTENT));
    statusDoc.setMimeType(STATUS_MIME);
    status.setDepositStatus(statusDoc);

    mgrs.get(1).setDepositStatus(depositid, status);

    String url = serv.getURL(status);

    MockHttpServletRequest request = new MockHttpServletRequest("GET", url);
    request.setPathInfo(url.replace(BASEURL, ""));

    MockHttpServletResponse response = new MockHttpServletResponse();

    serv.doGet(request, response);

    assertEquals("Wrong response http code", HttpServletResponse.SC_OK, response.getStatus());
    assertEquals("Wrong response mime type", response.getContentType(), STATUS_MIME);
    assertEquals("Did not return correct content", STATUS_CONTENT, response.getContentAsString());
}

From source file:org.dataconservancy.ui.api.FileControllerTest.java

/**
 * Test attempt to retrieve a good file by an admin, where a good file is one that exists and is retrievable by
 * authorized user./*from  w  w w.  j a va 2 s .  c  o m*/
 *
 * Expected: Status 200
 *           Etag header
 *           Content-Disposition header
 *           Content-Type header
 *           Content-Lenth header
 *           Last-modified header
 *           File bytestream
 *
 * @throws IOException
 */
@Test
public void testGetFileRequestByAdmin() throws IOException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();
    final int lowContentLength = 4;
    final int highContentLength = 8;

    fileController.handleFileGetRequest(null, null, null, req, res);

    //Test status code
    assertEquals(200, res.getStatus());
    //Test headers
    assertNotNull(res.getHeader(ETAG));
    assertNotNull(res.getHeader(CONTENT_DISPOSITION));
    assertNotNull(res.getContentType());
    assertNotNull(res.getHeader(LAST_MODIFIED));
    assertEquals(rfcDateFormatter(lastModifiedDate), res.getHeader(LAST_MODIFIED));

    assertTrue("Content Length out of bounds: " + res.getContentLength(),
            res.getContentLength() > lowContentLength && res.getContentLength() < highContentLength);

    byte[] originalContent = DATA_FILE_ONE_CONTENT.getBytes();
    assertEquals(new String(originalContent), new String(res.getContentAsByteArray()).trim());
}

From source file:org.dataconservancy.ui.api.FileControllerTest.java

/**
 * Test sending a get file request as a user with no permission to retrieve the file.
 * Expected return code: 403/*  w ww.  j  a va2 s.  c o m*/
 * @throws BizPolicyException
 * @throws IOException
 * @throws ArchiveServiceException
 * @throws RelationshipConstraintException
 */
@Test
public void testGetFileRequestByUnauthorizedUser()
        throws BizPolicyException, IOException, ArchiveServiceException, RelationshipConstraintException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();

    FileBizService bizService = fileController.getFileBizService();
    when(bizService.getFile(dataFileOne.getId(), admin))
            .thenThrow(new BizPolicyException("Mock exception", Type.AUTHORIZATION_ERROR));

    //run the handle request
    fileController.handleFileGetRequest(null, null, null, req, res);
    //Test status code
    assertEquals(403, res.getStatus());
}

From source file:org.dataconservancy.ui.api.FileControllerTest.java

/**
 * Test sending a request to retrieve a non existing file.
 * Expected return code: 404//  w  w w  . ja v a  2 s . c  om
 * @throws BizPolicyException
 * @throws IOException
 * @throws ArchiveServiceException
 * @throws RelationshipConstraintException
 */
@Test
public void testGetFileRequestNonExistingFile()
        throws BizPolicyException, IOException, ArchiveServiceException, RelationshipConstraintException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();

    FileBizService bizService = fileController.getFileBizService();
    when(bizService.getFile(dataFileOne.getId(), admin)).thenReturn(null);

    MetadataFileBizService metaBizService = fileController.getMetadataFileBizService();
    when(metaBizService.retrieveMetadataFile(dataItemOne.getId())).thenReturn(null);

    //run the handle request
    fileController.handleFileGetRequest("foo", "application/*", beforeModifiedDate.toDate(), req, res);
    //Test status code
    assertEquals(404, res.getStatus());
}

From source file:org.dataconservancy.ui.api.FileControllerTest.java

/**
 * Test trying to retrieve a file that has not been modified since the provided if-modified-date.
 * Expected return code: 304/*from   w  w  w  .  ja  v a  2 s. co m*/
 * @throws BizPolicyException
 * @throws IOException
 * @throws ArchiveServiceException
 * @throws RelationshipConstraintException
 */
@Test
public void testGetFileRequestNotUpdatedSinceFile()
        throws BizPolicyException, IOException, ArchiveServiceException, RelationshipConstraintException {
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_STRING);
    MockHttpServletResponse res = new MockHttpServletResponse();

    //run the handle request
    fileController.handleFileGetRequest("foo", "application/*", afterModifiedDate.toDate(), req, res);
    //Test status code
    assertEquals(304, res.getStatus());
}