gcube-sdi-suite/gcube-geoserver-client/src/test/java/org/gcube/spatial/data/clients/geoserver/GSTests.java

217 lines
7.2 KiB
Java

package org.gcube.spatial.data.clients.geoserver;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeTrue;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.sdi.test.GCubeSDITest;
import org.gcube.spatial.data.clients.SDIGenericPlugin;
import org.gcube.spatial.data.clients.geoserver.model.DataStoreRegistrationRequest;
import org.gcube.spatial.data.clients.geoserver.model.FeatureTypeInfo;
import org.gcube.spatial.data.clients.geoserver.model.FeatureTypeInfo.AttributeList;
import org.gcube.spatial.data.clients.model.engine.Engine;
import org.gcube.spatial.data.sdi.model.faults.RemoteException;
import org.gcube.spatial.data.sdi.utils.Files;
import org.gcube.spatial.data.sdi.utils.ISUtils;
import org.junit.Test;
public class GSTests extends GCubeSDITest{
protected GSRESTClient getClient() throws RemoteException, Exception {
SDIGenericPlugin obj=manager().getClientByEngineId(Engine.GS_ENGINE);
assertTrue(obj.getClass().isAssignableFrom(GeoServerPlugin.class));
return (GSRESTClient) obj.getRESTClient();
}
@Test
public void basics() throws RemoteException, Exception {
assumeTrue(isTestInfrastructureEnabled());
GSRESTClient client=getClient();
assertTrue(client.exist());
client.authenticate();
}
@Test
public void gets() throws RemoteException, Exception {
assumeTrue(isTestInfrastructureEnabled());
GSRESTClient client=getClient();
client.authenticate();
client.getWorkspaceNames().forEach((String ws)->{
try {
if(ws!=null) {
System.out.println("************************************ WS ***************************");
System.out.println(client.getWorkspace(ws));
List<String> sldList=client.getStylesNamesinWorkspace(ws);
System.out.println("*************** "+ws+" :SD "+sldList);
for(String sld:sldList)
if(sld!=null)try {
System.out.println(client.getSLD(sld));
}catch(RemoteException e ) {
System.err.println("Unable to read SLD "+sld+" from "+ws+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());}
System.out.println("WS "+ws+" ->"+client.getDataStoresInWorkspace(ws));
List<String> dsList=client.getDataStoresNamesInWorkspace(ws);
System.out.println("*************** "+ws+" : DS "+dsList);
for(String ds:dsList)
if(ds!=null) {
try{ System.out.println(client.getDataStore(ws, ds));
}catch(RemoteException e ) {
System.err.println("Unable to read DS "+ds+" from "+ws+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());}
}
List<String> ftList=client.getFeatureTypesInWorkspace(ws);
System.out.println("*************** "+ws+" : FT "+ftList);
for(String ft : ftList)
if(ft!=null)try {
FeatureTypeInfo ftInfo=client.getFeatureType(ws, ft);
assertTrue(ftInfo.getNativeName()!=null);
System.out.println(ftInfo);
}catch(RemoteException e ) {
System.err.println("Unable to read FT "+ft+" from "+ws+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());}
List<String> layerList=client.getLayers(ws);
System.out.println("*************** "+ws+" : LAYERS "+layerList);
for(String l : layerList)
if(l!=null)try {
System.out.println(client.getLayerInWorkspace(ws, l));
// System.out.println(client.getLayer(l));
}catch(RemoteException e ) {
System.err.println("Unable to read LA "+l+" from "+ws+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());}
}
}catch(Exception e) {
throw new RuntimeException("Unable to check "+ws+" : ",e);
}
});
// System.out.println("Listing layers without ws");
// client.getLayers().forEach((String l)->{
// try{System.out.println(client.getLayer(l));
// }catch(RemoteException e ) {System.err.println("Unable to read LA "+l+". Cause "+e.getResponseHTTPCode()+": "+e.getMessage());}
// catch(Exception e ) {System.err.println("Unable to read LA "+l+". Cause : "+e.getMessage());}
// });
}
@Test
public void CRUDS() throws RemoteException, Exception {
assumeTrue(isTestInfrastructureEnabled());
GSRESTClient client=getClient();
client.authenticate();
String ws=UUID.randomUUID().toString().replace("-", "_");
System.out.println("WS name is :"+ ws);
client.createWorkspace(ws);
client.getWorkspace(ws);
// SLD
String myStyle=UUID.randomUUID().toString().replace("-", "_");
System.out.println("style name is :"+ myStyle);
client.createStyle(myStyle,read("clustered_points.sld"));
assertTrue("SLD registered ",client.getStylesNames().contains(myStyle));
client.deleteStyle(myStyle, true, true);
assertFalse("SLD Removed",client.getStylesNames().contains(myStyle));
// DS
DataStoreRegistrationRequest dsReq=getDB();
System.out.println("Registering dsReq "+dsReq);
assumeNotNull(dsReq);
client.publishDataStore(ws,dsReq);
//FT
FeatureTypeInfo ft= new FeatureTypeInfo();
ft.setName(UUID.randomUUID().toString().replace("-", "_"));
ft.setNativeCRS("EPSG:4326");
List<FeatureTypeInfo.Attribute> atts=new ArrayList<FeatureTypeInfo.Attribute>();
atts.add(new FeatureTypeInfo.Attribute("the_geom",1,1,true,"com.vividsolutions.jts.geom.Point",0));
ft.setAttributes(new AttributeList(atts));
System.out.println("Writing "+ft);
client.createLayerAsFeatureType(ws, dsReq.getName(), ft);
}
@Test
public void specifics() throws RemoteException, Exception {
assumeTrue(isTestInfrastructureEnabled());
GSRESTClient client=getClient();
client.authenticate();
System.out.println(client.getFeatureType("gna_conc_65", "uuss15-19"));
}
private DataStoreRegistrationRequest getDB() {
List<ServiceEndpoint> eps=ISUtils.queryForServiceEndpoints("Database", "postgis");
System.out.println("EPS Size "+eps.size());
for (ServiceEndpoint se:eps) {
for(AccessPoint ap : se.profile().accessPoints().asCollection()) {
if(ap.propertyMap().containsKey("gs-tests")) {
if(Boolean.parseBoolean(ap.propertyMap().get("gs-tests").value())) {
System.out.println(new StringBuilder("Using AP ").
append(ap.name()).append(" from SE ").append(se.id()));
return new DataStoreRegistrationRequest(ap.name()).
param("host", se.profile().runtime().hostedOn()).
param("port", "5432").
param("database",ap.address().substring(ap.address().lastIndexOf('/'),ap.address().length()-1)).
param("user",ap.username()).
param("passwd",ISUtils.decryptString(ap.password())).
param("dbtype","postgis");
}
}
}
}
return null;
}
private static final String read(String toRead) throws IOException {
File f= Files.getFileFromResources(toRead);
return Files.readFileAsString(f.getAbsolutePath(), Charset.defaultCharset());
}
}