103 lines
4.5 KiB
Java
103 lines
4.5 KiB
Java
package org.gcube.gcat.configuration;
|
|
|
|
import java.io.StringWriter;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
|
|
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import org.gcube.common.authorization.utils.manager.SecretManager;
|
|
import org.gcube.common.resources.gcore.GenericResource;
|
|
import org.gcube.common.resources.gcore.Resources;
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
|
import org.gcube.gcat.ContextTest;
|
|
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
|
|
import org.gcube.informationsystem.publisher.RegistryPublisher;
|
|
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
|
import org.junit.Assert;
|
|
import org.junit.Test;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class GCoreISConfigurationProxyTest extends ContextTest {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(GCoreISConfigurationProxyTest.class);
|
|
|
|
@Test
|
|
public void testGetSupportedOrganizationsFromIS() throws Exception {
|
|
ContextTest.setContextByName("/gcube/devNext/NextNext");
|
|
String context = SecretManager.instance.get().getContext();
|
|
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
|
|
Set<String> organizations = gCoreISConfigurationProxy.getSupportedOrganizationsFromIS();
|
|
Assert.assertTrue(organizations.size()>0);
|
|
}
|
|
|
|
protected GenericResource instantiateGenericResource(String secondaryType, String name, String xml) throws Exception {
|
|
GenericResource genericResource = new GenericResource();
|
|
org.gcube.common.resources.gcore.GenericResource.Profile profile = genericResource.newProfile();
|
|
profile.type(secondaryType);
|
|
profile.name(name);
|
|
profile.description("This resource is read by gCat and define the list of CKAN organizations where a client is allowed to publish for the current context");
|
|
profile.newBody(xml);
|
|
StringWriter stringWriter = new StringWriter();
|
|
Resources.marshal(genericResource, stringWriter);
|
|
logger.debug("The generated {} is\n{}", GenericResource.class.getSimpleName(), stringWriter.toString());
|
|
return genericResource;
|
|
}
|
|
|
|
protected void createGenericResource(String xml) throws Exception {
|
|
GenericResource genericResource = instantiateGenericResource(
|
|
GCoreISConfigurationProxy.GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS,
|
|
GCoreISConfigurationProxy.GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, xml);
|
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
|
genericResource = registryPublisher.create(genericResource);
|
|
StringWriter stringWriter = new StringWriter();
|
|
Resources.marshal(genericResource, stringWriter);
|
|
logger.trace("The {} with ID {} has been created \n{}", GenericResource.class.getSimpleName(),
|
|
genericResource.id(), stringWriter.toString());
|
|
}
|
|
|
|
protected String createGRBody(List<String> organizations) throws Exception {
|
|
if(organizations==null || organizations.size()<1) {
|
|
throw new Exception("Unable to create the body for the generic resource with empty organization list");
|
|
}
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
ObjectNode objectNode = objectMapper.createObjectNode();
|
|
ArrayNode arrayNode = objectNode.putArray(GCoreISConfigurationProxy.GENERIC_RESOURCE_CKAN_ORGANIZATIONS);
|
|
for(String organizationName : organizations) {
|
|
arrayNode.add(organizationName);
|
|
}
|
|
return objectMapper.writeValueAsString(objectNode);
|
|
}
|
|
|
|
protected void createGenericResourceForSupportedOrganizations(List<String> organizations) throws Exception {
|
|
String json = createGRBody(organizations);
|
|
createGenericResource(json);
|
|
}
|
|
|
|
// @Test
|
|
public void createGenericResourceForSupportedOrganizationsByName() throws Exception {
|
|
List<String> organizations = new ArrayList<>();
|
|
organizations.add("nextnext");
|
|
organizations.add("devvre");
|
|
createGenericResourceForSupportedOrganizations(organizations);
|
|
}
|
|
|
|
// @Test
|
|
public void createGenericResourceForSupportedOrganizationsByScopeBean() throws Exception {
|
|
ContextTest.setContextByName("/gcube/devNext/NextNext");
|
|
|
|
List<ScopeBean> scopeBeans = new ArrayList<>();
|
|
scopeBeans.add(new ScopeBean("/gcube/devNext/NextNext"));
|
|
scopeBeans.add(new ScopeBean("/gcube/devsec/devVRE"));
|
|
List<String> organizations = new ArrayList<>();
|
|
for(ScopeBean scopeBean : scopeBeans) {
|
|
organizations.add(CatalogueConfiguration.getOrganizationName(scopeBean));
|
|
}
|
|
createGenericResourceForSupportedOrganizations(organizations);
|
|
}
|
|
|
|
}
|