160 lines
5.0 KiB
Java
160 lines
5.0 KiB
Java
package org.gcube.datacatalogue.ckanutillibrary;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.CKanUserWrapper;
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.CkanDatasetRelationship;
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.DatasetRelationships;
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.RolesIntoOrganization;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import eu.trentorise.opendata.jackan.model.CkanOrganization;
|
|
|
|
public class TestCKanLib {
|
|
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestCKanLib.class);
|
|
|
|
private CkanUtilsFactory factory;
|
|
private String scope = "/gcube/devsec/devVRE";
|
|
private String testUser = "costantino_perciante";
|
|
|
|
//@Before
|
|
public void before(){
|
|
factory = CkanUtilsFactory.getFactory();
|
|
}
|
|
|
|
//@Test
|
|
public void datasetsRelationshipCreateDelete() throws Exception{
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String subjectId = "lucio_organization";
|
|
String objectId = "test_for_visibility";
|
|
DatasetRelationships relation = DatasetRelationships.depends_on;
|
|
|
|
boolean resC = instance.createDatasetRelationship(subjectId, objectId, relation, "Comment for this relationship", instance.getApiKeyFromUsername(testUser));
|
|
|
|
Thread.sleep(500);
|
|
|
|
boolean resD = instance.deleteDatasetRelationship(subjectId, objectId, relation, instance.getApiKeyFromUsername(testUser));
|
|
|
|
logger.debug("ResC is " + resC);
|
|
logger.debug("ResD is " + resD);
|
|
}
|
|
|
|
//@Test
|
|
public void datasetRelationshipRetrieve() throws Exception{
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String subjectId = "lucio_organization";
|
|
String objectId = "test_for_visibility";
|
|
|
|
List<CkanDatasetRelationship> res = instance.getRelationshipDatasets(subjectId, objectId, instance.getApiKeyFromUsername(testUser));
|
|
|
|
logger.debug("Relationships " + res);
|
|
|
|
}
|
|
|
|
//@Test
|
|
public void factoryTest() throws Exception{
|
|
|
|
CkanUtilsFactory factory = CkanUtilsFactory.getFactory();
|
|
|
|
while(true){
|
|
factory.getUtilsPerScope("/gcube");
|
|
Thread.sleep(60* 1000 * 3);
|
|
factory.getUtilsPerScope("/gcube");
|
|
break;
|
|
}
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
Thread.sleep(1000);
|
|
factory.getUtilsPerScope("/gcube");
|
|
}
|
|
|
|
}
|
|
|
|
//@Test
|
|
public void testgetApiKeyFromUser() throws Exception {
|
|
|
|
logger.debug("Testing getApiKeyFromUser");
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String username = "francescomangiacrapa";
|
|
String key = instance.getApiKeyFromUsername(username);
|
|
|
|
System.out.println("key for " + username + " is " + key);
|
|
}
|
|
|
|
//@Test
|
|
public void testgetUserFromApiKey() throws Exception {
|
|
|
|
logger.debug("Testing getApiKeyFromUser");
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String key = "put-your-key-here";
|
|
CKanUserWrapper user = instance.getUserFromApiKey(key);
|
|
|
|
System.out.println("user for " + key + " is " + user);
|
|
}
|
|
|
|
//@Test
|
|
public void getOrganizationsByUser() throws Exception {
|
|
|
|
System.out.println("Testing getOrganizationsByUser");
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String username = "francescomangiacrapa";
|
|
List<CkanOrganization> organizations = instance.getOrganizationsByUser(username);
|
|
|
|
System.out.println("organizations for user " + username + " are: ");
|
|
|
|
for (CkanOrganization ckanOrganization : organizations) {
|
|
System.out.println("-" + ckanOrganization.getName());
|
|
}
|
|
}
|
|
|
|
//@Test
|
|
public void getGroupsAndRolesByUser() throws Exception {
|
|
|
|
logger.debug("Testing getGroupsAndRolesByUser");
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
String username = "andrea.rossi";
|
|
instance = new CKanUtilsImpl("/gcube");
|
|
List<RolesIntoOrganization> rolesToMatch = new ArrayList<RolesIntoOrganization>();
|
|
rolesToMatch.add(RolesIntoOrganization.ADMIN);
|
|
rolesToMatch.add(RolesIntoOrganization.MEMBER);
|
|
rolesToMatch.add(RolesIntoOrganization.EDITOR);
|
|
Map<String, List<RolesIntoOrganization>> map = instance.getGroupsAndRolesByUser(username, rolesToMatch);
|
|
|
|
System.out.println("organizations for user " + username + " are " + map);
|
|
}
|
|
|
|
//@Test
|
|
public void getUsers() throws Exception{
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
List<RolesIntoOrganization> rolesToMatch = new ArrayList<RolesIntoOrganization>();
|
|
rolesToMatch.add(RolesIntoOrganization.ADMIN);
|
|
rolesToMatch.add(RolesIntoOrganization.EDITOR);
|
|
|
|
Map<String, List<RolesIntoOrganization>> orgs = instance.getGroupsAndRolesByUser("costantino_perciante", rolesToMatch);
|
|
|
|
Iterator<Entry<String, List<RolesIntoOrganization>>> iterator = orgs.entrySet().iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
Map.Entry<java.lang.String, java.util.List<org.gcube.datacatalogue.ckanutillibrary.models.RolesIntoOrganization>> entry = (Map.Entry<java.lang.String, java.util.List<org.gcube.datacatalogue.ckanutillibrary.models.RolesIntoOrganization>>) iterator
|
|
.next();
|
|
|
|
logger.debug("Org is " + entry.getKey() + " and role is " + entry.getValue().get(0));
|
|
|
|
}
|
|
}
|
|
}
|