2015-06-25 14:30:50 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2015-07-02 12:01:59 +02:00
|
|
|
package org.gcube.accounting.datamodel.basetypes;
|
2015-06-25 14:30:50 +02:00
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
2015-06-25 14:30:50 +02:00
|
|
|
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
|
2015-07-01 16:40:49 +02:00
|
|
|
import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord;
|
2015-07-01 12:50:46 +02:00
|
|
|
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
|
|
|
|
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
|
2015-06-25 14:30:50 +02:00
|
|
|
import org.gcube.accounting.exception.InvalidValueException;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
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_SERVICE_CLASS = "TestServiceClass";
|
|
|
|
public final static String TEST_SERVICE_NAME = "TestServiceName";
|
2015-07-13 11:26:50 +02:00
|
|
|
public final static String TEST_CALLED_METHOD = "TestCalledMethod";
|
2015-06-25 14:30:50 +02:00
|
|
|
|
2015-06-26 15:50:39 +02:00
|
|
|
public final static String TEST_CALLER_HOST = "remotehost";
|
|
|
|
public final static String TEST_HOST = "localhost";
|
2015-06-25 14:30:50 +02:00
|
|
|
|
|
|
|
public final static String TEST_PROPERTY_NAME = "TestPropertyName";
|
|
|
|
public final static String TEST_PROPERTY_VALUE = "TestPropertyValue";
|
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
private final static long MIN_DURATION = 60; // millisec
|
|
|
|
private final static long MAX_DURATION = 1000; // millisec
|
2015-06-25 14:30:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate A Random long in a range between min and max.
|
|
|
|
* This function is internally used to set random duration.
|
|
|
|
* @return the generated random long
|
|
|
|
*/
|
|
|
|
public static long generateRandomLong(long min, long max){
|
|
|
|
return min + (int)(Math.random() * ((max - min) + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-25 17:41:56 +02:00
|
|
|
* Create a valid #ServiceUsageRecord
|
|
|
|
* @return the created #ServiceUsageRecord
|
2015-07-20 17:02:43 +02:00
|
|
|
* @throws InvalidValueException
|
2015-06-25 14:30:50 +02:00
|
|
|
*/
|
2015-07-20 17:02:43 +02:00
|
|
|
public static ServiceUsageRecord createTestServiceUsageRecordExplicitScope() throws InvalidValueException {
|
|
|
|
ServiceUsageRecord usageRecord = createTestServiceUsageRecordAutomaticScope();
|
|
|
|
try {
|
|
|
|
usageRecord.setScope(TEST_SCOPE);
|
|
|
|
} catch (InvalidValueException e) {
|
2015-07-22 17:28:30 +02:00
|
|
|
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Setting the Scope", e);
|
2015-07-20 17:02:43 +02:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return usageRecord;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a valid #ServiceUsageRecord with scope set automatically.
|
|
|
|
* @return the created #ServiceUsageRecord
|
|
|
|
*/
|
|
|
|
public static ServiceUsageRecord createTestServiceUsageRecordAutomaticScope() {
|
2015-06-25 17:41:56 +02:00
|
|
|
ServiceUsageRecord usageRecord = new ServiceUsageRecord();
|
2015-06-25 14:30:50 +02:00
|
|
|
try {
|
2015-06-25 17:41:56 +02:00
|
|
|
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
|
|
|
usageRecord.setOperationResult(OperationResult.SUCCESS);
|
2015-06-25 14:30:50 +02:00
|
|
|
|
2015-06-26 15:50:39 +02:00
|
|
|
usageRecord.setCallerHost(TEST_CALLER_HOST);
|
|
|
|
usageRecord.setHost(TEST_HOST);
|
2015-06-25 14:30:50 +02:00
|
|
|
|
2015-06-26 15:50:39 +02:00
|
|
|
usageRecord.setServiceClass(TEST_SERVICE_CLASS);
|
2015-07-13 11:26:50 +02:00
|
|
|
usageRecord.setServiceName(TEST_SERVICE_NAME);
|
|
|
|
usageRecord.setCalledMethod(TEST_CALLED_METHOD);
|
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
usageRecord.setDuration(generateRandomLong(MIN_DURATION, MAX_DURATION));
|
2015-06-25 14:30:50 +02:00
|
|
|
|
2015-07-01 16:40:49 +02:00
|
|
|
|
2015-06-25 14:30:50 +02:00
|
|
|
|
|
|
|
} catch (InvalidValueException e) {
|
2015-07-22 17:28:30 +02:00
|
|
|
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
|
|
|
throw new RuntimeException(e);
|
2015-06-25 14:30:50 +02:00
|
|
|
}
|
2015-06-25 17:41:56 +02:00
|
|
|
return usageRecord;
|
2015-06-25 14:30:50 +02:00
|
|
|
|
|
|
|
}
|
2015-06-25 17:41:56 +02:00
|
|
|
public final static String TEST_RESOUCE_OWNER = "resource.owner";
|
|
|
|
public final static String TEST_RESOUCE_SCOPE = TEST_SCOPE;
|
|
|
|
|
2015-06-26 15:50:39 +02:00
|
|
|
public final static String TEST_RESOURCE_URI = "testprotocol://objectURI";
|
|
|
|
public final static String TEST_PROVIDER_URI = "testprotocol://providerURI";
|
2015-06-25 17:41:56 +02:00
|
|
|
|
|
|
|
private final static long MIN_DATA_VOLUME = 1024;
|
|
|
|
private final static long MAX_DATA_VOLUME = 10240;
|
|
|
|
/**
|
|
|
|
* Create a valid #StorageUsageRecord
|
|
|
|
* @return the created #StorageUsageRecord
|
|
|
|
*/
|
2015-07-20 17:02:43 +02:00
|
|
|
public static StorageUsageRecord createTestStorageUsageRecordExplicitScope() {
|
|
|
|
StorageUsageRecord usageRecord = createTestStorageUsageRecordAutomaticScope();
|
|
|
|
try {
|
|
|
|
usageRecord.setScope(TEST_SCOPE);
|
|
|
|
} catch (InvalidValueException e) {
|
2015-07-22 17:28:30 +02:00
|
|
|
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Setting the Scope", e);
|
|
|
|
throw new RuntimeException(e);
|
2015-07-20 17:02:43 +02:00
|
|
|
}
|
|
|
|
return usageRecord;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a valid #StorageUsageRecord with scope set automatically.
|
|
|
|
* @return the created #StorageUsageRecord
|
|
|
|
*/
|
|
|
|
public static StorageUsageRecord createTestStorageUsageRecordAutomaticScope() {
|
2015-06-25 17:41:56 +02:00
|
|
|
StorageUsageRecord usageRecord = new StorageUsageRecord();
|
|
|
|
try {
|
|
|
|
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
|
|
|
usageRecord.setOperationResult(OperationResult.SUCCESS);
|
|
|
|
|
|
|
|
usageRecord.setResourceOwner(TEST_RESOUCE_OWNER);
|
|
|
|
usageRecord.setResourceScope(TEST_RESOUCE_SCOPE);
|
|
|
|
|
2015-06-26 15:50:39 +02:00
|
|
|
usageRecord.setResourceURI(new URI(TEST_RESOURCE_URI));
|
|
|
|
usageRecord.setProviderURI(new URI(TEST_PROVIDER_URI));
|
|
|
|
|
2015-07-02 12:01:59 +02:00
|
|
|
usageRecord.setOperationType(StorageUsageRecord.OperationType.READ);
|
|
|
|
usageRecord.setDataType(StorageUsageRecord.DataType.STORAGE);
|
2015-06-25 17:41:56 +02:00
|
|
|
|
|
|
|
usageRecord.setDataVolume(generateRandomLong(MIN_DATA_VOLUME, MAX_DATA_VOLUME));
|
|
|
|
|
2015-06-26 17:13:26 +02:00
|
|
|
usageRecord.setQualifier("image/png");
|
2015-07-01 16:40:49 +02:00
|
|
|
|
2015-06-25 17:41:56 +02:00
|
|
|
|
|
|
|
} catch (InvalidValueException | URISyntaxException e) {
|
2015-07-22 17:28:30 +02:00
|
|
|
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
|
|
|
throw new RuntimeException(e);
|
2015-06-25 17:41:56 +02:00
|
|
|
}
|
|
|
|
return usageRecord;
|
|
|
|
|
|
|
|
}
|
2015-07-20 17:02:43 +02:00
|
|
|
|
2015-07-01 16:40:49 +02:00
|
|
|
/**
|
|
|
|
* @return
|
|
|
|
*/
|
2015-07-20 17:02:43 +02:00
|
|
|
public static JobUsageRecord createTestJobUsageRecordExplicitScope() {
|
|
|
|
|
|
|
|
JobUsageRecord usageRecord = createTestJobUsageRecordAutomaticScope();
|
|
|
|
try {
|
|
|
|
usageRecord.setScope(TEST_SCOPE);
|
|
|
|
} catch (InvalidValueException e) {
|
2015-07-22 17:28:30 +02:00
|
|
|
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Setting the Scope", e);
|
|
|
|
throw new RuntimeException(e);
|
2015-07-20 17:02:43 +02:00
|
|
|
}
|
|
|
|
return usageRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static JobUsageRecord createTestJobUsageRecordAutomaticScope() {
|
2015-07-01 16:40:49 +02:00
|
|
|
|
|
|
|
JobUsageRecord usageRecord = new JobUsageRecord();
|
|
|
|
try {
|
|
|
|
usageRecord.setConsumerId(TEST_CONSUMER_ID);
|
|
|
|
usageRecord.setOperationResult(OperationResult.SUCCESS);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (InvalidValueException e) {
|
2015-07-22 17:28:30 +02:00
|
|
|
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
|
2015-07-01 16:40:49 +02:00
|
|
|
}
|
|
|
|
return usageRecord;
|
|
|
|
}
|
2015-06-25 14:30:50 +02:00
|
|
|
}
|