From a56aa61cf5f45284444fcf4779a6fc881dbfcb9e Mon Sep 17 00:00:00 2001 From: ccancellieri Date: Thu, 18 Oct 2012 11:57:30 +0200 Subject: [PATCH] FeatureTypeEncoder is missing Attributes: fix to close issue 45. --- .../rest/decoder/RESTFeatureType.java | 1 + .../geoserver/rest/decoder/RESTResource.java | 189 +++++++++---- .../rest/encoder/GSLayerEncoder.java | 28 +- .../rest/encoder/GSResourceEncoder.java | 3 +- .../encoder/feature/FeatureTypeAttribute.java | 32 +++ .../encoder/feature/GSAttributeEncoder.java | 92 ++++++ .../encoder/feature/GSFeatureTypeEncoder.java | 64 ++++- .../rest/encoder/utils/ElementUtils.java | 20 +- .../encoder/utils/PropertyXMLEncoder.java | 8 +- .../geoserver/rest/ConfigTest.java | 49 ++-- .../geoserver/rest/GeoserverRESTTest.java | 158 +++++------ .../encoder/feature/GSFeatureEncoderTest.java | 262 ++++++++++++------ .../rest/encoder/utils/ElementUtilsTest.java | 2 +- 13 files changed, 631 insertions(+), 277 deletions(-) create mode 100644 src/main/java/it/geosolutions/geoserver/rest/encoder/feature/FeatureTypeAttribute.java create mode 100644 src/main/java/it/geosolutions/geoserver/rest/encoder/feature/GSAttributeEncoder.java diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTFeatureType.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTFeatureType.java index 38a8d6b..b5c34f5 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTFeatureType.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTFeatureType.java @@ -27,6 +27,7 @@ package it.geosolutions.geoserver.rest.decoder; import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; import it.geosolutions.geoserver.rest.decoder.utils.JDOMListIterator; + import java.util.Iterator; import org.jdom.Element; diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTResource.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTResource.java index 363f4cb..9067b49 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTResource.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTResource.java @@ -26,80 +26,171 @@ package it.geosolutions.geoserver.rest.decoder; import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; +import it.geosolutions.geoserver.rest.encoder.feature.FeatureTypeAttribute; +import it.geosolutions.geoserver.rest.encoder.feature.GSAttributeEncoder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.jdom.Element; import org.jdom.Namespace; /** * Parse a resource (FeatureType or Coverage) returned as XML REST objects. - * + * * @author etj */ public class RESTResource { - protected final Element rootElem; + protected final Element rootElem; public static RESTResource build(String response) { Element elem = JDOMBuilder.buildElement(response); - return elem == null? null : new RESTCoverage(elem); - } + return elem == null ? null : new RESTCoverage(elem); + } - public RESTResource(Element resource) { - this.rootElem = resource; - } + public RESTResource(Element resource) { + this.rootElem = resource; + } - public String getName() { - return rootElem.getChildText("name"); - } + public String getName() { + return rootElem.getChildText("name"); + } public String getTitle() { - return rootElem.getChildText("title"); - } + return rootElem.getChildText("title"); + } - public String getNativeName() { - return rootElem.getChildText("nativeName"); - } + public String getNativeName() { + return rootElem.getChildText("nativeName"); + } - public String getAbstract() { - return rootElem.getChildText("abstract"); - } + public String getAbstract() { + return rootElem.getChildText("abstract"); + } - public String getNameSpace() { - return rootElem.getChild("namespace").getChildText("name"); - } + public String getNameSpace() { + return rootElem.getChild("namespace").getChildText("name"); + } - public String getStoreName() { - return rootElem.getChild("store").getChildText("name"); - } + public String getStoreName() { + return rootElem.getChild("store").getChildText("name"); + } - public String getStoreType() { - return rootElem.getChild("store").getAttributeValue("class"); - } + public String getStoreType() { + return rootElem.getChild("store").getAttributeValue("class"); + } public String getStoreUrl() { - Element store = rootElem.getChild("store"); - Element atom = store.getChild("link", Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom")); + Element store = rootElem.getChild("store"); + Element atom = store.getChild("link", + Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom")); return atom.getAttributeValue("href"); } - public String getCRS() { - Element elBBox = rootElem.getChild("latLonBoundingBox"); - return elBBox.getChildText("crs"); - } + public String getCRS() { + Element elBBox = rootElem.getChild("latLonBoundingBox"); + return elBBox.getChildText("crs"); + } - protected double getLatLonEdge(String edge) { - Element elBBox = rootElem.getChild("latLonBoundingBox"); - return Double.parseDouble(elBBox.getChildText(edge)); - } + protected double getLatLonEdge(String edge) { + Element elBBox = rootElem.getChild("latLonBoundingBox"); + return Double.parseDouble(elBBox.getChildText(edge)); + } - public double getMinX() { - return getLatLonEdge("minx"); - } - public double getMaxX() { - return getLatLonEdge("maxx"); - } - public double getMinY() { - return getLatLonEdge("miny"); - } - public double getMaxY() { - return getLatLonEdge("maxy"); - } + public double getMinX() { + return getLatLonEdge("minx"); + } + + public double getMaxX() { + return getLatLonEdge("maxx"); + } + + public double getMinY() { + return getLatLonEdge("miny"); + } + + public double getMaxY() { + return getLatLonEdge("maxy"); + } + + public List> getAttributeList() { + List> attrsList = null; + + final Element attrsRoot = rootElem.getChild("attributes"); + final List attrs = attrsRoot.getChildren(); + if (attrs != null) { + attrsList = new ArrayList>(attrs.size()); + for (Element attr : attrs) { + Map attrsMap = new HashMap(); + attrsList.add(attrsMap); + for (FeatureTypeAttribute at : FeatureTypeAttribute.values()) { + String key = at.toString(); + attrsMap.put(at, attr.getChildText(key)); + } + } + } + return attrsList; + } + + public List getEncodedAttributeList() { + List attrsList = null; + + final Element attrsRoot = rootElem.getChild("attributes"); + final List attrs = attrsRoot.getChildren(); + if (attrs != null) { + attrsList = new ArrayList(attrs.size()); + for (Element attr : attrs) { + final GSAttributeEncoder attrEnc = new GSAttributeEncoder(); + for (FeatureTypeAttribute at : FeatureTypeAttribute.values()) { + String key = at.toString(); + attrEnc.setAttribute(at, attr.getChildText(key)); + } + attrsList.add(attrEnc); + } + + } + return attrsList; + } + + // /** + // * @return the list of available attribute names + // */ + // public List getAttributeNames() { + // return getAttributes("name"); + // } + // + // /** + // * @return a list of object which are String representation of Classes + // */ + // public List getAttributeBinding() { + // return getAttributes("binding"); + // } + + // /** + // *
+ // * NATION
+ // * 0
+ // * 1
+ // * true
+ // * java.lang.Integer
+ // * 3
+ // *

+ // * + // * @param name + // * @return + // */ + // private List getAttributes(String name) { + // final Element attrsRoot = rootElem.getChild("attributes"); + // final List attrs = attrsRoot.getChildren(); + // List attrNames = null; + // if (attrs != null) { + // attrNames = new ArrayList(attrs.size()); + // for (Element attr : attrs) { + // attrNames.add(attr.getChildText(name)); + // } + // } + // return attrNames; + // } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java index b83f717..9a474d4 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java @@ -61,20 +61,20 @@ public class GSLayerEncoder extends PropertyXMLEncoder { set("enabled","false"); } - private final static String DESCRIPTION = "description"; - /** - * Add the 'description' node with a text value from 'description' - * - */ - protected void addDescription(final String description) { - add(DESCRIPTION, description); - } - /** - * Set or modify the 'description' node with a text value from 'description' - */ - public void setDescription(final String description) { - set(DESCRIPTION, description); - } +// private final static String DESCRIPTION = "description"; +// /** +// * Add the 'description' node with a text value from 'description' +// * +// */ +// protected void addDescription(final String description) { +// add(DESCRIPTION, description); +// } +// /** +// * Set or modify the 'description' node with a text value from 'description' +// */ +// public void setDescription(final String description) { +// set(DESCRIPTION, description); +// } // queryable private final static String QUERYABLE = "queryable"; diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSResourceEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSResourceEncoder.java index ef746cf..92cb5a7 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSResourceEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSResourceEncoder.java @@ -29,6 +29,7 @@ import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder; import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder; import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder; import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; @@ -188,7 +189,7 @@ public abstract class GSResourceEncoder } public String getName() { - final Element nameNode = get(NAME); + final Element nameNode = ElementUtils.contains(getRoot(), NAME, 1); if (nameNode != null) return nameNode.getText(); else diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/FeatureTypeAttribute.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/FeatureTypeAttribute.java new file mode 100644 index 0000000..c79664d --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/FeatureTypeAttribute.java @@ -0,0 +1,32 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2012 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.encoder.feature; + +/** + * Enumeration of featureType attribute members + */ +public enum FeatureTypeAttribute { + name, minOccurs, maxOccurs, nillable, binding, length +} \ No newline at end of file diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/GSAttributeEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/GSAttributeEncoder.java new file mode 100644 index 0000000..539bc30 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/GSAttributeEncoder.java @@ -0,0 +1,92 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2012 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.encoder.feature; + +import java.util.Map; +import java.util.Map.Entry; + +import org.jdom.Element; +import org.jdom.filter.Filter; + +import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; +import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder; + +/** + * + * @author carlo cancellieri - GeoSolutions + * + */ +public class GSAttributeEncoder extends PropertyXMLEncoder { + + public static class filterByName implements Filter { + + final private String key; + + public filterByName(String keyword){ + this.key=keyword; + } + + private static final long serialVersionUID = 1L; + + public boolean matches(Object obj) { + Element el=((Element) obj).getChild(FeatureTypeAttribute.name.toString()); + if (el!=null && el.getTextTrim().equals(key)) { + return true; + } + return false; + } + } + + public static Filter getFilterByName(String name){ + return new filterByName(name); + } + + public GSAttributeEncoder() { + super("attribute"); + } + + public void setup(Map attributes){ + for (Entry attr:attributes.entrySet()){ + set(attr.getKey().toString(),attr.getValue()); + } + } + + public void setAttribute(FeatureTypeAttribute type, String value){ + set(type.toString(),value); + } + + public void delAttribute(FeatureTypeAttribute type){ + ElementUtils.remove(this.getRoot(), get(type.toString())); + } + + public String getAttribute(FeatureTypeAttribute type){ + Element el = get(type.toString()); + if (el!=null) + return el.getTextTrim(); + else + return null; + } +} + diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureTypeEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureTypeEncoder.java index a11bcb4..d55e528 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureTypeEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureTypeEncoder.java @@ -28,28 +28,64 @@ package it.geosolutions.geoserver.rest.encoder.feature; import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder; +import org.jdom.Element; + /** - * - * Encode a GeoServer resource as FeatureType - * + * + * Encode a GeoServer resource as FeatureType + * * @author ETj (etj at geo-solutions.it) * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it */ public class GSFeatureTypeEncoder extends GSResourceEncoder { + public final static String ATTRIBUTES = "attributes"; + + final private Element attributes = new Element(ATTRIBUTES); + public GSFeatureTypeEncoder() { super("featureType"); + addContent(attributes); + } + + /** + * @param key + * @param dimensionInfo + */ + protected void addMetadata(String key, GSFeatureDimensionInfoEncoder dimensionInfo) { + super.addMetadata(key, dimensionInfo); + } + + public void setMetadata(String key, GSFeatureDimensionInfoEncoder dimensionInfo) { + super.setMetadata(key, dimensionInfo); + } + + /** + * delete a keyword from the list + * + * @param keyword + * @return true if something is removed, false otherwise + */ + public boolean delAttribute(final String keyword) { + final Element el = new Element("string"); + el.setText(keyword); + return (attributes.removeContent(GSAttributeEncoder.getFilterByName(keyword))).size() == 0 ? false + : true; + } + + /** + * @param attribute the attribute to add + */ + protected void addAttribute(GSAttributeEncoder attribute) { + attributes.addContent(attribute.getRoot()); + } + + /** + * @param attribute the attribute to set (overriding an attribute with the same name if present) + */ + public void setAttribute(GSAttributeEncoder attribute) { + delAttribute(attribute.getAttribute(FeatureTypeAttribute.name)); + addAttribute(attribute); } - /** - * @param key - * @param dimensionInfo - */ - protected void addMetadata(String key, GSFeatureDimensionInfoEncoder dimensionInfo) { - super.addMetadata(key, dimensionInfo); - } - - public void setMetadata(String key, GSFeatureDimensionInfoEncoder dimensionInfo) { - super.setMetadata(key, dimensionInfo); - } } \ No newline at end of file diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/ElementUtils.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/ElementUtils.java index 5528879..b595239 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/ElementUtils.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/ElementUtils.java @@ -93,7 +93,7 @@ public abstract class ElementUtils { final Filter filter, final int depth) throws IllegalArgumentException { - if (root == null || filter == null || depth < 0) { + if (root == null || filter == null) { throw new IllegalArgumentException("Bad arguments: root=" + root + " filter=" + filter + " depth=" + depth); } @@ -106,7 +106,7 @@ public abstract class ElementUtils { ret.add(root); } // check my children - if (depth > 1) { + if (depth != 0) { final List childrenList = root.getContent(); final Iterator it = childrenList.iterator(); while (it.hasNext()) { @@ -201,6 +201,18 @@ public abstract class ElementUtils { return null; } + + /** + * return the FIRST element with name equals to the passed key + * + * @param root + * @param name + * @return + */ + public static Element contains(final Element root, final String name){ + return contains(root, name, -1); + } + /** * return the FIRST element with name equals to the passed key * @@ -208,7 +220,7 @@ public abstract class ElementUtils { * @param name * @return */ - public static Element contains(final Element root, final String name) + public static Element contains(final Element root, final String name, final int deep) throws IllegalArgumentException { if (root == null || name == null) { throw new IllegalArgumentException("Bad arguments: root=" + root @@ -228,7 +240,7 @@ public abstract class ElementUtils { return false; } }; - final Iterator it = search(root, filter).iterator(); + final Iterator it = search(root, filter, deep).iterator(); if (it.hasNext()) return it.next(); else diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/PropertyXMLEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/PropertyXMLEncoder.java index 5f121e5..ec03cc9 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/PropertyXMLEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/PropertyXMLEncoder.java @@ -57,10 +57,10 @@ public class PropertyXMLEncoder extends XmlElement { super(rootName); } - protected void get(final String key, final String value) { - - } - + protected Element get(final String key, int deep) { + return get(getRoot(), key); + } + protected Element get(final String key) { return get(getRoot(), key); } diff --git a/src/test/java/it/geosolutions/geoserver/rest/ConfigTest.java b/src/test/java/it/geosolutions/geoserver/rest/ConfigTest.java index 855d300..01d2a8a 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/ConfigTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/ConfigTest.java @@ -54,24 +54,24 @@ public class ConfigTest extends GeoserverRESTTest { private static final String DEFAULT_WS = "geosolutions"; - @Test - public void testEtj() throws FileNotFoundException, IOException { - if(!enabled()){ - LOGGER.info("Skipping test "+"testEtj"+"for class:"+this.getClass().getSimpleName()); - return; - } - deleteAll(); - - assertTrue(reader.getWorkspaces().isEmpty()); - assertTrue(publisher.createWorkspace(DEFAULT_WS)); - - insertStyles(); - insertExternalGeotiff(); - insertExternalShape(); - - boolean ok = publisher.publishDBLayer(DEFAULT_WS, "pg_kids", "easia_gaul_0_aggr", "EPSG:4326", "default_polygon"); -// assertTrue(ok); - } +// @Test +// public void testEtj() throws FileNotFoundException, IOException { +// if(!enabled()){ +// LOGGER.info("Skipping test "+"testEtj"+"for class:"+this.getClass().getSimpleName()); +// return; +// } +// deleteAll(); +// +// assertTrue(reader.getWorkspaces().isEmpty()); +// assertTrue(publisher.createWorkspace(DEFAULT_WS)); +// +// insertStyles(); +// insertExternalGeotiff(); +// insertExternalShape(); +// +// boolean ok = publisher.publishDBLayer(DEFAULT_WS, "pg_kids", "easia_gaul_0_aggr", "EPSG:4326", "default_polygon"); +//// assertTrue(ok); +// } @Test public void insertStyles() throws FileNotFoundException, IOException { @@ -79,6 +79,8 @@ public class ConfigTest extends GeoserverRESTTest { LOGGER.info("Skipping test "+"insertStyles"+"for class:"+this.getClass().getSimpleName()); return; } + deleteAllStyles(); + File sldDir = new ClassPathResource("testdata").getFile(); for(File sldFile : sldDir.listFiles((FilenameFilter)new SuffixFileFilter(".sld"))) { LOGGER.info("Existing styles: " + reader.getStyles().getNames()); @@ -94,20 +96,31 @@ public class ConfigTest extends GeoserverRESTTest { LOGGER.info("Skipping test "+"insertExternalGeotiff"+"for class:"+this.getClass().getSimpleName()); return; } + deleteAll(); + String storeName = "testRESTStoreGeotiff"; String layerName = "resttestdem"; + publisher.createWorkspace(DEFAULT_WS); + publisher.publishStyle(new File(new ClassPathResource("testdata").getFile(),"raster.sld")); + File geotiff = new ClassPathResource("testdata/resttestdem.tif").getFile(); boolean pc = publisher.publishExternalGeoTIFF(DEFAULT_WS, storeName, geotiff, layerName,"EPSG:4326",ProjectionPolicy.FORCE_DECLARED,"raster"); assertTrue(pc); } + @Test public void insertExternalShape() throws FileNotFoundException, IOException { if(!enabled()){ LOGGER.info("Skipping test "+"insertExternalShape"+"for class:"+this.getClass().getSimpleName()); return; } + deleteAll(); + + publisher.createWorkspace(DEFAULT_WS); + publisher.publishStyle(new File(new ClassPathResource("testdata").getFile(),"default_point.sld")); + File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); boolean published = publisher.publishShp(DEFAULT_WS, "anyname", "cities", zipFile, "EPSG:41001", "default_point"); diff --git a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java index c533770..1d3902e 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java @@ -37,8 +37,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.List; -import junit.framework.TestCase; - import org.junit.Assert; import org.junit.BeforeClass; import org.slf4j.Logger; @@ -47,76 +45,84 @@ import org.slf4j.LoggerFactory; /** * Initializes REST params. *

- * These tests are destructive, so you have to explicitly enable them - * by setting the env var resttest to true. + * These tests are destructive, so you have to explicitly enable them by setting the env var resttest to true. *

- * The target geoserver instance can be customized by defining the following env vars:

    + * The target geoserver instance can be customized by defining the following env vars: + *
      *
    • resturl (default http://localhost:8080/geoserver)
    • *
    • restuser (default: admin)
    • *
    • restpw (default: geoserver)
    • *
    - * + * * @author etj + * @author carlo cancellieri - GeoSolutions */ public abstract class GeoserverRESTTest extends Assert { private final static Logger LOGGER = LoggerFactory.getLogger(GeoserverRESTTest.class); - protected static final String DEFAULT_WS = "geosolutions"; + public static final String DEFAULT_WS = "geosolutions"; - public static final String RESTURL; - public static final String RESTUSER; - public static final String RESTPW; + public static final String RESTURL; - public static URL URL; - public static GeoServerRESTManager manager; - public static GeoServerRESTReader reader; - public static GeoServerRESTPublisher publisher; + public static final String RESTUSER; + + public static final String RESTPW; + + public static URL URL; + + public static GeoServerRESTManager manager; + + public static GeoServerRESTReader reader; + + public static GeoServerRESTPublisher publisher; private static boolean enabled = false; + private static Boolean existgs = null; - - static { - RESTURL = getenv("gsmgr_resturl", "http://localhost:8080/geoserver"); + + static { + RESTURL = getenv("gsmgr_resturl", "http://localhost:8080/geoserver"); RESTUSER = getenv("gsmgr_restuser", "admin"); - RESTPW = getenv("gsmgr_restpw", "geoserver"); - + RESTPW = getenv("gsmgr_restpw", "geoserver"); + // These tests will destroy data, so let's make sure we do want to run them - enabled = getenv("gsmgr_resttest", "false").equalsIgnoreCase("true"); - if( ! enabled ) + enabled = getenv("gsmgr_resttest", "false").equalsIgnoreCase("true"); + if (!enabled) LOGGER.warn("Tests are disabled. Please read the documentation to enable them."); try { - URL = new URL(RESTURL); - manager = new GeoServerRESTManager(URL, RESTUSER, RESTPW); - reader = manager.getReader(); - publisher = manager.getPublisher(); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - } + URL = new URL(RESTURL); + manager = new GeoServerRESTManager(URL, RESTUSER, RESTPW); + reader = manager.getReader(); + publisher = manager.getPublisher(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } private static String getenv(String envName, String envDefault) { String env = System.getenv(envName); String ret = System.getProperty(envName, env); LOGGER.debug("env var " + envName + " is " + ret); - return ret != null? ret : envDefault; + return ret != null ? ret : envDefault; } @BeforeClass public static void setUp() throws Exception { - - if(enabled) { - if(existgs == null) { + if (enabled) { + if (existgs == null) { existgs = reader.existGeoserver(); - if ( ! existgs ) { - LOGGER.error("TESTS WILL FAIL BECAUSE NO GEOSERVER WAS FOUND AT " + RESTURL + " ("+ RESTUSER+":"+RESTPW+")"); + if (!existgs) { + LOGGER.error("TESTS WILL FAIL BECAUSE NO GEOSERVER WAS FOUND AT " + RESTURL + + " (" + RESTUSER + ":" + RESTPW + ")"); } else { - LOGGER.info("Using geoserver instance " + RESTUSER+":"+RESTPW+ " @ " + RESTURL); + LOGGER.info("Using geoserver instance " + RESTUSER + ":" + RESTPW + " @ " + + RESTURL); } } - if ( ! existgs ) { + if (!existgs) { System.out.println("Failing tests : geoserver not found"); fail("GeoServer not found"); } @@ -141,7 +147,7 @@ public abstract class GeoserverRESTTest extends Assert { deleteAllDataStores(); deleteAllWorkspaces(); -// assertTrue("Some workspaces were not removed", reader.getWorkspaces().isEmpty()); + // assertTrue("Some workspaces were not removed", reader.getWorkspaces().isEmpty()); deleteAllStyles(); assertTrue("Some styles were not removed", reader.getStyles().isEmpty()); @@ -160,7 +166,7 @@ public abstract class GeoserverRESTTest extends Assert { } boolean removed = publisher.removeLayerGroup(groupName); - LOGGER.info(sb.toString()+ ": removed: " + removed); + LOGGER.info(sb.toString() + ": removed: " + removed); assertTrue("LayerGroup not removed: " + groupName, removed); } @@ -170,9 +176,9 @@ public abstract class GeoserverRESTTest extends Assert { List layers = reader.getLayers().getNames(); for (String layerName : layers) { RESTLayer layer = reader.getLayer(layerName); - if(layer.getType() == RESTLayer.Type.VECTOR) + if (layer.getType() == RESTLayer.Type.VECTOR) deleteFeatureType(layer); - else if(layer.getType() == RESTLayer.Type.RASTER) + else if (layer.getType() == RESTLayer.Type.RASTER) deleteCoverage(layer); else LOGGER.error("Unknown layer type " + layer.getType()); @@ -186,7 +192,7 @@ public abstract class GeoserverRESTTest extends Assert { List stores = reader.getCoverageStores(workspace).getNames(); for (String storename : stores) { -// RESTCoverageStore store = reader.getCoverageStore(workspace, storename); + // RESTCoverageStore store = reader.getCoverageStore(workspace, storename); LOGGER.warn("Deleting CoverageStore " + workspace + " : " + storename); boolean removed = publisher.removeCoverageStore(workspace, storename, false); @@ -201,12 +207,12 @@ public abstract class GeoserverRESTTest extends Assert { List stores = reader.getDatastores(workspace).getNames(); for (String storename : stores) { -// RESTDataStore store = reader.getDatastore(workspace, storename); + // RESTDataStore store = reader.getDatastore(workspace, storename); -// if(store.getType() == RESTDataStore.DBType.POSTGIS) { -// LOGGER.info("Skipping PG datastore " + store.getWorkspaceName()+":"+store.getName()); -// continue; -// } + // if(store.getType() == RESTDataStore.DBType.POSTGIS) { + // LOGGER.info("Skipping PG datastore " + store.getWorkspaceName()+":"+store.getName()); + // continue; + // } LOGGER.warn("Deleting DataStore " + workspace + " : " + storename); boolean removed = publisher.removeDatastore(workspace, storename, false); @@ -214,13 +220,13 @@ public abstract class GeoserverRESTTest extends Assert { } } } - + protected void deleteAllWorkspacesRecursively() { List workspaces = reader.getWorkspaceNames(); for (String workspace : workspaces) { - LOGGER.warn("Deleting Workspace " + workspace ); - boolean removed = publisher.removeWorkspace(workspace,true); - assertTrue("Workspace not removed " + workspace, removed ); + LOGGER.warn("Deleting Workspace " + workspace); + boolean removed = publisher.removeWorkspace(workspace, true); + assertTrue("Workspace not removed " + workspace, removed); } } @@ -228,19 +234,19 @@ public abstract class GeoserverRESTTest extends Assert { protected void deleteAllWorkspaces() { List workspaces = reader.getWorkspaceNames(); for (String workspace : workspaces) { - LOGGER.warn("Deleting Workspace " + workspace ); - boolean removed = publisher.removeWorkspace(workspace,true); - assertTrue("Workspace not removed " + workspace, removed ); + LOGGER.warn("Deleting Workspace " + workspace); + boolean removed = publisher.removeWorkspace(workspace, true); + assertTrue("Workspace not removed " + workspace, removed); } } - private void deleteAllStyles() { + protected void deleteAllStyles() { List styles = reader.getStyles().getNames(); for (String style : styles) { - LOGGER.warn("Deleting Style " + style ); - boolean removed = publisher.removeStyle(style); - assertTrue("Style not removed " + style, removed ); + LOGGER.warn("Deleting Style " + style); + boolean removed = publisher.removeStyle(style,true); + assertTrue("Style not removed " + style, removed); } } @@ -249,18 +255,14 @@ public abstract class GeoserverRESTTest extends Assert { RESTFeatureType featureType = reader.getFeatureType(layer); RESTDataStore datastore = reader.getDatastore(featureType); - LOGGER.warn("Deleting FeatureType" - + datastore.getWorkspaceName() + " : " - + datastore.getName() + " / " - + featureType.getName() - ); + LOGGER.warn("Deleting FeatureType" + datastore.getWorkspaceName() + " : " + + datastore.getName() + " / " + featureType.getName()); - boolean removed = publisher.unpublishFeatureType(datastore.getWorkspaceName(), datastore.getName(), layer.getName()); - assertTrue("FeatureType not removed:" - + datastore.getWorkspaceName() + " : " - + datastore.getName() + " / " - + featureType.getName(), - removed); + boolean removed = publisher.unpublishFeatureType(datastore.getWorkspaceName(), + datastore.getName(), layer.getName()); + assertTrue( + "FeatureType not removed:" + datastore.getWorkspaceName() + " : " + + datastore.getName() + " / " + featureType.getName(), removed); } @@ -268,21 +270,15 @@ public abstract class GeoserverRESTTest extends Assert { RESTCoverage coverage = reader.getCoverage(layer); RESTCoverageStore coverageStore = reader.getCoverageStore(coverage); - LOGGER.warn("Deleting Coverage " - + coverageStore.getWorkspaceName() + " : " - + coverageStore.getName() + " / " - + coverage.getName()); + LOGGER.warn("Deleting Coverage " + coverageStore.getWorkspaceName() + " : " + + coverageStore.getName() + " / " + coverage.getName()); boolean removed = publisher.unpublishCoverage(coverageStore.getWorkspaceName(), - coverageStore.getName(), - coverage.getName()); - assertTrue("Coverage not deleted " - + coverageStore.getWorkspaceName() + " : " - + coverageStore.getName() + " / " - + coverage.getName(), - removed); + coverageStore.getName(), coverage.getName()); + assertTrue("Coverage not deleted " + coverageStore.getWorkspaceName() + " : " + + coverageStore.getName() + " / " + coverage.getName(), removed); } - + protected boolean existsLayer(String layername) { return reader.getLayer(layername) != null; } diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java index 9e71009..342929e 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java @@ -19,123 +19,203 @@ */ package it.geosolutions.geoserver.rest.encoder.feature; +import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; +import it.geosolutions.geoserver.rest.decoder.RESTLayer; +import it.geosolutions.geoserver.rest.decoder.RESTResource; +import it.geosolutions.geoserver.rest.encoder.GSAbstractStoreEncoder; +import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder; +import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.Presentation; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.PresentationDiscrete; import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder; import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; +import it.geosolutions.geoserver.rest.publisher.GeoserverRESTPublisherTest; +import java.io.File; +import java.io.IOException; import java.math.BigDecimal; - -import junit.framework.TestCase; +import java.security.GeneralSecurityException; +import java.util.List; +import java.util.Map; import org.jdom.Element; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.io.ClassPathResource; /** * * @author ETj (etj at geo-solutions.it) * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it */ -public class GSFeatureEncoderTest extends TestCase { - protected final static Logger LOGGER = LoggerFactory.getLogger(GSFeatureEncoderTest.class); +public class GSFeatureEncoderTest extends GeoserverRESTPublisherTest { + protected final static Logger LOGGER = LoggerFactory.getLogger(GSFeatureEncoderTest.class); - @Test - public void testAll() { - - GSFeatureTypeEncoder encoder = new GSFeatureTypeEncoder(); - encoder.addKeyword("KEYWORD_1"); - encoder.addKeyword("KEYWORD_2"); - encoder.addKeyword("..."); - encoder.addKeyword("KEYWORD_N"); - - GSFeatureDimensionInfoEncoder dim2 = new GSFeatureDimensionInfoEncoder("ELE"); + @Test + public void testIntegration() throws IOException { - if (LOGGER.isInfoEnabled()) - LOGGER.info(encoder.toString()); + if (!enabled()) + return; + deleteAll(); - encoder.addMetadata("elevation", dim2); - - dim2.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL, - BigDecimal.valueOf(10)); - - Element el=ElementUtils.contains(encoder.getRoot(),GSDimensionInfoEncoder.PRESENTATION); - Assert.assertNotNull(el); - - - LOGGER.info("contains_key:"+el.toString()); - - dim2.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL, - BigDecimal.valueOf(12)); - - el=ElementUtils.contains(encoder.getRoot(),GSDimensionInfoEncoder.RESOLUTION); - Assert.assertNotNull(el); - Assert.assertEquals("12", el.getText()); - - dim2.setPresentation(Presentation.CONTINUOUS_INTERVAL); - - - - encoder.setMetadata("time", new GSFeatureDimensionInfoEncoder("time")); - - el=ElementUtils.contains(encoder.getRoot(),GSDimensionInfoEncoder.PRESENTATION); - Assert.assertNotNull(el); - el=ElementUtils.contains(encoder.getRoot(),GSDimensionInfoEncoder.RESOLUTION); - Assert.assertNull(el); + GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(RESTURL, RESTUSER, RESTPW); - el=ElementUtils.contains(encoder.getRoot(),GSResourceEncoder.METADATA); - Assert.assertNotNull(el); - LOGGER.info("contains_key:"+el.toString()); - - final boolean removed=ElementUtils.remove(encoder.getRoot(),el); - LOGGER.info("remove:"+removed); - Assert.assertTrue(removed); - - el=ElementUtils.contains(encoder.getRoot(),"metadata"); - Assert.assertNull(el); - if (el==null) - LOGGER.info("REMOVED"); + String storeName = "resttestshp"; + String layerName = "cities"; - } - - @Test - public void testModifyFeature() { - GSFeatureTypeEncoder encoder = new GSFeatureTypeEncoder(); - encoder.addKeyword("KEYWORD_1"); - encoder.addKeyword("KEYWORD_2"); - encoder.addKeyword("..."); - encoder.addKeyword("KEYWORD_N"); - - Assert.assertTrue(encoder.delKeyword("KEYWORD_2")); - Assert.assertFalse(encoder.delKeyword("KEYWORD_M")); - - final GSFeatureDimensionInfoEncoder elevationDimension = new GSFeatureDimensionInfoEncoder("elevation_field"); + GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder(); + fte.setName(layerName + "_NEW"); + fte.setTitle("title"); + // fte.addKeyword("TODO"); + fte.setNativeCRS("EPSG:4326"); + fte.setDescription("desc"); + fte.setEnabled(true); -// if (LOGGER.isInfoEnabled()) -// LOGGER.info(encoder.toString()); + GSLayerEncoder layerEncoder = new GSLayerEncoder(); + layerEncoder.setEnabled(true); + layerEncoder.setQueryable(true); - final String metadata="elevation"; - encoder.setMetadata(metadata, elevationDimension); - - elevationDimension.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL, - BigDecimal.valueOf(10)); - - if (LOGGER.isInfoEnabled()) - LOGGER.info(encoder.toString()); - - Assert.assertTrue(encoder.delMetadata(metadata)); - - if (LOGGER.isInfoEnabled()) - LOGGER.info(encoder.toString()); - - final Element el=ElementUtils.contains(encoder.getRoot(),GSDimensionInfoEncoder.DIMENSIONINFO); - Assert.assertNull(el); - if (el==null) - LOGGER.info("REMOVED"); + layerEncoder.setDefaultStyle("point"); - } + publisher.createWorkspace(DEFAULT_WS); + + 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)); + + publisher.publishStyle(new File(new ClassPathResource("testdata").getFile(), + "default_point.sld")); + + // optionally select the attributes to publish + RESTLayer layer = reader.getLayer(layerName); + RESTResource resource = reader.getResource(layer); + List attrs = resource.getEncodedAttributeList(); + assertNotNull(attrs); + for (GSAttributeEncoder enc : attrs) { + fte.setAttribute(enc); + } + + assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); + } + + @Test + public void testFeatureTypeEncoder() { + + GSFeatureTypeEncoder encoder = new GSFeatureTypeEncoder(); + encoder.addKeyword("KEYWORD_1"); + encoder.addKeyword("KEYWORD_2"); + encoder.addKeyword("..."); + encoder.addKeyword("KEYWORD_N"); + + encoder.setName("Layername"); + + encoder.setTitle("title"); + encoder.addKeyword("TODO"); + encoder.setNativeCRS("EPSG:4326"); + encoder.setDescription("desc"); + encoder.setEnabled(true); + + GSAttributeEncoder attribute = new GSAttributeEncoder(); + attribute.setAttribute(FeatureTypeAttribute.name, "NAME"); + attribute.setAttribute(FeatureTypeAttribute.binding, "java.lang.String"); + attribute.setAttribute(FeatureTypeAttribute.maxOccurs, "1"); + attribute.setAttribute(FeatureTypeAttribute.minOccurs, "0"); + attribute.setAttribute(FeatureTypeAttribute.nillable, "true"); + + encoder.setAttribute(attribute); + + encoder.delAttribute("NAME"); + + attribute.setAttribute(FeatureTypeAttribute.name, "NEW_NAME"); + + encoder.setAttribute(attribute); + + // TODO encoder.getAttribute("NAME"); + + GSFeatureDimensionInfoEncoder dim2 = new GSFeatureDimensionInfoEncoder("ELE"); + + encoder.addMetadata("elevation", dim2); + dim2.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL, BigDecimal.valueOf(10)); + Element el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.PRESENTATION); + Assert.assertNotNull(el); + + LOGGER.info("contains_key:" + el.toString()); + + dim2.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL, BigDecimal.valueOf(12)); + el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.RESOLUTION); + Assert.assertNotNull(el); + Assert.assertEquals("12", el.getText()); + + dim2.setPresentation(Presentation.CONTINUOUS_INTERVAL); + + encoder.setMetadata("time", new GSFeatureDimensionInfoEncoder("time")); + el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.PRESENTATION); + Assert.assertNotNull(el); + el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.RESOLUTION); + Assert.assertNull(el); + + el = ElementUtils.contains(encoder.getRoot(), GSResourceEncoder.METADATA); + Assert.assertNotNull(el); + LOGGER.info("contains_key:" + el.toString()); + + final boolean removed = ElementUtils.remove(encoder.getRoot(), el); + LOGGER.info("remove:" + removed); + Assert.assertTrue(removed); + + el = ElementUtils.contains(encoder.getRoot(), "metadata"); + Assert.assertNull(el); + if (el == null) + LOGGER.info("REMOVED"); + + if (LOGGER.isInfoEnabled()) + LOGGER.info(encoder.toString()); + + assertEquals(encoder.getName(),"Layername"); + } + + @Test + public void testModifyFeature() { + GSFeatureTypeEncoder encoder = new GSFeatureTypeEncoder(); + encoder.addKeyword("KEYWORD_1"); + encoder.addKeyword("KEYWORD_2"); + encoder.addKeyword("..."); + encoder.addKeyword("KEYWORD_N"); + + Assert.assertTrue(encoder.delKeyword("KEYWORD_2")); + Assert.assertFalse(encoder.delKeyword("KEYWORD_M")); + + final GSFeatureDimensionInfoEncoder elevationDimension = new GSFeatureDimensionInfoEncoder( + "elevation_field"); + + // if (LOGGER.isInfoEnabled()) + // LOGGER.info(encoder.toString()); + + final String metadata = "elevation"; + encoder.setMetadata(metadata, elevationDimension); + + elevationDimension.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL, + BigDecimal.valueOf(10)); + + if (LOGGER.isInfoEnabled()) + LOGGER.info(encoder.toString()); + + Assert.assertTrue(encoder.delMetadata(metadata)); + + if (LOGGER.isInfoEnabled()) + LOGGER.info(encoder.toString()); + + final Element el = ElementUtils.contains(encoder.getRoot(), + GSDimensionInfoEncoder.DIMENSIONINFO); + Assert.assertNull(el); + if (el == null) + LOGGER.info("REMOVED"); + + } } diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/ElementUtilsTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/ElementUtilsTest.java index e037deb..e352688 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/ElementUtilsTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/ElementUtilsTest.java @@ -63,7 +63,7 @@ public class ElementUtilsTest { public void containsFilterDepthTest(){ LOGGER.info("STARTING-> containsFilterDepthTest"); - final List list=ElementUtils.search(this.root, filter, 1); + final List list=ElementUtils.search(this.root, filter, 0); Assert.assertEquals(1,list.size()); final List list2=ElementUtils.search(this.root, filter, 6);