diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java index 011873e..d14eba7 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/RESTLayer.java @@ -25,7 +25,16 @@ package it.geosolutions.geoserver.rest.decoder; +import java.util.ArrayList; +import java.util.List; + import it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; +import it.geosolutions.geoserver.rest.encoder.authorityurl.AuthorityURLInfo; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.IdentifierInfo; +import it.geosolutions.geoserver.rest.encoder.metadatalink.GSMetadataLinkInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.metadatalink.ResourceMetadataLinkInfo; import org.jdom.Element; import org.jdom.Namespace; @@ -97,6 +106,18 @@ public class RESTLayer { this.layerElem = layerElem; } + public boolean getEnabled(){ + return Boolean.parseBoolean(layerElem.getChildText("enabled")); + } + + public boolean getQueryable(){ + return Boolean.parseBoolean(layerElem.getChildText("queryable")); + } + + public boolean getAdvertised(){ + return Boolean.parseBoolean(layerElem.getChildText("advertised")); + } + public String getName() { return layerElem.getChildText("name"); } @@ -164,6 +185,62 @@ public class RESTLayer { Element atom = resource.getChild("link", Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom")); return atom.getAttributeValue("href"); } + + + /** + * Decodes the list of AuthorityURLInfo from the GeoServer Layer + * + * @return the list of GSAuthorityURLInfoEncoder + */ + public List getEncodedAuthorityURLInfoList() { + List authorityURLList = null; + + final Element authorityURLsRoot = layerElem.getChild("authorityURLs"); + if (authorityURLsRoot != null) { + final List authorityURLs = authorityURLsRoot.getChildren(); + if (authorityURLs != null) { + authorityURLList = new ArrayList( + authorityURLs.size()); + for (Element authorityURL : authorityURLs) { + final GSAuthorityURLInfoEncoder authEnc = new GSAuthorityURLInfoEncoder(); + authEnc.setName(authorityURL + .getChildText(AuthorityURLInfo.name.name())); + authEnc.setHref(authorityURL + .getChildText(AuthorityURLInfo.href.name())); + authorityURLList.add(authEnc); + } + } + } + return authorityURLList; + } + + /** + * Decodes the list of IdentifierInfo from the GeoServer Layer + * + * @return the list of IdentifierInfoEncoder + */ + public List getEncodedIdentifierInfoList() { + List idList = null; + + final Element idRoot = layerElem.getChild("identifiers"); + if (idRoot != null) { + final List identifiers = idRoot.getChildren(); + if (identifiers != null) { + idList = new ArrayList( + identifiers.size()); + for (Element identifier : identifiers) { + final GSIdentifierInfoEncoder idEnc = new GSIdentifierInfoEncoder(); + idEnc.setAuthority(identifier + .getChildText(IdentifierInfo.authority.name())); + idEnc.setIdentifier(identifier + .getChildText(IdentifierInfo.identifier.name())); + idList.add(idEnc); + } + } + } + return idList; + } + // protected double getLatLonEdge(String edge) { // Element resource = layerElem.getChild("resource"); diff --git a/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java new file mode 100644 index 0000000..7962769 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/decoder/LayerDecoderTest.java @@ -0,0 +1,98 @@ +package it.geosolutions.geoserver.decoder; + +import it.geosolutions.geoserver.rest.decoder.RESTLayer; +import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder; +import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import junit.framework.Assert; + +import org.apache.commons.io.FileUtils; +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.io.ClassPathResource; + +/** + * + * @author eblondel + * + */ +public class LayerDecoderTest { + + RESTLayer layer; + + @Before + public void setUp() throws IOException{ + File layerFile = new ClassPathResource("testdata/layerExample.xml").getFile(); + String layerString = FileUtils.readFileToString(layerFile); + layer = RESTLayer.build(layerString); + } + + @Test + public void testEnabled(){ + Assert.assertEquals(true, layer.getEnabled()); + } + + @Test + public void testQueryable(){ + Assert.assertEquals(true, layer.getQueryable()); + } + + @Test + public void testAdvertised(){ + Assert.assertEquals(true, layer.getAdvertised()); + } + + @Test + public void testName() { + Assert.assertEquals("tasmania_cities", layer.getName()); + } + + @Test + public void testTypeString() { + Assert.assertEquals("VECTOR", layer.getTypeString()); + } + + @Test + public void testType(){ + Assert.assertEquals(RESTLayer.Type.VECTOR, layer.getType()); + } + + @Test + public void testDefaultStyle() { + Assert.assertEquals("capitals", layer.getDefaultStyle()); + } + + @Test + public void testResourceUrl() { + Assert.assertEquals( + "http://localhost:8080/geoserver/rest/workspaces/topp/datastores/taz_shapes/featuretypes/tasmania_cities.xml", + layer.getResourceUrl()); + } + + @Test + public void testAuthorityURLs() { + List authorityURLs = layer + .getEncodedAuthorityURLInfoList(); + Assert.assertEquals("authority1", authorityURLs.get(0).getName()); + Assert.assertEquals("http://www.authority1.org", authorityURLs.get(0) + .getHref()); + Assert.assertEquals("authority2", authorityURLs.get(1).getName()); + Assert.assertEquals("http://www.authority2.org", authorityURLs.get(1) + .getHref()); + } + + @Test + public void testIdentifiers() { + List authorityURLs = layer + .getEncodedIdentifierInfoList(); + Assert.assertEquals("authority1", authorityURLs.get(0).getAuthority()); + Assert.assertEquals("identifier1", authorityURLs.get(0).getIdentifier()); + Assert.assertEquals("authority2", authorityURLs.get(1).getAuthority()); + Assert.assertEquals("identifier2", authorityURLs.get(1).getIdentifier()); + } + +} diff --git a/src/test/resources/testdata/layerExample.xml b/src/test/resources/testdata/layerExample.xml new file mode 100644 index 0000000..af98cfa --- /dev/null +++ b/src/test/resources/testdata/layerExample.xml @@ -0,0 +1,40 @@ + + tasmania_cities + / + VECTOR + + capitals + + + + tasmania_cities + + + true + true + true + + 0 + 0 + + + + authority1 + http://www.authority1.org + + + authority2 + http://www.authority2.org + + + + + authority1 + identifier1 + + + authority2 + identifier2 + + + \ No newline at end of file