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 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 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 rolesToMatch = new ArrayList(); rolesToMatch.add(RolesIntoOrganization.ADMIN); rolesToMatch.add(RolesIntoOrganization.MEMBER); rolesToMatch.add(RolesIntoOrganization.EDITOR); Map> 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 rolesToMatch = new ArrayList(); rolesToMatch.add(RolesIntoOrganization.ADMIN); rolesToMatch.add(RolesIntoOrganization.EDITOR); Map> orgs = instance.getGroupsAndRolesByUser("costantino_perciante", rolesToMatch); Iterator>> iterator = orgs.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry> entry = (Map.Entry>) iterator .next(); logger.debug("Org is " + entry.getKey() + " and role is " + entry.getValue().get(0)); } } }