Example usage for org.springframework.mock.web MockHttpServletRequest setMethod

List of usage examples for org.springframework.mock.web MockHttpServletRequest setMethod

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest setMethod.

Prototype

public void setMethod(@Nullable String method) 

Source Link

Usage

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testImportInfo() throws IOException {
    Catalog catalog = getCatalog();//from w  w  w.j  av a 2  s  . c  o m
    StoreInfo targetStore = catalog.getStoreByName("sf", "sf", StoreInfo.class);
    assertNotNull(targetStore);

    ImportController ctrl = new ImportController(getGeoServer(), applicationContext);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/hello");
    request.setMethod("post");

    JSONObj obj = ctrl.info("sf");

    assertEquals("sf", obj.str("workspace"));
    assertNotNull(obj.get("spaceAvailable"));
    assertEquals(obj.get("spaceAvailable"), obj.get("tmpSpace"));
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testImportGeoJSONintoDb() throws IOException, Exception {
    Catalog catalog = getCatalog();/*  w  ww  .  ja  v  a 2  s  .com*/
    StoreInfo targetStore = catalog.getStoreByName("sf", "sf", StoreInfo.class);
    assertNotNull(targetStore);

    ImportController ctrl = new ImportController(getGeoServer(), applicationContext);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/hello");
    request.setMethod("post");

    createMultiPartFormContent(request, "form-data; name=\"upload\"; filename=\"point.json\"",
            "application/json", IOUtils.toByteArray(getClass().getResourceAsStream("point.json")));

    JSONObj result = ctrl.importFile("sf", "sf", request);
    Long id = Long.parseLong(result.str("id"));

    //Wait for the import to complete
    result = pollImport(ctrl, "gs", id, "pending", request);
    assertNotNull(result);
    result = ctrl.update("gs", id, getUpdateTasks(result), request);
    result = pollImport(ctrl, "gs", id, "complete", request);
    assertNotNull(result);

    assertEquals(1, result.array("tasks").size());
    JSONObj obj = result.array("tasks").object(0);
    assertEquals("COMPLETE", obj.get("status"));

    assertEquals("sf", obj.object("layer").str("workspace"));
    assertEquals("point", obj.object("layer").str("name"));

    LayerInfo l = catalog.getLayerByName("sf:point");
    assertNotNull(l);
    FeatureTypeInfo f = (FeatureTypeInfo) l.getResource();
    assertEquals(targetStore, f.getStore());

    // ensure style in workspace
    StyleInfo s = l.getDefaultStyle();
    assertNotNull(s.getWorkspace());
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testListAttributes() throws Exception {
    Catalog cat = getCatalog();//w ww  .  j  a v  a  2 s.  c o m

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/hello");
    request.setMethod("get");

    StoreController ctrl = new StoreController(getGeoServer());
    JSONObj obj = ctrl.attributes("sf", "sf", "PrimitiveGeoFeature", 10, request);
    JSONArr attributes = obj.object("schema").array("attributes");
    JSONArr values = obj.array("values");

    //Page through actual features and ensure values match what is returned
    StoreInfo store = cat.getStoreByName("sf", "sf", StoreInfo.class);
    DataAccess data = ((DataStoreInfo) store).getDataStore(new NullProgressListener());
    FeatureSource source = data.getFeatureSource(new NameImpl("PrimitiveGeoFeature"));

    Query query = new Query(Query.ALL);
    query.setMaxFeatures(10);
    FeatureIterator features = source.getFeatures(query).features();
    int featureIndex = 0;
    while (features.hasNext()) {
        Feature feature = features.next();
        JSONArr featureJSON = (JSONArr) values.at(featureIndex);
        Property[] properties = feature.getProperties().toArray(new Property[feature.getProperties().size()]);

        for (int i = 0; i < attributes.size(); i++) {

            //Verify the schema matches the feature
            JSONObj attribute = attributes.object(i);
            assertEquals(attribute.get("type"),
                    properties[i].getDescriptor().getType().getBinding().getSimpleName());
            assertEquals(attribute.get("name"), properties[i].getDescriptor().getName().getLocalPart());

            //Verify the value matches the feature
            assertEquals(featureJSON.str(i),
                    properties[i].getValue() == null ? null : properties[i].getValue().toString());
        }
        featureIndex++;
    }
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testImportShapefileAsZip() throws Exception {
    Catalog catalog = getCatalog();/*w  w  w.j  a  v a 2  s . c  om*/
    //Test default root of "data"
    RESTUtils.loadMapFromGlobal().remove("root");
    assertNull(catalog.getLayerByName("gs:point"));

    ImportController ctrl = new ImportController(getGeoServer(), applicationContext);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/hello");
    request.setMethod("post");

    createMultiPartFormContent(request, "form-data; name=\"upload\"; filename=\"point.zip\"", "application/zip",
            IOUtils.toByteArray(getClass().getResourceAsStream("point.shp.zip")));

    JSONObj result = ctrl.importFile("gs", request);
    Long id = Long.parseLong(result.str("id"));

    //Wait for the import to complete
    result = pollImport(ctrl, "gs", id, "pending", request);
    assertNotNull(result);
    result = ctrl.update("gs", id, getUpdateTasks(result), request);
    result = pollImport(ctrl, "gs", id, "complete", request);
    assertNotNull(result);

    assertEquals(1, result.array("tasks").size());
    JSONObj obj = result.array("tasks").object(0);

    assertEquals("gs", obj.object("layer").str("workspace"));
    assertEquals("point", obj.object("layer").str("name"));

    LayerInfo l = catalog.getLayerByName("gs:point");
    StoreInfo s = catalog.getStoreByName("gs", "point", StoreInfo.class);
    assertNotNull(l);
    assertNotNull(s);
    //Wait for the taskListener to move the imported file
    Thread.sleep(500);
    assertTrue(new File(catalog.getResourceLoader().getBaseDirectory(), "data/gs/point/point.shp").exists());
    assertTrue(new File(new URL(ResourcePool.getParams(s.getConnectionParameters(), catalog.getResourceLoader())
            .get("url").toString()).getFile()).exists());
    assertEquals(new File(catalog.getResourceLoader().getBaseDirectory(), "data/gs/point").getAbsoluteFile(),
            new File(new URL(ResourcePool.getParams(s.getConnectionParameters(), catalog.getResourceLoader())
                    .get("url").toString()).getFile()).getAbsoluteFile());

    // ensure style in workspace
    StyleInfo style = l.getDefaultStyle();
    assertNotNull(style.getWorkspace());

    //Try to reimport the same store - should succeed
    createMultiPartFormContent(request, "form-data; name=\"upload\"; filename=\"point.zip\"", "application/zip",
            IOUtils.toByteArray(getClass().getResourceAsStream("point.shp.zip")));
    obj = ctrl.importFile("gs", request);

    assertNotNull(obj.get("id"));

}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testIconsUploadDelete() throws Exception {
    Catalog catalog = getCatalog();/*from w ww.j av a2 s.  c  o  m*/
    IconController ctrl = new IconController(getGeoServer());

    // test upload
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/api/icons/cite");
    request.setMethod("post");

    createMultiPartFormContent(request, "form-data; name=\"icon\"; filename=\"STYLE.PROPERTIES\"",
            "text/x-java-properties", "square=LINESTRING((0 0,0 1,1 1,1 0,0 0))".getBytes());

    JSONArr arr = ctrl.create("cite", request);
    assertEquals(1, arr.size());

    Resource r = catalog.getResourceLoader().get("workspaces/cite/styles/STYLE.PROPERTIES");
    assertEquals("created", Resource.Type.RESOURCE, r.getType());

    // test delete
    MockHttpServletRequestBuilder delete = delete("/api/icons/cite/icon.png");
    ctrl.delete("cite", "STYLE.PROPERTIES");

    r = catalog.getResourceLoader().get("workspaces/cite/styles/STYLE.PROPERTIES");
    assertEquals("deleted", Resource.Type.UNDEFINED, r.getType());

    //Global style directory

    // test upload
    request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/api/icons");
    request.setMethod("post");

    createMultiPartFormContent(request, "form-data; name=\"icon\"; filename=\"STYLE.PROPERTIES\"",
            "text/x-java-properties", "square=LINESTRING((0 0,0 1,1 1,1 0,0 0))".getBytes());

    arr = ctrl.create(request);
    assertEquals(1, arr.size());

    r = catalog.getResourceLoader().get("styles/STYLE.PROPERTIES");
    assertEquals("created", Resource.Type.RESOURCE, r.getType());

    // test delete
    delete = delete("/api/icons/icon.png");
    ctrl.delete("STYLE.PROPERTIES");

    r = catalog.getResourceLoader().get("styles/STYLE.PROPERTIES");
    assertEquals("deleted", Resource.Type.UNDEFINED, r.getType());
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testImportShapefilesWithSpaceInName() throws Exception {
    Catalog catalog = getCatalog();//  ww  w  . j a va 2  s.  co m
    //Test REST global root
    GeoServerInfo gsInfo = GeoServerExtensions.bean(GeoServer.class).getGlobal();
    SettingsInfo info = gsInfo.getSettings();
    info.getMetadata().put("root",
            catalog.getResourceLoader().findOrCreateDirectory("uploads").getAbsolutePath());
    getGeoServer().save(gsInfo);
    assertNull(catalog.getLayerByName("gs:point_space"));

    ImportController ctrl = new ImportController(getGeoServer(), applicationContext);
    StoreController storeCtrl = new StoreController(getGeoServer());

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/hello");
    request.setMethod("post");

    //Import as separate files
    MimeMultipart body = initMultiPartFormContent(request);

    appendMultiPartFormContent(body, "form-data; name=\"upload\"; filename=\"point space.dbf\"",
            "application/octet-stream", IOUtils.toByteArray(getClass().getResourceAsStream("point space.dbf")));
    appendMultiPartFormContent(body, "form-data; name=\"upload\"; filename=\"point space.prj\"",
            "application/octet-stream", IOUtils.toByteArray(getClass().getResourceAsStream("point space.prj")));
    appendMultiPartFormContent(body, "form-data; name=\"upload\"; filename=\"point space.shp\"",
            "application/octet-stream", IOUtils.toByteArray(getClass().getResourceAsStream("point space.shp")));
    appendMultiPartFormContent(body, "form-data; name=\"upload\"; filename=\"point space.shx\"",
            "application/octet-stream", IOUtils.toByteArray(getClass().getResourceAsStream("point space.shx")));

    createMultiPartFormContent(body, request);

    JSONObj result = ctrl.importFile("gs", request);
    Long id = Long.parseLong(result.str("id"));

    //Wait for the import to complete
    result = pollImport(ctrl, "gs", id, "pending", request);
    assertNotNull(result);
    result = ctrl.update("gs", id, getUpdateTasks(result), request);
    result = pollImport(ctrl, "gs", id, "complete", request);
    assertNotNull(result);
    //Apparently the catalog is time-sensitive now...
    Thread.sleep(100);
    LayerInfo l = catalog.getLayerByName("gs:point_20space");
    StoreInfo s = catalog.getStoreByName("gs", "point_space", StoreInfo.class);
    assertNotNull(l);
    assertNotNull(s);
    //Wait for the taskListener to move the imported file
    Thread.sleep(500);
    assertTrue(
            new File(catalog.getResourceLoader().getBaseDirectory(), "uploads/gs/point_space/point space.shp")
                    .exists());
    assertTrue(new File(new URL(ResourcePool.getParams(s.getConnectionParameters(), catalog.getResourceLoader())
            .get("url").toString()).getFile()).exists());
    assertEquals(
            new File(catalog.getResourceLoader().getBaseDirectory(), "uploads/gs/point_space")
                    .getAbsoluteFile(),
            new File(new URL(ResourcePool.getParams(s.getConnectionParameters(), catalog.getResourceLoader())
                    .get("url").toString()).getFile()).getAbsoluteFile());

    StyleInfo style = l.getDefaultStyle();
    assertEquals("point_space", l.getDefaultStyle().getName());
    assertEquals("point_20space", l.getResource().getName());
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testImportShapefiles() throws Exception {
    Catalog catalog = getCatalog();//  w  ww .  j  av a2 s.c om
    catalog.addListener(new UploadDeleteCatalogListener(catalog));
    //Test REST global root
    GeoServerInfo gsInfo = GeoServerExtensions.bean(GeoServer.class).getGlobal();
    SettingsInfo info = gsInfo.getSettings();
    info.getMetadata().put("root",
            catalog.getResourceLoader().findOrCreateDirectory("uploads").getAbsolutePath());
    getGeoServer().save(gsInfo);
    assertNull(catalog.getLayerByName("gs:point"));

    ImportController ctrl = new ImportController(getGeoServer(), applicationContext);
    StoreController storeCtrl = new StoreController(getGeoServer());

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContextPath("/geoserver");
    request.setRequestURI("/geoserver/hello");
    request.setMethod("post");

    //Import as separate files
    MimeMultipart body = initMultiPartFormContent(request);

    appendMultiPartFormContent(body, "form-data; name=\"upload\"; filename=\"point.dbf\"",
            "application/octet-stream", IOUtils.toByteArray(getClass().getResourceAsStream("point.dbf")));
    appendMultiPartFormContent(body, "form-data; name=\"upload\"; filename=\"point.prj\"",
            "application/octet-stream", IOUtils.toByteArray(getClass().getResourceAsStream("point.prj")));
    appendMultiPartFormContent(body, "form-data; name=\"upload\"; filename=\"point.shp\"",
            "application/octet-stream", IOUtils.toByteArray(getClass().getResourceAsStream("point.shp")));
    appendMultiPartFormContent(body, "form-data; name=\"upload\"; filename=\"point.shx\"",
            "application/octet-stream", IOUtils.toByteArray(getClass().getResourceAsStream("point.shx")));

    createMultiPartFormContent(body, request);

    JSONObj result = ctrl.importFile("gs", request);
    Long id = Long.parseLong(result.str("id"));

    //Wait for the import to complete
    result = pollImport(ctrl, "gs", id, "pending", request);
    assertNotNull(result);
    result = ctrl.update("gs", id, getUpdateTasks(result), request);
    result = pollImport(ctrl, "gs", id, "complete", request);
    assertNotNull(result);

    assertEquals(1, result.array("tasks").size());
    JSONObj obj = result.array("tasks").object(0);

    assertEquals("gs", obj.object("layer").str("workspace"));
    assertEquals("point", obj.object("layer").str("name"));

    LayerInfo l = catalog.getLayerByName("gs:point");
    StoreInfo s = catalog.getStoreByName("gs", "point", StoreInfo.class);
    assertNotNull(l);
    assertNotNull(s);
    //Wait for the taskListener to move the imported file
    Thread.sleep(500);
    assertTrue(new File(catalog.getResourceLoader().getBaseDirectory(), "uploads/gs/point/point.shp").exists());
    assertTrue(new File(new URL(ResourcePool.getParams(s.getConnectionParameters(), catalog.getResourceLoader())
            .get("url").toString()).getFile()).exists());
    assertEquals(new File(catalog.getResourceLoader().getBaseDirectory(), "uploads/gs/point").getAbsoluteFile(),
            new File(new URL(ResourcePool.getParams(s.getConnectionParameters(), catalog.getResourceLoader())
                    .get("url").toString()).getFile()).getAbsoluteFile());

    // ensure style in workspace
    StyleInfo style = l.getDefaultStyle();
    assertNotNull(style.getWorkspace());

    //Delete the store and re-upload:
    MockHttpServletRequest deleteRequest = new MockHttpServletRequest();
    deleteRequest.setContextPath("/geoserver");
    deleteRequest.setRequestURI("/geoserver/hello");
    deleteRequest.setMethod("delete");
    storeCtrl.delete("gs", "point", true, deleteRequest);
    assertFalse(
            new File(catalog.getResourceLoader().getBaseDirectory(), "uploads/gs/point/point.shp").exists());
    result = ctrl.importFile("gs", request);
    id = Long.parseLong(result.str("id"));
    //Wait for the import to complete
    result = pollImport(ctrl, "gs", id, "pending", request);
    assertNotNull(result);
    result = ctrl.update("gs", id, getUpdateTasks(result), request);
    result = pollImport(ctrl, "gs", id, "complete", request);
    assertNotNull(result);
    assertEquals(1, result.array("tasks").size());
    assertEquals("COMPLETE", result.array("tasks").object(0).get("status"));
}

From source file:com.boundlessgeo.geoserver.AppIntegrationTest.java

@Test
public void testImportDb() throws Exception {
    Catalog catalog = getCatalog();//from  w  w  w.  j av a  2s.  c  o  m
    assertNull(catalog.getLayerByName("gs:point"));

    ImportController ctrl = new ImportController(getGeoServer(), applicationContext);

    try (H2TestData data = new H2TestData()) {

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setContextPath("/geoserver");
        request.setRequestURI("/geoserver/hello");
        request.setMethod("post");

        JSONObj result = data.createConnectionParameters();
        result = ctrl.importDb("gs", result, request);

        Long id = Long.parseLong(result.str("id"));
        result = pollImport(ctrl, "gs", id, "pending", request);
        assertNotNull(result);
        assertTrue(result.integer("tasksTotal") > 0);
        List<String> names = Arrays.asList(new String[] { "ft1", "ft2", "ft3" });
        JSONArr tasks = new JSONArr();
        for (JSONObj o : result.array("tasks").objects()) {
            if (names.contains(o.get("name")) && o.get("status").equals("READY")) {
                tasks.add(new JSONObj().put("task", o.get("task").toString()));
                assertEquals("table", o.get("type"));
            }
        }
        JSONObj response = new JSONObj();
        response.put("tasks", tasks);

        result = ctrl.update("gs", id, response, request);
        result = pollImport(ctrl, "gs", id, "complete", request);
        assertNotNull(result);

        int complete = 0;
        int ready = 0;
        int no_crs = 0;
        int failed = 0;
        tasks = new JSONArr();

        for (JSONObj o : result.array("tasks").objects()) {
            if (o.get("status").equals("READY")) {
                ready++;
            }
            if (o.get("status").equals("NO_CRS")) {
                no_crs++;
                String srs = "EPSG:4326";
                tasks.add(new JSONObj().put("task", o.get("task").toString()).put("proj",
                        IO.proj(new JSONObj(), CRS.decode(srs), srs)));
            }
            if (o.get("status").equals("COMPLETE")) {
                complete++;
            }
            if (o.get("status").equals("ERROR") || o.get("status").equals("NO_BOUNDS")) {
                failed++;
            }
        }
        assertEquals(0, ready);
        assertEquals(1, complete);
        assertEquals(2, no_crs);
        assertEquals(0, failed);

        response = new JSONObj();
        response.put("tasks", tasks);

        result = ctrl.update("gs", id, response, request);
        //Wait for the import to complete
        result = pollImport(ctrl, "gs", id, "complete", request);
        assertNotNull(result);

        for (JSONObj o : result.array("tasks").objects()) {
            assertEquals("COMPLETE", o.get("status"));
        }

        //Try to reimport the same store - should fail and return existing store
        result = data.createConnectionParameters();
        result = ctrl.importDb("gs", result, request);

        assertNotNull(result.get("store"));
        assertNull(result.get("id"));
    }
}

From source file:org.fao.geonet.api.records.formatters.FormatterApiIntegrationTest.java

@Test
public void testLastModified() throws Exception {
    String stage = systemInfo.getStagingProfile();
    systemInfo.setStagingProfile(SystemInfo.STAGE_PRODUCTION);
    try {//from w  ww .j  a  va2 s  .c o m
        metadataRepository.update(id, new Updater<Metadata>() {
            @Override
            public void apply(@Nonnull Metadata entity) {
                entity.getDataInfo().setChangeDate(new ISODate("2012-01-18T15:04:43"));
            }
        });
        dataManager.indexMetadata(Lists.newArrayList("" + this.id));

        final String formatterName = "full_view";

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.getSession();
        request.addParameter("h2IdentInfo", "true");

        MockHttpServletResponse response = new MockHttpServletResponse();
        formatService.exec("eng", "html", "" + id, null, formatterName, "true", false, _100,
                new ServletWebRequest(request, response));
        final String lastModified = response.getHeader("Last-Modified");
        assertEquals("no-cache", response.getHeader("Cache-Control"));
        final String viewString = response.getContentAsString();
        assertNotNull(viewString);

        request = new MockHttpServletRequest();
        request.getSession();
        request.setMethod("GET");
        response = new MockHttpServletResponse();

        request.addHeader("If-Modified-Since", lastModified);
        formatService.exec("eng", "html", "" + id, null, formatterName, "true", false, _100,
                new ServletWebRequest(request, response));
        assertEquals(HttpStatus.SC_NOT_MODIFIED, response.getStatus());
        final ISODate newChangeDate = new ISODate();
        metadataRepository.update(id, new Updater<Metadata>() {
            @Override
            public void apply(@Nonnull Metadata entity) {
                entity.getDataInfo().setChangeDate(newChangeDate);
            }
        });

        dataManager.indexMetadata(Lists.newArrayList("" + this.id));

        request = new MockHttpServletRequest();
        request.getSession();
        request.setMethod("GET");
        response = new MockHttpServletResponse();

        request.addHeader("If-Modified-Since", lastModified);
        formatService.exec("eng", "html", "" + id, null, formatterName, "true", false, _100,
                new ServletWebRequest(request, response));
        assertEquals(HttpStatus.SC_OK, response.getStatus());
    } finally {
        systemInfo.setStagingProfile(stage);
    }
}