2016-09-07 15:07:46 +02:00
|
|
|
import it.geosolutions.geonetwork.util.GNSearchRequest;
|
2016-09-08 15:29:57 +02:00
|
|
|
import it.geosolutions.geonetwork.util.GNSearchResponse;
|
2016-09-07 15:07:46 +02:00
|
|
|
|
2016-12-02 16:59:04 +01:00
|
|
|
import org.gcube.common.encryption.StringEncrypter;
|
2016-09-07 15:07:46 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.spatial.data.geonetwork.GeoNetwork;
|
2016-09-08 15:29:57 +02:00
|
|
|
import org.gcube.spatial.data.geonetwork.GeoNetworkPublisher;
|
2016-09-07 15:07:46 +02:00
|
|
|
import org.gcube.spatial.data.geonetwork.GeoNetworkReader;
|
|
|
|
import org.gcube.spatial.data.geonetwork.LoginLevel;
|
2016-09-08 15:29:57 +02:00
|
|
|
import org.gcube.spatial.data.geonetwork.configuration.Configuration;
|
|
|
|
import org.gcube.spatial.data.geonetwork.model.Account;
|
|
|
|
import org.gcube.spatial.data.geonetwork.model.Account.Type;
|
2017-02-01 15:47:11 +01:00
|
|
|
import org.junit.Test;
|
2016-09-07 15:07:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* Aug 31, 2016
|
|
|
|
*/
|
|
|
|
public class GeonetworkQueryTest {
|
|
|
|
|
2016-09-08 15:29:57 +02:00
|
|
|
private static final int MAX = 10;
|
|
|
|
|
2016-12-02 16:59:04 +01:00
|
|
|
//private String[] scopes = {"/gcube/devNext/NextNext"};
|
2016-09-07 15:07:46 +02:00
|
|
|
|
2017-05-15 14:45:20 +02:00
|
|
|
private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/SIASPA"};
|
2016-09-07 15:07:46 +02:00
|
|
|
|
2017-03-09 11:22:02 +01:00
|
|
|
//private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/fisheriesandecosystematmii"};
|
2017-02-01 15:47:11 +01:00
|
|
|
|
2017-03-24 17:38:36 +01:00
|
|
|
//private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research"};
|
2017-03-09 11:22:02 +01:00
|
|
|
|
|
|
|
//private String[] scopesProd = {"/d4science.research-infrastructures.eu"};
|
|
|
|
|
2017-03-24 17:38:36 +01:00
|
|
|
private LoginLevel loginLevel = LoginLevel.CKAN;
|
2016-09-08 15:29:57 +02:00
|
|
|
|
2016-12-02 16:59:04 +01:00
|
|
|
private Type accountType = Type.SCOPE;
|
2016-09-08 15:29:57 +02:00
|
|
|
|
2017-03-24 17:38:36 +01:00
|
|
|
private String textToSearch = "geo_fea";
|
2017-03-09 11:22:02 +01:00
|
|
|
|
2017-03-24 17:38:36 +01:00
|
|
|
@Test
|
2016-09-07 15:07:46 +02:00
|
|
|
public void getCount() throws Exception{
|
|
|
|
try{
|
2016-12-02 16:59:04 +01:00
|
|
|
for(String scope:scopesProd){
|
2016-09-07 15:07:46 +02:00
|
|
|
ScopeProvider.instance.set(scope);
|
2016-09-08 15:29:57 +02:00
|
|
|
GeoNetworkPublisher reader=GeoNetwork.get();
|
2016-09-30 17:38:44 +02:00
|
|
|
|
2016-09-08 15:29:57 +02:00
|
|
|
Configuration config = reader.getConfiguration();
|
2016-09-08 15:30:53 +02:00
|
|
|
Account account=config.getScopeConfiguration().getAccounts().get(accountType);
|
2016-09-08 15:29:57 +02:00
|
|
|
|
2016-12-02 16:59:04 +01:00
|
|
|
//System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword());
|
|
|
|
System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword());
|
|
|
|
|
|
|
|
try{
|
|
|
|
String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword());
|
|
|
|
System.out.println("Decrypted Password: "+decryptedPassword);
|
|
|
|
}catch(Exception e){
|
|
|
|
System.out.println("ignoring exception during pwd decrypting");
|
|
|
|
}
|
2016-09-08 15:29:57 +02:00
|
|
|
|
|
|
|
|
2016-09-07 15:07:46 +02:00
|
|
|
// req.addParam("keyword", "Thredds");
|
2016-09-08 15:29:57 +02:00
|
|
|
final GNSearchRequest req=new GNSearchRequest();
|
2016-12-02 16:59:04 +01:00
|
|
|
// req.addParam(GNSearchRequest.Param.any,"Thredds");
|
2016-09-08 15:29:57 +02:00
|
|
|
GNSearchResponse resp = reader.query(req);
|
2016-09-30 17:38:44 +02:00
|
|
|
int publicCount=resp.getCount();
|
|
|
|
reader.login(loginLevel);
|
|
|
|
int totalCount=reader.query(req).getCount();
|
2016-09-07 15:07:46 +02:00
|
|
|
System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
|
2016-09-08 15:29:57 +02:00
|
|
|
if(totalCount==0)
|
|
|
|
return;
|
2016-09-30 17:38:44 +02:00
|
|
|
/*try{
|
|
|
|
int last = totalCount>MAX?totalCount:MAX;
|
|
|
|
for(int i=0; i<last; i++){
|
|
|
|
String xml = reader.getByIdAsRawString(resp.getMetadata(i).getUUID());
|
|
|
|
System.out.println(i+") is Thredds? "+containsString(xml, "Thredds"));
|
|
|
|
}
|
|
|
|
}catch(Exception e ){
|
|
|
|
e.printStackTrace();
|
|
|
|
}*/
|
2016-09-08 15:29:57 +02:00
|
|
|
|
2016-09-07 15:07:46 +02:00
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-24 17:38:36 +01:00
|
|
|
//@Test
|
2017-03-09 11:22:02 +01:00
|
|
|
public void getLayersBySearch() throws Exception{
|
|
|
|
try{
|
|
|
|
for(String scope:scopesProd){
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
GeoNetworkPublisher reader=GeoNetwork.get();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Configuration config = reader.getConfiguration();
|
|
|
|
Account account=config.getScopeConfiguration().getAccounts().get(accountType);
|
|
|
|
|
|
|
|
//System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword());
|
|
|
|
System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword());
|
|
|
|
|
|
|
|
try{
|
|
|
|
String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword());
|
|
|
|
System.out.println("Decrypted Password: "+decryptedPassword);
|
|
|
|
}catch(Exception e){
|
|
|
|
System.out.println("ignoring exception during pwd decrypting");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// req.addParam("keyword", "Thredds");
|
|
|
|
//final GNSearchRequest req=new GNSearchRequest();
|
|
|
|
final GNSearchRequest req = getRequest(true, textToSearch);
|
|
|
|
// req.addParam(GNSearchRequest.Param.any,"Thredds");
|
|
|
|
GNSearchResponse resp = reader.query(req);
|
|
|
|
int publicCount=resp.getCount();
|
|
|
|
reader.login(loginLevel);
|
|
|
|
int totalCount=reader.query(req).getCount();
|
|
|
|
System.out.println("QUERY "+textToSearch);
|
|
|
|
System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
|
|
|
|
|
|
|
|
// if(totalCount==0)
|
|
|
|
// return;
|
|
|
|
// try{
|
|
|
|
// int last = totalCount>MAX?totalCount:MAX;
|
|
|
|
//
|
|
|
|
// for(int i=0; i<last; i++){
|
|
|
|
// //String xml = reader.getByIdAsRawString(resp.getMetadata(i).getUUID());
|
|
|
|
// GNMetadata xml = resp.getMetadata(i);
|
|
|
|
// //System.out.println(i+") is Thredds? "+containsString(xml, "Thredds"));
|
|
|
|
// System.out.println(xml.toString());
|
|
|
|
// }
|
|
|
|
// }catch(Exception e ){
|
|
|
|
// e.printStackTrace();
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-08 15:29:57 +02:00
|
|
|
private boolean containsString(String txt, String value){
|
|
|
|
return txt.contains(value);
|
|
|
|
}
|
|
|
|
|
2017-03-09 11:22:02 +01:00
|
|
|
/**
|
|
|
|
* Gets the request.
|
|
|
|
*
|
|
|
|
* @param sortByTitle the sort by title
|
|
|
|
* @param textToSearch the text to search
|
|
|
|
* @return the request
|
|
|
|
*/
|
|
|
|
public GNSearchRequest getRequest(boolean sortByTitle, String textToSearch) {
|
|
|
|
|
|
|
|
GNSearchRequest req = new GNSearchRequest();
|
|
|
|
|
|
|
|
if(sortByTitle)
|
|
|
|
req.addConfig(GNSearchRequest.Config.sortBy, "title");
|
|
|
|
|
|
|
|
if(textToSearch==null || textToSearch.isEmpty()){
|
|
|
|
req.addParam(GNSearchRequest.Param.any, textToSearch);
|
|
|
|
System.out.println("search by any text");
|
|
|
|
}else{
|
|
|
|
req.addParam(GNSearchRequest.Param.title, textToSearch);
|
2017-03-24 17:38:36 +01:00
|
|
|
req.addConfig(GNSearchRequest.Config.similarity, "1");
|
2017-03-09 11:22:02 +01:00
|
|
|
System.out.println("search by title");
|
|
|
|
}
|
|
|
|
System.out.println("text to search "+textToSearch);
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:29:57 +02:00
|
|
|
|
2016-12-02 16:59:04 +01:00
|
|
|
// @Test
|
2016-09-07 15:07:46 +02:00
|
|
|
public void getCountProd() throws Exception{
|
|
|
|
try{
|
|
|
|
for(String scope:scopesProd){
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
GeoNetworkReader reader=GeoNetwork.get();
|
|
|
|
final GNSearchRequest req=new GNSearchRequest();
|
|
|
|
// req.addParam("keyword", "Thredds");
|
2016-09-30 17:38:44 +02:00
|
|
|
// req.addParam(GNSearchRequest.Param.any,"Oscar");
|
2016-09-07 15:07:46 +02:00
|
|
|
int publicCount=reader.query(req).getCount();
|
2016-09-08 15:29:57 +02:00
|
|
|
reader.login(loginLevel);
|
2016-09-07 15:07:46 +02:00
|
|
|
int totalCount=reader.query(req).getCount();
|
|
|
|
System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")");
|
|
|
|
}
|
|
|
|
}catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|