Skip to content

Commit

Permalink
sonarcloud fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
LuciaM1 committed Sep 20, 2024
1 parent 067f756 commit 4a34bdc
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List<ResourceCreationDto> convertToResourceCreationDtoList(ResourceMultip
for (int i = 0; i < multipleDto.getFilenameList().size(); i++) {
ResourceCreationDto resourceCreationDto = new ResourceCreationDto();

resourceCreationDto.setFile(fromStringToFile(multipleDto.getFileList().get(i), multipleDto.getPath()));
resourceCreationDto.setFile(fromStringToFile(multipleDto.getFileList().get(i)));
resourceCreationDto.setFilename(multipleDto.getFilenameList().get(i));
resourceCreationDto.setResourceType(multipleDto.getResourceType());
resourceCreationDto.setPath(multipleDto.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,6 @@ public BpmnResource(BpmnVersionService bpmnVersionService, BpmnBankConfigService
this.tracer = tracer;
}

// @GET
// @Produces(MediaType.APPLICATION_JSON)
// public Uni<List<BpmnDTO>> getAllBpmn() {
// return this.bpmnVersionService.getAll()
// .onItem()
// .transform(Unchecked.function(list -> {
// if (list.isEmpty()) {
// log.info("No BPMN files saved in database");
// }
// return bpmnVersionMapper.toDTOList(list);
// }));
// }

@GET
@Path("/{bpmnId}/version/{version}")
@Consumes(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,6 @@ public Uni<ResourceDTO> updateResource(@RequestBody(required = true) @FormParam(
.transformToUni(updatedResource -> Uni.createFrom().item(resourceEntityMapper.toDTO(updatedResource)));
}

// @GET
// @Produces(MediaType.APPLICATION_JSON)
// public Uni<List<ResourceDTO>> getAll() {
// return this.resourceEntityService.getAll()
// .onItem()
// .transform(Unchecked.function(list -> {
// if (list.isEmpty()) {
// log.info("No Resource files saved in database");
// }
// return resourceEntityMapper.toDTOList(list);
// }));
// }

@GET
@Path("/{uuid}")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,6 @@ public WorkflowResourceResource(WorkflowResourceService workflowResourceService,
this.tracer = tracer;
}

// @GET
// @Produces(MediaType.APPLICATION_JSON)
// public Uni<List<WorkflowResourceDTO>> getAll() {
// return this.workflowResourceService.getAll()
// .onItem()
// .transform(Unchecked.function(list -> {
// if (list.isEmpty()) {
// log.info("No Workflow Resource files saved in database");
// }
// return workflowResourceMapper.toDTOList(list);
// }));
// }

@GET
@Path("/filter")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,39 +152,37 @@ public Uni<List<String>> createResourceMultiple(List<ResourceEntity> resourceEnt
return findBySHA256(resourceEntity.getSha256())
.onItem().transformToUni(x -> {
if (x.isPresent()) {
errors.add(String.format("File %s: Esiste già una risorsa con lo stesso contenuto", filename));
errors.add(String.format("%s-Esiste già una risorsa con lo stesso contenuto", filename));
return Uni.createFrom().nullItem();
}
return resourceFileService.findByStorageKey(resourceEntity.getStorageKey())
.onItem()
.transformToUni(resource -> {
if (resource.isPresent()) {
errors.add(String.format("File %s: Impossibile caricare: la risorsa con lo stesso nome file e percorso esiste già", filename));
errors.add(String.format("%s-Impossibile caricare: la risorsa con lo stesso nome file e percorso esiste già", filename));
return Uni.createFrom().nullItem();
}
return saveAndUpload(resourceEntity, file, filename, resourceCreationDtoList.get(index).getPath())
.onItem().transformToUni(bpmn -> this.findByUUID(resourceEntity.getResourceId())
.onItem().transformToUni(optionalResource -> {
if (optionalResource.isEmpty()) {
errors.add(String.format("File %s: Problema di sincronizzazione sulla creazione della risorsa", filename));
errors.add(String.format("%s-Problema di sincronizzazione sulla creazione della risorsa", filename));
return Uni.createFrom().nullItem();
}
return Uni.createFrom().item(optionalResource.get());
}));
});
})
.onFailure().recoverWithItem(throwable -> {
errors.add(String.format("File %s: %s", filename, throwable.getMessage()));
errors.add(String.format("%s: %s", filename, throwable.getMessage()));
return null;
});
})
.collect().asList()
.onItem().transform(resourceDTOList -> {
if (!errors.isEmpty()) {
throw new AtmLayerException("Alcuni file non sono stati creati: " + String.join(", ", errors),
throw new AtmLayerException("Errore nel caricamento dovuto ai seguenti file: " + String.join(", ", errors),
Response.Status.BAD_REQUEST, RESOURCES_CREATION_ERROR);
} else {
errors.add("file creati senza errori");
}
return errors; // This will be empty if no errors occurred
});
Expand All @@ -202,9 +200,6 @@ public Uni<ResourceEntity> updateResource(UUID uuid, File file) {
}
ResourceEntity resourceEntity = optionalResource.get();
String newFileSha256 = calculateSha256(file);
// if (Objects.equals(resourceEntity.getSha256(), newFileSha256)) {
// throw new AtmLayerException("La risorsa è già presente", Response.Status.BAD_REQUEST, RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS);
// }
return findBySHA256(newFileSha256)
.onItem().transformToUni(Unchecked.function(x -> {
if (x.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@
@ApplicationScoped
@Slf4j
public class ResourceEntityStorageServiceImpl implements ResourceEntityStorageService {
@Inject
ObjectStoreStrategy objectStoreStrategy;
private final ObjectStoreStrategy objectStoreStrategy;
private final ObjectStoreService objectStoreService;
@Inject
ObjectStoreProperties objectStoreProperties;
@Inject
ResourceFileService resourceFileService;
private final ObjectStoreProperties objectStoreProperties;
private final ResourceFileService resourceFileService;

@Inject
public ResourceEntityStorageServiceImpl(ObjectStoreStrategy objectStoreStrategy,
ObjectStoreProperties objectStoreProperties) {
ObjectStoreProperties objectStoreProperties,
ResourceFileService resourceFileService) {
this.objectStoreStrategy = objectStoreStrategy;
this.objectStoreService = objectStoreStrategy.getType(
ObjectStoreStrategyEnum.fromValue(objectStoreProperties.type()));
this.objectStoreProperties = objectStoreProperties;
this.resourceFileService = resourceFileService;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
Expand Down Expand Up @@ -93,10 +94,10 @@ public static String toHexString(byte[] hash) {
return hexString.toString();
}

public static File fromStringToFile(String fileBase64, String fileName) {
public static File fromStringToFile(String fileBase64) {
try {
byte[] decodedBytes = Base64.getDecoder().decode(fileBase64);
File tempFile = File.createTempFile("tempfile", ".tmp");
File tempFile = Files.createTempFile("tempfile", ".tmp").toFile();
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
fos.write(decodedBytes);
}
Expand All @@ -111,25 +112,4 @@ public static File fromStringToFile(String fileBase64, String fileName) {
}


// public static boolean isExtensionValid(File file, String fileName) throws IOException, MimeTypeException {
// String detectedExtension = getExtension(file);
// String extension = FilenameUtils.getExtension(fileName);
// if (Objects.equals(extension, "bpmn") || Objects.equals(extension, "dmn")) {
// extension = UtilityValues.XML_EXTENSION.getValue();
// }
// if (Objects.equals(extension, "form")) {
// extension = UtilityValues.TXT_EXTENSION.getValue();
// }
// return Objects.equals(extension, detectedExtension);
// }
//
// public static String getExtension(File file) throws IOException, MimeTypeException {
// Tika tika = new Tika();
// String mimeType = tika.detect(file);
// log.info("Detected mimeType: {}", mimeType);
// MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
// MimeType type = allTypes.forName(mimeType);
// return type.getExtension().replace(".", "");
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void testJsonPropertyOrder() {
.message("Invalid input data")
.build();

ObjectMapper objectMapper = new ObjectMapper();
try {
String json = objectMapper.writeValueAsString(errorResponse);
assertTrue(json.contains("\"type\":\"Validation Error\""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,66 +47,6 @@ class BpmnResourceTest {
@InjectMock
BpmnBankConfigService bpmnBankConfigService;

// @Test
// void testGetAllBpmn() {
// List<BpmnVersion> bpmnList = new ArrayList<>();
// BpmnVersion bpmnVersion = new BpmnVersion();
// bpmnList.add(bpmnVersion);
// List<BpmnDTO> dtoList = new ArrayList<>();
// BpmnDTO bpmnDTO = new BpmnDTO();
// dtoList.add(bpmnDTO);
// when(bpmnVersionService.getAll()).thenReturn(Uni.createFrom().item(bpmnList));
// when(bpmnVersionMapper.toDTOList(any(ArrayList.class))).thenReturn(dtoList);
// ArrayList result = given()
// .when().get("/api/v1/model/bpmn")
// .then()
// .statusCode(200)
// .extract()
// .body()
// .as(ArrayList.class);
// assertEquals(1, result.size());
// verify(bpmnVersionService, times(1)).getAll();
// verify(bpmnVersionMapper, times(1)).toDTOList(bpmnList);
// }

// @Test
// void testGetAllEmptyList() {
// List<BpmnVersion> bpmnList = new ArrayList<>();
// List<BpmnDTO> dtoList = new ArrayList<>();
// when(bpmnVersionService.getAll()).thenReturn(Uni.createFrom().item(bpmnList));
// when(bpmnVersionMapper.toDTOList(any(ArrayList.class))).thenReturn(dtoList);
// ArrayList result = given()
// .when().get("/api/v1/model/bpmn")
// .then()
// .statusCode(200)
// .extract()
// .body()
// .as(ArrayList.class);
// assertEquals(0, result.size());
// verify(bpmnVersionService, times(1)).getAll();
// verify(bpmnVersionMapper, times(1)).toDTOList(bpmnList);
// }

// @Test
// void testGetAllBpmnEmptyList() {
// List<BpmnVersion> bpmnList = new ArrayList<>();
// BpmnVersion bpmnVersion = new BpmnVersion();
// bpmnList.add(bpmnVersion);
// List<BpmnDTO> dtoList = new ArrayList<>();
// when(bpmnVersionService.getAll()).thenReturn(Uni.createFrom().item(bpmnList));
// when(bpmnVersionMapper.toDTOList(any(ArrayList.class))).thenReturn(dtoList);
// ArrayList result = given()
// .when().get("/api/v1/model/bpmn")
// .then()
// .statusCode(200)
// .extract()
// .body()
// .as(ArrayList.class);
// Assertions.assertTrue(result.isEmpty());
// verify(bpmnVersionService, times(1)).getAll();
// verify(bpmnVersionMapper, times(1)).toDTOList(bpmnList);
// }

@Test
void testGetEncodedFileOK() {
BpmnVersion bpmnVersion = new BpmnVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,6 @@ void testUpdateResource() {
assertEquals(resourceDTO, result);
}


// @Test
// void testGetAllResources() {
// List<ResourceEntity> resourceEntities = new ArrayList<>();
// ResourceEntity resourceEntity = new ResourceEntity();
// resourceEntities.add(resourceEntity);
// List<ResourceDTO> dtoList = new ArrayList<>();
// ResourceDTO resourceDTO = new ResourceDTO();
// dtoList.add(resourceDTO);
// when(resourceEntityService.getAll()).thenReturn(Uni.createFrom().item(resourceEntities));
// when(resourceEntityMapper.toDTOList(any(ArrayList.class))).thenReturn(dtoList);
// ArrayList result = given()
// .when().get("/api/v1/model/resources")
// .then()
// .statusCode(200)
// .extract()
// .body()
// .as(ArrayList.class);
// assertEquals(1, result.size());
// verify(resourceEntityService, times(1)).getAll();
// verify(resourceEntityMapper, times(1)).toDTOList(resourceEntities);
// }

// @Test
// void testGetAllResourcesEmptyList() {
// List<ResourceEntity> emptyList = new ArrayList<>();
//
// when(resourceEntityService.getAll()).thenReturn(Uni.createFrom().item(emptyList));
//
// given()
// .when().get("/api/v1/model/resources")
// .then()
// .statusCode(200);
//
// verify(resourceEntityService, times(1)).getAll();
// }

@Test
void testGetResourceById() {
UUID uuid = UUID.randomUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,44 +146,6 @@ void testDeploy() {
assertEquals(uuid, result.getWorkflowResourceId());
}

// @Test
// void testGetAll() {
// List<WorkflowResource> workflowResources = new ArrayList<>();
// WorkflowResource workflowResource = new WorkflowResource();
// workflowResources.add(workflowResource);
// List<WorkflowResourceDTO> dtoList = new ArrayList<>();
// WorkflowResourceDTO workflowResourceDTO = new WorkflowResourceDTO();
// dtoList.add(workflowResourceDTO);
//
// when(workflowResourceService.getAll()).thenReturn(Uni.createFrom().item(workflowResources));
// when(workflowResourceMapper.toDTOList(any(ArrayList.class))).thenReturn(dtoList);
//
// ArrayList result = given()
// .when().get("/api/v1/model/workflow-resource")
// .then()
// .statusCode(200)
// .extract()
// .body()
// .as(ArrayList.class);
// assertEquals(1, result.size());
// verify(workflowResourceService, times(1)).getAll();
// verify(workflowResourceMapper, times(1)).toDTOList(workflowResources);
// }

// @Test
// void testGetAllEmptyList() {
// List<WorkflowResource> emptyList = new ArrayList<>();
//
// when(workflowResourceService.getAll()).thenReturn(Uni.createFrom().item(emptyList));
//
// given()
// .when().get("/api/v1/model/workflow-resource")
// .then()
// .statusCode(200);
//
// verify(workflowResourceService, times(1)).getAll();
// }

@Test
void testById() {
UUID uuid = UUID.randomUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ void testUndeployWithNonExistingUUID() {

when(bpmnVersionRepoMock.findAllById(uuid)).thenReturn(Uni.createFrom().item(List.of()));

AtmLayerException thrown = assertThrows(AtmLayerException.class, () -> {
bpmnVersionServiceImpl.undeploy(uuid).await().indefinitely();
});
bpmnVersionServiceImpl.undeploy(uuid)
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(AtmLayerException.class,"Il file BPMN di riferimento non può essere undeployed");

assertEquals("Il file BPMN di riferimento non può essere undeployed", thrown.getMessage());
verify(bpmnVersionRepoMock, times(1)).findAllById(uuid);
verify(processClientMock, never()).undeploy(any(String.class));
}
Expand Down Expand Up @@ -115,11 +114,9 @@ void testUndeployErrorDuringProcessClient() {
when(bpmnVersionRepoMock.findAllById(uuid)).thenReturn(Uni.createFrom().item(List.of(bpmnVersion)));
when(processClientMock.undeploy(validDeploymentId)).thenReturn(Uni.createFrom().failure(new RuntimeException("Errore nel processClient")));

RuntimeException thrown = assertThrows(RuntimeException.class, () -> {
bpmnVersionServiceImpl.undeploy(uuid).await().indefinitely();
});

assertEquals("Errore nel processClient", thrown.getMessage());
bpmnVersionServiceImpl.undeploy(uuid)
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(RuntimeException.class,"Errore nel processClient");

verify(bpmnVersionRepoMock, times(1)).findAllById(uuid);
verify(processClientMock, times(1)).undeploy(validDeploymentId);
Expand Down
Loading

0 comments on commit 4a34bdc

Please sign in to comment.