2016-06-07 16:00:05 +02:00
|
|
|
package org.gcube.datacatalogue.ckanutillibrary;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2016-09-14 18:00:10 +02:00
|
|
|
import java.util.Arrays;
|
2016-06-27 10:04:07 +02:00
|
|
|
import java.util.Iterator;
|
2016-06-07 16:00:05 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2016-06-27 10:04:07 +02:00
|
|
|
import java.util.Map.Entry;
|
2016-06-07 16:00:05 +02:00
|
|
|
|
2016-09-05 18:03:27 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2016-06-07 16:00:05 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.CKanUserWrapper;
|
2016-08-04 18:24:39 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.CkanDatasetRelationship;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.DatasetRelationships;
|
2016-06-14 17:05:24 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.RolesIntoOrganization;
|
2016-06-07 16:00:05 +02:00
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-09-14 16:23:30 +02:00
|
|
|
import eu.trentorise.opendata.jackan.CheckedCkanClient;
|
2016-06-07 16:00:05 +02:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanOrganization;
|
2016-09-14 16:23:30 +02:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanUser;
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
public class TestCKanLib {
|
|
|
|
|
2016-06-08 18:31:25 +02:00
|
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestCKanLib.class);
|
2016-06-07 16:00:05 +02:00
|
|
|
|
2016-08-04 18:24:39 +02:00
|
|
|
private CkanUtilsFactory factory;
|
2016-09-14 18:00:10 +02:00
|
|
|
private String scope = "/gcube/devNext/NextNext";
|
2016-08-04 18:24:39 +02:00
|
|
|
private String testUser = "costantino_perciante";
|
2016-09-02 11:18:11 +02:00
|
|
|
String subjectId = "aa_father4";
|
|
|
|
String objectId = "bb_son4";
|
2016-08-04 18:24:39 +02:00
|
|
|
|
|
|
|
//@Before
|
|
|
|
public void before(){
|
|
|
|
factory = CkanUtilsFactory.getFactory();
|
|
|
|
}
|
2016-09-14 18:00:10 +02:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void getRole() throws Exception{
|
|
|
|
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
instance.getGroupsAndRolesByUser(testUser, Arrays.asList(RolesIntoOrganization.ADMIN, RolesIntoOrganization.EDITOR, RolesIntoOrganization.MEMBER
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
2016-08-04 18:24:39 +02:00
|
|
|
|
|
|
|
//@Test
|
2016-09-02 11:18:11 +02:00
|
|
|
public void datasetsRelationshipCreate() throws Exception{
|
2016-08-04 18:24:39 +02:00
|
|
|
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
|
2016-09-02 11:18:11 +02:00
|
|
|
DatasetRelationships relation = DatasetRelationships.parent_of;
|
2016-08-04 18:24:39 +02:00
|
|
|
|
|
|
|
boolean resC = instance.createDatasetRelationship(subjectId, objectId, relation, "Comment for this relationship", instance.getApiKeyFromUsername(testUser));
|
|
|
|
|
2016-09-02 11:18:11 +02:00
|
|
|
logger.debug("Res is " + resC);
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void datasetsRelationshipDelete() throws Exception{
|
|
|
|
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
DatasetRelationships relation = DatasetRelationships.child_of;
|
2016-08-04 18:24:39 +02:00
|
|
|
|
|
|
|
boolean resD = instance.deleteDatasetRelationship(subjectId, objectId, relation, instance.getApiKeyFromUsername(testUser));
|
|
|
|
|
|
|
|
logger.debug("ResD is " + resD);
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void datasetRelationshipRetrieve() throws Exception{
|
|
|
|
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
|
|
|
|
|
|
|
List<CkanDatasetRelationship> res = instance.getRelationshipDatasets(subjectId, objectId, instance.getApiKeyFromUsername(testUser));
|
|
|
|
|
|
|
|
logger.debug("Relationships " + res);
|
|
|
|
|
|
|
|
}
|
2016-06-07 16:00:05 +02:00
|
|
|
|
2016-08-01 18:02:51 +02:00
|
|
|
//@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");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-16 18:09:26 +02:00
|
|
|
//@Test
|
2016-08-04 18:24:39 +02:00
|
|
|
public void testgetApiKeyFromUser() throws Exception {
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
logger.debug("Testing getApiKeyFromUser");
|
2016-08-04 18:24:39 +02:00
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
String username = "francescomangiacrapa";
|
2016-06-16 18:09:26 +02:00
|
|
|
String key = instance.getApiKeyFromUsername(username);
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
System.out.println("key for " + username + " is " + key);
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
2016-08-04 18:24:39 +02:00
|
|
|
public void testgetUserFromApiKey() throws Exception {
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
logger.debug("Testing getApiKeyFromUser");
|
2016-08-04 18:24:39 +02:00
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
String key = "put-your-key-here";
|
|
|
|
CKanUserWrapper user = instance.getUserFromApiKey(key);
|
|
|
|
|
|
|
|
System.out.println("user for " + key + " is " + user);
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
2016-08-04 18:24:39 +02:00
|
|
|
public void getOrganizationsByUser() throws Exception {
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
System.out.println("Testing getOrganizationsByUser");
|
2016-08-04 18:24:39 +02:00
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 12:16:09 +02:00
|
|
|
//@Test
|
2016-06-20 11:29:58 +02:00
|
|
|
public void getGroupsAndRolesByUser() throws Exception {
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
logger.debug("Testing getGroupsAndRolesByUser");
|
2016-08-04 18:24:39 +02:00
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
2016-06-07 16:00:05 +02:00
|
|
|
|
2016-06-20 11:29:58 +02:00
|
|
|
String username = "andrea.rossi";
|
|
|
|
instance = new CKanUtilsImpl("/gcube");
|
2016-06-14 17:05:24 +02:00
|
|
|
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);
|
2016-06-07 16:00:05 +02:00
|
|
|
|
|
|
|
System.out.println("organizations for user " + username + " are " + map);
|
|
|
|
}
|
2016-06-22 17:25:17 +02:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void getUsers() throws Exception{
|
2016-08-04 18:24:39 +02:00
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope(scope);
|
2016-06-22 17:25:17 +02:00
|
|
|
|
2016-06-27 10:04:07 +02:00
|
|
|
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();
|
2016-06-22 17:25:17 +02:00
|
|
|
|
2016-06-27 10:04:07 +02:00
|
|
|
logger.debug("Org is " + entry.getKey() + " and role is " + entry.getValue().get(0));
|
2016-06-22 17:25:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2016-09-05 18:03:27 +02:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void getScopePerUrl(){
|
|
|
|
|
|
|
|
ScopeProvider.instance.set("/gcube");
|
|
|
|
String url = "https://dev4.d4science.org/group/devvre/ckan";
|
|
|
|
String scopeToUse = new ApplicationProfileScopePerUrlReader().getScopePerUrl(url);
|
|
|
|
logger.debug("Retrieved scope is " + scopeToUse);
|
2016-09-14 16:23:30 +02:00
|
|
|
|
|
|
|
ScopeProvider.instance.reset(); // the following sysout should print null
|
2016-09-08 15:13:49 +02:00
|
|
|
String url2 = "https://dev4.d4science.org/group/devvre/ckan";
|
|
|
|
String scopeToUse2 = new ApplicationProfileScopePerUrlReader().getScopePerUrl(url2);
|
|
|
|
logger.debug("Retrieved scope is " + scopeToUse2);
|
2016-09-05 18:03:27 +02:00
|
|
|
}
|
2016-09-14 16:23:30 +02:00
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void createUsers() throws Exception{
|
|
|
|
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE");
|
|
|
|
CheckedCkanClient client = new CheckedCkanClient(instance.getCatalogueUrl(), instance.getApiKeyFromUsername("costantino_perciante"));
|
|
|
|
CkanUser editorUser = new CkanUser("user_editor_devvre", "user_editor_devvre@test.it", "");
|
|
|
|
client.createUser(editorUser);
|
|
|
|
CkanUser adminUser = new CkanUser("user_admin_devvre", "user_admin_devvre@test.it", "");
|
|
|
|
client.createUser(adminUser);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void createAsEditor() throws Exception{
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE");
|
|
|
|
boolean checkedEditor = instance.checkRole("user_editor_devvre", "devvre", RolesIntoOrganization.EDITOR);
|
|
|
|
if(checkedEditor){
|
|
|
|
logger.debug("Created editor in devvre? " + checkedEditor);
|
|
|
|
}
|
|
|
|
boolean checkedAdmin = instance.checkRole("user_admin_devvre", "devvre", RolesIntoOrganization.ADMIN);
|
|
|
|
if(checkedAdmin){
|
|
|
|
logger.debug("Created admin in devvre? " + checkedAdmin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void editorCreateDataset() throws Exception{
|
|
|
|
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE");
|
|
|
|
instance.createCKanDataset(instance.getApiKeyFromUsername("user_editor_devvre"), "dataset_as_editor_devvre_private", "devvre", null, null, null, null, 1, null, null, null, null, null, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void adminCreateDataset() throws Exception{
|
|
|
|
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE");
|
|
|
|
//instance.createCKanDataset(instance.getApiKeyFromUsername("user_admin_devvre"), "dataset_as_admin_devvre", "devvre", null, null, null, null, 1, null, null, null, null, null, false);
|
|
|
|
instance.createCKanDataset(instance.getApiKeyFromUsername("user_admin_devvre"), "dataset_as_admin_devvre_private", "devvre", null, null, null, null, 1, null, null, null, null, null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
public void adminChangeVisibility() throws Exception{
|
|
|
|
|
|
|
|
CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE");
|
|
|
|
//instance.createCKanDataset(instance.getApiKeyFromUsername("user_editor_devvre"), "dataset_as_editor_devvre", "devvre", null, null, null, null, 1, null, null, null, null, null, false);
|
|
|
|
//instance.setDatasetPrivate(true, "3571cca5-b0ae-4dc6-b791-434a8e062ce5", "dataset_as_admin_devvre_public", instance.getApiKeyFromUsername("user_admin_devvre"));
|
|
|
|
|
|
|
|
instance.setDatasetPrivate(true, "3571cca5-b0ae-4dc6-b791-434a8e062ce5", "33bbdcb1-929f-441f-8718-a9e5134f517d", instance.getApiKeyFromUsername("user_editor_devvre"));
|
|
|
|
|
|
|
|
// CheckedCkanClient client = new CheckedCkanClient(instance.getCatalogueUrl(), instance.getApiKeyFromUsername("user_admin_devvre"));
|
|
|
|
// CkanDataset dataset = client.getDataset("dataset_as_admin_devvre_private");
|
|
|
|
// logger.debug("Current value for private: " + dataset.isPriv());
|
|
|
|
// dataset.setPriv(!dataset.isPriv());
|
|
|
|
// CkanDataset datasetUpd = client.updateDataset(dataset);
|
|
|
|
// logger.debug("Private value is " + datasetUpd.isPriv());
|
|
|
|
//
|
|
|
|
|
|
|
|
}
|
2016-06-07 16:00:05 +02:00
|
|
|
}
|