Compare commits

...

4 Commits

Author SHA1 Message Date
Luca Frosini 2aa2909e4e Improved code 2024-04-16 16:35:38 +02:00
Luca Frosini 489eed2787 Added the possibility to create any type of resource without gcube-model 2024-04-16 16:11:14 +02:00
Luca Frosini 483826685c improved tests 2024-04-15 11:00:02 +02:00
luca.frosini 179013ec5b Fixed context cache renew 2023-11-09 19:11:59 +01:00
9 changed files with 191 additions and 55 deletions

View File

@ -7,6 +7,9 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.gxhttp.reference.GXConnection;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.common.http.GXHTTPUtility;
@ -44,6 +47,7 @@ import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath;
import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath.SharingOperation;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.utils.JsonUtility;
import org.gcube.informationsystem.utils.TypeUtility;
import org.gcube.informationsystem.utils.UUIDManager;
import org.gcube.informationsystem.utils.UUIDUtility;
@ -107,47 +111,59 @@ public class ResourceRegistryPublisherImpl extends BaseRequestInfo implements Re
private void addHierarchicalMode(Map<String,String> queryParams) throws UnsupportedEncodingException{
if(hierarchicalMode) {
queryParams.put(AccessPath.HIERARCHICAL_MODE_QUERY_PARAMETER, Boolean.toString(hierarchicalMode));
queryParams.put(InstancePath.HIERARCHICAL_MODE_QUERY_PARAMETER, Boolean.toString(hierarchicalMode));
}
}
private void addIncludeContexts(Map<String,String> queryParams) throws UnsupportedEncodingException{
if(includeContexts) {
queryParams.put(AccessPath.INCLUDE_CONTEXTS_QUERY_PARAMETER, Boolean.toString(includeContexts));
queryParams.put(InstancePath.INCLUDE_CONTEXTS_QUERY_PARAMETER, Boolean.toString(includeContexts));
}
}
private void addIncludeMeta(Map<String,String> queryParams) throws UnsupportedEncodingException{
addIncludeMeta(queryParams, includeMeta);
}
private void addIncludeMeta(Map<String,String> queryParams, boolean includeMeta) throws UnsupportedEncodingException{
if(includeMeta) {
queryParams.put(AccessPath.INCLUDE_META_QUERY_PARAMETER, Boolean.toString(includeMeta));
queryParams.put(InstancePath.INCLUDE_META_QUERY_PARAMETER, Boolean.toString(includeMeta));
}
}
private void addIncludeAllMeta(Map<String,String> queryParams) throws UnsupportedEncodingException{
if(allMeta) {
queryParams.put(AccessPath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER, Boolean.toString(allMeta));
queryParams.put(InstancePath.INCLUDE_META_IN_ALL_INSTANCES_QUERY_PARAMETER, Boolean.toString(allMeta));
}
}
private void addOffset(Map<String,String> queryParams) throws UnsupportedEncodingException{
addOffset(queryParams, offset);
}
private void addOffset(Map<String,String> queryParams, Integer offset) throws UnsupportedEncodingException{
if(offset!=null) {
queryParams.put(AccessPath.OFFSET_QUERY_PARAMETER, offset.toString());
queryParams.put(InstancePath.OFFSET_QUERY_PARAMETER, offset.toString());
}
}
private void addLimit(Map<String,String> queryParams) throws UnsupportedEncodingException{
addLimit(queryParams, limit);
}
private void addLimit(Map<String,String> queryParams, Integer limit) throws UnsupportedEncodingException{
if(limit!=null) {
queryParams.put(AccessPath.LIMIT_QUERY_PARAMETER, limit.toString());
queryParams.put(InstancePath.LIMIT_QUERY_PARAMETER, limit.toString());
}
}
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() {
@Override
public List<Context> renew() throws ResourceRegistryException {
return getContextsFromServer();
return getAllContextFromServer(true, 0, BaseRequestInfo.UNBOUNDED_LIMIT);
}
};
@Override
@ -186,16 +202,22 @@ public class ResourceRegistryPublisherImpl extends BaseRequestInfo implements Re
* @return All Contexts read from server
* @throws ResourceRegistryException
*/
public List<Context> getContextsFromServer() throws ResourceRegistryException {
public List<Context> getAllContextFromServer() throws ResourceRegistryException {
return getAllContextFromServer(includeMeta, offset, limit);
}
protected List<Context> getAllContextFromServer(boolean includeMeta, Integer offset, Integer limit) throws ResourceRegistryException {
try {
logger.info("Going to read all {}s", Context.NAME);
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
gxHTTPStringRequest.header(ACCEPT_HTTP_HEADER_KEY, GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART);
Map<String,String> parameters = new HashMap<>();
addIncludeMeta(parameters);
addIncludeMeta(parameters, includeMeta);
addOffset(parameters, offset);
addLimit(parameters, limit);
gxHTTPStringRequest.queryParams(parameters);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
@ -422,8 +444,16 @@ public class ResourceRegistryPublisherImpl extends BaseRequestInfo implements Re
public String create(String json)
throws SchemaViolationException, AlreadyPresentException, ResourceRegistryException {
try {
ERElement e = ElementMapper.unmarshal(ERElement.class, json);
return internalCreate(e);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);
String type = TypeUtility.getTypeName(jsonNode);
UUID uuid = UUIDUtility.getUUID(jsonNode);
if(uuid==null) {
uuid = UUIDManager.getInstance().generateValidUUID();
((ObjectNode) jsonNode).put(ERElement.ID_PROPERTY, uuid.toString());
json = mapper.writeValueAsString(jsonNode);
}
return create(type, json, uuid);
} catch (ResourceRegistryException e) {
// logger.trace("Error Creating {}", facet, e);
throw e;

View File

@ -13,9 +13,8 @@ import org.gcube.common.authorization.utils.secret.JWTSecret;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.KeycloakClientHelper;
import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
@ -24,7 +23,6 @@ import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@SuppressWarnings("deprecation")
public class ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
@ -40,17 +38,15 @@ public class ContextTest {
public static final String NEXTNEXT;
public static final String DEVSEC;
public static final String DEVVRE;
protected static final Properties properties;
protected static final String CLIENT_ID_PROPERTY_KEY = "client_id";
protected static final String CLIENT_SECRET_PROPERTY_KEY = "client_secret";
protected static final String clientID;
protected static final String clientSecret;
public static final String TYPE_PROPERTY_KEY = "type";
public static final String USERNAME_PROPERTY_KEY = "username";
public static final String PASSWORD_PROPERTY_KEY = "password";
public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL";
public static final String RESOURCE_REGISTRY_URL;
static {
GCUBE = "/gcube";
@ -59,7 +55,7 @@ public class ContextTest {
DEVSEC = GCUBE + "/devsec";
DEVVRE = DEVSEC + "/devVRE";
PARENT_DEFAULT_TEST_SCOPE = "/gcube";
PARENT_DEFAULT_TEST_SCOPE = GCUBE;
DEFAULT_TEST_SCOPE = DEVNEXT;
ALTERNATIVE_TEST_SCOPE = NEXTNEXT;
@ -68,18 +64,16 @@ public class ContextTest {
try {
// load the properties file
properties.load(input);
clientID = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
clientSecret = properties.getProperty(CLIENT_SECRET_PROPERTY_KEY);
RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private enum Type{
USER, CLIENT_ID
};
public static void set(Secret secret) throws Exception {
SecretManagerProvider.instance.reset();
SecretManager secretManager = new SecretManager();
@ -89,15 +83,56 @@ public class ContextTest {
}
public static void setContextByName(String fullContextName) throws Exception {
logger.debug("Going to set credentials for context {}", fullContextName);
Secret secret = getSecretByContextName(fullContextName);
set(secret);
}
private static TokenResponse getJWTAccessToken(String context) throws Exception {
ScopeProvider.instance.set(context);
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null);
return tr;
Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
TokenResponse tr = null;
int index = context.indexOf('/', 1);
String root = context.substring(0, index == -1 ? context.length() : index);
switch (type) {
case CLIENT_ID:
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
String clientSecret = properties.getProperty(root);
tr = KeycloakClientFactory.newInstance().queryUMAToken(context, clientId, clientSecret, context, null);
break;
case USER:
default:
String username = properties.getProperty(USERNAME_PROPERTY_KEY);
String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
switch (root) {
case "/gcube":
default:
clientId = "next.d4science.org";
break;
case "/pred4s":
clientId = "pre.d4science.org";
break;
case "/d4science.research-infrastructures.eu":
clientId = "services.d4science.org";
break;
}
clientSecret = null;
tr = KeycloakClientHelper.getTokenForUser(context, username, password);
break;
}
return tr;
}
public static Secret getSecretByContextName(String context) throws Exception {
@ -117,7 +152,7 @@ public class ContextTest {
}
public static String getUser() {
String user = Metadata.UNKNOWN_USER;
String user = "UNKNOWN";
try {
user = SecretManagerProvider.instance.get().getUser().getUsername();
} catch(Exception e) {

View File

@ -86,8 +86,9 @@ public class ERManagementTest extends ContextTest {
protected ResourceRegistryPublisher resourceRegistryPublisher;
public ERManagementTest() {
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL);
Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
}else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
}
@ -585,5 +586,11 @@ public class ERManagementTest extends ContextTest {
// resourceManagement.update();
//
// }
// @Test
// public void testCreateResourceFromJson() throws Exception {
// String json = "{\"type\":\"VirtualService\",\"consistsOf\":[{\"type\":\"IsIdentifiedBy\",\"target\":{\"name\":\"aaaaaa\",\"optional\":\"true\",\"group\":\"xxxxx\",\"description\":\"\",\"version\":\"cvcvvv\",\"qualifier\":\"\",\"type\":\"SoftwareFacet\",\"aaaa\":\"dddddd\"}}]}";
// String ret = resourceRegistryPublisher.create(json);
// }
}

View File

@ -0,0 +1,46 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.publisher;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* To properly use thois test you must comment
* gcube-model dependency
* @author Luca Frosini (ISTI - CNR)
*/
public class NoGcubeModelTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(NoGcubeModelTest.class);
public static final String GROUP = "InformationSystem";
public static final String NAME = "resource-registry";
public static final String VERSION = "1.0.0";
public static final String NEW_VERSION = "2.0.0";
protected ResourceRegistryPublisher resourceRegistryPublisher;
public NoGcubeModelTest() {
Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
}else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
}
resourceRegistryPublisher.setIncludeMeta(true);
resourceRegistryPublisher.setAllMeta(true);
}
@Ignore
@Test
public void testCreateResourceFromJson() throws Exception {
String json = "{\"type\":\"VirtualService\",\"consistsOf\":[{\"type\":\"IsIdentifiedBy\",\"target\":{\"name\":\"aaaaaa\",\"optional\":\"true\",\"group\":\"xxxxx\",\"description\":\"\",\"version\":\"cvcvvv\",\"qualifier\":\"\",\"type\":\"SoftwareFacet\",\"aaaa\":\"dddddd\"}}]}";
String ret = resourceRegistryPublisher.createResource(json);
logger.info(ret);
}
}

View File

@ -85,8 +85,9 @@ public class SmartgearResourcesTest extends ERManagementTest {
protected ResourceRegistryPublisher resourceRegistryPublisher;
public SmartgearResourcesTest() {
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL);
Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
}else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
}

View File

@ -84,9 +84,10 @@ public class ERManagementTest extends ContextTest {
protected ResourceRegistryClient resourceRegistryClient;
public ERManagementTest() {
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL);
resourceRegistryClient = new ResourceRegistryClientImpl(ContextTest.RESOURCE_REGISTRY_URL);
Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
resourceRegistryClient = new ResourceRegistryClientImpl(rrURLOBj.toString());
}else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
resourceRegistryClient = ResourceRegistryClientFactory.create();

View File

@ -42,8 +42,9 @@ public class EntityManagementTest extends ContextTest {
protected ResourceRegistryPublisher resourceRegistryPublisher;
public EntityManagementTest(){
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL);
Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
}else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
}

View File

@ -72,10 +72,10 @@ public class MultiContextTest extends ContextTest {
protected ResourceRegistryClient resourceRegistryClient;
public MultiContextTest() throws Exception {
// ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
if(ContextTest.RESOURCE_REGISTRY_URL !=null && !ContextTest.RESOURCE_REGISTRY_URL.isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(ContextTest.RESOURCE_REGISTRY_URL);
resourceRegistryClient = new ResourceRegistryClientImpl(ContextTest.RESOURCE_REGISTRY_URL);
Object rrURLOBj = ContextTest.properties.get(RESOURCE_REGISTRY_URL_PROPERTY);
if(rrURLOBj!=null && !rrURLOBj.toString().isEmpty()) {
resourceRegistryPublisher = new ResourceRegistryPublisherImpl(rrURLOBj.toString());
resourceRegistryClient = new ResourceRegistryClientImpl(rrURLOBj.toString());
}else {
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
resourceRegistryClient = ResourceRegistryClientFactory.create();

View File

@ -1,7 +1,22 @@
client_id=junit.tests.luca.frosini
client_secret=6b0d0222-3130-41f1-a86d-6b23993547be
#RESOURCE_REGISTRY_URL=http://localhost:8080/resource-registry
#RESOURCE_REGISTRY_URL=http://pc-frosini.isti.cnr.it:8080/resource-registry
#RESOURCE_REGISTRY_URL=http://resourceregistry1-d-d4s.d4science.org/resource-registry
#RESOURCE_REGISTRY_URL=https://resource-registry.dev.d4science.org/resource-registry
#RESOURCE_REGISTRY_URL=http://url.gcube.d4science.org/resource-registry
type=USER
#type=CLIENT_ID
username=luca.frosini
password=Pupp4M3l0
#username=grsf.publisher
#password=grsfcambiami
#clientId=grsf-publisher
#/gcube=54cc7454-1a84-11ee-be56-0242ac120002
#/pred4s=de293f60-e004-45ce-b64f-20de77fad888
#/d4science.research-infrastructures.eu=
#client_id=junit.tests.luca.frosini
#/gcube=6b0d0222-3130-41f1-a86d-6b23993547be
#clientId=gcat
#/gcube=712add63-c642-4a6a-8ddc-283dc978120a
#/pred4s=4e35a122-8f25-48f9-9788-8588ee86633f
#/d4science.research-infrastructures.eu=