Merge pull request from branch 'master' of https://github.com/andypower/geoserver-manager.

Fix tests and style REST URLs.
Fix v110 SLD.
Closes #145.
This commit is contained in:
etj 2015-04-01 01:23:59 +02:00
commit c9f51cf388
4 changed files with 668 additions and 357 deletions

View File

@ -1,7 +1,7 @@
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2013 GeoSolutions S.A.S.
* Copyright (C) 2007,2015 GeoSolutions S.A.S.
* http://www.geo-solutions.it
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@ -356,6 +356,62 @@ public class GeoServerRESTPublisher {
return styleManager.publishStyle(sldFile, name);
}
/**
* Store and publish a Style, assigning it a name and choosing the raw format.
*
* @param sldBody the full SLD document as a String.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> if the operation completed successfully.
*/
public boolean publishStyle(String sldBody, String name, boolean raw) {
return styleManager.publishStyle(sldBody, name, raw);
}
/**
* Store and publish a Style, assigning it a name and choosing the raw format.
*
* @param sldFile the File containing the SLD document.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> if the operation completed successfully.
*/
public boolean publishStyle(File sldFile, String name, boolean raw) {
return styleManager.publishStyle(sldFile, name, raw);
}
/**
* Update a Style.
*
* @param sldFile the File containing the SLD document.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> if the operation completed successfully.
* @throws IllegalArgumentException if the style body or name are null or empty.
*/
public boolean updateStyle(final File sldFile, final String name, boolean raw)
throws IllegalArgumentException {
return styleManager.updateStyle(sldFile, name, raw);
}
/**
* Update a Style.
*
* @param sldBody the new SLD document as a String.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> if the operation completed successfully.
* @throws IllegalArgumentException if the style body or name are null or empty.
*/
public boolean updateStyle(final String sldBody, final String name, boolean raw)
throws IllegalArgumentException {
return styleManager.updateStyle(sldBody, name, raw);
}
/**
* Update a Style.
*
@ -1340,7 +1396,7 @@ public class GeoServerRESTPublisher {
* </ul>
*/
public enum Format {
XML, JSON, HTML, SLD;
XML, JSON, HTML, SLD, SLD_1_1_0;
/**
* Gets the mime type from a format.
@ -1358,6 +1414,8 @@ public class GeoServerRESTPublisher {
return "application/json";
case SLD:
return "application/vnd.ogc.sld+xml";
case SLD_1_1_0:
return "application/vnd.ogc.se+xml";
default:
return null;
}

View File

@ -1,7 +1,7 @@
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2013 GeoSolutions S.A.S.
* Copyright (C) 2007,2015 GeoSolutions S.A.S.
* http://www.geo-solutions.it
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@ -29,11 +29,25 @@ import it.geosolutions.geoserver.rest.HTTPUtils;
import it.geosolutions.geoserver.rest.Util;
import it.geosolutions.geoserver.rest.decoder.RESTStyle;
import it.geosolutions.geoserver.rest.decoder.RESTStyleList;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
*
@ -259,6 +273,136 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
String result = HTTPUtils.post(sUrl, sldFile, GeoServerRESTPublisher.Format.SLD.getContentType(), gsuser, gspass);
return result != null;
}
/**
* Store and publish a Style, assigning it a name and choosing the raw
* format.
*
* @param sldBody the full SLD document as a String.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> if the operation completed successfully.
*/
public boolean publishStyle(final String sldBody, final String name, final boolean raw) {
/*
* This is the equivalent call with cUrl:
*
* {@code curl -u admin:geoserver -XPOST \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \
* http://$GSIP:$GSPORT/$SERVLET/rest/styles?name=$name&raw=$raw}
*/
if (sldBody == null || sldBody.isEmpty()) {
throw new IllegalArgumentException("The style body may not be null or empty");
}
String sUrl = buildPostUrl(null, name);
sUrl += "&raw=" + raw;
LOGGER.debug("POSTing new style " + name + " to " + sUrl);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldBody)){
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
String result = HTTPUtils.post(sUrl, sldBody, contentType, gsuser, gspass);
return result != null;
}
/**
* Store and publish a Style, assigning it a name and choosing the raw
* format.
*
* @param sldFile the File containing the SLD document.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> if the operation completed successfully.
*/
public boolean publishStyle(final File sldFile, final String name, final boolean raw) {
/*
* This is the equivalent call with cUrl:
*
* {@code curl -u admin:geoserver -XPOST \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \
* http://$GSIP:$GSPORT/$SERVLET/rest/styles?name=$name&raw=$raw}
*/
String sUrl = buildPostUrl(null, name);
sUrl += "&raw=" + raw;
LOGGER.debug("POSTing new style " + name + " to " + sUrl);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldFile)){
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
String result = HTTPUtils.post(sUrl, sldFile, contentType, gsuser, gspass);
return result != null;
}
/**
* Update a Style.
*
* @param sldFile the File containing the SLD document.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> if the operation completed successfully.
* @throws IllegalArgumentException if the style body or name are null or empty.
*/
public boolean updateStyle(final File sldFile, final String name, final boolean raw)
throws IllegalArgumentException {
/*
* This is the equivalent call with cUrl:
*
* {@code curl -u admin:geoserver -XPUT \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \
* http://$GSIP:$GSPORT/$SERVLET/rest/styles?name=$name&raw=$raw}
*/
if (sldFile == null) {
throw new IllegalArgumentException("Unable to updateStyle using a null parameter file");
} else if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("The style name may not be null or empty");
}
String sUrl = buildUrl(null, name, null);
sUrl += "?raw=" + raw;
LOGGER.debug("POSTing style " + name + " to " + sUrl);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldFile)){
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
String result = HTTPUtils.put(sUrl, sldFile, contentType, gsuser, gspass);
return result != null;
}
/**
* Update a Style.
*
* @param sldBody the new SLD document as a String.
* @param name the Style name.
* @param raw the raw format
*
* @return <TT>true</TT> if the operation completed successfully.
* @throws IllegalArgumentException if the style body or name are null or empty.
*/
public boolean updateStyle(final String sldBody, final String name, final boolean raw)
throws IllegalArgumentException {
/*
* This is the equivalent call with cUrl:
*
* {@code curl -u admin:geoserver -XPUT \ -H 'Content-type: application/vnd.ogc.sld+xml' \ -d @$FULLSLD \
* http://$GSIP:$GSPORT/$SERVLET/rest/styles?name=$name&raw=$raw}
*/
if (sldBody == null || sldBody.isEmpty()) {
throw new IllegalArgumentException("The style body may not be null or empty");
} else if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("The style name may not be null or empty");
}
String sUrl = buildUrl(null, name, null);
sUrl += "&raw=" + raw;
LOGGER.debug("POSTing style " + name + " to " + sUrl);
String contentType = GeoServerRESTPublisher.Format.SLD.getContentType();
if(!this.checkSLD10Version(sldBody)){
contentType = GeoServerRESTPublisher.Format.SLD_1_1_0.getContentType();
}
String result = HTTPUtils.put(sUrl, sldBody, contentType, gsuser, gspass);
return result != null;
}
/**
* Update a Style.
@ -596,4 +740,52 @@ public class GeoServerRESTStyleManager extends GeoServerRESTAbstractManager {
return sUrl.toString();
}
private boolean checkSLD10Version(String sldBody) {
boolean result = false;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream stream = new ByteArrayInputStream(sldBody.getBytes(StandardCharsets.UTF_8));
Document doc = builder.parse(stream);
result = this.checkSLD10Version(doc);
} catch (SAXException ex) {
LOGGER.error("Error parsing SLD file: " + ex);
} catch (IOException ex) {
LOGGER.error("Error parsing SLD file: " + ex);
} catch (ParserConfigurationException ex) {
LOGGER.error("Error parsing SLD file: " + ex);
}
return result;
}
private boolean checkSLD10Version(File fileSLD) {
boolean result = false;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(fileSLD);
result = this.checkSLD10Version(doc);
} catch (SAXException ex) {
LOGGER.error("Error parsing SLD file: " + ex);
} catch (IOException ex) {
LOGGER.error("Error parsing SLD file: " + ex);
} catch (ParserConfigurationException ex) {
LOGGER.error("Error parsing SLD file: " + ex);
}
return result;
}
private boolean checkSLD10Version(Document doc) {
boolean result = false;
try {
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//@version='1.0.0'");
result = (Boolean)expr.evaluate(doc, XPathConstants.BOOLEAN);
} catch (XPathExpressionException ex) {
LOGGER.error("Error parsing SLD file: " + ex);
}
return result;
}
}

View File

@ -1,354 +1,380 @@
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2013 GeoSolutions S.A.S.
* http://www.geo-solutions.it
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package it.geosolutions.geoserver.rest.publisher;
import it.geosolutions.geoserver.rest.GeoserverRESTTest;
import static it.geosolutions.geoserver.rest.GeoserverRESTTest.publisher;
import static it.geosolutions.geoserver.rest.GeoserverRESTTest.reader;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.decoder.RESTStyle;
import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.jdom.Element;
import org.jdom.Namespace;
import org.junit.Test;
import static org.junit.Assert.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
/**
* Testcase for publishing layers on geoserver. We need a running GeoServer to
* properly run the tests. If such geoserver instance cannot be contacted, tests
* will be skipped.
*
* @author etj
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/
public class GeoserverRESTStyleTest extends GeoserverRESTTest {
private final static Logger LOGGER = LoggerFactory
.getLogger(GeoserverRESTStyleTest.class);
@Test
public void testStyles() throws IOException {
if (!enabled())
return;
deleteAll();
assertEquals(0, reader.getStyles().size());
final String STYLENAME = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// insert style
assertTrue(publisher.publishStyle(sldFile));
assertTrue(reader.existsStyle(STYLENAME));
assertFalse(publisher.publishStyle(sldFile));
assertTrue(reader.existsStyle(STYLENAME));
RESTStyle style = reader.getStyle(STYLENAME);
assertEquals(STYLENAME, style.getName());
assertNull(style.getWorkspace());
String sld = reader.getSLD(STYLENAME);
assertNotNull(sld);
Element styleEl = JDOMBuilder.buildElement(sld);
assertNotNull(styleEl);
Namespace SLDNS = Namespace.getNamespace("sld",
"http://www.opengis.net/sld");
try {
assertEquals(STYLENAME, styleEl.getChild("NamedLayer", SLDNS)
.getChild("Name", SLDNS).getText());
assertEquals(
"STYLE FOR TESTING PURPOSES",
styleEl.getChild("NamedLayer", SLDNS)
.getChild("UserStyle", SLDNS)
.getChild("Title", SLDNS).getText());
} catch (NullPointerException npe) {
fail("Error in SLD");
}
// assertEquals(1475, sld.length());
assertEquals(1, reader.getStyles().size());
}
protected void cleanupTestStyle(final String styleName) {
// dry run delete to work in a known state
if (reader.existsStyle(styleName)) {
LOGGER.info("Clearing stale test style " + styleName);
boolean ok = publisher.removeStyle(styleName);
if (!ok) {
fail("Could not unpublish style " + styleName);
}
}
assertFalse("Cleanup failed", reader.existsStyle(styleName));
}
@Test
public void testPublishDeleteStyleFile() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
// Assume.assumeTrue(enabled);
final String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// known state?
cleanupTestStyle(styleName);
// test insert
boolean published = publisher.publishStyle(sldFile); // Will take the
// name from sld
// contents
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName));
sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile();
published = publisher.updateStyle(sldFile, styleName); // update
assertTrue("update() failed", published);
// test delete
boolean ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
}
@Test
public void testPublishDeleteStyleString() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
// Assume.assumeTrue(enabled);
String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// known state?
cleanupTestStyle(styleName);
// test insert
String sldContent = IOUtils.toString(new FileInputStream(sldFile));
boolean published = publisher.publishStyle(sldContent); // Will take the
// name from sld
// contents
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName));
// test delete
boolean ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
styleName = "restteststyle_with_name";
// test insert with name
published = publisher.publishStyle(sldContent, styleName); // Will set
// the name
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName));
String newSldContent = sldContent.replace(
"<sld:Title>STYLE FOR TESTING PURPOSES</sld:Title>",
"<sld:Title>MODIFIED STYLE FOR TESTING</sld:Title>");
published = publisher.updateStyle(newSldContent, styleName); // update
assertTrue("publish() failed", published);
// test delete
ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
}
@Test
public void testUpdateDefaultStyle() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
deleteAll();
String storeName = "resttestshp";
String layerName = "cities";
final String styleName = "restteststyle";
{
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
cleanupTestStyle(styleName);
boolean sldpublished = publisher.publishStyle(sldFile); // Will take
// the name
// from sld
// contents
assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName));
}
final String styleName2 = "restteststyle2";
{
File sldFile = new ClassPathResource("testdata/restteststyle2.sld")
.getFile();
cleanupTestStyle(styleName2);
boolean sldpublished = publisher.publishStyle(sldFile, styleName2);
assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName2));
}
File zipFile = new ClassPathResource("testdata/resttestshp.zip")
.getFile();
assertTrue(publisher.createWorkspace(DEFAULT_WS));
// test insert
boolean published = publisher.publishShp(DEFAULT_WS, storeName,
layerName, zipFile, "EPSG:4326", styleName);
assertTrue("publish() failed", published);
assertTrue(existsLayer(layerName));
{
RESTLayer layer = reader.getLayer(layerName);
LOGGER.info("Layer style is " + layer.getDefaultStyle());
assertEquals(styleName, layer.getDefaultStyle());
}
GSLayerEncoder le = new GSLayerEncoder();
le.setDefaultStyle(styleName2);
publisher.configureLayer(DEFAULT_WS, layerName, le);
{
RESTLayer layer = reader.getLayer(layerName);
LOGGER.info("Layer style is " + layer.getDefaultStyle());
assertEquals(styleName2, layer.getDefaultStyle());
}
// remove layer and datastore
boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName,
true);
assertTrue("removeDatastore() failed", dsRemoved);
}
@Test
public void testStylesInWorkspace() throws IOException {
if (!enabled())
return;
deleteAll();
final String WORKSPACE = "testWorkspace";
final String STYLENAME = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile();
publisher.createWorkspace(WORKSPACE);
assertEquals(0, reader.getStyles().size());
assertEquals(0, reader.getStyles(WORKSPACE).size());
// insert style
assertTrue(publisher.publishStyleInWorkspace(WORKSPACE, sldFile));
assertTrue(reader.existsStyle(WORKSPACE, STYLENAME));
assertFalse(reader.existsStyle(STYLENAME));
// insert style again
assertFalse(publisher.publishStyleInWorkspace(WORKSPACE, sldFile));
assertTrue(reader.existsStyle(WORKSPACE, STYLENAME));
assertFalse(reader.existsStyle(STYLENAME));
String sld = reader.getSLD(WORKSPACE, STYLENAME);
assertNotNull(sld);
RESTStyle style = reader.getStyle(WORKSPACE, STYLENAME);
assertEquals(STYLENAME, style.getName());
assertEquals(WORKSPACE, style.getWorkspace());
Element styleEl = JDOMBuilder.buildElement(sld);
assertNotNull(styleEl);
Namespace SLDNS = Namespace.getNamespace("sld",
"http://www.opengis.net/sld");
try {
assertEquals(STYLENAME, styleEl.getChild("NamedLayer", SLDNS)
.getChild("Name", SLDNS).getText());
assertEquals(
"STYLE FOR TESTING PURPOSES",
styleEl.getChild("NamedLayer", SLDNS)
.getChild("UserStyle", SLDNS)
.getChild("Title", SLDNS).getText());
} catch (NullPointerException npe) {
fail("Error in SLD");
}
// assertEquals(1475, sld.length());
assertEquals(0, reader.getStyles().size());
assertEquals(1, reader.getStyles(WORKSPACE).size());
}
@Test
public void testRemoveStylesInWorkspace() throws IOException {
if (!enabled())
return;
deleteAll();
final String WORKSPACE = "testWorkspace";
final String STYLENAME = "restteststyle";
final File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile();
publisher.createWorkspace(WORKSPACE);
assertEquals(0, reader.getStyles(WORKSPACE).size());
// insert style
assertTrue(publisher.publishStyleInWorkspace(WORKSPACE, sldFile));
assertEquals(1, reader.getStyles(WORKSPACE).size());
assertTrue(reader.existsStyle(WORKSPACE, STYLENAME));
// remove style
assertTrue(publisher.removeStyleInWorkspace(WORKSPACE, STYLENAME, true));
assertEquals(0, reader.getStyles(WORKSPACE).size());
assertFalse(reader.existsStyle(WORKSPACE, STYLENAME));
}
}
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2015 GeoSolutions S.A.S.
* http://www.geo-solutions.it
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package it.geosolutions.geoserver.rest.publisher;
import it.geosolutions.geoserver.rest.GeoserverRESTTest;
import static it.geosolutions.geoserver.rest.GeoserverRESTTest.publisher;
import static it.geosolutions.geoserver.rest.GeoserverRESTTest.reader;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.decoder.RESTStyle;
import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.jdom.Element;
import org.jdom.Namespace;
import org.junit.Test;
import static org.junit.Assert.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
/**
* Testcase for publishing layers on geoserver. We need a running GeoServer to
* properly run the tests. If such geoserver instance cannot be contacted, tests
* will be skipped.
*
* @author etj
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/
public class GeoserverRESTStyleTest extends GeoserverRESTTest {
private final static Logger LOGGER = LoggerFactory
.getLogger(GeoserverRESTStyleTest.class);
@Test
public void testStyles() throws IOException {
if (!enabled())
return;
deleteAll();
assertEquals(0, reader.getStyles().size());
final String STYLENAME = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// insert style
assertTrue(publisher.publishStyle(sldFile));
assertTrue(reader.existsStyle(STYLENAME));
assertFalse(publisher.publishStyle(sldFile));
assertTrue(reader.existsStyle(STYLENAME));
// insert style v110
final String STYLENAMEV110 = "restteststyleV110";
File sldFileV110 = new ClassPathResource("testdata/" + STYLENAMEV110 + ".sld").getFile();
assertTrue(publisher.publishStyle(sldFileV110, STYLENAMEV110, true));
assertTrue(reader.existsStyle(STYLENAMEV110));
assertFalse(publisher.publishStyle(sldFileV110, STYLENAMEV110, true));
RESTStyle style = reader.getStyle(STYLENAME);
assertEquals(STYLENAME, style.getName());
assertNull(style.getWorkspace());
String sld = reader.getSLD(STYLENAME);
assertNotNull(sld);
Element styleEl = JDOMBuilder.buildElement(sld);
assertNotNull(styleEl);
Namespace SLDNS = Namespace.getNamespace("sld",
"http://www.opengis.net/sld");
try {
assertEquals(STYLENAME, styleEl.getChild("NamedLayer", SLDNS)
.getChild("Name", SLDNS).getText());
assertEquals("STYLE FOR TESTING PURPOSES",
styleEl.getChild("NamedLayer", SLDNS)
.getChild("UserStyle", SLDNS)
.getChild("Title", SLDNS).getText());
} catch (NullPointerException npe) {
fail("Error in SLD");
}
// assertEquals(1475, sld.length());
assertEquals(2, reader.getStyles().size());
}
protected void cleanupTestStyle(final String styleName) {
// dry run delete to work in a known state
if (reader.existsStyle(styleName)) {
LOGGER.info("Clearing stale test style " + styleName);
boolean ok = publisher.removeStyle(styleName);
if (!ok) {
fail("Could not unpublish style " + styleName);
}
}
assertFalse("Cleanup failed", reader.existsStyle(styleName));
}
@Test
public void testPublishDeleteStyleFile() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
// Assume.assumeTrue(enabled);
final String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
final String STYLENAMEV110 = "restteststyleV110";
File sldFileV110 = new ClassPathResource("testdata/" + STYLENAMEV110 + ".sld")
.getFile();
// known state?
cleanupTestStyle(styleName);
cleanupTestStyle(STYLENAMEV110);
// test insert
boolean published = publisher.publishStyle(sldFile); // Will take the
// name from sld
// contents
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName));
sldFile = new ClassPathResource("testdata/restteststyle2.sld").getFile();
published = publisher.updateStyle(sldFile, styleName); // update
assertTrue("update() failed", published);
// test delete
boolean ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
published = publisher.publishStyle(sldFileV110, STYLENAMEV110, true);
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(STYLENAMEV110));
boolean updated = publisher.updateStyle(sldFileV110, STYLENAMEV110, true);
assertTrue("update() failed", updated);
// test delete
ok = publisher.removeStyle(STYLENAMEV110);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(STYLENAMEV110));
}
@Test
public void testPublishDeleteStyleString() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
// Assume.assumeTrue(enabled);
String styleName = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
// known state?
cleanupTestStyle(styleName);
// test insert
String sldContent = IOUtils.toString(new FileInputStream(sldFile));
boolean published = publisher.publishStyle(sldContent); // Will take the
// name from sld
// contents
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName));
// test delete
boolean ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
styleName = "restteststyle_with_name";
// test insert with name
published = publisher.publishStyle(sldContent, styleName); // Will set
// the name
assertTrue("publish() failed", published);
assertTrue(reader.existsStyle(styleName));
String newSldContent = sldContent.replace(
"<sld:Title>STYLE FOR TESTING PURPOSES</sld:Title>",
"<sld:Title>MODIFIED STYLE FOR TESTING</sld:Title>");
published = publisher.updateStyle(newSldContent, styleName); // update
assertTrue("publish() failed", published);
// test delete
ok = publisher.removeStyle(styleName);
assertTrue("Unpublish() failed", ok);
assertFalse(reader.existsStyle(styleName));
}
@Test
public void testUpdateDefaultStyle() throws FileNotFoundException,
IOException {
if (!enabled()) {
return;
}
deleteAll();
String storeName = "resttestshp";
String layerName = "cities";
final String styleName = "restteststyle";
{
File sldFile = new ClassPathResource("testdata/restteststyle.sld")
.getFile();
cleanupTestStyle(styleName);
boolean sldpublished = publisher.publishStyle(sldFile); // Will take
// the name
// from sld
// contents
assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName));
}
final String styleName2 = "restteststyle2";
{
File sldFile = new ClassPathResource("testdata/restteststyle2.sld")
.getFile();
cleanupTestStyle(styleName2);
boolean sldpublished = publisher.publishStyle(sldFile, styleName2);
assertTrue("style publish() failed", sldpublished);
assertTrue(reader.existsStyle(styleName2));
}
File zipFile = new ClassPathResource("testdata/resttestshp.zip")
.getFile();
assertTrue(publisher.createWorkspace(DEFAULT_WS));
// test insert
boolean published = publisher.publishShp(DEFAULT_WS, storeName,
layerName, zipFile, "EPSG:4326", styleName);
assertTrue("publish() failed", published);
assertTrue(existsLayer(layerName));
{
RESTLayer layer = reader.getLayer(layerName);
LOGGER.info("Layer style is " + layer.getDefaultStyle());
assertEquals(styleName, layer.getDefaultStyle());
}
GSLayerEncoder le = new GSLayerEncoder();
le.setDefaultStyle(styleName2);
publisher.configureLayer(DEFAULT_WS, layerName, le);
{
RESTLayer layer = reader.getLayer(layerName);
LOGGER.info("Layer style is " + layer.getDefaultStyle());
assertEquals(styleName2, layer.getDefaultStyle());
}
// remove layer and datastore
boolean dsRemoved = publisher.removeDatastore(DEFAULT_WS, storeName,
true);
assertTrue("removeDatastore() failed", dsRemoved);
}
@Test
public void testStylesInWorkspace() throws IOException {
if (!enabled())
return;
deleteAll();
final String WORKSPACE = "testWorkspace";
final String STYLENAME = "restteststyle";
File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile();
publisher.createWorkspace(WORKSPACE);
assertEquals(0, reader.getStyles().size());
assertEquals(0, reader.getStyles(WORKSPACE).size());
// insert style
assertTrue(publisher.publishStyleInWorkspace(WORKSPACE, sldFile));
assertTrue(reader.existsStyle(WORKSPACE, STYLENAME));
assertFalse(reader.existsStyle(STYLENAME));
// insert style again
assertFalse(publisher.publishStyleInWorkspace(WORKSPACE, sldFile));
assertTrue(reader.existsStyle(WORKSPACE, STYLENAME));
assertFalse(reader.existsStyle(STYLENAME));
String sld = reader.getSLD(WORKSPACE, STYLENAME);
assertNotNull(sld);
RESTStyle style = reader.getStyle(WORKSPACE, STYLENAME);
assertEquals(STYLENAME, style.getName());
assertEquals(WORKSPACE, style.getWorkspace());
Element styleEl = JDOMBuilder.buildElement(sld);
assertNotNull(styleEl);
Namespace SLDNS = Namespace.getNamespace("sld",
"http://www.opengis.net/sld");
try {
assertEquals(STYLENAME, styleEl.getChild("NamedLayer", SLDNS)
.getChild("Name", SLDNS).getText());
assertEquals(
"STYLE FOR TESTING PURPOSES",
styleEl.getChild("NamedLayer", SLDNS)
.getChild("UserStyle", SLDNS)
.getChild("Title", SLDNS).getText());
} catch (NullPointerException npe) {
fail("Error in SLD");
}
// assertEquals(1475, sld.length());
assertEquals(0, reader.getStyles().size());
assertEquals(1, reader.getStyles(WORKSPACE).size());
}
@Test
public void testRemoveStylesInWorkspace() throws IOException {
if (!enabled())
return;
deleteAll();
final String WORKSPACE = "testWorkspace";
final String STYLENAME = "restteststyle";
final File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile();
publisher.createWorkspace(WORKSPACE);
assertEquals(0, reader.getStyles(WORKSPACE).size());
// insert style
assertTrue(publisher.publishStyleInWorkspace(WORKSPACE, sldFile));
assertEquals(1, reader.getStyles(WORKSPACE).size());
assertTrue(reader.existsStyle(WORKSPACE, STYLENAME));
// remove style
assertTrue(publisher.removeStyleInWorkspace(WORKSPACE, STYLENAME, true));
assertEquals(0, reader.getStyles(WORKSPACE).size());
assertFalse(reader.existsStyle(WORKSPACE, STYLENAME));
}
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor version="1.1.0"
xmlns="http://www.opengis.net/sld"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:se="http://www.opengis.net/se"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd">
<NamedLayer>
<se:Name>OCEANSEA_1M:Foundation</se:Name>
<UserStyle>
<se:Name>GEOSYM</se:Name>
<IsDefault>1</IsDefault>
<se:FeatureTypeStyle>
<se:FeatureTypeName>Foundation</se:FeatureTypeName>
<se:Rule>
<se:Name>main</se:Name>
<se:PolygonSymbolizer uom="http://www.opengeospatial.org/se/units/pixel">
<se:Name>MySymbol</se:Name>
<se:Description>
<se:Title>Example Symbol</se:Title>
<se:Abstract>This is just a simple example.</se:Abstract>
</se:Description>
<se:Geometry>
<ogc:PropertyName>GEOMETRY</ogc:PropertyName>
</se:Geometry>
<se:Fill>
<se:SvgParameter name="fill">#96C3F5</se:SvgParameter>
</se:Fill>
</se:PolygonSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>