From 9405b74b5decc774ab6c8e5dd04659a6ed32f276 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Mon, 16 Oct 2023 13:33:16 +0200 Subject: [PATCH 1/7] refactore stashed / restore / delete modifications Signed-off-by: jamal-khey --- .../server/NetworkModificationController.java | 41 +++++++-------- .../server/NetworkModificationException.java | 1 + .../NetworkModificationRepository.java | 50 ++++++++++++++++--- .../service/NetworkModificationService.java | 7 +++ .../server/ModificationControllerTest.java | 50 ++++++++++++------- .../AbstractNetworkModificationTest.java | 2 +- 6 files changed, 106 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java index 10326403f..05c7d7c49 100644 --- a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java +++ b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java @@ -47,14 +47,14 @@ public NetworkModificationController(NetworkModificationService networkModificat this.lineTypesCatalogService = lineTypesCatalogService; } - @GetMapping(value = "/groups/{groupUuid}/modifications", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping(value = "/groups/{groupUuid}/network-modifications", produces = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Get modifications list of a group") @ApiResponse(responseCode = "200", description = "List of modifications of the group") public ResponseEntity> getNetworkModifications(@Parameter(description = "Group UUID") @PathVariable("groupUuid") UUID groupUuid, @Parameter(description = "Only metadata") @RequestParam(name = "onlyMetadata", required = false, defaultValue = "false") Boolean onlyMetadata, - @Parameter(description = "Stashed modifications") @RequestParam(name = "stashed", required = false, defaultValue = "false") Boolean stashed, + @Parameter(description = "Stashed modifications") @RequestParam(name = "onlyStashed", required = false, defaultValue = "false") Boolean onlyStashed, @Parameter(description = "Return 404 if group is not found or an empty list") @RequestParam(name = "errorOnGroupNotFound", required = false, defaultValue = "true") Boolean errorOnGroupNotFound) { - return ResponseEntity.ok().body(networkModificationService.getNetworkModifications(groupUuid, onlyMetadata, errorOnGroupNotFound, stashed)); + return ResponseEntity.ok().body(networkModificationService.getNetworkModifications(groupUuid, onlyMetadata, errorOnGroupNotFound, onlyStashed)); } @PostMapping(value = "/groups") @@ -148,9 +148,15 @@ public ResponseEntity getNetworkModification( @Operation(summary = "Delete network modifications") @ApiResponse(responseCode = "200", description = "The network modifications were deleted") public ResponseEntity deleteNetworkModifications( - @Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List networkModificationUuids, - @Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid) { - networkModificationService.deleteNetworkModifications(groupUuid, networkModificationUuids); + @Parameter(description = "Network modification UUIDs") @RequestParam(name = "uuids", required = false) List networkModificationUuids, + @Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid, + @Parameter(description = "delete only stashed modifications") @RequestParam(name = "onlyStashed", required = false, defaultValue = "false") Boolean onlyStashed) { + + if (networkModificationUuids == null || networkModificationUuids.isEmpty()) { + networkModificationService.deleteNetworkModifications(groupUuid, onlyStashed); + } else { + networkModificationService.deleteNetworkModifications(groupUuid, networkModificationUuids); + } return ResponseEntity.ok().build(); } @@ -202,23 +208,18 @@ public ResponseEntity createModificationInGroup(@RequestBody ModificationI return ResponseEntity.ok().body(networkModificationService.createModificationInGroup(modificationsInfos)); } - @PostMapping(value = "/network-modifications/stash", produces = MediaType.APPLICATION_JSON_VALUE) - @Operation(summary = "stash network modifications") + @PutMapping(value = "/network-modifications", produces = MediaType.APPLICATION_JSON_VALUE) + @Operation(summary = "stash or unstash network modifications") @ApiResponse(responseCode = "200", description = "The network modifications were stashed") public ResponseEntity stashNetworkModifications( @Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List networkModificationUuids, - @Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid) { - networkModificationService.stashNetworkModifications(networkModificationUuids); - return ResponseEntity.ok().build(); - } - - @PostMapping(value = "/network-modifications/restore", produces = MediaType.APPLICATION_JSON_VALUE) - @Operation(summary = "restore network modifications") - @ApiResponse(responseCode = "200", description = "The network modifications were restored") - public ResponseEntity restoreNetworkModifications( - @Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List networkModificationUuids, - @Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid) { - networkModificationService.restoreNetworkModifications(networkModificationUuids); + @Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid, + @Parameter(description = "stash or unstash network modifications") @RequestParam(name = "stashed", required = true) Boolean stashed) { + if (stashed.booleanValue()) { + networkModificationService.stashNetworkModifications(networkModificationUuids); + } else { + networkModificationService.restoreNetworkModifications(networkModificationUuids); + } return ResponseEntity.ok().build(); } diff --git a/src/main/java/org/gridsuite/modification/server/NetworkModificationException.java b/src/main/java/org/gridsuite/modification/server/NetworkModificationException.java index 3eacb5744..4e8d14780 100644 --- a/src/main/java/org/gridsuite/modification/server/NetworkModificationException.java +++ b/src/main/java/org/gridsuite/modification/server/NetworkModificationException.java @@ -26,6 +26,7 @@ public enum Type { GROOVY_SCRIPT_ERROR(HttpStatus.BAD_REQUEST), NETWORK_NOT_FOUND(HttpStatus.NOT_FOUND), VARIANT_NOT_FOUND(HttpStatus.NOT_FOUND), + NOTHING_TO_DELETE(HttpStatus.BAD_REQUEST), MODIFICATION_GROUP_NOT_FOUND(HttpStatus.NOT_FOUND), MODIFICATION_NOT_FOUND(HttpStatus.NOT_FOUND), SWITCH_NOT_FOUND(HttpStatus.NOT_FOUND), diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index a9b6d565b..772aa8343 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -125,9 +125,9 @@ public List getModifications(UUID groupUuid, boolean onlyMeta } @Transactional(readOnly = true) - public List getModifications(UUID groupUuid, boolean onlyMetadata, boolean errorOnGroupNotFound, boolean stashedModifications) { + public List getModifications(UUID groupUuid, boolean onlyMetadata, boolean errorOnGroupNotFound, boolean onlyStashed) { try { - return onlyMetadata ? getModificationsMetadata(groupUuid, stashedModifications) : getModificationsInfos(List.of(groupUuid), stashedModifications); + return onlyMetadata ? getModificationsMetadata(groupUuid, onlyStashed) : getModificationsInfos(List.of(groupUuid), onlyStashed); } catch (NetworkModificationException e) { if (e.getType() == MODIFICATION_GROUP_NOT_FOUND && !errorOnGroupNotFound) { return List.of(); @@ -144,7 +144,6 @@ public List getModificationsMetadata(UUID groupUuid, boolean .map(this::getModificationInfos) .collect(Collectors.toList()); } - public TabularModificationEntity loadTabularModificationSubEntities(ModificationEntity modificationEntity) { TabularModificationEntity tabularModificationEntity = (TabularModificationEntity) modificationEntity; switch (tabularModificationEntity.getModificationType()) { @@ -167,13 +166,20 @@ public ModificationInfos getModificationInfos(ModificationEntity modificationEnt return modificationEntity.toModificationInfos(); } - public List getModificationsInfos(List groupUuids, boolean stashedModifications) { - return groupUuids.stream().flatMap(this::getModificationEntityStream) - .filter(m -> m.getStashed() == stashedModifications) - .map(this::getModificationInfos) + public List getModificationsInfos(List groupUuids, boolean onlyStashed) { + Stream modificationEntity = groupUuids.stream().flatMap(this::getModificationEntityStream); + if (onlyStashed) { + return modificationEntity.filter(m -> m.getStashed() == onlyStashed) + .map(ModificationEntity::toModificationInfos) + .collect(Collectors.toList()); + } else { + return modificationEntity.map(ModificationEntity::toModificationInfos) + .collect(Collectors.toList()); + } } + @Transactional(readOnly = true) public ModificationInfos getModificationInfo(UUID modificationUuid) { Optional optionalModificationEntity = modificationRepository.findById(modificationUuid); @@ -199,6 +205,23 @@ public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound } } + @Transactional + public int deleteModifications(UUID groupUuid, boolean onlyStashed) { + ModificationGroupEntity groupEntity = getModificationGroup(groupUuid); + List modifications; + if (onlyStashed) { + modifications = getModificationEntityStream(groupUuid) + .filter(ModificationEntity::getStashed) + .collect(Collectors.toList()); + } else { + modifications = getModificationEntityStream(groupUuid).collect(Collectors.toList()); + } + modifications.forEach(groupEntity::removeModification); + int count = modifications.size(); + this.modificationRepository.deleteAll(modifications); + return count; + } + @Transactional // To have the find and delete in the same transaction (atomic) public int deleteModifications(UUID groupUuid, List uuids) { ModificationGroupEntity groupEntity = getModificationGroup(groupUuid); @@ -211,6 +234,19 @@ public int deleteModifications(UUID groupUuid, List uuids) { return count; } + @Transactional // To have the find and delete in the same transaction (atomic) + public int deleteStashedModifications(UUID groupUuid, List uuids) { + ModificationGroupEntity groupEntity = getModificationGroup(groupUuid); + List modifications = getModificationEntityStream(groupUuid) + .filter(m -> uuids.contains(m.getId())) + .filter(ModificationEntity::getStashed) + .collect(Collectors.toList()); + modifications.forEach(groupEntity::removeModification); + int count = modifications.size(); + this.modificationRepository.deleteAll(modifications); + return count; + } + private ModificationGroupEntity getModificationGroup(UUID groupUuid) { return this.modificationGroupRepository.findById(groupUuid).orElseThrow(() -> new NetworkModificationException(MODIFICATION_GROUP_NOT_FOUND, groupUuid.toString())); } diff --git a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java index 6ec1c610b..e620b96e3 100644 --- a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java +++ b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java @@ -198,6 +198,12 @@ public void stopBuildRequest(String receiver) { notificationService.emitCancelBuildMessage(receiver); } + public void deleteNetworkModifications(UUID groupUuid, boolean onlyStashed) { + if (networkModificationRepository.deleteModifications(groupUuid, onlyStashed) == 0) { + throw new NetworkModificationException(NOTHING_TO_DELETE); + } + } + public void deleteNetworkModifications(UUID groupUuid, List modificationsUuids) { if (networkModificationRepository.deleteModifications(groupUuid, modificationsUuids) == 0) { throw new NetworkModificationException(MODIFICATION_NOT_FOUND); @@ -293,4 +299,5 @@ public Optional duplicateModificationsInGroup(UUID ta public void deleteStashedModificationInGroup(UUID groupUuid, boolean errorOnGroupNotFound) { networkModificationRepository.deleteStashedModificationInGroup(groupUuid, errorOnGroupNotFound); } + } diff --git a/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java b/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java index be7189bfe..8a4d57182 100644 --- a/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java +++ b/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java @@ -89,7 +89,6 @@ public class ModificationControllerTest { private static final UUID TEST_REPORT_ID = UUID.randomUUID(); private static final String URI_NETWORK_MODIF_BASE = "/v1/network-modifications"; - private static final String URI_RESTORE_NETWORK_MODIF_BASE = URI_NETWORK_MODIF_BASE + "/restore"; private static final String URI_NETWORK_MODIF_PARAMS = "&groupUuid=" + TEST_GROUP_ID + "&reportUuid=" + TEST_REPORT_ID + "&reporterId=" + UUID.randomUUID(); private static final String URI_NETWORK_MODIF = URI_NETWORK_MODIF_BASE + "?networkUuid=" + TEST_NETWORK_ID + URI_NETWORK_MODIF_PARAMS; private static final String URI_NETWORK_MODIF_BUS_BREAKER = URI_NETWORK_MODIF_BASE + "?networkUuid=" + TEST_NETWORK_BUS_BREAKER_ID + URI_NETWORK_MODIF_PARAMS; @@ -248,14 +247,14 @@ public void testModificationGroups() throws Exception { resultAsString = mvcResult.getResponse().getContentAsString(); List bsicListResultUUID = mapper.readValue(resultAsString, new TypeReference<>() { }); assertEquals(bsicListResultUUID, List.of(TEST_GROUP_ID)); - mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/modifications", TEST_GROUP_ID)).andExpectAll( + mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications", TEST_GROUP_ID)).andExpectAll( status().isOk(), content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); resultAsString = mvcResult.getResponse().getContentAsString(); List bsicListResulModifInfos = mapper.readValue(resultAsString, new TypeReference<>() { }); assertEquals(1, bsicListResulModifInfos.size()); - mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/modifications?onlyMetadata=true", TEST_GROUP_ID)) + mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=true", TEST_GROUP_ID)) .andExpectAll(status().isOk(), content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); resultAsString = mvcResult.getResponse().getContentAsString(); @@ -266,10 +265,10 @@ public void testModificationGroups() throws Exception { mockMvc.perform(delete("/v1/groups/{groupUuid}", TEST_GROUP_ID)) .andExpect(status().isOk()); - mockMvc.perform(get("/v1/groups/{groupUuid}/modifications?onlyMetadata=true", TEST_GROUP_ID)).andExpectAll(status().isNotFound(), + mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=true", TEST_GROUP_ID)).andExpectAll(status().isNotFound(), content().string(new NetworkModificationException(MODIFICATION_GROUP_NOT_FOUND, TEST_GROUP_ID.toString()).getMessage())); - mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/modifications?onlyMetadata=true&errorOnGroupNotFound=false", TEST_GROUP_ID)).andExpectAll( + mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=true&errorOnGroupNotFound=false", TEST_GROUP_ID)).andExpectAll( status().isOk(), content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); @@ -298,9 +297,10 @@ public void testRestoreNetworkModifications() throws Exception { assertEquals(1, modifications.size()); String uuidString = modifications.get(0).getUuid().toString(); - mockMvc.perform(post(URI_RESTORE_NETWORK_MODIF_BASE) + mockMvc.perform(put(URI_NETWORK_MODIF_BASE) .queryParam("groupUuid", TEST_GROUP_ID.toString()) - .queryParam("uuids", uuidString)) + .queryParam("uuids", uuidString) + .queryParam("stashed", "false")) .andExpect(status().isOk()); assertEquals(0, modificationRepository.getModifications(TEST_GROUP_ID, true, true, true).size()); } @@ -324,11 +324,12 @@ public void testStashNetworkModifications() throws Exception { List modifications = modificationRepository.getModifications(TEST_GROUP_ID, false, true); assertEquals(1, modifications.size()); String uuidString = modifications.get(0).getUuid().toString(); - mockMvc.perform(post(URI_NETWORK_MODIF_BASE + "/stash") + mockMvc.perform(put(URI_NETWORK_MODIF_BASE) .queryParam("groupUuid", TEST_GROUP_ID.toString()) - .queryParam("uuids", uuidString)) + .queryParam("uuids", uuidString) + .queryParam("stashed", "true")) .andExpect(status().isOk()); - assertEquals(0, modificationRepository.getModifications(TEST_GROUP_ID, false, true, false).size()); + assertEquals(true, modificationRepository.getModificationInfo(UUID.fromString(uuidString)).getStashed()); } @Test @@ -371,6 +372,18 @@ public void testDeleteModification() throws Exception { mockMvc.perform(delete("/v1/groups/" + TEST_GROUP_ID).queryParam("errorOnGroupNotFound", "false")).andExpect(status().isOk()); } + //test delete all modifications + @Test + public void testDeleteAllModification() throws Exception { + List modificationList = createSomeSwitchModifications(TEST_GROUP_ID, 3); + + assertEquals(3, modificationRepository.getModifications(TEST_GROUP_ID, false, true).size()); + mockMvc.perform(delete(URI_NETWORK_MODIF_BASE) + .queryParam("groupUuid", TEST_GROUP_ID.toString())) + .andExpect(status().isOk()); + assertEquals(0, modificationRepository.getModifications(TEST_GROUP_ID, false, true).size()); + } + @Test public void testNetworkModificationsWithErrorOnNetworkFlush() throws Exception { String uriString = URI_NETWORK_MODIF_BASE + "?networkUuid=" + TEST_NETWORK_WITH_FLUSH_ERROR_ID + URI_NETWORK_MODIF_PARAMS; @@ -762,12 +775,14 @@ public void testDuplicateModificationGroup() throws Exception { //stash the first modification String uuidString = modifications.get(0).getUuid().toString(); - mockMvc.perform(post(URI_NETWORK_MODIF_BASE + "/stash") + mockMvc.perform(put(URI_NETWORK_MODIF_BASE) .queryParam("groupUuid", TEST_GROUP_ID.toString()) - .queryParam("uuids", uuidString)) + .queryParam("uuids", uuidString) + .queryParam("stashed", "true")) .andExpect(status().isOk()); List stashedModifications = modificationRepository.getModificationsMetadata(TEST_GROUP_ID, true); - List modificationAfterStash = modificationRepository.getModificationsMetadata(TEST_GROUP_ID, false); + List modificationAfterStash = modificationRepository.getModificationsMetadata(TEST_GROUP_ID, false) + .stream().filter(modificationInfos -> !modificationInfos.getStashed()).toList(); assertEquals(1, stashedModifications.size()); assertEquals(4, modificationAfterStash.size()); @@ -1112,7 +1127,7 @@ private void testNetworkModificationsCount(UUID groupUuid, int actualSize) throw MvcResult mvcResult; String resultAsString; // get all modifications for the given group of a network - mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/modifications?onlyMetadata=true", groupUuid).contentType(MediaType.APPLICATION_JSON)) + mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=true", groupUuid).contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); resultAsString = mvcResult.getResponse().getContentAsString(); List modificationsTestGroupId = mapper.readValue(resultAsString, new TypeReference<>() { }); @@ -1288,7 +1303,7 @@ public void testCreateVoltageInitModification() throws Exception { UUID groupUuid = UUID.fromString(mapper.readValue(mvcResult.getResponse().getContentAsString(), String.class)); // Get the modifications - mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/modifications?onlyMetadata=false", groupUuid)).andExpectAll( + mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=false", groupUuid)).andExpectAll( status().isOk(), content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); @@ -1315,9 +1330,10 @@ public void testDeleteStashedNetworkModifications() throws Exception { List modifications = modificationRepository.getModifications(TEST_GROUP_ID, true, true); assertEquals(1, modifications.size()); String uuidString = modifications.get(0).getUuid().toString(); - mockMvc.perform(post(URI_NETWORK_MODIF_BASE + "/stash") + mockMvc.perform(put(URI_NETWORK_MODIF_BASE) .queryParam("groupUuid", TEST_GROUP_ID.toString()) - .queryParam("uuids", uuidString)) + .queryParam("uuids", uuidString) + .queryParam("stashed", "true")) .andExpect(status().isOk()); assertEquals(1, modificationRepository.getModifications(TEST_GROUP_ID, true, true, true).size()); mockMvc.perform(delete("/v1/groups/" + TEST_GROUP_ID + "/stashed-modifications").queryParam("errorOnGroupNotFound", "false")).andExpect(status().isOk()); diff --git a/src/test/java/org/gridsuite/modification/server/modifications/AbstractNetworkModificationTest.java b/src/test/java/org/gridsuite/modification/server/modifications/AbstractNetworkModificationTest.java index 78db091c9..626cdbdea 100644 --- a/src/test/java/org/gridsuite/modification/server/modifications/AbstractNetworkModificationTest.java +++ b/src/test/java/org/gridsuite/modification/server/modifications/AbstractNetworkModificationTest.java @@ -240,7 +240,7 @@ protected void testNetworkModificationsCount(UUID groupUuid, int actualSize) thr MvcResult mvcResult; String resultAsString; // get all modifications for the given group of a network - mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/modifications?onlyMetadata=true", groupUuid).contentType(MediaType.APPLICATION_JSON)) + mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=true", groupUuid).contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); resultAsString = mvcResult.getResponse().getContentAsString(); List modificationsTestGroupId = mapper.readValue(resultAsString, new TypeReference<>() { }); From 3dbcada739ab5825215753a87dfea923f612804c Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Mon, 6 Nov 2023 15:00:36 +0100 Subject: [PATCH 2/7] fix Signed-off-by: jamal-khey --- .../server/repositories/NetworkModificationRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 772aa8343..c2bf9689a 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -144,6 +144,7 @@ public List getModificationsMetadata(UUID groupUuid, boolean .map(this::getModificationInfos) .collect(Collectors.toList()); } + public TabularModificationEntity loadTabularModificationSubEntities(ModificationEntity modificationEntity) { TabularModificationEntity tabularModificationEntity = (TabularModificationEntity) modificationEntity; switch (tabularModificationEntity.getModificationType()) { @@ -179,7 +180,6 @@ public List getModificationsInfos(List groupUuids, bool } } - @Transactional(readOnly = true) public ModificationInfos getModificationInfo(UUID modificationUuid) { Optional optionalModificationEntity = modificationRepository.findById(modificationUuid); From 5d5253edfc0631c625d6e53e8e3ee19765057db8 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Tue, 7 Nov 2023 12:42:34 +0100 Subject: [PATCH 3/7] fix only stashed not implemented in getModificationsMetadata Signed-off-by: jamal-khey --- .../NetworkModificationRepository.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index c2bf9689a..acdd20707 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -136,13 +136,20 @@ public List getModifications(UUID groupUuid, boolean onlyMeta } } - public List getModificationsMetadata(UUID groupUuid, boolean stashedModifications) { - return modificationRepository + public List getModificationsMetadata(UUID groupUuid, boolean onlyStashed) { + Stream modificationEntitySteam = modificationRepository .findAllBaseByGroupId(getModificationGroup(groupUuid).getId()) - .stream() - .filter(m -> m.getStashed() == stashedModifications) - .map(this::getModificationInfos) - .collect(Collectors.toList()); + .stream(); + if(onlyStashed) { + return modificationEntitySteam.filter(m -> m.getStashed()) + .map(this::getModificationInfos) + .collect(Collectors.toList()); + } + else { + return modificationEntitySteam + .map(this::getModificationInfos) + .collect(Collectors.toList()); + } } public TabularModificationEntity loadTabularModificationSubEntities(ModificationEntity modificationEntity) { @@ -175,7 +182,6 @@ public List getModificationsInfos(List groupUuids, bool .collect(Collectors.toList()); } else { return modificationEntity.map(ModificationEntity::toModificationInfos) - .collect(Collectors.toList()); } } From a8ee6db337626de6341d63d7f3ce9cd23e79ef9b Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Tue, 7 Nov 2023 12:59:53 +0100 Subject: [PATCH 4/7] code review comments Signed-off-by: jamal-khey --- .../modification/server/NetworkModificationController.java | 4 ++-- .../server/repositories/NetworkModificationRepository.java | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java index 05c7d7c49..bbf86ddb1 100644 --- a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java +++ b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java @@ -214,8 +214,8 @@ public ResponseEntity createModificationInGroup(@RequestBody ModificationI public ResponseEntity stashNetworkModifications( @Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List networkModificationUuids, @Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid, - @Parameter(description = "stash or unstash network modifications") @RequestParam(name = "stashed", required = true) Boolean stashed) { - if (stashed.booleanValue()) { + @Parameter(description = "stash or unstash network modifications") @RequestParam(name = "stashed", defaultValue = "true") Boolean stashed) { + if (stashed) { networkModificationService.stashNetworkModifications(networkModificationUuids); } else { networkModificationService.restoreNetworkModifications(networkModificationUuids); diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index acdd20707..29b798a0f 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -140,12 +140,11 @@ public List getModificationsMetadata(UUID groupUuid, boolean Stream modificationEntitySteam = modificationRepository .findAllBaseByGroupId(getModificationGroup(groupUuid).getId()) .stream(); - if(onlyStashed) { + if (onlyStashed) { return modificationEntitySteam.filter(m -> m.getStashed()) .map(this::getModificationInfos) .collect(Collectors.toList()); - } - else { + } else { return modificationEntitySteam .map(this::getModificationInfos) .collect(Collectors.toList()); From e01121ce84b69665e879f9d3d7975661cf1d419d Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Tue, 7 Nov 2023 14:11:30 +0100 Subject: [PATCH 5/7] increase coverage Signed-off-by: jamal-khey --- .../NetworkModificationRepository.java | 13 -------- .../service/ModificationRepositoryTest.java | 32 +++++++++++++++++++ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 29b798a0f..50324cb2c 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -239,19 +239,6 @@ public int deleteModifications(UUID groupUuid, List uuids) { return count; } - @Transactional // To have the find and delete in the same transaction (atomic) - public int deleteStashedModifications(UUID groupUuid, List uuids) { - ModificationGroupEntity groupEntity = getModificationGroup(groupUuid); - List modifications = getModificationEntityStream(groupUuid) - .filter(m -> uuids.contains(m.getId())) - .filter(ModificationEntity::getStashed) - .collect(Collectors.toList()); - modifications.forEach(groupEntity::removeModification); - int count = modifications.size(); - this.modificationRepository.deleteAll(modifications); - return count; - } - private ModificationGroupEntity getModificationGroup(UUID groupUuid) { return this.modificationGroupRepository.findById(groupUuid).orElseThrow(() -> new NetworkModificationException(MODIFICATION_GROUP_NOT_FOUND, groupUuid.toString())); } diff --git a/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java b/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java index 0d3beb0e2..47765bf55 100644 --- a/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java +++ b/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java @@ -975,6 +975,38 @@ public void testLinesAttachToSplitLines() { ); } + @Test + public void testDeleteStashedModificationList() { + //create a modification and add it to the repository + var groovyScriptEntity1 = GroovyScriptInfos.builder().stashed(true).script("script1").build().toEntity(); + networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(groovyScriptEntity1)); + //check the modification is in the repository + List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, false, true); + assertEquals(1, modificationInfos.size()); + + //delete the modification + networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(groovyScriptEntity1.getId())); + //check the modification is not in the repository + modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, false, true); + assertEquals(0, modificationInfos.size()); + } + + @Test + public void testDeleteNonStashedModificationList() { + //create a modification and add it to the repository + var groovyScriptEntity1 = GroovyScriptInfos.builder().stashed(false).script("script1").build().toEntity(); + networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(groovyScriptEntity1)); + //check the modification is in the repository + List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, false, true); + assertEquals(1, modificationInfos.size()); + + //delete the modification + networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(groovyScriptEntity1.getId())); + //check the modification is not in the repository + modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, false, true); + assertEquals(0, modificationInfos.size()); + } + @Test public void testDeleteAttachingLine() { DeleteAttachingLineEntity deleteAttachingLineEntity = DeleteAttachingLineInfos.builder() From 4468359b7ee68d1fda758bc1d98f50e901aa94c2 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Tue, 7 Nov 2023 14:28:27 +0100 Subject: [PATCH 6/7] increase coverage Signed-off-by: jamal-khey --- .../server/ModificationControllerTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java b/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java index 8a4d57182..b441f2af0 100644 --- a/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java +++ b/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java @@ -384,6 +384,18 @@ public void testDeleteAllModification() throws Exception { assertEquals(0, modificationRepository.getModifications(TEST_GROUP_ID, false, true).size()); } + @Test + public void testDeleteTryToDeleteEmptyStashedModification() throws Exception { + List modificationList = createSomeSwitchModifications(TEST_GROUP_ID, 3); + + assertEquals(3, modificationRepository.getModifications(TEST_GROUP_ID, false, true).size()); + mockMvc.perform(delete(URI_NETWORK_MODIF_BASE) + .queryParam("onlyStashed", "true") + .queryParam("groupUuid", TEST_GROUP_ID.toString())) + .andExpect(status().isBadRequest()); +// assertEquals(3, modificationRepository.getModifications(TEST_GROUP_ID, false, true).size()); + } + @Test public void testNetworkModificationsWithErrorOnNetworkFlush() throws Exception { String uriString = URI_NETWORK_MODIF_BASE + "?networkUuid=" + TEST_NETWORK_WITH_FLUSH_ERROR_ID + URI_NETWORK_MODIF_PARAMS; From a94310fcd8c8a03cce466e408a3559c262e3c77a Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Tue, 7 Nov 2023 14:28:39 +0100 Subject: [PATCH 7/7] increase coverage Signed-off-by: jamal-khey --- .../modification/server/ModificationControllerTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java b/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java index b441f2af0..350ef1fd1 100644 --- a/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java +++ b/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java @@ -393,7 +393,6 @@ public void testDeleteTryToDeleteEmptyStashedModification() throws Exception { .queryParam("onlyStashed", "true") .queryParam("groupUuid", TEST_GROUP_ID.toString())) .andExpect(status().isBadRequest()); -// assertEquals(3, modificationRepository.getModifications(TEST_GROUP_ID, false, true).size()); } @Test