diff --git a/README b/README index 5d1ca2e..af2037c 100644 --- a/README +++ b/README @@ -4,4 +4,4 @@ The purpose of this project is to hold a REST client library to interact with Ge For more information see this page: -http://code.google.com/p/geoserver-manager/ \ No newline at end of file +https://github.com/geosolutions-it/geoserver-manager \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5b0e73c..9f00b6a 100644 --- a/pom.xml +++ b/pom.xml @@ -129,8 +129,8 @@ maven-compiler-plugin 2.0.2 - 1.5 - 1.5 + 1.6 + 1.6 diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java index 5a77450..b22f5b8 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTPublisher.java @@ -40,13 +40,11 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.Iterator; -import java.util.List; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.io.FilenameUtils; import org.apache.log4j.Level; import org.apache.log4j.Logger; -import org.apache.log4j.Priority; /** * Connect to a GeoServer instance to publish or modify data. @@ -257,7 +255,7 @@ public class GeoServerRESTPublisher { try { GSLayerEncoder layerEncoder = new GSLayerEncoder(); - layerEncoder.addDefaultStyle(defaultStyle); + layerEncoder.setDefaultStyle(defaultStyle); configureLayer(workspace, layerName, layerEncoder); } catch (Exception e) { LOGGER.warn("Error in publishing shapefile " + e.getMessage(), @@ -334,17 +332,7 @@ public class GeoServerRESTPublisher { .append("/datastores/").append(storename).append("/file.shp?"); // append parameters - if (params != null) { - final int paramsSize = params.length; - if (paramsSize > 0) { - sbUrl.append(params[0].getName()).append("=") - .append(params[0].getValue()); - for (int i = 1; i < paramsSize; i++) { - sbUrl.append("&").append(params[i].getName()).append("=") - .append(params[i].getValue()); - } - } - } + sbUrl.append(appendParameters(params)); // if (workspace != null) { // sbUrl.append("namespace=").append(workspace); @@ -492,7 +480,7 @@ public class GeoServerRESTPublisher { /** * The configure parameter is used to control how the data store is * configured upon file upload. It can take one of the three values “first”, - * “none”, or “all”.
+ * none, or all.
* first - Only setup the first feature type available in the data store. * This is the default.
* none - Do not configure any feature types.
@@ -516,7 +504,7 @@ public class GeoServerRESTPublisher { * The update parameter is used to control how existing data is handled when * the file is PUT into a datastore that (a) already exists and (b) already * contains a schema that matches the content of the file. It can take one - * of the two values “append”, or “overwrite”.
+ * of the two values append, or overwrite.
* append - Data being uploaded is appended to the existing data. This is * the default.
* overwrite - Data being uploaded replaces any existing data.
@@ -537,6 +525,75 @@ public class GeoServerRESTPublisher { // === COVERAGES // ========================================================================== + /** + * + * Publish a zipped worldimage file. It is assumed that the the zip-file + * contain the *.prj to set the srs. + *

+ * This is equivalent call with cUrl: + * + *

+	 * {@code
+	 * curl -u admin:geoserver -XPUT -H 'Content-type: application/zip' \
+	 * 
+	 *       --data-binary @$ZIPFILE \
+	 * 
+	 *       http://$GSIP:$GSPORT/$SERVLET/rest/workspaces/$WORKSPACE/coveragestores/$COVERAGESTORE/file.worldimage
+	 * 
+ * + * @param workspace + * Workspace to use + * @param coveragestore + * Name of the coveragestore + * @param file + * zip file to upload + * @param configure + * Configure parameter. It may be null. + * @param params + * parameters to append to the url (can be null).
+ * Accepted parameters are: + * + * @see #{@link ParameterConfigure} + * @return true if the operation completed successfully. + */ + private boolean publishCoverage(String workspace, String coveragestore, String format, String mimeType, + File file, ParameterConfigure configure, NameValuePair... params) + throws FileNotFoundException { + // build full URL + StringBuilder sbUrl = new StringBuilder(restURL) + .append("/rest/workspaces/").append(workspace) + .append("/coveragestores/").append(coveragestore) + .append("/file.").append(format); + + if (configure != null) { + sbUrl.append("?configure=").append(configure); + if (params!= null && !configure.equals(ParameterConfigure.NONE)){ + final String paramString=appendParameters(params); + if (!paramString.isEmpty()){ + sbUrl.append("&").append(paramString); + } + } + } + String sentResult = HTTPUtils.put(sbUrl.toString(), file, + mimeType, gsuser, gspass); + boolean fileSent = sentResult != null; + + if (fileSent) { + if (LOGGER.isInfoEnabled()) + LOGGER.info("File successfully uploaded ( " + file + + ")"); + } else { + if (LOGGER.isEnabledFor(Level.WARN)) + LOGGER.warn("Error in sending file " + file); + } + return fileSent; + } + // ========================================================================== // === GEOTIFF // ========================================================================== @@ -555,16 +612,10 @@ public class GeoServerRESTPublisher { * * * @return true if the operation completed successfully. - * @deprecated UNTESTED */ public boolean publishGeoTIFF(String workspace, String storeName, File geotiff) throws FileNotFoundException { - String sUrl = restURL + "/rest/workspaces/" + workspace - + "/coveragestores/" + storeName + "/geotiff"; - String sendResult = HTTPUtils - .put(sUrl, geotiff, "text", gsuser, gspass); // CHECKME: text?!? - boolean sent = sendResult != null; - return sent; + return publishCoverage(workspace, storeName, "geotiff", "image/geotiff", geotiff, ParameterConfigure.FIRST, (NameValuePair[])null); } /** @@ -629,9 +680,84 @@ public class GeoServerRESTPublisher { return store; } + // ========================================================================== + // === WORLDIMAGE + // ========================================================================== + + /** + * {@link #publishWorldImage(String, String, File, ParameterConfigure, NameValuePair...)} + */ + public boolean publishWorldImage(String workspace, String coveragestore, File zipFile) + throws FileNotFoundException { + return publishWorldImage(workspace, coveragestore, zipFile, ParameterConfigure.FIRST,(NameValuePair)null); + } + + /** + * + * Publish a zipped worldimage file. It is assumed that the the zip-file + * contain the *.prj to set the srs. + *

+ * This is equivalent call with cUrl: + * + *

+	 * {@code
+	 * curl -u admin:geoserver -XPUT -H 'Content-type: application/zip' \
+	 * 
+	 *       --data-binary @$ZIPFILE \
+	 * 
+	 *       http://$GSIP:$GSPORT/$SERVLET/rest/workspaces/$WORKSPACE/coveragestores/$COVERAGESTORE/file.worldimage
+	 * 
+ * + * @param workspace + * Workspace to use + * @param coveragestore + * Name of the coveragestore + * @param zipFile + * zip file to upload + * @param configure + * Configure parameter. It may be null. + * @param params + * parameters to append to the url (can be null).
+ * Accepted parameters are: + * + * @see #{@link ParameterConfigure} + * @return true if the operation completed successfully. + */ + public boolean publishWorldImage(String workspace, String coveragestore, + File zipFile, ParameterConfigure configure, NameValuePair... params) + throws FileNotFoundException { + return publishCoverage(workspace, coveragestore, "worldimage", "application/zip", zipFile, configure, params); + } + + // ========================================================================== // === MOSAIC // ========================================================================== + + /** + * Publish imagemosaic as zip file + * + * @see {@link #publishWorldImage(String, String, File)} + */ + public boolean publishImageMosaic(String workspace, String storeName, + File zipFile) throws FileNotFoundException { + return publishCoverage(workspace, storeName, "imagemosaic", "application/zip", zipFile, ParameterConfigure.FIRST, (NameValuePair[])null); + } + + /** + * Publish imagemosaic as zip file + * + * @see {@link #publishWorldImage(String, String, File, ParameterConfigure, NameValuePair...)} + */ + public boolean publishImageMosaic(String workspace, String storeName, + File zipFile, ParameterConfigure configure, NameValuePair... params) throws FileNotFoundException { + return publishCoverage(workspace, storeName, "imagemosaic", "application/zip", zipFile, configure, params); + } /** * Publish a Mosaic from a filesystem currently readable by GeoServer. @@ -659,7 +785,7 @@ public class GeoServerRESTPublisher { /* * Carlo (23 Nov 2011): commented out since this directory should be - * readable by targhet GeoServer not the calling client! + * readable by target GeoServer not the calling client! */ if (!mosaicDir.isDirectory()) { if (LOGGER.isEnabledFor(Level.WARN)) @@ -779,7 +905,7 @@ public class GeoServerRESTPublisher { if (store == null) { LOGGER.warn("Unable to get the store" + workspace + ":" + storeName - + " from the targhet geoserver."); + + " from the target geoserver."); return null; } @@ -1051,11 +1177,12 @@ public class GeoServerRESTPublisher { public boolean removeDatastore(String workspace, String storename, final boolean recurse) throws IllegalArgumentException { try { - if (workspace==null || storename==null) + if (workspace == null || storename == null) throw new IllegalArgumentException("Arguments may not be null!"); if (workspace.isEmpty() || storename.isEmpty()) - throw new IllegalArgumentException("Arguments may not be empty!"); - + throw new IllegalArgumentException( + "Arguments may not be empty!"); + final StringBuilder url = new StringBuilder(restURL); url.append("/rest/workspaces/").append(workspace) .append("/datastores/").append(storename); @@ -1113,13 +1240,15 @@ public class GeoServerRESTPublisher { * @return true if the CoverageStore was successfully removed. */ public boolean removeCoverageStore(final String workspace, - final String storename, final boolean recurse) throws IllegalArgumentException { + final String storename, final boolean recurse) + throws IllegalArgumentException { try { - if (workspace==null || storename==null) + if (workspace == null || storename == null) throw new IllegalArgumentException("Arguments may not be null!"); if (workspace.isEmpty() || storename.isEmpty()) - throw new IllegalArgumentException("Arguments may not be empty!"); - + throw new IllegalArgumentException( + "Arguments may not be empty!"); + final StringBuilder url = new StringBuilder(restURL); url.append("/rest/workspaces/").append(workspace) .append("/coveragestores/").append(storename); @@ -1173,18 +1302,20 @@ public class GeoServerRESTPublisher { * The recurse parameter is used to recursively delete all * resources contained by the specified workspace. This includes * data stores, coverage stores, feature types, etc... Allowable - * values for this parameter are “true” or “false”. The default - * value is “false”. + * values for this parameter are true or false. The + * default value is false. * @return true if the WorkSpace was successfully removed. */ - public boolean removeWorkspace(String workspace, boolean recurse) throws IllegalArgumentException { + public boolean removeWorkspace(String workspace, boolean recurse) + throws IllegalArgumentException { workspace = sanitize(workspace); try { - if (workspace==null) + if (workspace == null) throw new IllegalArgumentException("Arguments may not be null!"); if (workspace.isEmpty()) - throw new IllegalArgumentException("Arguments may not be empty!"); - + throw new IllegalArgumentException( + "Arguments may not be empty!"); + StringBuffer url = new StringBuffer(restURL).append( "/rest/workspaces/").append(workspace); if (recurse) @@ -1315,8 +1446,8 @@ public class GeoServerRESTPublisher { } /** - * Allows to configure some layer attributes such and DefaultStyle TODO - * WmsPath + * Allows to configure some layer attributes such and DefaultStyle + * @TODO WmsPath */ public boolean configureLayer(final String workspace, final String layerName, final GSLayerEncoder layer) { @@ -1492,7 +1623,8 @@ public class GeoServerRESTPublisher { * coverage name (if != null will override the CoverageEncoder * name) * @return true if success - * @deprecated use {@link GeoServerRESTPublisher#configureCoverage(GSCoverageEncoder, String, String)} + * @deprecated use + * {@link GeoServerRESTPublisher#configureCoverage(GSCoverageEncoder, String, String)} */ protected boolean configureCoverage(final GSCoverageEncoder ce, final String wsname, final String csname, String cname) { @@ -1514,6 +1646,34 @@ public class GeoServerRESTPublisher { return s; } + /** + * Append params generating a string in the form:
+ *

+ * NAME_0=VALUE_0&NAME_1=VALUE_1&....&NAME_n-1=VALUE_n-1 + *

+ *
+ * + * @param params + * an array of NameValuePair + * @return the parameter string or empty an string + */ + private String appendParameters(NameValuePair... params) { + StringBuilder sbUrl = new StringBuilder(); + // append parameters + if (params != null) { + final int paramsSize = params.length; + if (paramsSize > 0) { + sbUrl.append(params[0].getName()).append("=") + .append(params[0].getValue()); + for (int i = 1; i < paramsSize; i++) { + sbUrl.append("&").append(params[i].getName()).append("=") + .append(params[i].getValue()); + } + } + } + return sbUrl.toString(); + } + protected String encode(String s) { // try { // return URLEncoder.encode(s,"UTF-8"); diff --git a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTPublisherTest.java b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTPublisherTest.java deleted file mode 100644 index 60e45ee..0000000 --- a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTPublisherTest.java +++ /dev/null @@ -1,478 +0,0 @@ -/* - * GeoServer-Manager - Simple Manager Library for GeoServer - * - * Copyright (C) 2007,2011 GeoSolutions S.A.S. - * http://www.geo-solutions.it - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package it.geosolutions.geoserver.rest; - -import it.geosolutions.geoserver.rest.decoder.RESTLayer; -import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore; -import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; -import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; - -import org.apache.commons.httpclient.NameValuePair; -import org.apache.commons.io.IOUtils; -import org.apache.log4j.Logger; -import org.jdom.Element; -import org.jdom.Namespace; -import org.springframework.core.io.ClassPathResource; - -/** - * Testcase for publishing layers on geoserver. - * We need a running GeoServer to properly run the tests. - * If such geoserver instance cannot be contacted, tests will be skipped. - * - * @author etj - */ -public class GeoserverRESTPublisherTest extends GeoserverRESTTest { - - private final static Logger LOGGER = Logger.getLogger(GeoserverRESTPublisherTest.class); - - public GeoserverRESTPublisherTest(String testName) { - super(testName); - } - - public void testWorkspaces() { - if (!enabled()) return; - deleteAll(); - - assertEquals(0, reader.getWorkspaces().size()); - - assertTrue(publisher.createWorkspace("WS1")); - assertTrue(publisher.createWorkspace("WS2")); - assertEquals(2, reader.getWorkspaces().size()); - - assertFalse(publisher.createWorkspace("WS2")); - assertEquals(2, reader.getWorkspaces().size()); - } - - /** - * remove workspace and all of its contents - * @throws IOException - */ - public void testWorkspaceRemoval() throws IOException { - if (!enabled()) return; - deleteAll(); - - String storeName = "testRESTStoreGeotiff"; - String layerName = "resttestdem"; - - assertTrue(reader.getWorkspaces().isEmpty()); - assertTrue(publisher.createWorkspace(DEFAULT_WS)); - - File geotiff = new ClassPathResource("testdata/resttestdem.tif").getFile(); - - // known state? - assertFalse("Cleanup failed", existsLayer(layerName)); - - // test insert - RESTCoverageStore pc = publisher.publishExternalGeoTIFF(DEFAULT_WS, storeName, geotiff, null, null); - - // remove workspace and all of its contents - assertTrue(publisher.removeWorkspace(DEFAULT_WS,true)); - } - - public void testStyles() throws IOException { - if (!enabled()) return; - deleteAll(); - - assertEquals(0, reader.getStyles().size()); - - final String styleName = "restteststyle"; - File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); - - // insert style - assertTrue(publisher.publishStyle(sldFile)); - assertTrue(reader.existsStyle(styleName)); - - assertFalse(publisher.publishStyle(sldFile)); - assertTrue(reader.existsStyle(styleName)); - - String sld = reader.getSLD(styleName); - assertNotNull(sld); - - Element styleEl = JDOMBuilder.buildElement(sld); - assertNotNull(styleEl); - - Namespace SLDNS = Namespace.getNamespace("sld", "http://www.opengis.net/sld"); - - try{ - - assertEquals(styleName, styleEl.getChild("NamedLayer", SLDNS).getChild("Name",SLDNS).getText()); - assertEquals("STYLE FOR TESTING PURPOSES", styleEl.getChild("NamedLayer", SLDNS).getChild("UserStyle", SLDNS).getChild("Title", SLDNS).getText()); - } catch(NullPointerException npe) { - fail("Error in SLD"); - } - -// assertEquals(1475, sld.length()); - - assertEquals(1, reader.getStyles().size()); - } - - public void testExternalGeotiff() throws FileNotFoundException, IOException { - if (!enabled()) return; - deleteAll(); - - String storeName = "testRESTStoreGeotiff"; - String layerName = "resttestdem"; - - assertTrue(reader.getWorkspaces().isEmpty()); - assertTrue(publisher.createWorkspace(DEFAULT_WS)); - - File geotiff = new ClassPathResource("testdata/resttestdem.tif").getFile(); - - // known state? - assertFalse("Cleanup failed", existsLayer(layerName)); - - // test insert - RESTCoverageStore pc = publisher.publishExternalGeoTIFF(DEFAULT_WS, storeName, geotiff, null, null); - assertNotNull("publish() failed", pc); - assertTrue(existsLayer(layerName)); - LOGGER.info(pc); - RESTCoverageStore reloadedCS = reader.getCoverageStore(DEFAULT_WS, storeName); - - assertEquals(pc.getName(), reloadedCS.getName()); - assertEquals(pc.getWorkspaceName(), reloadedCS.getWorkspaceName()); - - //test delete - assertTrue("Unpublish() failed", publisher.unpublishCoverage(DEFAULT_WS, storeName, layerName)); - assertTrue("Unpublish() failed", publisher.removeCoverageStore(DEFAULT_WS, storeName)); - assertFalse("Bad unpublish()", publisher.unpublishCoverage(DEFAULT_WS, storeName, layerName)); - assertFalse(existsLayer(layerName)); - } - - protected void cleanupTestFT(String layerName, String ns, String storeName) { - // dry run delete to work in a known state - RESTLayer testLayer = reader.getLayer(layerName); - if (testLayer != null) { - LOGGER.info("Clearing stale test layer " + layerName); - boolean ok = publisher.unpublishFeatureType(ns, storeName, layerName); - if (!ok) { - fail("Could not unpublish layer " + layerName); - } - } - if (publisher.removeDatastore(ns, storeName)) { - LOGGER.info("Cleared stale datastore " + storeName); - } - - assertFalse("Cleanup failed", existsLayer(layerName)); - } - - protected void cleanupTestStyle(final String styleName) { - // dry run delete to work in a known state - if (reader.existsStyle(styleName)) { - LOGGER.info("Clearing stale test style " + styleName); - boolean ok = publisher.removeStyle(styleName); - if (!ok) { - fail("Could not unpublish style " + styleName); - } - } - assertFalse("Cleanup failed", reader.existsStyle(styleName)); - } - - public void testPublishDeleteShapeZip() throws FileNotFoundException, IOException { - if (!enabled()) { - return; - } -// Assume.assumeTrue(enabled); - deleteAllWorkspaces(); - assertTrue(publisher.createWorkspace(DEFAULT_WS)); - - String storeName = "resttestshp"; - String layerName = "cities"; - - File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); - - // known state? - cleanupTestFT(layerName, DEFAULT_WS, storeName); - - // test insert - boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile); - assertTrue("publish() failed", published); - assertTrue(existsLayer(layerName)); - - RESTLayer layer = reader.getLayer(layerName); - - LOGGER.info("Layer style is " + layer.getDefaultStyle()); - - //test delete - boolean ok = publisher.unpublishFeatureType(DEFAULT_WS, storeName, layerName); - assertTrue("Unpublish() failed", ok); - assertFalse(existsLayer(layerName)); - - // remove also datastore - boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName,false); - assertTrue("removeDatastore() failed", dsRemoved); - - } - - public void testPublishDeleteStyledShapeZip() throws FileNotFoundException, IOException { - if (!enabled()) { - return; - } -// Assume.assumeTrue(enabled); - - String ns = "geosolutions"; - String storeName = "resttestshp"; - String layerName = "cities"; - - File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); - cleanupTestFT(layerName, ns, storeName); - - final String styleName = "restteststyle"; - File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); - cleanupTestStyle(styleName); - - // insert style - boolean sldpublished = publisher.publishStyle(sldFile); // Will take the name from sld contents - assertTrue("style publish() failed", sldpublished); - assertTrue(reader.existsStyle(styleName)); - - // test insert - boolean published = publisher.publishShp(ns, storeName, layerName, zipFile, "EPSG:4326", styleName); - assertTrue("publish() failed", published); - assertTrue(existsLayer(layerName)); - - RESTLayer layer = reader.getLayer(layerName); -// RESTLayer layerDecoder = new RESTLayer(layer); - LOGGER.info("Layer style is " + layer.getDefaultStyle()); - assertEquals("Style not assigned properly", styleName, layer.getDefaultStyle()); - - //test delete - boolean ok = publisher.unpublishFeatureType(ns, storeName, layerName); - assertTrue("Unpublish() failed", ok); - assertFalse(existsLayer(layerName)); - - // remove also datastore - boolean dsRemoved = publisher.removeDatastore(ns, storeName); - assertTrue("removeDatastore() failed", dsRemoved); - - //test delete style - boolean oksld = publisher.removeStyle(styleName); - assertTrue("Unpublish() failed", oksld); - assertFalse(reader.existsStyle(styleName)); - } - - public void testPublishDeleteShapeZipWithParams() throws FileNotFoundException, IOException { - if (!enabled()) { - return; - } -// Assume.assumeTrue(enabled); - deleteAllWorkspaces(); - assertTrue(publisher.createWorkspace(DEFAULT_WS)); - - String storeName = "resttestshp"; - String layerName = "cities"; - - File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); - - // known state? - cleanupTestFT(layerName, DEFAULT_WS, storeName); - - // test insert - boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile,"EPSG:4326",new NameValuePair("charset","UTF-8")); - assertTrue("publish() failed", published); - assertTrue(existsLayer(layerName)); - - RESTLayer layer = reader.getLayer(layerName); - - LOGGER.info("Layer style is " + layer.getDefaultStyle()); - - //test delete - boolean ok = publisher.unpublishFeatureType(DEFAULT_WS, storeName, layerName); - assertTrue("Unpublish() failed", ok); - assertFalse(existsLayer(layerName)); - - // remove also datastore - boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName); - assertTrue("removeDatastore() failed", dsRemoved); - - } - - public void testPublishDeleteStyleFile() throws FileNotFoundException, IOException { - if (!enabled()) { - return; - } -// Assume.assumeTrue(enabled); - final String styleName = "restteststyle"; - - File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); - - // known state? - cleanupTestStyle(styleName); - - // test insert - boolean published = publisher.publishStyle(sldFile); // Will take the name from sld contents - assertTrue("publish() failed", published); - assertTrue(reader.existsStyle(styleName)); - - //test delete - boolean ok = publisher.removeStyle(styleName); - assertTrue("Unpublish() failed", ok); - assertFalse(reader.existsStyle(styleName)); - } - - public void testPublishDeleteStyleString() throws FileNotFoundException, IOException { - if (!enabled()) { - return; - } -// Assume.assumeTrue(enabled); - final String styleName = "restteststyle"; - - File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); - - // known state? - cleanupTestStyle(styleName); - - // test insert - String sldContent = IOUtils.toString(new FileInputStream(sldFile)); - - boolean published = publisher.publishStyle(sldContent); // Will take the name from sld contents - assertTrue("publish() failed", published); - assertTrue(reader.existsStyle(styleName)); - - //test delete - boolean ok = publisher.removeStyle(styleName); - assertTrue("Unpublish() failed", ok); - assertFalse(reader.existsStyle(styleName)); - } - - public void testDeleteUnexistingCoverage() throws FileNotFoundException, IOException { - if (!enabled()) { - return; - } -// Assume.assumeTrue(enabled); - - String wsName = "this_ws_does_not_exist"; - String storeName = "this_store_does_not_exist"; - String layerName = "this_layer_does_not_exist"; - - boolean ok = publisher.unpublishCoverage(wsName, storeName, layerName); - assertFalse("unpublished not existing layer", ok); - } - - public void testDeleteUnexistingFeatureType() throws FileNotFoundException, IOException { - if (!enabled()) { - return; - } -// Assume.assumeTrue(enabled); - - String wsName = "this_ws_does_not_exist"; - String storeName = "this_store_does_not_exist"; - String layerName = "this_layer_does_not_exist"; - - boolean ok = publisher.unpublishFeatureType(wsName, storeName, layerName); - assertFalse("unpublished not existing layer", ok); - } - - public void testDeleteUnexistingDatastore() throws FileNotFoundException, IOException { - if (!enabled()) { - return; - } -// Assume.assumeTrue(enabled); - - String wsName = "this_ws_does_not_exist"; - String storeName = "this_store_does_not_exist"; - - boolean ok = publisher.removeDatastore(wsName, storeName); - assertFalse("removed not existing datastore", ok); - } - - - public void testUpdateDefaultStyle() throws FileNotFoundException, IOException { - if (!enabled()) { - return; - } - - String storeName = "resttestshp"; - String layerName = "cities"; - - final String styleName = "restteststyle"; - { - File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); - cleanupTestStyle(styleName); - boolean sldpublished = publisher.publishStyle(sldFile); // Will take the name from sld contents - assertTrue("style publish() failed", sldpublished); - assertTrue(reader.existsStyle(styleName)); - } - - final String styleName2 = "restteststyle2"; - { - File sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile(); - cleanupTestStyle(styleName2); - boolean sldpublished = publisher.publishStyle(sldFile); // Will take the name from sld contents - assertTrue("style publish() failed", sldpublished); - assertTrue(reader.existsStyle(styleName2)); - } - - - File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); - - // known state? - cleanupTestFT(layerName, DEFAULT_WS, storeName); - - // test insert - boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile, "EPSG:4326", styleName); - assertTrue("publish() failed", published); - assertTrue(existsLayer(layerName)); - - { - RESTLayer layer = reader.getLayer(layerName); - LOGGER.info("Layer style is " + layer.getDefaultStyle()); - assertEquals(styleName, layer.getDefaultStyle()); - } - - GSLayerEncoder le = new GSLayerEncoder(); - le.addDefaultStyle(styleName2); - publisher.configureLayer(DEFAULT_WS, layerName, le); - - { - RESTLayer layer = reader.getLayer(layerName); - LOGGER.info("Layer style is " + layer.getDefaultStyle()); - assertEquals(styleName2, layer.getDefaultStyle()); - } - - - // remove layer and datastore - boolean ok = publisher.unpublishFeatureType(DEFAULT_WS, storeName, layerName); - assertFalse(existsLayer(layerName)); - boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName); - assertTrue("removeDatastore() failed", dsRemoved); - } - -// public void testDeleteUnexistingFT() throws FileNotFoundException, IOException { -// String wsName = "this_ws_does_not_exist"; -// String storeName = "this_store_does_not_exist"; -// String layerName = "this_layer_does_not_exist"; -// -// boolean ok = publisher.unpublishFT(wsName, storeName, layerName); -// assertFalse("unpublished not existing layer", ok); -// } - private boolean existsLayer(String layername) { - return reader.getLayer(layername) != null; - } -} diff --git a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java index b976a21..e90f9a3 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java @@ -283,5 +283,9 @@ public abstract class GeoserverRESTTest extends TestCase { + coverage.getName(), removed); } + + protected boolean existsLayer(String layername) { + return reader.getLayer(layername) != null; + } } \ No newline at end of file diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java new file mode 100644 index 0000000..a584a98 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java @@ -0,0 +1,108 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package it.geosolutions.geoserver.rest.publisher; + +import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.apache.log4j.Logger; +import org.springframework.core.io.ClassPathResource; + +/** + * Testcase for publishing layers on geoserver. + * We need a running GeoServer to properly run the tests. + * If such geoserver instance cannot be contacted, tests will be skipped. + * + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + */ +public class GeoserverRESTGeoTiffTest extends GeoserverRESTTest { + + private final static Logger LOGGER = Logger.getLogger(GeoserverRESTGeoTiffTest.class); + + public GeoserverRESTGeoTiffTest(String testName) { + super(testName); + } + + public void testExternalGeotiff() throws FileNotFoundException, IOException { + if (!enabled()) return; + deleteAll(); + + String storeName = "testRESTStoreGeotiff"; + String layerName = "resttestdem"; + + assertTrue(reader.getWorkspaces().isEmpty()); + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + + File geotiff = new ClassPathResource("testdata/resttestdem.tif").getFile(); + + // known state? + assertFalse("Cleanup failed", existsLayer(layerName)); + + // test insert + RESTCoverageStore pc = publisher.publishExternalGeoTIFF(DEFAULT_WS, storeName, geotiff, null, null); + assertNotNull("publish() failed", pc); + assertTrue(existsLayer(layerName)); + LOGGER.info(pc); + RESTCoverageStore reloadedCS = reader.getCoverageStore(DEFAULT_WS, storeName); + + assertEquals(pc.getName(), reloadedCS.getName()); + assertEquals(pc.getWorkspaceName(), reloadedCS.getWorkspaceName()); + + //test delete + assertTrue("Unpublish() failed", publisher.unpublishCoverage(DEFAULT_WS, storeName, layerName)); + assertTrue("Unpublish() failed", publisher.removeCoverageStore(DEFAULT_WS, storeName)); + assertFalse("Bad unpublish()", publisher.unpublishCoverage(DEFAULT_WS, storeName, layerName)); + assertFalse(existsLayer(layerName)); + } + + public void testGeotiff() throws FileNotFoundException, IOException { + if (!enabled()) return; + deleteAll(); + + String storeName = "testRESTStoreGeotiff"; + String layerName = "resttestdem"; + + assertTrue(reader.getWorkspaces().isEmpty()); + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + + File geotiff = new ClassPathResource("testdata/resttestdem.tif").getFile(); + + // known state? + assertFalse("Cleanup failed", existsLayer(layerName)); + + // test insert + boolean pub = publisher.publishGeoTIFF(DEFAULT_WS, storeName, geotiff); + + assertNotNull("publish() failed", pub); + + //delete + assertTrue("Unpublish() failed", publisher.removeCoverageStore(DEFAULT_WS, storeName,true)); + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTImageMosaicTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTImageMosaicTest.java similarity index 98% rename from src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTImageMosaicTest.java rename to src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTImageMosaicTest.java index 8175229..6399925 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTImageMosaicTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTImageMosaicTest.java @@ -23,9 +23,10 @@ * THE SOFTWARE. */ -package it.geosolutions.geoserver.rest; +package it.geosolutions.geoserver.rest.publisher; +import it.geosolutions.geoserver.rest.GeoserverRESTTest; import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder.ProjectionPolicy; diff --git a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTPostgisDatastoreTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPostgisDatastoreTest.java similarity index 98% rename from src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTPostgisDatastoreTest.java rename to src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPostgisDatastoreTest.java index 9511abf..8f61849 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTPostgisDatastoreTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPostgisDatastoreTest.java @@ -23,9 +23,10 @@ * THE SOFTWARE. */ -package it.geosolutions.geoserver.rest; +package it.geosolutions.geoserver.rest.publisher; +import it.geosolutions.geoserver.rest.GeoserverRESTTest; import it.geosolutions.geoserver.rest.decoder.RESTDataStore; import it.geosolutions.geoserver.rest.encoder.GSPostGISDatastoreEncoder; import org.apache.log4j.Logger; diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublisherTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublisherTest.java new file mode 100644 index 0000000..7a19fd2 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTPublisherTest.java @@ -0,0 +1,120 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package it.geosolutions.geoserver.rest.publisher; + +import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.decoder.RESTLayer; + +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.apache.log4j.Logger; + +/** + * Testcase for publishing layers on geoserver. + * We need a running GeoServer to properly run the tests. + * If such geoserver instance cannot be contacted, tests will be skipped. + * + * @author etj + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + */ +public class GeoserverRESTPublisherTest extends GeoserverRESTTest { + + private final static Logger LOGGER = Logger.getLogger(GeoserverRESTPublisherTest.class); + + public GeoserverRESTPublisherTest(String testName) { + super(testName); + } + + protected void cleanupTestFT(String layerName, String ns, String storeName) { + // dry run delete to work in a known state + RESTLayer testLayer = reader.getLayer(layerName); + if (testLayer != null) { + LOGGER.info("Clearing stale test layer " + layerName); + boolean ok = publisher.unpublishFeatureType(ns, storeName, layerName); + if (!ok) { + fail("Could not unpublish layer " + layerName); + } + } + if (publisher.removeDatastore(ns, storeName)) { + LOGGER.info("Cleared stale datastore " + storeName); + } + + assertFalse("Cleanup failed", existsLayer(layerName)); + } + + public void testDeleteUnexistingCoverage() throws FileNotFoundException, IOException { + if (!enabled()) { + return; + } +// Assume.assumeTrue(enabled); + + String wsName = "this_ws_does_not_exist"; + String storeName = "this_store_does_not_exist"; + String layerName = "this_layer_does_not_exist"; + + boolean ok = publisher.unpublishCoverage(wsName, storeName, layerName); + assertFalse("unpublished not existing layer", ok); + } + + public void testDeleteUnexistingFeatureType() throws FileNotFoundException, IOException { + if (!enabled()) { + return; + } +// Assume.assumeTrue(enabled); + + String wsName = "this_ws_does_not_exist"; + String storeName = "this_store_does_not_exist"; + String layerName = "this_layer_does_not_exist"; + + boolean ok = publisher.unpublishFeatureType(wsName, storeName, layerName); + assertFalse("unpublished not existing layer", ok); + } + + public void testDeleteUnexistingDatastore() throws FileNotFoundException, IOException { + if (!enabled()) { + return; + } +// Assume.assumeTrue(enabled); + + String wsName = "this_ws_does_not_exist"; + String storeName = "this_store_does_not_exist"; + + boolean ok = publisher.removeDatastore(wsName, storeName); + assertFalse("removed not existing datastore", ok); + } + + + // public void testDeleteUnexistingFT() throws FileNotFoundException, IOException { +// String wsName = "this_ws_does_not_exist"; +// String storeName = "this_store_does_not_exist"; +// String layerName = "this_layer_does_not_exist"; +// +// boolean ok = publisher.unpublishFT(wsName, storeName, layerName); +// assertFalse("unpublished not existing layer", ok); +// } + +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java new file mode 100644 index 0000000..9ff27c4 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java @@ -0,0 +1,176 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package it.geosolutions.geoserver.rest.publisher; + +import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.decoder.RESTLayer; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.apache.commons.httpclient.NameValuePair; +import org.apache.log4j.Logger; +import org.springframework.core.io.ClassPathResource; + +/** + * Testcase for publishing layers on geoserver. + * We need a running GeoServer to properly run the tests. + * If such geoserver instance cannot be contacted, tests will be skipped. + * + * @author etj + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + */ +public class GeoserverRESTShapeTest extends GeoserverRESTTest { + + private final static Logger LOGGER = Logger.getLogger(GeoserverRESTShapeTest.class); + + public GeoserverRESTShapeTest(String testName) { + super(testName); + } + + public void testPublishDeleteShapeZip() throws FileNotFoundException, IOException { + if (!enabled()) { + return; + } +// Assume.assumeTrue(enabled); + deleteAllWorkspaces(); + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + + String storeName = "resttestshp"; + String layerName = "cities"; + + File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); + + + // test insert + boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile); + assertTrue("publish() failed", published); + assertTrue(existsLayer(layerName)); + + RESTLayer layer = reader.getLayer(layerName); + + LOGGER.info("Layer style is " + layer.getDefaultStyle()); + + //test delete + boolean ok = publisher.unpublishFeatureType(DEFAULT_WS, storeName, layerName); + assertTrue("Unpublish() failed", ok); + assertFalse(existsLayer(layerName)); + + // remove also datastore + boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName,false); + assertTrue("removeDatastore() failed", dsRemoved); + + } + + public void testPublishDeleteStyledShapeZip() throws FileNotFoundException, IOException { + if (!enabled()) { + return; + } +// Assume.assumeTrue(enabled); + + String ns = "geosolutions"; + String storeName = "resttestshp"; + String layerName = "cities"; + + File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); + publisher.removeDatastore(DEFAULT_WS, storeName,true); + + final String styleName = "restteststyle"; + File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); + + // insert style + boolean sldpublished = publisher.publishStyle(sldFile); // Will take the name from sld contents + assertTrue("style publish() failed", sldpublished); + assertTrue(reader.existsStyle(styleName)); + + // test insert + boolean published = publisher.publishShp(ns, storeName, layerName, zipFile, "EPSG:4326", styleName); + assertTrue("publish() failed", published); + assertTrue(existsLayer(layerName)); + + RESTLayer layer = reader.getLayer(layerName); +// RESTLayer layerDecoder = new RESTLayer(layer); + LOGGER.info("Layer style is " + layer.getDefaultStyle()); + assertEquals("Style not assigned properly", styleName, layer.getDefaultStyle()); + + // remove also datastore + boolean dsRemoved = publisher.removeDatastore(ns, storeName,true); + assertTrue("removeDatastore() failed", dsRemoved); + + //test delete style + boolean oksld = publisher.removeStyle(styleName); + assertTrue("Unpublish() failed", oksld); + assertFalse(reader.existsStyle(styleName)); + } + + public void testPublishDeleteShapeZipWithParams() throws FileNotFoundException, IOException { + if (!enabled()) { + return; + } +// Assume.assumeTrue(enabled); + deleteAllWorkspaces(); + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + + String storeName = "resttestshp"; + String layerName = "cities"; + + File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); + + // known state? + publisher.removeDatastore(DEFAULT_WS, storeName,true); + + // test insert + boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile,"EPSG:4326",new NameValuePair("charset","UTF-8")); + assertTrue("publish() failed", published); + assertTrue(existsLayer(layerName)); + + RESTLayer layer = reader.getLayer(layerName); + + LOGGER.info("Layer style is " + layer.getDefaultStyle()); + + //test delete + boolean ok = publisher.unpublishFeatureType(DEFAULT_WS, storeName, layerName); + assertTrue("Unpublish() failed", ok); + assertFalse(existsLayer(layerName)); + + // remove also datastore + boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName); + assertTrue("removeDatastore() failed", dsRemoved); + + } + + + // public void testDeleteUnexistingFT() throws FileNotFoundException, IOException { +// String wsName = "this_ws_does_not_exist"; +// String storeName = "this_store_does_not_exist"; +// String layerName = "this_layer_does_not_exist"; +// +// boolean ok = publisher.unpublishFT(wsName, storeName, layerName); +// assertFalse("unpublished not existing layer", ok); +// } + +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java new file mode 100644 index 0000000..8f50fb3 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java @@ -0,0 +1,213 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package it.geosolutions.geoserver.rest.publisher; + +import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.decoder.RESTLayer; +import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; +import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.apache.log4j.Logger; +import org.jdom.Element; +import org.jdom.Namespace; +import org.springframework.core.io.ClassPathResource; + +/** + * Testcase for publishing layers on geoserver. + * We need a running GeoServer to properly run the tests. + * If such geoserver instance cannot be contacted, tests will be skipped. + * + * @author etj + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + */ +public class GeoserverRESTStyleTest extends GeoserverRESTTest { + + private final static Logger LOGGER = Logger.getLogger(GeoserverRESTStyleTest.class); + + public GeoserverRESTStyleTest(String testName) { + super(testName); + } + + public void testStyles() throws IOException { + if (!enabled()) return; + deleteAll(); + + assertEquals(0, reader.getStyles().size()); + + final String styleName = "restteststyle"; + File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); + + // insert style + assertTrue(publisher.publishStyle(sldFile)); + assertTrue(reader.existsStyle(styleName)); + + assertFalse(publisher.publishStyle(sldFile)); + assertTrue(reader.existsStyle(styleName)); + + String sld = reader.getSLD(styleName); + assertNotNull(sld); + + Element styleEl = JDOMBuilder.buildElement(sld); + assertNotNull(styleEl); + + Namespace SLDNS = Namespace.getNamespace("sld", "http://www.opengis.net/sld"); + + try{ + + assertEquals(styleName, styleEl.getChild("NamedLayer", SLDNS).getChild("Name",SLDNS).getText()); + assertEquals("STYLE FOR TESTING PURPOSES", styleEl.getChild("NamedLayer", SLDNS).getChild("UserStyle", SLDNS).getChild("Title", SLDNS).getText()); + } catch(NullPointerException npe) { + fail("Error in SLD"); + } + +// assertEquals(1475, sld.length()); + + assertEquals(1, reader.getStyles().size()); + } + + protected void cleanupTestStyle(final String styleName) { + // dry run delete to work in a known state + if (reader.existsStyle(styleName)) { + LOGGER.info("Clearing stale test style " + styleName); + boolean ok = publisher.removeStyle(styleName); + if (!ok) { + fail("Could not unpublish style " + styleName); + } + } + assertFalse("Cleanup failed", reader.existsStyle(styleName)); + } + + public void testPublishDeleteStyleFile() throws FileNotFoundException, IOException { + if (!enabled()) { + return; + } +// Assume.assumeTrue(enabled); + final String styleName = "restteststyle"; + + File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); + + // known state? + cleanupTestStyle(styleName); + + // test insert + boolean published = publisher.publishStyle(sldFile); // Will take the name from sld contents + assertTrue("publish() failed", published); + assertTrue(reader.existsStyle(styleName)); + + //test delete + boolean ok = publisher.removeStyle(styleName); + assertTrue("Unpublish() failed", ok); + assertFalse(reader.existsStyle(styleName)); + } + + public void testPublishDeleteStyleString() throws FileNotFoundException, IOException { + if (!enabled()) { + return; + } +// Assume.assumeTrue(enabled); + final String styleName = "restteststyle"; + + File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); + + // known state? + cleanupTestStyle(styleName); + + // test insert + String sldContent = IOUtils.toString(new FileInputStream(sldFile)); + + boolean published = publisher.publishStyle(sldContent); // Will take the name from sld contents + assertTrue("publish() failed", published); + assertTrue(reader.existsStyle(styleName)); + + //test delete + boolean ok = publisher.removeStyle(styleName); + assertTrue("Unpublish() failed", ok); + assertFalse(reader.existsStyle(styleName)); + } + + public void testUpdateDefaultStyle() throws FileNotFoundException, IOException { + if (!enabled()) { + return; + } + deleteAll(); + + String storeName = "resttestshp"; + String layerName = "cities"; + + final String styleName = "restteststyle"; + { + File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); + cleanupTestStyle(styleName); + boolean sldpublished = publisher.publishStyle(sldFile); // Will take the name from sld contents + assertTrue("style publish() failed", sldpublished); + assertTrue(reader.existsStyle(styleName)); + } + + final String styleName2 = "restteststyle2"; + { + File sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile(); + cleanupTestStyle(styleName2); + boolean sldpublished = publisher.publishStyle(sldFile,styleName2); + assertTrue("style publish() failed", sldpublished); + assertTrue(reader.existsStyle(styleName2)); + } + + File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); + + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + + // test insert + boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile, "EPSG:4326", styleName); + assertTrue("publish() failed", published); + assertTrue(existsLayer(layerName)); + + { + RESTLayer layer = reader.getLayer(layerName); + LOGGER.info("Layer style is " + layer.getDefaultStyle()); + assertEquals(styleName, layer.getDefaultStyle()); + } + + GSLayerEncoder le = new GSLayerEncoder(); + le.setDefaultStyle(styleName2); + publisher.configureLayer(DEFAULT_WS, layerName, le); + + { + RESTLayer layer = reader.getLayer(layerName); + LOGGER.info("Layer style is " + layer.getDefaultStyle()); + assertEquals(styleName2, layer.getDefaultStyle()); + } + + // remove layer and datastore + boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName, true); + assertTrue("removeDatastore() failed", dsRemoved); + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorkspaceTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorkspaceTest.java new file mode 100644 index 0000000..d8aba9c --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorkspaceTest.java @@ -0,0 +1,92 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package it.geosolutions.geoserver.rest.publisher; + +import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.decoder.RESTCoverageStore; + +import java.io.File; +import java.io.IOException; + +import org.apache.log4j.Logger; +import org.springframework.core.io.ClassPathResource; + +/** + * Testcase for publishing layers on geoserver. + * We need a running GeoServer to properly run the tests. + * If such geoserver instance cannot be contacted, tests will be skipped. + * + * @author etj + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + */ +public class GeoserverRESTWorkspaceTest extends GeoserverRESTTest { + + private final static Logger LOGGER = Logger.getLogger(GeoserverRESTWorkspaceTest.class); + + public GeoserverRESTWorkspaceTest(String testName) { + super(testName); + } + + public void testWorkspaces() { + if (!enabled()) return; + deleteAll(); + + assertEquals(0, reader.getWorkspaces().size()); + + assertTrue(publisher.createWorkspace("WS1")); + assertTrue(publisher.createWorkspace("WS2")); + assertEquals(2, reader.getWorkspaces().size()); + + assertFalse(publisher.createWorkspace("WS2")); + assertEquals(2, reader.getWorkspaces().size()); + } + + /** + * remove workspace and all of its contents + * @throws IOException + */ + public void testWorkspaceRemoval() throws IOException { + if (!enabled()) return; + deleteAll(); + + String storeName = "testRESTStoreGeotiff"; + String layerName = "resttestdem"; + + assertTrue(reader.getWorkspaces().isEmpty()); + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + + File geotiff = new ClassPathResource("testdata/resttestdem.tif").getFile(); + + // known state? + assertFalse("Cleanup failed", existsLayer(layerName)); + + // test insert + RESTCoverageStore pc = publisher.publishExternalGeoTIFF(DEFAULT_WS, storeName, geotiff, null, null); + + // remove workspace and all of its contents + assertTrue(publisher.removeWorkspace(DEFAULT_WS,true)); + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorldImageTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorldImageTest.java new file mode 100644 index 0000000..d4ce426 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTWorldImageTest.java @@ -0,0 +1,95 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package it.geosolutions.geoserver.rest.publisher; + +import it.geosolutions.geoserver.rest.GeoserverRESTTest; +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.ParameterConfigure; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.httpclient.NameValuePair; +import org.apache.log4j.Logger; +import org.springframework.core.io.ClassPathResource; + +/** + * Testcase for publishing layers on geoserver. + * We need a running GeoServer to properly run the tests. + * If such geoserver instance cannot be contacted, tests will be skipped. + * + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + */ +public class GeoserverRESTWorldImageTest extends GeoserverRESTTest { + + private final static Logger LOGGER = Logger.getLogger(GeoserverRESTWorldImageTest.class); + + public GeoserverRESTWorldImageTest(String testName) { + super(testName); + } + + public void testPublishWorldImage() throws IOException { + + if (!enabled()) { + return; + } + deleteAll(); + String storeName = "testWorldimage"; + + assertTrue(reader.getWorkspaces().isEmpty()); + + assertTrue(publisher.createWorkspace(DEFAULT_WS)); + + File worldImageFile = new ClassPathResource( + "testdata/sw.zip").getFile(); + + // test publish + + boolean wp = publisher.publishWorldImage(DEFAULT_WS, storeName, + worldImageFile, ParameterConfigure.NONE, null); + + assertTrue("Publish worldfile with no layer configured, failed.", wp); + + assertTrue("Unpublish() failed", publisher.removeCoverageStore(DEFAULT_WS, storeName, true)); + + // create default style + File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); + assertTrue(publisher.publishStyle(sldFile,"raster")); + + wp = publisher.publishWorldImage(DEFAULT_WS, storeName, + worldImageFile, ParameterConfigure.FIRST, new NameValuePair("coverageName", "worldImage_test")); + + assertTrue("Publish worldfile configuring layer name, failed.", wp); + + assertTrue("Unpublish() failed", publisher.removeCoverageStore(DEFAULT_WS, storeName, true)); + + wp = publisher.publishWorldImage(DEFAULT_WS, storeName, + worldImageFile, ParameterConfigure.ALL,null); + + assertTrue("Publish worldfile configuring all available layers, failed.", wp); + + assertTrue("Unpublish() failed", publisher.removeCoverageStore(DEFAULT_WS, storeName, true)); + } +} diff --git a/src/test/resources/testdata/sw.zip b/src/test/resources/testdata/sw.zip new file mode 100644 index 0000000..ef00400 Binary files /dev/null and b/src/test/resources/testdata/sw.zip differ