diff --git a/src/main/java/org/gridsuite/study/server/StudyController.java b/src/main/java/org/gridsuite/study/server/StudyController.java index 15ee5ab33..965824c4c 100644 --- a/src/main/java/org/gridsuite/study/server/StudyController.java +++ b/src/main/java/org/gridsuite/study/server/StudyController.java @@ -250,18 +250,6 @@ public ResponseEntity recreateStudyNetwork(@PathVariable("study return ResponseEntity.ok().build(); } - @GetMapping(value = "/studies/{studyUuid}/indexation/status") - @Operation(summary = "check study indexation") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "The study indexation status"), - @ApiResponse(responseCode = "204", description = "The study indexation status doesn't exist"), - @ApiResponse(responseCode = "404", description = "The study or network doesn't exist")}) - public ResponseEntity checkStudyIndexationStatus(@PathVariable("studyUuid") UUID studyUuid) { - String result = studyService.getStudyIndexationStatus(studyUuid).name(); - return result != null ? ResponseEntity.ok().body(result) : - ResponseEntity.noContent().build(); - } - @PostMapping(value = "/studies/{studyUuid}/tree/subtrees", params = {"subtreeToCutParentNodeUuid", "referenceNodeUuid"}) @Operation(summary = "cut and paste a subtree") @ApiResponses(value = { @@ -1317,11 +1305,13 @@ public ResponseEntity getDefaultDynamicSimulationProvider() { return ResponseEntity.ok().body(studyService.getDefaultDynamicSimulationProvider()); } - @PostMapping(value = "/studies/{studyUuid}/reindex-all") - @Operation(summary = "reindex the study") - @ApiResponse(responseCode = "200", description = "Study reindexed") - public ResponseEntity reindexStudy(@Parameter(description = "study uuid") @PathVariable("studyUuid") UUID studyUuid) { - studyService.reindexStudy(studyUuid); + @PostMapping(value = "/studies/{studyUuid}/reindex-if-needed") + @Operation(summary = "reindex the study if needed") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Study reindexed"), + @ApiResponse(responseCode = "404", description = "The study or network doesn't exist")}) + public ResponseEntity reindexStudyIfNeeded(@Parameter(description = "study uuid") @PathVariable("studyUuid") UUID studyUuid) { + studyService.reindexStudyIfNeeded(studyUuid); return ResponseEntity.ok().build(); } diff --git a/src/main/java/org/gridsuite/study/server/service/StudyService.java b/src/main/java/org/gridsuite/study/server/service/StudyService.java index d0f93e9dc..1eeccabfd 100644 --- a/src/main/java/org/gridsuite/study/server/service/StudyService.java +++ b/src/main/java/org/gridsuite/study/server/service/StudyService.java @@ -1663,6 +1663,13 @@ public void reindexStudy(UUID studyUuid) { } @Transactional + public void reindexStudyIfNeeded(UUID studyUuid) { + StudyIndexationStatus status = getStudyIndexationStatus(studyUuid); + if (status == StudyIndexationStatus.NOT_INDEXED) { + reindexStudy(studyUuid); + } + } + public StudyIndexationStatus getStudyIndexationStatus(UUID studyUuid) { StudyEntity study = studyRepository.findById(studyUuid).orElseThrow(() -> new StudyException(STUDY_NOT_FOUND)); if (study.getIndexationStatus() == StudyIndexationStatus.INDEXED diff --git a/src/test/java/org/gridsuite/study/server/StudyTest.java b/src/test/java/org/gridsuite/study/server/StudyTest.java index a0564d28c..d30e889aa 100644 --- a/src/test/java/org/gridsuite/study/server/StudyTest.java +++ b/src/test/java/org/gridsuite/study/server/StudyTest.java @@ -2194,63 +2194,49 @@ private void checkElementUpdatedMessageSent(UUID elementUuid, String userId) { @Test public void reindexStudyTest() throws Exception { - mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", UUID.randomUUID())) + // if study doesn't exist + mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", UUID.randomUUID())) .andExpect(status().isNotFound()); - mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", UUID.randomUUID())) - .andExpectAll(status().isNotFound()); - UUID notExistingNetworkStudyUuid = createStudy("userId", NOT_EXISTING_NETWORK_CASE_UUID); - mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", notExistingNetworkStudyUuid)) + // if study exists but not the network + mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", notExistingNetworkStudyUuid)) .andExpect(status().isInternalServerError()); Message indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination); Message indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination); var requests = TestUtils.getRequestsWithBodyDone(1, server); - assertTrue(requests.stream().anyMatch(r -> r.getPath().contains("/v1/networks/" + NOT_EXISTING_NETWORK_UUID + "/reindex-all"))); - - mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", notExistingNetworkStudyUuid)) - .andExpectAll(status().isInternalServerError()); - - requests = TestUtils.getRequestsWithBodyDone(1, server); assertEquals(1, requests.stream().filter(r -> r.getPath().contains("/v1/networks/" + NOT_EXISTING_NETWORK_UUID + "/indexed-equipments")).count()); UUID study1Uuid = createStudy("userId", CASE_UUID); - mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", study1Uuid)) - .andExpectAll(status().isOk(), - content().string("NOT_INDEXED")); - indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination); - - mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", study1Uuid)) + mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", study1Uuid)) .andExpect(status().isOk()); + // first the indexationStatus is updated to NOT_INDEXED when checking status + indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination); + // Then it follow the reinxation process indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination); Message indexationStatusMessageDone = output.receive(TIMEOUT, studyUpdateDestination); assertEquals(study1Uuid, indexationStatusMessageDone.getHeaders().get(NotificationService.HEADER_STUDY_UUID)); assertEquals(NotificationService.UPDATE_TYPE_INDEXATION_STATUS, indexationStatusMessageDone.getHeaders().get(HEADER_UPDATE_TYPE)); - mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", study1Uuid)) - .andExpectAll(status().isOk(), - content().string("INDEXED")); - - requests = TestUtils.getRequestsWithBodyDone(4, server); + requests = TestUtils.getRequestsWithBodyDone(3, server); assertTrue(requests.stream().anyMatch(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/reindex-all"))); - assertEquals(2, requests.stream().filter(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/indexed-equipments")).count()); + assertEquals(1, requests.stream().filter(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/indexed-equipments")).count()); assertEquals(1, requests.stream().filter(r -> r.getPath().matches("/v1/reports/.*")).count()); Message buildStatusMessage = output.receive(TIMEOUT, studyUpdateDestination); assertEquals(study1Uuid, buildStatusMessage.getHeaders().get(NotificationService.HEADER_STUDY_UUID)); assertEquals(NotificationService.NODE_BUILD_STATUS_UPDATED, buildStatusMessage.getHeaders().get(HEADER_UPDATE_TYPE)); - mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", study1Uuid)) - .andExpect(status().is5xxServerError()); - indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination); - indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination); + // Finally we just check the status and it's ok, we don't need a reindexation for this study + mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", study1Uuid)) + .andExpect(status().isOk()); requests = TestUtils.getRequestsWithBodyDone(1, server); - assertTrue(requests.stream().anyMatch(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/reindex-all"))); + assertEquals(1, requests.stream().filter(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/indexed-equipments")).count()); } @Test @@ -2313,18 +2299,15 @@ public void testSupervision() throws Exception { MvcResult mvcResult; UUID studyUuid = createStudy("userId", CASE_UUID); - mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", studyUuid)) + mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", studyUuid)) .andExpect(status().isOk()); + Message indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination); Message indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination); Message indexationStatusMessageDone = output.receive(TIMEOUT, studyUpdateDestination); assertEquals(studyUuid, indexationStatusMessageDone.getHeaders().get(NotificationService.HEADER_STUDY_UUID)); assertEquals(NotificationService.UPDATE_TYPE_INDEXATION_STATUS, indexationStatusMessageDone.getHeaders().get(HEADER_UPDATE_TYPE)); - mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", studyUuid)) - .andExpectAll(status().isOk(), - content().string("INDEXED")); - Message buildStatusMessage = output.receive(TIMEOUT, studyUpdateDestination); assertEquals(studyUuid, buildStatusMessage.getHeaders().get(NotificationService.HEADER_STUDY_UUID)); assertEquals(NotificationService.NODE_BUILD_STATUS_UPDATED, buildStatusMessage.getHeaders().get(HEADER_UPDATE_TYPE)); @@ -2369,11 +2352,7 @@ public void testSupervision() throws Exception { assertEquals(20, Long.parseLong(mvcResult.getResponse().getContentAsString())); - Message indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination); - - mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", studyUuid)) - .andExpectAll(status().isOk(), - content().string("NOT_INDEXED")); + indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination); var requests = TestUtils.getRequestsWithBodyDone(3, server); assertTrue(requests.stream().anyMatch(r -> r.getPath().contains("/v1/networks/" + NETWORK_UUID_STRING + "/reindex-all")));