Add a new UsageRecord for Storage Volume
e.g. { "scope": "/gcube", "operationCount": 2, "dataCount": 2802, "dataServiceName": "dataServiceName", "endTime": 1480606453491, "consumerId": "alessandro.pieve", "startTime": 1480606453487, "id": "6e6808bf-3567-4ebf-93d3-0d55c97202a1", "dataVolume": 5104, "dataType": "STORAGE", "operationResult": "SUCCESS", "dataServiceId": "dataServiceId", "aggregated": true, "providerId": "testprotocol://providerURI", "creationTime": 1480606453494, "recordType": "StorageStatusRecord", "dataServiceClass": "dataServiceClass" } git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@141599 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
0d80ace47a
commit
a57a673874
2
pom.xml
2
pom.xml
|
@ -10,7 +10,7 @@
|
|||
|
||||
<groupId>org.gcube.accounting</groupId>
|
||||
<artifactId>accounting-lib</artifactId>
|
||||
<version>2.3.0-SNAPSHOT</version>
|
||||
<version>2.4.0-SNAPSHOT</version>
|
||||
<name>Accounting Library</name>
|
||||
<description>Accounting Library</description>
|
||||
<packaging>jar</packaging>
|
||||
|
|
|
@ -0,0 +1,164 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.accounting.datamodel.aggregation;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Calendar;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.accounting.datamodel.AggregatedUsageRecord;
|
||||
import org.gcube.accounting.datamodel.basetypes.AbstractStorageStatusRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecord;
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions;
|
||||
import org.gcube.documentstore.records.aggregation.AggregationUtility;
|
||||
import org.gcube.documentstore.records.implementation.AggregatedField;
|
||||
|
||||
/**
|
||||
* This Class is for library internal use only
|
||||
* @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it
|
||||
*/
|
||||
public class AggregatedStorageStatusRecord extends AbstractStorageStatusRecord implements AggregatedUsageRecord<AggregatedStorageStatusRecord, StorageStatusRecord> {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
*/
|
||||
private static final long serialVersionUID = 4683337274367137236L;
|
||||
|
||||
@AggregatedField
|
||||
public static final String DATA_VOLUME = AbstractStorageStatusRecord.DATA_VOLUME;
|
||||
|
||||
@AggregatedField
|
||||
public static final String DATA_COUNT = AbstractStorageStatusRecord.DATA_COUNT;
|
||||
|
||||
|
||||
public AggregatedStorageStatusRecord() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AggregatedStorageStatusRecord(Map<String, ? extends Serializable> properties) throws InvalidValueException{
|
||||
super(properties);
|
||||
}
|
||||
|
||||
public AggregatedStorageStatusRecord(StorageStatusRecord record) throws InvalidValueException{
|
||||
super(record.getResourceProperties());
|
||||
this.setOperationCount(1);
|
||||
Calendar creationTime = record.getCreationTime();
|
||||
this.setCreationTime(Calendar.getInstance());
|
||||
this.setStartTime(creationTime);
|
||||
this.setEndTime(creationTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOperationCount() {
|
||||
return super.getOperationCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOperationCount(int operationCount) throws InvalidValueException {
|
||||
super.setOperationCount(operationCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Calendar getStartTime() {
|
||||
return super.getStartTimeAsCalendar();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void setStartTime(Calendar startTime) throws InvalidValueException {
|
||||
super.setStartTime(startTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Calendar getEndTime() {
|
||||
return super.getEndTimeAsCalendar();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void setEndTime(Calendar endTime) throws InvalidValueException {
|
||||
super.setEndTime(endTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public AggregatedStorageStatusRecord aggregate(
|
||||
AggregatedStorageStatusRecord record)
|
||||
throws NotAggregatableRecordsExceptions {
|
||||
try {
|
||||
AggregationUtility<AggregatedStorageStatusRecord> aggregationUtility = new AggregationUtility<AggregatedStorageStatusRecord>(this);
|
||||
aggregationUtility.aggregate(record);
|
||||
this.setDataVolume(record.getDataVolume());
|
||||
this.setDataCount(record.getDataCount());
|
||||
|
||||
}catch(NotAggregatableRecordsExceptions e){
|
||||
throw e;
|
||||
} catch(Exception ex){
|
||||
throw new NotAggregatableRecordsExceptions(ex);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public AggregatedStorageStatusRecord aggregate(StorageStatusRecord record)
|
||||
throws NotAggregatableRecordsExceptions {
|
||||
try {
|
||||
return aggregate(new AggregatedStorageStatusRecord(record));
|
||||
} catch(NotAggregatableRecordsExceptions e){
|
||||
throw e;
|
||||
} catch(Exception ex){
|
||||
throw new NotAggregatableRecordsExceptions(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean isAggregable(AggregatedStorageStatusRecord record)
|
||||
throws NotAggregatableRecordsExceptions {
|
||||
AggregationUtility<AggregatedStorageStatusRecord> aggregationUtility = new AggregationUtility<AggregatedStorageStatusRecord>(this);
|
||||
return aggregationUtility.isAggregable(record);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean isAggregable(StorageStatusRecord record)
|
||||
throws NotAggregatableRecordsExceptions {
|
||||
try {
|
||||
return isAggregable(new AggregatedStorageStatusRecord(record));
|
||||
} catch(NotAggregatableRecordsExceptions e){
|
||||
throw e;
|
||||
} catch(Exception ex){
|
||||
throw new NotAggregatableRecordsExceptions(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Class<StorageStatusRecord> getAggregable() {
|
||||
return StorageStatusRecord.class;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.accounting.datamodel.basetypes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.accounting.datamodel.BasicUsageRecord;
|
||||
import org.gcube.accounting.datamodel.validations.annotations.FixDataVolumeSign;
|
||||
import org.gcube.accounting.datamodel.validations.annotations.ValidDataType;
|
||||
import org.gcube.accounting.datamodel.validations.annotations.ValidURI;
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
import org.gcube.documentstore.records.implementation.RequiredField;
|
||||
import org.gcube.documentstore.records.implementation.validations.annotations.NotEmpty;
|
||||
import org.gcube.documentstore.records.implementation.validations.annotations.ValidLong;
|
||||
|
||||
/**
|
||||
* @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractStorageStatusRecord extends BasicUsageRecord {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
*/
|
||||
private static final long serialVersionUID = -5754343539116896036L;
|
||||
|
||||
public enum DataType {
|
||||
STORAGE, TREE, GEO, DATABASE, OTHER
|
||||
}
|
||||
|
||||
/**
|
||||
* KEY for : Quantity of data in terms of KB
|
||||
*/
|
||||
@RequiredField @ValidLong @FixDataVolumeSign
|
||||
public static final String DATA_VOLUME = "dataVolume";
|
||||
|
||||
/**
|
||||
* KEY for : type of data accessed.
|
||||
* The value is a controlled dictionary by
|
||||
* {@link #StorageStatusRecord.DataType}
|
||||
*/
|
||||
@RequiredField @ValidDataType
|
||||
public static final String DATA_TYPE = "dataType";
|
||||
|
||||
/**
|
||||
* KEY for : data Count number of objects
|
||||
*/
|
||||
@RequiredField @NotEmpty
|
||||
public static final String DATA_COUNT = "dataCount";
|
||||
|
||||
/**
|
||||
* KEY for : data service class identifier
|
||||
*/
|
||||
@RequiredField @NotEmpty
|
||||
public static final String DATA_SERVICECLASS = "dataServiceClass";
|
||||
|
||||
/**
|
||||
* KEY for : data service name identifier
|
||||
*/
|
||||
@RequiredField @NotEmpty
|
||||
public static final String DATA_SERVICENAME = "dataServiceName";
|
||||
|
||||
/**
|
||||
* KEY for : data service name id
|
||||
*/
|
||||
@RequiredField @NotEmpty
|
||||
public static final String DATA_SERVICEID = "dataServiceId";
|
||||
|
||||
/**
|
||||
* KEY for : providerId the identifier of the provider which is the target of a read/write operation
|
||||
*/
|
||||
@RequiredField @ValidURI
|
||||
public static final String PROVIDER_ID = "providerId";
|
||||
|
||||
|
||||
public AbstractStorageStatusRecord() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AbstractStorageStatusRecord(Map<String, ? extends Serializable> properties) throws InvalidValueException {
|
||||
super(properties);
|
||||
}
|
||||
|
||||
private static final String ABSTRACT_TO_REPLACE = "Abstract";
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected String giveMeRecordType() {
|
||||
return AbstractStorageStatusRecord.class.getSimpleName().replace(ABSTRACT_TO_REPLACE, "");
|
||||
}
|
||||
|
||||
public long getDataVolume() {
|
||||
return (Long) this.resourceProperties.get(DATA_VOLUME);
|
||||
}
|
||||
|
||||
public void setDataVolume(long dataVolume) throws InvalidValueException {
|
||||
setResourceProperty(DATA_VOLUME, dataVolume);
|
||||
}
|
||||
|
||||
public DataType getDataType() {
|
||||
return (DataType) this.resourceProperties.get(DATA_TYPE);
|
||||
}
|
||||
|
||||
public void setDataType(DataType dataType) throws InvalidValueException {
|
||||
setResourceProperty(DATA_TYPE, dataType);
|
||||
}
|
||||
|
||||
public long getDataCount() {
|
||||
return (Long) this.resourceProperties.get(DATA_COUNT);
|
||||
}
|
||||
|
||||
public void setDataCount(long dataCount) throws InvalidValueException {
|
||||
setResourceProperty(DATA_COUNT, dataCount);
|
||||
}
|
||||
|
||||
public String getDataServiceClass() {
|
||||
return (String) this.resourceProperties.get(DATA_SERVICECLASS);
|
||||
}
|
||||
|
||||
public void setDataServiceClass(String dataServiceClass) throws InvalidValueException {
|
||||
setResourceProperty(DATA_SERVICECLASS, dataServiceClass);
|
||||
}
|
||||
|
||||
public String getDataServiceName() {
|
||||
return (String) this.resourceProperties.get(DATA_SERVICENAME);
|
||||
}
|
||||
|
||||
public void setDataServiceName(String dataServiceName) throws InvalidValueException {
|
||||
setResourceProperty(DATA_SERVICENAME, dataServiceName);
|
||||
}
|
||||
|
||||
public String getDataServiceId() {
|
||||
return (String) this.resourceProperties.get(DATA_SERVICEID);
|
||||
}
|
||||
|
||||
public void setDataServiceId(String dataServiceId) throws InvalidValueException {
|
||||
setResourceProperty(DATA_SERVICEID, dataServiceId);
|
||||
}
|
||||
|
||||
public URI getProviderId() {
|
||||
return (URI) this.resourceProperties.get(PROVIDER_ID);
|
||||
}
|
||||
|
||||
public void setProviderId(URI provideId) throws InvalidValueException {
|
||||
setResourceProperty(PROVIDER_ID, provideId);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package org.gcube.accounting.datamodel.usagerecords;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.accounting.datamodel.basetypes.AbstractStorageStatusRecord;
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
/**
|
||||
* @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it
|
||||
*
|
||||
*/
|
||||
public class StorageStatusRecord extends AbstractStorageStatusRecord {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
*/
|
||||
private static final long serialVersionUID = 8174719617939936365L;
|
||||
|
||||
public StorageStatusRecord() {
|
||||
super();
|
||||
}
|
||||
|
||||
public StorageStatusRecord(Map<String, ? extends Serializable> properties) throws InvalidValueException {
|
||||
super(properties);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,168 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.accounting.datamodel.aggregation;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecordTest;
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions;
|
||||
import org.gcube.testutility.ScopedTest;
|
||||
import org.gcube.testutility.TestUsageRecord;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it
|
||||
*
|
||||
*/
|
||||
public class AggregatedStorageStatusRecordTest extends ScopedTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AggregatedStorageStatusRecordTest.class);
|
||||
|
||||
@Test
|
||||
public void testRequiredFields() throws InvalidValueException {
|
||||
StorageStatusRecord storageVolumeUsageRecord = TestUsageRecord.createTestStorageVolumeUsageRecord();
|
||||
Assert.assertTrue(storageVolumeUsageRecord.getScope()==null);
|
||||
storageVolumeUsageRecord.setScope(TestUsageRecord.TEST_SCOPE);
|
||||
|
||||
AggregatedStorageStatusRecord aggregatedStorageUsageRecord = new AggregatedStorageStatusRecord(storageVolumeUsageRecord);
|
||||
|
||||
Set<String> expectedRequiredFields = StorageStatusRecordTest.getExpectedRequiredFields();
|
||||
expectedRequiredFields.addAll(AggregatedUsageRecordTest.getExpectedRequiredFields());
|
||||
|
||||
logger.debug("Expected Required Fields : {}", expectedRequiredFields);
|
||||
|
||||
Set<String> gotRequiredFields = aggregatedStorageUsageRecord.getRequiredFields();
|
||||
|
||||
logger.debug("Got Required Fields : {}", gotRequiredFields);
|
||||
|
||||
Assert.assertTrue(expectedRequiredFields.containsAll(gotRequiredFields));
|
||||
Assert.assertTrue(gotRequiredFields.containsAll(expectedRequiredFields));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void secondAsNotAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
|
||||
StorageStatusRecord storageVolumeUsageRecord = TestUsageRecord.createTestStorageVolumeUsageRecord();
|
||||
Assert.assertTrue(storageVolumeUsageRecord.getScope()==null);
|
||||
storageVolumeUsageRecord.setScope(TestUsageRecord.TEST_SCOPE);
|
||||
|
||||
storageVolumeUsageRecord.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
|
||||
storageVolumeUsageRecord.validate();
|
||||
logger.debug("StorageVolumeUsageRecord : {}", storageVolumeUsageRecord);
|
||||
|
||||
AggregatedStorageStatusRecord aggregated = new AggregatedStorageStatusRecord(storageVolumeUsageRecord);
|
||||
logger.debug("StorageVolumeUsageRecord Converted to Aggregated: {}", aggregated);
|
||||
aggregated.validate();
|
||||
|
||||
StorageStatusRecord storageVolumeUsageRecord2 = TestUsageRecord.createTestStorageVolumeUsageRecord();
|
||||
storageVolumeUsageRecord2.setScope(TestUsageRecord.TEST_SCOPE);
|
||||
|
||||
storageVolumeUsageRecord2.validate();
|
||||
logger.debug("StorageVolumeUsageRecord 2 : {}", storageVolumeUsageRecord2);
|
||||
|
||||
|
||||
long secondDataVolume = storageVolumeUsageRecord2.getDataVolume();
|
||||
|
||||
long secondDataCount = storageVolumeUsageRecord2.getDataCount();
|
||||
|
||||
aggregated.aggregate(storageVolumeUsageRecord2);
|
||||
logger.debug("Resulting Aggregated StorageVolumeUsageRecord: {}", aggregated);
|
||||
aggregated.validate();
|
||||
|
||||
Assert.assertTrue(aggregated.getDataVolume() == (secondDataVolume));
|
||||
Assert.assertTrue(aggregated.getDataCount() == (secondDataCount));
|
||||
Assert.assertTrue(aggregated.getOperationCount() == 2);
|
||||
Assert.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void secondAsAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
|
||||
StorageStatusRecord storageStatusUsageRecord = TestUsageRecord.createTestStorageVolumeUsageRecord();
|
||||
Assert.assertTrue(storageStatusUsageRecord.getScope()==null);
|
||||
storageStatusUsageRecord.setScope(TestUsageRecord.TEST_SCOPE);
|
||||
storageStatusUsageRecord.validate();
|
||||
logger.debug("StorageVolumeUsageRecord : {}", storageStatusUsageRecord);
|
||||
|
||||
AggregatedStorageStatusRecord aggregated = new AggregatedStorageStatusRecord(storageStatusUsageRecord);
|
||||
logger.debug("StorageVolumeUsageRecord Converted to Aggregated: {}", aggregated);
|
||||
aggregated.validate();
|
||||
|
||||
StorageStatusRecord storageVolumeUsageRecord2 = TestUsageRecord.createTestStorageVolumeUsageRecord();
|
||||
Assert.assertTrue(storageVolumeUsageRecord2.getScope()==null);
|
||||
storageVolumeUsageRecord2.setScope(TestUsageRecord.TEST_SCOPE);
|
||||
storageVolumeUsageRecord2.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
|
||||
storageVolumeUsageRecord2.validate();
|
||||
logger.debug("StorageVolumeUsageRecord 2 : {}", storageVolumeUsageRecord2);
|
||||
AggregatedStorageStatusRecord converted = new AggregatedStorageStatusRecord(storageVolumeUsageRecord2);
|
||||
logger.debug("StorageVolumeUsageRecord Converted to Aggregated: {}", converted);
|
||||
converted.validate();
|
||||
|
||||
long firstDataVolume = aggregated.getDataVolume();
|
||||
long secondDataVolume = converted.getDataVolume();
|
||||
|
||||
long secondDataCount =storageVolumeUsageRecord2.getDataCount();
|
||||
|
||||
aggregated.aggregate(converted);
|
||||
logger.debug("Resulting Aggregated StorageVolumeUsageRecord: {}", aggregated);
|
||||
aggregated.validate();
|
||||
|
||||
Assert.assertTrue(aggregated.getDataVolume() == (secondDataVolume));
|
||||
Assert.assertTrue(aggregated.getDataCount() == (secondDataCount));
|
||||
Assert.assertTrue(aggregated.getOperationCount() == 2);
|
||||
Assert.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aggregationStressTest() throws InvalidValueException, NotAggregatableRecordsExceptions {
|
||||
StorageStatusRecord storageVolumeUsageRecord = TestUsageRecord.createTestStorageVolumeUsageRecord();
|
||||
Assert.assertTrue(storageVolumeUsageRecord.getScope()==null);
|
||||
storageVolumeUsageRecord.setScope(TestUsageRecord.TEST_SCOPE);
|
||||
storageVolumeUsageRecord.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
|
||||
storageVolumeUsageRecord.validate();
|
||||
logger.debug("StorageVolumeUsageRecord : {}", storageVolumeUsageRecord);
|
||||
|
||||
AggregatedStorageStatusRecord aggregated = new AggregatedStorageStatusRecord(storageVolumeUsageRecord);
|
||||
logger.debug("StorageVolumeUsageRecord Converted to Aggregated: {}", aggregated);
|
||||
aggregated.validate();
|
||||
|
||||
for(int i=2; i<10; i++){
|
||||
|
||||
StorageStatusRecord sur = TestUsageRecord.createTestStorageVolumeUsageRecord();
|
||||
sur.setScope(TestUsageRecord.TEST_SCOPE);
|
||||
sur.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
|
||||
sur.validate();
|
||||
logger.debug("Cycle StorageVolumeUsageRecord {}: {}", i, sur);
|
||||
|
||||
//long oldDataVolume = aggregated.getDataVolume();
|
||||
long newDataVolume = sur.getDataVolume();
|
||||
|
||||
|
||||
//long oldDataCount = aggregated.getDataCount();
|
||||
long newDataCount = sur.getDataCount();
|
||||
|
||||
aggregated.aggregate(sur);
|
||||
logger.debug("Resulting Aggregated StorageUsageRecord : {}", aggregated);
|
||||
aggregated.validate();
|
||||
|
||||
Assert.assertTrue(aggregated.getDataVolume() == (newDataVolume));
|
||||
Assert.assertTrue(aggregated.getDataCount() == (newDataCount));
|
||||
Assert.assertTrue(aggregated.getOperationCount() == i);
|
||||
Assert.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME));
|
||||
}
|
||||
|
||||
logger.debug("Resulting Aggregated StorageUsageRecord: {}", aggregated);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -148,4 +148,9 @@ public class AggregatedStorageUsageRecordTest extends ScopedTest {
|
|||
logger.debug("Resulting Aggregated StorageUsageRecord: {}", aggregated);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
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.AbstractStorageStatusRecord;
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
import org.gcube.documentstore.records.Record;
|
||||
import org.gcube.testutility.ScopedTest;
|
||||
import org.gcube.testutility.TestUsageRecord;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Alessandro Pieve (ISTI - CNR) alessandro.pieve@isti.cnr.it
|
||||
*
|
||||
*/
|
||||
public class StorageStatusRecordTest extends ScopedTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(StorageStatusRecordTest.class);
|
||||
|
||||
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(AbstractStorageStatusRecord.DATA_VOLUME);
|
||||
expectedRequiredFields.add(AbstractStorageStatusRecord.DATA_COUNT);
|
||||
expectedRequiredFields.add(AbstractStorageStatusRecord.DATA_TYPE);
|
||||
expectedRequiredFields.add(AbstractStorageStatusRecord.DATA_SERVICECLASS);
|
||||
expectedRequiredFields.add(AbstractStorageStatusRecord.DATA_SERVICENAME);
|
||||
expectedRequiredFields.add(AbstractStorageStatusRecord.DATA_SERVICEID);
|
||||
expectedRequiredFields.add(AbstractStorageStatusRecord.PROVIDER_ID);
|
||||
|
||||
return expectedRequiredFields;
|
||||
}
|
||||
|
||||
@Test(expected=InvalidValueException.class)
|
||||
public void scopeNotSetValidationError() throws InvalidValueException {
|
||||
StorageStatusRecord usageRecord = TestUsageRecord.createTestStorageVolumeUsageRecord();
|
||||
usageRecord.validate();
|
||||
logger.debug("{}", usageRecord);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRequiredFields() throws InvalidValueException{
|
||||
StorageStatusRecord usageRecord = TestUsageRecord.createTestStorageVolumeUsageRecord();
|
||||
Assert.assertTrue(usageRecord.getScope()==null);
|
||||
usageRecord.setScope(TestUsageRecord.TEST_SCOPE);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -12,10 +12,12 @@ import java.util.UUID;
|
|||
|
||||
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
|
||||
import org.gcube.accounting.datamodel.basetypes.AbstractStorageUsageRecord;
|
||||
import org.gcube.accounting.datamodel.basetypes.AbstractStorageStatusRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.PortletUsageRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.StorageStatusRecord;
|
||||
import org.gcube.accounting.datamodel.usagerecords.TaskUsageRecord;
|
||||
import org.gcube.accounting.persistence.AccountingPersistenceFactory;
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
|
@ -29,42 +31,43 @@ import org.slf4j.LoggerFactory;
|
|||
public class TestUsageRecord {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TestUsageRecord.class);
|
||||
|
||||
|
||||
public final static String TEST_CONSUMER_ID = "name.surname";
|
||||
public final static String TEST_SCOPE = "/infrastructure/vo";
|
||||
public final static String TEST_SCOPE_2 = "/infrastructure/vo/vre";
|
||||
public final static OperationResult TEST_OPERATION_RESULT = OperationResult.SUCCESS;
|
||||
|
||||
|
||||
public final static String TEST_SERVICE_CLASS = "TestServiceClass";
|
||||
public final static String TEST_SERVICE_NAME = "TestServiceName";
|
||||
public final static String TEST_CALLED_METHOD = "TestCalledMethod";
|
||||
public final static String TEST_CALLER_QUALIFIER = "TestCallerQualifier";
|
||||
|
||||
|
||||
public final static String TEST_CALLER_HOST = "remotehost";
|
||||
public final static String TEST_HOST = "localhost";
|
||||
|
||||
|
||||
public final static String TEST_PROPERTY_NAME = "TestPropertyName";
|
||||
public final static String TEST_PROPERTY_VALUE = "TestPropertyValue";
|
||||
|
||||
|
||||
public final static String TEST_JOB_ID = UUID.randomUUID().toString();
|
||||
public final static String TEST_JOB_NAME = "TestJobName";
|
||||
public final static int TEST_VMS_USED = 2;
|
||||
public final static String TEST_JOB_QUALIFIER = "TestJobQualifier";
|
||||
public final static long HALF_DURATION = 10 * 60 * 1000; // 10 min
|
||||
|
||||
|
||||
public final static String TEST_TASK_ID = UUID.randomUUID().toString();
|
||||
public final static String TEST_NESTED_MAP = "TestNestedMap";
|
||||
|
||||
|
||||
public final static String TEST_PORTLET_ID = "TestPortlet";
|
||||
public final static String TEST_PORTLET_OPERATION_ID = "TestPortletOperationID";
|
||||
public final static String TEST_PORTLET_MESSAGE = "TestPortletMessage";
|
||||
|
||||
|
||||
private final static long MIN_DURATION = 60; // millisec
|
||||
private final static long MAX_DURATION = 1000; // millisec
|
||||
|
||||
|
||||
static {
|
||||
AccountingPersistenceFactory.initAccountingPackages();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate A Random long in a range between min and max.
|
||||
* This function is internally used to set random duration.
|
||||
|
@ -73,7 +76,7 @@ public class TestUsageRecord {
|
|||
public static long generateRandomLong(long min, long max){
|
||||
return min + (int)(Math.random() * ((max - min) + 1));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a valid #ServiceUsageRecord with scope set automatically.
|
||||
* @return the created #ServiceUsageRecord
|
||||
|
@ -83,29 +86,29 @@ public class TestUsageRecord {
|
|||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
|
||||
usageRecord.setCallerHost(TEST_CALLER_HOST);
|
||||
usageRecord.setHost(TEST_HOST);
|
||||
usageRecord.setCallerQualifier(TEST_CALLER_QUALIFIER);
|
||||
usageRecord.setServiceClass(TEST_SERVICE_CLASS);
|
||||
usageRecord.setServiceName(TEST_SERVICE_NAME);
|
||||
usageRecord.setCalledMethod(TEST_CALLED_METHOD);
|
||||
|
||||
|
||||
usageRecord.setDuration(generateRandomLong(MIN_DURATION, MAX_DURATION));
|
||||
|
||||
|
||||
} catch (InvalidValueException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return usageRecord;
|
||||
|
||||
|
||||
}
|
||||
public final static String TEST_RESOUCE_OWNER = "resource.owner";
|
||||
public final static String TEST_RESOUCE_SCOPE = TEST_SCOPE;
|
||||
|
||||
|
||||
public final static String TEST_RESOURCE_URI = "testprotocol://objectURI";
|
||||
public final static String TEST_PROVIDER_URI = "testprotocol://providerURI";
|
||||
|
||||
|
||||
private final static long MIN_DATA_VOLUME = 1024;
|
||||
private final static long MAX_DATA_VOLUME = 10240;
|
||||
|
||||
|
@ -118,18 +121,18 @@ public class TestUsageRecord {
|
|||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
|
||||
usageRecord.setResourceOwner(TEST_RESOUCE_OWNER);
|
||||
usageRecord.setResourceScope(TEST_RESOUCE_SCOPE);
|
||||
|
||||
|
||||
usageRecord.setResourceURI(new URI(TEST_RESOURCE_URI));
|
||||
usageRecord.setProviderURI(new URI(TEST_PROVIDER_URI));
|
||||
|
||||
|
||||
usageRecord.setOperationType(AbstractStorageUsageRecord.OperationType.READ);
|
||||
usageRecord.setDataType(AbstractStorageUsageRecord.DataType.STORAGE);
|
||||
|
||||
|
||||
usageRecord.setDataVolume(generateRandomLong(MIN_DATA_VOLUME, MAX_DATA_VOLUME));
|
||||
|
||||
|
||||
usageRecord.setQualifier("image/png");
|
||||
|
||||
|
||||
|
@ -138,103 +141,134 @@ public class TestUsageRecord {
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
return usageRecord;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a valid #StorageVolumeUsageRecord with scope set automatically.
|
||||
* @return the created #StorageVolumeUsageRecord
|
||||
*/
|
||||
public static StorageStatusRecord createTestStorageVolumeUsageRecord() {
|
||||
StorageStatusRecord usageRecord = new StorageStatusRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
usageRecord.setDataVolume(generateRandomLong(MIN_DATA_VOLUME, MAX_DATA_VOLUME));
|
||||
usageRecord.setDataType(AbstractStorageStatusRecord.DataType.STORAGE);
|
||||
usageRecord.setDataCount(generateRandomLong(MIN_DATA_VOLUME, MAX_DATA_VOLUME));
|
||||
usageRecord.setDataServiceClass("dataServiceClass");
|
||||
usageRecord.setDataServiceName("dataServiceName");
|
||||
usageRecord.setDataServiceId("dataServiceId");
|
||||
usageRecord.setProviderId(new URI(TEST_PROVIDER_URI));
|
||||
|
||||
|
||||
|
||||
} catch (InvalidValueException | URISyntaxException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return usageRecord;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public static JobUsageRecord createTestJobUsageRecord() {
|
||||
|
||||
|
||||
JobUsageRecord usageRecord = new JobUsageRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
|
||||
usageRecord.setJobId(TEST_JOB_ID);
|
||||
usageRecord.setJobName(TEST_JOB_NAME);
|
||||
Calendar startTime = Calendar.getInstance();
|
||||
Calendar endTime = Calendar.getInstance();
|
||||
endTime.setTimeInMillis(startTime.getTimeInMillis() + HALF_DURATION);
|
||||
startTime.setTimeInMillis(startTime.getTimeInMillis() - HALF_DURATION);
|
||||
|
||||
|
||||
usageRecord.setJobStartTime(startTime);
|
||||
usageRecord.setJobEndTime(endTime);
|
||||
|
||||
|
||||
} catch (InvalidValueException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
}
|
||||
|
||||
|
||||
return usageRecord;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public static TaskUsageRecord createTestTaskUsageRecord() {
|
||||
|
||||
|
||||
TaskUsageRecord usageRecord = new TaskUsageRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
|
||||
usageRecord.setTaskId(TEST_TASK_ID);
|
||||
usageRecord.setTaskId(TEST_JOB_ID);
|
||||
usageRecord.setHost(TEST_HOST);
|
||||
|
||||
|
||||
usageRecord.setRefHostingNodeId(UUID.randomUUID().toString());
|
||||
|
||||
|
||||
Calendar startTime = Calendar.getInstance();
|
||||
Calendar endTime = Calendar.getInstance();
|
||||
endTime.setTimeInMillis(startTime.getTimeInMillis() + HALF_DURATION);
|
||||
startTime.setTimeInMillis(startTime.getTimeInMillis() - HALF_DURATION);
|
||||
|
||||
|
||||
usageRecord.setTaskStartTime(startTime);
|
||||
usageRecord.setTaskEndTime(endTime);
|
||||
|
||||
|
||||
HashMap<String, Serializable> inputParameters = new HashMap<>();
|
||||
inputParameters.put(TEST_PROPERTY_NAME, TEST_PROPERTY_VALUE);
|
||||
inputParameters.put(TEST_PROPERTY_VALUE, TEST_PROPERTY_NAME);
|
||||
|
||||
|
||||
HashMap<String, Serializable> parameter = new HashMap<>();
|
||||
parameter.put(TEST_PROPERTY_NAME, TEST_PROPERTY_VALUE);
|
||||
parameter.put(TEST_PROPERTY_VALUE, TEST_PROPERTY_NAME);
|
||||
|
||||
|
||||
inputParameters.put(TEST_NESTED_MAP, parameter);
|
||||
|
||||
|
||||
usageRecord.setInputParameters(inputParameters);
|
||||
|
||||
|
||||
} catch (InvalidValueException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
}
|
||||
|
||||
|
||||
return usageRecord;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public static PortletUsageRecord createTestPortletUsageRecord() {
|
||||
|
||||
|
||||
PortletUsageRecord usageRecord = new PortletUsageRecord();
|
||||
try {
|
||||
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
||||
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
|
||||
|
||||
|
||||
Calendar startTime = Calendar.getInstance();
|
||||
Calendar endTime = Calendar.getInstance();
|
||||
endTime.setTimeInMillis(startTime.getTimeInMillis() + HALF_DURATION);
|
||||
startTime.setTimeInMillis(startTime.getTimeInMillis() - HALF_DURATION);
|
||||
|
||||
|
||||
usageRecord.setPortletId(TEST_PORTLET_ID);
|
||||
usageRecord.setOperationId(TEST_PORTLET_OPERATION_ID);
|
||||
usageRecord.setMessage(TEST_PORTLET_MESSAGE);
|
||||
|
||||
|
||||
} catch (InvalidValueException e) {
|
||||
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
||||
}
|
||||
|
||||
|
||||
return usageRecord;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue