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

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

Introduction

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

Prototype

public String getContentAsString() throws UnsupportedEncodingException 

Source Link

Document

Get the content of the response body as a String , using the charset specified for the response by the application, either through HttpServletResponse methods or through a charset parameter on the Content-Type .

Usage

From source file:org.geoserver.opensearch.rest.ProductsControllerTest.java

@Test
public void testGetProductDescription() throws Exception {
    MockHttpServletResponse response = getAsServletResponse(
            "/rest/oseo/collections/SENTINEL2/products/S2A_OPER_MSI_L1C_TL_SGS__20160117T141030_A002979_T32TPL_N02.01/description");
    assertEquals(200, response.getStatus());
    assertEquals("text/html", response.getContentType());
    assertThat(response.getContentAsString(), both(containsString("<table>"))
            .and(containsString("2016-01-17T10:10:30.743Z / 2016-01-17T10:10:30.743Z")));
}

From source file:org.geoserver.opensearch.rest.ProductsControllerTest.java

private void assertProductDescription() throws Exception, UnsupportedEncodingException {
    MockHttpServletResponse response;
    response = getAsServletResponse(/*from   w ww .j  av a2s  .co  m*/
            "rest/oseo/collections/SENTINEL2/products/S2A_OPER_MSI_L1C_TL_SGS__20180101T000000_A006640_T32TPP_N02.04/description");
    assertEquals(200, response.getStatus());
    assertEquals("text/html", response.getContentType());
    assertThat(response.getContentAsString(), both(containsString("<table"))
            .and(containsString("S2A_OPER_MSI_L1C_TL_SGS__20180101T000000_A006640_T32TPP_N02.04")));
}

From source file:org.geoserver.opensearch.rest.ProductsControllerTest.java

@Test
public void testGetProductMissingThumbnail() throws Exception {
    // this one does not have a thumbnail
    MockHttpServletResponse response = getAsServletResponse(
            "/rest/oseo/collections/SENTINEL2/products/S2A_OPER_MSI_L1C_TL_SGS__20160117T141030_A002979_T32TPL_N02.01/thumbnail");
    assertEquals(404, response.getStatus());
    assertThat(response.getContentAsString(),
            containsString("S2A_OPER_MSI_L1C_TL_SGS__20160117T141030_A002979_T32TPL_N02.01"));
}

From source file:org.geoserver.opensearch.rest.ProductsControllerTest.java

private void testCreateProductAsZip(Set<ProductPart> parts) throws Exception {
    LOGGER.info("Testing: " + parts);
    byte[] zip = null;
    try (final ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ZipOutputStream zos = new ZipOutputStream(bos)) {
        for (ProductPart part : parts) {
            String resource, name;
            switch (part) {
            case Product:
                resource = "/product.json";
                name = "product.json";
                break;
            case Description:
                resource = "/product-description.html";
                name = "description.html";
                break;
            case Metadata:
                resource = "/product-metadata.xml";
                name = "metadata.xml";
                break;
            case Thumbnail:
                resource = "/product-thumb.jpeg";
                name = "thumbnail.jpeg";
                break;
            case OwsLinks:
                resource = "/product-links.json";
                name = "owsLinks.json";
                break;
            case Granules:
                resource = "/product-granules.json";
                name = "granules.json";
                break;
            default:
                throw new RuntimeException("Unexpected part " + part);
            }/*w w w.jav a 2  s . co  m*/

            ZipEntry entry = new ZipEntry(name);
            zos.putNextEntry(entry);
            IOUtils.copy(getClass().getResourceAsStream(resource), zos);
            zos.flush();
            zos.closeEntry();
        }
        zip = bos.toByteArray();
    }

    MockHttpServletResponse response = postAsServletResponse("rest/oseo/collections/SENTINEL2/products", zip,
            MediaTypeExtensions.APPLICATION_ZIP_VALUE);
    if (parts.contains(ProductPart.Product)) {
        assertEquals(201, response.getStatus());
        assertEquals(
                "http://localhost:8080/geoserver/rest/oseo/collections/SENTINEL2/products/S2A_OPER_MSI_L1C_TL_SGS__20180101T000000_A006640_T32TPP_N02.04",
                response.getHeader("location"));

        assertProductCreated();
    } else {
        assertEquals(400, response.getStatus());
        assertThat(response.getContentAsString(), containsString("product.json"));
        // failed, nothing else to check
        return;
    }

    if (parts.contains(ProductPart.Description)) {
        assertProductDescription();
    }
    if (parts.contains(ProductPart.Metadata)) {
        assertProductMetadata();
    }
    if (parts.contains(ProductPart.Thumbnail)) {
        assertProductThumbnail();
    }
    if (parts.contains(ProductPart.OwsLinks)) {
        assertProductLinks();
    }
    if (parts.contains(ProductPart.Granules)) {
        assertProductGranules();
    }
}

From source file:org.geoserver.rest.catalog.CoverageControllerTest.java

@Test
public void testGetWrongCoverage() throws Exception {
    // Parameters for the request
    String ws = "wcs";
    String cs = "BlueMarble";
    String c = "BlueMarblesssss";
    // Request path
    String requestPath = RestBaseController.ROOT_PATH + "/workspaces/" + ws + "/coverages/" + c + ".html";
    String requestPath2 = RestBaseController.ROOT_PATH + "/workspaces/" + ws + "/coveragestores/" + cs
            + "/coverages/" + c + ".html";
    // Exception path
    String exception = "No such coverage: " + ws + "," + c;
    String exception2 = "No such coverage: " + ws + "," + cs + "," + c;

    // CASE 1: No coveragestore set

    // First request should thrown an exception
    MockHttpServletResponse response = getAsServletResponse(requestPath);
    assertEquals(404, response.getStatus());
    assertTrue(response.getContentAsString().contains(exception));

    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath + "?quietOnNotFound=true");
    assertEquals(404, response.getStatus());
    assertFalse(response.getContentAsString().contains(exception));
    // No exception thrown
    assertTrue(response.getContentAsString().isEmpty());

    // CASE 2: coveragestore set

    // First request should thrown an exception
    response = getAsServletResponse(requestPath2);
    assertEquals(404, response.getStatus());
    assertTrue(response.getContentAsString().contains(exception2));

    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath2 + "?quietOnNotFound=true");
    assertEquals(404, response.getStatus());
    assertFalse(response.getContentAsString().contains(exception2));
    // No exception thrown
    assertTrue(response.getContentAsString().isEmpty());
}

From source file:org.geoserver.rest.catalog.CoverageControllerTest.java

@Test
public void testPutWithCalculation() throws Exception {
    String path = RestBaseController.ROOT_PATH + "/workspaces/wcs/coveragestores/DEM/coverages/DEM.xml";
    String clearLatLonBoundingBox = "<coverage>" + "<latLonBoundingBox/>" + "</coverage>";

    MockHttpServletResponse response = putAsServletResponse(path, clearLatLonBoundingBox, "text/xml");
    assertEquals("Couldn't remove lat/lon bounding box: \n" + response.getContentAsString(), 200,
            response.getStatus());/*from   w w w  .j  a v a2s . co  m*/

    Document dom = getAsDOM(path);
    assertXpathEvaluatesTo("0.0", "/coverage/latLonBoundingBox/minx", dom);
    print(dom);

    String updateNativeBounds = "<coverage>" + "<srs>EPSG:3785</srs>" + "</coverage>";

    response = putAsServletResponse(path, updateNativeBounds, "text/xml");

    assertEquals("Couldn't update native bounding box: \n" + response.getContentAsString(), 200,
            response.getStatus());
    dom = getAsDOM(path);
    print(dom);
    assertXpathExists("/coverage/nativeBoundingBox/minx[text()!='0.0']", dom);
}

From source file:org.geoserver.rest.catalog.CoverageStoreControllerTest.java

@Test
public void testGetAllOnMissingWorkspace() throws Exception {
    MockHttpServletResponse response = getAsServletResponse(
            RestBaseController.ROOT_PATH + "/workspaces/abcde/coveragestores.xml");
    assertEquals(404, response.getStatus());
    assertThat(response.getContentAsString(), containsString("abcde"));
}

From source file:org.geoserver.rest.catalog.CoverageStoreControllerTest.java

@Test
public void testGetWrongCoverageStore() throws Exception {
    // Parameters for the request
    String ws = "wcs";
    String cs = "BlueMarblesssss";
    // Request path
    String requestPath = RestBaseController.ROOT_PATH + "/workspaces/" + ws + "/coveragestores/" + cs + ".html";
    // Exception path
    String exception = "No such coverage store: " + ws + "," + cs;
    // First request should thrown an exception
    MockHttpServletResponse response = getAsServletResponse(requestPath);
    assertEquals(404, response.getStatus());
    assertTrue(response.getContentAsString().contains(exception));
    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath + "?quietOnNotFound=true");
    assertEquals(404, response.getStatus());
    assertFalse(response.getContentAsString().contains(exception));
    // No exception thrown
    assertTrue(response.getContentAsString().isEmpty());
}

From source file:org.geoserver.rest.catalog.CoverageStoreFileUploadTest.java

@Test
public void testWorldImageUploadZipped() throws Exception {
    URL zip = getClass().getResource("test-data/usa.zip");
    byte[] bytes = FileUtils.readFileToByteArray(DataUtilities.urlToFile(zip));

    MockHttpServletResponse response = putAsServletResponse(
            RestBaseController.ROOT_PATH + "/workspaces/sf/coveragestores/usa/file.worldimage", bytes,
            "application/zip");
    assertEquals(201, response.getStatus());

    String content = response.getContentAsString();
    Document d = dom(new ByteArrayInputStream(content.getBytes()));
    assertEquals("coverageStore", d.getDocumentElement().getNodeName());

    CoverageStoreInfo cs = getCatalog().getCoverageStoreByName("sf", "usa");
    assertNotNull(cs);//from w w  w  .  j av a 2  s  . co  m
    CoverageInfo ci = getCatalog().getCoverageByName("sf", "usa");
    assertNotNull(ci);
}

From source file:org.geoserver.rest.catalog.CoverageStoreFileUploadTest.java

@Test
public void testUploadImageMosaic() throws Exception {
    URL zip = MockData.class.getResource("watertemp.zip");
    InputStream is = null;//from  w  ww . j a  va 2  s .  com
    byte[] bytes;
    try {
        is = zip.openStream();
        bytes = IOUtils.toByteArray(is);
    } finally {
        IOUtils.closeQuietly(is);
    }

    MockHttpServletResponse response = putAsServletResponse(
            RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp/file.imagemosaic", bytes,
            "application/zip");
    assertEquals(201, response.getStatus());

    // check the response contents
    String content = response.getContentAsString();
    Document d = dom(new ByteArrayInputStream(content.getBytes()));

    XMLAssert.assertXpathEvaluatesTo("watertemp", "//coverageStore/name", d);
    XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d);

    // check the coverage is actually there
    CoverageStoreInfo storeInfo = getCatalog().getCoverageStoreByName("watertemp");
    assertNotNull(storeInfo);
    CoverageInfo ci = getCatalog().getCoverageByName("watertemp");
    assertNotNull(ci);
    assertEquals(storeInfo, ci.getStore());
}