Created a separated class for GRSF utilities

This commit is contained in:
Luca Frosini 2021-11-12 14:33:17 +01:00
parent 7d822e07cb
commit e3b3c67c42
3 changed files with 137 additions and 112 deletions

View File

@ -1,27 +1,23 @@
package org.gcube.gcat.persistence.ckan;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.gcat.ContextTest;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class CKANGroupTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(CKANGroupTest.class);
@Ignore
@Test
public void count() throws Exception {
CKANGroup ckanGroup = new CKANGroup();
@ -29,6 +25,7 @@ public class CKANGroupTest extends ContextTest {
logger.debug("The groups are {}", count);
}
@Ignore
@Test
public void list() throws Exception {
CKANGroup ckanGroup = new CKANGroup();
@ -36,6 +33,7 @@ public class CKANGroupTest extends ContextTest {
logger.debug("{}", ret);
}
@Ignore
@Test
public void read() throws Exception {
CKANGroup ckanGroup = new CKANGroup();
@ -46,14 +44,7 @@ public class CKANGroupTest extends ContextTest {
logger.debug("{}", ret);
}
public static List<String> listGroup() throws Exception {
CKANGroup group = new CKANGroup();
String groups = group.list(1000, 0);
ObjectMapper objectMapper = new ObjectMapper();
String[] groupArray = objectMapper.readValue(groups, String[].class);
return Arrays.asList(groupArray);
}
@Ignore
@Test
public void createDeleteGroup() throws Exception {
CKANGroup ckanGroup = new CKANGroup();
@ -64,9 +55,17 @@ public class CKANGroupTest extends ContextTest {
ckanGroup.delete(true);
}
public static List<String> listGroup() throws Exception {
CKANGroup group = new CKANGroup();
String groups = group.list(1000, 0);
ObjectMapper objectMapper = new ObjectMapper();
String[] groupArray = objectMapper.readValue(groups, String[].class);
return Arrays.asList(groupArray);
}
// @Test
public void listAllGroups() throws Exception {
ContextTest.setContextByName("/d4science.research-infrastructures.eu/D4Research/AGINFRAplusDev");
ContextTest.setContextByName("");
CKANGroup ckanGroup = new CKANGroup();
ckanGroup.setApiKey(CKANUtility.getSysAdminAPI());
List<String> groups = listGroup();
@ -75,7 +74,7 @@ public class CKANGroupTest extends ContextTest {
// @Test
public void deleteAllGroups() throws Exception {
//ContextTest.setContextByName("/d4science.research-infrastructures.eu/D4Research/AGINFRAplusDev");
//ContextTest.setContextByName("");
CKANGroup ckanGroup = new CKANGroup();
ckanGroup.setApiKey(CKANUtility.getSysAdminAPI());
List<String> groups = listGroup();
@ -84,65 +83,5 @@ public class CKANGroupTest extends ContextTest {
ckanGroup.delete(true);
}
}
private void create(Set<String> createdGroup, Map<String, String> groups, String name) throws JsonProcessingException, IOException {
if(createdGroup.contains(name)) {
return;
}
String sysAdminAPI = ""; // CKANUtility.getSysAdminAPI();
CKANGroup ckanGroupToCreate = new CKANGroup();
ckanGroupToCreate.setApiKey(sysAdminAPI);
ckanGroupToCreate.setName(name);
// ckanGroupToCreate.purge();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(groups.get(name));
ArrayNode array = (ArrayNode) jsonNode.get("groups");
for(JsonNode node : array) {
String parentName = node.get("name").asText();
if(!createdGroup.contains(parentName)) {
create(createdGroup, groups, parentName);
}
}
ckanGroupToCreate.create(groups.get(name));
createdGroup.add(name);
}
// @Test
public void createGRSFGroups() throws ObjectNotFound, Exception {
// Set grsf.publisher user
ContextTest.setContext("");
String key = CKANUtility.getSysAdminAPI();
List<String> groupNames = listGroup();
Map<String, String> groups = new HashMap<>();
for(String name : groupNames) {
CKANGroup ckanGroup = new CKANGroup();
ckanGroup.setApiKey(key);
ckanGroup.setName(name);
String read = ckanGroup.read();
groups.put(name, read);
}
Set<String> createdGroup = new HashSet<>();
// Setting GRSF_PRE token of grsf.publisher user to create groups
ContextTest.setContext("");
for(String name : groupNames) {
create(createdGroup, groups, name);
}
}
// @Test
public void associateUserToAllCKANGroups() throws ObjectNotFound, Exception {
// Set grsf.publisher user
ContextTest.setContext("");
String sysAdminAPI = CKANUtility.getSysAdminAPI();
List<String> groupNames = listGroup();
for(String groupName : groupNames) {
CKANUser ckanUser = new CKANUser();
ckanUser.setApiKey(sysAdminAPI);
ckanUser.setName("grsf_publisher");
ckanUser.addToGroup(CKANGroup.getCKANGroupName(groupName));
}
}
}

View File

@ -561,36 +561,5 @@ public class CKANPackageTest extends ContextTest {
ckanPackage.setName("vre_picture");
ckanPackage.update("{\"rating\": 0.0, \"license_title\": \"Academic Free License 3.0\", \"maintainer\": \"Kerekes Kata\", \"relationships_as_object\": [], \"private\": false, \"maintainer_email\": \"kerekeska@nebih.gov.hu\", \"num_tags\": 1, \"id\": \"7731b70f-47ff-4b74-b943-188215e82d07\", \"metadata_created\": \"2020-01-07T16:40:24.822719\", \"owner_org\": \"3571cca5-b0ae-4dc6-b791-434a8e062ce5\", \"metadata_modified\": \"2020-02-03T15:16:42.596068\", \"author\": \"Kerekes Kata\", \"author_email\": \"kerekeska@nebih.gov.hu\", \"state\": \"active\", \"version\": \"1\", \"license_id\": \"AFL-3.0\", \"type\": \"dataset\", \"resources\": [{\"cache_last_updated\": null, \"cache_url\": null, \"mimetype_inner\": null, \"hash\": \"\", \"description\": \"\", \"format\": \"JPEG\", \"url\": \"https://goo.gl/SnwAM7\", \"created\": \"2019-04-01T13:24:40.738838\", \"state\": \"active\", \"package_id\": \"7731b70f-47ff-4b74-b943-188215e82d07\", \"last_modified\": null, \"mimetype\": \"image/jpeg\", \"url_type\": null, \"position\": 0, \"revision_id\": \"06d61000-a0c1-4155-ad2d-78ede56d6bb5\", \"size\": null, \"datastore_active\": false, \"id\": \"1de8851d-1385-47ae-9c93-6040d170a9cc\", \"resource_type\": null, \"name\": \"th.jpeg\"}], \"num_resources\": 1, \"tags\": [{\"vocabulary_id\": null, \"state\": \"active\", \"display_name\": \"DEMETER\", \"id\": \"4e05058b-a006-4dbf-94f5-277a30318323\", \"name\": \"DEMETER\"}], \"groups\": [], \"creator_user_id\": \"7020f836-45f4-4ee8-9c65-e7504209644f\", \"relationships_as_subject\": [], \"name\": \"vre_picture\", \"isopen\": true, \"url\": \"\", \"notes\": \"This is a nice picture of a VRE ;)\", \"title\": \"VRE picture\", \"extras\": [{\"value\": \"https://data.d4science.org/ctlg/DEMETER_trial/vre_picture\", \"key\": \"Item URL\"}, {\"value\": \"ResearchObject\", \"key\": \"system:type\"}], \"license_url\": \"https://www.opensource.org/licenses/AFL-3.0\", \"ratings_count\": 0, \"organization\": {\"description\": \"\", \"title\": \"devVRE\", \"created\": \"2016-05-30T11:30:41.710079\", \"approval_status\": \"approved\", \"is_organization\": true, \"state\": \"active\", \"image_url\": \"\", \"revision_id\": \"7c8463df-ed3f-4d33-87d8-6c0bcbe30d5d\", \"type\": \"organization\", \"id\": \"3571cca5-b0ae-4dc6-b791-434a8e062ce5\", \"name\": \"devvre\"}, \"revision_id\": \"bdb6169a-6268-43d6-b7e1-265c0c9e1a1c\"}");
}
/*
@Test
public void purgeAll() throws Exception {
//ContextTest.setContextByName("/d4science.research-infrastructures.eu/FARM/GRSF_Pre");
logger.debug("Going to purge all records");
CKANPackage ckanPackage = new CKANPackage();
ObjectMapper mapper = new ObjectMapper();
boolean go = true;
while(go) {
String ret = ckanPackage.list(10, 0);
JsonNode gotList = mapper.readTree(ret);
Assert.assertTrue(gotList instanceof ArrayNode);
ArrayNode itemsArray = (ArrayNode) gotList;
if(itemsArray.size()>0) {
for(JsonNode jsonNode : itemsArray) {
String name = jsonNode.asText();
logger.debug("Going to purge record with name {}", name);
ckanPackage.setName(name);
// ckanPackage.delete(true);
if(go) {
break;
}
Thread.sleep(500);
}
}else {
go=false;
}
}
}
*/
}

View File

@ -0,0 +1,117 @@
package org.gcube.gcat.persistence.ckan;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.gcat.ContextTest;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class GRSFUtilities extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(CKANGroupTest.class);
private void create(Set<String> createdGroup, Map<String, String> groups, String name) throws JsonProcessingException, IOException {
if(createdGroup.contains(name)) {
return;
}
String sysAdminAPI = ""; // CKANUtility.getSysAdminAPI();
CKANGroup ckanGroupToCreate = new CKANGroup();
ckanGroupToCreate.setApiKey(sysAdminAPI);
ckanGroupToCreate.setName(name);
// ckanGroupToCreate.purge();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(groups.get(name));
ArrayNode array = (ArrayNode) jsonNode.get("groups");
for(JsonNode node : array) {
String parentName = node.get("name").asText();
if(!createdGroup.contains(parentName)) {
create(createdGroup, groups, parentName);
}
}
ckanGroupToCreate.create(groups.get(name));
createdGroup.add(name);
}
// @Test
public void createGRSFGroups() throws ObjectNotFound, Exception {
// Set grsf.publisher user
ContextTest.setContext("");
String key = CKANUtility.getSysAdminAPI();
List<String> groupNames = CKANGroupTest.listGroup();
Map<String, String> groups = new HashMap<>();
for(String name : groupNames) {
CKANGroup ckanGroup = new CKANGroup();
ckanGroup.setApiKey(key);
ckanGroup.setName(name);
String read = ckanGroup.read();
groups.put(name, read);
}
Set<String> createdGroup = new HashSet<>();
// Setting GRSF_PRE token of grsf.publisher user to create groups
ContextTest.setContext("");
for(String name : groupNames) {
create(createdGroup, groups, name);
}
}
// @Test
public void associateUserToAllCKANGroups() throws ObjectNotFound, Exception {
// Set grsf.publisher user
ContextTest.setContext("");
String sysAdminAPI = CKANUtility.getSysAdminAPI();
List<String> groupNames = CKANGroupTest.listGroup();
for(String groupName : groupNames) {
CKANUser ckanUser = new CKANUser();
ckanUser.setApiKey(sysAdminAPI);
ckanUser.setName("grsf_publisher");
ckanUser.addToGroup(CKANGroup.getCKANGroupName(groupName));
}
}
// @Test
public void purgeAllGRSFRecords() throws Exception {
ContextTest.setContextByName("/d4science.research-infrastructures.eu/FARM/GRSF_Pre");
logger.debug("Going to purge all records");
CKANPackage ckanPackage = new CKANPackage();
ObjectMapper mapper = new ObjectMapper();
boolean go = true;
while(go) {
String ret = ckanPackage.list(10, 0);
JsonNode gotList = mapper.readTree(ret);
Assert.assertTrue(gotList instanceof ArrayNode);
ArrayNode itemsArray = (ArrayNode) gotList;
if(itemsArray.size()>0) {
for(JsonNode jsonNode : itemsArray) {
String name = jsonNode.asText();
logger.debug("Going to purge record with name {}", name);
ckanPackage.setName(name);
// ckanPackage.delete(true);
if(go) {
break;
}
Thread.sleep(500);
}
}else {
go=false;
}
}
}
}