From 3e0e9252ceb9f69b2833b1a89fce47b3dbb5566b Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 28 Jul 2022 15:22:14 +0200 Subject: [PATCH 1/4] #23715 integrated the `all_fields` parameter --- CHANGELOG.md | 7 +++ pom.xml | 7 +-- .../utillibrary/gcat/GCatCaller.java | 29 +++++----- .../utillibrary/gcat/GCatCallerUtil.java | 34 ++++++----- .../cms/CatalogueContentModeratorSystem.java | 16 +++-- .../server/cms/DataCatalogueCMSImpl.java | 58 ++++++++++++------- src/test/.gitignore | 1 + 7 files changed, 94 insertions(+), 58 deletions(-) create mode 100644 src/test/.gitignore diff --git a/CHANGELOG.md b/CHANGELOG.md index ee6d92f..c857bbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v1.2.0-SNAPSHOT] - 2022-07-28 + +**Enhancements** + +- [#23692] Optimized the listing and the paging of catalogue items +- Moved to maven-portal-bom 3.7.0-SNAPSHOT + ## [v1.1.0] - 2022-05-18 **New Features** diff --git a/pom.xml b/pom.xml index 377b23d..6971770 100644 --- a/pom.xml +++ b/pom.xml @@ -12,8 +12,7 @@ org.gcube.datacatalogue catalogue-util-library - 1.1.0 - + 1.2.0-SNAPSHOT Ckan utility library Utility library to retrieve users information, organizations information and so on from the ckan d4science datacatalogue @@ -42,7 +41,7 @@ org.gcube.distribution maven-portal-bom - 3.6.3 + 3.7.0-SNAPSHOT pom import @@ -101,7 +100,7 @@ org.gcube.data-catalogue gcat-client - [2.0.0-SNAPSHOT, 3.0.0) + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) compile diff --git a/src/main/java/org/gcube/datacatalogue/utillibrary/gcat/GCatCaller.java b/src/main/java/org/gcube/datacatalogue/utillibrary/gcat/GCatCaller.java index f2cd002..6179bde 100644 --- a/src/main/java/org/gcube/datacatalogue/utillibrary/gcat/GCatCaller.java +++ b/src/main/java/org/gcube/datacatalogue/utillibrary/gcat/GCatCaller.java @@ -34,11 +34,11 @@ public class GCatCaller { private static final Logger LOG = LoggerFactory.getLogger(GCatCaller.class); - public final static String MODERATOR_ITEM_STATUS_PARAMETER = Moderated.CM_ITEM_STATUS_QUERY_PARAMETER; - - public final static String MODERATOR_ITEM_ANY_STATUS_VALUE = CMItemStatus.ANY.getValue(); - - public final static String DEFAULT_SORT_VALUE = "name asc"; + public final static String MODERATOR_ITEM_STATUS_PARAMETER = Moderated.CM_ITEM_STATUS_QUERY_PARAMETER; + + public final static String MODERATOR_ITEM_ANY_STATUS_VALUE = CMItemStatus.ANY.getValue(); + + public final static String DEFAULT_SORT_VALUE = "name asc"; /** * Instantiates a new g cat caller. @@ -223,22 +223,24 @@ public class GCatCaller { /** * Gets the list items for CM status. * - * @param status the status - * @param limit the limit - * @param offset the offset - * @param filters the filters + * @param status the status + * @param limit the limit + * @param offset the offset + * @param allFields the all fields. If true returns the all fields of an item + * @param filters the filters * @param sortForField the sort for field * @return the list items for CM status * @throws WebServiceException the web service exception * @throws MalformedURLException the malformed URL exception */ - public String getListItemsForCMStatus(CMItemStatus status, int limit, int offset, Map filters, String sortForField) - throws WebServiceException, MalformedURLException { + public String getListItemsForCMStatus(CMItemStatus status, int limit, int offset, Boolean allFields, + Map filters, String sortForField) throws WebServiceException, MalformedURLException { LOG.trace("getListItemsForCMStatus called"); LOG.info("called getListItemsForCMStatus called with [status: " + status + "], [limit: " + limit + "], [offset: " + offset + "], [filters: " + filters + "]"); - Map queryParams = GCatCallerUtil.genericQueryBuilderFor(status, limit, offset, false, filters, sortForField); + Map queryParams = GCatCallerUtil.genericQueryBuilderFor(status, limit, offset, false, allFields, filters, + sortForField); return getListItemsForQuery(queryParams); } @@ -257,7 +259,8 @@ public class GCatCaller { LOG.trace("countListItemsForCMStatus called"); LOG.info("Calling count list items for [status: " + status + "], [filters: " + filters + "]"); - Map queryParams = GCatCallerUtil.genericQueryBuilderFor(status, null, null, true, filters, null); + Map queryParams = GCatCallerUtil.genericQueryBuilderFor(status, null, null, true, false, + filters, null); String theCount = getListItemsForQuery(queryParams); int count = 0; diff --git a/src/main/java/org/gcube/datacatalogue/utillibrary/gcat/GCatCallerUtil.java b/src/main/java/org/gcube/datacatalogue/utillibrary/gcat/GCatCallerUtil.java index b7ad6fc..d434df0 100644 --- a/src/main/java/org/gcube/datacatalogue/utillibrary/gcat/GCatCallerUtil.java +++ b/src/main/java/org/gcube/datacatalogue/utillibrary/gcat/GCatCallerUtil.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.gcube.gcat.api.GCatConstants; import org.gcube.gcat.api.moderation.CMItemStatus; import org.json.simple.JSONArray; import org.json.simple.parser.JSONParser; @@ -27,17 +28,19 @@ public class GCatCallerUtil { /** * Generic query builder for. * - * @param status the status - * @param limit the limit - * @param offset the offset - * @param count the count - * @param filters the filters + * @param status the status + * @param limit the limit + * @param offset the offset + * @param count the count. If true returns only the count + * @param allFields the all fields. If true returns the all fields of an item + * @param filters the filters + * @param sort the sort * @return the map */ public static Map genericQueryBuilderFor(CMItemStatus status, Integer limit, Integer offset, - Boolean count, Map filters, String sort) { + Boolean count, Boolean allFields, Map filters, String sort) { LOG.info("genericQueryBuilderFor called with [status: " + status + "], [limit: " + limit + "], [offset: " - + offset + "], [count: " + count + "], [filters: " + filters + "]"); + + offset + "], [count: " + count + "], [allFields: " + allFields + "], [filters: " + filters + "]"); Map queryParams = new HashMap(); @@ -62,24 +65,29 @@ public class GCatCallerUtil { if (limit != null) { limit = limit < 0 ? 10 : limit; - queryParams.put("limit", limit + ""); + queryParams.put(GCatConstants.LIMIT_QUERY_PARAMETER, String.valueOf(limit)); } if (offset != null) { offset = offset < 0 ? 0 : offset; - queryParams.put("offset", offset + ""); + queryParams.put(GCatConstants.OFFSET_QUERY_PARAMETER, String.valueOf(offset)); } if (sort != null) { queryParams.put("sort", sort); - }else { - LOG.info("Adding defautl sort: "+GCatCaller.DEFAULT_SORT_VALUE); + } else { + LOG.info("Adding defautl sort: " + GCatCaller.DEFAULT_SORT_VALUE); queryParams.put("sort", GCatCaller.DEFAULT_SORT_VALUE); } - if (count != null) { if (count) { - queryParams.put("count", "true"); + queryParams.put(GCatConstants.COUNT_QUERY_PARAMETER, String.valueOf(count)); + } + } + + if (allFields != null) { + if (allFields) { + queryParams.put(GCatConstants.ALL_FIELDS_QUERY_PARAMETER, String.valueOf(allFields)); } } diff --git a/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/CatalogueContentModeratorSystem.java b/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/CatalogueContentModeratorSystem.java index 8d00881..0af1cd8 100644 --- a/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/CatalogueContentModeratorSystem.java +++ b/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/CatalogueContentModeratorSystem.java @@ -32,18 +32,20 @@ public interface CatalogueContentModeratorSystem { /** * Gets the list items for status. * - * @param theStatus the the status - * @param limit the limit - * @param offset the offset - * @param filters add the input filters to query on CKAN + * @param theStatus the the status + * @param limit the limit + * @param offset the offset + * @param allFields the all fields. If true returns the all fields of an item + * @param filters add the input filters to query on CKAN * @param sortForField the sort for field * @return the list items for status * @throws WebApplicationException the web application exception * @throws MalformedURLException the malformed URL exception * @throws InvalidObjectException the invalid object exception */ - public List getListItemsForStatus(ItemStatus theStatus, int limit, int offset, - Map filters, String sortForField) throws WebApplicationException, MalformedURLException, InvalidObjectException; + List getListItemsForStatus(ItemStatus theStatus, int limit, int offset, Boolean allFields, + Map filters, String sortForField) + throws WebApplicationException, MalformedURLException, InvalidObjectException; /** * Reject item. @@ -85,4 +87,6 @@ public interface CatalogueContentModeratorSystem { */ void approveItem(String itemName, String moderatorMessage) throws WebApplicationException, MalformedURLException; + + } diff --git a/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/DataCatalogueCMSImpl.java b/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/DataCatalogueCMSImpl.java index c5ba458..10f419c 100644 --- a/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/DataCatalogueCMSImpl.java +++ b/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/DataCatalogueCMSImpl.java @@ -115,10 +115,11 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem { /** * Gets the list items for status. * - * @param theStatus the the status - * @param limit the limit - * @param offset the offset - * @param filters add the input filters to query on CKAN + * @param theStatus the the status + * @param limit the limit + * @param offset the offset + * @param allFields the all fields. If true returns the all fields of an item + * @param filters add the input filters to query on CKAN * @param sortForField the sort for field * @return the list items for status * @throws WebApplicationException the web application exception @@ -126,30 +127,40 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem { * @throws InvalidObjectException the invalid object exception */ @Override - public List getListItemsForStatus(ItemStatus theStatus, int limit, int offset, - Map filters, String sortForField) throws WebApplicationException, MalformedURLException, InvalidObjectException { + public List getListItemsForStatus(ItemStatus theStatus, int limit, int offset, Boolean allFields, + Map filters, String sortForField) + throws WebApplicationException, MalformedURLException, InvalidObjectException { LOG.info("called getListItemsForStatus with [status: " + theStatus + "], [limit: " + limit + "], [offset: " - + offset + "], [filters: " + filters + "]"); + + offset + "], [allFields: " + allFields + "], [filters: " + filters + "]"); List listDataset = null; checkNotNull(theStatus); - JSONArray jsonArray = getSourceArrayOfItemsForStatus(theStatus, limit, offset, filters, sortForField); + JSONArray jsonArray = getSourceArrayOfItemsForStatus(theStatus, limit, offset, allFields, filters, sortForField); if (jsonArray != null) { int size = jsonArray.size(); listDataset = new ArrayList(size); LOG.info("reading and converting " + size + " dataset..."); for (int i = 0; i < size; i++) { - String datasetName = null; + String jsonValueDataset = null; try { - datasetName = (String) jsonArray.get(i); - LOG.debug("reading dataset: " + datasetName); - String jsonValueDataset = gCatCaller.getDatasetForName(datasetName); - LOG.trace("the JSON dataset is: " + jsonValueDataset); - CkanDataset toCkanDataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset, METHOD.TO_READ); + CkanDataset toCkanDataset = null; + if(!allFields) { + //here the array contains the list of dataset name + String datasetName = (String) jsonArray.get(i); + LOG.debug("going to read from gCat the dataset: " + datasetName); + jsonValueDataset = gCatCaller.getDatasetForName(datasetName); + LOG.trace("the JSON dataset is: " + jsonValueDataset); + }else { + LOG.debug("reading from the array the "+i+"mo json object"); + //here the array contains the list of dataset json fields + jsonValueDataset = (String) jsonArray.get(i); + LOG.trace("the JSON dataset is: " + jsonValueDataset); + } + toCkanDataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset, METHOD.TO_READ); LOG.debug("converted as dataset: " + toCkanDataset); listDataset.add(toCkanDataset); } catch (IOException e) { - LOG.warn("Error on reading/converting the dataset name: " + datasetName, e); + LOG.warn("Error on reading/converting the dataset: " + jsonValueDataset, e); } } } @@ -190,10 +201,11 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem { /** * Gets the source array of items for status read by gCatClient. * - * @param theStatus the the status - * @param limit the limit - * @param offset the offset - * @param filters the filters + * @param theStatus the the status + * @param limit the limit + * @param offset the offset + * @param allFields the all fields. If true returns the all fields of an item + * @param filters the filters * @param sortForField the sort for field * @return the source array of items for status * @throws WebApplicationException the web application exception @@ -201,9 +213,10 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem { * @throws InvalidObjectException the invalid object exception */ protected org.json.simple.JSONArray getSourceArrayOfItemsForStatus(ItemStatus theStatus, int limit, int offset, - Map filters, String sortForField) throws WebApplicationException, MalformedURLException, InvalidObjectException { + Boolean allFields, Map filters, String sortForField) + throws WebApplicationException, MalformedURLException, InvalidObjectException { LOG.info("called getSourceArrayOfItemsForStatus with [status: " + theStatus + "], [limit: " + limit - + "], [offset: " + offset + "], [filters: " + filters + "]"); + + "], [offset: " + offset + "], [allFields: " + allFields + "], [filters: " + filters + "]"); checkNotNull(theStatus); // TODO MUST BE CHANGED FOR THE STATUS org.json.simple.JSONArray jsonArray = null; @@ -216,7 +229,8 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem { */ CMItemStatus cmiStatus = toCMStatus(theStatus); - String datasetNames = gCatCaller.getListItemsForCMStatus(cmiStatus, limit, offset, filters, sortForField); + String datasetNames = gCatCaller.getListItemsForCMStatus(cmiStatus, limit, offset, allFields, filters, + sortForField); if (datasetNames != null) { LOG.debug("for status " + theStatus + " found dataset: " + datasetNames); diff --git a/src/test/.gitignore b/src/test/.gitignore new file mode 100644 index 0000000..cb6eb2c --- /dev/null +++ b/src/test/.gitignore @@ -0,0 +1 @@ +/resources/ From e0eff32f80f2f202059efa17456d5b930c2e5c79 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 28 Jul 2022 15:25:17 +0200 Subject: [PATCH 2/4] fixing compilation issue --- .../test/TestDataCatalogueCMS.java | 76 ++++++++++++------- 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java index cf78fbf..63ba762 100644 --- a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java +++ b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java @@ -13,8 +13,7 @@ import org.gcube.datacatalogue.utillibrary.server.DataCatalogueImpl; import org.gcube.datacatalogue.utillibrary.server.cms.CatalogueContentModeratorSystem; import org.gcube.datacatalogue.utillibrary.shared.ItemStatus; import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset; -import org.junit.Before; -import org.junit.Test; +import org.gcube.gcat.client.Item; import org.slf4j.LoggerFactory; /** @@ -27,20 +26,38 @@ public class TestDataCatalogueCMS { private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TestDataCatalogueCMS.class); private DataCatalogueFactory factory; - private String scope = "/gcube/devsec/devVRE"; private String testUser = "francesco.mangiacrapa"; - private String authorizationToken = ""; + private String scope = "/gcube/devsec/devVRE"; + //private String scope = "/gcube/devsec"; + private String authorizationToken = ""; //devVRE /** * Before. * * @throws Exception the exception */ - @Before + //@Before public void before() throws Exception { factory = DataCatalogueFactory.getFactory(); } + //@Test + public void testGCatAvailability() { + try { + LOG.info("testGCatAvailability running"); + + int offset = 0; + int limit = 10; + ScopeProvider.instance.set(scope); + SecurityTokenProvider.instance.set(authorizationToken); + LOG.debug("Trying with scope: "+scope); + String items = new Item().list(limit, offset); + LOG.debug("List items: " + items); + } catch (Exception e) { + e.printStackTrace(); + } + } + /** * Factory test. * @@ -58,27 +75,34 @@ public class TestDataCatalogueCMS { // @Test public void listItemsForCMStatus() throws Exception { - ItemStatus theStatus = ItemStatus.PENDING; - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); - DataCatalogueImpl dImpl = factory.getUtilsPerScope(scope); - CatalogueContentModeratorSystem cCMS = dImpl.getCatalogueContentModerator(); - LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty"); + try { + ItemStatus theStatus = ItemStatus.PENDING; - List emailsAddresses = new ArrayList(); - // emailsAddresses.add("luca.frosini@isti.cnr.it"); - emailsAddresses.add("francesco.mangiacrapa@isti.cnr.it"); - // emailsAddresses.add("pagano@cnr.it"); + SecurityTokenProvider.instance.set(authorizationToken); + DataCatalogueImpl dImpl = factory.getUtilsPerScope(scope); + CatalogueContentModeratorSystem cCMS = dImpl.getCatalogueContentModerator(); + LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty"); - String theQuery = mockQueryForEmails(emailsAddresses, "OR"); + List emailsAddresses = new ArrayList(); + // emailsAddresses.add("luca.frosini@isti.cnr.it"); + emailsAddresses.add("francesco.mangiacrapa@isti.cnr.it"); + // emailsAddresses.add("pagano@cnr.it"); - Map filters = new HashMap(); - filters.put("author_email", theQuery); + String theQuery = mockQueryForEmails(emailsAddresses, "OR"); - List listItems = cCMS.getListItemsForStatus(theStatus, 10, 0, filters, GCatCaller.DEFAULT_SORT_VALUE); - int i = 0; - for (CkanDataset ckanDataset : listItems) { - System.out.println(++i + ") item returned: " + ckanDataset); + Map filters = new HashMap(); + filters.put("author_email", theQuery); + + ScopeProvider.instance.set(scope); + SecurityTokenProvider.instance.set(authorizationToken); + List listItems = cCMS.getListItemsForStatus(theStatus, 10, 0, true, filters, + GCatCaller.DEFAULT_SORT_VALUE); + int i = 0; + for (CkanDataset ckanDataset : listItems) { + System.out.println(++i + ") item returned: " + ckanDataset); + } + } catch (Exception e) { + e.printStackTrace(); } } @@ -89,7 +113,7 @@ public class TestDataCatalogueCMS { * @return the scope per url * @throws Exception */ - @Test + // @Test public void countListItemsForStatus() throws Exception { try { @@ -101,9 +125,9 @@ public class TestDataCatalogueCMS { LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty"); List emailsAddresses = new ArrayList(); - //emailsAddresses.add("luca.frosini@isti.cnr.it"); - //emailsAddresses.add("francesco.mangiacrapa@isti.cnr.it"); - //emailsAddresses.add("pagano@cnr.it"); + // emailsAddresses.add("luca.frosini@isti.cnr.it"); + emailsAddresses.add("francesco.mangiacrapa@isti.cnr.it"); + // emailsAddresses.add("pagano@cnr.it"); String theQuery = mockQueryForEmails(emailsAddresses, "OR"); From b0a36a3da2556baa84dda1f8ceb8a1a8646b0c50 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Fri, 29 Jul 2022 10:18:34 +0200 Subject: [PATCH 3/4] Completed task #23692. Integrated with ContextText. Using gcat-client [2.0.0-SNAPSHOT, 2.3.0-SNAPSHOT) --- pom.xml | 11 +- .../server/cms/DataCatalogueCMSImpl.java | 5 +- src/test/java/.gitignore | 1 + .../utillibrary/test/ContextTest.java | 147 ++++++++++++++++++ .../test/TestDataCatalogueCMS.java | 33 ++-- .../test/TestDataCatalogueLib.java | 7 +- 6 files changed, 182 insertions(+), 22 deletions(-) create mode 100644 src/test/java/.gitignore create mode 100644 src/test/java/org/gcube/datacatalogue/utillibrary/test/ContextTest.java diff --git a/pom.xml b/pom.xml index 6971770..1be29cd 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,7 @@ com.google.gwt gwt-user + ${gwtVersion} provided @@ -100,7 +101,7 @@ org.gcube.data-catalogue gcat-client - [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + [2.0.0-SNAPSHOT, 2.3.0-SNAPSHOT) compile @@ -170,6 +171,14 @@ slf4j-api provided + + + + org.gcube.common + authorization-utils + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + test + diff --git a/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/DataCatalogueCMSImpl.java b/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/DataCatalogueCMSImpl.java index 10f419c..40aba2a 100644 --- a/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/DataCatalogueCMSImpl.java +++ b/src/main/java/org/gcube/datacatalogue/utillibrary/server/cms/DataCatalogueCMSImpl.java @@ -19,6 +19,7 @@ import org.gcube.datacatalogue.utillibrary.shared.ItemStatus; import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset; import org.gcube.gcat.api.moderation.CMItemStatus; import org.json.simple.JSONArray; +import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.slf4j.Logger; @@ -153,7 +154,7 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem { }else { LOG.debug("reading from the array the "+i+"mo json object"); //here the array contains the list of dataset json fields - jsonValueDataset = (String) jsonArray.get(i); + jsonValueDataset = ((JSONObject) jsonArray.get(i)).toString(); LOG.trace("the JSON dataset is: " + jsonValueDataset); } toCkanDataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset, METHOD.TO_READ); @@ -233,7 +234,7 @@ public class DataCatalogueCMSImpl implements CatalogueContentModeratorSystem { sortForField); if (datasetNames != null) { - LOG.debug("for status " + theStatus + " found dataset: " + datasetNames); + LOG.trace("for status " + theStatus + " found dataset: " + datasetNames); JSONParser parser = new JSONParser(); try { jsonArray = (JSONArray) parser.parse(datasetNames); diff --git a/src/test/java/.gitignore b/src/test/java/.gitignore new file mode 100644 index 0000000..a8e4366 --- /dev/null +++ b/src/test/java/.gitignore @@ -0,0 +1 @@ +/token.properties diff --git a/src/test/java/org/gcube/datacatalogue/utillibrary/test/ContextTest.java b/src/test/java/org/gcube/datacatalogue/utillibrary/test/ContextTest.java new file mode 100644 index 0000000..82abe9f --- /dev/null +++ b/src/test/java/org/gcube/datacatalogue/utillibrary/test/ContextTest.java @@ -0,0 +1,147 @@ +package org.gcube.datacatalogue.utillibrary.test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.GCubeSecret; +import org.gcube.common.authorization.utils.secret.Secret; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class ContextTest. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Jul 28, 2022 + */ +public class ContextTest { + + private static Logger logger = LoggerFactory.getLogger(ContextTest.class); + + protected static Properties properties; + protected static final String PROPERTIES_FILENAME = "token.properties"; + + public static final String ROOT = "/gcube"; + public static final String VO = ROOT + "/devsec"; + public static final String VRE = VO + "/devVRE"; + + static { + properties = new Properties(); + + try (InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME)) { + + if (input == null) { + String error = "Sorry, unable to find the file: " + PROPERTIES_FILENAME; + System.out.println(error); + throw new RuntimeException(error); + } + + // load a properties file from class path, inside static method + properties.load(input); + +// // get the property value and print it out +// System.out.println(prop.getProperty("db.url")); +// System.out.println(prop.getProperty("db.user")); +// System.out.println(prop.getProperty("db.password")); + + } catch (IOException ex) { + ex.printStackTrace(); + throw new RuntimeException(ex); + } + } + + /** + * Sets the. + * + * @param secret the secret + * @throws Exception the exception + */ + public static void set(Secret secret) throws Exception { + SecretManagerProvider.instance.reset(); + SecretManager secretManager = new SecretManager(); + SecretManagerProvider.instance.set(secretManager); + secretManager.addSecret(secret); + secretManager.set(); + String username = secretManager.getUser().getUsername(); + String context = secretManager.getContext(); + logger.debug("Set authorization for user {} in context {}", username, context); + } + + /** + * Sets the context. + * + * @param token the new context + * @throws Exception the exception + */ + public static void setContext(String token) throws Exception { + Secret secret = getSecret(token); + set(secret); + } + + /** + * Sets the context by name. + * + * @param fullContextName the new context by name + * @throws Exception the exception + */ + public static void setContextByName(String fullContextName) throws Exception { + logger.debug("setContextByName {}", fullContextName); + Secret secret = getSecretByContextName(fullContextName); + set(secret); + } + + /** + * Gets the secret. + * + * @param token the token + * @return the secret + * @throws Exception the exception + */ + private static Secret getSecret(String token) throws Exception { + GCubeSecret secret = new GCubeSecret(token); + return secret; + } + + /** + * Gets the secret by context name. + * + * @param fullContextName the full context name + * @return the secret by context name + * @throws Exception the exception + */ + private static Secret getSecretByContextName(String fullContextName) throws Exception { + logger.debug("getSecretByContextName {}", fullContextName); + String token = ContextTest.properties.getProperty(fullContextName); + logger.debug("token is {}", token); + return getSecret(token); + } + + /** + * Before class. + * + * @throws Exception the exception + */ + @BeforeClass + public static void beforeClass() throws Exception { + logger.debug("beforeClass"); + setContextByName(VRE); + } + + /** + * After class. + * + * @throws Exception the exception + */ + @AfterClass + public static void afterClass() throws Exception { + logger.debug("afterClass"); + SecretManagerProvider.instance.reset(); + } + +} diff --git a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java index 63ba762..f719c29 100644 --- a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java +++ b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java @@ -14,6 +14,8 @@ import org.gcube.datacatalogue.utillibrary.server.cms.CatalogueContentModeratorS import org.gcube.datacatalogue.utillibrary.shared.ItemStatus; import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset; import org.gcube.gcat.client.Item; +import org.junit.Before; +import org.junit.Test; import org.slf4j.LoggerFactory; /** @@ -21,7 +23,7 @@ import org.slf4j.LoggerFactory; * * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) Jun 1, 2020 */ -public class TestDataCatalogueCMS { +public class TestDataCatalogueCMS extends ContextTest { private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TestDataCatalogueCMS.class); @@ -36,21 +38,21 @@ public class TestDataCatalogueCMS { * * @throws Exception the exception */ - //@Before + @Before public void before() throws Exception { factory = DataCatalogueFactory.getFactory(); } - //@Test + @Test public void testGCatAvailability() { try { LOG.info("testGCatAvailability running"); int offset = 0; int limit = 10; - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); - LOG.debug("Trying with scope: "+scope); +// ScopeProvider.instance.set(scope); +// SecurityTokenProvider.instance.set(authorizationToken); +// LOG.debug("Trying with scope: "+scope); String items = new Item().list(limit, offset); LOG.debug("List items: " + items); } catch (Exception e) { @@ -63,22 +65,23 @@ public class TestDataCatalogueCMS { * * @throws Exception the exception */ - // @Test + //@Test public void contentModeratorTest() throws Exception { - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); +// ScopeProvider.instance.set(scope); +// SecurityTokenProvider.instance.set(authorizationToken); DataCatalogueFactory factory = DataCatalogueFactory.getFactory(); DataCatalogueImpl dImpl = factory.getUtilsPerScope(scope); CatalogueContentModeratorSystem cCMS = dImpl.getCatalogueContentModerator(); LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty"); } - // @Test + @Test public void listItemsForCMStatus() throws Exception { try { + long start = System.currentTimeMillis(); ItemStatus theStatus = ItemStatus.PENDING; - - SecurityTokenProvider.instance.set(authorizationToken); +// ScopeProvider.instance.set(scope); +// SecurityTokenProvider.instance.set(authorizationToken); DataCatalogueImpl dImpl = factory.getUtilsPerScope(scope); CatalogueContentModeratorSystem cCMS = dImpl.getCatalogueContentModerator(); LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty"); @@ -93,14 +96,14 @@ public class TestDataCatalogueCMS { Map filters = new HashMap(); filters.put("author_email", theQuery); - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); - List listItems = cCMS.getListItemsForStatus(theStatus, 10, 0, true, filters, + List listItems = cCMS.getListItemsForStatus(theStatus, 10, 0, false, filters, GCatCaller.DEFAULT_SORT_VALUE); int i = 0; for (CkanDataset ckanDataset : listItems) { System.out.println(++i + ") item returned: " + ckanDataset); } + long endTime = System.currentTimeMillis()-start; + System.out.println("Terminated in: "+endTime + "ms"); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueLib.java b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueLib.java index d452104..7eb0b57 100644 --- a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueLib.java +++ b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueLib.java @@ -11,7 +11,6 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject; import org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject.METHOD; -import org.gcube.datacatalogue.utillibrary.gcat.GCatCaller; import org.gcube.datacatalogue.utillibrary.server.ApplicationProfileScopePerUrlReader; import org.gcube.datacatalogue.utillibrary.server.DataCatalogueFactory; import org.gcube.datacatalogue.utillibrary.server.DataCatalogueImpl; @@ -63,7 +62,7 @@ public class TestDataCatalogueLib { * * @throws Exception the exception */ - // @Test + //@Test public void factoryTest() throws Exception { DataCatalogueFactory factory = DataCatalogueFactory.getFactory(); @@ -123,7 +122,7 @@ public class TestDataCatalogueLib { * @return the user role by group * @throws Exception the exception */ - // @Test + //@Test public void getDataset() throws Exception { DataCatalogueImpl instance = factory.getUtilsPerScope(scope); String username = testUser; @@ -157,7 +156,7 @@ public class TestDataCatalogueLib { LOG.debug("The user " + testUser + " in the org " + org.getName() + " has the role " + role); } - // @Test + //@Test public void getOrganizationForName() throws Exception { String orgName = "devvre"; From 4cab07998b1c6a59ff883e948711ebe06f84d1de Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 1 Aug 2022 10:45:39 +0200 Subject: [PATCH 4/4] Updated gcat-client --- pom.xml | 2 +- .../utillibrary/test/TestDataCatalogueCMS.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 1be29cd..6b81818 100644 --- a/pom.xml +++ b/pom.xml @@ -101,7 +101,7 @@ org.gcube.data-catalogue gcat-client - [2.0.0-SNAPSHOT, 2.3.0-SNAPSHOT) + [2.0.0, 3.0.0-SNAPSHOT) compile diff --git a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java index f719c29..d34c9a5 100644 --- a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java +++ b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java @@ -38,12 +38,12 @@ public class TestDataCatalogueCMS extends ContextTest { * * @throws Exception the exception */ - @Before + //@Before public void before() throws Exception { factory = DataCatalogueFactory.getFactory(); } - @Test + //@Test public void testGCatAvailability() { try { LOG.info("testGCatAvailability running"); @@ -75,7 +75,7 @@ public class TestDataCatalogueCMS extends ContextTest { LOG.debug(CatalogueContentModeratorSystem.class.getName() + " instancied correclty"); } - @Test + //@Test public void listItemsForCMStatus() throws Exception { try { long start = System.currentTimeMillis(); @@ -96,7 +96,7 @@ public class TestDataCatalogueCMS extends ContextTest { Map filters = new HashMap(); filters.put("author_email", theQuery); - List listItems = cCMS.getListItemsForStatus(theStatus, 10, 0, false, filters, + List listItems = cCMS.getListItemsForStatus(theStatus, 10, 0, true, filters, GCatCaller.DEFAULT_SORT_VALUE); int i = 0; for (CkanDataset ckanDataset : listItems) {