diff --git a/src/main/java/org/gcube/dataharvest/AccountingDataHarvesterPlugin.java b/src/main/java/org/gcube/dataharvest/AccountingDataHarvesterPlugin.java index 851ef16..588fbf9 100644 --- a/src/main/java/org/gcube/dataharvest/AccountingDataHarvesterPlugin.java +++ b/src/main/java/org/gcube/dataharvest/AccountingDataHarvesterPlugin.java @@ -88,7 +88,7 @@ public class AccountingDataHarvesterPlugin extends Plugin> dimensions = new InheritableThreadLocal>() { + protected static final InheritableThreadLocal> dimensions = new InheritableThreadLocal>() { @Override protected Map initialValue() { @@ -102,7 +102,7 @@ public class AccountingDataHarvesterPlugin extends Plugin> scopeDescriptors = new InheritableThreadLocal>() { + protected static final InheritableThreadLocal> scopeDescriptors = new InheritableThreadLocal>() { @Override protected Map initialValue() { diff --git a/src/main/java/org/gcube/dataharvest/utils/ContextAuthorization.java b/src/main/java/org/gcube/dataharvest/utils/ContextAuthorization.java index e8e8727..4f0aec5 100644 --- a/src/main/java/org/gcube/dataharvest/utils/ContextAuthorization.java +++ b/src/main/java/org/gcube/dataharvest/utils/ContextAuthorization.java @@ -50,6 +50,24 @@ public class ContextAuthorization { retrieveContextsAndTokens(); } + public String generateTokenForContext(String context, Properties properties) throws Exception { + if(properties==null) { + properties = AccountingDataHarvesterPlugin.getProperties().get(); + } + logger.info("Going to generate Token for Context {}", context); + UserInfo userInfo = new UserInfo(properties.getProperty(USERNAME, DEFAULT_USERNAME), + new ArrayList<>()); + String userToken = authorizationService().generateUserToken(userInfo, context); + SecurityTokenProvider.instance.set(userToken); + String generatedToken = authorizationService() + .generateExternalServiceToken(properties.getProperty(SERVICE_NAME, DEFAULT_SERVICE_NAME)); + + logger.trace("Token for Context {} is {}", context, generatedToken); + + return generatedToken; + + } + protected void retrieveContextsAndTokens() throws Exception { String initialToken = SecurityTokenProvider.instance.get(); @@ -62,16 +80,9 @@ public class ContextAuthorization { for(String scope : map.keySet()) { try { String context = map.get(scope).toString(); - logger.info("Going to generate Token for Context {}", context); - UserInfo userInfo = new UserInfo(properties.getProperty(USERNAME, DEFAULT_USERNAME), - new ArrayList<>()); - String userToken = authorizationService().generateUserToken(userInfo, context); - SecurityTokenProvider.instance.set(userToken); - String generatedToken = authorizationService() - .generateExternalServiceToken(properties.getProperty(SERVICE_NAME, DEFAULT_SERVICE_NAME)); - - logger.trace("Token for Context {} is {}", context, generatedToken); + String generatedToken = generateTokenForContext(context, properties); + contextToToken.put(context, generatedToken); tokenToContext.put(generatedToken, context); diff --git a/src/test/java/org/gcube/dataharvest/AccountingDataHarvesterPluginTest.java b/src/test/java/org/gcube/dataharvest/AccountingDataHarvesterPluginTest.java index 5f10080..4891613 100644 --- a/src/test/java/org/gcube/dataharvest/AccountingDataHarvesterPluginTest.java +++ b/src/test/java/org/gcube/dataharvest/AccountingDataHarvesterPluginTest.java @@ -1,5 +1,6 @@ package org.gcube.dataharvest; +import java.time.Instant; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -14,10 +15,12 @@ import java.util.TreeSet; import java.util.concurrent.TimeUnit; 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.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean.Type; +import org.gcube.dataharvest.datamodel.HarvestedDataKey; import org.gcube.dataharvest.harvester.MethodInvocationHarvester; import org.gcube.dataharvest.harvester.SocialInteractionsHarvester; import org.gcube.dataharvest.harvester.VREAccessesHarvester; @@ -41,6 +44,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { public static final String SO_BIG_DATA_CONTEXT = "/d4science.research-infrastructures.eu/SoBigData"; + public static SortedSet getContexts() throws Exception { SortedSet contexts = new TreeSet<>(); LinkedHashMap map = ContextManager.readContexts(); @@ -567,7 +571,7 @@ public class AccountingDataHarvesterPluginTest extends ContextTest { } } - @Test + // @Test public void testDataMethodDownloadHarvester() { try { @@ -597,4 +601,78 @@ 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); + + DataHarvestPluginDeclaration dataHarvestPluginDeclaration = new DataHarvestPluginDeclaration(); + AccountingDataHarvesterPlugin adhp = new AccountingDataHarvesterPlugin(dataHarvestPluginDeclaration); + Properties properties = adhp.getConfigParameters(); + AccountingDataHarvesterPlugin.getProperties().set(properties); + + ContextAuthorization contextAuthorization = new ContextAuthorization(); + + // DatabaseManager dbaseManager = new DatabaseManager(); + 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); + + // ArrayList data = new ArrayList(); + ArrayList accountingRecords = new ArrayList(); + + String context = E_LEARNING_AREA_VRE; + + // Setting the token for the context + Utils.setContext(contextAuthorization.getTokenForContext(context)); + + + ScopeBean scopeBean = new ScopeBean(context); + ScopeDescriptor scopeDescriptor = new ScopeDescriptor(scopeBean.name(), context); + + Dimension dimension = AccountingDataHarvesterPlugin.getDimension(HarvestedDataKey.ACCESSES.getKey()); + + Calendar calendar = DateUtils.getStartCalendar(2018, Calendar.JULY, 1); + calendar.set(Calendar.DAY_OF_MONTH, 15); + + Map monthValues = new HashMap<>(); + monthValues.put(Calendar.JULY, 54); + monthValues.put(Calendar.AUGUST, 23); + monthValues.put(Calendar.SEPTEMBER, 127); + monthValues.put(Calendar.OCTOBER, 192); + + for(Integer month : monthValues.keySet()) { + calendar.set(Calendar.MONTH, month); + Instant instant = calendar.toInstant(); + + AccountingRecord ar = new AccountingRecord(scopeDescriptor, instant, dimension, (long) monthValues.get(month)); + logger.debug("{} : {}", ar.getDimension().getId(), ar.getMeasure()); + accountingRecords.add(ar); + } + + logger.trace("{}", accountingRecords); + dao.insertRecords(accountingRecords.toArray(new AccountingRecord[1])); + + } catch(Exception e) { + logger.error("", e); + } + } }