From 466e99ba8623e73233d26d0695db3043f17a59c9 Mon Sep 17 00:00:00 2001 From: achour94 Date: Wed, 4 Oct 2023 18:04:52 +0200 Subject: [PATCH 1/3] merge check indexation status and reindex all in one endpoint --- .../org/gridsuite/study/server/StudyController.java | 11 +++++++++++ .../gridsuite/study/server/service/StudyService.java | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/org/gridsuite/study/server/StudyController.java b/src/main/java/org/gridsuite/study/server/StudyController.java index f7674077d..48990beba 100644 --- a/src/main/java/org/gridsuite/study/server/StudyController.java +++ b/src/main/java/org/gridsuite/study/server/StudyController.java @@ -54,6 +54,7 @@ * @author Franck Lecuyer */ +@SuppressWarnings("checkstyle:RegexpSingleline") @RestController @RequestMapping(value = "/" + StudyApi.API_VERSION) @Tag(name = "Study server") @@ -1325,6 +1326,16 @@ public ResponseEntity reindexStudy(@Parameter(description = "study uuid") return ResponseEntity.ok().build(); } + @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(); + } + @PostMapping(value = "/studies/{studyUuid}/notification") @Operation(summary = "Create study related notification") @ApiResponses(value = { 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 a434874c2..dcc54b8f8 100644 --- a/src/main/java/org/gridsuite/study/server/service/StudyService.java +++ b/src/main/java/org/gridsuite/study/server/service/StudyService.java @@ -1666,6 +1666,14 @@ public void reindexStudy(UUID studyUuid) { reindexStudy(studyRepository.findById(studyUuid).orElseThrow(() -> new StudyException(STUDY_NOT_FOUND))); } + @Transactional + public void reindexStudyIfNeeded(UUID studyUuid) { + StudyIndexationStatus status = getStudyIndexationStatus(studyUuid); + if (status == StudyIndexationStatus.NOT_INDEXED) { + reindexStudy(studyUuid); + } + } + @Transactional public StudyIndexationStatus getStudyIndexationStatus(UUID studyUuid) { StudyEntity study = studyRepository.findById(studyUuid).orElseThrow(() -> new StudyException(STUDY_NOT_FOUND)); From 913f818d1fb4340532d1ac9b062c2da28f42a51a Mon Sep 17 00:00:00 2001 From: achour94 Date: Tue, 17 Oct 2023 11:28:36 +0200 Subject: [PATCH 2/3] test reindex all if needed --- .../study/server/StudyController.java | 21 ----------- .../study/server/service/StudyService.java | 1 - .../org/gridsuite/study/server/StudyTest.java | 36 +++++++++---------- 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/gridsuite/study/server/StudyController.java b/src/main/java/org/gridsuite/study/server/StudyController.java index 48990beba..1feb6cc3b 100644 --- a/src/main/java/org/gridsuite/study/server/StudyController.java +++ b/src/main/java/org/gridsuite/study/server/StudyController.java @@ -54,7 +54,6 @@ * @author Franck Lecuyer */ -@SuppressWarnings("checkstyle:RegexpSingleline") @RestController @RequestMapping(value = "/" + StudyApi.API_VERSION) @Tag(name = "Study server") @@ -251,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 = { @@ -1318,14 +1305,6 @@ 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); - return ResponseEntity.ok().build(); - } - @PostMapping(value = "/studies/{studyUuid}/reindex-if-needed") @Operation(summary = "reindex the study if needed") @ApiResponses(value = { 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 1c1f09174..2754eb62e 100644 --- a/src/main/java/org/gridsuite/study/server/service/StudyService.java +++ b/src/main/java/org/gridsuite/study/server/service/StudyService.java @@ -1682,7 +1682,6 @@ public void reindexStudyIfNeeded(UUID studyUuid) { } } - @Transactional 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..06cc2e98c 100644 --- a/src/test/java/org/gridsuite/study/server/StudyTest.java +++ b/src/test/java/org/gridsuite/study/server/StudyTest.java @@ -2194,36 +2194,36 @@ private void checkElementUpdatedMessageSent(UUID elementUuid, String userId) { @Test public void reindexStudyTest() throws Exception { - mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-all", UUID.randomUUID())) + 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()); +// 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)) + 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"))); +// 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()); +// 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()); +// 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")); +// 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()); indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination); @@ -2231,20 +2231,20 @@ public void reindexStudyTest() throws Exception { 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")); +// mockMvc.perform(get("/v1/studies/{studyUuid}/indexation/status", study1Uuid)) +// .andExpectAll(status().isOk(), +// content().string("INDEXED")); requests = TestUtils.getRequestsWithBodyDone(4, 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(2, 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)) + mockMvc.perform(post("/v1/studies/{studyUuid}/reindex-if-needed", study1Uuid)) .andExpect(status().is5xxServerError()); indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination); indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination); From 0bca8c6d10b0e597a0cf2a5f99f458d694f3c437 Mon Sep 17 00:00:00 2001 From: achour94 Date: Tue, 17 Oct 2023 14:51:38 +0200 Subject: [PATCH 3/3] test reindex all if needed --- .../org/gridsuite/study/server/StudyTest.java | 49 ++++++------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/src/test/java/org/gridsuite/study/server/StudyTest.java b/src/test/java/org/gridsuite/study/server/StudyTest.java index 06cc2e98c..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 { + // 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); + // 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()); + 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-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)); + // 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().is5xxServerError()); - indexationStatusMessageOnGoing = output.receive(TIMEOUT, studyUpdateDestination); - indexationStatusMessageNotIndexed = output.receive(TIMEOUT, studyUpdateDestination); + .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")));