Added useful tests for GRSF

This commit is contained in:
Luca Frosini 2022-04-28 10:47:52 +02:00
parent 4d290b3a13
commit 137608b1f1
2 changed files with 70 additions and 11 deletions

View File

@ -1,5 +1,8 @@
package org.gcube.gcat.persistence.ckan;
import java.util.Arrays;
import java.util.List;
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;
@ -29,15 +32,21 @@ public class CKANOrganizationTest extends ContextTest {
@Test
public void countOrganizations() throws Exception {
ContextTest.setContextByName("/gcube");
CKANOrganization ckanOrganization = new CKANOrganization();
int count = ckanOrganization.count();
logger.debug("The organizations are {}", count);
}
public static List<String> listOrg() throws Exception {
CKANOrganization ckanOrganization = new CKANOrganization();
String ret = ckanOrganization.list(1000, 0);
ObjectMapper objectMapper = new ObjectMapper();
String[] groupArray = objectMapper.readValue(ret, String[].class);
return Arrays.asList(groupArray);
}
@Test
public void listOrganizations() throws Exception {
ContextTest.setContextByName("/gcube");
CKANOrganization ckanOrganization = new CKANOrganization();
String ret = ckanOrganization.list(1000, 0);
logger.debug("{}", ret);

View File

@ -104,7 +104,18 @@ public class GRSFUtilities extends ContextTest {
return modified;
}
// @Test
public static final String GROUP_SUFFIX = "_group";
public static String getGroupId(String name) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(getGroupNameOnCkan(name));
if(!name.endsWith(GROUP_SUFFIX)) {
stringBuffer.append(GROUP_SUFFIX);
}
return stringBuffer.toString();
}
@Test
public void testGroupName() {
String[] groupNames = new String[] {
"GRSF", "Legacy", "Fishery", "Stock", "FIRMS", "FishSource", "RAM",
@ -115,11 +126,13 @@ public class GRSFUtilities extends ContextTest {
"GRSF SDG Flag", "GRSF Traceability Flag"
};
for(String name : groupNames) {
String ckanGroupName = getGroupNameOnCkan(name);
String groupName = CKANGroup.fromGroupTitleToName(name);
String ckanGroupName = getGroupId(name);
String groupName = CKANGroup.fromGroupTitleToName(name) + GROUP_SUFFIX;
logger.info("{} -> {}", name, ckanGroupName, groupName);
Assert.assertTrue(ckanGroupName.compareTo(groupName)==0);
}
}
// @Test
@ -141,11 +154,12 @@ public class GRSFUtilities extends ContextTest {
ObjectNode node = objectMapper.createObjectNode();
node.put("display_name", name);
node.put("title", name);
node.put("name", getGroupNameOnCkan(name));
node.put("name", getGroupId(name));
String json = objectMapper.writeValueAsString(node);
logger.info(json);
ckanGroupToCreate.create(json);
// ckanGroupToCreate.create(json);
}
}
// @Test
@ -162,7 +176,7 @@ public class GRSFUtilities extends ContextTest {
for(String name : groupNames) {
CKANGroup ckanGroupToCreate = new CKANGroup();
ckanGroupToCreate.setApiKey(sysAdminAPI);
ckanGroupToCreate.setName(getGroupNameOnCkan(name));
ckanGroupToCreate.setName(getGroupId(name));
try {
ckanGroupToCreate.purge();
}catch (Exception e) {
@ -173,15 +187,51 @@ public class GRSFUtilities extends ContextTest {
// @Test
public void associateUserToAllCKANGroups() throws ObjectNotFound, Exception {
public void associateUserToAllCKANGroupsAndOrganization() throws ObjectNotFound, Exception {
String username = "grsf_publisher";
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));
ckanUser.setName(username);
ckanUser.addToGroup(groupName);
}
List<String> orgs = CKANOrganizationTest.listOrg();
for(String org : orgs) {
CKANUser ckanUser = new CKANUser();
ckanUser.setApiKey(sysAdminAPI);
ckanUser.addUserToOrganization(org, username, "admin");
}
}
// @Test
public void manageOrganizations() throws JsonProcessingException {
String sysAdminAPI = CKANUtility.getSysAdminAPI();
String[] organizations = new String[] {
"GRSF_Pre", "FIRMS", "FishSource", "RAM"
};
ObjectMapper objectMapper = new ObjectMapper();
for(String org : organizations) {
CKANOrganization ckanOrganization = new CKANOrganization();
ckanOrganization.setApiKey(sysAdminAPI);
ckanOrganization.setName(org.toLowerCase());
ObjectNode node = objectMapper.createObjectNode();
node.put("display_name", org);
node.put("title", org);
node.put("name", org.toLowerCase());
String json = objectMapper.writeValueAsString(node);
try {
// ckanOrganization.create(json);
// ckanOrganization.purge();
}catch (Exception e) {
}
}
}
private void purgeGRSFRecords(int limit, int offset) throws Exception {