Skip to content

Commit

Permalink
rollback workflowResource
Browse files Browse the repository at this point in the history
  • Loading branch information
LuciaM1 committed Nov 24, 2023
1 parent e0def8f commit 26a0714
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
import it.gov.pagopa.atmlayer.service.model.dto.DeployResponseDto;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import java.io.File;

@RegisterRestClient(configKey = "process-deploy")
public interface ProcessClient {
@POST
@Path("/api/v1/processes/deploy/{type}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
Uni<DeployResponseDto> deploy(@FormParam("url") String url, @PathParam("type") String type);

@GET
@Path("/api/v1/processes/deploy/{id}/data")
@Produces(MediaType.APPLICATION_XML)
Uni<File> getDeployedResource(@PathParam("id") String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public enum AppErrorCodeEnum {
RESOURCE_DOES_NOT_EXIST("ATMLM_4000029", "The referenced Resource does not exist", NOT_EXISTING_REFERENCED_ENTITY),
WORKFLOW_RESOURCE_CANNOT_BE_UPDATED("ATMLM_4000030", "The referenced Workflow Resource file can not be updated", NOT_UPDATABLE),
RESOURCE_FILE_DOES_NOT_EXIST("ATMLM_4000031", "The referenced Resource file does not exist", NOT_EXISTING_REFERENCED_ENTITY),
WORKFLOW_RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS("ATMLM_4000032","A workflow resource with the same content already exists", CONSTRAINT_VIOLATION);
WORKFLOW_RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS("ATMLM_4000032","A workflow resource with the same content already exists", CONSTRAINT_VIOLATION),
DEPLOYED_FILE_WAS_NOT_RETRIEVED("ATMLM_4000033","Error with Process communication: the referenced file was not retrieved", INTERNAL),
WORKFLOW_RESOURCE_NOT_DEPLOYED_CANNOT_ROLLBACK("ATMLM_4000034","CamundaDefinitionId of the referenced resource is null: cannot rollback", NOT_EXISTING_REFERENCED_ENTITY);

private final String errorCode;
private final String errorMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,18 @@ public Uni<Void> delete(@PathParam("uuid") UUID uuid) {
public Uni<WorkflowResourceDTO> update(@RequestBody(required = true) @FormParam("file") File file,
@PathParam("uuid") UUID uuid) throws NoSuchAlgorithmException, IOException {

return workflowResourceService.update(uuid, file)
return workflowResourceService.update(uuid, file,false)
.onItem()
.transformToUni(updatedWorkflowResource -> Uni.createFrom().item(workflowResourceMapper.toDTO(updatedWorkflowResource)));
}

@PUT
@Path("/rollback/{uuid}")
@Produces(MediaType.APPLICATION_JSON)
public Uni<WorkflowResourceDTO> rollback(@PathParam("uuid") UUID uuid) throws NoSuchAlgorithmException, IOException {
return workflowResourceService.rollback(uuid)
.onItem()
.transformToUni(rolledBackWorkflowResource -> Uni.createFrom().item(workflowResourceMapper.toDTO(rolledBackWorkflowResource)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ public interface WorkflowResourceService {

Uni<List<WorkflowResource>> getAll();

Uni<WorkflowResource> update(UUID id, File file) throws NoSuchAlgorithmException, IOException;
Uni<WorkflowResource> update(UUID id, File file,boolean isRollback) throws NoSuchAlgorithmException, IOException;

Uni<WorkflowResource> rollback(UUID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
import java.util.UUID;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.ATMLM_500;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.DEPLOYED_FILE_WAS_NOT_RETRIEVED;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.DEPLOY_ERROR;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.OBJECT_STORE_SAVE_FILE_ERROR;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.WORKFLOW_FILE_DOES_NOT_EXIST;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.WORKFLOW_RESOURCE_CANNOT_BE_UPDATED;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.WORKFLOW_RESOURCE_FILE_WITH_SAME_CAMUNDA_DEFINITION_KEY_ALREADY_EXISTS;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.WORKFLOW_RESOURCE_FILE_WITH_SAME_CONTENT_ALREADY_EXIST;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.WORKFLOW_RESOURCE_NOT_DEPLOYED_CANNOT_ROLLBACK;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.WORKFLOW_RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS;
import static it.gov.pagopa.atmlayer.service.model.utils.FileUtilities.calculateSha256;
import static it.gov.pagopa.atmlayer.service.model.utils.FileUtilities.extractIdValue;
Expand Down Expand Up @@ -300,7 +302,7 @@ public Uni<List<WorkflowResource>> getAll() {

@Override
@WithTransaction
public Uni<WorkflowResource> update(UUID id, File file) throws NoSuchAlgorithmException, IOException {
public Uni<WorkflowResource> update(UUID id, File file,boolean isRollback) throws NoSuchAlgorithmException, IOException {
return this.findById(id)
.onItem()
.transformToUni(Unchecked.function(workflow -> {
Expand All @@ -321,9 +323,12 @@ public Uni<WorkflowResource> update(UUID id, File file) throws NoSuchAlgorithmEx
if (!workflowFound.getDefinitionKey().equals(definitionKey)) {
throw new AtmLayerException(String.format("The definition key in your Workflow Resource: %s does not match the Workflow Resource you are trying to update", definitionKey), Response.Status.BAD_REQUEST, WORKFLOW_RESOURCE_CANNOT_BE_UPDATED);
}
if (workflowFound.getStatus().equals(StatusEnum.DEPLOYED)) {
if (!isRollback && workflowFound.getStatus().equals(StatusEnum.DEPLOYED)) {
workflowFound.setStatus(StatusEnum.UPDATED_BUT_NOT_DEPLOYED);
}
if (isRollback && workflowFound.getStatus().equals(StatusEnum.UPDATED_BUT_NOT_DEPLOYED)) {
workflowFound.setStatus(StatusEnum.DEPLOYED);
}
Date date = new Date();
workflowFound.setLastUpdatedAt(new Timestamp(date.getTime()));
workflowFound.getResourceFile().setLastUpdatedAt(new Timestamp(date.getTime()));
Expand All @@ -336,4 +341,27 @@ public Uni<WorkflowResource> update(UUID id, File file) throws NoSuchAlgorithmEx


}

@Override
public Uni<WorkflowResource> rollback(UUID id) {
return this.findById(id)
.onItem()
.transformToUni(Unchecked.function(workflow -> {
if (workflow.isEmpty()) {
throw new AtmLayerException(Response.Status.NOT_FOUND, WORKFLOW_FILE_DOES_NOT_EXIST);
}
WorkflowResource workflowResourceToRollBack = workflow.get();
String camundaId=workflowResourceToRollBack.getCamundaDefinitionId();
if(camundaId==null){
throw new AtmLayerException("CamundaDefinitionId of the referenced resource is null: cannot rollback", Response.Status.NOT_FOUND,WORKFLOW_RESOURCE_NOT_DEPLOYED_CANNOT_ROLLBACK);
}
return processClient.getDeployedResource(camundaId)
.onItem()
.transformToUni(Unchecked.function(file -> update(id,file,true)))
.onFailure()
.recoverWithUni(exception ->
Uni.createFrom().failure(new AtmLayerException("Error retrieving workflow resource from Process", Response.Status.INTERNAL_SERVER_ERROR,DEPLOYED_FILE_WAS_NOT_RETRIEVED))
);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void testUpdate() throws NoSuchAlgorithmException, IOException {
WorkflowResourceDTO workflowResourceDTO = new WorkflowResourceDTO();
UUID uuid = UUID.randomUUID();

when(workflowResourceService.update(any(UUID.class), any(File.class)))
when(workflowResourceService.update(any(UUID.class), any(File.class),any(Boolean.class)))
.thenReturn(Uni.createFrom().item(workflowResource));
when(workflowResourceMapper.toDTO(any(WorkflowResource.class))).thenReturn(workflowResourceDTO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void deleteNotDeletableStatus() {
void updateResourceDoesNotExist() throws NoSuchAlgorithmException, IOException {
File file = new File("src/test/resources/Test.bpmn");
when(workflowResourceRepository.findById(any(UUID.class))).thenReturn(Uni.createFrom().nullItem());
workflowResourceService.update(UUID.randomUUID(), file)
workflowResourceService.update(UUID.randomUUID(), file,false)
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(AtmLayerException.class, "The referenced Workflow Resource file does not exist");
}
Expand Down

0 comments on commit 26a0714

Please sign in to comment.