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.rest.catalog.CoverageStoreFileUploadTest.java

@Test
public void testHarvestImageMosaic() throws Exception {
    // Upload of the Mosaic via REST
    URL zip = MockData.class.getResource("watertemp.zip");
    InputStream is = null;//  www. ja  v  a 2s . com
    byte[] bytes;
    try {
        is = zip.openStream();
        bytes = IOUtils.toByteArray(is);
    } finally {
        IOUtils.closeQuietly(is);
    }

    MockHttpServletResponse response = putAsServletResponse(
            RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp2/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("watertemp2", "//coverageStore/name", d);
    XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d);

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

    // Harvesting of the Mosaic
    URL zipHarvest = MockData.class.getResource("harvesting.zip");
    // Extract a Byte array from the zip file
    is = null;
    try {
        is = zipHarvest.openStream();
        bytes = IOUtils.toByteArray(is);
    } finally {
        IOUtils.closeQuietly(is);
    }
    // Create the POST request
    MockHttpServletRequest request = createRequest(
            RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp2/file.imagemosaic");
    request.setMethod("POST");
    request.setContentType("application/zip");
    request.setContent(bytes);
    request.addHeader("Content-type", "application/zip");
    // Get The response
    dispatch(request);
    // Get the Mosaic Reader
    GridCoverageReader reader = storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints());
    // Test if all the TIME DOMAINS are present
    String[] metadataNames = reader.getMetadataNames();
    assertNotNull(metadataNames);
    assertEquals("true", reader.getMetadataValue("HAS_TIME_DOMAIN"));
    assertEquals("2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z",
            reader.getMetadataValue(metadataNames[0]));
}

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

@Test
public void testHarvestImageMosaicWithDirectory() throws Exception {
    // Upload of the Mosaic via REST
    URL zip = MockData.class.getResource("watertemp.zip");
    InputStream is = null;//  www .ja  va  2 s  .c  o  m
    byte[] bytes;
    try {
        is = zip.openStream();
        bytes = IOUtils.toByteArray(is);
    } finally {
        IOUtils.closeQuietly(is);
    }

    MockHttpServletResponse response = putAsServletResponse(
            RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp3/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("watertemp3", "//coverageStore/name", d);
    XMLAssert.assertXpathEvaluatesTo("ImageMosaic", "//coverageStore/type", d);

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

    // Harvesting of the Mosaic
    URL zipHarvest = MockData.class.getResource("harvesting.zip");
    Resource newZip = Files.asResource(new File("./target/harvesting2.zip"));
    // Copy the content of the first zip to the second
    IOUtils.copyStream(zipHarvest.openStream(), newZip.out(), true, true);
    Resource outputDirectory = Files.asResource(new File("./target/harvesting"));
    RESTUtils.unzipFile(newZip, outputDirectory);
    // Create the POST request
    MockHttpServletRequest request = createRequest(
            RestBaseController.ROOT_PATH + "/workspaces/gs/coveragestores/watertemp3/external.imagemosaic");
    request.setMethod("POST");
    request.setContentType("text/plain");
    request.setContent(("file:///" + outputDirectory.dir().getAbsolutePath()).getBytes("UTF-8"));
    request.addHeader("Content-type", "text/plain");
    // Get The response
    dispatch(request);
    // Get the Mosaic Reader
    GridCoverageReader reader = storeInfo.getGridCoverageReader(null, GeoTools.getDefaultHints());
    // Test if all the TIME DOMAINS are present
    String[] metadataNames = reader.getMetadataNames();
    assertNotNull(metadataNames);
    assertEquals("true", reader.getMetadataValue("HAS_TIME_DOMAIN"));
    assertEquals("2008-10-31T00:00:00.000Z,2008-11-01T00:00:00.000Z,2008-11-02T00:00:00.000Z",
            reader.getMetadataValue(metadataNames[0]));
    // Removal of the temporary directory
    outputDirectory.delete();
}

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

@Test
public void testShapeFileUploadWithCharset() throws Exception {
    /* Requires that a zipped shapefile (chinese_poly.zip) be in test-data directory */
    byte[] bytes = shpChineseZipAsBytes();
    MockHttpServletResponse response = putAsServletResponse(
            ROOT_PATH + "/workspaces/gs/datastores/chinese_poly/file.shp?charset=UTF-8", bytes,
            "application/zip");
    assertEquals(201, response.getStatus());

    MockHttpServletResponse response2 = getAsServletResponse("wfs?request=getfeature&typename=gs:chinese_poly",
            "GB18030");
    assertTrue(response2.getContentAsString().contains("\u951f\u65a4\u62f7"));
}

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

@Test
public void getBodyAsJsonUsingAcceptHeader() throws Exception {
    MockHttpServletRequest request = createRequest("/rest/styles/teststyle");
    request.setMethod("GET");
    request.addHeader("Accept", MBStyleHandler.MIME_TYPE);
    MockHttpServletResponse response = dispatch(request);

    assertEquals(200, response.getStatus());
    assertEquals(MBStyleHandler.MIME_TYPE, response.getContentType());
    String responseContent = response.getContentAsString();
    String expected = IOUtils.toString(this.getClass().getResourceAsStream("teststyle.json"));
    assertEquals(expected, responseContent);
}

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

@Test
public void getInfoAsJsonUsingExtension() throws Exception {
    MockHttpServletResponse response = getAsServletResponse("/rest/styles/teststyle.json");
    assertEquals(200, response.getStatus());
    String responseContent = response.getContentAsString();
    assertEquals("application/json", response.getContentType());

    // Assert that the response contains the style info as json 
    assertEquals(/* w  w  w.ja  va  2 s . c  o m*/
            "{\"style\":{\"name\":\"teststyle\"," + "\"format\":\"mbstyle\","
                    + "\"languageVersion\":{\"version\":\"1.0.0\"}," + "\"filename\":\"teststyle.json\"}}",
            responseContent);
}

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

@Test
public void getBodyAsSLDUsingAcceptHeader() throws Exception {
    MockHttpServletRequest request = createRequest("/rest/styles/teststyle");
    request.setMethod("GET");
    request.addHeader("Accept", SLDHandler.MIMETYPE_10);
    MockHttpServletResponse response = dispatch(request);

    assertEquals(200, response.getStatus());
    assertEquals(SLDHandler.MIMETYPE_10, response.getContentType());
    String content = response.getContentAsString();
    assertTrue(content.contains("<sld:Name>test-layer</sld:Name>"));
}

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

@Test
public void getBodyAsSLDUsingExtension() throws Exception {
    MockHttpServletResponse response = getAsServletResponse("/rest/styles/teststyle.sld");
    assertEquals(200, response.getStatus());
    assertEquals(SLDHandler.MIMETYPE_10, response.getContentType());
    String content = response.getContentAsString();
    assertTrue(content.contains("<sld:Name>test-layer</sld:Name>"));
}

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

@Test
public void getAsHTML() throws Exception {
    MockHttpServletResponse response = getAsServletResponse("/rest/styles/teststyle.html");
    assertEquals(200, response.getStatus());
    assertEquals(MediaType.TEXT_HTML_VALUE, response.getContentType());
    String content = response.getContentAsString();
    assertTrue(content.contains("<a href=\"http://localhost:8080/geoserver/rest/styles/teststyle"));
}

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

@Test
public void testGetWrongGranule() throws Exception {
    // Parameters for the request
    String ws = "wcs";
    String cs = "watertemp";
    String g = "notThere";
    // Request path
    String requestPath = RestBaseController.ROOT_PATH + "/workspaces/" + ws + "/coveragestores/" + cs
            + "/coverages/" + cs + "/index/granules/" + g;
    // Exception path
    // First request should thrown an exception
    MockHttpServletResponse response = getAsServletResponse(requestPath);
    assertEquals(404, response.getStatus());
    assertThat(response.getContentAsString(), containsString(g));
    // Same request with ?quietOnNotFound should not throw an exception
    response = getAsServletResponse(requestPath + "?quietOnNotFound=true");
    assertEquals(404, response.getStatus());
    // No exception thrown
    assertTrue(response.getContentAsString().isEmpty());
}

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

@Test
public void testGetWrongStyle() throws Exception {
    // Parameters for the request
    String ws = "gs";
    String style = "foooooo";
    // Request path
    String requestPath = RestBaseController.ROOT_PATH + "/styles/" + style + ".html";
    String requestPath2 = RestBaseController.ROOT_PATH + "/workspaces/" + ws + "/styles/" + style + ".html";
    // Exception path
    String exception = "No such style: " + style;
    String exception2 = "No such style " + style + " in workspace " + ws;

    // CASE 1: No workspace 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: workspace 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());
}