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.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  . ja v a 2  s .  com*/
    CoverageInfo ci = getCatalog().getCoverageByName("sf", "usa");
    assertNotNull(ci);
}

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

@Test
@Ignore//  ww  w. j a  va  2 s  .  c om
// fixing https://osgeo-org.atlassian.net/browse/GEOS-6845, re-enable when a proper fix for spaces in
// name has been made
public void testUploadWithSpaces() throws Exception {
    URL zip = getClass().getResource("test-data/usa.zip");
    byte[] bytes = FileUtils.readFileToByteArray(DataUtilities.urlToFile(zip));

    MockHttpServletResponse response = putAsServletResponse(
            RestBaseController.ROOT_PATH
                    + "/workspaces/gs/coveragestores/store%20with%20spaces/file.worldimage",
            bytes, "application/zip");
    assertEquals(500, response.getStatus());
}

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;/* w w  w  . j  a  v  a  2s .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/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());
}

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;//from w  w w  .j a  v a 2 s  .c om
    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 testHarvestNotAllowedOnSimpleCoverageStore() throws Exception {
    // add bluemarble
    getTestData().addDefaultRasterLayer(SystemTestData.TASMANIA_BM, getCatalog());

    // Harvesting of the Mosaic
    URL zipHarvest = MockData.class.getResource("harvesting.zip");
    // Extract a Byte array from the zip file
    InputStream is = null;/*from  w w w . ja  v a 2s.c o m*/
    byte[] bytes;
    try {
        is = zipHarvest.openStream();
        bytes = IOUtils.toByteArray(is);
    } finally {
        IOUtils.closeQuietly(is);
    }
    // Create the POST request
    MockHttpServletRequest request = createRequest(
            RestBaseController.ROOT_PATH + "/workspaces/wcs/coveragestores/BlueMarble");
    request.setMethod("POST");
    request.setContentType("application/zip");
    request.setContent(bytes);
    request.addHeader("Content-type", "application/zip");
    // Get The response
    MockHttpServletResponse response = dispatch(request);
    // not allowed
    assertEquals(405, response.getStatus());
}

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;//ww  w . j av a  2 s. c  om
    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.DataStoreFileUploadTest.java

@Test
public void testShapeFileUploadExternal() throws Exception {
    Document dom = getAsDOM("wfs?request=getfeature&typename=gs:pds");
    assertEquals("ows:ExceptionReport", dom.getDocumentElement().getNodeName());

    File target = new File("target");
    File f = File.createTempFile("rest", "dir", target);
    try {/*from ww w . j  ava  2 s. c o m*/
        f.delete();
        f.mkdir();

        File zip = new File(f, "pds.zip");
        IOUtils.copy(getClass().getResourceAsStream("test-data/pds.zip"), new FileOutputStream(zip));
        org.geoserver.rest.util.IOUtils.inflate(new ZipFile(zip), Files.asResource(f), null);

        MockHttpServletResponse resp = putAsServletResponse(
                ROOT_PATH + "/workspaces/gs/datastores/pds/external.shp",
                new File(f, "pds.shp").toURL().toString(), "text/plain");
        assertEquals(201, resp.getStatus());

        dom = getAsDOM("wfs?request=getfeature&typename=gs:pds");
        assertFeatures(dom);

        // try to download it again after a full reload from disk (GEOS-4616)
        getGeoServer().reload();

        resp = getAsServletResponse(ROOT_PATH + "/workspaces/gs/datastores/pds/file.shp");
        assertEquals(200, resp.getStatus());
        assertEquals("application/zip", resp.getContentType());

        Set<String> entryNames = new HashSet<>();
        try (ByteArrayInputStream bin = getBinaryInputStream(resp);
                ZipInputStream zin = new ZipInputStream(bin)) {
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                entryNames.add(entry.getName());
            }
        }
        assertTrue(entryNames.contains("pds.shp"));
        assertTrue(entryNames.contains("pds.shx"));
        assertTrue(entryNames.contains("pds.dbf"));
    } finally {
        FileUtils.deleteQuietly(f);
    }
}

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

@Test
public void testShapeFileUploadNotExisting() throws Exception {
    File file = new File("./target/notThere.tiff");
    if (file.exists()) {
        assertTrue(file.delete());//from w  ww.j  a v a2 s . c  o  m
    }

    URL url = DataUtilities.fileToURL(file.getCanonicalFile());
    String body = url.toExternalForm();
    MockHttpServletResponse response = putAsServletResponse(
            ROOT_PATH + "/workspaces/gs/datastores/pds/external.shp", body, "text/plain");
    assertEquals(400, response.getStatus());
}

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

@Test
public void testGetProperties() throws Exception {
    MockHttpServletResponse resp = getAsServletResponse(
            ROOT_PATH + "/workspaces/gs/datastores/pds/file.properties");
    assertEquals(404, resp.getStatus());

    byte[] bytes = propertyFile();
    put(ROOT_PATH + "/workspaces/gs/datastores/pds/file.properties", bytes, "text/plain");

    resp = getAsServletResponse(ROOT_PATH + "/workspaces/gs/datastores/pds/file.properties");
    assertEquals(200, resp.getStatus());
    assertEquals("application/zip", resp.getContentType());

    ByteArrayInputStream bin = getBinaryInputStream(resp);
    ZipInputStream zin = new ZipInputStream(bin);

    ZipEntry entry = zin.getNextEntry();
    assertNotNull(entry);/*from   w  w  w. j a v  a2 s .  c o m*/
    assertEquals("pds.properties", entry.getName());
}