diff --git a/src/main/java/org/gcube/dataharvest/AccountingDataHarvesterPlugin.java b/src/main/java/org/gcube/dataharvest/AccountingDataHarvesterPlugin.java index 588fbf9..68ecb46 100644 --- a/src/main/java/org/gcube/dataharvest/AccountingDataHarvesterPlugin.java +++ b/src/main/java/org/gcube/dataharvest/AccountingDataHarvesterPlugin.java @@ -115,7 +115,7 @@ public class AccountingDataHarvesterPlugin extends Plugin scopeDescriptor = new InheritableThreadLocal() { + public static final InheritableThreadLocal scopeDescriptor = new InheritableThreadLocal() { @Override protected ScopeDescriptor initialValue() { diff --git a/src/main/java/org/gcube/dataharvest/harvester/VREAccessesHarvester.java b/src/main/java/org/gcube/dataharvest/harvester/VREAccessesHarvester.java index 9e6f028..69e802e 100644 --- a/src/main/java/org/gcube/dataharvest/harvester/VREAccessesHarvester.java +++ b/src/main/java/org/gcube/dataharvest/harvester/VREAccessesHarvester.java @@ -26,7 +26,7 @@ import java.util.List; import org.gcube.accounting.accounting.summary.access.model.ScopeDescriptor; import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord; -import org.gcube.common.encryption.StringEncrypter; +import org.gcube.common.encryption.encrypter.StringEncrypter; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.Property; @@ -184,7 +184,7 @@ public class VREAccessesHarvester extends BasicHarvester { for(String view : viewIDs) { logger.info("Getting data from Google Analytics for viewid: " + view); // Create the ReportRequest object. - ReportRequest request = new ReportRequest().setViewId(view).setDateRanges(Arrays.asList(dateRange)) + ReportRequest request = new ReportRequest().setViewId(view.trim()).setDateRanges(Arrays.asList(dateRange)) .setMetrics(Arrays.asList(sessions)).setDimensions(Arrays.asList(pageTitle)); ArrayList requests = new ArrayList(); diff --git a/src/main/java/org/gcube/dataharvest/harvester/sobigdata/TagMeMethodInvocationHarvester.java b/src/main/java/org/gcube/dataharvest/harvester/sobigdata/TagMeMethodInvocationHarvester.java index 1f994c5..4ed52b9 100644 --- a/src/main/java/org/gcube/dataharvest/harvester/sobigdata/TagMeMethodInvocationHarvester.java +++ b/src/main/java/org/gcube/dataharvest/harvester/sobigdata/TagMeMethodInvocationHarvester.java @@ -35,7 +35,11 @@ public class TagMeMethodInvocationHarvester extends BasicHarvester { private static Logger logger = LoggerFactory.getLogger(TagMeMethodInvocationHarvester.class); - public static final String TAG_METHOD = "/tag"; + public static final String SWAT = "SWAT"; + public static final String TAGME = "TagMe"; + public static final String WAT = "WAT"; + + public static String[] SERVICE_NAMES = {SWAT,TAGME,WAT}; public TagMeMethodInvocationHarvester(Date start, Date end) throws Exception { super(start, end); @@ -51,42 +55,46 @@ public class TagMeMethodInvocationHarvester extends BasicHarvester { TemporalConstraint temporalConstraint = new TemporalConstraint(start.getTime(), end.getTime(), AggregationMode.MONTHLY); - List filters = new ArrayList<>(); - filters.add(new Filter(ServiceUsageRecord.CALLED_METHOD, TAG_METHOD)); - - String context = Utils.getCurrentContext(); - List contexts = new ArrayList<>(); + String context = Utils.getCurrentContext(); contexts.add(context); + + long numberOfInvocation = 0; + + for(String serviceName : SERVICE_NAMES) { + + List filters = new ArrayList<>(); + filters.add(new Filter(ServiceUsageRecord.SERVICE_NAME, serviceName)); - SortedMap> result = accountingPersistenceQuery.getContextTimeSeries( - AggregatedServiceUsageRecord.class, temporalConstraint, filters, contexts, true); - + SortedMap> result = accountingPersistenceQuery.getContextTimeSeries( + AggregatedServiceUsageRecord.class, temporalConstraint, filters, contexts, true); + + if(result != null) { + for(Filter filter : result.keySet()) { + SortedMap infoMap = result.get(filter); + + Calendar calendar = DateUtils.dateToCalendar(start); + + Info info = infoMap.get(calendar); + logger.debug("Service {} - {} : {}", serviceName, DateUtils.format(calendar), info); + + JSONObject jsonObject = info.getValue(); + numberOfInvocation += jsonObject.getLong(AggregatedUsageRecord.OPERATION_COUNT); + } + + } else { + logger.error("No data found in {} for service {}.", context, serviceName); + } + + } + ScopeDescriptor scopeDescriptor = AccountingDataHarvesterPlugin.getScopeDescriptor(); Dimension dimension = getDimension(HarvestedDataKey.METHOD_INVOCATIONS); - if(result != null) { - for(Filter filter : result.keySet()) { - SortedMap infoMap = result.get(filter); - - Calendar calendar = DateUtils.dateToCalendar(start); - - Info info = infoMap.get(calendar); - logger.debug("{} : {}", DateUtils.format(calendar), info); - - JSONObject jsonObject = info.getValue(); - long numberOfInvocation = jsonObject.getLong(AggregatedUsageRecord.OPERATION_COUNT); - - AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant, dimension, numberOfInvocation); - logger.debug("{} : {}", ar.getDimension().getId(), ar.getMeasure()); - accountingRecords.add(ar); - - } - - } else { - logger.error("No data found."); - } - + AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant, dimension, numberOfInvocation); + logger.debug("{} : {}", ar.getDimension().getId(), ar.getMeasure()); + accountingRecords.add(ar); + return accountingRecords; } catch(Exception e) { diff --git a/src/test/java/org/gcube/dataharvest/AccountingDataHarvesterPluginTest.java b/src/test/java/org/gcube/dataharvest/AccountingDataHarvesterPluginTest.java index 552333f..15062e0 100644 --- a/src/test/java/org/gcube/dataharvest/AccountingDataHarvesterPluginTest.java +++ b/src/test/java/org/gcube/dataharvest/AccountingDataHarvesterPluginTest.java @@ -18,6 +18,8 @@ import org.gcube.accounting.accounting.summary.access.AccountingDao; import org.gcube.accounting.accounting.summary.access.model.ScopeDescriptor; import org.gcube.accounting.accounting.summary.access.model.internal.Dimension; import org.gcube.accounting.accounting.summary.access.model.update.AccountingRecord; +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.dataharvest.datamodel.HarvestedDataKey; @@ -32,7 +34,6 @@ import org.gcube.dataharvest.utils.AggregationType; import org.gcube.dataharvest.utils.ContextAuthorization; import org.gcube.dataharvest.utils.ContextTest; import org.gcube.dataharvest.utils.DateUtils; -import org.gcube.dataharvest.utils.Utils; import org.gcube.resourcemanagement.support.server.managers.context.ContextManager; import org.gcube.vremanagement.executor.api.rest.SmartExecutor; import org.gcube.vremanagement.executor.api.types.LaunchParameter; @@ -48,8 +49,11 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { private static Logger logger = LoggerFactory.getLogger(AccountingDataHarvesterPluginTest.class); - public static final String SO_BIG_DATA_CONTEXT = "/d4science.research-infrastructures.eu/SoBigData"; + public static final String ROOT = "/d4science.research-infrastructures.eu"; + public static final String SO_BIG_VO = "/d4science.research-infrastructures.eu/SoBigData"; + public static final String TAGME_VRE = "/d4science.research-infrastructures.eu/SoBigData/TagMe"; + public static final String STOCK_ASSESMENT_VRE = "/d4science.research-infrastructures.eu/gCubeApps/StockAssessment"; public static SortedSet getContexts() throws Exception { SortedSet contexts = new TreeSet<>(); @@ -69,7 +73,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { public void getDimensions() { try { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); AccountingDao dao = AccountingDao.get(); @@ -89,7 +93,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { public void launch() { try { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); DataHarvestPluginDeclaration dataHarvestPluginDeclaration = new DataHarvestPluginDeclaration(); @@ -121,12 +125,11 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { } } - // @Test public void launchPluginOnSmartExecutor() { try { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); SmartExecutor smartExecutor = SmartExecutorClientFactory.create(DataHarvestPluginDeclaration.NAME); Assert.assertNotNull(smartExecutor); @@ -151,7 +154,8 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { CronExpression cronExpression = new CronExpression("0 0 10 3 1/1 ? *"); Scheduling scheduling = new Scheduling(cronExpression); scheduling.setGlobal(false); - LaunchParameter launchParameter = new LaunchParameter(DataHarvestPluginDeclaration.NAME, inputs, scheduling); + LaunchParameter launchParameter = new LaunchParameter(DataHarvestPluginDeclaration.NAME, inputs, + scheduling); smartExecutor.launch(launchParameter); logger.info("End."); @@ -165,7 +169,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { public void launchOldData() { try { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); DataHarvestPluginDeclaration dataHarvestPluginDeclaration = new DataHarvestPluginDeclaration(); @@ -204,7 +208,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { public void launchOldDataVREAccessesHarvester() { try { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); // AccountingDao dao = AccountingDao.get(); DataHarvestPluginDeclaration dataHarvestPluginDeclaration = new DataHarvestPluginDeclaration(); @@ -235,7 +239,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { for(String context : contexts) { // Setting the token for the context - Utils.setContext(contextAuthorization.getTokenForContext(context)); + ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ScopeBean scopeBean = new ScopeBean(context); @@ -252,12 +256,12 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { } // Setting back token for the context - Utils.setContext(contextAuthorization.getTokenForContext(parent.toString())); + ContextTest.setContext(contextAuthorization.getTokenForContext(parent.toString())); vreAccessesHarvester = new VREAccessesHarvester(start, end); // Setting back token for the context - Utils.setContext(contextAuthorization.getTokenForContext(context)); + ContextTest.setContext(contextAuthorization.getTokenForContext(context)); } } @@ -280,7 +284,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { logger.debug("Harvest Measures from {} to {} are {}", DateUtils.format(start), DateUtils.format(end), accountingRecords); - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); // dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1])); @@ -290,7 +294,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { } - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); } catch(Exception e) { logger.error("", e); @@ -302,7 +306,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { // @Test public void testScopeBean() throws Exception { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); SortedSet contexts = getContexts(); AggregationType aggregationType = AggregationType.MONTHLY; @@ -346,11 +350,61 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { } + @Test + public void testVREAccessesHarvester() throws Exception { + try { + AccountingDao dao = getAccountingDao(); + + List starts = new ArrayList<>(); + starts.add(DateUtils.getStartCalendar(2018, Calendar.SEPTEMBER, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2018, Calendar.OCTOBER, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2018, Calendar.NOVEMBER, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2018, Calendar.DECEMBER, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2019, Calendar.JANUARY, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2019, Calendar.FEBRUARY, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2019, Calendar.MARCH, 1).getTime()); + + AggregationType measureType = AggregationType.MONTHLY; + + String[] contextFullNames = new String[] {"/d4science.research-infrastructures.eu/FARM/GRSF", + "/d4science.research-infrastructures.eu/FARM/GRSF_Admin"}; + + List accountingRecords = new ArrayList<>(); + + for(Date start : starts) { + Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1); + + ContextTest.setContextByName(ROOT); + VREAccessesHarvester vreAccessesHarvester = new VREAccessesHarvester(start, end); + + for(String contextFullname : contextFullNames) { + + setContextByNameAndScopeDescriptor(contextFullname); + + List harvested = vreAccessesHarvester.getAccountingRecords(); + accountingRecords.addAll(harvested); + + logger.debug("{} - {}", contextFullname, accountingRecords); + + } + } + + logger.debug("{}", accountingRecords); + + ContextTest.setContextByName(ROOT); + // dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1])); + + } catch(Exception e) { + logger.error("", e); + throw e; + } + } + // @Test - public void testVREAccessesHarvester() { + public void testVREAccessesHarvesterAll() { try { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); AggregationType measureType = AggregationType.MONTHLY; @@ -372,7 +426,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { for(String context : contexts) { // Setting the token for the context - Utils.setContext(contextAuthorization.getTokenForContext(context)); + ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ScopeBean scopeBean = new ScopeBean(context); @@ -389,12 +443,12 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { } // Setting back token for the context - Utils.setContext(contextAuthorization.getTokenForContext(parent.toString())); + ContextTest.setContext(contextAuthorization.getTokenForContext(parent.toString())); vreAccessesHarvester = new VREAccessesHarvester(start, end); // Setting back token for the context - Utils.setContext(contextAuthorization.getTokenForContext(context)); + ContextTest.setContext(contextAuthorization.getTokenForContext(context)); } } @@ -425,7 +479,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { public void testSocialInteraction() { try { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); // AccountingDao dao = AccountingDao.get(); DataHarvestPluginDeclaration dataHarvestPluginDeclaration = new DataHarvestPluginDeclaration(); @@ -457,7 +511,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { for(String context : contexts) { // Setting the token for the context - Utils.setContext(contextAuthorization.getTokenForContext(context)); + ContextTest.setContext(contextAuthorization.getTokenForContext(context)); try { // Collecting info on social (posts, replies and likes) logger.info("Going to harvest Social Interactions for {}", context); @@ -471,7 +525,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { logger.debug("Harvest Measures from {} to {} are {}", DateUtils.format(start), DateUtils.format(end), accountingRecords); - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); // dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1])); } catch(Exception e) { @@ -486,7 +540,11 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { public void testMethodInvocation() { try { - Utils.setContext(StockAssessment); + ContextTest.setContextByName(ROOT); + ContextAuthorization contextAuthorization = new ContextAuthorization(); + String stockAssessmentToken = contextAuthorization.generateTokenForContext(STOCK_ASSESMENT_VRE, null); + + ContextTest.setContext(stockAssessmentToken); AggregationType measureType = AggregationType.MONTHLY; @@ -503,31 +561,85 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { } } - // @Test - public void testTagMeMethodInvocation() { + protected AccountingDao getAccountingDao() throws ObjectNotFound, Exception { + ContextTest.setContextByName(ROOT); + AccountingDao dao = AccountingDao.get(); + + Set scopeDescriptorSet = dao.getContexts(); + Map scopeDescriptorMap = new HashMap<>(); + for(ScopeDescriptor scopeDescriptor : scopeDescriptorSet) { + scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor); + } + AccountingDataHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap); + + Set dimensionSet = dao.getDimensions(); + Map dimensionMap = new HashMap<>(); + for(Dimension dimension : dimensionSet) { + dimensionMap.put(dimension.getId(), dimension); + } + + AccountingDataHarvesterPlugin.dimensions.set(dimensionMap); + + return dao; + } + + protected void setContextByNameAndScopeDescriptor(String contextFullName) throws ObjectNotFound, Exception { + ContextAuthorization contextAuthorization = new ContextAuthorization(); + String tagMeToken = contextAuthorization.getTokenForContext(contextFullName); + + ContextTest.setContext(tagMeToken); + + ScopeBean scopeBean = new ScopeBean(contextFullName); + ScopeDescriptor actualScopeDescriptor = AccountingDataHarvesterPlugin.scopeDescriptors.get() + .get(contextFullName); + if(actualScopeDescriptor == null) { + actualScopeDescriptor = new ScopeDescriptor(scopeBean.name(), contextFullName); + } + + AccountingDataHarvesterPlugin.scopeDescriptor.set(actualScopeDescriptor); + } + + @Test + public void testTagMeMethodInvocation() throws Exception { try { + AccountingDao dao = getAccountingDao(); + setContextByNameAndScopeDescriptor(TAGME_VRE); - Utils.setContext(TAGME); + List accountingRecords = new ArrayList<>(); AggregationType measureType = AggregationType.MONTHLY; - Date start = DateUtils.getPreviousPeriod(measureType).getTime(); - Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1); + List starts = new ArrayList<>(); + starts.add(DateUtils.getStartCalendar(2018, Calendar.NOVEMBER, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2018, Calendar.DECEMBER, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2019, Calendar.JANUARY, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2019, Calendar.FEBRUARY, 1).getTime()); + starts.add(DateUtils.getStartCalendar(2019, Calendar.MARCH, 1).getTime()); - TagMeMethodInvocationHarvester methodInvocationHarvester = new TagMeMethodInvocationHarvester(start, end); - List accountingRecords = methodInvocationHarvester.getAccountingRecords(); + for(Date start : starts) { + + Date end = DateUtils.getEndDateFromStartDate(measureType, start, 1); + + TagMeMethodInvocationHarvester methodInvocationHarvester = new TagMeMethodInvocationHarvester(start, + end); + accountingRecords.addAll(methodInvocationHarvester.getAccountingRecords()); + + logger.debug("{}", accountingRecords); + } - logger.debug("{}", accountingRecords); + ContextTest.setContextByName(ROOT); + //dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1])); } catch(Exception e) { logger.error("", e); + throw e; } } // @Test public void testGetVREUsersForSpecificVRE() { try { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); DataHarvestPluginDeclaration dataHarvestPluginDeclaration = new DataHarvestPluginDeclaration(); AccountingDataHarvesterPlugin accountingDataHarvesterPlugin = new AccountingDataHarvesterPlugin( @@ -538,7 +650,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { // AccountingDao dao = AccountingDao.get(); ContextAuthorization contextAuthorization = new ContextAuthorization(); - Utils.setContext(contextAuthorization + ContextTest.setContext(contextAuthorization .getTokenForContext("/d4science.research-infrastructures.eu/SoBigData/SportsDataScience")); AggregationType measureType = AggregationType.MONTHLY; @@ -551,7 +663,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { logger.info("Harvested Data from {} to {} : {}", DateUtils.format(start), DateUtils.format(end), harvested); - org.gcube.dataharvest.utils.Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); // dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1])); } catch(Exception e) { @@ -563,7 +675,8 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { // @Test public void testFilteringGenericResource() { try { - Utils.setContext(RESOURCE_CATALOGUE); + ContextTest.setContextByName(ROOT); + //Utils.setContext(RESOURCE_CATALOGUE); AggregationType measureType = AggregationType.MONTHLY; @@ -577,8 +690,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { ResourceCatalogueHarvester resourceCatalogueHarvester = new ResourceCatalogueHarvester(start, end, contexts); - SortedSet validContexts = resourceCatalogueHarvester.getValidContexts(contexts, - SO_BIG_DATA_CONTEXT + "/"); + SortedSet validContexts = resourceCatalogueHarvester.getValidContexts(contexts, SO_BIG_VO + "/"); logger.info("Valid Contexts {}", validContexts); } catch(Exception e) { @@ -591,7 +703,8 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { public void testResourceCatalogueHarvester() { try { - Utils.setContext(RESOURCE_CATALOGUE); + //Utils.setContext(RESOURCE_CATALOGUE); + ContextTest.setContextByName(ROOT); AggregationType measureType = AggregationType.MONTHLY; @@ -621,7 +734,8 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { public void testDataMethodDownloadHarvester() { try { - Utils.setContext(RESOURCE_CATALOGUE); + //Utils.setContext(RESOURCE_CATALOGUE); + ContextTest.setContextByName(ROOT); AggregationType measureType = AggregationType.MONTHLY; @@ -647,14 +761,13 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { } } - public static final String E_LEARNING_AREA_VRE = "/d4science.research-infrastructures.eu/SoBigData/E-Learning_Area"; // @Test public void addMissingVREAccesses() { try { - Utils.setContext(ROOT); + ContextTest.setContextByName(ROOT); DataHarvestPluginDeclaration dataHarvestPluginDeclaration = new DataHarvestPluginDeclaration(); AccountingDataHarvesterPlugin adhp = new AccountingDataHarvesterPlugin(dataHarvestPluginDeclaration); @@ -673,7 +786,6 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { } AccountingDataHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap); - Set dimensionSet = dao.getDimensions(); Map dimensionMap = new HashMap<>(); for(Dimension dimension : dimensionSet) { @@ -688,8 +800,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { String context = E_LEARNING_AREA_VRE; // Setting the token for the context - Utils.setContext(contextAuthorization.getTokenForContext(context)); - + ContextTest.setContext(contextAuthorization.getTokenForContext(context)); ScopeBean scopeBean = new ScopeBean(context); ScopeDescriptor scopeDescriptor = new ScopeDescriptor(scopeBean.name(), context); @@ -699,7 +810,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { Calendar calendar = DateUtils.getStartCalendar(2018, Calendar.JULY, 1); calendar.set(Calendar.DAY_OF_MONTH, 15); - Map monthValues = new HashMap<>(); + Map monthValues = new HashMap<>(); monthValues.put(Calendar.JULY, 54); monthValues.put(Calendar.AUGUST, 23); monthValues.put(Calendar.SEPTEMBER, 127); @@ -709,7 +820,8 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { calendar.set(Calendar.MONTH, month); Instant instant = calendar.toInstant(); - AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant, dimension, (long) monthValues.get(month)); + AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant, dimension, + (long) monthValues.get(month)); logger.debug("{} : {}", ar.getDimension().getId(), ar.getMeasure()); accountingRecords.add(ar); } diff --git a/src/test/java/org/gcube/dataharvest/utils/ContextTest.java b/src/test/java/org/gcube/dataharvest/utils/ContextTest.java index 31f5e0f..db0da7b 100644 --- a/src/test/java/org/gcube/dataharvest/utils/ContextTest.java +++ b/src/test/java/org/gcube/dataharvest/utils/ContextTest.java @@ -7,7 +7,13 @@ import java.io.IOException; import java.io.InputStream; import java.util.Properties; +import org.gcube.common.authorization.client.Constants; +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.provider.AuthorizationProvider; +import org.gcube.common.authorization.library.provider.ClientInfo; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.authorization.library.utils.Caller; import org.gcube.common.scope.api.ScopeProvider; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -16,43 +22,19 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) + * */ public class ContextTest { private static final Logger logger = LoggerFactory.getLogger(ContextTest.class); + protected static Properties properties; protected static final String PROPERTIES_FILENAME = "token.properties"; - private static final String GCUBE_DEVNEXT_VARNAME = "GCUBE_DEVNEXT"; - public static final String GCUBE_DEVNEXT; - - private static final String GCUBE_DEVNEXT_NEXTNEXT_VARNAME = "GCUBE_DEVNEXT_NEXTNEXT"; - public static final String GCUBE_DEVNEXT_NEXTNEXT; - - public static final String GCUBE_VARNAME = "GCUBE"; - public static final String GCUBE; - - public static final String DEFAULT_TEST_SCOPE; - - public static final String ROOT_VARNAME = "ROOT_ERIC"; - public static final String ROOT; - - public static final String TAGME_VARNAME = "TAGME_ERIC"; - public static final String TAGME; - - public static final String StockAssessment_VARNAME = "StockAssessment"; - public static final String StockAssessment; - - public static final String RESOURCE_CATALOGUE_VARNAME = "RESOURCE_CATALOGUE"; - public static final String RESOURCE_CATALOGUE; - - + public static final String DEFAULT_TEST_SCOPE_NAME; static { - - logger.trace("Retrieving Tokens from {}", PROPERTIES_FILENAME); - - Properties properties = new Properties(); + properties = new Properties(); InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME); try { @@ -62,25 +44,36 @@ public class ContextTest { throw new RuntimeException(e); } - GCUBE = properties.getProperty(GCUBE_VARNAME); - - GCUBE_DEVNEXT = properties.getProperty(GCUBE_DEVNEXT_VARNAME); - GCUBE_DEVNEXT_NEXTNEXT = properties.getProperty(GCUBE_DEVNEXT_NEXTNEXT_VARNAME); - - ROOT = properties.getProperty(ROOT_VARNAME); - - TAGME = properties.getProperty(TAGME_VARNAME); - - RESOURCE_CATALOGUE = properties.getProperty(RESOURCE_CATALOGUE_VARNAME); - - StockAssessment = properties.getProperty(StockAssessment_VARNAME); - - DEFAULT_TEST_SCOPE = GCUBE; + //DEFAULT_TEST_SCOPE_NAME = "/pred4s/preprod/preVRE"; + DEFAULT_TEST_SCOPE_NAME = "/gcube/devNext/NextNext"; + } + + public static String getCurrentScope(String token) throws ObjectNotFound, Exception { + AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); + String context = authorizationEntry.getContext(); + logger.info("Context of token {} is {}", token, context); + return context; + } + + public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception { + String token = ContextTest.properties.getProperty(fullContextName); + setContext(token); + } + + public static void setContext(String token) throws ObjectNotFound, Exception { + SecurityTokenProvider.instance.set(token); + AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); + ClientInfo clientInfo = authorizationEntry.getClientInfo(); + logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name()); + String qualifier = authorizationEntry.getQualifier(); + Caller caller = new Caller(clientInfo, qualifier); + AuthorizationProvider.instance.set(caller); + ScopeProvider.instance.set(getCurrentScope(token)); } @BeforeClass public static void beforeClass() throws Exception { - Utils.setContext(DEFAULT_TEST_SCOPE); + setContextByName(DEFAULT_TEST_SCOPE_NAME); } @AfterClass