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 * 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 * http://www.geo-solutions.it
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * 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); 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. * Update a Style.
* *
@ -1340,7 +1396,7 @@ public class GeoServerRESTPublisher {
* </ul> * </ul>
*/ */
public enum Format { public enum Format {
XML, JSON, HTML, SLD; XML, JSON, HTML, SLD, SLD_1_1_0;
/** /**
* Gets the mime type from a format. * Gets the mime type from a format.
@ -1358,6 +1414,8 @@ public class GeoServerRESTPublisher {
return "application/json"; return "application/json";
case SLD: case SLD:
return "application/vnd.ogc.sld+xml"; return "application/vnd.ogc.sld+xml";
case SLD_1_1_0:
return "application/vnd.ogc.se+xml";
default: default:
return null; return null;
} }

View File

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