Skip to content

Commit

Permalink
Fix Static Resource extension & integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleValentini1 committed Oct 8, 2024
1 parent 0b29ba3 commit c25a481
Show file tree
Hide file tree
Showing 6 changed files with 13,779 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ public enum AppErrorCodeEnum {
BPMN_INTERNAL_ERROR("ATMLM_4000051", "Nessun file associato a BPMN o nessuna storageKey trovata", INTERNAL),
BPMN_FILE_CANNOT_BE_UNDEPLOYED("ATMLM_4000052", "La risorsa di processo indicata non può essere rilasciata", INTERNAL),
NO_ASSOCIATION_FOUND("ATMLM_4000060","Nessuna associazione trovata", CONSTRAINT_VIOLATION),
ALL_FIELDS_ARE_BLANK("ATMLM_4000061", "Tutti i campi sono vuoti", AppErrorType.BLANK_FIELDS),
ALL_FIELDS_ARE_BLANK("ATMLM_4000061", "Tutti i campi sono vuoti", BLANK_FIELDS),
RESOURCES_CREATION_ERROR("ATMLM_4000062", "Errore nella creazione di resource multipli", GENERIC ),
FILE_DECODE_ERROR("ATMLM_4000063", "Errore nella decodifica del file", GENERIC ),
DATABASE_SAVE_FILE_ERROR("ATMLM_4000064", "Errore nella persistenza del file sul database", INTERNAL),
OBJECT_STORE_COPY_FILE_ERROR("ATMLM_4000065", "Errore nella copia del file nella cartella DELETE su Object Store", INTERNAL);
OBJECT_STORE_COPY_FILE_ERROR("ATMLM_4000065", "Errore nella copia del file nella cartella DELETE su Object Store", INTERNAL),
INVALID_FILE_EXTENSION("ATMLM_4000066", "Estensione del file non valida", INVALID_EXTENSION);
private final String errorCode;
private final String errorMessage;
private final AppErrorType type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public enum AppErrorType {
CANNOT_ASSOCIATE,
NOT_EXISTING_USER_ID,
NOT_EXISTING_USER_PROFILE, CANNOT_REPLACE_ASSOCIATION,
BLANK_FIELDS
BLANK_FIELDS,
INVALID_EXTENSION
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

Expand Down Expand Up @@ -120,12 +119,14 @@ public Uni<ResourceFile> upload(ResourceEntity resourceEntity, File file,
@Override
public Uni<ResourceEntity> createResource(ResourceEntity resourceEntity, File file,
String filename, String path, String description) {
validateFileExtension(filename, Arrays.asList("html", "jpeg", "jpg", "png", "svg"));

return findBySHA256(resourceEntity.getSha256())
.onItem().transformToUni(Unchecked.function(x -> {
if (x.isPresent()) {
throw new AtmLayerException(String.format("Esiste già una risorsa con lo stesso contenuto: %s", x.get().getResourceFile().getStorageKey().substring(15)),
Response.Status.BAD_REQUEST,
RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS);
throw new AtmLayerException(String.format("Esiste già una risorsa con lo stesso contenuto: %s",
x.get().getResourceFile().getStorageKey().substring(15)),
Response.Status.BAD_REQUEST, RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS);
}
return resourceFileService.findByStorageKey(resourceEntity.getStorageKey())
.onItem()
Expand All @@ -148,6 +149,21 @@ public Uni<ResourceEntity> createResource(ResourceEntity resourceEntity, File fi
}));
}


private void validateFileExtension(String filename, List<String> validExtensions) {
String fileExtension = Optional.ofNullable(filename)
.filter(f -> f.contains("."))
.map(f -> f.substring(filename.lastIndexOf(".") + 1).toLowerCase())
.orElse("");

if (!validExtensions.contains(fileExtension)) {
throw new AtmLayerException(String.format("Estensione del file non valida: %s. Estensioni consentite: %s",
fileExtension, String.join(", ", validExtensions)),
Response.Status.BAD_REQUEST, AppErrorCodeEnum.INVALID_FILE_EXTENSION);
}
}


@Override
@WithTransaction
public Uni<List<String>> createResourceMultiple(List<ResourceEntity> resourceEntityList, List<ResourceCreationDto> resourceCreationDtoList) {
Expand All @@ -158,6 +174,14 @@ public Uni<List<String>> createResourceMultiple(List<ResourceEntity> resourceEnt
int index = resourceEntityList.indexOf(resourceEntity);
File file = resourceCreationDtoList.get(index).getFile();
String filename = resourceCreationDtoList.get(index).getFilename();

try {
validateFileExtension(filename, Arrays.asList("html", "jpeg", "jpg", "png", "svg"));
} catch (AtmLayerException ex) {
errors.add(String.format("%s-%s", filename, ex.getMessage()));
return Uni.createFrom().nullItem();
}

return findBySHA256(resourceEntity.getSha256())
.onItem().transformToUni(x -> {
if (x.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void testCreateAlreadyExists() {
resourceEntity.setStorageKey("storageKey");
when(resourceEntityRepository.findBySHA256(any(String.class))).thenReturn(Uni.createFrom().nullItem());
when(resourceFileService.findByStorageKey(any(String.class))).thenReturn(Uni.createFrom().item(Optional.of(resourceFile)));
resourceEntityService.createResource(resourceEntity, file, "filename", "path", "description")
resourceEntityService.createResource(resourceEntity, file, "filename.html", "path", "description")
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(AtmLayerException.class, "Impossibile caricare storageKey: la risorsa con lo stesso nome file e percorso esiste già");
}
Expand All @@ -92,7 +92,7 @@ void testCreateSyncError() {
when(resourceEntityStorageService.saveFile(any(ResourceEntity.class), any(File.class), any(String.class), any(String.class)))
.thenReturn(Uni.createFrom().item(resourceFile));
when(resourceEntityRepository.findById(any(UUID.class))).thenReturn(Uni.createFrom().nullItem());
resourceEntityService.createResource(resourceEntity, new File("path/to/file"), "filename.xml", "path", "description")
resourceEntityService.createResource(resourceEntity, new File("path/to/file"), "filename.jpeg", "path", "description")
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(AtmLayerException.class, "Problema di sincronizzazione sulla creazione della risorsa");
}
Expand All @@ -110,7 +110,7 @@ void testCreateResourceWithDuplicateSHA256() {

when(resourceEntityRepository.findBySHA256(sha256)).thenReturn(Uni.createFrom().item(existingResourceEntity));

resourceEntityService.createResource(existingResourceEntity, file, "filename", "path", "description")
resourceEntityService.createResource(existingResourceEntity, file, "filename.jpg", "path", "description")
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(AtmLayerException.class, "Esiste già una risorsa con lo stesso contenuto");
}
Expand All @@ -128,10 +128,10 @@ void testCreateResourceSuccess() {
when(resourceEntityRepository.findBySHA256(sha256)).thenReturn(Uni.createFrom().nullItem());
when(resourceFileService.findByStorageKey(any(String.class))).thenReturn(Uni.createFrom().item(Optional.empty()));
when(resourceEntityRepository.persist(any(ResourceEntity.class))).thenReturn(Uni.createFrom().item(resourceEntity1));
when(resourceEntityStorageService.saveFile(resourceEntity1, file, "prova.txt", "/file/prova")).thenReturn(Uni.createFrom().item(resourceFile));
when(resourceEntityStorageService.saveFile(resourceEntity1, file, "prova.png", "/file/prova")).thenReturn(Uni.createFrom().item(resourceFile));
when(resourceEntityRepository.findById(uuid)).thenReturn(Uni.createFrom().item(resourceEntity1));

resourceEntityService.createResource(resourceEntity1, file, "filename", "path", "description")
resourceEntityService.createResource(resourceEntity1, file, "filename.svg", "path", "description")
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertCompleted()
.assertItem(resourceEntity1);
Expand Down Expand Up @@ -315,7 +315,7 @@ void testCreateResourceMultipleOK() {
}

private List<ResourceCreationDto> getResourceCreationDtoInstance(){
ResourceCreationDto resourceCreationDto = new ResourceCreationDto(new File("test.txt"), "filename", NoDeployableResourceType.HTML, "path", "description");
ResourceCreationDto resourceCreationDto = new ResourceCreationDto(new File("test.jpg"), "filename.jpg", NoDeployableResourceType.HTML, "path", "description");
List<ResourceCreationDto> dtoList = new ArrayList<>();
dtoList.add(resourceCreationDto);
return dtoList;
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/test/resources/integration-test/run-postman.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
newman run /postman/Integration_test_model.postman_collection.json -r cli,json,htmlextra \
newman run /postman/Integration_test_model_1.postman_collection.json -r cli,json,htmlextra \
--reporter-json-export output/result.json --reporter-htmlextra-export output/result.html \
--env-var "baseUrl=http://host.testcontainers.internal:8086"

0 comments on commit c25a481

Please sign in to comment.