From ff7f38359108c78573dad0cb14919bd96f9f1dd8 Mon Sep 17 00:00:00 2001 From: etj Date: Fri, 8 Apr 2016 19:28:54 +0200 Subject: [PATCH] #176 Fix tests related to styles in workspaces --- .../publisher/GeoserverRESTArcGridTest.java | 2 +- .../publisher/GeoserverRESTGeoTiffTest.java | 11 +- .../publisher/GeoserverRESTShapeTest.java | 2 +- .../publisher/GeoserverRESTStyleTest.java | 764 +++++++++--------- 4 files changed, 392 insertions(+), 387 deletions(-) diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTArcGridTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTArcGridTest.java index b068b4a..b2eedb7 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTArcGridTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTArcGridTest.java @@ -156,7 +156,7 @@ public class GeoserverRESTArcGridTest extends GeoserverRESTTest { assertTrue(reader.existsCoveragestore(DEFAULT_WS, storeName)); assertTrue(reader.existsCoverage(DEFAULT_WS, storeName, storeName)); RESTLayer layer = reader.getLayer(DEFAULT_WS, storeName); - assertEquals("mystyle", layer.getDefaultStyle()); + assertEquals(DEFAULT_WS + ":mystyle", layer.getDefaultStyle()); assertEquals(DEFAULT_WS, layer.getDefaultStyleWorkspace()); } diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java index f72f6b5..6e6a9ea 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTGeoTiffTest.java @@ -130,7 +130,8 @@ public class GeoserverRESTGeoTiffTest extends GeoserverRESTTest { } @Test - public void testGeoTiffWithStyleInWorkspace() throws IOException { + public void testGeoTiffWithStyleInWorkspace() throws IOException + { if (!enabled()) return; deleteAll(); @@ -155,11 +156,11 @@ public class GeoserverRESTGeoTiffTest extends GeoserverRESTTest { assertNotNull("publish() failed", pub); // Test exists - assertTrue(reader.existsCoveragestore(DEFAULT_WS, storeName)); - assertTrue(reader.existsCoverage(DEFAULT_WS, storeName, storeName)); + assertTrue("New coverage not found", reader.existsCoveragestore(DEFAULT_WS, storeName)); + assertTrue("New Store not found", reader.existsCoverage(DEFAULT_WS, storeName, storeName)); RESTLayer layer = reader.getLayer(DEFAULT_WS, storeName); - assertEquals("mystyle", layer.getDefaultStyle()); - assertEquals(DEFAULT_WS, layer.getDefaultStyleWorkspace()); + assertEquals("Bad default style", DEFAULT_WS + ":mystyle", layer.getDefaultStyle()); + assertEquals("Bad workspace for style", DEFAULT_WS, layer.getDefaultStyleWorkspace()); } @Test diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java index 2f28857..ea7e11c 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTShapeTest.java @@ -274,7 +274,7 @@ public class GeoserverRESTShapeTest extends GeoserverRESTTest { RESTLayer layer = reader.getLayer(layerName); // RESTLayer layerDecoder = new RESTLayer(layer); LOGGER.info("Layer style is " + layer.getDefaultStyle()); - assertEquals("Style not assigned properly", styleName, layer.getDefaultStyle()); + assertEquals("Style not assigned properly", DEFAULT_WS + ":" + styleName, layer.getDefaultStyle()); assertEquals("Style not assigned properly", DEFAULT_WS, layer.getDefaultStyleWorkspace()); // remove also datastore diff --git a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java index dc03abf..c89a522 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/publisher/GeoserverRESTStyleTest.java @@ -1,380 +1,384 @@ -/* - * 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( - "STYLE FOR TESTING PURPOSES", - "MODIFIED STYLE FOR TESTING"); - 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)); - } - -} \ No newline at end of file +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007-2016 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( + "STYLE FOR TESTING PURPOSES", + "MODIFIED STYLE FOR TESTING"); + 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("Error inserting style", publisher.publishStyleInWorkspace(WORKSPACE, sldFile)); + assertTrue("Style does not exist in workspace", reader.existsStyle(WORKSPACE, STYLENAME)); + + // this assertion is not enforced by geoserver, which is quite lenient in searching names + //assertFalse("Style should not be global", reader.existsStyle(STYLENAME)); + + // insert style again + assertFalse("Dup style not trapped", publisher.publishStyleInWorkspace(WORKSPACE, sldFile)); + assertTrue("Style does not exist in workspace (2)", reader.existsStyle(WORKSPACE, STYLENAME)); + // this assertion is not enforced by geoserver, which is quite lenient in searching names + //assertFalse("Style should not be global (2)", 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)); + } + +}