114 lines
3.1 KiB
Java
114 lines
3.1 KiB
Java
package org.gcube.datacatalogue.ckanutillibrary;
|
|
|
|
import java.net.URL;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.CKanUserWrapper;
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.MetaDataBean;
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.ROLES_IN_ORGANIZATION;
|
|
import org.junit.Test;
|
|
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);
|
|
|
|
CKanUtilsImpl instance;
|
|
|
|
//@Before
|
|
public void before() throws Exception{
|
|
|
|
instance = new CKanUtilsImpl("/gcube");
|
|
|
|
}
|
|
|
|
//@Test
|
|
public void testgetApiKeyFromUser() {
|
|
|
|
logger.debug("Testing getApiKeyFromUser");
|
|
|
|
String username = "francescomangiacrapa";
|
|
String key = instance.getApiKeyFromUser(username);
|
|
|
|
System.out.println("key for " + username + " is " + key);
|
|
}
|
|
|
|
//@Test
|
|
public void testgetUserFromApiKey() {
|
|
|
|
logger.debug("Testing getApiKeyFromUser");
|
|
|
|
String key = "put-your-key-here";
|
|
CKanUserWrapper user = instance.getUserFromApiKey(key);
|
|
|
|
System.out.println("user for " + key + " is " + user);
|
|
}
|
|
|
|
//@Test
|
|
public void getOrganizationsByUser() {
|
|
|
|
System.out.println("Testing getOrganizationsByUser");
|
|
|
|
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() {
|
|
|
|
logger.debug("Testing getGroupsAndRolesByUser");
|
|
|
|
String username = "francescomangiacrapa";
|
|
List<ROLES_IN_ORGANIZATION> rolesToMatch = new ArrayList<ROLES_IN_ORGANIZATION>();
|
|
rolesToMatch.add(ROLES_IN_ORGANIZATION.admin);
|
|
rolesToMatch.add(ROLES_IN_ORGANIZATION.member);
|
|
rolesToMatch.add(ROLES_IN_ORGANIZATION.editor);
|
|
Map<String, List<ROLES_IN_ORGANIZATION>> map = instance.getGroupsAndRolesByUser(username, rolesToMatch);
|
|
|
|
System.out.println("organizations for user " + username + " are " + map);
|
|
}
|
|
|
|
//@Test
|
|
public void testFactory() throws Exception{
|
|
|
|
System.out.println("Creating factory object");
|
|
CKanUtilsImpl obj = CKanUtilsFactory.getInstance().getCkanUtilsForScope("/gcube");
|
|
System.out.println("Object created " + obj.getCatalogueUrl());
|
|
|
|
|
|
}
|
|
|
|
//@Test
|
|
public void getMetadataTest() throws Exception{
|
|
|
|
|
|
List<MetaDataBean> beans = CKanUtilsFactory.getInstance().getCkanUtilsForScope("/gcube").getMetadataList();
|
|
|
|
System.out.println("List is " + beans);
|
|
|
|
}
|
|
|
|
//@Test
|
|
public void createOrganization() throws Exception{
|
|
String callUrl = "https://ckan-d-d4s.d4science.org/ckan-connector/organization/CreationTest?gcube-token=34c34146-ab38-42d5-9332-f325e8b2b930";
|
|
URL url = new URL(callUrl);
|
|
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
|
|
connection.setRequestMethod("PUT");
|
|
|
|
System.out.println(connection.getResponseCode() + " " + connection.getResponseMessage());
|
|
}
|
|
|
|
}
|