From 7deaac685f84313a4a0c2c7e229ce4125896014d Mon Sep 17 00:00:00 2001 From: etj Date: Thu, 22 Aug 2013 13:18:45 +0200 Subject: [PATCH] Added Util class. --- .../it/geosolutions/geoserver/rest/Util.java | 53 ++++++++++ .../geosolutions/geoserver/rest/UtilTest.java | 98 +++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/main/java/it/geosolutions/geoserver/rest/Util.java create mode 100644 src/test/java/it/geosolutions/geoserver/rest/UtilTest.java diff --git a/src/main/java/it/geosolutions/geoserver/rest/Util.java b/src/main/java/it/geosolutions/geoserver/rest/Util.java new file mode 100644 index 0000000..be27fe5 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/Util.java @@ -0,0 +1,53 @@ +/* + * 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; + +import it.geosolutions.geoserver.rest.decoder.RESTStyle; + +/** + * + * @author ETj (etj at geo-solutions.it) + */ +public class Util { + + /** + * Search for a stylename in global and in all workspaces. + */ + public static RESTStyle searchStyle(GeoServerRESTReader reader, String stylename) { + + RESTStyle style = reader.getStyle(stylename); + if(style != null) + return style; + + for (String workspace : reader.getWorkspaceNames()) { + style = reader.getStyle(workspace, stylename); + if(style != null) + return style; + } + + return null; + } +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/UtilTest.java b/src/test/java/it/geosolutions/geoserver/rest/UtilTest.java new file mode 100644 index 0000000..d5e6497 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/UtilTest.java @@ -0,0 +1,98 @@ +/* + * 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; + +import static it.geosolutions.geoserver.rest.GeoserverRESTTest.publisher; +import static it.geosolutions.geoserver.rest.GeoserverRESTTest.reader; +import it.geosolutions.geoserver.rest.decoder.RESTStyle; + +import java.io.File; +import java.io.IOException; +import org.junit.Test; +import static org.junit.Assert.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.ClassPathResource; + +/** + * + * @author etj + */ +public class UtilTest extends GeoserverRESTTest { + + private final static Logger LOGGER = LoggerFactory.getLogger(UtilTest.class); + + + @Test + public void testSearchStyle() throws IOException { + if (!enabled()) + return; + deleteAll(); + + final String WORKSPACE = "testWorkspace"; + final String STYLENAME = "restteststyle"; + final String STYLENAME2 = "restteststyle2"; + File sldFile = new ClassPathResource("testdata/restteststyle.sld").getFile(); + + publisher.createWorkspace(WORKSPACE); + + assertEquals(0, reader.getStyles().size()); + assertEquals(0, reader.getStyles(WORKSPACE).size()); + assertNull(Util.searchStyle(reader, STYLENAME)); + assertNull(Util.searchStyle(reader, STYLENAME2)); + + // insert style in workspace + assertTrue(publisher.publishStyleInWorkspace(WORKSPACE, sldFile, STYLENAME)); + assertTrue(reader.existsStyle(WORKSPACE, STYLENAME)); + assertFalse(reader.existsStyle(STYLENAME)); + assertEquals(0, reader.getStyles().size()); + assertEquals(1, reader.getStyles(WORKSPACE).size()); + assertNotNull(Util.searchStyle(reader, STYLENAME)); + assertNull(Util.searchStyle(reader, STYLENAME2)); + + // insert global style + assertTrue(publisher.publishStyle(sldFile, STYLENAME2)); + + assertFalse(reader.existsStyle(STYLENAME)); + assertTrue(reader.existsStyle(STYLENAME2)); + + assertTrue(reader.existsStyle(WORKSPACE, STYLENAME)); + assertFalse(reader.existsStyle(WORKSPACE, STYLENAME2)); + + assertEquals(1, reader.getStyles().size()); + assertEquals(1, reader.getStyles(WORKSPACE).size()); + + RESTStyle style; + style = Util.searchStyle(reader, STYLENAME); + assertEquals(STYLENAME, style.getName()); + assertEquals(WORKSPACE, style.getWorkspace()); + + style = Util.searchStyle(reader, STYLENAME2); + assertEquals(STYLENAME2, style.getName()); + assertEquals(null, style.getWorkspace()); + } + +}