Merge pull request #46 from geosolutions-it/master-45-featureTypeEncoder-attributes

Master 45 feature type encoder attributes
This commit is contained in:
Damiano 2013-01-10 03:12:18 -08:00
commit 852cabbd8c
13 changed files with 627 additions and 277 deletions

View File

@ -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.JDOMBuilder;
import it.geosolutions.geoserver.rest.decoder.utils.JDOMListIterator; import it.geosolutions.geoserver.rest.decoder.utils.JDOMListIterator;
import java.util.Iterator; import java.util.Iterator;
import org.jdom.Element; import org.jdom.Element;

View File

@ -26,6 +26,14 @@
package it.geosolutions.geoserver.rest.decoder; package it.geosolutions.geoserver.rest.decoder;
import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; 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.Element;
import org.jdom.Namespace; import org.jdom.Namespace;
@ -76,7 +84,8 @@ public class RESTResource {
public String getStoreUrl() { public String getStoreUrl() {
Element store = rootElem.getChild("store"); Element store = rootElem.getChild("store");
Element atom = store.getChild("link", Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom")); Element atom = store.getChild("link",
Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom"));
return atom.getAttributeValue("href"); return atom.getAttributeValue("href");
} }
@ -93,13 +102,95 @@ public class RESTResource {
public double getMinX() { public double getMinX() {
return getLatLonEdge("minx"); return getLatLonEdge("minx");
} }
public double getMaxX() { public double getMaxX() {
return getLatLonEdge("maxx"); return getLatLonEdge("maxx");
} }
public double getMinY() { public double getMinY() {
return getLatLonEdge("miny"); return getLatLonEdge("miny");
} }
public double getMaxY() { public double getMaxY() {
return getLatLonEdge("maxy"); return getLatLonEdge("maxy");
} }
public List<Map<FeatureTypeAttribute, String>> getAttributeList() {
List<Map<FeatureTypeAttribute, String>> attrsList = null;
final Element attrsRoot = rootElem.getChild("attributes");
final List<Element> attrs = attrsRoot.getChildren();
if (attrs != null) {
attrsList = new ArrayList<Map<FeatureTypeAttribute, String>>(attrs.size());
for (Element attr : attrs) {
Map<FeatureTypeAttribute, String> attrsMap = new HashMap<FeatureTypeAttribute, String>();
attrsList.add(attrsMap);
for (FeatureTypeAttribute at : FeatureTypeAttribute.values()) {
String key = at.toString();
attrsMap.put(at, attr.getChildText(key));
}
}
}
return attrsList;
}
public List<GSAttributeEncoder> getEncodedAttributeList() {
List<GSAttributeEncoder> attrsList = null;
final Element attrsRoot = rootElem.getChild("attributes");
final List<Element> attrs = attrsRoot.getChildren();
if (attrs != null) {
attrsList = new ArrayList<GSAttributeEncoder>(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<String> getAttributeNames() {
// return getAttributes("name");
// }
//
// /**
// * @return a list of object which are String representation of Classes
// */
// public List<String> getAttributeBinding() {
// return getAttributes("binding");
// }
// /**
// * <attribute><br>
// * <name>NATION</name><br>
// * <minOccurs>0</minOccurs><br>
// * <maxOccurs>1</maxOccurs><br>
// * <nillable>true</nillable><br>
// * <binding>java.lang.Integer</binding><br>
// * <length>3</length><br>
// * </attribute><br>
// *
// * @param name
// * @return
// */
// private List<String> getAttributes(String name) {
// final Element attrsRoot = rootElem.getChild("attributes");
// final List<Element> attrs = attrsRoot.getChildren();
// List<String> attrNames = null;
// if (attrs != null) {
// attrNames = new ArrayList<String>(attrs.size());
// for (Element attr : attrs) {
// attrNames.add(attr.getChildText(name));
// }
// }
// return attrNames;
// }
} }

View File

@ -61,20 +61,20 @@ public class GSLayerEncoder extends PropertyXMLEncoder {
set("enabled","false"); set("enabled","false");
} }
private final static String DESCRIPTION = "description"; // private final static String DESCRIPTION = "description";
/** // /**
* Add the 'description' node with a text value from 'description' // * Add the 'description' node with a text value from 'description'
* // *
*/ // */
protected void addDescription(final String description) { // protected void addDescription(final String description) {
add(DESCRIPTION, description); // add(DESCRIPTION, description);
} // }
/** // /**
* Set or modify the 'description' node with a text value from 'description' // * Set or modify the 'description' node with a text value from 'description'
*/ // */
public void setDescription(final String description) { // public void setDescription(final String description) {
set(DESCRIPTION, description); // set(DESCRIPTION, description);
} // }
// queryable // queryable
private final static String QUERYABLE = "queryable"; private final static String QUERYABLE = "queryable";

View File

@ -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.feature.GSFeatureTypeEncoder;
import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder; 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.NestedElementEncoder;
import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder; import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; import it.geosolutions.geoserver.rest.encoder.utils.XmlElement;
@ -188,7 +189,7 @@ public abstract class GSResourceEncoder
} }
public String getName() { public String getName() {
final Element nameNode = get(NAME); final Element nameNode = ElementUtils.contains(getRoot(), NAME, 1);
if (nameNode != null) if (nameNode != null)
return nameNode.getText(); return nameNode.getText();
else else

View File

@ -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
}

View File

@ -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<FeatureTypeAttribute, String> attributes){
for (Entry<FeatureTypeAttribute,String> 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;
}
}

View File

@ -28,6 +28,8 @@ package it.geosolutions.geoserver.rest.encoder.feature;
import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder; import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder;
import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder;
import org.jdom.Element;
/** /**
* *
* Encode a GeoServer resource as FeatureType * Encode a GeoServer resource as FeatureType
@ -37,8 +39,13 @@ import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEnc
*/ */
public class GSFeatureTypeEncoder extends GSResourceEncoder { public class GSFeatureTypeEncoder extends GSResourceEncoder {
public final static String ATTRIBUTES = "attributes";
final private Element attributes = new Element(ATTRIBUTES);
public GSFeatureTypeEncoder() { public GSFeatureTypeEncoder() {
super("featureType"); super("featureType");
addContent(attributes);
} }
/** /**
@ -52,4 +59,33 @@ public class GSFeatureTypeEncoder extends GSResourceEncoder {
public void setMetadata(String key, GSFeatureDimensionInfoEncoder dimensionInfo) { public void setMetadata(String key, GSFeatureDimensionInfoEncoder dimensionInfo) {
super.setMetadata(key, 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);
}
} }

View File

@ -93,7 +93,7 @@ public abstract class ElementUtils {
final Filter filter, final int depth) final Filter filter, final int depth)
throws IllegalArgumentException { throws IllegalArgumentException {
if (root == null || filter == null || depth < 0) { if (root == null || filter == null) {
throw new IllegalArgumentException("Bad arguments: root=" + root throw new IllegalArgumentException("Bad arguments: root=" + root
+ " filter=" + filter + " depth=" + depth); + " filter=" + filter + " depth=" + depth);
} }
@ -106,7 +106,7 @@ public abstract class ElementUtils {
ret.add(root); ret.add(root);
} }
// check my children // check my children
if (depth > 1) { if (depth != 0) {
final List<?> childrenList = root.getContent(); final List<?> childrenList = root.getContent();
final Iterator<?> it = childrenList.iterator(); final Iterator<?> it = childrenList.iterator();
while (it.hasNext()) { while (it.hasNext()) {
@ -201,6 +201,7 @@ public abstract class ElementUtils {
return null; return null;
} }
/** /**
* return the FIRST element with name equals to the passed key * return the FIRST element with name equals to the passed key
* *
@ -208,7 +209,18 @@ public abstract class ElementUtils {
* @param name * @param name
* @return * @return
*/ */
public static Element contains(final Element root, final String name) 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
*
* @param root
* @param name
* @return
*/
public static Element contains(final Element root, final String name, final int deep)
throws IllegalArgumentException { throws IllegalArgumentException {
if (root == null || name == null) { if (root == null || name == null) {
throw new IllegalArgumentException("Bad arguments: root=" + root throw new IllegalArgumentException("Bad arguments: root=" + root
@ -228,7 +240,7 @@ public abstract class ElementUtils {
return false; return false;
} }
}; };
final Iterator<Element> it = search(root, filter).iterator(); final Iterator<Element> it = search(root, filter, deep).iterator();
if (it.hasNext()) if (it.hasNext())
return it.next(); return it.next();
else else

View File

@ -57,8 +57,8 @@ public class PropertyXMLEncoder extends XmlElement {
super(rootName); 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) { protected Element get(final String key) {

View File

@ -54,24 +54,24 @@ public class ConfigTest extends GeoserverRESTTest {
private static final String DEFAULT_WS = "geosolutions"; private static final String DEFAULT_WS = "geosolutions";
@Test // @Test
public void testEtj() throws FileNotFoundException, IOException { // public void testEtj() throws FileNotFoundException, IOException {
if(!enabled()){ // if(!enabled()){
LOGGER.info("Skipping test "+"testEtj"+"for class:"+this.getClass().getSimpleName()); // LOGGER.info("Skipping test "+"testEtj"+"for class:"+this.getClass().getSimpleName());
return; // return;
} // }
deleteAll(); // deleteAll();
//
assertTrue(reader.getWorkspaces().isEmpty()); // assertTrue(reader.getWorkspaces().isEmpty());
assertTrue(publisher.createWorkspace(DEFAULT_WS)); // assertTrue(publisher.createWorkspace(DEFAULT_WS));
//
insertStyles(); // insertStyles();
insertExternalGeotiff(); // insertExternalGeotiff();
insertExternalShape(); // insertExternalShape();
//
boolean ok = publisher.publishDBLayer(DEFAULT_WS, "pg_kids", "easia_gaul_0_aggr", "EPSG:4326", "default_polygon"); // boolean ok = publisher.publishDBLayer(DEFAULT_WS, "pg_kids", "easia_gaul_0_aggr", "EPSG:4326", "default_polygon");
// assertTrue(ok); //// assertTrue(ok);
} // }
@Test @Test
public void insertStyles() throws FileNotFoundException, IOException { 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()); LOGGER.info("Skipping test "+"insertStyles"+"for class:"+this.getClass().getSimpleName());
return; return;
} }
deleteAllStyles();
File sldDir = new ClassPathResource("testdata").getFile(); File sldDir = new ClassPathResource("testdata").getFile();
for(File sldFile : sldDir.listFiles((FilenameFilter)new SuffixFileFilter(".sld"))) { for(File sldFile : sldDir.listFiles((FilenameFilter)new SuffixFileFilter(".sld"))) {
LOGGER.info("Existing styles: " + reader.getStyles().getNames()); 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()); LOGGER.info("Skipping test "+"insertExternalGeotiff"+"for class:"+this.getClass().getSimpleName());
return; return;
} }
deleteAll();
String storeName = "testRESTStoreGeotiff"; String storeName = "testRESTStoreGeotiff";
String layerName = "resttestdem"; 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(); File geotiff = new ClassPathResource("testdata/resttestdem.tif").getFile();
boolean pc = publisher.publishExternalGeoTIFF(DEFAULT_WS, storeName, geotiff, layerName,"EPSG:4326",ProjectionPolicy.FORCE_DECLARED,"raster"); boolean pc = publisher.publishExternalGeoTIFF(DEFAULT_WS, storeName, geotiff, layerName,"EPSG:4326",ProjectionPolicy.FORCE_DECLARED,"raster");
assertTrue(pc); assertTrue(pc);
} }
@Test @Test
public void insertExternalShape() throws FileNotFoundException, IOException { public void insertExternalShape() throws FileNotFoundException, IOException {
if(!enabled()){ if(!enabled()){
LOGGER.info("Skipping test "+"insertExternalShape"+"for class:"+this.getClass().getSimpleName()); LOGGER.info("Skipping test "+"insertExternalShape"+"for class:"+this.getClass().getSimpleName());
return; 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(); File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile();
boolean published = publisher.publishShp(DEFAULT_WS, "anyname", "cities", zipFile, "EPSG:41001", "default_point"); boolean published = publisher.publishShp(DEFAULT_WS, "anyname", "cities", zipFile, "EPSG:41001", "default_point");

View File

@ -37,8 +37,6 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import junit.framework.TestCase;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -47,32 +45,39 @@ import org.slf4j.LoggerFactory;
/** /**
* Initializes REST params. * Initializes REST params.
* <P> * <P>
* <B>These tests are destructive, so you have to explicitly enable them</B> * <B>These tests are destructive, so you have to explicitly enable them</B> by setting the env var <TT>resttest</TT> to <TT>true</TT>.
* by setting the env var <TT>resttest</TT> to <TT>true</TT>.
* <P> * <P>
* The target geoserver instance can be customized by defining the following env vars: <ul> * The target geoserver instance can be customized by defining the following env vars:
* <ul>
* <LI><TT>resturl</TT> (default <TT>http://localhost:8080/geoserver</TT>)</LI> * <LI><TT>resturl</TT> (default <TT>http://localhost:8080/geoserver</TT>)</LI>
* <LI><TT>restuser</TT> (default: <TT>admin</TT>)</LI> * <LI><TT>restuser</TT> (default: <TT>admin</TT>)</LI>
* <LI><TT>restpw</TT> (default: <TT>geoserver</TT>)</LI> * <LI><TT>restpw</TT> (default: <TT>geoserver</TT>)</LI>
* </ul> * </ul>
* *
* @author etj * @author etj
* @author carlo cancellieri - GeoSolutions
*/ */
public abstract class GeoserverRESTTest extends Assert { public abstract class GeoserverRESTTest extends Assert {
private final static Logger LOGGER = LoggerFactory.getLogger(GeoserverRESTTest.class); 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 RESTURL;
public static final String RESTUSER; public static final String RESTUSER;
public static final String RESTPW; public static final String RESTPW;
public static URL URL; public static URL URL;
public static GeoServerRESTManager manager; public static GeoServerRESTManager manager;
public static GeoServerRESTReader reader; public static GeoServerRESTReader reader;
public static GeoServerRESTPublisher publisher; public static GeoServerRESTPublisher publisher;
private static boolean enabled = false; private static boolean enabled = false;
private static Boolean existgs = null; private static Boolean existgs = null;
static { static {
@ -105,14 +110,15 @@ public abstract class GeoserverRESTTest extends Assert {
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
if (enabled) { if (enabled) {
if (existgs == null) { if (existgs == null) {
existgs = reader.existGeoserver(); existgs = reader.existGeoserver();
if (!existgs) { if (!existgs) {
LOGGER.error("TESTS WILL FAIL BECAUSE NO GEOSERVER WAS FOUND AT " + RESTURL + " ("+ RESTUSER+":"+RESTPW+")"); LOGGER.error("TESTS WILL FAIL BECAUSE NO GEOSERVER WAS FOUND AT " + RESTURL
+ " (" + RESTUSER + ":" + RESTPW + ")");
} else { } else {
LOGGER.info("Using geoserver instance " + RESTUSER+":"+RESTPW+ " @ " + RESTURL); LOGGER.info("Using geoserver instance " + RESTUSER + ":" + RESTPW + " @ "
+ RESTURL);
} }
} }
@ -235,11 +241,11 @@ public abstract class GeoserverRESTTest extends Assert {
} }
} }
private void deleteAllStyles() { protected void deleteAllStyles() {
List<String> styles = reader.getStyles().getNames(); List<String> styles = reader.getStyles().getNames();
for (String style : styles) { for (String style : styles) {
LOGGER.warn("Deleting Style " + style); LOGGER.warn("Deleting Style " + style);
boolean removed = publisher.removeStyle(style); boolean removed = publisher.removeStyle(style,true);
assertTrue("Style not removed " + style, removed); assertTrue("Style not removed " + style, removed);
} }
@ -249,18 +255,14 @@ public abstract class GeoserverRESTTest extends Assert {
RESTFeatureType featureType = reader.getFeatureType(layer); RESTFeatureType featureType = reader.getFeatureType(layer);
RESTDataStore datastore = reader.getDatastore(featureType); RESTDataStore datastore = reader.getDatastore(featureType);
LOGGER.warn("Deleting FeatureType" LOGGER.warn("Deleting FeatureType" + datastore.getWorkspaceName() + " : "
+ datastore.getWorkspaceName() + " : " + datastore.getName() + " / " + featureType.getName());
+ datastore.getName() + " / "
+ featureType.getName()
);
boolean removed = publisher.unpublishFeatureType(datastore.getWorkspaceName(), datastore.getName(), layer.getName()); boolean removed = publisher.unpublishFeatureType(datastore.getWorkspaceName(),
assertTrue("FeatureType not removed:" datastore.getName(), layer.getName());
+ datastore.getWorkspaceName() + " : " assertTrue(
+ datastore.getName() + " / " "FeatureType not removed:" + datastore.getWorkspaceName() + " : "
+ featureType.getName(), + datastore.getName() + " / " + featureType.getName(), removed);
removed);
} }
@ -268,19 +270,13 @@ public abstract class GeoserverRESTTest extends Assert {
RESTCoverage coverage = reader.getCoverage(layer); RESTCoverage coverage = reader.getCoverage(layer);
RESTCoverageStore coverageStore = reader.getCoverageStore(coverage); RESTCoverageStore coverageStore = reader.getCoverageStore(coverage);
LOGGER.warn("Deleting Coverage " LOGGER.warn("Deleting Coverage " + coverageStore.getWorkspaceName() + " : "
+ coverageStore.getWorkspaceName() + " : " + coverageStore.getName() + " / " + coverage.getName());
+ coverageStore.getName() + " / "
+ coverage.getName());
boolean removed = publisher.unpublishCoverage(coverageStore.getWorkspaceName(), boolean removed = publisher.unpublishCoverage(coverageStore.getWorkspaceName(),
coverageStore.getName(), coverageStore.getName(), coverage.getName());
coverage.getName()); assertTrue("Coverage not deleted " + coverageStore.getWorkspaceName() + " : "
assertTrue("Coverage not deleted " + coverageStore.getName() + " / " + coverage.getName(), removed);
+ coverageStore.getWorkspaceName() + " : "
+ coverageStore.getName() + " / "
+ coverage.getName(),
removed);
} }
protected boolean existsLayer(String layername) { protected boolean existsLayer(String layername) {

View File

@ -19,33 +19,90 @@
*/ */
package it.geosolutions.geoserver.rest.encoder.feature; 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.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder; import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder;
import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder; 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.Presentation;
import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.PresentationDiscrete; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.PresentationDiscrete;
import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; 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 java.math.BigDecimal;
import java.util.List;
import junit.framework.TestCase;
import org.jdom.Element; import org.jdom.Element;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
/** /**
* *
* @author ETj (etj at geo-solutions.it) * @author ETj (etj at geo-solutions.it)
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/ */
public class GSFeatureEncoderTest extends TestCase { public class GSFeatureEncoderTest extends GeoserverRESTPublisherTest {
protected final static Logger LOGGER = LoggerFactory.getLogger(GSFeatureEncoderTest.class); protected final static Logger LOGGER = LoggerFactory.getLogger(GSFeatureEncoderTest.class);
@Test @Test
public void testAll() { public void testIntegration() throws IOException {
if (!enabled())
return;
deleteAll();
GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(RESTURL, RESTUSER, RESTPW);
String storeName = "resttestshp";
String layerName = "cities";
GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder();
fte.setName(layerName + "_NEW");
fte.setTitle("title");
// fte.addKeyword("TODO");
fte.setNativeCRS("EPSG:4326");
fte.setDescription("desc");
fte.setEnabled(true);
GSLayerEncoder layerEncoder = new GSLayerEncoder();
layerEncoder.setEnabled(true);
layerEncoder.setQueryable(true);
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<GSAttributeEncoder> 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(); GSFeatureTypeEncoder encoder = new GSFeatureTypeEncoder();
encoder.addKeyword("KEYWORD_1"); encoder.addKeyword("KEYWORD_1");
@ -53,35 +110,48 @@ public class GSFeatureEncoderTest extends TestCase {
encoder.addKeyword("..."); encoder.addKeyword("...");
encoder.addKeyword("KEYWORD_N"); 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"); GSFeatureDimensionInfoEncoder dim2 = new GSFeatureDimensionInfoEncoder("ELE");
if (LOGGER.isInfoEnabled())
LOGGER.info(encoder.toString());
encoder.addMetadata("elevation", dim2); encoder.addMetadata("elevation", dim2);
dim2.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL, BigDecimal.valueOf(10));
dim2.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL,
BigDecimal.valueOf(10));
Element el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.PRESENTATION); Element el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.PRESENTATION);
Assert.assertNotNull(el); Assert.assertNotNull(el);
LOGGER.info("contains_key:" + el.toString()); LOGGER.info("contains_key:" + el.toString());
dim2.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL, dim2.setPresentation(PresentationDiscrete.DISCRETE_INTERVAL, BigDecimal.valueOf(12));
BigDecimal.valueOf(12));
el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.RESOLUTION); el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.RESOLUTION);
Assert.assertNotNull(el); Assert.assertNotNull(el);
Assert.assertEquals("12", el.getText()); Assert.assertEquals("12", el.getText());
dim2.setPresentation(Presentation.CONTINUOUS_INTERVAL); dim2.setPresentation(Presentation.CONTINUOUS_INTERVAL);
encoder.setMetadata("time", new GSFeatureDimensionInfoEncoder("time")); encoder.setMetadata("time", new GSFeatureDimensionInfoEncoder("time"));
el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.PRESENTATION); el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.PRESENTATION);
Assert.assertNotNull(el); Assert.assertNotNull(el);
el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.RESOLUTION); el = ElementUtils.contains(encoder.getRoot(), GSDimensionInfoEncoder.RESOLUTION);
@ -100,6 +170,10 @@ public class GSFeatureEncoderTest extends TestCase {
if (el == null) if (el == null)
LOGGER.info("REMOVED"); LOGGER.info("REMOVED");
if (LOGGER.isInfoEnabled())
LOGGER.info(encoder.toString());
assertEquals(encoder.getName(),"Layername");
} }
@Test @Test
@ -113,7 +187,8 @@ public class GSFeatureEncoderTest extends TestCase {
Assert.assertTrue(encoder.delKeyword("KEYWORD_2")); Assert.assertTrue(encoder.delKeyword("KEYWORD_2"));
Assert.assertFalse(encoder.delKeyword("KEYWORD_M")); Assert.assertFalse(encoder.delKeyword("KEYWORD_M"));
final GSFeatureDimensionInfoEncoder elevationDimension = new GSFeatureDimensionInfoEncoder("elevation_field"); final GSFeatureDimensionInfoEncoder elevationDimension = new GSFeatureDimensionInfoEncoder(
"elevation_field");
// if (LOGGER.isInfoEnabled()) // if (LOGGER.isInfoEnabled())
// LOGGER.info(encoder.toString()); // LOGGER.info(encoder.toString());
@ -132,7 +207,8 @@ public class GSFeatureEncoderTest extends TestCase {
if (LOGGER.isInfoEnabled()) if (LOGGER.isInfoEnabled())
LOGGER.info(encoder.toString()); LOGGER.info(encoder.toString());
final Element el=ElementUtils.contains(encoder.getRoot(),GSDimensionInfoEncoder.DIMENSIONINFO); final Element el = ElementUtils.contains(encoder.getRoot(),
GSDimensionInfoEncoder.DIMENSIONINFO);
Assert.assertNull(el); Assert.assertNull(el);
if (el == null) if (el == null)
LOGGER.info("REMOVED"); LOGGER.info("REMOVED");

View File

@ -63,7 +63,7 @@ public class ElementUtilsTest {
public void containsFilterDepthTest(){ public void containsFilterDepthTest(){
LOGGER.info("STARTING-> containsFilterDepthTest"); LOGGER.info("STARTING-> containsFilterDepthTest");
final List<Element> list=ElementUtils.search(this.root, filter, 1); final List<Element> list=ElementUtils.search(this.root, filter, 0);
Assert.assertEquals(1,list.size()); Assert.assertEquals(1,list.size());
final List<Element> list2=ElementUtils.search(this.root, filter, 6); final List<Element> list2=ElementUtils.search(this.root, filter, 6);