Fixing tests due to refactoring

This commit is contained in:
Luca Frosini 2021-03-18 16:24:52 +01:00
parent eddcb0b451
commit 060f8673ee
2 changed files with 170 additions and 220 deletions

View File

@ -3,23 +3,7 @@
*/
package org.gcube.documentstore.persistence;
import java.util.Calendar;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
import org.gcube.accounting.datamodel.aggregation.AggregatedJobUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedPortletUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageStatusRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
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.utility.postgresql.PostgreSQLQuery;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.junit.Assert;
import org.junit.Test;
@ -32,199 +16,7 @@ import org.slf4j.LoggerFactory;
public class PersistencePostgreSQLTest extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQLTest.class);
public static final String[] scopes = new String[]{
"/gcube",
"/gcube/devsec", "/gcube/devsec/devVRE",
"/gcube/devNext", "/gcube/devNext/NextNext"
};
public static final String[] calledMethods = new String[]{"create", "read", "update", "delete", "purge", "execute", "addToContext", "removeFromContext", "other"};
public static final String[] users = new String[]{"luca.frosini", "lucio.lelii", "francesco.frangiacrapa", "fabio.sinibaldi", "massimiliano.assante", "giancarlo.panichi", "leonardo.candela", "pasquale.pagano"};
public static final String[] serviceClasses = new String[]{
"information-system",
"data-publishing",
"data-catalogue",
"vre-management",
"accounting",
"data-access",
"transfer"
};
public static final String[] serviceNames = new String[]{
"resource-registry", "registry-publisher",
"catalogue-ws", "sdmx-publisher",
"gcat", "grsf-publisher-ws",
"smart-executor", "ghn-manager",
"accounting-service", "accounting-analytics",
"storage-hub", "species-products-discovery",
"data-transfer-service", "uri-resolver"
};
public static final int minutesInAnYear;
public static final int maxDuration = 450;
private static final Random random;
static {
random = new Random();
minutesInAnYear = (int) TimeUnit.DAYS.toMinutes(365);
}
public static void setCalledMethod(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(calledMethods.length);
serviceUsageRecord.setCalledMethod(calledMethods[randomNumber]);
}
public static void setCallerHostAndHost(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(25);
serviceUsageRecord.setCallerHost("host" + randomNumber + ".d4science.org");
serviceUsageRecord.setHost("host" + (25-randomNumber) + ".d4science.org");
}
public static void setConsumerId(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(users.length);
serviceUsageRecord.setConsumerId(users[randomNumber]);
}
public static void setServiceClassAndName(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(serviceClasses.length);
serviceUsageRecord.setServiceClass(serviceClasses[randomNumber]);
int randomInt = random.nextInt(2);
serviceUsageRecord.setServiceName(serviceNames[randomNumber+randomInt]);
}
public static double randomBetween(int min, int max) {
return (Math.random()*(max-min+1)+min);
}
public static void setTiming(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int operationCount = random.nextInt(55);
operationCount = operationCount + 1;
serviceUsageRecord.setOperationCount(operationCount);
int minutesToSubstract = random.nextInt(minutesInAnYear);
Calendar creationTime = Calendar.getInstance();
creationTime.add(Calendar.MINUTE, -minutesToSubstract);
serviceUsageRecord.setCreationTime(creationTime);
long duration = (long) randomBetween(90, maxDuration);
serviceUsageRecord.setDuration(duration);
if(operationCount==1) {
serviceUsageRecord.setMaxInvocationTime(duration);
serviceUsageRecord.setMinInvocationTime(duration);
serviceUsageRecord.setEndTime(creationTime);
serviceUsageRecord.setStartTime(creationTime);
}else {
// Random number between generated duration and 678;
long maxInvocationTime = (long) randomBetween((int) duration, 678);
serviceUsageRecord.setMaxInvocationTime(maxInvocationTime);
// Random number between 30 and generated duration
long minInvocationTime = (long) randomBetween(30, (int) duration);
serviceUsageRecord.setMinInvocationTime(minInvocationTime);
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(creationTime.getTimeInMillis());
int startTimeMinutesToSubstract = (int) randomBetween(2880, 1440);
startTime.add(Calendar.MINUTE, -startTimeMinutesToSubstract);
serviceUsageRecord.setStartTime(startTime);
Calendar endTime = Calendar.getInstance();
endTime.setTimeInMillis(creationTime.getTimeInMillis());
int edTimeMinutesToSubstract = (int) randomBetween(startTimeMinutesToSubstract, 60);
endTime.add(Calendar.MINUTE, -edTimeMinutesToSubstract);
serviceUsageRecord.setEndTime(endTime);
}
}
public static void setOperationResult(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomInt = random.nextInt(OperationResult.values().length);
serviceUsageRecord.setOperationResult(OperationResult.values()[randomInt]);
}
public static void setScope(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomInt = random.nextInt(scopes.length);
serviceUsageRecord.setScope(scopes[randomInt]);
}
protected AggregatedServiceUsageRecord getTestServiceUsageRecord() throws Exception {
AggregatedServiceUsageRecord serviceUsageRecord = new AggregatedServiceUsageRecord();
serviceUsageRecord.setAggregated(true);
setCalledMethod(serviceUsageRecord);
setCallerHostAndHost(serviceUsageRecord);
setConsumerId(serviceUsageRecord);
serviceUsageRecord.setCallerQualifier("TOKEN");
setTiming(serviceUsageRecord);
setOperationResult(serviceUsageRecord);
setScope(serviceUsageRecord);
setServiceClassAndName(serviceUsageRecord);
return serviceUsageRecord;
}
protected AggregatedJobUsageRecord getTestAggregatedJobUsageRecord() throws Exception {
JobUsageRecord jobUsageRecord = TestUsageRecord.createTestJobUsageRecord();
AggregatedJobUsageRecord aggregatedJobUsageRecord = new AggregatedJobUsageRecord(jobUsageRecord);
return aggregatedJobUsageRecord;
}
protected AggregatedPortletUsageRecord getTestAggregatedPortletUsageRecord() throws Exception {
PortletUsageRecord portletUsageRecord = TestUsageRecord.createTestPortletUsageRecord();
AggregatedPortletUsageRecord aggregatedPortletUsageRecord = new AggregatedPortletUsageRecord(portletUsageRecord);
return aggregatedPortletUsageRecord;
}
protected AggregatedServiceUsageRecord getTestAggregatedServiceUsageRecord() throws Exception {
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecord();
AggregatedServiceUsageRecord aggregatedServiceUsageRecord = new AggregatedServiceUsageRecord(serviceUsageRecord);
return aggregatedServiceUsageRecord;
}
protected AggregatedStorageStatusRecord getTestAggregatedStorageStatusRecord() throws Exception {
StorageStatusRecord storageStatusRecord = TestUsageRecord.createTestStorageVolumeUsageRecord();
AggregatedStorageStatusRecord aggregatedStorageStatusRecord = new AggregatedStorageStatusRecord(storageStatusRecord);
return aggregatedStorageStatusRecord;
}
protected AggregatedStorageUsageRecord getTestAggregatedStorageUsageRecord() throws Exception {
StorageUsageRecord storageUsageRecord = TestUsageRecord.createTestStorageUsageRecord();
AggregatedStorageUsageRecord aggregatedStorageUsageRecord = new AggregatedStorageUsageRecord(storageUsageRecord);
return aggregatedStorageUsageRecord;
}
@Test
public void testSQLStatementString() throws Exception {
UsageRecord usageRecord = getTestAggregatedJobUsageRecord();
PostgreSQLQuery postgreSQLQuery = new PostgreSQLQuery();
String sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
logger.debug(sql);
usageRecord = getTestAggregatedPortletUsageRecord();
sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
logger.debug(sql);
usageRecord = getTestAggregatedServiceUsageRecord();
sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
logger.debug(sql);
usageRecord = getTestAggregatedStorageStatusRecord();
sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
logger.debug(sql);
usageRecord = getTestAggregatedStorageUsageRecord();
sql = postgreSQLQuery.getSQLInsertCommand(usageRecord);
logger.debug(sql);
}
@Test
public void persistenceIsPostgreSQL() throws ObjectNotFound, Exception {
logger.debug("Going to check if the Persistence is CouchBase");
@ -233,12 +25,9 @@ public class PersistencePostgreSQLTest extends ContextTest {
FallbackPersistenceBackend fallbackPersistenceBackend = PersistenceBackendFactory.createFallback(context);
PersistenceBackend persistenceBackend = PersistenceBackendFactory.rediscoverPersistenceBackend(fallbackPersistenceBackend, context);
Assert.assertTrue(persistenceBackend instanceof PersistencePostgreSQL);
}
@Test
public void testInsertRecords() throws ObjectNotFound, Exception {
PersistenceBackendFactory.setFallbackLocation(null);
@ -278,7 +67,7 @@ public class PersistencePostgreSQLTest extends ContextTest {
persistencePostgreSQL.insert(usageRecord);
*/
UsageRecord usageRecord = getTestAggregatedStorageUsageRecord();
UsageRecord usageRecord = TestUsageRecord.getTestAggregatedStorageUsageRecord();
persistencePostgreSQL.insert(usageRecord);
}
persistencePostgreSQL.commitAndClose();

View File

@ -5,9 +5,17 @@ package org.gcube.documentstore.persistence;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Calendar;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
import org.gcube.accounting.datamodel.aggregation.AggregatedJobUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedPortletUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageStatusRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
import org.gcube.accounting.datamodel.basetypes.AbstractStorageStatusRecord;
import org.gcube.accounting.datamodel.basetypes.AbstractStorageUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord;
@ -59,16 +67,18 @@ public class TestUsageRecord {
}
/**
* Generate A Random long in a range between min and max.
* This function is internally used to set random duration.
* 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));
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() {
@ -92,6 +102,7 @@ public class TestUsageRecord {
return usageRecord;
}
public final static String TEST_RESOUCE_OWNER = "resource.owner";
public final static String TEST_RESOUCE_SCOPE = TEST_SCOPE;
@ -103,6 +114,7 @@ public class TestUsageRecord {
/**
* Create a valid #StorageUsageRecord with scope set automatically.
*
* @return the created #StorageUsageRecord
*/
public static StorageUsageRecord createTestStorageUsageRecord() {
@ -124,7 +136,6 @@ public class TestUsageRecord {
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);
@ -135,6 +146,7 @@ public class TestUsageRecord {
/**
* Create a valid #StorageVolumeUsageRecord with scope set automatically.
*
* @return the created #StorageVolumeUsageRecord
*/
public static StorageStatusRecord createTestStorageVolumeUsageRecord() {
@ -149,9 +161,7 @@ public class TestUsageRecord {
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);
@ -204,7 +214,158 @@ public class TestUsageRecord {
return usageRecord;
}
public static final String[] scopes = new String[] { "/gcube", "/gcube/devsec", "/gcube/devsec/devVRE",
"/gcube/devNext", "/gcube/devNext/NextNext" };
public static final String[] calledMethods = new String[] { "create", "read", "update", "delete", "purge",
"execute", "addToContext", "removeFromContext", "other" };
public static final String[] users = new String[] { "luca.frosini", "lucio.lelii", "francesco.frangiacrapa",
"fabio.sinibaldi", "massimiliano.assante", "giancarlo.panichi", "leonardo.candela", "pasquale.pagano" };
public static final String[] serviceClasses = new String[] { "information-system", "data-publishing",
"data-catalogue", "vre-management", "accounting", "data-access", "transfer" };
public static final String[] serviceNames = new String[] { "resource-registry", "registry-publisher",
"catalogue-ws", "sdmx-publisher", "gcat", "grsf-publisher-ws", "smart-executor", "ghn-manager",
"accounting-service", "accounting-analytics", "storage-hub", "species-products-discovery",
"data-transfer-service", "uri-resolver" };
public static final int minutesInAnYear;
public static final int maxDuration = 450;
private static final Random random;
static {
random = new Random();
minutesInAnYear = (int) TimeUnit.DAYS.toMinutes(365);
}
public static void setCalledMethod(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(calledMethods.length);
serviceUsageRecord.setCalledMethod(calledMethods[randomNumber]);
}
public static void setCallerHostAndHost(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(25);
serviceUsageRecord.setCallerHost("host" + randomNumber + ".d4science.org");
serviceUsageRecord.setHost("host" + (25 - randomNumber) + ".d4science.org");
}
public static void setConsumerId(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(users.length);
serviceUsageRecord.setConsumerId(users[randomNumber]);
}
public static void setServiceClassAndName(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(serviceClasses.length);
serviceUsageRecord.setServiceClass(serviceClasses[randomNumber]);
int randomInt = random.nextInt(2);
serviceUsageRecord.setServiceName(serviceNames[randomNumber + randomInt]);
}
public static double randomBetween(int min, int max) {
return (Math.random() * (max - min + 1) + min);
}
public static void setTiming(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int operationCount = random.nextInt(55);
operationCount = operationCount + 1;
serviceUsageRecord.setOperationCount(operationCount);
int minutesToSubstract = random.nextInt(minutesInAnYear);
Calendar creationTime = Calendar.getInstance();
creationTime.add(Calendar.MINUTE, -minutesToSubstract);
serviceUsageRecord.setCreationTime(creationTime);
long duration = (long) randomBetween(90, maxDuration);
serviceUsageRecord.setDuration(duration);
if (operationCount == 1) {
serviceUsageRecord.setMaxInvocationTime(duration);
serviceUsageRecord.setMinInvocationTime(duration);
serviceUsageRecord.setEndTime(creationTime);
serviceUsageRecord.setStartTime(creationTime);
} else {
// Random number between generated duration and 678;
long maxInvocationTime = (long) randomBetween((int) duration, 678);
serviceUsageRecord.setMaxInvocationTime(maxInvocationTime);
// Random number between 30 and generated duration
long minInvocationTime = (long) randomBetween(30, (int) duration);
serviceUsageRecord.setMinInvocationTime(minInvocationTime);
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(creationTime.getTimeInMillis());
int startTimeMinutesToSubstract = (int) randomBetween(2880, 1440);
startTime.add(Calendar.MINUTE, -startTimeMinutesToSubstract);
serviceUsageRecord.setStartTime(startTime);
Calendar endTime = Calendar.getInstance();
endTime.setTimeInMillis(creationTime.getTimeInMillis());
int edTimeMinutesToSubstract = (int) randomBetween(startTimeMinutesToSubstract, 60);
endTime.add(Calendar.MINUTE, -edTimeMinutesToSubstract);
serviceUsageRecord.setEndTime(endTime);
}
}
public static void setOperationResult(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomInt = random.nextInt(OperationResult.values().length);
serviceUsageRecord.setOperationResult(OperationResult.values()[randomInt]);
}
public static void setScope(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomInt = random.nextInt(scopes.length);
serviceUsageRecord.setScope(scopes[randomInt]);
}
public static AggregatedServiceUsageRecord getTestAggregatedServiceUsageRecord() throws Exception {
AggregatedServiceUsageRecord serviceUsageRecord = new AggregatedServiceUsageRecord();
serviceUsageRecord.setAggregated(true);
setCalledMethod(serviceUsageRecord);
setCallerHostAndHost(serviceUsageRecord);
setConsumerId(serviceUsageRecord);
serviceUsageRecord.setCallerQualifier("TOKEN");
setTiming(serviceUsageRecord);
setOperationResult(serviceUsageRecord);
setScope(serviceUsageRecord);
setServiceClassAndName(serviceUsageRecord);
return serviceUsageRecord;
}
public static AggregatedJobUsageRecord getTestAggregatedJobUsageRecord() throws Exception {
JobUsageRecord jobUsageRecord = TestUsageRecord.createTestJobUsageRecord();
AggregatedJobUsageRecord aggregatedJobUsageRecord = new AggregatedJobUsageRecord(jobUsageRecord);
return aggregatedJobUsageRecord;
}
public static AggregatedPortletUsageRecord getTestAggregatedPortletUsageRecord() throws Exception {
PortletUsageRecord portletUsageRecord = TestUsageRecord.createTestPortletUsageRecord();
AggregatedPortletUsageRecord aggregatedPortletUsageRecord = new AggregatedPortletUsageRecord(portletUsageRecord);
return aggregatedPortletUsageRecord;
}
public static AggregatedServiceUsageRecord getTestSimpleAggregatedServiceUsageRecord() throws Exception {
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecord();
AggregatedServiceUsageRecord aggregatedServiceUsageRecord = new AggregatedServiceUsageRecord(serviceUsageRecord);
return aggregatedServiceUsageRecord;
}
public static AggregatedStorageStatusRecord getTestAggregatedStorageStatusRecord() throws Exception {
StorageStatusRecord storageStatusRecord = TestUsageRecord.createTestStorageVolumeUsageRecord();
AggregatedStorageStatusRecord aggregatedStorageStatusRecord = new AggregatedStorageStatusRecord(storageStatusRecord);
return aggregatedStorageStatusRecord;
}
public static AggregatedStorageUsageRecord getTestAggregatedStorageUsageRecord() throws Exception {
StorageUsageRecord storageUsageRecord = TestUsageRecord.createTestStorageUsageRecord();
AggregatedStorageUsageRecord aggregatedStorageUsageRecord = new AggregatedStorageUsageRecord(storageUsageRecord);
return aggregatedStorageUsageRecord;
}
}