Skip to content

Commit

Permalink
review and fix
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <etienne.lesot@rte-france.com>
  • Loading branch information
EtienneLt committed Jan 16, 2025
1 parent b96797f commit 0e3e697
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/powsybl/caseserver/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private Utils() {
public static final String GZIP_FORMAT = "gz";
public static final List<String> COMPRESSION_FORMATS = List.of("bz2", GZIP_FORMAT, "xz", "zst");
public static final List<String> ARCHIVE_FORMATS = List.of("zip");
public static final String NOT_FOUND = " not found";

public static String removeExtension(String filename, String extension) {
int index = filename.lastIndexOf(extension);
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/powsybl/caseserver/service/CaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import com.powsybl.iidm.network.Exporter;
import com.powsybl.iidm.network.Importer;
import com.powsybl.iidm.network.Network;
import org.springframework.http.HttpStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -46,19 +48,18 @@ default void validateCaseName(String caseName) {
}
}

CaseMetadataEntity getCaseMetaDataEntity(UUID caseUuid);
default CaseMetadataEntity getCaseMetaDataEntity(UUID caseUuid) {
return getCaseMetadataRepository().findById(caseUuid).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "case " + caseUuid + NOT_FOUND));
}

default Boolean isUploadedAsPlainFile(UUID caseUuid) {
String name = getCaseMetaDataEntity(caseUuid).getOriginalFilename();
return !isCompressedCaseFile(name) && !isArchivedCaseFile(name);
return name != null && !isCompressedCaseFile(name) && !isArchivedCaseFile(name);
}

default CaseInfos createInfos(String fileBaseName, UUID caseUuid, String format) {
FileNameParser parser = FileNameParsers.findParser(fileBaseName);
String baseName = fileBaseName;
if (isCompressedCaseFile(fileBaseName)) {
baseName = removeExtension(fileBaseName, GZIP_EXTENSION);
}
if (parser != null) {
Optional<? extends FileNameInfos> fileNameInfos = parser.parse(fileBaseName);
if (fileNameInfos.isPresent()) {
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/com/powsybl/caseserver/service/FsCaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public List<CaseInfos> getCases() {
return walk.filter(Files::isRegularFile)
.map(this::getCaseInfos)
.filter(Objects::nonNull)
.map(this::convertCaseInfosForPlainFile)
.toList();
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down Expand Up @@ -141,7 +142,8 @@ public CaseInfos getCaseInfos(UUID caseUuid) {
LOGGER.error("The directory with the following uuid doesn't exist: {}", caseUuid);
return null;
}
return getCaseInfos(file);
CaseInfos caseInfos = getCaseInfos(file);
return this.convertCaseInfosForPlainFile(caseInfos);
}

public Path getCaseFile(UUID caseUuid) {
Expand Down Expand Up @@ -227,7 +229,11 @@ public UUID importCase(MultipartFile mpf, boolean withExpiration, boolean withIn

String format = importer.getFormat();
createCaseMetadataEntity(caseUuid, withExpiration, withIndexation, caseName, compressionFormat, format);
CaseInfos caseInfos = createInfos(caseFile.getFileName().toString(), caseUuid, format);
String caseInfoFileName = caseFile.getFileName().toString();
if (Boolean.TRUE.equals(isUploadedAsPlainFile(caseUuid))) {
caseInfoFileName = removeExtension(caseInfoFileName, GZIP_EXTENSION);
}
CaseInfos caseInfos = createInfos(caseInfoFileName, caseUuid, format);
if (withIndexation) {
caseInfosService.addCaseInfos(caseInfos);
}
Expand All @@ -251,12 +257,11 @@ public UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) {
Files.copy(existingCaseFile, newCaseFile, StandardCopyOption.COPY_ATTRIBUTES);

CaseMetadataEntity existingCase = getCaseMetaDataEntity(sourceCaseUuid);
CaseInfos caseInfos = createInfos(newCaseFile, newCaseUuid);
CaseInfos caseInfos = createInfos(existingCase.getOriginalFilename(), newCaseUuid, existingCase.getFormat());
if (existingCase.isIndexed()) {
caseInfosService.addCaseInfos(caseInfos);
}
createCaseMetadataEntity(newCaseUuid, withExpiration, existingCase.isIndexed());

notificationService.sendImportMessage(caseInfos.createMessage());
return newCaseUuid;

Expand All @@ -269,11 +274,6 @@ private CaseInfos createInfos(Path caseFile, UUID caseUuid) {
return createInfos(caseFile.getFileName().toString(), caseUuid, getFormat(caseFile));
}

@Override
public CaseMetadataEntity getCaseMetaDataEntity(UUID caseUuid) {
return caseMetadataRepository.findById(caseUuid).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "case " + caseUuid + " not found"));
}

@Override
public Optional<Network> loadNetwork(UUID caseUuid) {
checkStorageInitialization();
Expand Down Expand Up @@ -387,4 +387,11 @@ public CaseMetadataRepository getCaseMetadataRepository() {
return caseMetadataRepository;
}

private CaseInfos convertCaseInfosForPlainFile(CaseInfos caseInfos) {
if (caseInfos != null && Boolean.TRUE.equals(isUploadedAsPlainFile(caseInfos.getUuid()))) {
return createInfos(removeExtension(caseInfos.getName(), GZIP_EXTENSION), caseInfos.getUuid(), caseInfos.getFormat());
}
return caseInfos;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import java.util.UUID;

import static com.powsybl.caseserver.service.S3CaseService.NOT_FOUND;
import static com.powsybl.caseserver.Utils.NOT_FOUND;

/**
* @author Etienne Homer <etienne.homer at rte-france.com>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public class S3CaseService implements CaseService {
@Value("${case-subpath}")
private String rootDirectory;

public static final String NOT_FOUND = " not found";

@Autowired
private S3Client s3Client;

Expand Down Expand Up @@ -565,9 +563,4 @@ public CaseMetadataRepository getCaseMetadataRepository() {
return caseMetadataRepository;
}

@Override
public CaseMetadataEntity getCaseMetaDataEntity(UUID caseUuid) {
return caseMetadataRepository.findById(caseUuid).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "case " + caseUuid + NOT_FOUND));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ protected static UUID importCase(String filename, String contentType) throws IOE
return caseUUID;
}

protected static UUID importIidmCase() throws IOException {
UUID caseUUID;
try (InputStream inputStream = S3CaseDataSourceControllerTest.class.getResourceAsStream("/" + IIDM_NAME)) {
caseUUID = caseService.importCase(new MockMultipartFile(IIDM_NAME, IIDM_NAME, "application/zip", inputStream.readAllBytes()), false, false);
}
return caseUUID;
}

@Test
public void testBaseName() throws Exception {
MvcResult mvcResult = mvc.perform(get("/v1/cases/{caseUuid}/datasource/baseName", cgmesCaseUuid))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void setUp() throws URISyntaxException, IOException {
}
cgmesDataSource = DataSource.fromPath(Paths.get(getClass().getResource("/" + CGMES_ZIP_NAME).toURI()));

iidmCaseUuid = importIidmCase();
iidmCaseUuid = importCase(IIDM_NAME, "text/plain");
iidmDataSource = DataSource.fromPath(Paths.get(S3CaseDataSourceControllerTest.class.getResource("/" + IIDM_NAME).toURI()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void setUp() throws URISyntaxException, IOException {
caseService = s3CaseService;
cgmesCaseUuid = importCase(CGMES_ZIP_NAME, "application/zip");
cgmesDataSource = DataSource.fromPath(Paths.get(S3CaseDataSourceControllerTest.class.getResource("/" + CGMES_ZIP_NAME).toURI()));
iidmCaseUuid = importIidmCase();
iidmCaseUuid = importCase(IIDM_NAME, "text/plain");
iidmDataSource = DataSource.fromPath(Paths.get(S3CaseDataSourceControllerTest.class.getResource("/" + IIDM_NAME).toURI()));
}

Expand Down

0 comments on commit 0e3e697

Please sign in to comment.