From e3e8ffd39336e6322f9ac97b9d7629ad23a04f97 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 12 May 2022 16:25:58 +0200 Subject: [PATCH 01/10] Changed GRSF tests --- CHANGELOG.md | 5 ++ pom.xml | 2 +- .../org/gcube/gcat/persistence/ckan/CKAN.java | 2 +- .../gcat/persistence/ckan/CKANPackage.java | 23 ++++- .../gcat/persistence/ckan/CKANResource.java | 2 +- .../gcube/gcat/persistence/ckan/CKANUser.java | 2 +- .../persistence/ckan/CKANPackageTest.java | 2 +- .../{ckan => grsf}/GRSFUtilities.java | 86 +++++++++++++------ src/test/resources/logback-test.xml | 3 +- 9 files changed, 91 insertions(+), 36 deletions(-) rename src/test/java/org/gcube/gcat/persistence/{ckan => grsf}/GRSFUtilities.java (75%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24583d0..e9967e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm # Changelog for gCube Catalogue (gCat) Service +## [v2.3.0-SNAPSHOT] + +- Added moderation link in moderation message [#23142] +- Switched moderation messages to notification [#23317] + ## [v2.2.0] - Switched gcat credentials to new IAM authz [#21628][#22727] diff --git a/pom.xml b/pom.xml index 78ccd48..769aba2 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ org.gcube.data-catalogue gcat war - 2.2.0 + 2.3.0-SNAPSHOT gCube Catalogue (gCat) Service This service allows any client to publish on the gCube Catalogue. diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKAN.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKAN.java index 8190dc8..493b2af 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKAN.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKAN.java @@ -360,7 +360,7 @@ public abstract class CKAN { } } - protected void purge() { + public void purge() { sendPostRequest(PURGE, createJsonNodeWithNameAsID()); } diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java index 82d6e3d..56fe2d9 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java @@ -504,6 +504,23 @@ public class CKANPackage extends CKAN implements Moderated { return getAsString(arrayNode); } + public String listNoCheck(int limit, int offset) { + Map parameters = new HashMap<>(); + if(limit <= 0) { + // According to CKAN documentation + // the number of matching rows to return. There is a hard limit of 1000 datasets per query. + // see https://docs.ckan.org/en/2.6/api/index.html#ckan.logic.action.get.package_search + limit = 1000; + } + parameters.put(ROWS_KEY, String.valueOf(limit)); + + if(offset < 0) { + offset = 0; + } + parameters.put(START_KEY, String.valueOf(offset)); + return list(parameters); + } + @Override public String list(int limit, int offset) { Map parameters = getListingParameters(limit, offset); @@ -983,9 +1000,9 @@ public class CKANPackage extends CKAN implements Moderated { /** * Used for bulk purging. Internal use only */ - protected void purgeNoCheckNoDeleteFiles() { -// setApiKey(CKANUtility.getSysAdminAPI()); -// super.purge(); + public void purgeNoCheckNoDeleteFiles() { + setApiKey(CKANUtility.getSysAdminAPI()); + super.purge(); } /* diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANResource.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANResource.java index 64903db..d5743cb 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANResource.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANResource.java @@ -471,7 +471,7 @@ public class CKANResource extends CKAN { } @Override - protected void purge() { + public void purge() { String[] moreAllowed = new String[] {HEAD.class.getSimpleName(), GET.class.getSimpleName(), PUT.class.getSimpleName(), DELETE.class.getSimpleName()}; throw new NotAllowedException(OPTIONS.class.getSimpleName(), moreAllowed); diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java index d0379a4..b8c444f 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java @@ -215,7 +215,7 @@ public class CKANUser extends CKAN { return ret; } - protected void addUserToOrganization(String organizationName, String ckanUsername, String role) { + public void addUserToOrganization(String organizationName, String ckanUsername, String role) { logger.trace("Going to add user {} to organization {} with role {}", ckanUsername, organizationName, role); CKANOrganization ckanOrganization = new CKANOrganization(); ckanOrganization.setApiKey(CKANUtility.getSysAdminAPI()); diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java index de150ff..6998e90 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java @@ -75,7 +75,7 @@ public class CKANPackageTest extends ContextTest { logger.debug("List:\n{}", mapper.writeValueAsString(gotList)); } - protected UriInfo getUriInfo(MultivaluedMap queryParameters) { + public static UriInfo getUriInfo(MultivaluedMap queryParameters) { UriInfo uriInfo = new UriInfo() { @Override diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/GRSFUtilities.java b/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java similarity index 75% rename from src/test/java/org/gcube/gcat/persistence/ckan/GRSFUtilities.java rename to src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java index acf5440..d0ec8b1 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/GRSFUtilities.java +++ b/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java @@ -1,4 +1,4 @@ -package org.gcube.gcat.persistence.ckan; +package org.gcube.gcat.persistence.grsf; import java.io.IOException; import java.util.HashMap; @@ -7,14 +7,26 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriInfo; + 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.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.gcat.ContextTest; +import org.gcube.gcat.persistence.ckan.CKANGroup; +import org.gcube.gcat.persistence.ckan.CKANGroupTest; +import org.gcube.gcat.persistence.ckan.CKANOrganization; +import org.gcube.gcat.persistence.ckan.CKANOrganizationTest; +import org.gcube.gcat.persistence.ckan.CKANPackage; +import org.gcube.gcat.persistence.ckan.CKANPackageTest; +import org.gcube.gcat.persistence.ckan.CKANUser; +import org.gcube.gcat.persistence.ckan.CKANUtility; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; @@ -25,7 +37,7 @@ import org.slf4j.LoggerFactory; */ public class GRSFUtilities extends ContextTest { - private static Logger logger = LoggerFactory.getLogger(CKANGroupTest.class); + private static Logger logger = LoggerFactory.getLogger(GRSFUtilities.class); public static final String PRODUCTION_GRSF = "/d4science.research-infrastructures.eu/FARM/GRSF"; public static final String PRODUCTION_GRSF_ADMIN = "/d4science.research-infrastructures.eu/FARM/GRSF_Admin"; @@ -137,6 +149,8 @@ public class GRSFUtilities extends ContextTest { // @Test public void createNewGRSFGroups() throws ObjectNotFound, Exception { +// ContextTest.setContext(""); + logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); String sysAdminAPI = CKANUtility.getSysAdminAPI(); String[] groupNames = new String[] { "GRSF", "Legacy", "Fishery", "Stock", "FIRMS", "FishSource", "RAM", @@ -157,28 +171,35 @@ public class GRSFUtilities extends ContextTest { node.put("name", getGroupId(name)); String json = objectMapper.writeValueAsString(node); logger.info(json); - // ckanGroupToCreate.create(json); + ckanGroupToCreate.create(json); } - } // @Test public void deleteNewGRSFGroups() throws ObjectNotFound, Exception { + ContextTest.setContext(""); + logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); + logger.debug(SecretManagerProvider.instance.get().getContext()); + 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" - }; +// 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" +// }; +// + List groupNames = CKANGroupTest.listGroup(); + for(String name : groupNames) { CKANGroup ckanGroupToCreate = new CKANGroup(); ckanGroupToCreate.setApiKey(sysAdminAPI); - ckanGroupToCreate.setName(getGroupId(name)); +// ckanGroupToCreate.setName(getGroupId(name)); + ckanGroupToCreate.setName(name); try { - ckanGroupToCreate.purge(); +// ckanGroupToCreate.purge(); }catch (Exception e) { } @@ -186,28 +207,34 @@ public class GRSFUtilities extends ContextTest { } - // @Test + @Test public void associateUserToAllCKANGroupsAndOrganization() throws ObjectNotFound, Exception { - String username = "grsf_publisher"; - // username = "luca_frosini"; - // username = "francesco_mangiacrapa"; + ContextTest.setContext(""); + logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); + logger.debug(SecretManagerProvider.instance.get().getContext()); + + String[] usernames = new String[] { "grsf_publisher", "luca_frosini", "francesco_mangiacrapa"}; + String sysAdminAPI = CKANUtility.getSysAdminAPI(); List groupNames = CKANGroupTest.listGroup(); for(String groupName : groupNames) { CKANUser ckanUser = new CKANUser(); ckanUser.setApiKey(sysAdminAPI); - ckanUser.setName(username); - ckanUser.addToGroup(groupName); + for(String username : usernames) { + ckanUser.setName(username); + ckanUser.addToGroup(groupName); + } } List orgs = CKANOrganizationTest.listOrg(); for(String org : orgs) { CKANUser ckanUser = new CKANUser(); ckanUser.setApiKey(sysAdminAPI); - ckanUser.addUserToOrganization(org, username, "admin"); + for(String username : usernames) { + ckanUser.addUserToOrganization(org, username, "admin"); + } } - } // @Test @@ -239,10 +266,13 @@ public class GRSFUtilities extends ContextTest { private void purgeGRSFRecords(int limit, int offset) throws Exception { logger.debug("Going to purge {} records, starting from {}", limit, limit*offset); CKANPackage ckanPackage = new CKANPackage(); + MultivaluedMap mvm = new MultivaluedHashMap(); + UriInfo uriInfo = CKANPackageTest.getUriInfo(mvm); + ckanPackage.setUriInfo(uriInfo); ObjectMapper mapper = new ObjectMapper(); boolean go = true; while(go) { - String ret = ckanPackage.list(limit, offset); + String ret = ckanPackage.listNoCheck(limit, offset); JsonNode gotList = mapper.readTree(ret); Assert.assertTrue(gotList instanceof ArrayNode); ArrayNode itemsArray = (ArrayNode) gotList; @@ -252,14 +282,14 @@ public class GRSFUtilities extends ContextTest { logger.debug("Going to purge record with name {}", name); ckanPackage.setName(name); try { - ckanPackage.purgeNoCheckNoDeleteFiles(); + // ckanPackage.purgeNoCheckNoDeleteFiles(); if(go) { break; } }catch (Exception e) { // TODO: handle exception } - // Thread.sleep(500); + Thread.sleep(500); } }else { go=false; @@ -267,9 +297,11 @@ public class GRSFUtilities extends ContextTest { } } - @JsonIgnore // @Test public void purgeAllGRSFRecords() throws Exception { + ContextTest.setContext(""); + logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); + logger.debug(SecretManagerProvider.instance.get().getContext()); purgeGRSFRecords(100, 0); } diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index 4da43d9..2e2f160 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -10,7 +10,8 @@ - + + From eff29b0176527e5050d15c17b383808792cf9afd Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 12 May 2022 16:29:00 +0200 Subject: [PATCH 02/10] Fixed test --- .../gcat/persistence/grsf/GRSFUtilities.java | 30 ++++++------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java b/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java index d0ec8b1..3d29a40 100644 --- a/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java +++ b/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java @@ -28,6 +28,7 @@ import org.gcube.gcat.persistence.ckan.CKANPackageTest; import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.persistence.ckan.CKANUtility; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,17 +46,16 @@ public class GRSFUtilities extends ContextTest { public static final String PREPROD_GRSF_PRE = "/pred4s/preprod/GRSF_Pre"; - public GRSFUtilities() throws ObjectNotFound, Exception { - ContextTest.setContextByName(PREPROD_GRSF_PRE); - } - - @Test - public void test() throws ObjectNotFound, Exception { + @BeforeClass + public void before() throws Exception { ContextTest.setContextByName(PREPROD_GRSF_PRE); // // ContextTest.setContextByName(PRODUCTION_GRSF); // ContextTest.setContextByName(PRODUCTION_GRSF_ADMIN); // ContextTest.setContextByName(PRODUCTION_GRSF_PRE); + + logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); + logger.debug(SecretManagerProvider.instance.get().getContext()); } private void create(Set createdGroup, Map groups, String name) throws JsonProcessingException, IOException { @@ -149,8 +149,6 @@ public class GRSFUtilities extends ContextTest { // @Test public void createNewGRSFGroups() throws ObjectNotFound, Exception { -// ContextTest.setContext(""); - logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); String sysAdminAPI = CKANUtility.getSysAdminAPI(); String[] groupNames = new String[] { "GRSF", "Legacy", "Fishery", "Stock", "FIRMS", "FishSource", "RAM", @@ -177,10 +175,6 @@ public class GRSFUtilities extends ContextTest { // @Test public void deleteNewGRSFGroups() throws ObjectNotFound, Exception { - ContextTest.setContext(""); - logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); - logger.debug(SecretManagerProvider.instance.get().getContext()); - String sysAdminAPI = CKANUtility.getSysAdminAPI(); // String[] groupNames = new String[] { // "GRSF", "Legacy", "Fishery", "Stock", "FIRMS", "FishSource", "RAM", @@ -192,6 +186,8 @@ public class GRSFUtilities extends ContextTest { // }; // List groupNames = CKANGroupTest.listGroup(); + logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); + logger.debug(SecretManagerProvider.instance.get().getContext()); for(String name : groupNames) { CKANGroup ckanGroupToCreate = new CKANGroup(); @@ -207,14 +203,9 @@ public class GRSFUtilities extends ContextTest { } - @Test + // @Test public void associateUserToAllCKANGroupsAndOrganization() throws ObjectNotFound, Exception { - ContextTest.setContext(""); - logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); - logger.debug(SecretManagerProvider.instance.get().getContext()); - String[] usernames = new String[] { "grsf_publisher", "luca_frosini", "francesco_mangiacrapa"}; - String sysAdminAPI = CKANUtility.getSysAdminAPI(); List groupNames = CKANGroupTest.listGroup(); @@ -299,9 +290,6 @@ public class GRSFUtilities extends ContextTest { // @Test public void purgeAllGRSFRecords() throws Exception { - ContextTest.setContext(""); - logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); - logger.debug(SecretManagerProvider.instance.get().getContext()); purgeGRSFRecords(100, 0); } From ff7b8d7399bbe1c7696d899a1ff3fe6e9201ac8b Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 19 May 2022 12:06:59 +0200 Subject: [PATCH 03/10] Added moderation link --- .../moderation/thread/ModerationThread.java | 39 ++++++++++++++++++- .../social/SocialMessageModerationThread.java | 6 ++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java index 0be68b4..dd7831b 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java @@ -1,9 +1,18 @@ package org.gcube.gcat.moderation.thread; +import java.util.HashMap; +import java.util.Map; + import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.gcat.api.configuration.CatalogueConfiguration; import org.gcube.gcat.api.moderation.CMItemStatus; import org.gcube.gcat.moderation.thread.social.SocialMessageModerationThread; import org.gcube.gcat.persistence.ckan.CKANUser; +import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; +import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP; +import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryStringBuilder; /** * @author Luca Frosini (ISTI - CNR) @@ -14,6 +23,8 @@ public abstract class ModerationThread { protected String itemName; protected String itemTitle; protected String itemURL; + + protected String moderationURL; protected boolean create; protected CMItemStatus cmItemStatus; @@ -31,7 +42,7 @@ public abstract class ModerationThread { this.objectMapper = new ObjectMapper(); this.itemAuthor = false; this.create = false; - cmItemStatus = CMItemStatus.PENDING; + this.cmItemStatus = CMItemStatus.PENDING; } public void setItemCoordinates(String itemID, String itemName, String itemTitle, String itemURL) { @@ -49,6 +60,32 @@ public abstract class ModerationThread { this.ckanUser = ckanUser; } + public String getModerationURL() { + if(moderationURL==null) { + try { + SecretManager secretManager = SecretManagerProvider.instance.get(); + String context = secretManager.getContext(); + UriResolverManager resolver = new UriResolverManager("CTLG"); + Map params = new HashMap(); + params.put("gcube_scope", context); //e.g. /gcube/devsec/devVRE + params.put("entity_context", "organization"); + params.put("entity_name", CatalogueConfiguration.getOrganizationName(context)); //e.g. devvre + + CatalogueResolverQueryStringBuilder builder = new CatalogueResolverQueryStringBuilder(itemName); //item name under moderation + builder.itemStatus(cmItemStatus.name()). //e.g. pending, approved, rejected + moderation(MODERATION_OP.show); + + String queryString = builder.buildQueryParametersToQueryString(); + params.put(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER, queryString); + + moderationURL = resolver.getLink(params, true); + }catch (Exception e) { + return itemURL; + } + } + return moderationURL; + } + /** * The message is sent as gCat * @param message diff --git a/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java index fc3eb3a..92c1f01 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java @@ -37,7 +37,11 @@ public class SocialMessageModerationThread extends ModerationThread { stringBuffer.append(itemID); stringBuffer.append("\n"); stringBuffer.append("URL: "); - stringBuffer.append(itemURL); + if(cmItemStatus == CMItemStatus.APPROVED) { + stringBuffer.append(itemURL); + }else { + stringBuffer.append(getModerationURL()); + } stringBuffer.append("\n\n"); return stringBuffer; } From 08312a1a19557e89f8f21c72170b84d3fd3b9934 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 23 May 2022 15:09:30 +0200 Subject: [PATCH 04/10] Fixed GRSF utility --- .../gcat/persistence/ckan/CKANPackage.java | 7 ++++++ .../gcat/persistence/grsf/GRSFUtilities.java | 23 ++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java index 56fe2d9..bffb802 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java @@ -518,6 +518,13 @@ public class CKANPackage extends CKAN implements Moderated { offset = 0; } parameters.put(START_KEY, String.valueOf(offset)); + + // This filter by type +// StringWriter qStringWriter = new StringWriter(); +// qStringWriter.append("extras_systemtype:"); +// qStringWriter.append(""); +// parameters.put(GCatConstants.Q_KEY, qStringWriter.toString()); + return list(parameters); } diff --git a/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java b/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java index 3d29a40..efc325b 100644 --- a/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java +++ b/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java @@ -28,7 +28,7 @@ import org.gcube.gcat.persistence.ckan.CKANPackageTest; import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.persistence.ckan.CKANUtility; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,14 +46,14 @@ public class GRSFUtilities extends ContextTest { public static final String PREPROD_GRSF_PRE = "/pred4s/preprod/GRSF_Pre"; - @BeforeClass + @Before public void before() throws Exception { - ContextTest.setContextByName(PREPROD_GRSF_PRE); -// -// ContextTest.setContextByName(PRODUCTION_GRSF); +// ContextTest.setContextByName(PREPROD_GRSF_PRE); + + ContextTest.setContextByName(PRODUCTION_GRSF); // ContextTest.setContextByName(PRODUCTION_GRSF_ADMIN); // ContextTest.setContextByName(PRODUCTION_GRSF_PRE); - + logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); logger.debug(SecretManagerProvider.instance.get().getContext()); } @@ -273,22 +273,19 @@ public class GRSFUtilities extends ContextTest { logger.debug("Going to purge record with name {}", name); ckanPackage.setName(name); try { - // ckanPackage.purgeNoCheckNoDeleteFiles(); - if(go) { - break; - } + ckanPackage.purgeNoCheckNoDeleteFiles(); }catch (Exception e) { - // TODO: handle exception + logger.error("Unable to purge record with name {}", name, e); } - Thread.sleep(500); } + Thread.sleep(500); }else { go=false; } } } - // @Test + @Test public void purgeAllGRSFRecords() throws Exception { purgeGRSFRecords(100, 0); } From 3ec771527c52520b262c5ef8ef126f185d227282 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 24 May 2022 17:35:48 +0200 Subject: [PATCH 05/10] Moved specific code for GRSF to the dedicated test class --- .../gcat/persistence/ckan/CKANPackage.java | 28 +--- .../gcat/persistence/grsf/GRSFUtilities.java | 127 +++++++++++++++++- 2 files changed, 122 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java index bffb802..b221991 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java @@ -67,9 +67,9 @@ public class CKANPackage extends CKAN implements Moderated { // limit in https://docs.ckan.org/en/latest/api/index.html#ckan.logic.action.get.package_search - protected static final String ROWS_KEY = "rows"; + public static final String ROWS_KEY = "rows"; // offset in https://docs.ckan.org/en/latest/api/index.html#ckan.logic.action.get.package_search - protected static final String START_KEY = "start"; + public static final String START_KEY = "start"; protected static final String ORGANIZATION_FILTER_TEMPLATE = GCatConstants.ORGANIZATION_PARAMETER + ":%s"; @@ -504,30 +504,6 @@ public class CKANPackage extends CKAN implements Moderated { return getAsString(arrayNode); } - public String listNoCheck(int limit, int offset) { - Map parameters = new HashMap<>(); - if(limit <= 0) { - // According to CKAN documentation - // the number of matching rows to return. There is a hard limit of 1000 datasets per query. - // see https://docs.ckan.org/en/2.6/api/index.html#ckan.logic.action.get.package_search - limit = 1000; - } - parameters.put(ROWS_KEY, String.valueOf(limit)); - - if(offset < 0) { - offset = 0; - } - parameters.put(START_KEY, String.valueOf(offset)); - - // This filter by type -// StringWriter qStringWriter = new StringWriter(); -// qStringWriter.append("extras_systemtype:"); -// qStringWriter.append(""); -// parameters.put(GCatConstants.Q_KEY, qStringWriter.toString()); - - return list(parameters); - } - @Override public String list(int limit, int offset) { Map parameters = getListingParameters(limit, offset); diff --git a/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java b/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java index efc325b..4ffb42b 100644 --- a/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java +++ b/src/test/java/org/gcube/gcat/persistence/grsf/GRSFUtilities.java @@ -1,11 +1,18 @@ package org.gcube.gcat.persistence.grsf; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; @@ -18,7 +25,9 @@ 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.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.scope.impl.ScopeBean; import org.gcube.gcat.ContextTest; +import org.gcube.gcat.api.GCatConstants; import org.gcube.gcat.persistence.ckan.CKANGroup; import org.gcube.gcat.persistence.ckan.CKANGroupTest; import org.gcube.gcat.persistence.ckan.CKANOrganization; @@ -49,13 +58,11 @@ public class GRSFUtilities extends ContextTest { @Before public void before() throws Exception { // ContextTest.setContextByName(PREPROD_GRSF_PRE); - - ContextTest.setContextByName(PRODUCTION_GRSF); +// ContextTest.setContextByName(PRODUCTION_GRSF); // ContextTest.setContextByName(PRODUCTION_GRSF_ADMIN); // ContextTest.setContextByName(PRODUCTION_GRSF_PRE); - - logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); - logger.debug(SecretManagerProvider.instance.get().getContext()); +// logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); +// logger.debug(SecretManagerProvider.instance.get().getContext()); } private void create(Set createdGroup, Map groups, String name) throws JsonProcessingException, IOException { @@ -254,6 +261,111 @@ public class GRSFUtilities extends ContextTest { } + protected void printLine(File file, String line) throws Exception { + try (FileWriter fw = new FileWriter(file, true); + BufferedWriter bw = new BufferedWriter(fw); + PrintWriter out = new PrintWriter(bw)) { + out.println(line); + out.flush(); + } catch (IOException e) { + throw e; + } + } + + + public Map getListParameter(int limit, int offset, String type, String org) { + Map parameters = new HashMap<>(); + if(limit <= 0) { + // According to CKAN documentation + // the number of matching rows to return. There is a hard limit of 1000 datasets per query. + // see https://docs.ckan.org/en/2.6/api/index.html#ckan.logic.action.get.package_search + limit = 1000; + } + parameters.put(CKANPackage.ROWS_KEY, String.valueOf(limit)); + + if(offset < 0) { + offset = 0; + } + parameters.put(CKANPackage.START_KEY, String.valueOf(offset)); + + String q = null; + + if(type!=null) { + // This filter by type + StringWriter qStringWriter = new StringWriter(); + qStringWriter.append("extras_systemtype:"); + qStringWriter.append(type); + q = qStringWriter.toString(); + + } + + if(org!=null) { + StringWriter qStringWriter = new StringWriter(); + qStringWriter.append("organization:"); + qStringWriter.append(org); + if(q!=null) { + q = q + " AND " + qStringWriter.toString(); + } + } + + if(q!=null) { + q = q + " AND (StatusoftheRecord:Approved OR StatusoftheRecord:Archived)"; + // q = q + " AND (groups:stock-group)"; + } + + + if(q!=null) { + parameters.put(GCatConstants.Q_KEY, q); + } + + + return parameters; + } + + // @Test + public void list() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + int limit = 100; + // String[] contexts = new String[] {PRODUCTION_GRSF, PRODUCTION_GRSF_ADMIN}; + String[] contexts = new String[] {PRODUCTION_GRSF}; + for(String context : contexts) { + ContextTest.setContextByName(context); + logger.debug(SecretManagerProvider.instance.get().getUser().getUsername()); + logger.debug(SecretManagerProvider.instance.get().getContext()); + CKANPackage ckanPackage = new CKANPackage(); + SortedSet sortedSet = new TreeSet<>(); + String[] types = new String[] {"Marine Resource", "Assessment Unit"}; + for(String type : types) { + int offset = 0; + boolean go = true; + ScopeBean scopeBean = new ScopeBean(context); + while(go) { + Map parameters = getListParameter(limit, offset==0 ? 0 : (offset*limit), type, scopeBean.name().toLowerCase()); + String ret = ckanPackage.list(parameters); + 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(); + sortedSet.add(name); + } + offset++; + }else { + go=false; + } + } + File file = new File(scopeBean.name() + "-" + type.replace(" ", "_")+".txt"); + if(file.exists()) { + file.delete(); + } + for(String name : sortedSet) { + printLine(file, name); + } + } + } + } + private void purgeGRSFRecords(int limit, int offset) throws Exception { logger.debug("Going to purge {} records, starting from {}", limit, limit*offset); CKANPackage ckanPackage = new CKANPackage(); @@ -263,7 +375,8 @@ public class GRSFUtilities extends ContextTest { ObjectMapper mapper = new ObjectMapper(); boolean go = true; while(go) { - String ret = ckanPackage.listNoCheck(limit, offset); + Map parameters = getListParameter(limit, offset, null, null); + String ret = ckanPackage.list(parameters); JsonNode gotList = mapper.readTree(ret); Assert.assertTrue(gotList instanceof ArrayNode); ArrayNode itemsArray = (ArrayNode) gotList; @@ -285,7 +398,7 @@ public class GRSFUtilities extends ContextTest { } } - @Test + // @Test public void purgeAllGRSFRecords() throws Exception { purgeGRSFRecords(100, 0); } From 48dbe09e3ecf74a2c2123b4f8cd7d8293723cceb Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 26 May 2022 10:53:27 +0200 Subject: [PATCH 06/10] Added social client dependency --- pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 769aba2..ffc3458 100644 --- a/pom.xml +++ b/pom.xml @@ -64,11 +64,11 @@ gcat-api [2.0.0,3.0.0-SNAPSHOT) - - - - - + + org.gcube.social-networking + social-service-client + 1.1.0-SNAPSHOT + org.gcube.common authorization-utils From bb60d40cd5f023a86a2c4c5cfe163b9f48e1323a Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 26 May 2022 11:34:42 +0200 Subject: [PATCH 07/10] Reorganizing packages to add notifications --- pom.xml | 6 ------ .../org/gcube/gcat/moderation/thread/ModerationThread.java | 2 +- .../thread/social/messages}/Message.java | 2 +- .../thread/social/messages}/SocialMessage.java | 2 +- .../{ => messages}/SocialMessageModerationThread.java | 5 ++--- 5 files changed, 5 insertions(+), 12 deletions(-) rename src/main/java/org/gcube/gcat/{social => moderation/thread/social/messages}/Message.java (96%) rename src/main/java/org/gcube/gcat/{social => moderation/thread/social/messages}/SocialMessage.java (98%) rename src/main/java/org/gcube/gcat/moderation/thread/social/{ => messages}/SocialMessageModerationThread.java (98%) diff --git a/pom.xml b/pom.xml index ffc3458..ed00129 100644 --- a/pom.xml +++ b/pom.xml @@ -228,12 +228,6 @@ ${enunciate.version} provided - - org.geotoolkit - geotk-xml-base - 3.20-geoapi-3.0 - provided - javax.servlet javax.servlet-api diff --git a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java index dd7831b..795c9f8 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java @@ -8,7 +8,7 @@ import org.gcube.common.authorization.utils.manager.SecretManager; import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.gcat.api.configuration.CatalogueConfiguration; import org.gcube.gcat.api.moderation.CMItemStatus; -import org.gcube.gcat.moderation.thread.social.SocialMessageModerationThread; +import org.gcube.gcat.moderation.thread.social.messages.SocialMessageModerationThread; import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP; diff --git a/src/main/java/org/gcube/gcat/social/Message.java b/src/main/java/org/gcube/gcat/moderation/thread/social/messages/Message.java similarity index 96% rename from src/main/java/org/gcube/gcat/social/Message.java rename to src/main/java/org/gcube/gcat/moderation/thread/social/messages/Message.java index a5daf87..326a2fc 100644 --- a/src/main/java/org/gcube/gcat/social/Message.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/social/messages/Message.java @@ -1,4 +1,4 @@ -package org.gcube.gcat.social; +package org.gcube.gcat.moderation.thread.social.messages; import java.util.ArrayList; import java.util.Collection; diff --git a/src/main/java/org/gcube/gcat/social/SocialMessage.java b/src/main/java/org/gcube/gcat/moderation/thread/social/messages/SocialMessage.java similarity index 98% rename from src/main/java/org/gcube/gcat/social/SocialMessage.java rename to src/main/java/org/gcube/gcat/moderation/thread/social/messages/SocialMessage.java index eaa7c4a..cec279c 100644 --- a/src/main/java/org/gcube/gcat/social/SocialMessage.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/social/messages/SocialMessage.java @@ -1,4 +1,4 @@ -package org.gcube.gcat.social; +package org.gcube.gcat.moderation.thread.social.messages; import java.net.HttpURLConnection; diff --git a/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/social/messages/SocialMessageModerationThread.java similarity index 98% rename from src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java rename to src/main/java/org/gcube/gcat/moderation/thread/social/messages/SocialMessageModerationThread.java index 92c1f01..2db8759 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/social/SocialMessageModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/social/messages/SocialMessageModerationThread.java @@ -1,4 +1,4 @@ -package org.gcube.gcat.moderation.thread.social; +package org.gcube.gcat.moderation.thread.social.messages; import java.io.StringWriter; import java.util.Set; @@ -9,8 +9,6 @@ import org.gcube.common.authorization.utils.secret.Secret; import org.gcube.gcat.api.moderation.CMItemStatus; import org.gcube.gcat.api.moderation.Moderated; import org.gcube.gcat.moderation.thread.ModerationThread; -import org.gcube.gcat.social.Message; -import org.gcube.gcat.social.SocialMessage; import org.gcube.gcat.social.SocialUsers; import org.gcube.gcat.utils.Constants; import org.slf4j.Logger; @@ -201,3 +199,4 @@ public class SocialMessageModerationThread extends ModerationThread { } } + \ No newline at end of file From 41a8011afb3b62c687315a88e3f6f26151011980 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 26 May 2022 13:35:33 +0200 Subject: [PATCH 08/10] Implementing notifications --- .../SocialNotificationModerationThread.java | 220 ++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java diff --git a/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java new file mode 100644 index 0000000..2dd9a8d --- /dev/null +++ b/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java @@ -0,0 +1,220 @@ +package org.gcube.gcat.moderation.thread.social.notifications; + +import java.io.StringWriter; +import java.net.URL; +import java.util.Set; + +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.Secret; +import org.gcube.gcat.api.moderation.CMItemStatus; +import org.gcube.gcat.api.moderation.Moderated; +import org.gcube.gcat.moderation.thread.ModerationThread; +import org.gcube.gcat.social.SocialUsers; +import org.gcube.gcat.utils.Constants; +import org.gcube.social_networking.social_networking_client_library.NotificationClient; +import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEvent; +import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEventType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class SocialNotificationModerationThread extends ModerationThread { + + private static final Logger logger = LoggerFactory.getLogger(SocialNotificationModerationThread.class); + + protected CatalogueEventType catalogueEventType; + + protected StringBuffer getMainItemInfo(StringBuffer stringBuffer) { + stringBuffer.append("Status: "); + stringBuffer.append(cmItemStatus.getFancyValue()); + stringBuffer.append("\n"); + stringBuffer.append("Title: "); + stringBuffer.append(itemTitle); + stringBuffer.append("\n"); + stringBuffer.append("Name: "); + stringBuffer.append(itemName); + stringBuffer.append("\n"); + stringBuffer.append("ID: "); + stringBuffer.append(itemID); + stringBuffer.append("\n"); + stringBuffer.append("URL: "); + if(cmItemStatus == CMItemStatus.APPROVED) { + stringBuffer.append(itemURL); + }else { + stringBuffer.append(getModerationURL()); + } + stringBuffer.append("\n\n"); + return stringBuffer; + } + + /** + * Create the message for an item that is created/updated + */ + public void notifyItemToBeManaged() throws Exception { + /* + * An example of created message is: + * + * [mister x] created / updated the following item + * + * Title: RESTful Transaction Model + * Name: my_first_restful_transaction_model + * ID: e31a6ba8-66ef-47b8-b61f-99a1366b4a69 + * URL: https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model + * + * You are kindly requested to review it and decide either to APPROVE or REJECT it at + * + */ + cmItemStatus = CMItemStatus.PENDING; + String fullName = ckanUser.getNameSurname(); + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append(fullName); + stringBuffer.append(create ? " created " : " updated "); + stringBuffer.append("the following item\n\n"); + stringBuffer = getMainItemInfo(stringBuffer); + stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT it."); + // stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT at"); + // String manageURL = getManageURL(); + // stringBuffer.append(manageURL); + postMessage(stringBuffer.toString()); + } + + + public void postItemCreated() throws Exception { + create = true; + cmItemStatus = CMItemStatus.PENDING; + catalogueEventType = CatalogueEventType.ITEM_SUBMITTED; + notifyItemToBeManaged(); + } + + public void postItemUpdated() throws Exception { + create = false; + cmItemStatus = CMItemStatus.PENDING; + catalogueEventType = CatalogueEventType.ITEM_UPDATED; + notifyItemToBeManaged(); + } + + protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) { + stringBuffer.append(fullName); + stringBuffer.append(" ["); + stringBuffer.append(role); + stringBuffer.append("] "); + return stringBuffer; + } + + public void postItemManaged(String userMessage) throws Exception { + create = false; + String fullName = ckanUser.getNameSurname(); + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); + stringBuffer.append(cmItemStatus.getValue()); + stringBuffer.append(" the following item"); + if(userMessage!=null && userMessage.length()>0) { + stringBuffer.append(" with this accompanying message\n\n"); + stringBuffer = getMainItemInfo(stringBuffer); + stringBuffer.append(userMessage); + }else { + stringBuffer.append("\n\n"); + stringBuffer = getMainItemInfo(stringBuffer); + } + postMessage(stringBuffer.toString()); + } + + @Override + public void postItemRejected(String userMessage) throws Exception { + create = false; + cmItemStatus = CMItemStatus.REJECTED; + postItemManaged(userMessage); + } + + @Override + public void postItemApproved(String userMessage) throws Exception { + create = false; + cmItemStatus = CMItemStatus.APPROVED; + postItemManaged(userMessage); + } + + protected String getSubject() { + StringWriter stringWriter = new StringWriter(); + if(!create) { + logger.trace("It's a reply"); + stringWriter.append("Re: "); + } + stringWriter.append("[Catalogue Service] "); + stringWriter.append(itemTitle); + return stringWriter.toString(); + } + + protected CatalogueEvent getCatalogueEvent(String messageString) throws Exception { + CatalogueEvent catalogueEvent = new CatalogueEvent(); + catalogueEvent.setItemId(itemID); + if(cmItemStatus == CMItemStatus.APPROVED) { + catalogueEvent.setItemURL(new URL(itemURL)); + }else { + catalogueEvent.setItemURL(new URL(getModerationURL())); + } + + catalogueEvent.setNotifyText(messageString); + + Set users = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR); + SecretManager secretManager = SecretManagerProvider.instance.get(); + String username = secretManager.getUser().getUsername(); + users.add(username); + + catalogueEvent.setIdsToNotify(users.toArray(new String[users.size()])); + + + + + + return catalogueEvent; + } + + @Override + protected void postMessage(String messageString) throws Exception { + CatalogueEvent catalogueEvent = getCatalogueEvent(messageString); + SecretManager secretManager = SecretManagerProvider.instance.get(); + Secret secret = Constants.getCatalogueSecret(); + secretManager.startSession(secret); + try { + sendNotification(catalogueEvent); + }finally { + secretManager.endSession(); + } + } + + @Override + public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception { + this.create = false; + this.cmItemStatus = cmItemStatus; + String fullName = ckanUser.getNameSurname(); + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer = addUserWithRole(fullName, itemAuthor ? "Author" : Moderated.CATALOGUE_MODERATOR, stringBuffer); + stringBuffer.append("sent a message regarding the following item\n\n"); + stringBuffer = getMainItemInfo(stringBuffer); + stringBuffer.append(userMessage); + CatalogueEvent catalogueEvent = getCatalogueEvent(stringBuffer.toString()); + SecretManager secretManager = SecretManagerProvider.instance.get(); + Secret secret = Constants.getCatalogueSecret(); + secretManager.startSession(secret); + try { + sendNotification(catalogueEvent); + }finally { + secretManager.endSession(); + } + } + + protected void sendNotification(CatalogueEvent catalogueEvent) throws Exception { + NotificationClient nc = new NotificationClient(); + nc.sendCatalogueEvent(catalogueEvent); + } + + @Override + protected void createModerationThread() throws Exception { + create = true; + cmItemStatus = CMItemStatus.PENDING; + } + +} From b88f72a6d2ed28a52103cc2b6c1350cab9082025 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 27 May 2022 12:14:07 +0200 Subject: [PATCH 09/10] Switching moderation communication to notification --- .../SocialNotificationModerationThread.java | 102 ++++++++++++------ .../gcube/gcat/persistence/ckan/CKANUser.java | 11 +- .../gcat/persistence/ckan/CKANUserTest.java | 11 ++ 3 files changed, 92 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java index 2dd9a8d..bc98e1c 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java @@ -10,6 +10,7 @@ import org.gcube.common.authorization.utils.secret.Secret; import org.gcube.gcat.api.moderation.CMItemStatus; import org.gcube.gcat.api.moderation.Moderated; import org.gcube.gcat.moderation.thread.ModerationThread; +import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.social.SocialUsers; import org.gcube.gcat.utils.Constants; import org.gcube.social_networking.social_networking_client_library.NotificationClient; @@ -53,31 +54,20 @@ public class SocialNotificationModerationThread extends ModerationThread { /** * Create the message for an item that is created/updated */ - public void notifyItemToBeManaged() throws Exception { + protected void notifyItemToBeManaged() throws Exception { /* * An example of created message is: * - * [mister x] created / updated the following item - * - * Title: RESTful Transaction Model - * Name: my_first_restful_transaction_model - * ID: e31a6ba8-66ef-47b8-b61f-99a1366b4a69 - * URL: https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model - * - * You are kindly requested to review it and decide either to APPROVE or REJECT it at + * [mister x] created /updated the item "[TITLE]". You are kindly requested to review it and decide either to APPROVE or REJECT it. [Go to catalogue] * */ - cmItemStatus = CMItemStatus.PENDING; String fullName = ckanUser.getNameSurname(); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(fullName); stringBuffer.append(create ? " created " : " updated "); - stringBuffer.append("the following item\n\n"); - stringBuffer = getMainItemInfo(stringBuffer); - stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT it."); - // stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT at"); - // String manageURL = getManageURL(); - // stringBuffer.append(manageURL); + stringBuffer.append("the item "); + stringBuffer.append(itemTitle); + stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT it. "); postMessage(stringBuffer.toString()); } @@ -105,19 +95,28 @@ public class SocialNotificationModerationThread extends ModerationThread { } public void postItemManaged(String userMessage) throws Exception { + /* + * [mister x] rejected the item "[TITLE]" with this accompanying message "[MESSAGE]". To resubmit it [Go to catalogue] + * + * [mister x] approved the item "[TITLE]" with this accompanying message "[MESSAGE]". [Go to catalogue] + */ create = false; String fullName = ckanUser.getNameSurname(); StringBuffer stringBuffer = new StringBuffer(); stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); stringBuffer.append(cmItemStatus.getValue()); - stringBuffer.append(" the following item"); + stringBuffer.append(" the item '"); + stringBuffer.append(itemTitle); + stringBuffer.append("'"); if(userMessage!=null && userMessage.length()>0) { - stringBuffer.append(" with this accompanying message\n\n"); - stringBuffer = getMainItemInfo(stringBuffer); + stringBuffer.append(" with this accompanying message '"); stringBuffer.append(userMessage); - }else { - stringBuffer.append("\n\n"); - stringBuffer = getMainItemInfo(stringBuffer); + stringBuffer.append("'"); + } + stringBuffer.append("."); + + if(cmItemStatus == CMItemStatus.REJECTED) { + stringBuffer.append(" To resubmit it "); } postMessage(stringBuffer.toString()); } @@ -126,6 +125,7 @@ public class SocialNotificationModerationThread extends ModerationThread { public void postItemRejected(String userMessage) throws Exception { create = false; cmItemStatus = CMItemStatus.REJECTED; + catalogueEventType = CatalogueEventType.ITEM_REJECTED; postItemManaged(userMessage); } @@ -133,23 +133,42 @@ public class SocialNotificationModerationThread extends ModerationThread { public void postItemApproved(String userMessage) throws Exception { create = false; cmItemStatus = CMItemStatus.APPROVED; + catalogueEventType = CatalogueEventType.ITEM_PUBLISHED; postItemManaged(userMessage); } protected String getSubject() { StringWriter stringWriter = new StringWriter(); - if(!create) { - logger.trace("It's a reply"); - stringWriter.append("Re: "); + if(catalogueEventType!=null) { + switch (catalogueEventType) { + case ITEM_SUBMITTED: + stringWriter.append("Submitted item '"); + break; + + case ITEM_UPDATED: + stringWriter.append("Updated item '"); + break; + + case ITEM_REJECTED: + case ITEM_PUBLISHED: + stringWriter.append(cmItemStatus.getFancyValue()); + stringWriter.append(" item '"); + break; + + default: + break; + } + }else { + stringWriter.append("Message for item '"); } - stringWriter.append("[Catalogue Service] "); stringWriter.append(itemTitle); + stringWriter.append("'"); return stringWriter.toString(); } protected CatalogueEvent getCatalogueEvent(String messageString) throws Exception { CatalogueEvent catalogueEvent = new CatalogueEvent(); - catalogueEvent.setItemId(itemID); + catalogueEvent.setItemId(getSubject()); if(cmItemStatus == CMItemStatus.APPROVED) { catalogueEvent.setItemURL(new URL(itemURL)); }else { @@ -159,15 +178,18 @@ public class SocialNotificationModerationThread extends ModerationThread { catalogueEvent.setNotifyText(messageString); Set users = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR); + SecretManager secretManager = SecretManagerProvider.instance.get(); String username = secretManager.getUser().getUsername(); + + // Adding the user is generating the event users.add(username); + // Adding item creator + users.add(CKANUser.getUsernameFromCKANUsername(ckanUser.getName())); + catalogueEvent.setIdsToNotify(users.toArray(new String[users.size()])); - - - - + catalogueEvent.setIdsAsGroup(false); return catalogueEvent; } @@ -189,6 +211,24 @@ public class SocialNotificationModerationThread extends ModerationThread { public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception { this.create = false; this.cmItemStatus = cmItemStatus; + + switch (cmItemStatus) { + case PENDING: + catalogueEventType = CatalogueEventType.ITEM_UPDATED; + break; + + case APPROVED: + catalogueEventType = CatalogueEventType.ITEM_PUBLISHED; + break; + + case REJECTED: + catalogueEventType = CatalogueEventType.ITEM_REJECTED; + break; + + default: + break; + } + String fullName = ckanUser.getNameSurname(); StringBuffer stringBuffer = new StringBuffer(); stringBuffer = addUserWithRole(fullName, itemAuthor ? "Author" : Moderated.CATALOGUE_MODERATOR, stringBuffer); diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java index b8c444f..39b24aa 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANUser.java @@ -199,11 +199,20 @@ public class CKANUser extends CKAN { } protected static String getCKANUsername(String username) { - if(username == null) + if(username == null) { return null; + } return username.trim().replaceAll("\\.", "_"); } + public static String getUsernameFromCKANUsername(String ckanUsername) { + if(ckanUsername == null) { + return null; + } + return ckanUsername.trim().replaceAll("_", "."); + } + + public static String getCKANUsername() { String username = SecretManagerProvider.instance.get().getUser().getUsername(); return getCKANUsername(username); diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANUserTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANUserTest.java index bd27472..1bd6dbd 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANUserTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANUserTest.java @@ -1,6 +1,7 @@ package org.gcube.gcat.persistence.ckan; import org.gcube.gcat.ContextTest; +import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,6 +23,16 @@ public class CKANUserTest extends ContextTest { return user; } + @Test + public void getUsernameFromCkanUsername() { + String username = "luca.frosini"; + String ckanUsername = CKANUser.getCKANUsername(username); + Assert.assertTrue(ckanUsername.compareTo("luca_frosini")==0); + String gotUsername = CKANUser.getUsernameFromCKANUsername(ckanUsername); + Assert.assertTrue(gotUsername.compareTo(username)==0); + } + + @Test public void list() throws Exception { CKANUser ckanUser = getCKANUser(); From 01d2e527a140db7f8a9eca822eaedf732da07416 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 27 May 2022 17:20:52 +0200 Subject: [PATCH 10/10] Switch to notification completed --- .../moderation/thread/ModerationThread.java | 5 +- .../SocialNotificationModerationThread.java | 157 +++++++++++------- .../gcat/moderation/ModerationThreadTest.java | 35 +++- src/test/resources/logback-test.xml | 3 +- 4 files changed, 131 insertions(+), 69 deletions(-) diff --git a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java index 795c9f8..e66881e 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/ModerationThread.java @@ -8,7 +8,7 @@ import org.gcube.common.authorization.utils.manager.SecretManager; import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.gcat.api.configuration.CatalogueConfiguration; import org.gcube.gcat.api.moderation.CMItemStatus; -import org.gcube.gcat.moderation.thread.social.messages.SocialMessageModerationThread; +import org.gcube.gcat.moderation.thread.social.notifications.SocialNotificationModerationThread; import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP; @@ -35,7 +35,8 @@ public abstract class ModerationThread { public static ModerationThread getDefaultInstance() { // return new FakeModerationThread(); - return new SocialMessageModerationThread(); + // return new SocialMessageModerationThread(); + return new SocialNotificationModerationThread(); } public ModerationThread() { diff --git a/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java b/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java index bc98e1c..b406b65 100644 --- a/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java +++ b/src/main/java/org/gcube/gcat/moderation/thread/social/notifications/SocialNotificationModerationThread.java @@ -1,6 +1,5 @@ package org.gcube.gcat.moderation.thread.social.notifications; -import java.io.StringWriter; import java.net.URL; import java.util.Set; @@ -26,29 +25,20 @@ public class SocialNotificationModerationThread extends ModerationThread { private static final Logger logger = LoggerFactory.getLogger(SocialNotificationModerationThread.class); - protected CatalogueEventType catalogueEventType; + public static final String AUTHOR = "Author"; - protected StringBuffer getMainItemInfo(StringBuffer stringBuffer) { - stringBuffer.append("Status: "); - stringBuffer.append(cmItemStatus.getFancyValue()); - stringBuffer.append("\n"); - stringBuffer.append("Title: "); - stringBuffer.append(itemTitle); - stringBuffer.append("\n"); - stringBuffer.append("Name: "); - stringBuffer.append(itemName); - stringBuffer.append("\n"); - stringBuffer.append("ID: "); - stringBuffer.append(itemID); - stringBuffer.append("\n"); - stringBuffer.append("URL: "); - if(cmItemStatus == CMItemStatus.APPROVED) { - stringBuffer.append(itemURL); - }else { - stringBuffer.append(getModerationURL()); - } - stringBuffer.append("\n\n"); - return stringBuffer; + protected CatalogueEventType catalogueEventType; + protected boolean comment; + + protected static final boolean notificationSentByGCat; + + static { + notificationSentByGCat = false; + } + + public SocialNotificationModerationThread() { + super(); + this.comment = false; } /** @@ -58,16 +48,18 @@ public class SocialNotificationModerationThread extends ModerationThread { /* * An example of created message is: * - * [mister x] created /updated the item "[TITLE]". You are kindly requested to review it and decide either to APPROVE or REJECT it. [Go to catalogue] + * [mister x] created/updated the item "[TITLE]". You are kindly requested to review it and decide either to APPROVE or REJECT it. [Go to catalogue] * */ String fullName = ckanUser.getNameSurname(); StringBuffer stringBuffer = new StringBuffer(); - stringBuffer.append(fullName); + if(notificationSentByGCat) { + stringBuffer.append(fullName); + } stringBuffer.append(create ? " created " : " updated "); stringBuffer.append("the item "); - stringBuffer.append(itemTitle); - stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT it. "); + stringBuffer = addQuotedTitle(stringBuffer); + stringBuffer.append(". You are kindly requested to review it and decide either to APPROVE or REJECT it. "); postMessage(stringBuffer.toString()); } @@ -86,14 +78,22 @@ public class SocialNotificationModerationThread extends ModerationThread { notifyItemToBeManaged(); } - protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) { - stringBuffer.append(fullName); - stringBuffer.append(" ["); - stringBuffer.append(role); - stringBuffer.append("] "); + protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer, boolean addUserFullName) { + if(addUserFullName) { + stringBuffer.append(fullName); + } + if(role!=null) { + stringBuffer.append(" ["); + stringBuffer.append(role); + stringBuffer.append("] "); + } return stringBuffer; } + protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) { + return addUserWithRole(fullName, role, stringBuffer, notificationSentByGCat); + } + public void postItemManaged(String userMessage) throws Exception { /* * [mister x] rejected the item "[TITLE]" with this accompanying message "[MESSAGE]". To resubmit it [Go to catalogue] @@ -105,13 +105,12 @@ public class SocialNotificationModerationThread extends ModerationThread { StringBuffer stringBuffer = new StringBuffer(); stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer); stringBuffer.append(cmItemStatus.getValue()); - stringBuffer.append(" the item '"); - stringBuffer.append(itemTitle); - stringBuffer.append("'"); + stringBuffer.append(" the item "); + stringBuffer = addQuotedTitle(stringBuffer); if(userMessage!=null && userMessage.length()>0) { - stringBuffer.append(" with this accompanying message '"); + stringBuffer.append(" with this accompanying message \""); stringBuffer.append(userMessage); - stringBuffer.append("'"); + stringBuffer.append("\""); } stringBuffer.append("."); @@ -137,37 +136,54 @@ public class SocialNotificationModerationThread extends ModerationThread { postItemManaged(userMessage); } + protected StringBuffer addQuotedTitle(StringBuffer stringBuffer, String quotingCharacter) { + stringBuffer.append(quotingCharacter); + stringBuffer.append(itemTitle); + stringBuffer.append(quotingCharacter); + return stringBuffer; + } + + protected StringBuffer addQuotedTitle(StringBuffer stringBuffer) { + return addQuotedTitle(stringBuffer, "\""); + } + protected String getSubject() { - StringWriter stringWriter = new StringWriter(); - if(catalogueEventType!=null) { + StringBuffer stringBuffer = new StringBuffer(); + String fullName = ckanUser.getNameSurname(); + if(!comment) { switch (catalogueEventType) { case ITEM_SUBMITTED: - stringWriter.append("Submitted item '"); + stringBuffer.append(fullName); + stringBuffer.append(" created the item "); break; case ITEM_UPDATED: - stringWriter.append("Updated item '"); + stringBuffer.append(fullName); + stringBuffer.append(" updated the item "); break; case ITEM_REJECTED: case ITEM_PUBLISHED: - stringWriter.append(cmItemStatus.getFancyValue()); - stringWriter.append(" item '"); + addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer, true); + stringBuffer.append(cmItemStatus.getValue()); + stringBuffer.append(" the item "); break; default: break; } }else { - stringWriter.append("Message for item '"); + addUserWithRole(fullName, itemAuthor ? SocialNotificationModerationThread.AUTHOR : Moderated.CATALOGUE_MODERATOR, stringBuffer, true); + stringBuffer.append("commented on the item "); } - stringWriter.append(itemTitle); - stringWriter.append("'"); - return stringWriter.toString(); + stringBuffer = addQuotedTitle(stringBuffer); + return stringBuffer.toString(); } protected CatalogueEvent getCatalogueEvent(String messageString) throws Exception { CatalogueEvent catalogueEvent = new CatalogueEvent(); + catalogueEvent.setType(catalogueEventType); + catalogueEvent.setNotifyText(messageString); catalogueEvent.setItemId(getSubject()); if(cmItemStatus == CMItemStatus.APPROVED) { catalogueEvent.setItemURL(new URL(itemURL)); @@ -175,16 +191,13 @@ public class SocialNotificationModerationThread extends ModerationThread { catalogueEvent.setItemURL(new URL(getModerationURL())); } - catalogueEvent.setNotifyText(messageString); - Set users = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR); + users.add("luca.frosini"); SecretManager secretManager = SecretManagerProvider.instance.get(); String username = secretManager.getUser().getUsername(); - // Adding the user is generating the event users.add(username); - // Adding item creator users.add(CKANUser.getUsernameFromCKANUsername(ckanUser.getName())); @@ -199,18 +212,26 @@ public class SocialNotificationModerationThread extends ModerationThread { CatalogueEvent catalogueEvent = getCatalogueEvent(messageString); SecretManager secretManager = SecretManagerProvider.instance.get(); Secret secret = Constants.getCatalogueSecret(); - secretManager.startSession(secret); + if(notificationSentByGCat) { + secretManager.startSession(secret); + } try { sendNotification(catalogueEvent); }finally { - secretManager.endSession(); + if(notificationSentByGCat) { + secretManager.endSession(); + } } } @Override public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception { + /* + * [mister x] ([Role]) commented on the item "[TITLE]" as follows "[MESSAGE]". [Go to catalogue] + */ this.create = false; this.cmItemStatus = cmItemStatus; + this.comment = true; switch (cmItemStatus) { case PENDING: @@ -231,24 +252,40 @@ public class SocialNotificationModerationThread extends ModerationThread { String fullName = ckanUser.getNameSurname(); StringBuffer stringBuffer = new StringBuffer(); - stringBuffer = addUserWithRole(fullName, itemAuthor ? "Author" : Moderated.CATALOGUE_MODERATOR, stringBuffer); - stringBuffer.append("sent a message regarding the following item\n\n"); - stringBuffer = getMainItemInfo(stringBuffer); + stringBuffer = addUserWithRole(fullName, itemAuthor ? SocialNotificationModerationThread.AUTHOR : Moderated.CATALOGUE_MODERATOR, stringBuffer); + stringBuffer.append("commented on the item "); + stringBuffer = addQuotedTitle(stringBuffer); + stringBuffer.append(" as follows \""); stringBuffer.append(userMessage); + stringBuffer.append("\"."); CatalogueEvent catalogueEvent = getCatalogueEvent(stringBuffer.toString()); SecretManager secretManager = SecretManagerProvider.instance.get(); Secret secret = Constants.getCatalogueSecret(); - secretManager.startSession(secret); + if(notificationSentByGCat) { + secretManager.startSession(secret); + } try { sendNotification(catalogueEvent); }finally { - secretManager.endSession(); + if(notificationSentByGCat) { + secretManager.endSession(); + } } } protected void sendNotification(CatalogueEvent catalogueEvent) throws Exception { - NotificationClient nc = new NotificationClient(); - nc.sendCatalogueEvent(catalogueEvent); + Thread thread = new Thread() { + public void run() { + try { + NotificationClient nc = new NotificationClient(); + nc.sendCatalogueEvent(catalogueEvent); + } catch(Exception e) { + logger.error("Error while sending notification.", e); + } + } + }; + thread.run(); + // thread.start(); } @Override diff --git a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java index 1b95f88..ba5ea18 100644 --- a/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java +++ b/src/test/java/org/gcube/gcat/moderation/ModerationThreadTest.java @@ -4,7 +4,6 @@ import org.gcube.gcat.ContextTest; import org.gcube.gcat.api.moderation.CMItemStatus; import org.gcube.gcat.moderation.thread.ModerationThread; import org.gcube.gcat.persistence.ckan.CKANUser; -import org.junit.Ignore; import org.junit.Test; /** @@ -12,23 +11,47 @@ import org.junit.Test; */ public class ModerationThreadTest extends ContextTest { - @Test - @Ignore - public void test() throws Exception { + protected ModerationThread getModerationThread(CKANUser ckanUser) { ModerationThread moderationThread = ModerationThread.getDefaultInstance(); - moderationThread.setItemCoordinates("e31a6ba8-66ef-47b8-b61f-99a1366b4a69", "my_first_restful_transaction_model", "RESTful Transaction Model", "https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model"); + moderationThread.setItemCoordinates("b1040e70-774f-47b6-95e9-f24efca50caf", "my_first_restful_transaction_model", "RESTful Transaction Model", "https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model"); + moderationThread.setCKANUser(ckanUser); + return moderationThread; + } + + @Test + // @Ignore + public void testModerationThread() throws Exception { + ContextTest.setContextByName("pasquale.pagano_/gcube/devsec/devVRE"); + CKANUser ckanUser = new CKANUser(); ckanUser.setName(CKANUser.getCKANUsername()); ckanUser.read(); - moderationThread.setCKANUser(ckanUser); + + ModerationThread moderationThread = getModerationThread(ckanUser); moderationThread.postItemCreated(); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemUpdated(); + + moderationThread = getModerationThread(ckanUser); + moderationThread.postUserMessage(CMItemStatus.PENDING, "Pensaci Bene"); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemRejected(null); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemRejected("reject con messaggio: Non mi garba"); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemApproved(null); + + moderationThread = getModerationThread(ckanUser); moderationThread.postItemApproved("approve con messaggio: Ora mi garba"); + + moderationThread = getModerationThread(ckanUser); moderationThread.setItemAuthor(true); moderationThread.postUserMessage(CMItemStatus.APPROVED, "Grazie"); + Thread.sleep(1000); } diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index 2e2f160..4c2b21d 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -10,8 +10,9 @@ - + +