2016-02-10 15:15:01 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2016-02-17 14:17:31 +01:00
|
|
|
package org.gcube.testutility;
|
2016-02-10 15:15:01 +01:00
|
|
|
|
2016-02-10 16:49:09 +01:00
|
|
|
import java.io.StringWriter;
|
|
|
|
|
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2016-09-23 14:24:23 +02:00
|
|
|
import org.gcube.common.authorization.client.Constants;
|
2016-02-10 16:49:09 +01:00
|
|
|
import org.gcube.common.resources.gcore.Resource;
|
|
|
|
import org.gcube.common.resources.gcore.Resources;
|
2016-02-12 17:56:12 +01:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2016-02-10 16:49:09 +01:00
|
|
|
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;
|
|
|
|
|
2016-02-10 15:15:01 +01:00
|
|
|
/**
|
2016-10-12 14:24:02 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
2016-02-10 15:15:01 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class TestUtility {
|
|
|
|
|
2016-09-23 14:24:23 +02:00
|
|
|
public static final String TOKEN = "7c66c94c-7f6e-49cd-9a34-909cd3832f3e-98187548";
|
2016-02-10 16:51:09 +01:00
|
|
|
public static final String PARENT_TOKEN = "";
|
2016-02-10 15:15:01 +01:00
|
|
|
|
2016-02-10 16:49:09 +01:00
|
|
|
/**
|
|
|
|
* Logger
|
|
|
|
*/
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(TestUtility.class);
|
|
|
|
|
2016-02-12 17:56:12 +01:00
|
|
|
public static String getScope(){
|
2016-02-10 16:49:09 +01:00
|
|
|
String token = SecurityTokenProvider.instance.get();
|
|
|
|
AuthorizationEntry authorizationEntry;
|
|
|
|
try {
|
|
|
|
authorizationEntry = Constants.authorizationService().get(token);
|
|
|
|
} catch (Exception e) {
|
2016-09-23 14:24:23 +02:00
|
|
|
return ScopeProvider.instance.get();
|
2016-02-10 16:49:09 +01:00
|
|
|
}
|
|
|
|
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 {
|
2016-02-12 17:56:12 +01:00
|
|
|
logger.debug("Trying to publish to {}:\n{}", getScope(), stringWriter);
|
2016-02-10 16:49:09 +01:00
|
|
|
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();
|
2016-02-12 17:56:12 +01:00
|
|
|
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getScope());
|
2016-02-10 16:49:09 +01:00
|
|
|
|
|
|
|
registryPublisher.remove(resource);
|
|
|
|
|
|
|
|
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
|
|
|
|
}
|
|
|
|
|
2016-02-10 15:15:01 +01:00
|
|
|
}
|