128 lines
4.1 KiB
Java
128 lines
4.1 KiB
Java
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.Test;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class CKANGroupTest extends ContextTest {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(CKANGroupTest.class);
|
|
|
|
@Test
|
|
public void list() throws Exception {
|
|
CKANGroup ckanGroup = new CKANGroup();
|
|
String ret = ckanGroup.list(10, 0);
|
|
logger.debug("{}", ret);
|
|
}
|
|
|
|
@Test
|
|
public void read() throws Exception {
|
|
CKANGroup ckanGroup = new CKANGroup();
|
|
ckanGroup.setApiKey(CKANUtility.getSysAdminAPI());
|
|
String name = "abundance-level";
|
|
ckanGroup.setName(name);
|
|
String ret = ckanGroup.read();
|
|
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);
|
|
}
|
|
|
|
@Test
|
|
public void createDeleteGroup() throws Exception {
|
|
CKANGroup ckanGroup = new CKANGroup();
|
|
ckanGroup.setApiKey(CKANUtility.getSysAdminAPI());
|
|
String name = "my-test-group";
|
|
ckanGroup.setName(name);
|
|
ckanGroup.create();
|
|
ckanGroup.delete(true);
|
|
}
|
|
|
|
// @Test
|
|
public void listAllGroups() throws Exception {
|
|
ContextTest.setContextByName("/d4science.research-infrastructures.eu/D4Research/AGINFRAplusDev");
|
|
CKANGroup ckanGroup = new CKANGroup();
|
|
ckanGroup.setApiKey(CKANUtility.getSysAdminAPI());
|
|
List<String> groups = listGroup();
|
|
logger.debug("Groups are:\n{}", groups);
|
|
}
|
|
|
|
// @Test
|
|
public void deleteAllGroups() throws Exception {
|
|
//ContextTest.setContextByName("/d4science.research-infrastructures.eu/D4Research/AGINFRAplusDev");
|
|
CKANGroup ckanGroup = new CKANGroup();
|
|
ckanGroup.setApiKey(CKANUtility.getSysAdminAPI());
|
|
List<String> groups = listGroup();
|
|
for(String name : groups) {
|
|
ckanGroup.setName(name);
|
|
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 = "522557b4-f709-4935-be81-947e13afa2a7"; // 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 {
|
|
// Setting GRSF ADMIN token of grsf.publisher user to read groups
|
|
ContextTest.setContext("5213d3d8-6ea3-4d55-bf1c-da25ed3aba43-843339462");
|
|
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("17f72d12-5fe0-48a4-90d3-c398c41aedfe-843339462");
|
|
for(String name : groupNames) {
|
|
create(createdGroup, groups, name);
|
|
}
|
|
|
|
}
|
|
}
|