From bc233d4f7a5ba42531e9933aadbbd0a143898b00 Mon Sep 17 00:00:00 2001 From: eblondel Date: Thu, 17 Oct 2013 20:12:39 +0200 Subject: [PATCH 01/10] #102 master - encoder for AuthorityURL/Identifier/advertised (GS>=2.2) --- .../rest/encoder/GSLayerEncoder.java | 65 ++++++ .../authorityurl/AuthorityURLInfo.java | 37 +++ .../GSAuthorityURLInfoEncoder.java | 215 ++++++++++++++++++ .../identifier/GSIdentifierInfoEncoder.java | 215 ++++++++++++++++++ .../encoder/identifier/IdentifierInfo.java | 37 +++ .../rest/encoder/GSLayerEncoderTest.java | 31 ++- .../GSAuthorityURLInfoEncoderTest.java | 49 ++++ .../encoder/feature/GSFeatureEncoderTest.java | 15 ++ .../GSIdentifierInfoEncoderTest.java | 50 ++++ 9 files changed, 713 insertions(+), 1 deletion(-) create mode 100644 src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/AuthorityURLInfo.java create mode 100644 src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoder.java create mode 100644 src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoder.java create mode 100644 src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/IdentifierInfo.java create mode 100644 src/test/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoderTest.java create mode 100644 src/test/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoderTest.java 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 48dc583..f78ccac 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder.java @@ -25,15 +25,19 @@ package it.geosolutions.geoserver.rest.encoder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder; import org.jdom.Element; import org.jdom.filter.Filter; /** + * Layer encoder for Geoserver >= 2.2 * * @author ETj (etj at geo-solutions.it) * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + * @author Emmanuel Blondel - emmanuel.blondel1@gmail.com * * The layer encoder is enabled by default * @@ -41,12 +45,19 @@ import org.jdom.filter.Filter; public class GSLayerEncoder extends PropertyXMLEncoder { public final static String STYLES = "styles"; + public final static String AUTHORITY_URLS="authorityURLs"; + public final static String IDENTIFIERS="identifiers"; + final private Element stylesEncoder = new Element(STYLES); + final private Element authorityURLListEncoder = new Element(AUTHORITY_URLS); + final private Element identifierListEncoder = new Element(IDENTIFIERS); public GSLayerEncoder() { super("layer"); addEnabled(); addContent(stylesEncoder); + addContent(authorityURLListEncoder); + addContent(identifierListEncoder); } /** @@ -167,4 +178,58 @@ public class GSLayerEncoder extends PropertyXMLEncoder { } })).size() == 0 ? false : true; } + + /** + * + * @param advertised + * true if the layer should be advertised + */ + public void setAdvertised(boolean advertised) { + if (advertised) + set("advertised", "true"); + else + set("advertised", "false"); + } + + /** + * Add an authorityURLInfo to the GeoServer layer + * + * @param authorityURLInfo + */ + public void addAuthorityURL(GSAuthorityURLInfoEncoder authorityURLInfo) { + authorityURLListEncoder.addContent(authorityURLInfo.getRoot()); + } + + /** + * Deletes a AuthorityURLInfo from the list using the authorityURL + * (AuthorityURLInfo href) + * + * @param authorityURL + * @return true if something is removed, false otherwise + */ + public boolean delAuthorityURL(final String authorityURL) { + return (authorityURLListEncoder.removeContent(GSAuthorityURLInfoEncoder + .getFilterByHref(authorityURL))).size() == 0 ? false : true; + } + + /** + * Add an identifierInfo to the GeoServer layer + * + * @param identifierInfo + */ + public void addIdentifier(GSIdentifierInfoEncoder identifierInfo) { + identifierListEncoder.addContent(identifierInfo.getRoot()); + } + + /** + * Deletes a IdentifierInfo from the list using the authority name + * (IdentifierInfo authority) + * + * @param authority + * @return true if something is removed, false otherwise + */ + public boolean delIdentifier(final String authority) { + return (identifierListEncoder.removeContent(GSIdentifierInfoEncoder + .getFilterByHref(authority))).size() == 0 ? false : true; + } } diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/AuthorityURLInfo.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/AuthorityURLInfo.java new file mode 100644 index 0000000..402655a --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/AuthorityURLInfo.java @@ -0,0 +1,37 @@ +/* +* 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.encoder.authorityurl; + +/** + * Enumeration of layer authorityURL members + * + * @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | + * emmanuel.blondel@fao.org + * + */ +public enum AuthorityURLInfo { + + name, href; +} \ No newline at end of file diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoder.java new file mode 100644 index 0000000..f75ba24 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoder.java @@ -0,0 +1,215 @@ +/* +* 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.encoder.authorityurl; + +import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; +import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; + +import org.jdom.Element; +import org.jdom.filter.Filter; + +/** +* GSAuthorityURLInfoEncoder - encodes an authorityURL for a given GeoServer +* layer as follows: +*
+* {@code
+* final GSAuthorityURLInfoEncoder ae = new GSAuthorityURLInfoEncoder();
+* ae.setName("an authority");
+* ae.setHref("http://www.organization.org");
+* }
+* 
+* For this example, the XML output is: +*
+* {@code
+* 
+*   an authority
+*   http://www.organization.org
+* 
+* }
+* 
+* +* @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | +* emmanuel.blondel@fao.org +* +*/ +public class GSAuthorityURLInfoEncoder extends XmlElement { + + /** + * A class to filter the AuthorityURL by href + * + * + */ + private static class filterByHref implements Filter { + + final private String key; + + public filterByHref(String href) { + this.key = href; + } + + private static final long serialVersionUID = 1L; + + public boolean matches(Object obj) { + Element el = ((Element) obj).getChild(AuthorityURLInfo.href + .toString()); + if (el != null && el.getTextTrim().equals(key)) { + return true; + } + return false; + } + } + + /** + * Get a Filter using the AuthorityURLInfo href (authorityURL) + * + * @param href + * @return the filter + */ + public static Filter getFilterByHref(String href) { + return new filterByHref(href); + } + + /** + * Constructs a new GSAuthorityURLInfoEncoder + * + */ + public GSAuthorityURLInfoEncoder() { + super("AuthorityURL"); + } + + /** + * Constructs quickly an AuthorityURL info + * + * @param name + * (required) + * @param href + * (required) + */ + public GSAuthorityURLInfoEncoder(String name, String href) { + super("AuthorityURL"); + this.setup(name, href); + } + + /** + * Set-up quickly an AuthorityURL info + * + * @param name + * @param href + */ + protected void setup(String name, String href) { + set(AuthorityURLInfo.name.name(), name); + set(AuthorityURLInfo.href.name(), href); + } + + /** + * Set an AuthorityURLInfo member (name, href) + * + * @param type + * @param value + */ + protected void setMember(AuthorityURLInfo type, String value) { + set(type.toString(), value); + } + + /** + * Set the name + * + * @param name + */ + public void setName(String name) { + this.setMember(AuthorityURLInfo.name, name); + } + + /** + * Set the href + * + * @param href + */ + public void setHref(String href) { + this.setMember(AuthorityURLInfo.href, href); + } + + /** + * Deletes an AuthorityURLInfo member + * + * @param type + * @return true if the AuthorityURLInfo member is removed + */ + protected boolean delMember(AuthorityURLInfo type) { + return ElementUtils.remove(this.getRoot(), + this.getRoot().getChild(type.toString())); + } + + /** + * Deletes the authority name + * + * @return true if removed + */ + public boolean delName() { + return this.delMember(AuthorityURLInfo.name); + } + + /** + * Deletes the href + * + * @return true if removed + */ + public boolean delHref() { + return this.delMember(AuthorityURLInfo.href); + } + + /** + * Get the value of the AuthorityURLInfo member + * + * @param type + * @return the value of the AuthorityURLInfo member + */ + protected String getMember(AuthorityURLInfo type) { + Element el = this.getRoot().getChild(type.toString()); + if (el != null) + return el.getTextTrim(); + else + return null; + } + + /** + * Get the name + * + * @return + */ + public String getName() { + return this.getMember(AuthorityURLInfo.name); + } + + /** + * Get the href + * + * @return + */ + public String getHref() { + return this.getMember(AuthorityURLInfo.href); + } + +} \ No newline at end of file diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoder.java new file mode 100644 index 0000000..23ca90c --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoder.java @@ -0,0 +1,215 @@ +/* +* 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.encoder.identifier; + +import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; +import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; + +import org.jdom.Element; +import org.jdom.filter.Filter; + +/** +* GSIdentifierInfoEncoder - encodes an Identifier for a given GeoServer +* layer as follows: +*
+* {@code
+* final GSIdentifierInfoEncoder ie = new GSIdentifierInfoEncoder();
+* ie.setAuthority("an authority");
+* ie.setIdentifier("an identifier");
+* }
+* 
+* For this example, the XML output is: +*
+* {@code
+* 
+*   an authority
+*   an identifier
+* 
+* }
+* 
+* +* @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | +* emmanuel.blondel@fao.org +* +*/ +public class GSIdentifierInfoEncoder extends XmlElement { + + /** + * A class to filter the Idenfiers by authority + * + * + */ + private static class filterByAuthority implements Filter { + + final private String key; + + public filterByAuthority(String authority) { + this.key = authority; + } + + private static final long serialVersionUID = 1L; + + public boolean matches(Object obj) { + Element el = ((Element) obj).getChild(IdentifierInfo.authority + .toString()); + if (el != null && el.getTextTrim().equals(key)) { + return true; + } + return false; + } + } + + /** + * Get a Filter using the IdentifierInfo authority + * + * @param authority + * @return the filter + */ + public static Filter getFilterByHref(String authority) { + return new filterByAuthority(authority); + } + + /** + * Constructs a new GSIdentifierInfoEncoder + * + */ + public GSIdentifierInfoEncoder() { + super("Identifier"); + } + + /** + * Constructs quickly an Identifier info + * + * @param authority + * (required) + * @param identifier + * (required) + */ + public GSIdentifierInfoEncoder(String authority, String identifier) { + super("Identifier"); + this.setup(authority, identifier); + } + + /** + * Set-up quickly an Identifier info + * + * @param authority + * @param identifier + */ + protected void setup(String authority, String identifier) { + set(IdentifierInfo.authority.name(), authority); + set(IdentifierInfo.identifier.name(), identifier); + } + + /** + * Set an IdentifierInfo member (authority, identifier) + * + * @param type + * @param value + */ + protected void setMember(IdentifierInfo type, String value) { + set(type.toString(), value); + } + + /** + * Set the authority + * + * @param authority + */ + public void setAuthority(String authority) { + this.setMember(IdentifierInfo.authority, authority); + } + + /** + * Set the identifier + * + * @param identifier + */ + public void setIdentifier(String identifier) { + this.setMember(IdentifierInfo.identifier, identifier); + } + + /** + * Deletes an IdentifierInfo member + * + * @param type + * @return true if the IdentifierInfo member is removed + */ + protected boolean delMember(IdentifierInfo type) { + return ElementUtils.remove(this.getRoot(), + this.getRoot().getChild(type.toString())); + } + + /** + * Deletes the authority + * + * @return true if removed + */ + public boolean delAuthority() { + return this.delMember(IdentifierInfo.authority); + } + + /** + * Deletes the identifier + * + * @return true if removed + */ + public boolean delIdentifier() { + return this.delMember(IdentifierInfo.identifier); + } + + /** + * Get the value of the IdentifierInfo member + * + * @param type + * @return the value of the IdentifierInfo member + */ + protected String getMember(IdentifierInfo type) { + Element el = this.getRoot().getChild(type.toString()); + if (el != null) + return el.getTextTrim(); + else + return null; + } + + /** + * Get the authority + * + * @return + */ + public String getAuthority() { + return this.getMember(IdentifierInfo.authority); + } + + /** + * Get the identifier + * + * @return + */ + public String getIdentifier() { + return this.getMember(IdentifierInfo.identifier); + } + +} \ No newline at end of file diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/IdentifierInfo.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/IdentifierInfo.java new file mode 100644 index 0000000..63f4deb --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/identifier/IdentifierInfo.java @@ -0,0 +1,37 @@ +/* +* 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.encoder.identifier; + +/** +* Enumeration of layer authorityURL members +* +* @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | +* emmanuel.blondel@fao.org +* +*/ +public enum IdentifierInfo { + + authority, identifier; +} \ No newline at end of file diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoderTest.java index 8774bb1..1d71e58 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoderTest.java @@ -19,6 +19,8 @@ */ package it.geosolutions.geoserver.rest.encoder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; import junit.framework.Assert; import org.jdom.Element; @@ -40,9 +42,16 @@ public class GSLayerEncoderTest { layerEncoder = new GSLayerEncoder(); layerEncoder.setEnabled(true); layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); + layerEncoder.setDefaultStyle("point"); layerEncoder.addStyle("additional_style1"); layerEncoder.addStyle("additional_style2"); + + layerEncoder.addAuthorityURL(new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org")); + layerEncoder.addIdentifier(new GSIdentifierInfoEncoder("authority1", + "identifier1")); } @Test @@ -55,6 +64,10 @@ public class GSLayerEncoderTest { true, Boolean.parseBoolean(layerEncoder.getRoot() .getChild("queryable").getValue())); + Assert.assertEquals( + true, + Boolean.parseBoolean(layerEncoder.getRoot() + .getChild("advertised").getValue())); } @Test @@ -81,5 +94,21 @@ public class GSLayerEncoderTest { Assert.assertEquals("additional_style2", ((Element) layerEncoder .getRoot().getChild("styles").getChildren().get(0)).getText()); } - + + @Test + public void testAuthorityURL(){ + Element el = (Element) layerEncoder.getRoot().getChild("authorityURLs") + .getChildren().get(0); + Assert.assertEquals("authority1", el.getChild("name").getValue()); + Assert.assertEquals("http://www.authority1.org", el.getChild("href") + .getValue()); + } + + @Test + public void testIdentifier(){ + Element el = (Element) layerEncoder.getRoot().getChild("identifiers") + .getChildren().get(0); + Assert.assertEquals("authority1", el.getChild("authority").getValue()); + Assert.assertEquals("identifier1", el.getChild("identifier").getValue()); + } } diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoderTest.java new file mode 100644 index 0000000..03803a8 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/authorityurl/GSAuthorityURLInfoEncoderTest.java @@ -0,0 +1,49 @@ +/* +* Copyright (C) 2007 - 2011 GeoSolutions S.A.S. +* http://www.geo-solutions.it +* +* GPLv3 + Classpath exception +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ +package it.geosolutions.geoserver.rest.encoder.authorityurl; + +import junit.framework.Assert; + +import org.junit.Test; + +/** + * + * @author eblondel + * + */ +public class GSAuthorityURLInfoEncoderTest { + + @Test + public void authorityURLInfoTest() { + GSAuthorityURLInfoEncoder encoder = new GSAuthorityURLInfoEncoder(); + encoder.setup("authority1", "http://www.authority1.org"); + + Assert.assertEquals("authority1", encoder.getName()); + Assert.assertEquals("http://www.authority1.org", encoder.getHref()); + + Assert.assertTrue(encoder.delHref()); + Assert.assertNull(encoder.getHref()); + + encoder.setName("authority2"); + encoder.setHref("http://www.authority2.org"); + Assert.assertEquals("authority2", encoder.getName()); + Assert.assertEquals("http://www.authority2.org", encoder.getHref()); + } +} \ No newline at end of file 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 491bf0a..d4ee4f2 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 @@ -25,6 +25,8 @@ 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.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder; import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.Presentation; import it.geosolutions.geoserver.rest.encoder.metadata.GSFeatureDimensionInfoEncoder; @@ -73,6 +75,7 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { String layerName = "cities"; GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder(); + fte.setNativeName(layerName); fte.setName(layerName + "_NEW"); fte.setTitle("title"); // fte.addKeyword("TODO"); @@ -89,10 +92,22 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { GSLayerEncoder layerEncoder = new GSLayerEncoder(); layerEncoder.setEnabled(true); layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); layerEncoder.setDefaultStyle("point"); layerEncoder.addStyle("point2"); layerEncoder.addStyle("point3"); + + // authorityURL + GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org"); + layerEncoder.addAuthorityURL(authorityURL); + + // identifier + GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( + "authority1", "identifier1"); + layerEncoder.addIdentifier(identifier); + publisher.createWorkspace(DEFAULT_WS); diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoderTest.java new file mode 100644 index 0000000..18a2e00 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/identifier/GSIdentifierInfoEncoderTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2007 - 2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * GPLv3 + Classpath exception + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package it.geosolutions.geoserver.rest.encoder.identifier; + +import junit.framework.Assert; + +import org.junit.Test; + +/** + * + * @author eblondel + * + */ +public class GSIdentifierInfoEncoderTest { + + @Test + public void identifierInfoTest() { + GSIdentifierInfoEncoder encoder = new GSIdentifierInfoEncoder(); + encoder.setup("authority1", "identifier1"); + + Assert.assertEquals("authority1", encoder.getAuthority()); + Assert.assertEquals("identifier1", encoder.getIdentifier()); + + Assert.assertTrue(encoder.delIdentifier()); + Assert.assertNull(encoder.getIdentifier()); + + encoder.setAuthority("authority2"); + encoder.setIdentifier("identifier2"); + Assert.assertEquals("authority2", encoder.getAuthority()); + Assert.assertEquals("identifier2", encoder.getIdentifier()); + + } +} \ No newline at end of file From e5033df0008ae19285d5ad08f4c7cbbca550d68c Mon Sep 17 00:00:00 2001 From: eblondel Date: Thu, 17 Oct 2013 21:23:37 +0200 Subject: [PATCH 02/10] #102 master - specific encoders in GSLayer for GS=2.1 --- .../rest/encoder/GSLayerEncoder21.java | 188 ++++++++++++++++++ .../rest/encoder/GSLayerEncoder21Test.java | 95 +++++++++ .../encoder/feature/GSFeatureEncoderTest.java | 59 ++++++ 3 files changed, 342 insertions(+) create mode 100644 src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java create mode 100644 src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java new file mode 100644 index 0000000..5daf3f6 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21.java @@ -0,0 +1,188 @@ +/* + * 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.encoder; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import it.geosolutions.geoserver.rest.encoder.authorityurl.AuthorityURLInfo; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.IdentifierInfo; +import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder; +import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; + +/** + * Layer encoder for Geoserver = 2.1 + * + * @author Emmanuel Blondel - emmanuel.blondel1@gmail.com + * + * The layer encoder is enabled by default + * + */ +public class GSLayerEncoder21 extends GSLayerEncoder { + + + public final static String METADATA = "metadata"; + final private GSMetadataEncoder metadata = new GSMetadataEncoder(); + public Map authorityURLList; + public Map identifierList; + + private class GSMetadataEncoder extends NestedElementEncoder{ + public GSMetadataEncoder() { + super(METADATA); + } + } + + public GSLayerEncoder21() { + super(); + addContent(metadata.getRoot()); + addAdvertised(); + } + + /** + * @param key + * @param dimensionInfo + */ + protected void addMetadata(String key, XmlElement dimensionInfo) { + metadata.add(key, dimensionInfo.getRoot()); + } + + /** + * advertise the layer + */ + protected void addAdvertised(){ + metadata.add("advertised", "true"); + } + + /** + * + * @param advertised true if the layer should be advertised + */ + public void setAdvertised(boolean advertised){ + if(advertised){ + metadata.add("advertised", "true"); + }else{ + metadata.add("advertised", "false"); + } + } + + /** + * Add an authorityURLInfo to the GeoServer layer + * + * @param authorityURLInfo + */ + public void addAuthorityURL(GSAuthorityURLInfoEncoder authorityURLInfo){ + if(authorityURLList == null){ + authorityURLList = new HashMap(); + } + authorityURLList.put(authorityURLInfo.getHref(), authorityURLInfo.getName()); + String jsonStr = ""; + for(Entry entry : authorityURLList.entrySet()){ + jsonStr += "{"+ + "\""+AuthorityURLInfo.name.name()+"\":\""+entry.getValue()+"\","+ + "\""+AuthorityURLInfo.href.name()+"\":\""+entry.getKey()+"\""+ + "},"; + } + metadata.set("authorityURLs", "["+jsonStr+"]"); + } + + + /** + * Deletes a AuthorityURLInfo from the list using the authorityURL + * (AuthorityURLInfo href) + * + * @param authorityURL + * @return true if something is removed, false otherwise + */ + public boolean delAuthorityURL(final String authorityURL){ + boolean delete = false; + if(!(authorityURLList == null || authorityURLList.isEmpty())){ + if(authorityURLList.containsKey(authorityURL)){ + identifierList.remove(authorityURL); + String jsonStr = ""; + for(Entry entry : identifierList.entrySet()){ + jsonStr += "{"+ + "\""+AuthorityURLInfo.name.name()+"\":\""+entry.getValue()+"\","+ + "\""+AuthorityURLInfo.href.name()+"\":\""+entry.getKey()+"\""+ + "},"; + } + metadata.set("identifiers", "["+jsonStr+"]"); + delete = true; + } + } + return delete; + } + + /** + * Add an identifierInfo to the GeoServer layer + * + * @param identifierInfo + */ + public void addIdentifier(GSIdentifierInfoEncoder identifierInfo){ + if(identifierList == null){ + identifierList = new HashMap(); + } + identifierList.put(identifierInfo.getAuthority(), identifierInfo.getIdentifier()); + String jsonStr = ""; + for(Entry entry : identifierList.entrySet()){ + jsonStr += "{"+ + "\""+IdentifierInfo.authority.name()+"\":\""+entry.getKey()+"\","+ + "\""+IdentifierInfo.identifier.name()+"\":\""+entry.getValue()+"\""+ + "},"; + } + metadata.set("identifiers", "["+jsonStr+"]"); + } + + /** + * Deletes a IdentifierInfo from the list using the authority + * name (IdentifierInfo authority) + * + * @param authority + * @return true if something is removed, false otherwise + */ + public boolean delIdentifier(final String authority){ + boolean delete = false; + if(!(identifierList == null || identifierList.isEmpty())){ + if(identifierList.containsKey(authority)){ + identifierList.remove(authority); + String jsonStr = ""; + for(Entry entry : identifierList.entrySet()){ + jsonStr += "{"+ + "\""+IdentifierInfo.authority.name()+"\":\""+entry.getKey()+"\","+ + "\""+IdentifierInfo.identifier.name()+"\":\""+entry.getValue()+"\""+ + "},"; + } + metadata.set("identifiers", "["+jsonStr+"]"); + delete = true; + } + } + return delete; + } + + +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java new file mode 100644 index 0000000..11ebef6 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2007 - 2011 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * GPLv3 + Classpath exception + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package it.geosolutions.geoserver.rest.encoder; + +import java.util.List; + +import it.geosolutions.geoserver.rest.encoder.authorityurl.AuthorityURLInfo; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.IdentifierInfo; +import junit.framework.Assert; + +import org.jdom.Element; +import org.junit.Before; +import org.junit.Test; + +/** + * GSLayerEncoder21Test + * + * @author Emmanuel Blondel - emmanuel.blondel1@gmail.com | + * emmanuel.blondel@fao.org + */ +public class GSLayerEncoder21Test { + + GSLayerEncoder21 layerEncoder; + + @Before + public void setup() { + layerEncoder = new GSLayerEncoder21(); + layerEncoder.setAdvertised(true); + layerEncoder.addAuthorityURL(new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org")); + layerEncoder.addIdentifier(new GSIdentifierInfoEncoder("authority1", + "identifier1")); + } + + + @Test + public void testMetadata(){ + List metaElements = layerEncoder.getRoot().getChild("metadata").getChildren(); + for(Element el : metaElements){ + String key = el.getAttributeValue("key"); + + if(key.matches("advertised")){ + Assert.assertEquals(true, + Boolean.parseBoolean(el.getValue())); + + }else if(key.matches("authorityURLs")){ + String content = el.getValue(); + content = content.substring(2); + content = content.substring(0, content.length()-2); + String[] props = content.split(","); + for(String prop : props){ + String[] kvp = prop.split(":"); + if(kvp[0].matches(AuthorityURLInfo.name.name())){ + Assert.assertEquals("authority1", kvp[1]); + }else if(kvp[0].matches(AuthorityURLInfo.href.name())){ + Assert.assertEquals("http://www.authority1.org", kvp[1]); + } + } + + }else if(key.matches("identifiers")){ + String content = el.getValue(); + content = content.substring(2); + content = content.substring(0, content.length()-2); + String[] props = content.split(","); + for(String prop : props){ + String[] kvp = prop.split(":"); + if(kvp[0].matches(IdentifierInfo.authority.name())){ + Assert.assertEquals("authority1", kvp[1]); + }else if(kvp[0].matches(IdentifierInfo.identifier.name())){ + Assert.assertEquals("identifier1", kvp[1]); + } + } + } + } + } +} 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 d4ee4f2..1da7fc8 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 @@ -24,6 +24,7 @@ import it.geosolutions.geoserver.rest.GeoserverRESTTest; 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.GSLayerEncoder21; import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder; import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; @@ -133,6 +134,64 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); } + + @Test + public void testIntegration_21() throws IOException { + + if (!enabled()) + return; + deleteAll(); + + GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(RESTURL, RESTUSER, RESTPW); + + String storeName = "resttestshp"; + String layerName = "cities"; + + GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder(); + fte.setNativeName(layerName); + fte.setName(layerName + "_NEW2"); + fte.setTitle("title"); + fte.setNativeCRS("EPSG:4326"); + fte.setDescription("desc"); + fte.setEnabled(true); + + //metadataLink + GSMetadataLinkInfoEncoder metadatalink = new GSMetadataLinkInfoEncoder( + "text/xml", "ISO19115:2003", + "http://www.organization.org/metadata1"); + fte.addMetadataLinkInfo(metadatalink); + + //use of GSLayerEncoder specific to GS 2.1 + GSLayerEncoder21 layerEncoder = new GSLayerEncoder21(); + layerEncoder.setEnabled(true); + layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); + + layerEncoder.setDefaultStyle("point"); + layerEncoder.addStyle("point2"); + layerEncoder.addStyle("point3"); + + // authorityURL + GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org"); + GSAuthorityURLInfoEncoder authorityURL2 = new GSAuthorityURLInfoEncoder( + "authority2", "http://www.authority2.org"); + layerEncoder.addAuthorityURL(authorityURL); + layerEncoder.addAuthorityURL(authorityURL2); + + // identifier + GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( + "authority1", "identifier1"); + GSIdentifierInfoEncoder identifier2 = new GSIdentifierInfoEncoder( + "authority2", "identifier2"); + layerEncoder.addIdentifier(identifier); + layerEncoder.addIdentifier(identifier2); + + publisher.createWorkspace(DEFAULT_WS); + assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); + } + + @Test public void testFeatureTypeEncoder() { From 8d47a02e037c4eea38bfbb18e9222540cc6647ae Mon Sep 17 00:00:00 2001 From: eblondel Date: Thu, 17 Oct 2013 22:44:58 +0200 Subject: [PATCH 03/10] #102-AuthorityURL/Identifier/advertised decoders +fix/add other methods --- .../geoserver/rest/decoder/RESTLayer.java | 77 +++++++++++++++ .../geoserver/decoder/LayerDecoderTest.java | 98 +++++++++++++++++++ src/test/resources/testdata/layerExample.xml | 40 ++++++++ 3 files changed, 215 insertions(+) create mode 100644 src/test/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java create mode 100644 src/test/resources/testdata/layerExample.xml diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java index 011873e..d14eba7 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java @@ -25,7 +25,16 @@ package it.geosolutions.geoserver.rest.decoder; +import java.util.ArrayList; +import java.util.List; + import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.AuthorityURLInfo; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.IdentifierInfo; +import it.geosolutions.geoserver.rest.encoder.metadatalink.GSMetadataLinkInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.metadatalink.ResourceMetadataLinkInfo; import org.jdom.Element; import org.jdom.Namespace; @@ -97,6 +106,18 @@ public class RESTLayer { this.layerElem = layerElem; } + public boolean getEnabled(){ + return Boolean.parseBoolean(layerElem.getChildText("enabled")); + } + + public boolean getQueryable(){ + return Boolean.parseBoolean(layerElem.getChildText("queryable")); + } + + public boolean getAdvertised(){ + return Boolean.parseBoolean(layerElem.getChildText("advertised")); + } + public String getName() { return layerElem.getChildText("name"); } @@ -164,6 +185,62 @@ public class RESTLayer { Element atom = resource.getChild("link", Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom")); return atom.getAttributeValue("href"); } + + + /** + * Decodes the list of AuthorityURLInfo from the GeoServer Layer + * + * @return the list of GSAuthorityURLInfoEncoder + */ + public List getEncodedAuthorityURLInfoList() { + List authorityURLList = null; + + final Element authorityURLsRoot = layerElem.getChild("authorityURLs"); + if (authorityURLsRoot != null) { + final List authorityURLs = authorityURLsRoot.getChildren(); + if (authorityURLs != null) { + authorityURLList = new ArrayList( + authorityURLs.size()); + for (Element authorityURL : authorityURLs) { + final GSAuthorityURLInfoEncoder authEnc = new GSAuthorityURLInfoEncoder(); + authEnc.setName(authorityURL + .getChildText(AuthorityURLInfo.name.name())); + authEnc.setHref(authorityURL + .getChildText(AuthorityURLInfo.href.name())); + authorityURLList.add(authEnc); + } + } + } + return authorityURLList; + } + + /** + * Decodes the list of IdentifierInfo from the GeoServer Layer + * + * @return the list of IdentifierInfoEncoder + */ + public List getEncodedIdentifierInfoList() { + List idList = null; + + final Element idRoot = layerElem.getChild("identifiers"); + if (idRoot != null) { + final List identifiers = idRoot.getChildren(); + if (identifiers != null) { + idList = new ArrayList( + identifiers.size()); + for (Element identifier : identifiers) { + final GSIdentifierInfoEncoder idEnc = new GSIdentifierInfoEncoder(); + idEnc.setAuthority(identifier + .getChildText(IdentifierInfo.authority.name())); + idEnc.setIdentifier(identifier + .getChildText(IdentifierInfo.identifier.name())); + idList.add(idEnc); + } + } + } + return idList; + } + // protected double getLatLonEdge(String edge) { // Element resource = layerElem.getChild("resource"); diff --git a/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java new file mode 100644 index 0000000..7962769 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java @@ -0,0 +1,98 @@ +package it.geosolutions.geoserver.decoder; + +import it.geosolutions.geoserver.rest.decoder.RESTLayer; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import junit.framework.Assert; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.io.ClassPathResource; + +/** + * + * @author eblondel + * + */ +public class LayerDecoderTest { + + RESTLayer layer; + + @Before + public void setUp() throws IOException{ + File layerFile = new ClassPathResource("testdata/layerExample.xml").getFile(); + String layerString = FileUtils.readFileToString(layerFile); + layer = RESTLayer.build(layerString); + } + + @Test + public void testEnabled(){ + Assert.assertEquals(true, layer.getEnabled()); + } + + @Test + public void testQueryable(){ + Assert.assertEquals(true, layer.getQueryable()); + } + + @Test + public void testAdvertised(){ + Assert.assertEquals(true, layer.getAdvertised()); + } + + @Test + public void testName() { + Assert.assertEquals("tasmania_cities", layer.getName()); + } + + @Test + public void testTypeString() { + Assert.assertEquals("VECTOR", layer.getTypeString()); + } + + @Test + public void testType(){ + Assert.assertEquals(RESTLayer.Type.VECTOR, layer.getType()); + } + + @Test + public void testDefaultStyle() { + Assert.assertEquals("capitals", layer.getDefaultStyle()); + } + + @Test + public void testResourceUrl() { + Assert.assertEquals( + "http://localhost:8080/geoserver/rest/workspaces/topp/datastores/taz_shapes/featuretypes/tasmania_cities.xml", + layer.getResourceUrl()); + } + + @Test + public void testAuthorityURLs() { + List authorityURLs = layer + .getEncodedAuthorityURLInfoList(); + Assert.assertEquals("authority1", authorityURLs.get(0).getName()); + Assert.assertEquals("http://www.authority1.org", authorityURLs.get(0) + .getHref()); + Assert.assertEquals("authority2", authorityURLs.get(1).getName()); + Assert.assertEquals("http://www.authority2.org", authorityURLs.get(1) + .getHref()); + } + + @Test + public void testIdentifiers() { + List authorityURLs = layer + .getEncodedIdentifierInfoList(); + Assert.assertEquals("authority1", authorityURLs.get(0).getAuthority()); + Assert.assertEquals("identifier1", authorityURLs.get(0).getIdentifier()); + Assert.assertEquals("authority2", authorityURLs.get(1).getAuthority()); + Assert.assertEquals("identifier2", authorityURLs.get(1).getIdentifier()); + } + +} diff --git a/src/test/resources/testdata/layerExample.xml b/src/test/resources/testdata/layerExample.xml new file mode 100644 index 0000000..af98cfa --- /dev/null +++ b/src/test/resources/testdata/layerExample.xml @@ -0,0 +1,40 @@ + + tasmania_cities + / + VECTOR + + capitals + + + + tasmania_cities + + + true + true + true + + 0 + 0 + + + + authority1 + http://www.authority1.org + + + authority2 + http://www.authority2.org + + + + + authority1 + identifier1 + + + authority2 + identifier2 + + + \ No newline at end of file From 7b10be6bdb4b341968ec7122d300a7eff6be89a3 Mon Sep 17 00:00:00 2001 From: eblondel Date: Thu, 17 Oct 2013 22:54:43 +0200 Subject: [PATCH 04/10] #102 master update RESTLayer javadoc --- .../geoserver/rest/decoder/RESTLayer.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java index d14eba7..6bf5152 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java @@ -58,10 +58,24 @@ import org.jdom.Namespace; true + true + true 0 0 + + + authority1 + http://www.authority1.org + + + + + authority1 + identifier1 + + * } * @author etj From 9ca022093b164569521c49509dec6137d314707b Mon Sep 17 00:00:00 2001 From: eblondel Date: Thu, 17 Oct 2013 23:35:41 +0200 Subject: [PATCH 05/10] #102 master - fix GSLayerEncoder21Test --- .../rest/encoder/GSLayerEncoder21Test.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java index 11ebef6..636ff86 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java @@ -65,28 +65,28 @@ public class GSLayerEncoder21Test { }else if(key.matches("authorityURLs")){ String content = el.getValue(); content = content.substring(2); - content = content.substring(0, content.length()-2); + content = content.substring(0, content.length()-3); String[] props = content.split(","); for(String prop : props){ - String[] kvp = prop.split(":"); - if(kvp[0].matches(AuthorityURLInfo.name.name())){ - Assert.assertEquals("authority1", kvp[1]); - }else if(kvp[0].matches(AuthorityURLInfo.href.name())){ - Assert.assertEquals("http://www.authority1.org", kvp[1]); + String[] kvp = prop.split("\":"); + if(kvp[0].replace("\"", "").matches(AuthorityURLInfo.name.name())){ + Assert.assertEquals("authority1", kvp[1].replace("\"", "")); + }else if(kvp[0].replaceAll("\"", "").matches(AuthorityURLInfo.href.name())){ + Assert.assertEquals("http://www.authority1.org", kvp[1].replace("\"", "")); } } }else if(key.matches("identifiers")){ String content = el.getValue(); content = content.substring(2); - content = content.substring(0, content.length()-2); + content = content.substring(0, content.length()-3); String[] props = content.split(","); for(String prop : props){ - String[] kvp = prop.split(":"); - if(kvp[0].matches(IdentifierInfo.authority.name())){ - Assert.assertEquals("authority1", kvp[1]); - }else if(kvp[0].matches(IdentifierInfo.identifier.name())){ - Assert.assertEquals("identifier1", kvp[1]); + String[] kvp = prop.split("\":"); + if(kvp[0].replace("\"", "").matches(IdentifierInfo.authority.name())){ + Assert.assertEquals("authority1", kvp[1].replace("\"", "")); + }else if(kvp[0].replace("\"", "").matches(IdentifierInfo.identifier.name())){ + Assert.assertEquals("identifier1", kvp[1].replace("\"", "")); } } } From 6f8e457a77ebd86a9ecadee6454d295551cd9b7b Mon Sep 17 00:00:00 2001 From: eblondel Date: Fri, 18 Oct 2013 00:58:23 +0200 Subject: [PATCH 06/10] #102 master - improve GSLayerEncoder21 tests --- .../rest/encoder/GSLayerEncoder21Test.java | 75 +++++++++++++------ 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java index 636ff86..c88e351 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/GSLayerEncoder21Test.java @@ -19,6 +19,7 @@ */ package it.geosolutions.geoserver.rest.encoder; +import java.util.ArrayList; import java.util.List; import it.geosolutions.geoserver.rest.encoder.authorityurl.AuthorityURLInfo; @@ -49,6 +50,10 @@ public class GSLayerEncoder21Test { "authority1", "http://www.authority1.org")); layerEncoder.addIdentifier(new GSIdentifierInfoEncoder("authority1", "identifier1")); + layerEncoder.addAuthorityURL(new GSAuthorityURLInfoEncoder( + "authority2", "http://www.authority2.org")); + layerEncoder.addIdentifier(new GSIdentifierInfoEncoder("authority2", + "identifier2")); } @@ -63,32 +68,54 @@ public class GSLayerEncoder21Test { Boolean.parseBoolean(el.getValue())); }else if(key.matches("authorityURLs")){ - String content = el.getValue(); - content = content.substring(2); - content = content.substring(0, content.length()-3); - String[] props = content.split(","); - for(String prop : props){ - String[] kvp = prop.split("\":"); - if(kvp[0].replace("\"", "").matches(AuthorityURLInfo.name.name())){ - Assert.assertEquals("authority1", kvp[1].replace("\"", "")); - }else if(kvp[0].replaceAll("\"", "").matches(AuthorityURLInfo.href.name())){ - Assert.assertEquals("http://www.authority1.org", kvp[1].replace("\"", "")); - } - } + String jsonStr = el.getValue(); + jsonStr = jsonStr.substring(2); + jsonStr = jsonStr.substring(0, + jsonStr.length() - 3); + + String[] items = jsonStr.split("\\}(,)\\{"); + + String[] props1 = items[0].split(","); + String[] kvp1_1 = props1[0].split("\":"); + String[] kvp1_2 = props1[1].split("\":"); + Assert.assertEquals(AuthorityURLInfo.name.name(), kvp1_1[0].replace("\"", "")); + Assert.assertEquals("authority1", kvp1_1[1].replace("\"", "")); + Assert.assertEquals(AuthorityURLInfo.href.name(), kvp1_2[0].replace("\"", "")); + Assert.assertEquals("http://www.authority1.org", kvp1_2[1].replace("\"", "")); + + String[] props2 = items[1].split(","); + String[] kvp2_1 = props2[0].split("\":"); + String[] kvp2_2 = props2[1].split("\":"); + Assert.assertEquals(AuthorityURLInfo.name.name(), kvp2_1[0].replace("\"", "")); + Assert.assertEquals("authority2", kvp2_1[1].replace("\"", "")); + Assert.assertEquals(AuthorityURLInfo.href.name(), kvp2_2[0].replace("\"", "")); + Assert.assertEquals("http://www.authority2.org", kvp2_2[1].replace("\"", "")); + }else if(key.matches("identifiers")){ - String content = el.getValue(); - content = content.substring(2); - content = content.substring(0, content.length()-3); - String[] props = content.split(","); - for(String prop : props){ - String[] kvp = prop.split("\":"); - if(kvp[0].replace("\"", "").matches(IdentifierInfo.authority.name())){ - Assert.assertEquals("authority1", kvp[1].replace("\"", "")); - }else if(kvp[0].replace("\"", "").matches(IdentifierInfo.identifier.name())){ - Assert.assertEquals("identifier1", kvp[1].replace("\"", "")); - } - } + String jsonStr = el.getValue(); + jsonStr = jsonStr.substring(2); + jsonStr = jsonStr.substring(0, + jsonStr.length() - 3); + + String[] items = jsonStr.split("\\}(,)\\{"); + + String[] props1 = items[0].split(","); + String[] kvp1_1 = props1[0].split("\":"); + String[] kvp1_2 = props1[1].split("\":"); + Assert.assertEquals(IdentifierInfo.authority.name(), kvp1_1[0].replace("\"", "")); + Assert.assertEquals("authority2", kvp1_1[1].replace("\"", "")); + Assert.assertEquals(IdentifierInfo.identifier.name(), kvp1_2[0].replace("\"", "")); + Assert.assertEquals("identifier2", kvp1_2[1].replace("\"", "")); + + String[] props2 = items[1].split(","); + String[] kvp2_1 = props2[0].split("\":"); + String[] kvp2_2 = props2[1].split("\":"); + Assert.assertEquals(IdentifierInfo.authority.name(), kvp2_1[0].replace("\"", "")); + Assert.assertEquals("authority1", kvp2_1[1].replace("\"", "")); + Assert.assertEquals(IdentifierInfo.identifier.name(), kvp2_2[0].replace("\"", "")); + Assert.assertEquals("identifier1", kvp2_2[1].replace("\"", "")); + } } } From 5d820f5d43cd09d94e5a83ac3f5c08be7e0d39dc Mon Sep 17 00:00:00 2001 From: eblondel Date: Fri, 18 Oct 2013 00:59:16 +0200 Subject: [PATCH 07/10] #102 master - add decoders for GS 2.1 + tests --- .../geoserver/rest/decoder/RESTLayer.java | 3 +- .../geoserver/rest/decoder/RESTLayer21.java | 178 ++++++++++++++++++ .../geoserver/decoder/LayerDecoder21Test.java | 66 +++++++ .../resources/testdata/layerExample21.xml | 28 +++ 4 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer21.java create mode 100644 src/test/java/it/geosolutions/geoserver/decoder/LayerDecoder21Test.java create mode 100644 src/test/resources/testdata/layerExample21.xml diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java index 6bf5152..49d85e7 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java @@ -79,9 +79,10 @@ import org.jdom.Namespace; * } * @author etj + * @author eblondel */ public class RESTLayer { - private final Element layerElem; + protected final Element layerElem; public enum Type { VECTOR("VECTOR"), diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer21.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer21.java new file mode 100644 index 0000000..dd58926 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer21.java @@ -0,0 +1,178 @@ +package it.geosolutions.geoserver.rest.decoder; + +import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; +import java.util.ArrayList; +import java.util.List; + +import org.jdom.Element; + +/** + * Parse Layers returned as XML REST objects. Applicable to GS 2.1 for + * decoding: - AuthorityURLs - Identifiers - advertised property value + * + *

+ * This is the XML REST representation: + * + *

+ *  {@code
+ *  
+ *    tasmania_cities
+ *    /
+ *    VECTOR
+ *    
+ *        capitals
+ *        
+ *    
+ *    
+ *        tasmania_cities
+ *        
+ *    
+ *    true
+ *    true
+ *    true
+ *    
+ *        0
+ *        0
+ *    
+ *    
+ *    	
+ *    		[{"authority":"authority1","identifier":"identifier1"},]
+ *   	
+ *   	
+ *   		[{"name":"authority1","href":"http://www.authority1.org"},]
+ *   	
+ *   	true
+ *    
+ * 
+ *  }
+ * 
+ * + * @author eblondel + */ +public class RESTLayer21 extends RESTLayer{ + + + public RESTLayer21(Element layerElem) { + super(layerElem); + } + + public static RESTLayer21 build(String response) { + if(response == null) + return null; + + Element pb = JDOMBuilder.buildElement(response); + if(pb != null) + return new RESTLayer21(pb); + else + return null; + } + + /** + * Decodes the advertised property from the Geoserver Layer + * + */ + public boolean getAdvertised(){ + boolean advertised = true; + + final Element metadataRoot = layerElem.getChild("metadata"); + if(metadataRoot != null){ + final List metaElements = metadataRoot.getChildren(); + if(metaElements != null){ + for(Element el : metaElements){ + String key = el.getAttributeValue("key"); + if(key.matches("advertised")){ + advertised = Boolean.parseBoolean(el.getValue()); + } + } + } + } + return advertised; + } + + /** + * Decodes the list of AuthorityURLInfo from the GeoServer Layer + * + * @return the list of GSAuthorityURLInfoEncoder + */ + public List getEncodedAuthorityURLInfoList() { + List authorityURLList = null; + + final Element metadataRoot = layerElem.getChild("metadata"); + if (metadataRoot != null) { + final List metaElements = metadataRoot.getChildren(); + if (metaElements != null) { + for (Element element : metaElements) { + String key = element.getAttributeValue("key"); + if (key.matches("authorityURLs")) { + + String jsonStr = element.getValue(); + jsonStr = jsonStr.substring(2); + jsonStr = jsonStr.substring(0, + jsonStr.length() - 3); + + String[] items = jsonStr.split("\\}(,)\\{"); + authorityURLList = new ArrayList(items.length); + for (String item : items) { + String[] props = item.split(","); + + String[] kvp1 = props[0].split("\":"); + String name = kvp1[1].replace("\"", ""); + String[] kvp2 = props[1].split("\":"); + String href = kvp2[1].replace("\"", ""); + + authorityURLList + .add(new GSAuthorityURLInfoEncoder( + name, href)); + } + } + } + } + } + return authorityURLList; + } + + /** + * Decodes the list of IdentifierInfo from the GeoServer Layer + * + * @return the list of IdentifierInfoEncoder + */ + public List getEncodedIdentifierInfoList() { + List identifierList = null; + + final Element metadataRoot = layerElem.getChild("metadata"); + if (metadataRoot != null) { + final List metaElements = metadataRoot.getChildren(); + if (metaElements != null) { + for (Element element : metaElements) { + String key = element.getAttributeValue("key"); + if (key.matches("identifiers")) { + + String jsonStr = element.getValue(); + jsonStr = jsonStr.substring(2); + jsonStr = jsonStr.substring(0, + jsonStr.length() - 3); + + String[] items = jsonStr.split("\\}(,)\\{"); + identifierList = new ArrayList(items.length); + for (String item : items) { + String[] props = item.split(","); + + String[] kvp1 = props[0].split("\":"); + String authority = kvp1[1].replace("\"", ""); + String[] kvp2 = props[1].split("\":"); + String identifier = kvp2[1].replace("\"", ""); + + identifierList + .add(new GSIdentifierInfoEncoder( + authority, identifier)); + } + } + } + } + } + return identifierList; + } + +} diff --git a/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoder21Test.java b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoder21Test.java new file mode 100644 index 0000000..c31770a --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoder21Test.java @@ -0,0 +1,66 @@ +package it.geosolutions.geoserver.decoder; + +import it.geosolutions.geoserver.rest.decoder.RESTLayer; +import it.geosolutions.geoserver.rest.decoder.RESTLayer21; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import junit.framework.Assert; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.io.ClassPathResource; + +/** + * + * Apply Layer decoder tests on a GS 2.1 layer REST config + * + * @author eblondel + * + */ +public class LayerDecoder21Test{ + + RESTLayer21 layer; + + @Before + public void setUp() throws IOException{ + File layerFile = new ClassPathResource("testdata/layerExample21.xml").getFile(); + String layerString = FileUtils.readFileToString(layerFile); + layer = (RESTLayer21) RESTLayer21.build(layerString); + } + + @Test + public void testAdvertised(){ + Assert.assertEquals(true, layer.getAdvertised()); + } + + @Test + public void testAuthorityURLs() { + List authorityURLs = layer + .getEncodedAuthorityURLInfoList(); + System.out.println(authorityURLs.size()); + Assert.assertEquals("authority1", authorityURLs.get(0).getName()); + Assert.assertEquals("http://www.authority1.org", authorityURLs.get(0) + .getHref()); + Assert.assertEquals("authority2", authorityURLs.get(1).getName()); + Assert.assertEquals("http://www.authority2.org", authorityURLs.get(1) + .getHref()); + } + + @Test + public void testIdentifiers() { + List authorityURLs = layer + .getEncodedIdentifierInfoList(); + Assert.assertEquals("authority1", authorityURLs.get(0).getAuthority()); + Assert.assertEquals("identifier1", authorityURLs.get(0).getIdentifier()); + Assert.assertEquals("authority2", authorityURLs.get(1).getAuthority()); + Assert.assertEquals("identifier2", authorityURLs.get(1).getIdentifier()); + } + + +} \ No newline at end of file diff --git a/src/test/resources/testdata/layerExample21.xml b/src/test/resources/testdata/layerExample21.xml new file mode 100644 index 0000000..4a253a3 --- /dev/null +++ b/src/test/resources/testdata/layerExample21.xml @@ -0,0 +1,28 @@ + + tasmania_cities + / + VECTOR + + capitals + + + + tasmania_cities + + + true + true + true + + 0 + 0 + + + [{"authority":"authority1","identifier":"identifier1"},{"authority":"authority2","identifier":"identifier2"},] + [{"name":"authority1","href":"http://www.authority1.org"},{"name":"authority2","href":"http://www.authority2.org"},] + true + + \ No newline at end of file From a277d44e156dd4ea32dbafa9426cbbea1cdb842b Mon Sep 17 00:00:00 2001 From: eblondel Date: Fri, 18 Oct 2013 17:52:47 +0200 Subject: [PATCH 08/10] #102 - make tests based on GeoserverRESTTest VERSION --- .../encoder/feature/GSFeatureEncoderTest.java | 133 +++++++++--------- 1 file changed, 70 insertions(+), 63 deletions(-) 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 1da7fc8..05e4b8b 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 @@ -90,53 +90,57 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { "http://www.organization.org/metadata1"); fte.addMetadataLinkInfo(metadatalink); - GSLayerEncoder layerEncoder = new GSLayerEncoder(); - layerEncoder.setEnabled(true); - layerEncoder.setQueryable(true); - layerEncoder.setAdvertised(true); + GSLayerEncoder layerEncoder = null; + if(GeoserverRESTTest.VERSION != null){ + layerEncoder = new GSLayerEncoder(); + layerEncoder.setEnabled(true); + layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); - layerEncoder.setDefaultStyle("point"); - layerEncoder.addStyle("point2"); - layerEncoder.addStyle("point3"); - - // authorityURL - GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( - "authority1", "http://www.authority1.org"); - layerEncoder.addAuthorityURL(authorityURL); + layerEncoder.setDefaultStyle("point"); + layerEncoder.addStyle("point2"); + layerEncoder.addStyle("point3"); + + // authorityURL + GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org"); + layerEncoder.addAuthorityURL(authorityURL); - // identifier - GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( - "authority1", "identifier1"); - layerEncoder.addIdentifier(identifier); - + // identifier + GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( + "authority1", "identifier1"); + layerEncoder.addIdentifier(identifier); + - publisher.createWorkspace(DEFAULT_WS); + publisher.createWorkspace(DEFAULT_WS); - File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); + 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)); + // 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")); + 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); + // 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)); } - - assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); + } @Test - public void testIntegration_21() throws IOException { + public void testIntegration1() throws IOException { if (!enabled()) return; @@ -161,34 +165,37 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { "http://www.organization.org/metadata1"); fte.addMetadataLinkInfo(metadatalink); - //use of GSLayerEncoder specific to GS 2.1 - GSLayerEncoder21 layerEncoder = new GSLayerEncoder21(); - layerEncoder.setEnabled(true); - layerEncoder.setQueryable(true); - layerEncoder.setAdvertised(true); - - layerEncoder.setDefaultStyle("point"); - layerEncoder.addStyle("point2"); - layerEncoder.addStyle("point3"); - - // authorityURL - GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( - "authority1", "http://www.authority1.org"); - GSAuthorityURLInfoEncoder authorityURL2 = new GSAuthorityURLInfoEncoder( - "authority2", "http://www.authority2.org"); - layerEncoder.addAuthorityURL(authorityURL); - layerEncoder.addAuthorityURL(authorityURL2); - - // identifier - GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( - "authority1", "identifier1"); - GSIdentifierInfoEncoder identifier2 = new GSIdentifierInfoEncoder( - "authority2", "identifier2"); - layerEncoder.addIdentifier(identifier); - layerEncoder.addIdentifier(identifier2); - - publisher.createWorkspace(DEFAULT_WS); - assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); + //use of GSLayerEncoder for GS 2.1 & before + GSLayerEncoder21 layerEncoder = null; + if(GeoserverRESTTest.VERSION == null){ + layerEncoder = new GSLayerEncoder21(); + layerEncoder.setEnabled(true); + layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); + + layerEncoder.setDefaultStyle("point"); + layerEncoder.addStyle("point2"); + layerEncoder.addStyle("point3"); + + // authorityURL + GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org"); + GSAuthorityURLInfoEncoder authorityURL2 = new GSAuthorityURLInfoEncoder( + "authority2", "http://www.authority2.org"); + layerEncoder.addAuthorityURL(authorityURL); + layerEncoder.addAuthorityURL(authorityURL2); + + // identifier + GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( + "authority1", "identifier1"); + GSIdentifierInfoEncoder identifier2 = new GSIdentifierInfoEncoder( + "authority2", "identifier2"); + layerEncoder.addIdentifier(identifier); + layerEncoder.addIdentifier(identifier2); + + publisher.createWorkspace(DEFAULT_WS); + assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); + } } From 80b1767cd121a0de59dd56be272605c1c6e6fd71 Mon Sep 17 00:00:00 2001 From: eblondel Date: Sat, 19 Oct 2013 02:21:34 +0200 Subject: [PATCH 09/10] #106 master - fix tests to use GSVersionEncoder --- .../encoder/feature/GSFeatureEncoderTest.java | 109 ++++++++++-------- 1 file changed, 58 insertions(+), 51 deletions(-) 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 05e4b8b..a10f77f 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 @@ -23,6 +23,7 @@ import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; import it.geosolutions.geoserver.rest.GeoserverRESTTest; import it.geosolutions.geoserver.rest.decoder.RESTLayer; import it.geosolutions.geoserver.rest.decoder.RESTResource; +import it.geosolutions.geoserver.rest.decoder.about.GSVersionDecoder; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder21; import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder; @@ -90,50 +91,54 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { "http://www.organization.org/metadata1"); fte.addMetadataLinkInfo(metadatalink); - GSLayerEncoder layerEncoder = null; - if(GeoserverRESTTest.VERSION != null){ - layerEncoder = new GSLayerEncoder(); - layerEncoder.setEnabled(true); - layerEncoder.setQueryable(true); - layerEncoder.setAdvertised(true); + GSLayerEncoder layerEncoder = null; + if (!GSVersionDecoder.VERSION.getVersion(VERSION).equals( + GSVersionDecoder.VERSION.UNRECOGNIZED)) { + layerEncoder = new GSLayerEncoder(); + layerEncoder = new GSLayerEncoder(); + layerEncoder.setEnabled(true); + layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); - layerEncoder.setDefaultStyle("point"); - layerEncoder.addStyle("point2"); - layerEncoder.addStyle("point3"); - - // authorityURL - GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( - "authority1", "http://www.authority1.org"); - layerEncoder.addAuthorityURL(authorityURL); + layerEncoder.setDefaultStyle("point"); + layerEncoder.addStyle("point2"); + layerEncoder.addStyle("point3"); - // identifier - GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( - "authority1", "identifier1"); - layerEncoder.addIdentifier(identifier); - + // authorityURL + GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org"); + layerEncoder.addAuthorityURL(authorityURL); - publisher.createWorkspace(DEFAULT_WS); + // identifier + GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( + "authority1", "identifier1"); + layerEncoder.addIdentifier(identifier); - File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile(); + publisher.createWorkspace(DEFAULT_WS); - // test insert - boolean published = publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile); - assertTrue("publish() failed", published); - assertTrue(existsLayer(layerName)); + File zipFile = new ClassPathResource("testdata/resttestshp.zip") + .getFile(); - publisher.publishStyle(new File(new ClassPathResource("testdata").getFile(), - "default_point.sld")); + // test insert + boolean published = publisher.publishShp(DEFAULT_WS, storeName, + layerName, zipFile); + assertTrue("publish() failed", published); + assertTrue(existsLayer(layerName)); - // 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); - } + publisher.publishStyle(new File(new ClassPathResource("testdata") + .getFile(), "default_point.sld")); - assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); + // 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)); } } @@ -167,16 +172,17 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { //use of GSLayerEncoder for GS 2.1 & before GSLayerEncoder21 layerEncoder = null; - if(GeoserverRESTTest.VERSION == null){ - layerEncoder = new GSLayerEncoder21(); - layerEncoder.setEnabled(true); - layerEncoder.setQueryable(true); - layerEncoder.setAdvertised(true); - - layerEncoder.setDefaultStyle("point"); - layerEncoder.addStyle("point2"); - layerEncoder.addStyle("point3"); - + if (GSVersionDecoder.VERSION.getVersion(VERSION).equals( + GSVersionDecoder.VERSION.UNRECOGNIZED)) { + layerEncoder = new GSLayerEncoder21(); + layerEncoder.setEnabled(true); + layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); + + layerEncoder.setDefaultStyle("point"); + layerEncoder.addStyle("point2"); + layerEncoder.addStyle("point3"); + // authorityURL GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( "authority1", "http://www.authority1.org"); @@ -184,7 +190,7 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { "authority2", "http://www.authority2.org"); layerEncoder.addAuthorityURL(authorityURL); layerEncoder.addAuthorityURL(authorityURL2); - + // identifier GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( "authority1", "identifier1"); @@ -192,10 +198,11 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { "authority2", "identifier2"); layerEncoder.addIdentifier(identifier); layerEncoder.addIdentifier(identifier2); - - publisher.createWorkspace(DEFAULT_WS); - assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, layerEncoder)); - } + + publisher.createWorkspace(DEFAULT_WS); + assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, + layerEncoder)); + } } From 6689211b5d6218befc3484356b0eeabb023ee754 Mon Sep 17 00:00:00 2001 From: eblondel Date: Mon, 21 Oct 2013 11:28:29 +0200 Subject: [PATCH 10/10] #102 master - fix tests --- .../encoder/feature/GSFeatureEncoderTest.java | 152 ++++++------------ 1 file changed, 46 insertions(+), 106 deletions(-) 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 a10f77f..d67fd69 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 @@ -95,116 +95,56 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { if (!GSVersionDecoder.VERSION.getVersion(VERSION).equals( GSVersionDecoder.VERSION.UNRECOGNIZED)) { layerEncoder = new GSLayerEncoder(); - layerEncoder = new GSLayerEncoder(); - layerEncoder.setEnabled(true); - layerEncoder.setQueryable(true); - layerEncoder.setAdvertised(true); - - layerEncoder.setDefaultStyle("point"); - layerEncoder.addStyle("point2"); - layerEncoder.addStyle("point3"); - - // authorityURL - GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( - "authority1", "http://www.authority1.org"); - layerEncoder.addAuthorityURL(authorityURL); - - // identifier - GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( - "authority1", "identifier1"); - layerEncoder.addIdentifier(identifier); - - 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 testIntegration1() throws IOException { - - if (!enabled()) - return; - deleteAll(); - - GeoServerRESTPublisher publisher = new GeoServerRESTPublisher(RESTURL, RESTUSER, RESTPW); - - String storeName = "resttestshp"; - String layerName = "cities"; - - GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder(); - fte.setNativeName(layerName); - fte.setName(layerName + "_NEW2"); - fte.setTitle("title"); - fte.setNativeCRS("EPSG:4326"); - fte.setDescription("desc"); - fte.setEnabled(true); - - //metadataLink - GSMetadataLinkInfoEncoder metadatalink = new GSMetadataLinkInfoEncoder( - "text/xml", "ISO19115:2003", - "http://www.organization.org/metadata1"); - fte.addMetadataLinkInfo(metadatalink); - - //use of GSLayerEncoder for GS 2.1 & before - GSLayerEncoder21 layerEncoder = null; - if (GSVersionDecoder.VERSION.getVersion(VERSION).equals( + } else if (GSVersionDecoder.VERSION.getVersion(VERSION).equals( GSVersionDecoder.VERSION.UNRECOGNIZED)) { layerEncoder = new GSLayerEncoder21(); - layerEncoder.setEnabled(true); - layerEncoder.setQueryable(true); - layerEncoder.setAdvertised(true); - - layerEncoder.setDefaultStyle("point"); - layerEncoder.addStyle("point2"); - layerEncoder.addStyle("point3"); - - // authorityURL - GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( - "authority1", "http://www.authority1.org"); - GSAuthorityURLInfoEncoder authorityURL2 = new GSAuthorityURLInfoEncoder( - "authority2", "http://www.authority2.org"); - layerEncoder.addAuthorityURL(authorityURL); - layerEncoder.addAuthorityURL(authorityURL2); - - // identifier - GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( - "authority1", "identifier1"); - GSIdentifierInfoEncoder identifier2 = new GSIdentifierInfoEncoder( - "authority2", "identifier2"); - layerEncoder.addIdentifier(identifier); - layerEncoder.addIdentifier(identifier2); - - publisher.createWorkspace(DEFAULT_WS); - assertTrue(publisher.publishDBLayer(DEFAULT_WS, storeName, fte, - layerEncoder)); } + layerEncoder.setEnabled(true); + layerEncoder.setQueryable(true); + layerEncoder.setAdvertised(true); + + layerEncoder.setDefaultStyle("point"); + layerEncoder.addStyle("point2"); + layerEncoder.addStyle("point3"); + + // authorityURL + GSAuthorityURLInfoEncoder authorityURL = new GSAuthorityURLInfoEncoder( + "authority1", "http://www.authority1.org"); + layerEncoder.addAuthorityURL(authorityURL); + + // identifier + GSIdentifierInfoEncoder identifier = new GSIdentifierInfoEncoder( + "authority1", "identifier1"); + layerEncoder.addIdentifier(identifier); + + 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() {