Added test to create missing records

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-dashboard-harvester-se-plugin@174108 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2018-11-12 15:30:20 +00:00
parent c974aa57ab
commit 3923486937
3 changed files with 101 additions and 12 deletions

View File

@ -88,7 +88,7 @@ public class AccountingDataHarvesterPlugin extends Plugin<DataHarvestPluginDecla
return dimension;
}
private static final InheritableThreadLocal<Map<String, Dimension>> dimensions = new InheritableThreadLocal<Map<String, Dimension>>() {
protected static final InheritableThreadLocal<Map<String, Dimension>> dimensions = new InheritableThreadLocal<Map<String, Dimension>>() {
@Override
protected Map<String, Dimension> initialValue() {
@ -102,7 +102,7 @@ public class AccountingDataHarvesterPlugin extends Plugin<DataHarvestPluginDecla
return scopeDescriptors.get().get(context);
}
private static final InheritableThreadLocal<Map<String, ScopeDescriptor>> scopeDescriptors = new InheritableThreadLocal<Map<String, ScopeDescriptor>>() {
protected static final InheritableThreadLocal<Map<String, ScopeDescriptor>> scopeDescriptors = new InheritableThreadLocal<Map<String, ScopeDescriptor>>() {
@Override
protected Map<String, ScopeDescriptor> initialValue() {

View File

@ -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);

View File

@ -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<String> getContexts() throws Exception {
SortedSet<String> contexts = new TreeSet<>();
LinkedHashMap<String,ScopeBean> 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<ScopeDescriptor> scopeDescriptorSet = dao.getContexts();
Map<String,ScopeDescriptor> scopeDescriptorMap = new HashMap<>();
for(ScopeDescriptor scopeDescriptor : scopeDescriptorSet) {
scopeDescriptorMap.put(scopeDescriptor.getId(), scopeDescriptor);
}
AccountingDataHarvesterPlugin.scopeDescriptors.set(scopeDescriptorMap);
Set<Dimension> dimensionSet = dao.getDimensions();
Map<String,Dimension> dimensionMap = new HashMap<>();
for(Dimension dimension : dimensionSet) {
dimensionMap.put(dimension.getId(), dimension);
}
AccountingDataHarvesterPlugin.dimensions.set(dimensionMap);
// ArrayList<HarvestedData> data = new ArrayList<HarvestedData>();
ArrayList<AccountingRecord> accountingRecords = new ArrayList<AccountingRecord>();
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<Integer, Integer> 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);
}
}
}