diff --git a/distro/changelog.xml b/distro/changelog.xml index c07f533..cae9944 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,5 +1,5 @@ - Added support for datasets's relationships (create, delete, retrieve methods) @@ -8,6 +8,7 @@ instances of the utils class(cache) Added code to discover a new Application Profile (see ticket #4925) + Added methods to manage group creation, assing a user/product to a group diff --git a/pom.xml b/pom.xml index 719298e..7aa2afb 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.data-catalogue ckan-util-library - 1.1.0-SNAPSHOT + 2.0.0-SNAPSHOT jar CKan utility library diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtils.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogue.java similarity index 90% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtils.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogue.java index df76788..601a75f 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtils.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogue.java @@ -14,10 +14,10 @@ import eu.trentorise.opendata.jackan.model.CkanLicense; import eu.trentorise.opendata.jackan.model.CkanOrganization; /** - * This is the ckan-util-library interface that shows the utility methods. + * This is the data-catalogue-library interface that shows the utility methods. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public interface CKanUtils { +public interface DataCatalogue { /** * Retrieve the API_KEY given the username (only if it is active). @@ -93,16 +93,6 @@ public interface CKanUtils { */ String findLicenseIdByLicenseTitle(String chosenLicense); - // /** - // * Set dataset private - // * @param priv - // * @param organizationId (NOTE: The ID, not the name!) - // * @param datasetId (NOTE: The ID, not the name!) - // * @param apiKey the user's api key - // * @return true on success, false otherwise - // */ - // boolean setDatasetPrivate(boolean priv, String organizationId, String datasetId, String apiKey); - /** * Add a resource described by the bean to the dataset id into resource.datasetId * @param resource @@ -277,4 +267,24 @@ public interface CKanUtils { */ String getRoleOfUserInOrganization(String username, String orgName, String apiKeyFromUsername); + /** + * Given the username and the group name the method retrieves the role of the user (i.e. his/her 'capacity') + * @param username + * @param groupName + * @param apiKeyFromUsername + * @return the capacity of the user into this group or null + */ + String getRoleOfUserInGroup(String username, String groupName, String apiKey); + + /** + * Assign a dataset to a group. + * @param groupNameOrId the id or the name of the destination group. + * @param datasetNameOrId the id or the name of the dataset + * @param apiKey (the apiKey should belong to someone that has the role of editor/admin of the organization in which + * the dataset is placed, plus the admin role into the destination group. + * @return true on success, false otherwise + */ + boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, + String apiKey); + } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CkanUtilsFactory.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueFactory.java similarity index 77% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/CkanUtilsFactory.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueFactory.java index 5cbceab..788a4b0 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CkanUtilsFactory.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueFactory.java @@ -9,11 +9,11 @@ import org.slf4j.LoggerFactory; * Please invoke this method to retrieve an object of this kind per scope. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class CkanUtilsFactory { +public class DataCatalogueFactory { - private static final Logger logger = LoggerFactory.getLogger(CkanUtilsFactory.class); + private static final Logger logger = LoggerFactory.getLogger(DataCatalogueFactory.class); private static final long MAX_LIFETIME = 1000 * 60 * 2; // 2 MINUTES - private static CkanUtilsFactory instance = new CkanUtilsFactory(); + private static DataCatalogueFactory instance = new DataCatalogueFactory(); private static ConcurrentHashMap cache; /** @@ -23,10 +23,10 @@ public class CkanUtilsFactory { * */ private class CacheBean{ - CKanUtilsImpl utils; + DataCatalogueImpl utils; long ttl; - public CacheBean(long ttl, CKanUtilsImpl utils){ + public CacheBean(long ttl, DataCatalogueImpl utils){ this.ttl = ttl; this.utils = utils; } @@ -35,7 +35,7 @@ public class CkanUtilsFactory { /** * Private constructor */ - private CkanUtilsFactory(){ + private DataCatalogueFactory(){ logger.debug("Ckan factory object build"); cache = new ConcurrentHashMap(); @@ -46,7 +46,7 @@ public class CkanUtilsFactory { * Get the factory instance * @return */ - public static CkanUtilsFactory getFactory(){ + public static DataCatalogueFactory getFactory(){ logger.debug("Factory requested"); return instance; } @@ -57,7 +57,7 @@ public class CkanUtilsFactory { * @return * @throws Exception */ - public CKanUtilsImpl getUtilsPerScope(String scope) throws Exception{ + public DataCatalogueImpl getUtilsPerScope(String scope) throws Exception{ if(scope == null || scope.isEmpty()) throw new IllegalArgumentException("Invalid scope given!"); @@ -66,7 +66,7 @@ public class CkanUtilsFactory { } else{ logger.info("Creating CKAN LIB utils for scope " + scope); - CKanUtilsImpl utils = new CKanUtilsImpl(scope); + DataCatalogueImpl utils = new DataCatalogueImpl(scope); cache.put(scope, new CacheBean(System.currentTimeMillis(), utils)); return utils; } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtilsImpl.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java similarity index 90% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtilsImpl.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java index 17e9b45..bf7a13f 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtilsImpl.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueImpl.java @@ -56,9 +56,9 @@ import eu.trentorise.opendata.jackan.model.CkanUser; * This is the Ckan Utils implementation class. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class CKanUtilsImpl implements CKanUtils{ +public class DataCatalogueImpl implements DataCatalogue{ - private static final Logger logger = LoggerFactory.getLogger(CKanUtilsImpl.class); + private static final Logger logger = LoggerFactory.getLogger(DataCatalogueImpl.class); private String CKAN_CATALOGUE_URL; private String CKAN_DB_NAME; @@ -77,9 +77,9 @@ public class CKanUtilsImpl implements CKanUtils{ * @param scope * @throws Exception if unable to find datacatalogue info */ - public CKanUtilsImpl(String scope) throws Exception{ + public DataCatalogueImpl(String scope) throws Exception{ - CKanRunningCluster runningInstance = new CKanRunningCluster(scope); + DataCatalogueRunningCluster runningInstance = new DataCatalogueRunningCluster(scope); // save information CKAN_DB_URL = runningInstance.getDatabaseHosts().get(0); @@ -607,7 +607,9 @@ public class CKanUtilsImpl implements CKanUtils{ CkanDataset dataset = new CkanDataset(); // get the name from the title - dataset.setName(UtilMethods.nameFromTitle(title)); + String name = UtilMethods.fromTitleToName(title); + logger.debug("Name of the dataset is going to be " + name); + dataset.setName(name); dataset.setTitle(title); CkanOrganization orgOwner = client.getOrganization(organizationNameOrId); @@ -862,7 +864,7 @@ public class CKanUtilsImpl implements CKanUtils{ String ckanUsername = UtilMethods.fromUsernameToCKanUsername(username); // check if this role is already present in ckan for this user within the group - String groupNameToCheck = groupName.toLowerCase(); + String groupNameToCheck = UtilMethods.fromGroupTitleToName(groupName); try{ boolean alreadyPresent = isRoleAlreadySet(ckanUsername, groupNameToCheck, correspondentRoleToCheck, true); @@ -1133,20 +1135,21 @@ public class CKanUtilsImpl implements CKanUtils{ // checks checkNotNull(nameOrId); - checkArgument(!nameOrId.isEmpty()); + checkArgument(!nameOrId.trim().isEmpty()); // check if it exists CkanGroup toCreate = null; CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS); - + logger.debug("Request for creating group with name " + nameOrId + " title " + title + " and description " + description); - if((toCreate = groupExists(nameOrId, client))!= null) + String name = UtilMethods.fromGroupTitleToName(nameOrId); + if((toCreate = groupExists(name, client))!= null) return toCreate; else{ try{ - CkanGroup group = new CkanGroup(nameOrId); + CkanGroup group = new CkanGroup(name); group.setTitle(title); group.setDisplayName(title); group.setDescription(description); @@ -1181,93 +1184,40 @@ public class CKanUtilsImpl implements CKanUtils{ return toReturn; } - // @Override - // public boolean assignDatasetToGroup(String groupNameOrId, - // String datasetNameOrId, String apiKey) { - // - // // checks - // checkNotNull(groupNameOrId); - // checkArgument(!groupNameOrId.isEmpty()); - // checkNotNull(datasetNameOrId); - // checkArgument(!datasetNameOrId.isEmpty()); - // checkNotNull(apiKey); - // checkArgument(!apiKey.isEmpty()); - // - // try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){ - // - // // we need to use the apis to make it - // String pathPatch = "/api/3/action/group_patch"; - // String pathShow = "/api/3/action/group_show"; - // - // String urlShow = CKAN_CATALOGUE_URL + pathShow + "?" + "id=" + groupNameOrId + "&include_datasets=true"; - // - // HttpGet request = new HttpGet(urlShow); - // request.addHeader("Authorization", apiKey); - // HttpResponse response = httpClient.execute(request); - // logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); - // - // if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ - // - // // parse the json and convert to java beans - // BufferedReader br = new BufferedReader( - // new InputStreamReader((response.getEntity().getContent()))); - // - // String output; - // String res = ""; - // while ((output = br.readLine()) != null) { - // res += output; - // } - // - // if(res == "") - // return false; - // - // // parse the json object returned - // JSONParser parser = new JSONParser(); - // JSONObject json = (JSONObject) parser.parse(res); - // JSONObject resultJson = (JSONObject) json.get("result"); - // JSONArray packages = (JSONArray) resultJson.get("packages"); - // - // // get current list of packages - // List currentPackages = new ArrayList(); - // Iterator packageIterator = packages.iterator(); - // while (packageIterator.hasNext()) { - // JSONObject packageObj = (JSONObject) packageIterator.next(); - // currentPackages.add(packageObj.get("name").toString()); - // } - // - // // add the new one - // currentPackages.add(datasetNameOrId); - // - // String replaceWith = ""; - // boolean first = true; - // for (String datasetId : currentPackages) { - // replaceWith += first ? "{\"id\":\"" + datasetId + "\"}" : ",{\"id\":\"" + datasetId + "\"}"; - // first = false; - // } - // - // // build entity for patch - // String entity = "{\"id\":\"" + groupNameOrId + "\", \"packages\":[REPLACE_PACKAGES]}"; - // entity = entity.replace("REPLACE_PACKAGES", replaceWith); - // - // logger.debug("entity is " + entity); - // - // // perform post request - // HttpPost requestUpdate = new HttpPost(CKAN_CATALOGUE_URL + pathPatch); - // requestUpdate.addHeader("Authorization", apiKey); - // StringEntity params = new StringEntity(entity); - // requestUpdate.setEntity(params); - // HttpResponse responseUpdate = httpClient.execute(requestUpdate); - // logger.debug("Response code for update is " + responseUpdate.getStatusLine().getStatusCode() + " and response message is " + responseUpdate.getStatusLine().getReasonPhrase()); - // - // return responseUpdate.getStatusLine().getStatusCode() == HttpStatus.SC_OK; - // } - // - // }catch(Exception e){ - // logger.error("Unable to make this association", e); - // } - // - // return false; - // } + @Override + public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, String apiKey) { + + // checks + checkNotNull(groupNameOrId); + checkArgument(!groupNameOrId.isEmpty()); + checkNotNull(datasetNameOrId); + checkArgument(!datasetNameOrId.isEmpty()); + checkNotNull(apiKey); + checkArgument(!apiKey.isEmpty()); + + String groupNameToCheck = UtilMethods.fromGroupTitleToName(groupNameOrId); + + try{ + + CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey); + + // retrieve the dataset + CkanDataset dataset = client.getDataset(datasetNameOrId); + List datasetGroups = dataset.getGroups(); + CkanGroup destinationGroup = client.getGroup(groupNameToCheck); + datasetGroups.add(destinationGroup); + + // patch the dataset + client.patchUpdateDataset(dataset); + + return true; + + }catch(Exception e){ + logger.error("Unable to make this association", e); + } + + return false; + } @Override public Map> getRolesAndUsersOrganization(String organizationName) { @@ -1350,4 +1300,29 @@ public class CKanUtilsImpl implements CKanUtils{ return toReturn; } + @Override + public String getRoleOfUserInGroup(String username, String groupName, String apiKey) { + + String toReturn = null; + + String usernameCkan = UtilMethods.fromUsernameToCKanUsername(username); + + try{ + + CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey); + List users = client.getGroup(groupName).getUsers(); + for (CkanUser ckanUser : users) { + if(ckanUser.getName().equals(usernameCkan)){ + toReturn = ckanUser.getCapacity(); + break; + } + } + + }catch(Exception e){ + logger.error("Unable to retrieve the role the user has into this group", e); + } + + return toReturn; + } + } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanRunningCluster.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueRunningCluster.java similarity index 96% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanRunningCluster.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueRunningCluster.java index 8803f12..d718bf3 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanRunningCluster.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/DataCatalogueRunningCluster.java @@ -20,7 +20,7 @@ import org.gcube.common.resources.gcore.utils.XPathHelper; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.datacatalogue.ckanutillibrary.exceptions.ApplicationProfileNotFoundException; import org.gcube.datacatalogue.ckanutillibrary.exceptions.NoApplicationProfileMasterException; -import org.gcube.datacatalogue.ckanutillibrary.exceptions.NoCKanRuntimeResourceException; +import org.gcube.datacatalogue.ckanutillibrary.exceptions.NoDataCatalogueRuntimeResourceException; import org.gcube.datacatalogue.ckanutillibrary.exceptions.ServiceEndPointException; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.Query; @@ -35,10 +35,10 @@ import org.xml.sax.InputSource; * Retrieve ckan running instance information in the infrastructure (for both its database and data catalogue url) * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -public class CKanRunningCluster { +public class DataCatalogueRunningCluster { //logger - private static final Logger logger = LoggerFactory.getLogger(CKanRunningCluster.class); + private static final Logger logger = LoggerFactory.getLogger(DataCatalogueRunningCluster.class); // database of the datacatalogue info private final static String RUNTIME_DB_RESOURCE_NAME = "CKanDatabase"; @@ -67,7 +67,7 @@ public class CKanRunningCluster { // this token is needed in order to assign roles to user private String sysAdminToken; - public CKanRunningCluster(String scope) throws Exception{ + public DataCatalogueRunningCluster(String scope) throws Exception{ if(scope == null || scope.isEmpty()) throw new Exception("Invalid scope!!"); @@ -85,7 +85,7 @@ public class CKanRunningCluster { if (resources.size() == 0){ logger.error("There is no Runtime Resource having name " + RUNTIME_DB_RESOURCE_NAME +" and Platform " + PLATFORM_DB_NAME + " in this scope."); - throw new NoCKanRuntimeResourceException(); + throw new NoDataCatalogueRuntimeResourceException(); } else { try{ @@ -175,7 +175,7 @@ public class CKanRunningCluster { if (resources.size() == 0){ logger.error("There is no Runtime Resource having name " + RUNTIME_CATALOGUE_RESOURCE_NAME +" and Platform " + PLATFORM_CATALOGUE_NAME + " in this scope."); - throw new NoCKanRuntimeResourceException(); + throw new NoDataCatalogueRuntimeResourceException(); } else { logger.debug(resources.toString()); diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/NoCKanRuntimeResourceException.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/NoDataCatalogueRuntimeResourceException.java similarity index 50% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/NoCKanRuntimeResourceException.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/NoDataCatalogueRuntimeResourceException.java index 07a9c43..cfe41aa 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/NoCKanRuntimeResourceException.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/NoDataCatalogueRuntimeResourceException.java @@ -1,22 +1,21 @@ package org.gcube.datacatalogue.ckanutillibrary.exceptions; /** - * No elasticsearch cluster in the infrastructure found exception. + * No Data Catalogue node found. * @author Costantino Perciante at ISTI-CNR * (costantino.perciante@isti.cnr.it) - * */ -public class NoCKanRuntimeResourceException extends Exception { +public class NoDataCatalogueRuntimeResourceException extends Exception { private static final long serialVersionUID = -40748130477807648L; - private static final String DEFAULT_MESSAGE = "No CKan catalogue instance for this scope!"; + private static final String DEFAULT_MESSAGE = "No Data Catalogue instance for this scope!"; - public NoCKanRuntimeResourceException(){ + public NoDataCatalogueRuntimeResourceException(){ super(DEFAULT_MESSAGE); } - public NoCKanRuntimeResourceException(String message) { + public NoDataCatalogueRuntimeResourceException(String message) { super(message); } } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/ServiceEndPointException.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/ServiceEndPointException.java index a15e6a4..eeb6de7 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/ServiceEndPointException.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/exceptions/ServiceEndPointException.java @@ -2,15 +2,13 @@ package org.gcube.datacatalogue.ckanutillibrary.exceptions; /** * Exception thrown when it is not possible retrieve information from the ServiceEndpoint - * related to ElasticSearch - * @author Costantino Perciante at ISTI-CNR - * (costantino.perciante@isti.cnr.it) - * + * related to the Data Catalogue + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ public class ServiceEndPointException extends Exception { private static final long serialVersionUID = 7057074369001221035L; - private static final String DEFAULT_MESSAGE = "Unable to retrieve information from CKan endpoint!"; + private static final String DEFAULT_MESSAGE = "Unable to retrieve information from Data Catalogue endpoint!"; public ServiceEndPointException(){ super(DEFAULT_MESSAGE); diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/utils/UtilMethods.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/utils/UtilMethods.java index 6e7d634..1fdbd02 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/utils/UtilMethods.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/utils/UtilMethods.java @@ -23,7 +23,7 @@ public class UtilMethods { if(username == null) return null; - return username.replaceAll("\\.", "_").trim(); + return username.trim().replaceAll("\\.", "_"); } /** @@ -31,17 +31,26 @@ public class UtilMethods { * @param title * @return */ - public static String nameFromTitle(String title) { + public static String fromTitleToName(String title) { if(title == null) return null; - - String convertedName = title.replaceAll(" ", "_"); - convertedName = convertedName.replaceAll("\\.", "_"); - convertedName = convertedName.toLowerCase(); - if(convertedName.endsWith("_")) - convertedName = convertedName.substring(0, convertedName.length() - 2); - return convertedName; + String regexTitleNameTransform = "[^A-Za-z0-9_-]"; + return title.trim().replaceAll(regexTitleNameTransform, "_").replace("_+", "_").toLowerCase(); + } + + /** + * Convert a display group name to group id + * @param groupName + * @return + */ + public static String fromGroupTitleToName(String groupName){ + if(groupName == null) + return null; + + String regexGroupNameTransform = "[^A-Za-z0-9_]"; + return groupName.trim().replaceAll(regexGroupNameTransform, "_").replace("_+", "_").toLowerCase(); + } /** @@ -50,10 +59,10 @@ public class UtilMethods { * @return */ public static boolean resourceExists(String URLName){ - + if(URLName == null || URLName.isEmpty()) return false; - + try { HttpURLConnection.setFollowRedirects(true); HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection(); @@ -66,7 +75,7 @@ public class UtilMethods { return false; } } - + /** * Builds a string made of key + scope * @param key diff --git a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestDataCatalogueLib.java similarity index 80% rename from src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java rename to src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestDataCatalogueLib.java index 634da5e..3f25fea 100644 --- a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java +++ b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestDataCatalogueLib.java @@ -15,14 +15,19 @@ import org.gcube.datacatalogue.ckanutillibrary.models.RolesCkanGroupOrOrg; import org.slf4j.LoggerFactory; import eu.trentorise.opendata.jackan.CheckedCkanClient; +import eu.trentorise.opendata.jackan.model.CkanGroup; import eu.trentorise.opendata.jackan.model.CkanOrganization; import eu.trentorise.opendata.jackan.model.CkanUser; -public class TestCKanLib { +/** + * Tests class. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class TestDataCatalogueLib { - private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestCKanLib.class); + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestDataCatalogueLib.class); - private CkanUtilsFactory factory; + private DataCatalogueFactory factory; private String scope = "/gcube/devNext/NextNext"; private String testUser = "costantino_perciante"; String subjectId = "aa_father4"; @@ -30,13 +35,13 @@ public class TestCKanLib { //@Before public void before(){ - factory = CkanUtilsFactory.getFactory(); + factory = DataCatalogueFactory.getFactory(); } //@Test public void getRole() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); instance.getOrganizationsAndRolesByUser(testUser, Arrays.asList(RolesCkanGroupOrOrg.ADMIN, RolesCkanGroupOrOrg.EDITOR, RolesCkanGroupOrOrg.MEMBER )); @@ -45,7 +50,7 @@ public class TestCKanLib { //@Test public void datasetsRelationshipCreate() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); DatasetRelationships relation = DatasetRelationships.parent_of; @@ -57,7 +62,7 @@ public class TestCKanLib { //@Test public void datasetsRelationshipDelete() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); DatasetRelationships relation = DatasetRelationships.child_of; boolean resD = instance.deleteDatasetRelationship(subjectId, objectId, relation, instance.getApiKeyFromUsername(testUser)); @@ -68,7 +73,7 @@ public class TestCKanLib { //@Test public void datasetRelationshipRetrieve() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); List res = instance.getRelationshipDatasets(subjectId, objectId, instance.getApiKeyFromUsername(testUser)); @@ -79,7 +84,7 @@ public class TestCKanLib { //@Test public void factoryTest() throws Exception{ - CkanUtilsFactory factory = CkanUtilsFactory.getFactory(); + DataCatalogueFactory factory = DataCatalogueFactory.getFactory(); while(true){ factory.getUtilsPerScope("/gcube"); @@ -99,7 +104,7 @@ public class TestCKanLib { public void testgetApiKeyFromUser() throws Exception { logger.debug("Testing getApiKeyFromUser"); - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); String username = "francescomangiacrapa"; String key = instance.getApiKeyFromUsername(username); @@ -111,7 +116,7 @@ public class TestCKanLib { public void testgetUserFromApiKey() throws Exception { logger.debug("Testing getApiKeyFromUser"); - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); String key = "put-your-key-here"; CKanUserWrapper user = instance.getUserFromApiKey(key); @@ -123,7 +128,7 @@ public class TestCKanLib { public void getOrganizationsByUser() throws Exception { System.out.println("Testing getOrganizationsByUser"); - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); String username = "francescomangiacrapa"; List organizations = instance.getOrganizationsByUser(username); @@ -139,10 +144,10 @@ public class TestCKanLib { public void getGroupsAndRolesByUser() throws Exception { logger.debug("Testing getGroupsAndRolesByUser"); - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); String username = "andrea.rossi"; - instance = new CKanUtilsImpl("/gcube"); + instance = new DataCatalogueImpl("/gcube"); List rolesToMatch = new ArrayList(); rolesToMatch.add(RolesCkanGroupOrOrg.ADMIN); rolesToMatch.add(RolesCkanGroupOrOrg.MEMBER); @@ -154,7 +159,7 @@ public class TestCKanLib { //@Test public void getUsers() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); List rolesToMatch = new ArrayList(); rolesToMatch.add(RolesCkanGroupOrOrg.ADMIN); @@ -190,7 +195,7 @@ public class TestCKanLib { //@Test public void createUsers() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); + DataCatalogueImpl 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); @@ -203,7 +208,7 @@ public class TestCKanLib { //@Test public void createAsEditor() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); + DataCatalogueImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); boolean checkedEditor = instance.checkRoleIntoOrganization("user_editor_devvre", "devvre", RolesCkanGroupOrOrg.MEMBER); if(checkedEditor){ logger.debug("Created editor in devvre? " + checkedEditor); @@ -221,7 +226,7 @@ public class TestCKanLib { //@Test public void editorCreateDataset() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); + DataCatalogueImpl 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); } @@ -229,7 +234,7 @@ public class TestCKanLib { //@Test public void adminCreateDataset() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); + DataCatalogueImpl 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); } @@ -237,13 +242,13 @@ public class TestCKanLib { //@Test public void adminChangeVisibility() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope("/gcube/devsec/devVRE"); + DataCatalogueImpl 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")); boolean res = instance.setDatasetPrivate(false, "3571cca5-b0ae-4dc6-b791-434a8e062ce5", "33bbdcb1-929f-441f-8718-a9e5134f517d", instance.getApiKeyFromUsername("user_admin_devvre")); logger.debug(""+res); - + // 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()); @@ -257,10 +262,10 @@ public class TestCKanLib { //@Test public void testInvalidOrgRole() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); boolean res = instance.isRoleAlreadySet("francesco_mangiacrapa", "devvre_group", RolesCkanGroupOrOrg.ADMIN, true); logger.debug(""+res); - + // set to editor instance.checkRoleIntoGroup("francesco_mangiacrapa", "devvre_group", RolesCkanGroupOrOrg.EDITOR); @@ -269,10 +274,29 @@ public class TestCKanLib { //@Test public void existProductWithNameOrId() throws Exception{ - CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); boolean res = instance.existProductWithNameOrId("notification_portlet_2"); logger.debug(""+res); } + //@Test + public void createGroup() throws Exception{ + + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); + String title = "SoBigData.eu: Method Metadata NextNext"; + CkanGroup group = instance.createGroup(title, title, "A description for this group"); + + if(group != null){ + + boolean associated = instance.checkRoleIntoGroup("user_editor_devvre", title, RolesCkanGroupOrOrg.ADMIN); + + if(associated){ + + boolean assigned = instance.assignDatasetToGroup(title, "dataset_random_editor", instance.getApiKeyFromUsername("user_editor_devvre")); + logger.debug("Assigned is " + assigned); + } + } + } + }