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@124026 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-02-10 09:31:40 +00:00
parent 1a10ead141
commit 426355ce88
2 changed files with 86 additions and 51 deletions

View File

@ -5,11 +5,10 @@ package org.gcube.accounting.persistence;
import java.io.StringWriter;
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.accounting.testutility.TestUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
@ -19,10 +18,9 @@ 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.documentstore.persistence.PersistenceBackend;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.ScopedPublisher;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -56,49 +54,16 @@ public class AccountingPersistenceConfigurationTest {
}
public static final String[] ALL_SCOPES = new String[]{
"/gcube",
"/gcube/devsec",
"/gcube/devsec/devVRE",
"/gcube/devNext",
"/gcube/devNext/NextNext"
};
/**
* Publish the provided resource on all Service Scopes retrieved from
* Context
* @param resource to be published
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
private static void publishScopedResource(Resource resource, List<String> scopes) throws Exception {
StringWriter stringWriter = new StringWriter();
Resources.marshal(resource, stringWriter);
@Before
public void before() throws Exception{
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
}
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
try {
logger.debug("Trying to publish to {}:\n{}", scopes, stringWriter);
scopedPublisher.create(resource, scopes);
} catch (Exception e) {
logger.error("The resource was not published", e);
throw e;
}
@After
public void after(){
SecurityTokenProvider.instance.reset();
}
/**
* Remove the resource from IS
* @param resource to be unpublished
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
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, BasicUsageRecord.getScopeFromToken());
scopedPublisher.remove(resource, scopes);
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
}
/**
* Create the Service Endpoint using information related to discovered
* available plugins and their own discovered capabilities
@ -155,11 +120,10 @@ public class AccountingPersistenceConfigurationTest {
public void testPersistenceConfigurationFromIS() throws Exception{
boolean createResource = true;
ServiceEndpoint serviceEndpoint = null;
List<String> scopes = Arrays.asList(ALL_SCOPES);
if(createResource){
serviceEndpoint = createServiceEndpoint();
publishScopedResource(serviceEndpoint, scopes);
TestUtility.publishResource(serviceEndpoint);
}
Thread.sleep(5000); // Waiting 5 sec
@ -179,7 +143,7 @@ public class AccountingPersistenceConfigurationTest {
}
} finally {
if(createResource){
unPublishScopedResource(serviceEndpoint, scopes);
TestUtility.unPublishResource(serviceEndpoint);
}
}

View File

@ -3,6 +3,19 @@
*/
package org.gcube.accounting.testutility;
import java.io.StringWriter;
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.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.informationsystem.publisher.RegistryPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
@ -11,4 +24,62 @@ public class TestUtility {
public static final String TOKEN = "";
}
/**
* Logger
*/
private static Logger logger = LoggerFactory.getLogger(TestUtility.class);
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;
}
/**
* Publish the provided resource on current scope
* @param resource to be published
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
public static void publishResource(Resource resource) throws Exception {
StringWriter stringWriter = new StringWriter();
Resources.marshal(resource, stringWriter);
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
try {
logger.debug("Trying to publish to {}:\n{}", getScopeFromToken(), stringWriter);
registryPublisher.create(resource);
} catch (Exception e) {
logger.error("The resource was not published", e);
throw e;
}
}
/**
* Remove the resource from IS from curretn scope
* @param resource to be unpublished
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
public static void unPublishResource(Resource resource) throws Exception {
//StringWriter stringWriter = new StringWriter();
//Resources.marshal(resource, stringWriter);
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
String id = resource.id();
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getScopeFromToken());
registryPublisher.remove(resource);
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
}
}