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
This commit is contained in:
Manuele Simi 2018-02-24 05:01:11 +00:00
parent 89f4730523
commit 5a6ae07d22
6 changed files with 49 additions and 26 deletions

View File

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

View File

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

View File

@ -30,9 +30,13 @@ public final class DeleteRequest extends RequestToResourceRegistry {
*/ */
@Override @Override
public void validate() throws LocalCodeException { 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); throw new LocalCodeException(RMDeleteContextCode.CONTEXT_DOES_NOT_EXIST);
} }
if (queries.isContextEmpty(context)) {
throw new LocalCodeException(RMDeleteContextCode.CONTEXT_IS_NOT_EMPTY);
}
} }
/* (non-Javadoc) /* (non-Javadoc)

View File

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

View File

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

View File

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