diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/BindingAction.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/BindingAction.java index d469faa..0a15ef0 100644 --- a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/BindingAction.java +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/BindingAction.java @@ -8,12 +8,8 @@ import org.gcube.application.cms.cataloguebinding.config.BindingWhen; import org.gcube.application.cms.cataloguebinding.freemarker.FreemarkerConfig; import org.gcube.application.cms.cataloguebinding.freemarker.MappingToCatalogue; import org.gcube.application.cms.plugins.events.ItemObserved; -import org.gcube.application.cms.serviceaccount.GeoportalServiceAccount; import org.gcube.application.geoportal.common.model.document.Project; import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation; -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 freemarker.template.Template; import freemarker.template.TemplateException; @@ -51,12 +47,12 @@ public class BindingAction { // Create or Update the item on the catalogue String catalogueItemJSON = toCatalogueItem(); if (catalogueItemJSON != null) - new GCatCaller().createOrUpdateTheDatasetOnCatalogue(datasetName, catalogueItemJSON, true); + new CatalogueCaller().createOrUpdateTheDatasetOnCatalogue(datasetName, catalogueItemJSON, true); break; } case PROJECT_DELETED: { log.info("Going to delete item with name {} on the catalogue...", datasetName); - new GCatCaller().deleteDatasetOnCatalogue(datasetName, true); + new CatalogueCaller().deleteDatasetOnCatalogue(datasetName, true); break; } case PROJECT_UPDATED: { @@ -64,7 +60,7 @@ public class BindingAction { // Create or Update the item on the catalogue String catalogueItemJSON = toCatalogueItem(); if (catalogueItemJSON != null) - new GCatCaller().createOrUpdateTheDatasetOnCatalogue(datasetName, catalogueItemJSON, true); + new CatalogueCaller().createOrUpdateTheDatasetOnCatalogue(datasetName, catalogueItemJSON, true); break; } case LIFECYCLE_STEP_PERFORMED: { @@ -107,14 +103,14 @@ public class BindingAction { String catalogueItemJSON = toCatalogueItem(); if (catalogueItemJSON != null) { log.info("Going to create item with name {} on the catalogue...", datasetName); - new GCatCaller().createOrUpdateTheDatasetOnCatalogue(datasetName, catalogueItemJSON, + new CatalogueCaller().createOrUpdateTheDatasetOnCatalogue(datasetName, catalogueItemJSON, true); } } else if (itemPhase.equalsIgnoreCase(TARGET_PHASE_DRAFT)) { // Delete the item on the catalogue log.info("Going to delete item with name {} on the catalogue...", datasetName); - new GCatCaller().deleteDatasetOnCatalogue(datasetName, true); + new CatalogueCaller().deleteDatasetOnCatalogue(datasetName, true); } } else { log.info( diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/GCatCaller.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/CatalogueCaller.java similarity index 60% rename from catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/GCatCaller.java rename to catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/CatalogueCaller.java index 9420729..90eb19d 100644 --- a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/GCatCaller.java +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/CatalogueCaller.java @@ -16,15 +16,14 @@ import org.gcube.gcat.client.Item; import lombok.extern.slf4j.Slf4j; /** - * The Class GCatCaller. + * The Class CatalogueCaller. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * - * Jun 20, 2024 + * Jul 3, 2024 */ - @Slf4j -public class GCatCaller { +public class CatalogueCaller { /** * Creates the or update the dataset on catalogue. @@ -38,28 +37,29 @@ public class GCatCaller { boolean useGeoportalServiceAccount) throws Exception { log.info("createOrUpdateTheDatasetOnCatalogue with name {} called. Uses GeoportalServiceAccount: {}", datasetName, useGeoportalServiceAccount); - SecretManager secretManager = null; + + SecretManager secretManager = SecretManagerProvider.instance.get(); + boolean datasetExists = false; + Secret geoportalSecret = null; try { - boolean datasetExists = false; + // Starting geoportal secret session + if (useGeoportalServiceAccount) { + geoportalSecret = getGeoportalClientSecret(); + secretManager.startSession(geoportalSecret); + } try { - if (useGeoportalServiceAccount) - secretManager = setGeoportalClientSecret(); - String dataset = new Item().read(datasetName); datasetExists = dataset != null ? true : false; } catch (Exception e) { - resetGeoportalSecret(secretManager); + log.warn("Error on reading dataset for name: " + datasetName+". Does not exist?", e); } log.info("datasetExists is {}", datasetExists); if (!datasetExists) { - if (useGeoportalServiceAccount) - secretManager = setGeoportalClientSecret(); - log.info("calling create dataset..."); String itemCreated = new Item().create(datasetJSON, false); if (itemCreated != null) @@ -67,13 +67,8 @@ public class GCatCaller { else log.warn("Dataset not created!"); - resetGeoportalSecret(secretManager); - } else { - if (useGeoportalServiceAccount) - secretManager = setGeoportalClientSecret(); - log.info("calling update dataset..."); String itemCreated = new Item().update(datasetName, datasetJSON); if (itemCreated != null) @@ -81,13 +76,16 @@ public class GCatCaller { else log.warn("Dataset not updated!"); - resetGeoportalSecret(secretManager); } } catch (WebApplicationException | MalformedURLException e) { log.error("Error occurred on creating the dataset " + datasetName + " on the catalogue", e); - resetGeoportalSecret(secretManager); throw e; + } finally { + if (useGeoportalServiceAccount && geoportalSecret != null) { + secretManager.endSession(); + } + } } @@ -100,88 +98,101 @@ public class GCatCaller { public void deleteDatasetOnCatalogue(String datasetName, boolean useGeoportalServiceAccount) { log.info("deleteDatasetOnCatalogue with name {} called. Uses GeoportalServiceAccount: {}", datasetName, useGeoportalServiceAccount); - SecretManager secretManager = null; + + SecretManager secretManager = SecretManagerProvider.instance.get(); + boolean datasetExists = false; + Secret geoportalSecret = null; + try { - boolean datasetExists = false; - try { + // Starting geoportal secret session + if (useGeoportalServiceAccount) { + geoportalSecret = getGeoportalClientSecret(); + secretManager.startSession(geoportalSecret); + } - if (useGeoportalServiceAccount) - secretManager = setGeoportalClientSecret(); + try { String dataset = new Item().read(datasetName); datasetExists = dataset != null ? true : false; + + log.info("datasetExists is {}", datasetExists); } catch (Exception e) { - resetGeoportalSecret(secretManager); + log.warn("Error on checking datasetExists for name: " + datasetName, e); } log.info("datasetExists is {}", datasetExists); if (datasetExists) { - if (useGeoportalServiceAccount) - secretManager = setGeoportalClientSecret(); - log.info("calling delete dataset and purge"); new Item().delete(datasetName, true); - resetGeoportalSecret(secretManager); log.info("Dataset deleted and purged with success!"); } else { - log.warn("Dataset does not exists, returning.."); + log.warn("Dataset does not exist, returning.."); return; } - } catch (WebApplicationException | MalformedURLException e) { - log.error("Error occurred on deleting the dataset " + datasetName + " on the catalogue", e); - resetGeoportalSecret(secretManager); - } - } - - public String list(int limit, int offset, boolean useGeoportalServiceAccount) { - log.info("list called. Uses GeoportalServiceAccount: {}", useGeoportalServiceAccount); - SecretManager secretManager = null; - try { - if (useGeoportalServiceAccount) - secretManager = setGeoportalClientSecret(); - String theList = new Item().list(limit, offset); - resetGeoportalSecret(secretManager); - return theList; - } catch (WebApplicationException | MalformedURLException e) { - log.error("Error occurred on listing datasets", e); - resetGeoportalSecret(secretManager); - } - - return null; - - } - - private SecretManager setGeoportalClientSecret() { - log.debug("setting GeoportalClientSecret"); - try { - - final SecretManager secretManager = SecretManagerProvider.instance.get(); - final Secret geoportalSecret = GeoportalServiceAccount.getGeoportalSecret(); - secretManager.startSession(geoportalSecret); - log.debug("returning GeoportalSecret Manager"); - return secretManager; - } catch (Exception e) { - log.error("Error while setting GeoportalServiceAccount SecretManager", e); - } - - return null; - } - - private void resetGeoportalSecret(SecretManager secretManager) { - log.debug("resetting GeoportalClientSecret"); - if (secretManager != null) { - try { - log.debug("GeoportalSecret endSession"); + log.error("Error occurred on deleting the dataset " + datasetName + " on the catalogue", e); + } finally { + // Terminating geoportal secret session + if (useGeoportalServiceAccount && geoportalSecret != null) { secretManager.endSession(); - } catch (Exception e) { - log.warn("Error when resetting the session"); } } } + /** + * List. + * + * @param limit the limit + * @param offset the offset + * @param useGeoportalServiceAccount the use geoportal service account + * @return the string + */ + public String list(int limit, int offset, boolean useGeoportalServiceAccount) { + log.info("list called. Uses GeoportalServiceAccount: {}", useGeoportalServiceAccount); + + SecretManager secretManager = SecretManagerProvider.instance.get(); + Secret geoportalSecret = null; + try { + // Starting geoportal secret session + if (useGeoportalServiceAccount) { + geoportalSecret = getGeoportalClientSecret(); + secretManager.startSession(geoportalSecret); + } + + String theList = new Item().list(limit, offset); + return theList; + } catch (Exception e) { + log.error("Error occurred on listing datasets", e); + } finally { + // Terminating geoportal secret session + if (useGeoportalServiceAccount && geoportalSecret != null) { + secretManager.endSession(); + } + } + + return null; + + } + + /** + * Gets the geoportal client secret. + * + * @return the geoportal client secret + */ + private Secret getGeoportalClientSecret() { + log.debug("get GeoportalClientSecret"); + try { + return GeoportalServiceAccount.getGeoportalSecret(); + + } catch (Exception e) { + log.error("Error while getting GeoportalServiceAccount SecretManager", e); + } + + return null; + } + } diff --git a/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/CataloguePurgeDatasets.java b/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/CataloguePurgeDatasets.java index ec6495d..c3747b1 100644 --- a/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/CataloguePurgeDatasets.java +++ b/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/CataloguePurgeDatasets.java @@ -1,6 +1,8 @@ package geoportal_to_catalogue; -import org.gcube.application.cms.cataloguebinding.doaction.GCatCaller; +import org.gcube.application.cms.cataloguebinding.doaction.CatalogueCaller; +import org.gcube.application.cms.tests.TokenSetter; +import org.gcube.application.geoportal.common.utils.tests.GCubeTest; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; import org.json.JSONArray; @@ -13,13 +15,13 @@ public class CataloguePurgeDatasets { private static boolean useGeoportalServiceAccount = true; public static void main(String[] args) { - - //TokenSetter.set(GCubeTest.getContext()); - + org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); TestContextConfig.readContextSettings(); System.out.println("CONTEXT: " + TestContextConfig.CONTEXT); System.out.println("TOKEN: " + TestContextConfig.TOKEN); + + TokenSetter.set(GCubeTest.getContext()); int limit = 10; int offset = 0; @@ -31,7 +33,7 @@ public class CataloguePurgeDatasets { System.out.println("offest: " + offset + ", limit: " + limit); ScopeProvider.instance.set(TestContextConfig.CONTEXT); SecurityTokenProvider.instance.set(TestContextConfig.TOKEN); - String items = new GCatCaller().list(limit, offset, useGeoportalServiceAccount); + String items = new CatalogueCaller().list(limit, offset, useGeoportalServiceAccount); System.out.println("items: " + items); if (items != null) { resultExists = true; @@ -68,7 +70,7 @@ public class CataloguePurgeDatasets { System.out.println(index + ") Deleting dataset name : " + datasetName); ScopeProvider.instance.set(TestContextConfig.CONTEXT); SecurityTokenProvider.instance.set(TestContextConfig.TOKEN); - new GCatCaller().deleteDatasetOnCatalogue(datasetName, useGeoportalServiceAccount); + new CatalogueCaller().deleteDatasetOnCatalogue(datasetName, useGeoportalServiceAccount); System.out.println("sleeping..." + sleepingTime); Thread.sleep(sleepingTime); } @@ -87,7 +89,7 @@ public class CataloguePurgeDatasets { try { ScopeProvider.instance.set(TestContextConfig.CONTEXT); SecurityTokenProvider.instance.set(TestContextConfig.TOKEN); - String items = new GCatCaller().list(limit, offset, useGeoportalServiceAccount); + String items = new CatalogueCaller().list(limit, offset, useGeoportalServiceAccount); System.out.println("items: " + items); JSONArray array = new JSONArray(items); @@ -96,7 +98,7 @@ public class CataloguePurgeDatasets { System.out.println(i + ") name : " + datasetName); ScopeProvider.instance.set(TestContextConfig.CONTEXT); SecurityTokenProvider.instance.set(TestContextConfig.TOKEN); - new GCatCaller().deleteDatasetOnCatalogue(datasetName, useGeoportalServiceAccount); + new CatalogueCaller().deleteDatasetOnCatalogue(datasetName, useGeoportalServiceAccount); Thread.sleep(5000); } diff --git a/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/GeoportalToCatalogueBatchPublisher.java b/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/GeoportalToCatalogueBatchPublisher.java index 57bf118..97b98ec 100644 --- a/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/GeoportalToCatalogueBatchPublisher.java +++ b/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/GeoportalToCatalogueBatchPublisher.java @@ -18,12 +18,15 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; -import org.gcube.application.cms.cataloguebinding.doaction.GCatCaller; +import org.gcube.application.cms.cataloguebinding.CatalogueBindingPlugin; +import org.gcube.application.cms.cataloguebinding.doaction.CatalogueCaller; import org.gcube.application.cms.cataloguebinding.freemarker.MappingToCatalogue; +import org.gcube.application.cms.tests.plugins.BasicPluginTest; import org.gcube.application.geoportal.common.model.configuration.Archive; import org.gcube.application.geoportal.common.model.document.Project; import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor; import org.gcube.application.geoportal.common.rest.Projects; +import org.gcube.application.geoportal.common.utils.tests.GCubeTest; import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller; import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller; import org.gcube.application.geoportalcommon.shared.SearchingFilter; @@ -33,6 +36,7 @@ import org.gcube.application.geoportalcommon.shared.WhereClause; import org.gcube.application.geoportalcommon.shared.geoportal.config.ItemFieldDV; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; +import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; @@ -57,7 +61,7 @@ import test.TestContextConfig; * Jun 25, 2024 */ @Slf4j -public class GeoportalToCatalogueBatchPublisher { +public class GeoportalToCatalogueBatchPublisher extends BasicPluginTest { public final static String profileID = "profiledConcessioni"; public final static Integer MAX_ITEMS = 1; @@ -68,15 +72,17 @@ public class GeoportalToCatalogueBatchPublisher { private static final String CSV_DELIMITER = ";"; - private static boolean useGeoportalServiceAccount = false; + private static boolean useGeoportalServiceAccount = true; /** * The main method. * * @param args the arguments */ - public static void main(String[] args) { - + //@Test + public void testPublish() { + //org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); + //CatalogueBindingPlugin plugin = (CatalogueBindingPlugin) plugins.get(CatalogueBindingPlugin.DESCRIPTOR.getId()); procedureToPublishProjectsOnCatalogue(); // String projectId = " 6663016a312dc236d217be5c"; @@ -114,6 +120,9 @@ public class GeoportalToCatalogueBatchPublisher { TestContextConfig.readContextSettings(); String sourceScope = TestContextConfig.CONTEXT; String sourceToken = TestContextConfig.TOKEN; + + //Setting empty SecretManager for test case + //SecretManagerProvider.instance.set(new SecretManager()); String reportFile = TestContextConfig.CONTEXT.replaceAll("/", "_") + "_report_.csv"; String errorFile = TestContextConfig.CONTEXT.replaceAll("/", "_") + "_error_.csv"; @@ -200,7 +209,7 @@ public class GeoportalToCatalogueBatchPublisher { * ##### NB!!!! USING TARGET SCOPE * */ - String targetScope = ""; + String targetScope = "/gcube/devsec/devVRE"; String targetToken = ""; if (targetScope == null || targetToken == null) { @@ -270,7 +279,7 @@ public class GeoportalToCatalogueBatchPublisher { */ public static void publishOnCatalogue(Project theProject, String toCatalogueJSON) throws Exception { - new GCatCaller().createOrUpdateTheDatasetOnCatalogue(theProject.getId(), toCatalogueJSON, useGeoportalServiceAccount); + new CatalogueCaller().createOrUpdateTheDatasetOnCatalogue(theProject.getId(), toCatalogueJSON, useGeoportalServiceAccount); } /** diff --git a/catalogue-binding-plugin/src/test/java/junit/CatalogueBindingPluginTest.java b/catalogue-binding-plugin/src/test/java/junit/CatalogueBindingPluginTest.java index ec4c85a..b539d36 100644 --- a/catalogue-binding-plugin/src/test/java/junit/CatalogueBindingPluginTest.java +++ b/catalogue-binding-plugin/src/test/java/junit/CatalogueBindingPluginTest.java @@ -91,7 +91,7 @@ public class CatalogueBindingPluginTest extends BasicPluginTest { /** * Check notify APPROV E SUBMITTE D on catalogue. */ - @Test + //@Test public void checkNotify_APPROVE_SUBMITTED_onCatalogue() { org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); CatalogueBindingPlugin plugin = (CatalogueBindingPlugin) plugins.get(CatalogueBindingPlugin.DESCRIPTOR.getId());