diff --git a/src/main/java/com/powsybl/caseserver/CaseController.java b/src/main/java/com/powsybl/caseserver/CaseController.java index 1bab238..0f480f9 100644 --- a/src/main/java/com/powsybl/caseserver/CaseController.java +++ b/src/main/java/com/powsybl/caseserver/CaseController.java @@ -153,9 +153,9 @@ public ResponseEntity exists(@PathVariable("caseUuid") UUID caseUuid) { @SuppressWarnings("javasecurity:S5145") public ResponseEntity importCase(@RequestParam("file") MultipartFile file, @RequestParam(value = "withExpiration", required = false, defaultValue = "false") boolean withExpiration, - @RequestParam(value = "indexed", required = false, defaultValue = "false") boolean indexed) { + @RequestParam(value = "withIndexation", required = false, defaultValue = "false") boolean withIndexation) { LOGGER.debug("importCase request received with file = {}", file.getName()); - UUID caseUuid = caseService.importCase(file, withExpiration, indexed); + UUID caseUuid = caseService.importCase(file, withExpiration, withIndexation); return ResponseEntity.ok().body(caseUuid); } @@ -166,10 +166,9 @@ public ResponseEntity importCase(@RequestParam("file") MultipartFile file, @ApiResponse(responseCode = "500", description = "An error occurred during the case file duplication")}) public ResponseEntity duplicateCase( @RequestParam("duplicateFrom") UUID caseId, - @RequestParam(value = "withExpiration", required = false, defaultValue = "false") boolean withExpiration, - @RequestParam(value = "indexed", required = false, defaultValue = "false") boolean indexed) { + @RequestParam(value = "withExpiration", required = false, defaultValue = "false") boolean withExpiration) { LOGGER.debug("duplicateCase request received with parameter sourceCaseUuid = {}", caseId); - UUID newCaseUuid = caseService.duplicateCase(caseId, withExpiration, indexed); + UUID newCaseUuid = caseService.duplicateCase(caseId, withExpiration); return ResponseEntity.ok().body(newCaseUuid); } @@ -184,17 +183,6 @@ public ResponseEntity disableCaseExpiration(@PathVariable("caseUuid") UUID return ResponseEntity.ok().build(); } - @PutMapping(value = "/cases/{caseUuid}/indexation") - @Operation(summary = "enable automatic indexation of the case") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "The case index has been changed"), - @ApiResponse(responseCode = "404", description = "Source case not found")}) - public ResponseEntity enableCaseIndexation(@PathVariable("caseUuid") UUID caseUuid, @RequestParam("indexed") boolean indexed) { - LOGGER.debug("enableIndexation request received for caseUuid = {}", caseUuid); - caseService.enableCaseIndexation(caseUuid, indexed); - return ResponseEntity.ok().build(); - } - @DeleteMapping(value = "/cases/{caseUuid}") @Operation(summary = "delete a case") public ResponseEntity deleteCase(@PathVariable("caseUuid") UUID caseUuid) { diff --git a/src/main/java/com/powsybl/caseserver/CaseService.java b/src/main/java/com/powsybl/caseserver/CaseService.java index 880af96..3807b19 100644 --- a/src/main/java/com/powsybl/caseserver/CaseService.java +++ b/src/main/java/com/powsybl/caseserver/CaseService.java @@ -175,7 +175,7 @@ boolean caseExists(UUID caseName) { return Files.exists(caseFile) && Files.isRegularFile(caseFile); } - UUID importCase(MultipartFile mpf, boolean withExpiration, boolean indexed) { + UUID importCase(MultipartFile mpf, boolean withExpiration, boolean withIndexation) { checkStorageInitialization(); UUID caseUuid = UUID.randomUUID(); @@ -210,16 +210,16 @@ UUID importCase(MultipartFile mpf, boolean withExpiration, boolean indexed) { throw e; } - createCaseMetadataEntity(caseUuid, withExpiration, indexed); + createCaseMetadataEntity(caseUuid, withExpiration, withIndexation); CaseInfos caseInfos = createInfos(caseFile.getFileName().toString(), caseUuid, importer.getFormat()); - if (indexed) { + if (withIndexation) { caseInfosService.addCaseInfos(caseInfos); } sendImportMessage(caseInfos.createMessage()); return caseUuid; } - UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration, boolean indexed) { + UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) { try { Path existingCaseFile = getCaseFile(sourceCaseUuid); if (existingCaseFile == null || existingCaseFile.getParent() == null) { @@ -235,10 +235,11 @@ UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration, boolean indexed) CaseInfos existingCaseInfos = caseInfosService.getCaseInfosByUuid(sourceCaseUuid.toString()).orElseThrow(); CaseInfos caseInfos = createInfos(existingCaseInfos.getName(), newCaseUuid, existingCaseInfos.getFormat()); - if (indexed) { - caseInfosService.addCaseInfos(caseInfos); - } - createCaseMetadataEntity(newCaseUuid, withExpiration, indexed); + caseInfosService.addCaseInfos(caseInfos); + + Optional existingCase = caseMetadataRepository.findById(sourceCaseUuid); + existingCase.ifPresentOrElse(caseMetadataEntity -> createCaseMetadataEntity(newCaseUuid, withExpiration, caseMetadataEntity.isIndexed()), + () -> createCaseMetadataEntity(newCaseUuid, withExpiration, false)); sendImportMessage(caseInfos.createMessage()); return newCaseUuid; @@ -248,23 +249,20 @@ UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration, boolean indexed) } } - private void createCaseMetadataEntity(UUID newCaseUuid, boolean withExpiration, boolean indexed) { + private void createCaseMetadataEntity(UUID newCaseUuid, boolean withExpiration, boolean withIndexation) { Instant expirationTime = null; if (withExpiration) { expirationTime = Instant.now().plus(1, ChronoUnit.HOURS); } - caseMetadataRepository.save(new CaseMetadataEntity(newCaseUuid, expirationTime, indexed)); + caseMetadataRepository.save(new CaseMetadataEntity(newCaseUuid, expirationTime, withIndexation)); } - public Set getCaseToReindex() { - return caseMetadataRepository.findAllByIndexedTrue() + public List getCasesToReindex() { + Set casesToReindex = caseMetadataRepository.findAllByIndexedTrue() .stream() .map(CaseMetadataEntity::getId) .collect(Collectors.toSet()); - } - - public List getAllCases() { - return getCases(getStorageRootDir()); + return getCases(getStorageRootDir()).stream().filter(c -> casesToReindex.contains(c.getUuid())).toList(); } CaseInfos createInfos(String fileBaseName, UUID caseUuid, String format) { @@ -284,12 +282,6 @@ public void disableCaseExpiration(UUID caseUuid) { caseMetadataEntity.setExpirationDate(null); } - @Transactional - public void enableCaseIndexation(UUID caseUuid, boolean indexed) { - CaseMetadataEntity caseMetadataEntity = caseMetadataRepository.findById(caseUuid).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "case " + caseUuid + " not found")); - caseMetadataEntity.setIndexed(indexed); - } - Optional loadNetwork(UUID caseUuid) { checkStorageInitialization(); diff --git a/src/main/java/com/powsybl/caseserver/SupervisionController.java b/src/main/java/com/powsybl/caseserver/SupervisionController.java index 34e6987..e796cc7 100644 --- a/src/main/java/com/powsybl/caseserver/SupervisionController.java +++ b/src/main/java/com/powsybl/caseserver/SupervisionController.java @@ -29,11 +29,13 @@ public class SupervisionController { private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionController.class); private final SupervisionService supervisionService; + private final CaseService caseService; private final ClientConfiguration elasticsearchClientConfiguration; private final CaseInfosService caseInfosService; - public SupervisionController(SupervisionService supervisionService, ClientConfiguration elasticsearchClientConfiguration, CaseInfosService caseInfosService) { + public SupervisionController(SupervisionService supervisionService, CaseService caseService, ClientConfiguration elasticsearchClientConfiguration, CaseInfosService caseInfosService) { this.supervisionService = supervisionService; + this.caseService = caseService; this.elasticsearchClientConfiguration = elasticsearchClientConfiguration; this.caseInfosService = caseInfosService; } @@ -59,7 +61,7 @@ public ResponseEntity getIndexedDirectoryElementsIndexName() { @Operation(summary = "reindex all cases") public ResponseEntity reindexAllCases() { LOGGER.debug("reindex all cases request received"); - supervisionService.reindexAllCases(); + caseInfosService.recreateAllCaseInfos(caseService.getCasesToReindex()); return ResponseEntity.ok().build(); } @@ -73,8 +75,8 @@ public ResponseEntity deleteIndexedDirectoryElements() { @GetMapping(value = "/cases/indexation-count") @Operation(summary = "get indexed cases count") @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Indexed cases count")}) - public ResponseEntity getIndexedDirectoryElementsCount() { - return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(Long.toString(supervisionService.getIndexedCaseElementsCount())); + public ResponseEntity getIndexedCasesCount() { + return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(Long.toString(supervisionService.getIndexedCasesCount())); } } diff --git a/src/main/java/com/powsybl/caseserver/repository/CaseMetadataRepository.java b/src/main/java/com/powsybl/caseserver/repository/CaseMetadataRepository.java index 97a5da0..c44b383 100644 --- a/src/main/java/com/powsybl/caseserver/repository/CaseMetadataRepository.java +++ b/src/main/java/com/powsybl/caseserver/repository/CaseMetadataRepository.java @@ -17,9 +17,5 @@ */ @Repository public interface CaseMetadataRepository extends JpaRepository { - - @Override - List findAllById(Iterable uuids); - List findAllByIndexedTrue(); } diff --git a/src/main/java/com/powsybl/caseserver/services/SupervisionService.java b/src/main/java/com/powsybl/caseserver/services/SupervisionService.java index 94dbf17..6c6e53a 100644 --- a/src/main/java/com/powsybl/caseserver/services/SupervisionService.java +++ b/src/main/java/com/powsybl/caseserver/services/SupervisionService.java @@ -6,17 +6,11 @@ */ package com.powsybl.caseserver.services; -import com.powsybl.caseserver.CaseService; -import com.powsybl.caseserver.dto.CaseInfos; import com.powsybl.caseserver.elasticsearch.CaseInfosRepository; -import com.powsybl.caseserver.elasticsearch.CaseInfosService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import java.util.List; -import java.util.Set; -import java.util.UUID; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; @@ -27,34 +21,23 @@ public class SupervisionService { private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionService.class); - private final CaseInfosService caseInfosService; - private final CaseService caseService; private final CaseInfosRepository caseInfosRepository; - public SupervisionService(CaseInfosService caseInfosService, CaseService caseService, CaseInfosRepository caseInfosRepository) { - this.caseInfosService = caseInfosService; - this.caseService = caseService; + public SupervisionService(CaseInfosRepository caseInfosRepository) { this.caseInfosRepository = caseInfosRepository; } - public void reindexAllCases() { - List allCases = caseService.getAllCases(); - Set casesToIndex = caseService.getCaseToReindex(); - List data = allCases.stream().filter(c -> casesToIndex.contains(c.getUuid())).toList(); - caseInfosService.recreateAllCaseInfos(data); - } - public long deleteIndexedDirectoryElements() { AtomicReference startTime = new AtomicReference<>(); startTime.set(System.nanoTime()); - long nbIndexesToDelete = getIndexedCaseElementsCount(); + long nbIndexesToDelete = getIndexedCasesCount(); caseInfosRepository.deleteAll(); LOGGER.trace("Indexed directory elements deletion : {} seconds", TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime.get())); return nbIndexesToDelete; } - public long getIndexedCaseElementsCount() { + public long getIndexedCasesCount() { return caseInfosRepository.count(); } } diff --git a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java index 8a99daf..e92b746 100644 --- a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java @@ -474,35 +474,19 @@ public void test() throws Exception { assertTrue(response.contains("\"format\":\"XIIDM\"")); } - @Test - public void testChangeIndexation() throws Exception { - createStorageDir(); - - // import a case - UUID caseUuid = importCase(TEST_CASE, false); - assertNotNull(outputDestination.receive(1000, caseImportDestination)); - assertTrue(caseMetadataRepository.findAllById(List.of(caseUuid)).get(0).isIndexed()); - - // disable indexation - mvc.perform(put("/v1/cases/{caseUuid}/indexation", caseUuid).param("indexed", "false")) - .andExpect(status().isOk()); - - assertFalse(caseMetadataRepository.findAllById(List.of(caseUuid)).get(0).isIndexed()); - } - private UUID importCase(String testCase, Boolean withExpiration) throws Exception { String importedCase; if (withExpiration) { importedCase = mvc.perform(multipart("/v1/cases") .file(createMockMultipartFile(testCase)) .param("withExpiration", withExpiration.toString()) - .param("indexed", "true")) + .param("withIndexation", "true")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); } else { importedCase = mvc.perform(multipart("/v1/cases") .file(createMockMultipartFile(testCase)) - .param("indexed", "true")) + .param("withIndexation", "true")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); } @@ -546,7 +530,7 @@ public void searchCaseTest() throws Exception { // import IIDM test case String aCase = mvc.perform(multipart("/v1/cases") .file(createMockMultipartFile("testCase.xiidm")) - .param("indexed", "true")) + .param("withIndexation", "true")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); @@ -563,7 +547,7 @@ public void searchCaseTest() throws Exception { // import CGMES french file aCase = mvc.perform(multipart("/v1/cases") .file(createMockMultipartFile("20200424T1330Z_2D_RTEFRANCE_001.zip")) - .param("indexed", "true")) + .param("withIndexation", "true")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); @@ -580,7 +564,7 @@ public void searchCaseTest() throws Exception { // import UCTE french file aCase = mvc.perform(multipart("/v1/cases") .file(createMockMultipartFile("20200103_0915_FO5_FR0.UCT")) - .param("indexed", "true")) + .param("withIndexation", "true")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); @@ -597,7 +581,7 @@ public void searchCaseTest() throws Exception { // import UCTE german file aCase = mvc.perform(multipart("/v1/cases") .file(createMockMultipartFile("20200103_0915_SN5_D80.UCT")) - .param("indexed", "true")) + .param("withIndexation", "true")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); @@ -614,7 +598,7 @@ public void searchCaseTest() throws Exception { // import UCTE swiss file aCase = mvc.perform(multipart("/v1/cases") .file(createMockMultipartFile("20200103_0915_135_CH2.UCT")) - .param("indexed", "true")) + .param("withIndexation", "true")) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); diff --git a/src/test/java/com/powsybl/caseserver/SupervisionControllerTest.java b/src/test/java/com/powsybl/caseserver/SupervisionControllerTest.java index 88fee6e..184ce38 100644 --- a/src/test/java/com/powsybl/caseserver/SupervisionControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/SupervisionControllerTest.java @@ -8,7 +8,6 @@ import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; -import com.powsybl.caseserver.elasticsearch.CaseInfosRepository; import com.powsybl.caseserver.repository.CaseMetadataRepository; import com.powsybl.caseserver.services.SupervisionService; import com.powsybl.computation.ComputationManager; @@ -24,6 +23,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; @@ -41,14 +41,12 @@ */ @RunWith(SpringRunner.class) @AutoConfigureMockMvc -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, properties = {"case-store-directory=/cases"}) -@ContextConfigurationWithTestChannel +@SpringBootTest(properties = {"case-store-directory=/cases"}) +@ContextConfiguration(classes = {CaseApplication.class}) public class SupervisionControllerTest { @Autowired SupervisionService supervisionService; @Autowired - CaseInfosRepository caseInfosRepository; - @Autowired CaseMetadataRepository caseMetadataRepository; @Autowired CaseService caseService; @@ -63,7 +61,7 @@ public class SupervisionControllerTest { private FileSystem fileSystem; @Test - public void testGetElementInfosCount() throws Exception { + public void testGetCaseInfosCount() throws Exception { createStorageDir(); importCase(true); importCase(true); @@ -72,7 +70,7 @@ public void testGetElementInfosCount() throws Exception { mockMvc.perform(post("/v1/supervision/cases/reindex")) .andExpect(status().isOk()); - Assert.assertEquals(2, supervisionService.getIndexedCaseElementsCount()); + Assert.assertEquals(2, supervisionService.getIndexedCasesCount()); } @@ -86,7 +84,7 @@ public void testDeleteElementInfos() throws Exception { mockMvc.perform(delete("/v1/supervision/cases/indexation")) .andExpect(status().isOk()); - Assert.assertEquals(0, supervisionService.getIndexedCaseElementsCount()); + Assert.assertEquals(0, supervisionService.getIndexedCasesCount()); //reindex mockMvc.perform(post("/v1/supervision/cases/reindex")) @@ -96,14 +94,14 @@ public void testDeleteElementInfos() throws Exception { .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); Assert.assertEquals("2", countStr); - Assert.assertEquals(2, supervisionService.getIndexedCaseElementsCount()); + Assert.assertEquals(2, supervisionService.getIndexedCasesCount()); } private void importCase(Boolean indexed) throws Exception { mockMvc.perform(multipart("/v1/cases") .file(createMockMultipartFile()) - .param("indexed", indexed.toString())) + .param("withIndexation", indexed.toString())) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(); } @@ -119,12 +117,12 @@ public void setUp() { fileSystem = Jimfs.newFileSystem(Configuration.unix()); caseService.setFileSystem(fileSystem); caseService.setComputationManager(Mockito.mock(ComputationManager.class)); - cleanDB(); } @After public void tearDown() throws Exception { fileSystem.close(); + cleanDB(); } private void cleanDB() {