refs #2201: Remove the use of ScopeProvider if any from accounting-lib

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

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@124017 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-02-09 17:46:23 +00:00
parent ba7664dbf8
commit 67667da8d1
15 changed files with 133 additions and 173 deletions

12
pom.xml
View File

@ -7,10 +7,10 @@
<artifactId>maven-parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.gcube.accounting</groupId>
<artifactId>accounting-lib</artifactId>
<version>2.0.0-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
<name>Accounting Library</name>
<description>Accounting Library</description>
<packaging>jar</packaging>
@ -35,13 +35,13 @@
<dependency>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope</artifactId>
<version>[1.2.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -53,7 +53,7 @@
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
<version>[1.0.2-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -72,7 +72,7 @@
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -8,7 +8,9 @@ import java.util.Map;
import org.gcube.accounting.datamodel.backwardcompatibility.MoveToRecordType;
import org.gcube.accounting.datamodel.validations.annotations.ValidOperationResult;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.implementation.AbstractRecord;
import org.gcube.documentstore.records.implementation.RequiredField;
@ -41,6 +43,18 @@ public abstract class BasicUsageRecord extends AbstractRecord implements UsageRe
@RequiredField @ValidOperationResult
public static final String OPERATION_RESULT = UsageRecord.OPERATION_RESULT;
public static String getScopeFromToken(){
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry;
try {
authorizationEntry = Constants.authorizationService().get(token);
} catch (Exception e) {
throw new RuntimeException(e);
}
String scope = authorizationEntry.getContext();
return scope;
}
/**
* Initialize variable
*/
@ -48,7 +62,7 @@ public abstract class BasicUsageRecord extends AbstractRecord implements UsageRe
super.init();
// Backward compatibility
try {
this.setScope(ScopeProvider.instance.get());
this.setScope(getScopeFromToken());
} catch(Exception e) {
logger.warn("Unable to automaticcally set the scope using scope provider. The record will not be valid if the scope will not be explicitly set.");
}

View File

@ -5,7 +5,7 @@ package org.gcube.accounting.persistence;
import java.util.concurrent.TimeUnit;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
import org.gcube.documentstore.records.Record;
@ -38,7 +38,7 @@ public class AccountingPersistence {
* @throws InvalidValueException
*/
public void account(final Record record) throws InvalidValueException {
String scope = ScopeProvider.instance.get();
String scope = BasicUsageRecord.getScopeFromToken();
try {
PersistenceBackendFactory.getPersistenceBackend(scope).account(record);
} catch (org.gcube.documentstore.exception.InvalidValueException e) {
@ -51,12 +51,12 @@ public class AccountingPersistence {
}
public void flush(long timeout, TimeUnit timeUnit) throws Exception {
String scope = ScopeProvider.instance.get();
String scope = BasicUsageRecord.getScopeFromToken();
PersistenceBackendFactory.flush(scope, timeout, timeUnit);
}
public void close() throws Exception{
String scope = ScopeProvider.instance.get();
String scope = BasicUsageRecord.getScopeFromToken();
PersistenceBackendFactory.getPersistenceBackend(scope).close();
}

View File

@ -7,12 +7,12 @@ import java.security.Key;
import java.util.List;
import java.util.Map;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.documentstore.persistence.PersistenceBackend;
import org.gcube.documentstore.persistence.PersistenceBackendConfiguration;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
@ -59,7 +59,7 @@ public class AccountingPersistenceConfiguration extends PersistenceBackendConfig
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
if(serviceEndpoints.size()>1){
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", TARGET_SCOPE));
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", ScopeProvider.instance.get()));
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", BasicUsageRecord.getScopeFromToken()));
serviceEndpoints = client.submit(query);
}

View File

@ -8,10 +8,11 @@ import java.util.Set;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecordTest;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -24,6 +25,11 @@ public class AggregatedServiceUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(AggregatedServiceUsageRecordTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@Test
public void testRequiredFields() throws InvalidValueException {
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
@ -48,7 +54,6 @@ public class AggregatedServiceUsageRecordTest {
@Test
public void secondAsNotAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
serviceUsageRecord.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
serviceUsageRecord.validate();
@ -88,7 +93,6 @@ public class AggregatedServiceUsageRecordTest {
@Test
public void secondAsAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
serviceUsageRecord.validate();
logger.debug("ServiceUsageRecord : {}", serviceUsageRecord);
@ -133,7 +137,6 @@ public class AggregatedServiceUsageRecordTest {
@Test
public void aggregationStressTest() throws InvalidValueException, NotAggregatableRecordsExceptions {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
serviceUsageRecord.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
serviceUsageRecord.validate();

View File

@ -6,10 +6,11 @@ package org.gcube.accounting.datamodel.aggregation;
import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -22,9 +23,13 @@ public class AggregatedStorageUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(AggregatedStorageUsageRecordTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@Test
public void secondAsNotAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
StorageUsageRecord storageUsageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
storageUsageRecord.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
storageUsageRecord.validate();
@ -53,7 +58,6 @@ public class AggregatedStorageUsageRecordTest {
@Test
public void secondAsAggregated() throws InvalidValueException, NotAggregatableRecordsExceptions {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
StorageUsageRecord storageUsageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
storageUsageRecord.validate();
logger.debug("StorageUsageRecord : {}", storageUsageRecord);
@ -84,7 +88,6 @@ public class AggregatedStorageUsageRecordTest {
@Test
public void aggregationStressTest() throws InvalidValueException, NotAggregatableRecordsExceptions {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
StorageUsageRecord storageUsageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
storageUsageRecord.setResourceProperty(TestUsageRecord.TEST_PROPERTY_NAME, TestUsageRecord.TEST_PROPERTY_VALUE);
storageUsageRecord.validate();

View File

@ -9,7 +9,7 @@ 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.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.Record;
import org.junit.After;
@ -28,15 +28,13 @@ public class JobUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(JobUsageRecordTest.class);
@Before
public void before(){
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
logger.trace("Setting {} scope", TestUsageRecord.TEST_SCOPE);
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@After
public void after(){
ScopeProvider.instance.reset();
logger.trace("Scope reset");
SecurityTokenProvider.instance.reset();
}
public static Set<String> getExpectedRequiredFields(){
@ -56,7 +54,6 @@ public class JobUsageRecordTest {
@Test(expected=InvalidValueException.class)
public void scopeNotSetValidationError() throws InvalidValueException {
ScopeProvider.instance.reset();
JobUsageRecord usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
usageRecord.validate();
logger.debug("{}", usageRecord);
@ -81,7 +78,6 @@ public class JobUsageRecordTest {
@Test
public void testRequiredFieldsWithScopeSetExplicitely() throws InvalidValueException{
ScopeProvider.instance.reset();
JobUsageRecord usageRecord = TestUsageRecord.createTestJobUsageRecordAutomaticScope();
Assert.assertTrue(usageRecord.getScope()==null);
usageRecord.setScope(TestUsageRecord.TEST_SCOPE);

View File

@ -9,7 +9,7 @@ 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.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.Record;
import org.junit.After;
@ -28,15 +28,13 @@ public class ServiceUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(ServiceUsageRecordTest.class);
@Before
public void before(){
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
logger.trace("Setting {} scope", TestUsageRecord.TEST_SCOPE);
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@After
public void after(){
ScopeProvider.instance.reset();
logger.trace("Scope reset");
SecurityTokenProvider.instance.reset();
}
public static Set<String> getExpectedRequiredFields(){
@ -58,7 +56,7 @@ public class ServiceUsageRecordTest {
@Test(expected=InvalidValueException.class)
public void scopeNotSetValidationError() throws InvalidValueException {
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
ServiceUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
usageRecord.validate();
logger.debug("{}", usageRecord);
@ -83,7 +81,7 @@ public class ServiceUsageRecordTest {
@Test
public void testRequiredFieldsWithScopeSetExplicitely() throws InvalidValueException{
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
ServiceUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
Assert.assertTrue(usageRecord.getScope()==null);
usageRecord.setScope(TestUsageRecord.TEST_SCOPE);

View File

@ -9,7 +9,7 @@ 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.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.Record;
import org.junit.After;
@ -28,15 +28,13 @@ public class StorageUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(StorageUsageRecordTest.class);
@Before
public void before(){
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
logger.trace("Setting {} scope", TestUsageRecord.TEST_SCOPE);
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@After
public void after(){
ScopeProvider.instance.reset();
logger.trace("Scope reset");
SecurityTokenProvider.instance.reset();
}
public static Set<String> getExpectedRequiredFields(){
@ -60,7 +58,7 @@ public class StorageUsageRecordTest {
@Test(expected=InvalidValueException.class)
public void scopeNotSetValidationError() throws InvalidValueException {
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
StorageUsageRecord usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
usageRecord.validate();
logger.debug("{}", usageRecord);
@ -85,7 +83,7 @@ public class StorageUsageRecordTest {
@Test
public void testRequiredFieldsWithScopeSetExplicitely() throws InvalidValueException{
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
StorageUsageRecord usageRecord = TestUsageRecord.createTestStorageUsageRecordExplicitScope();
Assert.assertTrue(usageRecord.getScope()==null);
usageRecord.setScope(TestUsageRecord.TEST_SCOPE);

View File

@ -9,7 +9,7 @@ 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.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.Record;
import org.junit.After;
@ -28,15 +28,13 @@ public class TaskUsageRecordTest {
private static Logger logger = LoggerFactory.getLogger(TaskUsageRecordTest.class);
@Before
public void before(){
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
logger.trace("Setting {} scope", TestUsageRecord.TEST_SCOPE);
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@After
public void after(){
ScopeProvider.instance.reset();
logger.trace("Scope reset");
SecurityTokenProvider.instance.reset();
}
public static Set<String> getExpectedRequiredFields(){
@ -56,7 +54,7 @@ public class TaskUsageRecordTest {
@Test(expected=InvalidValueException.class)
public void scopeNotSetValidationError() throws InvalidValueException {
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
TaskUsageRecord usageRecord = TestUsageRecord.createTestTaskUsageRecordAutomaticScope();
usageRecord.validate();
logger.debug("{}", usageRecord);
@ -64,7 +62,6 @@ public class TaskUsageRecordTest {
@Test
public void testRequiredFields() throws InvalidValueException{
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
TaskUsageRecord usageRecord = TestUsageRecord.createTestTaskUsageRecordAutomaticScope();
Set<String> expectedRequiredFields = getExpectedRequiredFields();
@ -82,7 +79,7 @@ public class TaskUsageRecordTest {
@Test
public void testRequiredFieldsWithScopeSetExplicitely() throws InvalidValueException{
ScopeProvider.instance.reset();
SecurityTokenProvider.instance.reset();
TaskUsageRecord usageRecord = TestUsageRecord.createTestTaskUsageRecordExplicitScope();
Assert.assertTrue(usageRecord.getScope()==null);
usageRecord.setScope(TestUsageRecord.TEST_SCOPE);

View File

@ -8,6 +8,7 @@ import java.net.URL;
import java.util.Arrays;
import java.util.List;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.ServiceEndpoint;
@ -17,7 +18,6 @@ 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.common.scope.api.ScopeProvider;
import org.gcube.documentstore.persistence.PersistenceBackend;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.ScopedPublisher;
@ -94,7 +94,7 @@ public class AccountingPersistenceConfigurationTest {
private static void unPublishScopedResource(Resource resource, List<String> scopes) throws RegistryNotFoundException, Exception {
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
String id = resource.id();
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, ScopeProvider.instance.get());
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, BasicUsageRecord.getScopeFromToken());
scopedPublisher.remove(resource, scopes);
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
}
@ -153,7 +153,6 @@ public class AccountingPersistenceConfigurationTest {
}
public void testPersistenceConfigurationFromIS() throws Exception{
ScopeProvider.instance.set(ALL_SCOPES[0]);
boolean createResource = true;
ServiceEndpoint serviceEndpoint = null;
List<String> scopes = Arrays.asList(ALL_SCOPES);
@ -187,23 +186,20 @@ public class AccountingPersistenceConfigurationTest {
}
public void getUsernamePasswordForScopes() throws Exception{
for(String scope : ALL_SCOPES){
logger.debug("START ======================================================");
ScopeProvider.instance.set(scope);
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("{} - {} - {} - {}", scope, 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 ======================================================");
}
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

@ -1,74 +0,0 @@
/**
*
*/
package org.gcube.accounting.persistence;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.persistence.AccountingPersistence;
import org.gcube.accounting.testutility.StressTestUtility;
import org.gcube.accounting.testutility.TestOperation;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class AccountingPersistenceTest {
private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceTest.class);
public static final String[] SCOPES = new String[]{
"/gcube",
"/gcube/devsec", "/gcube/devsec/devVRE",
"/gcube/devNext", "/gcube/devNext/NextNext"
};
public static final long timeout = 5000;
public static final TimeUnit timeUnit = TimeUnit.MILLISECONDS;
@Test
public void stressTest() throws Exception {
final AccountingPersistence persistence = AccountingPersistence.getInstance();
PersistenceBackendFactory.setFallbackLocation(null);
StressTestUtility.stressTest(new TestOperation() {
@Override
public void operate(int i) {
int randomNumber = ThreadLocalRandom.current().nextInt(0, 5);
ScopeProvider.instance.set(SCOPES[randomNumber]);
UsageRecord usageRecord = null;
switch (i%2) {
case 0:
usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
break;
case 1:
usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
break;
default:
usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
break;
}
try {
usageRecord.setConsumerId(UUID.randomUUID().toString());
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 -----------------------------------------------");
}
}

View File

@ -5,12 +5,15 @@ 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.common.scope.api.ScopeProvider;
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;
@ -26,12 +29,21 @@ public class PersistenceBackendMonitorTest {
public static final long timeout = 5000;
public static final TimeUnit timeUnit = TimeUnit.MILLISECONDS;
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@After
public void after(){
SecurityTokenProvider.instance.reset();
}
@Test
public void parsingTest() throws Exception {
ScopeProvider.instance.set("/gcube/devsec");
PersistenceBackendFactory.setFallbackLocation(null);
final PersistenceBackend persistence = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
final PersistenceBackend persistence = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
StressTestUtility.stressTest(new TestOperation() {
@ -62,7 +74,7 @@ public class PersistenceBackendMonitorTest {
persistence.flush(timeout, timeUnit);
logger.debug(" END -----------------------------------------------");
PersistenceBackend persistenceBackend = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
PersistenceBackend persistenceBackend = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
persistenceBackend.setFallback((FallbackPersistenceBackend) persistenceBackend);
PersistenceBackendMonitor temporalDataPersistenceBackendMonitor = new PersistenceBackendMonitor(persistenceBackend);
@ -72,9 +84,8 @@ public class PersistenceBackendMonitorTest {
@Test
public void singleParsingTest() throws Exception {
ScopeProvider.instance.set("/gcube/devsec");
PersistenceBackendFactory.setFallbackLocation(null);
PersistenceBackend persistenceBackend = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
PersistenceBackend persistenceBackend = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
persistenceBackend.setFallback((FallbackPersistenceBackend) persistenceBackend);
PersistenceBackendMonitor temporalDataPersistenceBackendMonitor = new PersistenceBackendMonitor(persistenceBackend);

View File

@ -6,16 +6,16 @@ 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.common.scope.api.ScopeProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.persistence.FallbackPersistenceBackend;
import org.gcube.documentstore.persistence.PersistenceBackend;
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -35,10 +35,20 @@ public class PersistenceBackendTest {
public static final long timeout = 5000;
public static final TimeUnit timeUnit = TimeUnit.MILLISECONDS;
@BeforeClass
public static void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@AfterClass
public static void after(){
SecurityTokenProvider.instance.reset();
}
public static PersistenceBackend getPersistence(){
ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE);
PersistenceBackendFactory.setFallbackLocation(null);
return PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
return PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
}
@Test
@ -105,12 +115,11 @@ public class PersistenceBackendTest {
@Test
public void testScopeRecheck() throws Exception {
ScopeProvider.instance.set("/fakeScope");
PersistenceBackendFactory.setFallbackLocation(null);
logger.debug("Going to check First Time");
PersistenceBackend first = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
PersistenceBackend first = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
logger.debug("First {} : {}", PersistenceBackend.class.getSimpleName(), first);
Long firstCheck = PersistenceBackendFactory.getFallbackLastCheck(ScopeProvider.instance.get());
Long firstCheck = PersistenceBackendFactory.getFallbackLastCheck(BasicUsageRecord.getScopeFromToken());
logger.debug("First Check Time {}", firstCheck);
long startTime = Calendar.getInstance().getTimeInMillis();
@ -121,9 +130,9 @@ public class PersistenceBackendTest {
}
logger.debug("Going to check Second Time");
PersistenceBackend second = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
PersistenceBackend second = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
logger.debug("Second {} : {}", PersistenceBackend.class.getSimpleName(), second);
Long secondCheck = PersistenceBackendFactory.getFallbackLastCheck(ScopeProvider.instance.get());
Long secondCheck = PersistenceBackendFactory.getFallbackLastCheck(BasicUsageRecord.getScopeFromToken());
logger.debug("Second Check Time {}", secondCheck);
Assert.assertNotEquals(firstCheck, secondCheck);

View File

@ -12,11 +12,13 @@ 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.common.scope.api.ScopeProvider;
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;
@ -29,10 +31,22 @@ public class AggregationSchedulerTest {
private static final Logger logger = LoggerFactory.getLogger(AggregationSchedulerTest.class);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set("");
}
@After
public void after(){
SecurityTokenProvider.instance.reset();
}
public static AggregationScheduler getAggregationScheduler(){
return AggregationScheduler.newInstance();
}
public static PersistenceExecutor persistenceExecutor = new PersistenceExecutor(){
@Override
public void persist(Record... records) throws Exception {
@ -43,7 +57,6 @@ public class AggregationSchedulerTest {
};
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);
@ -60,7 +73,6 @@ public class AggregationSchedulerTest {
@Test
public void stressTestAggregableURSingleType() throws Exception {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
@ -80,7 +92,6 @@ public class AggregationSchedulerTest {
@Test
public void stressTestDifferentAggregableURSingleType() throws Exception {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
@ -103,7 +114,6 @@ public class AggregationSchedulerTest {
@Test
public void stressTestDifferentAggregableURTwoType() throws Exception {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override
@ -131,7 +141,6 @@ public class AggregationSchedulerTest {
@Test
public void stressTestDifferentAggregableURMultipleType() throws Exception {
ScopeProvider.instance.set(TestUsageRecord.TEST_SCOPE);
final AggregationScheduler aggregationScheduler = getAggregationScheduler();
StressTestUtility.stressTest(new TestOperation() {
@Override