refs #2222: Move infrastructure tests outside of components junit tests

https://support.d4science.org/issues/2222

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/infrastructure-tests@124045 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-02-10 14:15:01 +00:00
parent bc5fdad7fd
commit b118d3e7ca
15 changed files with 1475 additions and 2 deletions

79
pom.xml
View File

@ -5,9 +5,84 @@
<artifactId>maven-parent</artifactId> <artifactId>maven-parent</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
</parent> </parent>
<groupId>org.gcube.tools</groupId>
<artifactId>infrastructure-tests</artifactId> <artifactId>infrastructure-tests</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<name>Infrastructure Tests</name> <name>Infrastructure Tests</name>
<description>Tests involving infrastrcuture interaction</description> <description>Tests involving infrastructure interaction</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<distroDirectory>${project.basedir}/distro</distroDirectory>
</properties>
<scm>
<connection>scm:https://svn.d4science.research-infrastructures.eu/gcube//private/luca.frosini/${project.artifactId}</connection>
<developerConnection>scm:https://svn.d4science.research-infrastructures.eu/gcube//private/luca.frosini/${project.artifactId}</developerConnection>
<url>https://svn.d4science.research-infrastructures.eu/gcube//private/luca.frosini/${project.artifactId}</url>
</scm>
<dependencies>
<dependency>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-analytics</artifactId>
<version>[1.2.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-analytics-persistence-couchdb</artifactId>
<version>[1.2.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-analytics-persistence-couchbase</artifactId>
<version>[1.2.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.data.publishing</groupId>
<artifactId>document-store-lib</artifactId>
<version>[1.1.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-lib</artifactId>
<version>[2.1.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

View File

@ -0,0 +1,64 @@
/**
*
*/
package org.gcube.accounting.analytics.persistence;
import org.gcube.accounting.analytics.persistence.couchbase.AccountingPersistenceQueryCouchBase;
import org.gcube.accounting.analytics.persistence.couchdb.AccountingPersistenceQueryCouchDB;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.accounting.persistence.AccountingPersistenceConfiguration;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class AccountingPersistenceQueryConfigurationTest {
private static Logger logger = LoggerFactory.getLogger(AccountingPersistenceQueryConfigurationTest.class);
@Before
public void before(){
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after(){
SecurityTokenProvider.instance.reset();
}
@Test
public void test() throws Exception {
AccountingPersistenceBackendQueryConfiguration acbqc =
new AccountingPersistenceBackendQueryConfiguration(AccountingPersistenceQueryCouchDB.class);
logger.debug("{}", acbqc);
}
@Test
public void test() throws Exception {
AccountingPersistenceBackendQueryConfiguration acbqc = new AccountingPersistenceBackendQueryConfiguration(AccountingPersistenceQueryCouchBase.class);
logger.debug("{}", acbqc);
}
@Test
public void getUsernamePasswordForScopes() throws Exception{
try {
AccountingPersistenceBackendQueryConfiguration persitenceConfiguration = new AccountingPersistenceBackendQueryConfiguration(AccountingPersistenceQueryCouchDB.class);
String uri = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.URL_PROPERTY_KEY);
String username = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.USERNAME_PROPERTY_KEY);
String password = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.PASSWORD_PROPERTY_KEY);
logger.debug("{} {} - {} : {}", BasicUsageRecord.getScopeFromToken(), uri, username, password);
}catch(IndexOutOfBoundsException e){
logger.debug("No AccountingPersistenceConfiguration : \n {} {} \n\n", e.getClass().getName(), e.getMessage());
} catch(Exception e){
logger.error("Error getting AccountingPersistenceConfiguration", e);
throw e;
}
}
}

View File

@ -0,0 +1,260 @@
/**
*
*/
package org.gcube.accounting.analytics.persistence;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.ektorp.DocumentNotFoundException;
import org.gcube.accounting.analytics.Filter;
import org.gcube.accounting.analytics.Info;
import org.gcube.accounting.analytics.ResourceRecordQuery;
import org.gcube.accounting.analytics.TemporalConstraint;
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
import org.gcube.accounting.analytics.exception.NoAvailableScopeException;
import org.gcube.accounting.analytics.exception.NoUsableAccountingPersistenceQueryFound;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class AccountingPersistenceQueryFactoryTest {
private static Logger logger = LoggerFactory.getLogger(AccountingPersistenceQueryFactoryTest.class);
protected AccountingPersistenceBackendQuery apq;
@Before
public void before() throws NoAvailableScopeException, NoUsableAccountingPersistenceQueryFound{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
apq = AccountingPersistenceBackendQueryFactory.getInstance();
}
@After
public void after(){
apq = null;
SecurityTokenProvider.instance.reset();
}
@Test
public void testNullFilters() throws Exception {
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(startTime.getTimeInMillis()-(1000*60*60*24*3));
Calendar endTime = Calendar.getInstance();
TemporalConstraint temporalConstraint = new TemporalConstraint(startTime.getTimeInMillis(), endTime.getTimeInMillis(), AggregationMode.SECONDLY);
Map<Calendar, Info> infos = apq.query(AggregatedServiceUsageRecord.class, temporalConstraint, null);
Assert.assertTrue(infos!=null);
for(Info info : infos.values()){
logger.debug(info.toString());
}
}
@Test
public void testEmptyFilters() throws Exception {
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(startTime.getTimeInMillis()-(1000*60*60*24*3));
Calendar endTime = Calendar.getInstance();
TemporalConstraint temporalConstraint = new TemporalConstraint(startTime.getTimeInMillis(), endTime.getTimeInMillis(), AggregationMode.SECONDLY);
List<Filter> filters = new ArrayList<Filter>();
Map<Calendar, Info> infos = apq.query(AggregatedServiceUsageRecord.class, temporalConstraint, filters);
Assert.assertTrue(infos!=null);
for(Info info : infos.values()){
logger.debug(info.toString());
}
}
@Test(expected=DocumentNotFoundException.class)
public void testFakeFilters() throws Exception {
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(startTime.getTimeInMillis()-(1000*60*60*24*3));
Calendar endTime = Calendar.getInstance();
TemporalConstraint temporalConstraint = new TemporalConstraint(startTime.getTimeInMillis(), endTime.getTimeInMillis(), AggregationMode.SECONDLY);
List<Filter> filters = new ArrayList<Filter>();
filters.add(new Filter("AUX", "AUX"));
apq.query(AggregatedServiceUsageRecord.class, temporalConstraint, filters);
}
@Test
public void testFiltersGoodKeyFakeValue() throws Exception {
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(startTime.getTimeInMillis()-(1000*60*60*24*3));
Calendar endTime = Calendar.getInstance();
TemporalConstraint temporalConstraint = new TemporalConstraint(startTime.getTimeInMillis(), endTime.getTimeInMillis(), AggregationMode.SECONDLY);
List<Filter> filters = new ArrayList<Filter>();
filters.add(new Filter(ServiceUsageRecord.SERVICE_CLASS, "AUX"));
Map<Calendar, Info> infos = apq.query(AggregatedServiceUsageRecord.class, temporalConstraint, filters);
Assert.assertTrue(infos!=null);
Assert.assertTrue(infos.isEmpty());
}
public static final long timeout = 5000;
public static final TimeUnit timeUnit = TimeUnit.MILLISECONDS;
@Test
public void testFiltersGoodKeyGoodValue() throws Exception {
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(startTime.getTimeInMillis()-(1000*60*60*24*365));
Calendar endTime = Calendar.getInstance();
TemporalConstraint temporalConstraint = new TemporalConstraint(startTime.getTimeInMillis(), endTime.getTimeInMillis(), AggregationMode.SECONDLY);
List<Filter> filters = new ArrayList<Filter>();
filters.add(new Filter(ServiceUsageRecord.SERVICE_CLASS, TestUsageRecord.TEST_SERVICE_CLASS));
Map<Calendar, Info> infos = apq.query(AggregatedServiceUsageRecord.class, temporalConstraint, filters);
Assert.assertTrue(infos!=null);
Assert.assertTrue(!infos.isEmpty());
for(Info info : infos.values()){
logger.debug(info.toString());
}
filters = new ArrayList<Filter>();
filters.add(new Filter(StorageUsageRecord.RESOURCE_OWNER, TestUsageRecord.TEST_RESOUCE_OWNER));
infos = apq.query(AggregatedStorageUsageRecord.class, temporalConstraint, filters);
Assert.assertTrue(infos!=null);
Assert.assertTrue(!infos.isEmpty());
for(Info info : infos.values()){
logger.debug(info.toString());
}
}
//@ Test
public void testRange() throws Exception {
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.MONTH, Calendar.AUGUST);
startTime.set(Calendar.DAY_OF_MONTH, 7);
Calendar endTime = Calendar.getInstance();
endTime.set(Calendar.MONTH, Calendar.SEPTEMBER);
endTime.set(Calendar.DAY_OF_MONTH, 7);
TemporalConstraint temporalConstraint = new TemporalConstraint(startTime.getTimeInMillis(), endTime.getTimeInMillis(), AggregationMode.DAILY);
logger.trace("{} : {}", TemporalConstraint.class.getSimpleName(), temporalConstraint.toString());
List<Filter> filters = new ArrayList<Filter>();
/*
filters.add(new Filter(ServiceUsageRecord.SERVICE_CLASS, TestUsageRecord.TEST_SERVICE_CLASS));
Map<Calendar, Info> infos = apq.query(AggregatedServiceUsageRecord.class, temporalConstraint, filters);
Assert.assertTrue(infos!=null);
Assert.assertTrue(!infos.isEmpty());
for(Info info : infos.values()){
logger.debug(info.toString());
}
*/
filters = new ArrayList<Filter>();
filters.add(new Filter(UsageRecord.CONSUMER_ID, "gianpaolo.coro"));
AccountingPersistenceBackendQuery apq = AccountingPersistenceBackendQueryFactory.getInstance();
Map<Calendar, Info> infos = apq.query(AggregatedStorageUsageRecord.class, temporalConstraint, filters);
Assert.assertTrue(infos!=null);
Assert.assertTrue(!infos.isEmpty());
for(Info info : infos.values()){
logger.debug(info.toString());
}
ResourceRecordQuery resourceRecordQuery = new ResourceRecordQuery();
List<Info> padded = resourceRecordQuery.getInfo(AggregatedStorageUsageRecord.class, temporalConstraint, filters, true);
Assert.assertTrue(padded!=null);
Assert.assertTrue(!padded.isEmpty());
for(Info info : padded){
logger.debug(info.toString());
}
}
@Test
public void getKeysTest() throws Exception {
Set<String> keys = apq.getKeys(AggregatedServiceUsageRecord.class);
logger.debug("Got keys : {}", keys);
Set<String> required = AggregatedServiceUsageRecord.class.newInstance().getRequiredFields();
logger.debug("Required fields : {}", required);
Assert.assertTrue(required.containsAll(keys));
}
@Test
public void getPossibileValuesTest() throws Exception {
@SuppressWarnings("rawtypes")
Class[] classes = new Class[]{
AggregatedServiceUsageRecord.class,
AggregatedStorageUsageRecord.class
};
for(@SuppressWarnings("rawtypes") Class cls : classes){
@SuppressWarnings("unchecked")
Set<String> keys = apq.getKeys(cls);
for(String key : keys){
logger.debug("Querying Possible values of {} for key {}", cls.getSimpleName(), key);
@SuppressWarnings("unchecked")
Set<String> values = apq.getPossibleValuesForKey(cls, key);
logger.debug("Possible values of {} for key {} : {}", cls.getSimpleName(), key, values);
}
}
}
@Test
public void testMontly() throws Exception {
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.MONTH, Calendar.JANUARY);
startTime.set(Calendar.DAY_OF_MONTH, 1);
Calendar endTime = Calendar.getInstance();
endTime.set(Calendar.MONTH, Calendar.DECEMBER);
endTime.set(Calendar.DAY_OF_MONTH, 31);
TemporalConstraint temporalConstraint = new TemporalConstraint(startTime.getTimeInMillis(), endTime.getTimeInMillis(), AggregationMode.MONTHLY);
logger.trace("{} : {}", TemporalConstraint.class.getSimpleName(), temporalConstraint.toString());
List<Filter> filters = new ArrayList<Filter>();
AccountingPersistenceBackendQuery apq = AccountingPersistenceBackendQueryFactory.getInstance();
Map<Calendar, Info> infos = apq.query(AggregatedStorageUsageRecord.class, temporalConstraint, filters);
Assert.assertTrue(infos!=null);
Assert.assertTrue(!infos.isEmpty());
for(Info info : infos.values()){
logger.debug(info.toString());
}
ResourceRecordQuery resourceRecordQuery = new ResourceRecordQuery();
List<Info> padded = resourceRecordQuery.getInfo(AggregatedStorageUsageRecord.class, temporalConstraint, filters, true);
Assert.assertTrue(padded!=null);
Assert.assertTrue(!padded.isEmpty());
for(Info info : padded){
logger.debug(info.toString());
}
}
}

View File

@ -0,0 +1,14 @@
/**
*
*/
package org.gcube.accounting.analytics.persistence;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class TestUtility {
public static final String TOKEN = "";
}

View File

@ -0,0 +1,95 @@
/**
*
*/
package org.gcube.accounting.datamodel.usagerecords;
import java.util.HashSet;
import java.util.Set;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.AbstractJobUsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.testutility.TestUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.Record;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class JobUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(JobUsageRecordTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after() throws Exception{
SecurityTokenProvider.instance.reset();
}
public static Set<String> getExpectedRequiredFields(){
Set<String> expectedRequiredFields = new HashSet<String>();
expectedRequiredFields.add(Record.ID);
expectedRequiredFields.add(UsageRecord.CONSUMER_ID);
expectedRequiredFields.add(UsageRecord.CREATION_TIME);
expectedRequiredFields.add(UsageRecord.RECORD_TYPE);
expectedRequiredFields.add(UsageRecord.SCOPE);
expectedRequiredFields.add(UsageRecord.OPERATION_RESULT);
expectedRequiredFields.add(AbstractJobUsageRecord.JOB_ID);
expectedRequiredFields.add(AbstractJobUsageRecord.JOB_START_TIME);
expectedRequiredFields.add(AbstractJobUsageRecord.JOB_END_TIME);
return expectedRequiredFields;
}
@Test(expected=InvalidValueException.class)
public void scopeNotSetValidationError() throws InvalidValueException {
JobUsageRecord usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
usageRecord.validate();
logger.debug("{}", usageRecord);
}
@Test
public void testRequiredFields() throws InvalidValueException{
JobUsageRecord usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
Set<String> expectedRequiredFields = getExpectedRequiredFields();
logger.debug("Expected Required Fields : {}", expectedRequiredFields);
Set<String> gotRequiredFields = usageRecord.getRequiredFields();
logger.debug("Got Required Fields : {}", gotRequiredFields);
Assert.assertTrue(expectedRequiredFields.containsAll(gotRequiredFields));
Assert.assertTrue(gotRequiredFields.containsAll(expectedRequiredFields));
usageRecord.validate();
logger.debug("{}", usageRecord);
}
@Test
public void testRequiredFieldsWithScopeSetExplicitely() throws InvalidValueException{
JobUsageRecord usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
Assert.assertTrue(usageRecord.getScope()==null);
usageRecord.setScope(TestUsageRecord.TEST_SCOPE);
Set<String> expectedRequiredFields = getExpectedRequiredFields();
Set<String> gotRequiredFields = usageRecord.getRequiredFields();
Assert.assertTrue(expectedRequiredFields.containsAll(gotRequiredFields));
Assert.assertTrue(gotRequiredFields.containsAll(expectedRequiredFields));
usageRecord.validate();
logger.debug("{}", usageRecord);
}
}

View File

@ -0,0 +1,100 @@
/**
*
*/
package org.gcube.accounting.datamodel.usagerecords;
import java.util.HashSet;
import java.util.Set;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.AbstractServiceUsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.testutility.TestUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.Record;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class ServiceUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(ServiceUsageRecordTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after() throws Exception{
SecurityTokenProvider.instance.reset();
}
public static Set<String> getExpectedRequiredFields(){
Set<String> expectedRequiredFields = new HashSet<String>();
expectedRequiredFields.add(Record.ID);
expectedRequiredFields.add(UsageRecord.CONSUMER_ID);
expectedRequiredFields.add(UsageRecord.CREATION_TIME);
expectedRequiredFields.add(UsageRecord.RECORD_TYPE);
expectedRequiredFields.add(UsageRecord.SCOPE);
expectedRequiredFields.add(UsageRecord.OPERATION_RESULT);
expectedRequiredFields.add(AbstractServiceUsageRecord.CALLER_HOST);
expectedRequiredFields.add(AbstractServiceUsageRecord.HOST);
expectedRequiredFields.add(AbstractServiceUsageRecord.SERVICE_CLASS);
expectedRequiredFields.add(AbstractServiceUsageRecord.SERVICE_NAME);
expectedRequiredFields.add(AbstractServiceUsageRecord.CALLED_METHOD);
expectedRequiredFields.add(AbstractServiceUsageRecord.DURATION);
return expectedRequiredFields;
}
@Test(expected=InvalidValueException.class)
public void scopeNotSetValidationError() throws InvalidValueException {
SecurityTokenProvider.instance.reset();
ServiceUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
usageRecord.validate();
logger.debug("{}", usageRecord);
}
@Test
public void testRequiredFields() throws InvalidValueException{
ServiceUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
Set<String> expectedRequiredFields = getExpectedRequiredFields();
logger.debug("Expected Required Fields : {}", expectedRequiredFields);
Set<String> gotRequiredFields = usageRecord.getRequiredFields();
logger.debug("Got Required Fields : {}", gotRequiredFields);
Assert.assertTrue(expectedRequiredFields.containsAll(gotRequiredFields));
Assert.assertTrue(gotRequiredFields.containsAll(expectedRequiredFields));
usageRecord.validate();
logger.debug("{}", usageRecord);
}
@Test
public void testRequiredFieldsWithScopeSetExplicitely() throws InvalidValueException{
SecurityTokenProvider.instance.reset();
ServiceUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
Assert.assertTrue(usageRecord.getScope()==null);
usageRecord.setScope(TestUsageRecord.TEST_SCOPE);
Set<String> expectedRequiredFields = getExpectedRequiredFields();
Set<String> gotRequiredFields = usageRecord.getRequiredFields();
Assert.assertTrue(expectedRequiredFields.containsAll(gotRequiredFields));
Assert.assertTrue(gotRequiredFields.containsAll(expectedRequiredFields));
usageRecord.validate();
logger.debug("{}", usageRecord);
}
}

View File

@ -0,0 +1,102 @@
/**
*
*/
package org.gcube.accounting.datamodel.usagerecords;
import java.util.HashSet;
import java.util.Set;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.AbstractStorageUsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.testutility.TestUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.Record;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class StorageUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(StorageUsageRecordTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after() throws Exception{
SecurityTokenProvider.instance.reset();
}
public static Set<String> getExpectedRequiredFields(){
Set<String> expectedRequiredFields = new HashSet<String>();
expectedRequiredFields.add(Record.ID);
expectedRequiredFields.add(UsageRecord.CONSUMER_ID);
expectedRequiredFields.add(UsageRecord.CREATION_TIME);
expectedRequiredFields.add(UsageRecord.RECORD_TYPE);
expectedRequiredFields.add(UsageRecord.SCOPE);
expectedRequiredFields.add(UsageRecord.OPERATION_RESULT);
expectedRequiredFields.add(AbstractStorageUsageRecord.RESOURCE_OWNER);
expectedRequiredFields.add(AbstractStorageUsageRecord.RESOURCE_SCOPE);
expectedRequiredFields.add(AbstractStorageUsageRecord.RESOURCE_URI);
expectedRequiredFields.add(AbstractStorageUsageRecord.PROVIDER_URI);
expectedRequiredFields.add(AbstractStorageUsageRecord.OPERATION_TYPE);
expectedRequiredFields.add(AbstractStorageUsageRecord.DATA_TYPE);
expectedRequiredFields.add(AbstractStorageUsageRecord.DATA_VOLUME);
return expectedRequiredFields;
}
@Test(expected=InvalidValueException.class)
public void scopeNotSetValidationError() throws InvalidValueException {
SecurityTokenProvider.instance.reset();
StorageUsageRecord usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
usageRecord.validate();
logger.debug("{}", usageRecord);
}
@Test
public void testRequiredFields() throws InvalidValueException{
StorageUsageRecord usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
Set<String> expectedRequiredFields = getExpectedRequiredFields();
logger.debug("Expected Required Fields : {}", expectedRequiredFields);
Set<String> gotRequiredFields = usageRecord.getRequiredFields();
logger.debug("Got Required Fields : {}", gotRequiredFields);
Assert.assertTrue(expectedRequiredFields.containsAll(gotRequiredFields));
Assert.assertTrue(gotRequiredFields.containsAll(expectedRequiredFields));
usageRecord.validate();
logger.debug("{}", usageRecord);
}
@Test
public void testRequiredFieldsWithScopeSetExplicitely() throws InvalidValueException{
SecurityTokenProvider.instance.reset();
StorageUsageRecord usageRecord = TestUsageRecord.createTestStorageUsageRecordExplicitScope();
Assert.assertTrue(usageRecord.getScope()==null);
usageRecord.setScope(TestUsageRecord.TEST_SCOPE);
Set<String> expectedRequiredFields = getExpectedRequiredFields();
Set<String> gotRequiredFields = usageRecord.getRequiredFields();
Assert.assertTrue(expectedRequiredFields.containsAll(gotRequiredFields));
Assert.assertTrue(gotRequiredFields.containsAll(expectedRequiredFields));
usageRecord.validate();
logger.debug("{}", usageRecord);
}
}

View File

@ -0,0 +1,97 @@
/**
*
*/
package org.gcube.accounting.datamodel.usagerecords;
import java.util.HashSet;
import java.util.Set;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.AbstractTaskUsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.testutility.TestUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.Record;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class TaskUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(TaskUsageRecordTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after() throws Exception{
SecurityTokenProvider.instance.reset();
}
public static Set<String> getExpectedRequiredFields(){
Set<String> expectedRequiredFields = new HashSet<String>();
expectedRequiredFields.add(Record.ID);
expectedRequiredFields.add(UsageRecord.CONSUMER_ID);
expectedRequiredFields.add(UsageRecord.CREATION_TIME);
expectedRequiredFields.add(UsageRecord.RECORD_TYPE);
expectedRequiredFields.add(UsageRecord.SCOPE);
expectedRequiredFields.add(UsageRecord.OPERATION_RESULT);
expectedRequiredFields.add(AbstractTaskUsageRecord.TASK_ID);
expectedRequiredFields.add(AbstractTaskUsageRecord.TASK_START_TIME);
expectedRequiredFields.add(AbstractTaskUsageRecord.TASK_END_TIME);
return expectedRequiredFields;
}
@Test(expected=InvalidValueException.class)
public void scopeNotSetValidationError() throws InvalidValueException {
SecurityTokenProvider.instance.reset();
TaskUsageRecord usageRecord = TestUsageRecord.createTestTaskUsageRecordAutomaticScope();
usageRecord.validate();
logger.debug("{}", usageRecord);
}
@Test
public void testRequiredFields() throws InvalidValueException{
TaskUsageRecord usageRecord = TestUsageRecord.createTestTaskUsageRecordAutomaticScope();
Set<String> expectedRequiredFields = getExpectedRequiredFields();
logger.debug("Expected Required Fields : {}", expectedRequiredFields);
Set<String> gotRequiredFields = usageRecord.getRequiredFields();
logger.debug("Got Required Fields : {}", gotRequiredFields);
Assert.assertTrue(expectedRequiredFields.containsAll(gotRequiredFields));
Assert.assertTrue(gotRequiredFields.containsAll(expectedRequiredFields));
usageRecord.validate();
logger.debug("{}", usageRecord);
}
@Test
public void testRequiredFieldsWithScopeSetExplicitely() throws InvalidValueException{
SecurityTokenProvider.instance.reset();
TaskUsageRecord usageRecord = TestUsageRecord.createTestTaskUsageRecordExplicitScope();
Assert.assertTrue(usageRecord.getScope()==null);
usageRecord.setScope(TestUsageRecord.TEST_SCOPE);
Set<String> expectedRequiredFields = getExpectedRequiredFields();
Set<String> gotRequiredFields = usageRecord.getRequiredFields();
Assert.assertTrue(expectedRequiredFields.containsAll(gotRequiredFields));
Assert.assertTrue(gotRequiredFields.containsAll(expectedRequiredFields));
usageRecord.validate();
logger.debug("{}", usageRecord);
}
}

View File

@ -0,0 +1,95 @@
/**
*
*/
package org.gcube.documentstore.persistence;
import java.util.concurrent.TimeUnit;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.testutility.StressTestUtility;
import org.gcube.accounting.testutility.TestOperation;
import org.gcube.accounting.testutility.TestUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class PersistenceBackendMonitorTest {
private static final Logger logger = LoggerFactory.getLogger(PersistenceBackendMonitorTest.class);
public static final long timeout = 5000;
public static final TimeUnit timeUnit = TimeUnit.MILLISECONDS;
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after() throws Exception{
SecurityTokenProvider.instance.reset();
}
@Test
public void parsingTest() throws Exception {
PersistenceBackendFactory.setFallbackLocation(null);
final PersistenceBackend persistence = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) {
UsageRecord usageRecord = null;
switch (i%3) {
case 0:
usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
break;
case 1:
usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
break;
case 2:
usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
break;
}
try {
persistence.account(usageRecord);
} catch (InvalidValueException e) {
throw new RuntimeException(e);
}
}
});
logger.debug(" START -----------------------------------------------");
logger.debug("Flushing the buffered records");
persistence.flush(timeout, timeUnit);
logger.debug(" END -----------------------------------------------");
PersistenceBackend persistenceBackend = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
persistenceBackend.setFallback((FallbackPersistenceBackend) persistenceBackend);
PersistenceBackendMonitor temporalDataPersistenceBackendMonitor = new PersistenceBackendMonitor(persistenceBackend);
temporalDataPersistenceBackendMonitor.run();
}
@Test
public void singleParsingTest() throws Exception {
PersistenceBackendFactory.setFallbackLocation(null);
PersistenceBackend persistenceBackend = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
persistenceBackend.setFallback((FallbackPersistenceBackend) persistenceBackend);
PersistenceBackendMonitor temporalDataPersistenceBackendMonitor = new PersistenceBackendMonitor(persistenceBackend);
temporalDataPersistenceBackendMonitor.run();
}
}

View File

@ -0,0 +1,143 @@
/**
*
*/
package org.gcube.documentstore.persistence;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.testutility.StressTestUtility;
import org.gcube.accounting.testutility.TestOperation;
import org.gcube.accounting.testutility.TestUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class PersistenceBackendTest {
private static Logger logger = LoggerFactory.getLogger(PersistenceBackendTest.class);
public static final String[] SCOPES = new String[]{"/gcube", "/gcube/devNext"};
public static final String GCUBE_SCOPE = SCOPES[0];
public static final String GCUBE_DEVNEXT_SCOPE = SCOPES[1];
public static final long timeout = 5000;
public static final TimeUnit timeUnit = TimeUnit.MILLISECONDS;
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after() throws Exception{
SecurityTokenProvider.instance.reset();
}
public static PersistenceBackend getPersistence(){
PersistenceBackendFactory.setFallbackLocation(null);
return PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
}
@Test
public void singleTestNoScope() throws Exception {
PersistenceBackendFactory.setFallbackLocation(null);
final PersistenceBackend persistence = PersistenceBackendFactory.getPersistenceBackend(null);
Assert.assertTrue(persistence instanceof FallbackPersistenceBackend);
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) {
UsageRecord usageRecord;
try {
usageRecord = TestUsageRecord.createTestServiceUsageRecordExplicitScope();
persistence.accountValidateAggregate(usageRecord, true, false);
} catch (InvalidValueException e) {
throw new RuntimeException(e);
}
}
}, 1);
persistence.flush(timeout, timeUnit);
}
@Test
public void singleTest() throws Exception {
final PersistenceBackend persistence = getPersistence();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) {
UsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
persistence.accountValidateAggregate(usageRecord, true, false);
}
}, 1);
persistence.flush(timeout, timeUnit);
}
@Test
public void stressTestNoAggregation() throws Exception {
final PersistenceBackend persistence = getPersistence();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) {
UsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
persistence.accountValidateAggregate(usageRecord, true, false);
}
});
}
@Test
public void stressTestWithAggregation() throws Exception {
final PersistenceBackend persistence = getPersistence();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) throws Exception {
UsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
persistence.account(usageRecord);
}
});
persistence.flush(timeout, timeUnit);
}
@Test
public void testScopeRecheck() throws Exception {
PersistenceBackendFactory.setFallbackLocation(null);
logger.debug("Going to check First Time");
PersistenceBackend first = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
logger.debug("First {} : {}", PersistenceBackend.class.getSimpleName(), first);
Long firstCheck = PersistenceBackendFactory.getFallbackLastCheck(BasicUsageRecord.getScopeFromToken());
logger.debug("First Check Time {}", firstCheck);
long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime;
while(endTime <= (startTime + (PersistenceBackendFactory.FALLBACK_RETRY_TIME + 100))){
endTime = Calendar.getInstance().getTimeInMillis();
}
logger.debug("Going to check Second Time");
PersistenceBackend second = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
logger.debug("Second {} : {}", PersistenceBackend.class.getSimpleName(), second);
Long secondCheck = PersistenceBackendFactory.getFallbackLastCheck(BasicUsageRecord.getScopeFromToken());
logger.debug("Second Check Time {}", secondCheck);
Assert.assertNotEquals(firstCheck, secondCheck);
Assert.assertEquals(first, second);
}
}

View File

@ -0,0 +1,180 @@
/**
*
*/
package org.gcube.documentstore.records.aggregation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
import org.gcube.accounting.testutility.StressTestUtility;
import org.gcube.accounting.testutility.TestOperation;
import org.gcube.accounting.testutility.TestUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.persistence.PersistenceExecutor;
import org.gcube.documentstore.records.AggregatedRecord;
import org.gcube.documentstore.records.Record;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class AggregationSchedulerTest {
private static final Logger logger = LoggerFactory.getLogger(AggregationSchedulerTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after() throws Exception{
SecurityTokenProvider.instance.reset();
}
public static AggregationScheduler getAggregationScheduler(){
return AggregationScheduler.newInstance();
}
public static PersistenceExecutor persistenceExecutor = new PersistenceExecutor(){
@Override
public void persist(Record... records) throws Exception {
for(Record record : records){
logger.debug("Storing : {}", record.toString());
}
}
};
protected void madeAssertion(Map<String, List<Record>> bufferedRecords, List<String> types, int size, int count) {
for(String type : types){
Assert.assertTrue(bufferedRecords.containsKey(type));
List<Record> records = bufferedRecords.get(type);
Assert.assertTrue(records.size()==size);
for(Record record : records){
Assert.assertTrue(record.getRecordType().compareTo(type)==0);
Assert.assertTrue(record instanceof AggregatedRecord);
@SuppressWarnings("rawtypes")
AggregatedRecord aggregatedRecord = (AggregatedRecord) record;
Assert.assertTrue(aggregatedRecord.getOperationCount()==count);
}
}
}
@Test
public void stressTestAggregableURSingleType() throws Exception {
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) throws Exception {
UsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
aggregationScheduler.aggregate(usageRecord, persistenceExecutor);
}
});
List<String> types = new ArrayList<String>();
String serviceUsageRecordtype = TestUsageRecord.createTestServiceUsageRecordAutomaticScope().getRecordType();
types.add(serviceUsageRecordtype);
madeAssertion(aggregationScheduler.bufferedRecords, types, 1, StressTestUtility.DEFAULT_NUMBER_OF_RECORDS);
aggregationScheduler.flush(persistenceExecutor);
}
public static final String ALTERNATIVE_SERVICE_CLASS = "AlternativeServiceClass";
@Test
public void stressTestDifferentAggregableURSingleType() throws Exception {
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) throws Exception {
ServiceUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
if(i%2==0){
usageRecord.setServiceClass(ALTERNATIVE_SERVICE_CLASS);
}
aggregationScheduler.aggregate(usageRecord, persistenceExecutor);
}
});
List<String> types = new ArrayList<String>();
String serviceUsageRecordtype = TestUsageRecord.createTestServiceUsageRecordAutomaticScope().getRecordType();
types.add(serviceUsageRecordtype);
madeAssertion(aggregationScheduler.bufferedRecords, types, 2, StressTestUtility.DEFAULT_NUMBER_OF_RECORDS/2);
aggregationScheduler.flush(persistenceExecutor);
}
@Test
public void stressTestDifferentAggregableURTwoType() throws Exception {
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) throws Exception {
UsageRecord usageRecord;
if(i%2==0){
usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
}else{
usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
}
aggregationScheduler.aggregate(usageRecord, persistenceExecutor);
}
});
List<String> types = new ArrayList<String>();
String serviceUsageRecordtype = TestUsageRecord.createTestServiceUsageRecordAutomaticScope().getRecordType();
String storageUsageRecordtype = TestUsageRecord.createTestStorageUsageRecordAutomaticScope().getRecordType();
types.add(serviceUsageRecordtype);
types.add(storageUsageRecordtype);
madeAssertion(aggregationScheduler.bufferedRecords, types, 1, StressTestUtility.DEFAULT_NUMBER_OF_RECORDS/2);
aggregationScheduler.flush(persistenceExecutor);
}
@Test
public void stressTestDifferentAggregableURMultipleType() throws Exception {
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) throws Exception {
UsageRecord usageRecord;
switch (i%3) {
case 0:
usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
break;
case 1:
usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
break;
case 2:
usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
break;
default:
usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
}
aggregationScheduler.aggregate(usageRecord, persistenceExecutor);
}
});
/*
List<String> types = new ArrayList<String>();
String serviceUsageRecordtype = TestUsageRecord.createTestServiceUsageRecordAutomaticScope().getRecordType();
String storageUsageRecordtype = TestUsageRecord.createTestStorageUsageRecordAutomaticScope().getRecordType();
types.add(serviceUsageRecordtype);
types.add(storageUsageRecordtype);
madeAssertion(aggregationScheduler.bufferedRecords, types, 1, StressTestUtility.DEFAULT_NUMBER_OF_RECORDS/4);
*/
aggregationScheduler.flush(persistenceExecutor);
}
}

View File

@ -0,0 +1,170 @@
/**
*
*/
package org.gcube.accounting.persistence;
import java.io.StringWriter;
import java.net.URL;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.accounting.testutility.TestUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Profile;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
import org.gcube.common.resources.gcore.common.Platform;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.documentstore.persistence.PersistenceBackend;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class AccountingPersistenceConfigurationTest {
private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceConfigurationTest.class);
private static final String PROFILE_DESCRIPTION = "This ServiceEndpoint contains the parameter to connect to DB to persist log accounting";
private static final String HOSTED_ON = "pc-frosini.isti.cnr.it";
private static final String ENDPOINT = "http://localhost:5984";
private static final String READY = "READY";
private static final String PLATFORM_NAME = "Platform Name";
private static final String TEST_VERSION = "1.0.0";
private static final short[] VERSION_SLICES = new short[]{1,6,0,0};
private static final String DESCRIPTION = "Persistence Configuration Test";
private static final String FAKE_USERNAME = "fakeusername";
private static final String FAKE_PASSWORD = "fakepassword";
private static final String DB_NAME_PROPERTY_KEY = "dbName";
private static final String DB_NAME_PROPERTY_VALUE = "accounting";
private abstract class AccountingPersistenceFakeDB extends PersistenceBackend {
}
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
@After
public void after(){
SecurityTokenProvider.instance.reset();
}
/**
* Create the Service Endpoint using information related to discovered
* available plugins and their own discovered capabilities
* @return the created {@link ServiceEndpoint}
*/
protected static ServiceEndpoint createServiceEndpoint(){
logger.debug("Getting Available Plugins and their own supported capabilities");
logger.debug("Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities");
ServiceEndpoint serviceEndpoint = new ServiceEndpoint();
Profile profile = serviceEndpoint.newProfile();
profile.category(AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY);
profile.name(AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME);
profile.version(TEST_VERSION);
profile.description(PROFILE_DESCRIPTION);
Platform platform = profile.newPlatform();
platform.name(PLATFORM_NAME);
platform.version(VERSION_SLICES[0]);
platform.minorVersion(VERSION_SLICES[1]);
platform.buildVersion(VERSION_SLICES[2]);
platform.revisionVersion(VERSION_SLICES[3]);
Runtime runtime = profile.newRuntime();
runtime.hostedOn(HOSTED_ON);
runtime.status(READY);
Group<AccessPoint> accessPoints = profile.accessPoints();
AccessPoint accessPointElement = new AccessPoint();
accessPoints.add(accessPointElement);
accessPointElement.description(DESCRIPTION);
accessPointElement.credentials(FAKE_USERNAME, FAKE_PASSWORD);
accessPointElement.address(ENDPOINT);
accessPointElement.name(AccountingPersistenceFakeDB.class.getSimpleName());
Group<Property> properties = accessPointElement.properties();
Property dbName = new Property();
dbName.nameAndValue(DB_NAME_PROPERTY_KEY, DB_NAME_PROPERTY_VALUE);
dbName.encrypted(false);
properties.add(dbName);
StringWriter stringWriter = new StringWriter();
Resources.marshal(serviceEndpoint, stringWriter);
logger.debug("The created ServiceEndpoint profile is\n{}", stringWriter.toString());
return serviceEndpoint;
}
public void testPersistenceConfigurationFromIS() throws Exception{
boolean createResource = true;
ServiceEndpoint serviceEndpoint = null;
if(createResource){
serviceEndpoint = createServiceEndpoint();
TestUtility.publishResource(serviceEndpoint);
}
Thread.sleep(5000); // Waiting 5 sec
try {
AccountingPersistenceConfiguration persitenceConfiguration = new AccountingPersistenceConfiguration(AccountingPersistenceFakeDB.class);
if(createResource){
String uri = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.URL_PROPERTY_KEY);
Assert.assertTrue(uri.compareTo(new URL(ENDPOINT).toString())==0);
String username = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.USERNAME_PROPERTY_KEY);
Assert.assertTrue(username.compareTo(FAKE_USERNAME)==0);
String password = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.PASSWORD_PROPERTY_KEY);
Assert.assertTrue(password.compareTo(FAKE_PASSWORD)==0);
String dbName = persitenceConfiguration.getProperty(DB_NAME_PROPERTY_KEY);
Assert.assertTrue(dbName.compareTo(DB_NAME_PROPERTY_VALUE)==0);
}
} finally {
if(createResource){
TestUtility.unPublishResource(serviceEndpoint);
}
}
}
public void getUsernamePasswordForScopes() throws Exception{
logger.debug("START ======================================================");
try {
AccountingPersistenceConfiguration persitenceConfiguration = new AccountingPersistenceConfiguration(AccountingPersistenceFakeDB.class);
String uri = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.URL_PROPERTY_KEY);
String username = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.USERNAME_PROPERTY_KEY);
String password = persitenceConfiguration.getProperty(AccountingPersistenceConfiguration.PASSWORD_PROPERTY_KEY);
logger.debug("{} - {} - {} - {}", BasicUsageRecord.getScopeFromToken(), uri, username, password);
}catch(IndexOutOfBoundsException e){
logger.debug("No AccountingPersistenceConfiguration : \n {} {} \n\n", e.getClass().getName(), e.getMessage());
} catch(Exception e){
logger.error("Error getting AccountingPersistenceConfiguration", e);
throw e;
} finally {
logger.debug(" END ======================================================");
}
}
}

View File

@ -0,0 +1,40 @@
/**
*
*/
package org.gcube.accounting.testutility;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class StressTestUtility {
private static final Logger logger = LoggerFactory.getLogger(StressTestUtility.class);
protected final static int DEFAULT_NUMBER_OF_RECORDS = 3000;
public static void stressTest(TestOperation operation) throws Exception {
stressTest(operation, DEFAULT_NUMBER_OF_RECORDS);
}
public static void stressTest(TestOperation operation, int runs) throws Exception {
Calendar startTestTime = new GregorianCalendar();
for(int i=0; i< runs; i++){
operation.operate(i);
}
Calendar stopTestTime = new GregorianCalendar();
double startMillis = startTestTime.getTimeInMillis();
double stopMillis = stopTestTime.getTimeInMillis();
double duration = stopMillis - startMillis;
double average = (duration/runs);
logger.debug("Duration (in millisec) : " + duration);
logger.debug("Average (in millisec) : " + average);
}
}

View File

@ -0,0 +1,15 @@
/**
*
*/
package org.gcube.accounting.testutility;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public interface TestOperation {
public void operate(int i) throws Exception;
}

View File

@ -0,0 +1,23 @@
package org.gcube.utils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public abstract class IOUtility {
public static String readFile(String filePath) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
reader.close();
return stringBuilder.toString();
}
}