Implemented corner cases

This commit is contained in:
Luca Frosini 2021-03-12 19:13:57 +01:00
parent e77a2791c6
commit 5be33cb8ee
7 changed files with 309 additions and 17 deletions

View File

@ -50,7 +50,7 @@ public class PersistencePostgreSQL extends PersistenceBackend {
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(url, username, password);
logger.trace("Opened database successfully");
logger.trace("Database opened successfully");
connection.setAutoCommit(false);
statement = connection.createStatement();
} catch (Exception e) {
@ -69,16 +69,11 @@ public class PersistencePostgreSQL extends PersistenceBackend {
}
protected void appendValue(StringBuffer values, Serializable serializable) {
if(serializable instanceof Integer || serializable instanceof Long) {
if(serializable instanceof Number) {
values.append(serializable.toString());
return;
}
if(serializable instanceof String) {
appendString(values, serializable.toString());
return;
}
if(serializable instanceof Calendar) {
Calendar calendar = (Calendar) serializable;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATETIME_PATTERN);
@ -93,16 +88,22 @@ public class PersistencePostgreSQL extends PersistenceBackend {
return;
}
values.append(serializable.toString());
// String, URI etc
appendString(values, serializable.toString());
}
protected void appendKey(StringBuffer sql, String key) {
int lenght = key.length();
int lenght = key.length();
boolean lastLowerCase = true;
for (int i=0; i<lenght; i++) {
Character ch = key.charAt(i); /*traversing String one by one*/
if (Character.isUpperCase(ch)) {
sql.append("_");
if (Character.isUpperCase(ch)) {
if(lastLowerCase) {
sql.append("_");
}
lastLowerCase = false;
}else {
lastLowerCase = true;
}
sql.append(Character.toLowerCase(ch));
}

View File

@ -3,11 +3,7 @@
*/
package org.gcube.documentstore.persistence;
import java.util.Calendar;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.junit.Assert;
import org.junit.Test;
@ -21,6 +17,7 @@ public class PersistencePostgreSQLTest extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQLTest.class);
/*
protected UsageRecord getTestServiceUsageRecord() throws Exception {
AggregatedServiceUsageRecord serviceUsageRecord = new AggregatedServiceUsageRecord();
serviceUsageRecord.setAggregated(true);
@ -53,13 +50,18 @@ public class PersistencePostgreSQLTest extends ContextTest {
return serviceUsageRecord;
}
*/
@Test
public void testSQLStatementString() throws Exception {
UsageRecord usageRecord = getTestServiceUsageRecord();
UsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecord();
PersistencePostgreSQL persistencePostgreSQL = new PersistencePostgreSQL();
String sql = persistencePostgreSQL.getSQLInsertCommand(usageRecord);
logger.debug(sql);
usageRecord = TestUsageRecord.createTestStorageUsageRecord();
sql = persistencePostgreSQL.getSQLInsertCommand(usageRecord);
logger.debug(sql);
}
@Test
@ -75,4 +77,22 @@ public class PersistencePostgreSQLTest extends ContextTest {
}
@Test
public void testInsertRecords() throws ObjectNotFound, Exception {
PersistenceBackendFactory.setFallbackLocation(null);
String context = ContextTest.getCurrentContextFullName();
FallbackPersistenceBackend fallbackPersistenceBackend = PersistenceBackendFactory.createFallback(context);
PersistenceBackend persistenceBackend = PersistenceBackendFactory.rediscoverPersistenceBackend(fallbackPersistenceBackend, context);
Assert.assertTrue(persistenceBackend instanceof PersistencePostgreSQL);
PersistencePostgreSQL persistencePostgreSQL = (PersistencePostgreSQL) persistenceBackend;
persistencePostgreSQL.newConnection();
for(int i=0; i<10; i++) {
UsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecord();
persistencePostgreSQL.insert(usageRecord);
}
persistencePostgreSQL.commitAndClose();
}
}

View File

@ -0,0 +1,210 @@
/**
*
*/
package org.gcube.documentstore.persistence;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
import org.gcube.accounting.datamodel.basetypes.AbstractStorageStatusRecord;
import org.gcube.accounting.datamodel.basetypes.AbstractStorageUsageRecord;
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.StorageStatusRecord;
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
import org.gcube.accounting.persistence.AccountingPersistenceFactory;
import org.gcube.documentstore.exception.InvalidValueException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
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 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.
* @return the generated random long
*/
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
*/
public static ServiceUsageRecord createTestServiceUsageRecord() {
ServiceUsageRecord usageRecord = new ServiceUsageRecord();
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;
/**
* Create a valid #StorageUsageRecord with scope set automatically.
* @return the created #StorageUsageRecord
*/
public static StorageUsageRecord createTestStorageUsageRecord() {
StorageUsageRecord usageRecord = new StorageUsageRecord();
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");
} 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;
}
/**
* 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.setHost(TEST_HOST);
usageRecord.setCallerQualifier(TEST_CALLER_QUALIFIER);
usageRecord.setServiceClass(TEST_SERVICE_CLASS);
usageRecord.setServiceName(TEST_SERVICE_NAME);
usageRecord.setJobName(TEST_JOB_NAME);
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);
}
return usageRecord;
}
/**
* @return
*/
public static PortletUsageRecord createTestPortletUsageRecord() {
PortletUsageRecord usageRecord = new PortletUsageRecord();
try {
usageRecord.setConsumerId(TEST_CONSUMER_ID);
usageRecord.setOperationResult(TEST_OPERATION_RESULT);
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;
}
}

View File

@ -0,0 +1,21 @@
CREATE TYPE operation_result AS ENUM ('SUCCESS', 'FAILED');
CREATE TABLE "serviceusagerecord"(
id TEXT NOT NULL,
consumer_id TEXT NOT NULL,
creation_time TIMESTAMP WITH TIME ZONE NOT NULL,
scope TEXT NOT NULL,
operation_result operation_result NOT NULL,
caller_qualifier TEXT NOT NULL DEFAULT 'TOKEN',
caller_host TEXT NOT NULL,
host TEXT NOT NULL,
service_class TEXT NOT NULL,
service_name TEXT NOT NULL,
called_method TEXT NOT NULL,
duration NUMERIC NOT NULL,
max_invocation_time NUMERIC NOT NULL,
min_invocation_time NUMERIC NOT NULL,
operation_count INTEGER NOT NULL DEFAULT 1,
aggregated BOOLEAN NOT NULL DEFAULT true,
start_time TIMESTAMP WITH TIME ZONE NOT NULL,
end_time TIMESTAMP WITH TIME ZONE NOT NULL
);

View File

@ -0,0 +1,17 @@
CREATE TYPE operation_type AS ENUM ('insert', 'update', 'replace', 'delete', 'invalidate');
CREATE TYPE data_type AS ENUM ('String', 'Time', 'Time_Interval', 'Times_ListOf', 'Text', 'Boolean', 'Number', 'GeoJSON');
CREATE TABLE "serviceusagerecord"(
id TEXT NOT NULL,
consumer_id TEXT NOT NULL,
creation_time TIMESTAMP WITH TIME ZONE NOT NULL,
scope TEXT NOT NULL,
operation_result operation_result NOT NULL,
resource_owner TEXT NOT NULL,
resource_scope TEXT NOT NULL,
resource_uri TEXT NOT NULL,
provider_uri TEXT NOT NULL,
operation_type operation_type NOT NULL,
data_type data_type NOT NULL,
data_volume NUMERIC NOT NULL
);

View File

@ -0,0 +1,20 @@
INSERT INTO serviceusagerecord
(called_method,caller_host,caller_qualifier,consumer_id,
creation_time,duration,host,id,
operation_result,scope,service_class,service_name)
VALUES
('TestCalledMethod','remotehost','TestCallerQualifier','name.surname',
'2021-03-12 19:10:22.439 +0100',842,'localhost','4cc05634-683c-41e7-9110-3cb3cfb7205d',
'SUCCESS','/gcube/devNext','TestServiceClass','TestServiceName');
INSERT INTO storageusagerecord
(consumer_id,creation_time,data_type,data_volume,
id,operation_result,operation_type,provider_uri,
resource_owner,resource_scope,resource_uri,scope)
VALUES
('name.surname','2021-03-12 19:10:23.031 +0100','STORAGE',1250,
'3418d820-9329-4d46-a00e-01501980afba','SUCCESS','READ','testprotocol://providerURI',
'resource.owner','/infrastructure/vo','testprotocol://objectURI','/gcube/devNext');

3
src/test/resources/url Normal file
View File

@ -0,0 +1,3 @@
postgres://lucafrosini:silviomerda@localhost:5432/accounting
jdbc:postgres://localhost:5432/accounting