From e10dbc9c4a1fd8988411f8b07d43635eb45d8845 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 27 Apr 2022 15:36:26 +0200 Subject: [PATCH] Added GRSF tests --- .../gcat/persistence/ckan/GRSFUtilities.java | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/GRSFUtilities.java b/src/test/java/org/gcube/gcat/persistence/ckan/GRSFUtilities.java index b49e93b..951f496 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/GRSFUtilities.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/GRSFUtilities.java @@ -12,6 +12,7 @@ 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.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.gcat.ContextTest; import org.junit.Assert; @@ -33,12 +34,12 @@ public class GRSFUtilities extends ContextTest { public static final String PREPROD_GRSF_PRE = "/pred4s/preprod/GRSF_Pre"; public GRSFUtilities() throws ObjectNotFound, Exception { - ContextTest.setContextByName(PRODUCTION_GRSF_PRE); + ContextTest.setContextByName(PREPROD_GRSF_PRE); } @Test public void test() throws ObjectNotFound, Exception { -// ContextTest.setContextByName(PREPROD_GRSF_PRE); + ContextTest.setContextByName(PREPROD_GRSF_PRE); // // ContextTest.setContextByName(PRODUCTION_GRSF); // ContextTest.setContextByName(PRODUCTION_GRSF_ADMIN); @@ -87,6 +88,50 @@ public class GRSFUtilities extends ContextTest { } } + public static String getGroupNameOnCkan(String origName){ + + if(origName == null) + throw new IllegalArgumentException("origName cannot be null"); + + String modified = origName.replaceAll("\\(", ""); + modified = origName.replaceAll("\\)", ""); + modified = origName.trim().toLowerCase().replaceAll("[^A-Za-z0-9-]", "-"); + if(modified.startsWith("-")) + modified = modified.substring(1); + if(modified.endsWith("-")) + modified = modified.substring(0, modified.length() -1); + + logger.info("Group name generated is " + modified); + + return modified; + } + + // @Test + public void createNewGRSFGroups() throws ObjectNotFound, Exception { + String sysAdminAPI = CKANUtility.getSysAdminAPI(); + String[] groupNames = new String[] { + "GRSF", "Legacy", "Fishery", "Stock", "FIRMS", "FishSource", "RAM", + "Catch", "Landing", + "Abundance Level", "Abundance Level (FIRMS Standard)", "Biomass", + "Fishing Pressure", "Fishing Pressure (FIRMS Standard)", "State and Trend", + "FAO Stock Status Category", "Scientific Advice", + "GRSF SDG Flag", "GRSF Traceability Flag" + }; + ObjectMapper objectMapper = new ObjectMapper(); + for(String name : groupNames) { + CKANGroup ckanGroupToCreate = new CKANGroup(); + ckanGroupToCreate.setApiKey(sysAdminAPI); + ckanGroupToCreate.setName(name); + ObjectNode node = objectMapper.createObjectNode(); + node.put("display_name", name); + node.put("title", name); + node.put("name", getGroupNameOnCkan(name)); + String json = objectMapper.writeValueAsString(node); + logger.info(json); + ckanGroupToCreate.create(json); + } + } + // @Test public void associateUserToAllCKANGroups() throws ObjectNotFound, Exception { String sysAdminAPI = CKANUtility.getSysAdminAPI();