Introduce error codes for the delete context method. Query the RR to validate the delete request.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@162342 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Manuele Simi 2018-01-19 05:05:49 +00:00
parent f1e50d4d15
commit 865c2255f7
6 changed files with 72 additions and 17 deletions

View File

@ -1,7 +1,7 @@
package org.gcube.resourcemanagement.manager.io.rs;
/**
* REST paths for the RMContext exposed by the webapp.
* REST paths and parameters for the RMContext resource exposed by the webapp.
*
* @author Manuele Simi (ISTI - CNR)
*
@ -15,12 +15,19 @@ public class RMContextPath {
public static final String CREATE_PATH_PART = "create";
public static final String DELETE_PATH_PART = "delete";
/* PARAMETERS FOR SOME REQUESTS*/
/**
* Force the service to use the ResourceRegistry instance at the given URL.
*/
public static final String FORCE_RRURL_PARAM = "rrURL";
/**
* The identifier of the resource on which the method will operate.
*/
public static final String UUID_PARAM = "ContextUUID";

View File

@ -3,7 +3,7 @@ package org.gcube.resourcemanagement.manager.io.rs;
import org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode;
/**
* Error codes returned by the context resource.
* Error codes returned by the create method of the context resource.
*
* @author Manuele Simi (ISTI CNR)
*
@ -25,11 +25,15 @@ public enum RMCreateContextCode implements ErrorCode {
this.id = id;
this.msg = msg;
}
/* (non-Javadoc)
* @see org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode#getId()
*/
public int getId() {
return this.id;
}
/* (non-Javadoc)
* @see org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode#getMessage()
*/
public String getMessage() {
return this.msg;
}

View File

@ -0,0 +1,37 @@
package org.gcube.resourcemanagement.manager.io.rs;
import org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode;
/**
* Error codes returned by the delete method of the context resource.
*
* @author Manuele Simi (ISTI CNR)
*
*/
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.");
private int id;
private String msg;
private RMDeleteContextCode(int id, String msg) {
this.id = id;
this.msg = msg;
}
/* (non-Javadoc)
* @see org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode#getId()
*/
public int getId() {
return this.id;
}
/* (non-Javadoc)
* @see org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode#getMessage()
*/
public String getMessage() {
return this.msg;
}
}

View File

@ -4,6 +4,8 @@ import java.util.Objects;
import java.util.UUID;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.resourcemanagement.manager.io.codeexceptions.WebCodeException;
import org.gcube.resourcemanagement.manager.io.rs.RMDeleteContextCode;
/**
* Delete request for a context.
@ -28,7 +30,9 @@ public final class DeleteRequest extends RequestToResourceRegistry {
*/
@Override
public RequestToResourceRegistry validate() throws Exception {
// TODO check if the context exists
if (!new Queries().contextExists(context, resourceRegistryContextClient)) {
ResponseFromResourceRegistry.fromException(new WebCodeException(RMDeleteContextCode.CONTEXT_DOES_NOT_EXIST));
}
return null;
}
@ -38,13 +42,15 @@ public final class DeleteRequest extends RequestToResourceRegistry {
@Override
protected ResponseFromResourceRegistry send() {
try {
boolean created = resourceRegistryContextClient.delete(context);
boolean deleted = resourceRegistryContextClient.delete(context);
if (!deleted) {
ResponseFromResourceRegistry.fromException(new WebCodeException(RMDeleteContextCode.GENERIC_ERROR_FROM_RR));
}
} catch (ResourceRegistryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ResponseFromResourceRegistry.fromException(new WebCodeException(RMDeleteContextCode.GENERIC_ERROR_FROM_RR));
}
return null;
return ResponseFromResourceRegistry.newSuccessResponseWithMessage("Context successfully deleted");
}
}

View File

@ -45,7 +45,7 @@ public class RMContext {
logger.info("Force URL: " + rrURL);
ContextHolder holder = new ContextHolder(json);
ResponseFromResourceRegistry returned = CreateRequest.fromHolder(holder).forceURL(rrURL).submit();
return this.buildResponse(returned,"Context successfully created.","Failed to create the context.");
return this.buildResponse(returned,Status.CREATED,"Context successfully created.","Failed to create the context.");
}
/*
@ -59,7 +59,7 @@ public class RMContext {
logger.info("Requested to delete context with id {}", uuid);
logger.info("Force URL: " + rrURL);
ResponseFromResourceRegistry returned = DeleteRequest.fromUUID(UUID.fromString(uuid)).forceURL(rrURL).submit();
return this.buildResponse(returned,"Context successfully deleted.","Failed to delete the context.");
return this.buildResponse(returned,Status.OK,"Context successfully deleted.","Failed to delete the context.");
}
/**
@ -69,9 +69,9 @@ public class RMContext {
* @param failedMessage
* @return the response
*/
private Response buildResponse(ResponseFromResourceRegistry returned, String successMessage, String failedMessage) {
private Response buildResponse(ResponseFromResourceRegistry returned, Status successStatus, String successMessage, String failedMessage) {
if (returned.wasSuccessful()) {
return Response.status(Status.CREATED)
return Response.status(successStatus)
.entity(returned.getMessage().map(m -> m).orElse(successMessage))
.type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8).build();
} else {

View File

@ -145,11 +145,12 @@ public class RMContextTest extends JerseyTest {
if (skipTest)
return;
try {
Response create = target(RMContextPath.CONTEXT_ROOT).path(context1UUID.toString())
Response delete = target(RMContextPath.CONTEXT_ROOT).path(context1UUID.toString())
.queryParam(RMContextPath.FORCE_RRURL_PARAM, RR)
.request().delete();
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
Status.OK.getStatusCode(), create.getStatus());
assertNotNull(delete);
assertEquals("Unexpected returned code. Reason: " + delete.getStatusInfo().getReasonPhrase(),
Status.OK.getStatusCode(), delete.getStatus());
} catch (Exception e) {
assertFalse("Failed to delete the context.", false);
}