2021-02-12 16:08:18 +01:00
|
|
|
package org.gcube.datacatalogue.utillibrary.test;
|
2020-06-01 17:12:28 +02:00
|
|
|
|
|
|
|
|
2020-06-03 17:46:48 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2020-06-01 17:12:28 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2021-06-04 10:14:37 +02:00
|
|
|
import org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject;
|
|
|
|
import org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject.METHOD;
|
2021-02-12 16:08:18 +01:00
|
|
|
import org.gcube.datacatalogue.utillibrary.server.ApplicationProfileScopePerUrlReader;
|
|
|
|
import org.gcube.datacatalogue.utillibrary.server.DataCatalogueFactory;
|
|
|
|
import org.gcube.datacatalogue.utillibrary.server.DataCatalogueImpl;
|
|
|
|
import org.gcube.datacatalogue.utillibrary.shared.ResourceBean;
|
|
|
|
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset;
|
|
|
|
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanGroup;
|
|
|
|
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanOrganization;
|
|
|
|
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanUser;
|
2021-06-04 16:04:11 +02:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2020-06-01 17:12:28 +02:00
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-02-09 16:32:25 +01:00
|
|
|
|
2020-06-03 16:29:47 +02:00
|
|
|
|
2020-06-01 17:12:28 +02:00
|
|
|
/**
|
|
|
|
* The Class TestDataCatalogueLib.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy)
|
|
|
|
* Jun 1, 2020
|
|
|
|
*/
|
|
|
|
public class TestDataCatalogueLib {
|
|
|
|
|
2021-02-09 12:55:58 +01:00
|
|
|
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TestDataCatalogueLib.class);
|
2020-06-01 17:12:28 +02:00
|
|
|
|
|
|
|
private DataCatalogueFactory factory;
|
2021-06-04 10:14:37 +02:00
|
|
|
private String scope = "/gcube/devsec/devVRE";
|
|
|
|
//private String scope = "/gcube";
|
2020-06-05 12:42:01 +02:00
|
|
|
//private String scope = "/pred4s/preprod/preVRE";
|
2020-06-03 16:29:47 +02:00
|
|
|
//private String scope = "/d4science.research-infrastructures.eu/SoBigData/Catalogue-TerritoriAperti";
|
2020-06-03 14:39:41 +02:00
|
|
|
//private String scope = "/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab";
|
2020-06-01 17:12:28 +02:00
|
|
|
//private String testUser = "costantino_perciante";
|
2021-02-08 17:44:09 +01:00
|
|
|
private String testUser = "francesco.mangiacrapa";
|
2021-03-02 16:01:12 +01:00
|
|
|
private String authorizationToken = "";
|
2020-06-01 17:12:28 +02:00
|
|
|
String subjectId = "aa_father4";
|
|
|
|
String objectId = "bb_son4";
|
2021-02-12 14:58:23 +01:00
|
|
|
String productName = "adatasetcreatedbycatalogue-util-library-873805063";
|
2020-06-01 17:12:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Before.
|
|
|
|
*
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2021-03-02 15:12:33 +01:00
|
|
|
//@Before
|
2020-06-01 17:12:28 +02:00
|
|
|
public void before() throws Exception{
|
|
|
|
factory = DataCatalogueFactory.getFactory();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory test.
|
|
|
|
*
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
//@Test
|
|
|
|
public void factoryTest() throws Exception{
|
|
|
|
|
|
|
|
DataCatalogueFactory factory = DataCatalogueFactory.getFactory();
|
|
|
|
|
|
|
|
while(true){
|
2020-06-03 14:39:41 +02:00
|
|
|
factory.getUtilsPerScope(scope);
|
2020-06-01 17:12:28 +02:00
|
|
|
Thread.sleep(60* 1000 * 3);
|
2020-06-03 14:39:41 +02:00
|
|
|
factory.getUtilsPerScope(scope);
|
2020-06-01 17:12:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
Thread.sleep(1000);
|
2020-06-03 14:39:41 +02:00
|
|
|
factory.getUtilsPerScope(scope);
|
2020-06-01 17:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the scope per url.
|
|
|
|
*
|
|
|
|
* @return the scope per url
|
|
|
|
*/
|
|
|
|
//@Test
|
|
|
|
public void getScopePerUrl(){
|
|
|
|
|
2020-06-03 14:39:41 +02:00
|
|
|
ScopeProvider.instance.set(scope);
|
2021-02-09 16:32:25 +01:00
|
|
|
String url = "https://dev2.d4science.org/group/devvre/ckan";
|
2020-06-01 17:12:28 +02:00
|
|
|
String scopeToUse = ApplicationProfileScopePerUrlReader.getScopePerUrl(url);
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.debug("Retrieved scope is " + scopeToUse);
|
2020-06-01 17:12:28 +02:00
|
|
|
|
|
|
|
ScopeProvider.instance.reset(); // the following sysout should print null
|
|
|
|
String url2 = "https://dev4.d4science.org/group/devvre/ckan";
|
|
|
|
String scopeToUse2 = ApplicationProfileScopePerUrlReader.getScopePerUrl(url2);
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.debug("Retrieved scope is " + scopeToUse2);
|
2020-06-01 17:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user role by group.
|
|
|
|
*
|
|
|
|
* @return the user role by group
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2020-08-28 15:29:42 +02:00
|
|
|
//@Test
|
2020-06-01 17:12:28 +02:00
|
|
|
public void getUserRoleByGroup() throws Exception{
|
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String username = testUser;
|
|
|
|
long init = System.currentTimeMillis();
|
|
|
|
instance.getUserRoleByGroup(username);
|
|
|
|
long end = System.currentTimeMillis();
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.debug("Time taken " + (end - init));
|
2020-06-01 17:12:28 +02:00
|
|
|
}
|
2021-02-17 12:24:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user role by group.
|
|
|
|
*
|
|
|
|
* @return the user role by group
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2021-02-17 15:19:31 +01:00
|
|
|
//@Test
|
2021-02-17 12:24:44 +01:00
|
|
|
public void getDataset() throws Exception{
|
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String username = testUser;
|
|
|
|
CkanDataset dataset = instance.getDataset("sarda-sarda", username);
|
|
|
|
LOG.debug("Got dataset: " +dataset.getName() + ", with id: "+dataset.getId());
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:12:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user role by organization.
|
|
|
|
*
|
|
|
|
* @return the user role by organization
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
//@Test
|
|
|
|
public void getUserRoleByOrganization() throws Exception{
|
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String username = testUser;
|
|
|
|
long init = System.currentTimeMillis();
|
|
|
|
instance.getUserRoleByOrganization(username);
|
|
|
|
long end = System.currentTimeMillis();
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.debug("Time taken " + (end - init));
|
2020-06-01 17:12:28 +02:00
|
|
|
}
|
2020-06-03 16:29:47 +02:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void getRoleOfUserInOrganization() throws Exception{
|
|
|
|
|
2020-06-03 17:46:48 +02:00
|
|
|
String orgName = "devvre";
|
2020-06-03 16:29:47 +02:00
|
|
|
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
|
2020-08-27 14:37:19 +02:00
|
|
|
CkanOrganization org = utils.getOrganizationByIdOrName(orgName);
|
2020-06-03 16:29:47 +02:00
|
|
|
String role = utils.getRoleOfUserInOrganization(testUser, org.getName());
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.debug("The user "+testUser+" in the org "+org.getName() + " has the role "+role);
|
2020-06-03 16:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void getOrganizationForName() throws Exception{
|
|
|
|
|
2020-06-03 17:46:48 +02:00
|
|
|
String orgName = "devvre";
|
2020-08-27 14:37:19 +02:00
|
|
|
|
2020-06-03 16:29:47 +02:00
|
|
|
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
|
|
|
|
|
2020-08-27 14:37:19 +02:00
|
|
|
CkanOrganization org = utils.getOrganizationByIdOrName(orgName);
|
2020-06-03 16:29:47 +02:00
|
|
|
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.debug("The "+CkanOrganization.class.getSimpleName()+" is: "+org.getName());
|
|
|
|
LOG.debug("LandingPages of "+CkanOrganization.class.getSimpleName()+" for name " + utils.getLandingPages());
|
2020-06-03 16:29:47 +02:00
|
|
|
if(org.getUsers()!=null) {
|
|
|
|
|
|
|
|
for (CkanUser user : org.getUsers()) {
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.debug("User: "+user.getName());
|
2020-06-03 16:29:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 12:55:58 +01:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void getOrganizationsNamesByUser() throws Exception{
|
|
|
|
|
|
|
|
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
|
|
|
|
|
|
|
|
List<CkanOrganization> listOrgs = utils.getOrganizationsByUser(testUser);
|
|
|
|
|
|
|
|
LOG.debug("User :"+testUser+" found in the Organization/s:");
|
|
|
|
|
|
|
|
for (CkanOrganization ckanOrganization : listOrgs) {
|
|
|
|
LOG.debug("Org: "+ckanOrganization.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void getGroupsNamesByUser() throws Exception{
|
|
|
|
|
|
|
|
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
|
|
|
|
|
|
|
|
List<CkanGroup> listGroups = utils.getGroupsByUser(testUser);
|
|
|
|
|
|
|
|
LOG.debug("User :"+testUser+" found in the Group/s:");
|
|
|
|
|
|
|
|
for (CkanGroup ckanGroup : listGroups) {
|
|
|
|
LOG.debug("Group: "+ckanGroup.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-06-03 17:46:48 +02:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void getLandingPages() throws Exception{
|
|
|
|
|
|
|
|
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.debug("Landing pages " + utils.getLandingPages());
|
2020-06-03 17:46:48 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-27 14:37:19 +02:00
|
|
|
//@Test
|
|
|
|
public void countDatasetForOganization() throws Exception{
|
|
|
|
|
|
|
|
DataCatalogueImpl utils = new DataCatalogueImpl(scope,true);
|
|
|
|
|
|
|
|
List<String> listOrgIds = utils.getOrganizationsIds();
|
|
|
|
|
|
|
|
System.out.println("'ORGANIZATION NAME': 'NUMBER OF PACKAGE'");
|
|
|
|
|
|
|
|
for (String orgID : listOrgIds) {
|
|
|
|
CkanOrganization org = utils.getOrganizationByIdOrName(orgID);
|
|
|
|
int packageCount = org.getPackageCount();
|
|
|
|
//logger.info("ORG: "+org.getName() + " has "+packageCount+ " package/s");
|
|
|
|
System.out.println(org.getName()+": "+packageCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-09 12:55:58 +01:00
|
|
|
//@Test
|
2020-06-03 17:46:48 +02:00
|
|
|
public void createDataset() throws Exception{
|
|
|
|
|
|
|
|
try {
|
2020-08-27 14:37:19 +02:00
|
|
|
|
2020-06-03 17:46:48 +02:00
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
SecurityTokenProvider.instance.set(authorizationToken);
|
|
|
|
|
2021-02-09 17:55:44 +01:00
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
2020-06-03 17:46:48 +02:00
|
|
|
String licenseId = instance.getLicenses().get(0).getId();
|
|
|
|
|
|
|
|
Map<String, List<String>> customFieldsMultiple = new HashMap<String, List<String>>();
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
List<String> values = new ArrayList<String>();
|
|
|
|
for (int j = 0; j < new Random().nextInt(3); j++) {
|
2020-06-05 12:42:01 +02:00
|
|
|
values.add("value-random-"+ new Random().nextInt(10));
|
2020-06-03 17:46:48 +02:00
|
|
|
}
|
2020-06-05 12:42:01 +02:00
|
|
|
customFieldsMultiple.put("key-random-"+new Random().nextInt(10), values);
|
2020-06-03 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2021-02-11 11:03:22 +01:00
|
|
|
customFieldsMultiple.put("empty-key", Arrays.asList(""));
|
|
|
|
|
2020-09-02 17:40:12 +02:00
|
|
|
customFieldsMultiple.put("system:type", Arrays.asList("EmptyProfile"));
|
|
|
|
|
2020-06-03 17:46:48 +02:00
|
|
|
boolean setSearchable = true;
|
2021-06-04 10:14:37 +02:00
|
|
|
boolean setPublic = true;
|
2020-06-03 17:46:48 +02:00
|
|
|
List<ResourceBean> resources = null;
|
|
|
|
|
2020-06-05 12:42:01 +02:00
|
|
|
String orgName = scope.split("/")[3].toLowerCase();
|
|
|
|
//System.out.println("Org name by VRE: "+orgName);
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.debug("Org name by VRE: "+orgName);
|
2020-06-05 12:42:01 +02:00
|
|
|
|
2020-08-27 14:37:19 +02:00
|
|
|
int random = new Random().nextInt();
|
|
|
|
String datasetTitle = "a dataset created by catalogue-util-library "+random;
|
|
|
|
String datasetName = datasetTitle.replace(" ", "");
|
2020-08-28 15:29:42 +02:00
|
|
|
String createdDataset = instance.createCkanDatasetMultipleCustomFields(
|
2020-06-03 17:46:48 +02:00
|
|
|
testUser,
|
2020-08-27 14:37:19 +02:00
|
|
|
"a dataset created by catalogue-util-library "+random,
|
|
|
|
datasetName,
|
2020-06-05 12:42:01 +02:00
|
|
|
orgName,
|
2020-06-03 17:46:48 +02:00
|
|
|
testUser,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
1,
|
|
|
|
"the description",
|
|
|
|
licenseId,
|
|
|
|
Arrays.asList("tag 1","tag 2","tag 3"),
|
|
|
|
customFieldsMultiple,
|
|
|
|
resources,
|
|
|
|
setPublic,
|
2020-08-28 15:29:42 +02:00
|
|
|
setSearchable,
|
|
|
|
true);
|
2020-06-03 17:46:48 +02:00
|
|
|
|
2021-02-11 11:03:22 +01:00
|
|
|
LOG.info("Created the dataset with id: " +createdDataset);
|
2020-06-03 17:46:48 +02:00
|
|
|
}catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-08-28 15:29:42 +02:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void createGroup() throws Exception{
|
2021-02-09 17:55:44 +01:00
|
|
|
|
2020-08-28 15:29:42 +02:00
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
SecurityTokenProvider.instance.set(authorizationToken);
|
2021-02-09 17:55:44 +01:00
|
|
|
|
2020-08-28 15:29:42 +02:00
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
int random = new Random().nextInt();
|
|
|
|
String groupTitle = "a grop created by catalogue-util-library "+random;
|
|
|
|
String groupName = groupTitle.replace(" ", "");
|
|
|
|
CkanGroup ckanGroup = instance.createGroup(groupName, groupTitle, "description");
|
2021-02-09 12:55:58 +01:00
|
|
|
LOG.info("Created the group: "+ckanGroup);
|
2020-08-28 15:29:42 +02:00
|
|
|
}
|
2021-02-09 17:55:44 +01:00
|
|
|
|
2021-03-02 15:12:33 +01:00
|
|
|
//@Test
|
2021-02-12 14:58:23 +01:00
|
|
|
public void addResource() throws Exception {
|
2021-02-09 17:55:44 +01:00
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
SecurityTokenProvider.instance.set(authorizationToken);
|
2021-02-12 14:58:23 +01:00
|
|
|
|
2021-02-09 17:55:44 +01:00
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
2021-02-17 12:09:02 +01:00
|
|
|
CkanDataset dataset = instance.getDataset(productName, testUser);
|
2021-02-09 17:55:44 +01:00
|
|
|
|
2021-02-12 14:58:23 +01:00
|
|
|
for (int i = 1; i < 3; i++) {
|
|
|
|
ResourceBean resourceBean = new ResourceBean("https://data-dev.d4science.net/ie8Y", "resouce " + i,
|
|
|
|
"description " + i, null, testUser, dataset.getId(), null);
|
2021-03-02 14:32:21 +01:00
|
|
|
instance.addResourceToDataset(resourceBean,testUser,dataset.getOrganization().getName());
|
2021-02-09 17:55:44 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-01 17:12:28 +02:00
|
|
|
|
2021-03-02 16:01:12 +01:00
|
|
|
//@Test
|
|
|
|
public void deleteResource() throws Exception {
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
SecurityTokenProvider.instance.set(authorizationToken);
|
|
|
|
|
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String resourceId = "ce7295cf-47db-4faf-901f-4fec2d3fae7a";
|
|
|
|
boolean deleted = instance.deleteResourceFromDataset(resourceId, testUser);
|
|
|
|
|
|
|
|
LOG.info("Deleted the resource? "+deleted);
|
|
|
|
}
|
|
|
|
|
2021-02-12 14:58:23 +01:00
|
|
|
//@Test
|
|
|
|
public void patchFieldsForDataset() throws Exception{
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
SecurityTokenProvider.instance.set(authorizationToken);
|
|
|
|
|
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
Map<String, String> customFieldsToChange = new HashMap<String, String>();
|
|
|
|
customFieldsToChange.put("key-random-0", "patched");
|
|
|
|
instance.patchFieldsForDataset(productName, customFieldsToChange);
|
|
|
|
}
|
2021-02-12 18:38:31 +01:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void getUrlProduct() throws Exception{
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
SecurityTokenProvider.instance.set(authorizationToken);
|
|
|
|
|
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String datasetName = productName;
|
|
|
|
String url = instance.getUnencryptedUrlFromDatasetIdOrName(datasetName);
|
|
|
|
LOG.debug("url is " + url);
|
|
|
|
}
|
2021-06-04 10:14:37 +02:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void marshallingDataset() throws Exception{
|
|
|
|
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String username = testUser;
|
|
|
|
CkanDataset dataset = instance.getDataset("sarda-sarda", username);
|
|
|
|
LOG.debug("Got dataset: " +dataset.getName() + ", with id: "+dataset.getId() +" is private: "+dataset.isPriv());
|
|
|
|
String jsonValueDataset = MarshUnmarshCkanObject.toJsonValueDataset(dataset,METHOD.TO_CREATE);
|
|
|
|
System.out.println(jsonValueDataset);
|
|
|
|
dataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset, METHOD.TO_READ);
|
|
|
|
System.out.println(dataset);
|
|
|
|
|
|
|
|
}
|
2020-06-01 17:12:28 +02:00
|
|
|
}
|