From 0bc7c88d7d6580cc9eb091c5f4e3b8a65882448e Mon Sep 17 00:00:00 2001 From: basseche Date: Fri, 20 Dec 2024 15:36:10 +0100 Subject: [PATCH] get case base name from caseName with extension (#55) * Add function to get case base name from caseName (with extension) Signed-off-by: basseche --- .../powsybl/caseserver/CaseController.java | 10 +++++++ .../service/AbstractCaseControllerTest.java | 27 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/com/powsybl/caseserver/CaseController.java b/src/main/java/com/powsybl/caseserver/CaseController.java index 0e1d6e3..310c83b 100644 --- a/src/main/java/com/powsybl/caseserver/CaseController.java +++ b/src/main/java/com/powsybl/caseserver/CaseController.java @@ -10,6 +10,7 @@ import com.powsybl.caseserver.elasticsearch.CaseInfosService; import com.powsybl.caseserver.service.CaseService; import com.powsybl.caseserver.service.MetadataService; +import com.powsybl.commons.datasource.DataSourceUtil; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; @@ -224,4 +225,13 @@ public ResponseEntity> getMetadata(@RequestParam("ids") List getCaseBaseName(@RequestParam("caseName") String caseName) { + LOGGER.debug("getting base name from case file name"); + String baseName = DataSourceUtil.getBaseName(caseName); + return ResponseEntity.ok(baseName); + } } diff --git a/src/test/java/com/powsybl/caseserver/service/AbstractCaseControllerTest.java b/src/test/java/com/powsybl/caseserver/service/AbstractCaseControllerTest.java index 15e50e5..86c91fe 100644 --- a/src/test/java/com/powsybl/caseserver/service/AbstractCaseControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/service/AbstractCaseControllerTest.java @@ -739,4 +739,31 @@ void invalidFileInCaseDirectoryShouldBeIgnored() throws Exception { .andExpect(status().isOk()); assertNotNull(outputDestination.receive(1000, caseImportDestination)); } + + @Test + void testGetCaseBaseName() throws Exception { + MvcResult mvcResult = mvc.perform(get("/v1/cases/caseBaseName?caseName=case.xml")) + .andExpect(status().isOk()) + .andReturn(); + String response = mvcResult.getResponse().getContentAsString(); + assertEquals("case", response); + + mvcResult = mvc.perform(get("/v1/cases/caseBaseName?caseName=case.xml.gz")) + .andExpect(status().isOk()) + .andReturn(); + response = mvcResult.getResponse().getContentAsString(); + assertEquals("case", response); + + mvcResult = mvc.perform(get("/v1/cases/caseBaseName?caseName=case.v1.xml")) + .andExpect(status().isOk()) + .andReturn(); + response = mvcResult.getResponse().getContentAsString(); + assertEquals("case.v1", response); + + mvcResult = mvc.perform(get("/v1/cases/caseBaseName?caseName=case.v1.xml.gz")) + .andExpect(status().isOk()) + .andReturn(); + response = mvcResult.getResponse().getContentAsString(); + assertEquals("case.v1", response); + } }