Overload the Queries constructor to create an instance with the given context client. Add check if an context is empty before deleting it (in progress).

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@164573 82a268e6-3cf1-43bd-a215-b396298e98cf
master
Manuele Simi 6 years ago
parent 89f4730523
commit 5a6ae07d22

@ -9,9 +9,9 @@ import org.gcube.common.gxrest.response.outbound.ErrorCode;
*
*/
public enum RMDeleteContextCode implements ErrorCode {
CONTEXT_DOES_NOT_EXIST(1, "The context does not exist."),
GENERIC_ERROR_FROM_RR(2, "The Resource Registry returned an error.");
GENERIC_ERROR_FROM_RR(1, "The Resource Registry returned an error."),
CONTEXT_DOES_NOT_EXIST(2, "The context does not exist."),
CONTEXT_IS_NOT_EMPTY(3, "The context is not empty. It cannot be deleted. Use force parameter to delete.");
private int id;
private String msg;

@ -37,7 +37,7 @@ public final class CreateRequest extends RequestToResourceRegistry {
try {
IsParentOf<Context, Context> relationship = this.context.getParent();
logger.info("Fetching parent with UUID: " + relationship.getSource().getHeader().getUUID().toString());
Context parent = new Queries().fetchContext(relationship.getSource().getHeader().getUUID(), this.getContextClient());
Context parent = new Queries(this.getContextClient()).fetchContext(relationship.getSource().getHeader().getUUID());
if (Objects.isNull(parent)) {
throw new LocalCodeException(RMCreateContextCode.CONTEXT_PARENT_DOES_NOT_EXIST);
}

@ -30,9 +30,13 @@ public final class DeleteRequest extends RequestToResourceRegistry {
*/
@Override
public void validate() throws LocalCodeException {
if (!new Queries().contextExists(context, this.getContextClient())) {
Queries queries = new Queries(this.getContextClient());
if (!queries.contextExists(context)) {
throw new LocalCodeException(RMDeleteContextCode.CONTEXT_DOES_NOT_EXIST);
}
if (queries.isContextEmpty(context)) {
throw new LocalCodeException(RMDeleteContextCode.CONTEXT_IS_NOT_EMPTY);
}
}
/* (non-Javadoc)

@ -1,6 +1,5 @@
package org.gcube.resourcemanagement.manager.webapp.context;
import java.util.Objects;
import java.util.UUID;
import org.gcube.informationsystem.model.entity.Context;
@ -16,18 +15,30 @@ import org.gcube.informationsystem.resourceregistry.context.ResourceRegistryCont
*/
final class Queries {
private final ResourceRegistryContextClient localRegistryClient = ResourceRegistryContextClientFactory.create();
private final ResourceRegistryContextClient client;
protected Queries() {
client = ResourceRegistryContextClientFactory.create();
}
/**
* Makes the queries with the given client.
*
* @param client
*/
protected Queries(ResourceRegistryContextClient client) {
this.client = client;
}
/**
* Tests if the given context exists
*
* @param uuid
* @param registryClient
* @return
*/
protected boolean contextExists(UUID uuid, ResourceRegistryContextClient ... registryClient) {
protected boolean contextExists(UUID context) {
try {
return client(registryClient).read(uuid) != null;
return this.client.read(context) != null;
} catch (ResourceRegistryException e) {
return false;
}catch (RuntimeException e) {
@ -37,24 +48,28 @@ final class Queries {
}
/**
* Fetches the context.
* Fetches the resources context.
*
* @param uuid
* @param registryClient
* @param context the resource context to fetch
* @return
*/
protected Context fetchContext(UUID uuid, ResourceRegistryContextClient ... registryClient) {
protected Context fetchContext(UUID context) {
try {
return client(registryClient).read(uuid);
return this.client.read(context);
} catch (ResourceRegistryException e) {
return null;
}
}
private ResourceRegistryContextClient client(ResourceRegistryContextClient ... registryClient) {
if (registryClient.length > 0 && Objects.nonNull(registryClient[0]))
return registryClient[0];
else
return this.localRegistryClient;
/**
* Detects if the context has no resources.
*
* @param context the context to check
* @return
*/
protected boolean isContextEmpty(UUID context) {
return false;
}
}

@ -44,7 +44,7 @@ public abstract class RequestToResourceRegistry {
this.validate();
} catch (LocalCodeException e) {
ResponseFromResourceRegistry response = ResponseFromResourceRegistry.
fromException(new Exception());
fromErrorCode(e);
return response;
}
return this.validateResponse(this.send());
@ -65,7 +65,11 @@ public abstract class RequestToResourceRegistry {
* @return the context client
*/
ResourceRegistryContextClient getContextClient() {
return resourceRegistryContextClient;
if (Objects.nonNull(this.resourceRegistryContextClient)) {
return this.resourceRegistryContextClient;
} else {
return ResourceRegistryContextClientFactory.create();
}
}
/**

@ -91,8 +91,8 @@ public class QueriesTest {
if (skipTest)
return;
ResourceRegistryContextClient client = new ResourceRegistryContextClientImpl(RR);
Queries queries = new Queries();
assertFalse("Context does exist, but it should not", queries.contextExists(UUID.randomUUID(),client));
Queries queries = new Queries(client);
assertFalse("Context does exist, but it should not", queries.contextExists(UUID.randomUUID()));
}
/**
@ -103,8 +103,8 @@ public class QueriesTest {
if (skipTest)
return;
ResourceRegistryContextClient client = new ResourceRegistryContextClientImpl(RR);
Queries queries = new Queries();
Context context = queries.fetchContext(context1UUID,client);
Queries queries = new Queries(client);
Context context = queries.fetchContext(context1UUID);
assertNotNull("Context does not exist.", context);
assertEquals("Unexpected uuid for context", context1UUID, context.getHeader().getUUID());
}

Loading…
Cancel
Save