Added some tests
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-analytics-persistence-couchbase@154373 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
7b894ac020
commit
f748e0c958
|
@ -9,12 +9,10 @@ import java.util.Calendar;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.SortedMap;
|
import java.util.SortedMap;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
|
|
||||||
import org.gcube.accounting.analytics.Filter;
|
import org.gcube.accounting.analytics.Filter;
|
||||||
import org.gcube.accounting.analytics.FiltersValue;
|
|
||||||
import org.gcube.accounting.analytics.Info;
|
import org.gcube.accounting.analytics.Info;
|
||||||
import org.gcube.accounting.analytics.NumberedFilter;
|
import org.gcube.accounting.analytics.NumberedFilter;
|
||||||
import org.gcube.accounting.analytics.TemporalConstraint;
|
import org.gcube.accounting.analytics.TemporalConstraint;
|
||||||
|
@ -23,18 +21,15 @@ import org.gcube.accounting.analytics.UsageServiceValue;
|
||||||
import org.gcube.accounting.analytics.UsageStorageValue;
|
import org.gcube.accounting.analytics.UsageStorageValue;
|
||||||
import org.gcube.accounting.analytics.UsageValue;
|
import org.gcube.accounting.analytics.UsageValue;
|
||||||
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryConfiguration;
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryConfiguration;
|
||||||
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryFactory;
|
|
||||||
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
||||||
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
|
|
||||||
import org.gcube.accounting.datamodel.aggregation.AggregatedJobUsageRecord;
|
import org.gcube.accounting.datamodel.aggregation.AggregatedJobUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageStatusRecord;
|
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageStatusRecord;
|
||||||
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
|
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord;
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -44,7 +39,7 @@ import org.slf4j.LoggerFactory;
|
||||||
* @author Alessandro Pieve (ISTI - CNR)
|
* @author Alessandro Pieve (ISTI - CNR)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class AccountingPersistenceQueryCouchBaseTest {
|
public class AccountingPersistenceQueryCouchBaseTest extends ScopedTest {
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(AccountingPersistenceQueryCouchBaseTest.class);
|
private static Logger logger = LoggerFactory.getLogger(AccountingPersistenceQueryCouchBaseTest.class);
|
||||||
|
|
||||||
|
@ -85,24 +80,14 @@ public class AccountingPersistenceQueryCouchBaseTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() throws Exception{
|
public void before() throws Exception{
|
||||||
|
|
||||||
//ScopeProvider.instance.set("/gcube/devNext/NextNext");
|
|
||||||
//ScopeProvider.instance.set("/gcube/devNext");
|
|
||||||
ScopeProvider.instance.set("/gcube");
|
|
||||||
//ScopeProvider.instance.set("/d4science.research-infrastructures.eu");
|
|
||||||
|
|
||||||
AccountingPersistenceBackendQueryConfiguration configuration = new
|
AccountingPersistenceBackendQueryConfiguration configuration = new
|
||||||
AccountingPersistenceBackendQueryConfiguration(AccountingPersistenceQueryCouchBase.class);
|
AccountingPersistenceBackendQueryConfiguration(AccountingPersistenceQueryCouchBase.class);
|
||||||
|
|
||||||
accountingPersistenceQueryCouchBase = new AccountingPersistenceQueryCouchBase();
|
accountingPersistenceQueryCouchBase = new AccountingPersistenceQueryCouchBase();
|
||||||
accountingPersistenceQueryCouchBase.prepareConnection(configuration);
|
accountingPersistenceQueryCouchBase.prepareConnection(configuration);
|
||||||
}
|
}
|
||||||
@After
|
|
||||||
public void after() throws Exception{
|
|
||||||
//SecurityTokenProvider.instance.reset();
|
|
||||||
ScopeProvider.instance.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void printMap(Map<Calendar, Info> map){
|
public void printMap(Map<Calendar, Info> map){
|
||||||
for(Info info : map.values()){
|
for(Info info : map.values()){
|
||||||
|
@ -111,32 +96,81 @@ public class AccountingPersistenceQueryCouchBaseTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTopJob() throws Exception {
|
public void getUsersInVREs() throws Exception {
|
||||||
|
List<String> contexts = new ArrayList<>();
|
||||||
|
contexts.add("/d4science.research-infrastructures.eu/gCubeApps/ICCAT_BFT-E");
|
||||||
|
contexts.add("/d4science.research-infrastructures.eu/gCubeApps/StockAssessment");
|
||||||
|
contexts.add("/d4science.research-infrastructures.eu/gCubeApps/RStudioLab");
|
||||||
|
contexts.add("/d4science.research-infrastructures.eu/gCubeApps/FAO_TunaAtlas");
|
||||||
|
|
||||||
Calendar startTime = Calendar.getInstance();
|
Calendar startTime = Calendar.getInstance();
|
||||||
startTime.set(2015, Calendar.AUGUST, 20);
|
startTime.set(2015, Calendar.SEPTEMBER, 15);
|
||||||
|
|
||||||
|
logger.debug("StartTime {}", startTime.getTimeInMillis());
|
||||||
|
|
||||||
Calendar endTime = Calendar.getInstance();
|
Calendar endTime = Calendar.getInstance();
|
||||||
endTime.set(2016, Calendar.SEPTEMBER, 29,23,59);
|
endTime.set(2017, Calendar.JULY, 15);
|
||||||
|
|
||||||
|
logger.debug("EndTime {}", endTime.getTimeInMillis());
|
||||||
List<Filter> filters = new ArrayList<Filter>();
|
|
||||||
|
|
||||||
TemporalConstraint temporalConstraint =
|
TemporalConstraint temporalConstraint =
|
||||||
new TemporalConstraint(startTime.getTimeInMillis(),
|
new TemporalConstraint(startTime.getTimeInMillis(),
|
||||||
endTime.getTimeInMillis(), AggregationMode.DAILY);
|
endTime.getTimeInMillis(), AggregationMode.MONTHLY);
|
||||||
|
|
||||||
Class<AggregatedJobUsageRecord> clz =
|
Class<AggregatedServiceUsageRecord> clz =
|
||||||
AggregatedJobUsageRecord.class;
|
AggregatedServiceUsageRecord.class;
|
||||||
|
|
||||||
|
|
||||||
|
List<Filter> filters = new ArrayList<Filter>();
|
||||||
|
//Filter filter = new Filter(ServiceUsageRecord.SERVICE_NAME, "DataMiner");
|
||||||
|
Filter filter = new Filter(ServiceUsageRecord.SERVICE_NAME, "RConnector");
|
||||||
|
filters.add(filter);
|
||||||
|
|
||||||
|
|
||||||
|
for(String context : contexts){
|
||||||
|
ScopeProvider.instance.set(context);
|
||||||
|
|
||||||
|
SortedSet<NumberedFilter> top = accountingPersistenceQueryCouchBase.getNextPossibleValuesWithMap(clz, temporalConstraint, filters, ServiceUsageRecord.CONSUMER_ID, null);
|
||||||
|
|
||||||
|
logger.info("Context : {} - Users [{}] : {}", context, top.size(), top);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTop() throws Exception {
|
||||||
|
Calendar startTime = Calendar.getInstance();
|
||||||
|
startTime.set(2015, Calendar.SEPTEMBER, 15);
|
||||||
|
|
||||||
|
logger.debug("StartTime {}", startTime.getTimeInMillis());
|
||||||
|
|
||||||
|
Calendar endTime = Calendar.getInstance();
|
||||||
|
endTime.set(2017, Calendar.JULY, 15);
|
||||||
|
|
||||||
|
logger.debug("EndTime {}", endTime.getTimeInMillis());
|
||||||
|
|
||||||
|
|
||||||
|
List<Filter> filters = new ArrayList<Filter>();
|
||||||
|
Filter filter = new Filter(ServiceUsageRecord.SERVICE_NAME, "DataMiner");
|
||||||
|
//Filter filter = new Filter(ServiceUsageRecord.SERVICE_NAME, "RConnector");
|
||||||
|
filters.add(filter);
|
||||||
|
|
||||||
|
|
||||||
|
TemporalConstraint temporalConstraint =
|
||||||
|
new TemporalConstraint(startTime.getTimeInMillis(),
|
||||||
|
endTime.getTimeInMillis(), AggregationMode.MONTHLY);
|
||||||
|
|
||||||
|
Class<AggregatedServiceUsageRecord> clz =
|
||||||
|
AggregatedServiceUsageRecord.class;
|
||||||
|
|
||||||
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> set =
|
SortedMap<NumberedFilter, SortedMap<Calendar, Info>> set =
|
||||||
accountingPersistenceQueryCouchBase.getTopValues(
|
accountingPersistenceQueryCouchBase.getTopValues(
|
||||||
clz, temporalConstraint, filters,
|
clz, temporalConstraint, filters,
|
||||||
AggregatedJobUsageRecord.CONSUMER_ID, null);
|
ServiceUsageRecord.CONSUMER_ID, null);
|
||||||
|
|
||||||
|
|
||||||
JobUsageRecord record =new JobUsageRecord();
|
logger.debug("Result {}", set);
|
||||||
Set<String> result=record.getRequiredFields();
|
|
||||||
logger.debug("result"+result.toString());
|
|
||||||
logger.debug("Result final{}", set);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -168,9 +202,11 @@ public class AccountingPersistenceQueryCouchBaseTest {
|
||||||
endTime.set(2017, Calendar.APRIL, 29,23,59);
|
endTime.set(2017, Calendar.APRIL, 29,23,59);
|
||||||
List<Filter> filters = new ArrayList<Filter>();
|
List<Filter> filters = new ArrayList<Filter>();
|
||||||
|
|
||||||
|
/*
|
||||||
Filter filter =
|
Filter filter =
|
||||||
new Filter(AggregatedServiceUsageRecord.CALLED_METHOD, "WebProcessingService");
|
new Filter(AggregatedServiceUsageRecord.CALLED_METHOD, "WebProcessingService");
|
||||||
//filters.add(filter);
|
filters.add(filter);
|
||||||
|
*/
|
||||||
TemporalConstraint temporalConstraint =
|
TemporalConstraint temporalConstraint =
|
||||||
new TemporalConstraint(startTime.getTimeInMillis(),
|
new TemporalConstraint(startTime.getTimeInMillis(),
|
||||||
endTime.getTimeInMillis(), AggregationMode.DAILY);
|
endTime.getTimeInMillis(), AggregationMode.DAILY);
|
||||||
|
@ -243,13 +279,13 @@ public class AccountingPersistenceQueryCouchBaseTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void getUsageValueQuotaTotal() throws Exception{
|
public void getUsageValueQuotaTotal() throws Exception{
|
||||||
|
|
||||||
|
String context = ScopedTest.getCurrentScope(DEFAULT_TEST_SCOPE);
|
||||||
|
|
||||||
Calendar startTime = Calendar.getInstance();
|
Calendar startTime = Calendar.getInstance();
|
||||||
startTime.set(2017, Calendar.MAY, 1);
|
startTime.set(2017, Calendar.MAY, 1);
|
||||||
Calendar endTime = Calendar.getInstance();
|
Calendar endTime = Calendar.getInstance();
|
||||||
|
|
||||||
|
|
||||||
ScopeProvider.instance.set("/gcube");
|
|
||||||
String context="/gcube";
|
|
||||||
TemporalConstraint temporalConstraint =
|
TemporalConstraint temporalConstraint =
|
||||||
new TemporalConstraint(startTime.getTimeInMillis(),
|
new TemporalConstraint(startTime.getTimeInMillis(),
|
||||||
endTime.getTimeInMillis(), AggregationMode.DAILY);
|
endTime.getTimeInMillis(), AggregationMode.DAILY);
|
||||||
|
@ -548,15 +584,15 @@ public class AccountingPersistenceQueryCouchBaseTest {
|
||||||
endTime.set(2016, Calendar.OCTOBER, 20,23,59);
|
endTime.set(2016, Calendar.OCTOBER, 20,23,59);
|
||||||
List<Filter> filters = new ArrayList<Filter>();
|
List<Filter> filters = new ArrayList<Filter>();
|
||||||
filters.add(new Filter(AggregatedServiceUsageRecord.CONSUMER_ID, "valentina.marioli"));
|
filters.add(new Filter(AggregatedServiceUsageRecord.CONSUMER_ID, "valentina.marioli"));
|
||||||
TemporalConstraint temporalConstraint =
|
////TemporalConstraint temporalConstraint =
|
||||||
new TemporalConstraint(startTime.getTimeInMillis(),
|
new TemporalConstraint(startTime.getTimeInMillis(),
|
||||||
endTime.getTimeInMillis(), AggregationMode.DAILY);
|
endTime.getTimeInMillis(), AggregationMode.DAILY);
|
||||||
String context="/gcube/devNext";
|
//String context="/gcube/devNext";
|
||||||
List <String>parameters=new ArrayList<String>();
|
List <String>parameters=new ArrayList<String>();
|
||||||
parameters.add("serviceClass");
|
parameters.add("serviceClass");
|
||||||
parameters.add("serviceName");
|
parameters.add("serviceName");
|
||||||
Class<AggregatedServiceUsageRecord> clz =
|
//Class<AggregatedServiceUsageRecord> clz =
|
||||||
AggregatedServiceUsageRecord.class;
|
// AggregatedServiceUsageRecord.class;
|
||||||
// SortedMap<String,Integer> result= accountingPersistenceQueryCouchBase.getListUsage(clz,temporalConstraint, filters,context,parameters);
|
// SortedMap<String,Integer> result= accountingPersistenceQueryCouchBase.getListUsage(clz,temporalConstraint, filters,context,parameters);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -588,7 +624,7 @@ public class AccountingPersistenceQueryCouchBaseTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetSpaceProvidersIds(){
|
public void testGetSpaceProvidersIds() throws Exception{
|
||||||
SortedSet<String> listProvidersId =
|
SortedSet<String> listProvidersId =
|
||||||
accountingPersistenceQueryCouchBase.getSpaceProvidersIds();
|
accountingPersistenceQueryCouchBase.getSpaceProvidersIds();
|
||||||
logger.debug("Result final{}", listProvidersId);
|
logger.debug("Result final{}", listProvidersId);
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.accounting.analytics.persistence.couchbase;
|
||||||
|
|
||||||
|
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.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ScopedTest {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ScopedTest.class);
|
||||||
|
|
||||||
|
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_DEVSEC_VARNAME = "GCUBE_DEVSEC";
|
||||||
|
public static final String GCUBE_DEVSEC;
|
||||||
|
|
||||||
|
public static final String GCUBE_DEVSEC_DEVVRE_VARNAME = "GCUBE_DEVSEC_DEVVRE";
|
||||||
|
public static final String GCUBE_DEVSEC_DEVVRE;
|
||||||
|
|
||||||
|
public static final String SA_VARNAME = "SA";
|
||||||
|
public static final String SA;
|
||||||
|
|
||||||
|
|
||||||
|
public static final String DEFAULT_TEST_SCOPE;
|
||||||
|
public static final String ALTERNATIVE_TEST_SCOPE;
|
||||||
|
|
||||||
|
static {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
InputStream input = ScopedTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// load the properties file
|
||||||
|
properties.load(input);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
GCUBE_DEVNEXT = properties.getProperty(GCUBE_DEVNEXT_VARNAME);
|
||||||
|
GCUBE_DEVNEXT_NEXTNEXT = properties.getProperty(GCUBE_DEVNEXT_NEXTNEXT_VARNAME);
|
||||||
|
|
||||||
|
GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME);
|
||||||
|
GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME);
|
||||||
|
|
||||||
|
SA = properties.getProperty(SA_VARNAME);
|
||||||
|
|
||||||
|
DEFAULT_TEST_SCOPE = SA;
|
||||||
|
ALTERNATIVE_TEST_SCOPE = GCUBE_DEVSEC;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 setContext(String token) throws ObjectNotFound, Exception{
|
||||||
|
SecurityTokenProvider.instance.set(token);
|
||||||
|
ScopeProvider.instance.set(getCurrentScope(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeClass() throws Exception{
|
||||||
|
setContext(DEFAULT_TEST_SCOPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterClass() throws Exception{
|
||||||
|
SecurityTokenProvider.instance.reset();
|
||||||
|
ScopeProvider.instance.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue