diff --git a/src/main/java/org/gridsuite/study/server/StudyConstants.java b/src/main/java/org/gridsuite/study/server/StudyConstants.java index 6eec4c85b..905e8d3f6 100644 --- a/src/main/java/org/gridsuite/study/server/StudyConstants.java +++ b/src/main/java/org/gridsuite/study/server/StudyConstants.java @@ -60,6 +60,8 @@ private StudyConstants() { public static final String HEADER_IMPORT_PARAMETERS = "importParameters"; public static final String HEADER_MESSAGE = "message"; public static final String HEADER_USER_ID = "userId"; + public static final String QUERY_PARAM_ONLY_STASHED = "onlyStashed"; + public static final String QUERY_PARAM_STASHED = "stashed"; public enum SldDisplayMode { FEEDER_POSITION, diff --git a/src/main/java/org/gridsuite/study/server/StudyController.java b/src/main/java/org/gridsuite/study/server/StudyController.java index 32e8d949d..4cddb03ee 100644 --- a/src/main/java/org/gridsuite/study/server/StudyController.java +++ b/src/main/java/org/gridsuite/study/server/StudyController.java @@ -1053,10 +1053,10 @@ public ResponseEntity> getAvailableSvgComponentLibraries() { @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The network modifications was returned"), @ApiResponse(responseCode = "404", description = "The study/node is not found")}) public ResponseEntity getNetworkModifications(@Parameter(description = "Study UUID") @PathVariable("studyUuid") UUID studyUuid, @Parameter(description = "Node UUID") @PathVariable("nodeUuid") UUID nodeUuid, - @Parameter(description = "Stashed Modification") @RequestParam(name = "stashed", required = false, defaultValue = "false") Boolean stashed, + @RequestParam(name = "onlyStashed", required = false, defaultValue = "false") Boolean onlyStashed, @Parameter(description = "Only metadata") @RequestParam(name = "onlyMetadata", required = false, defaultValue = "false") Boolean onlyMetadata) { // Return json string because modification dtos are not available here - return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(networkModificationTreeService.getNetworkModifications(nodeUuid, stashed, onlyMetadata)); + return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(networkModificationTreeService.getNetworkModifications(nodeUuid, onlyStashed, onlyMetadata)); } @PostMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications") @@ -1089,34 +1089,29 @@ public ResponseEntity updateNetworkModification(@Parameter(description = " @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The network modifications was deleted"), @ApiResponse(responseCode = "404", description = "The study/node is not found")}) public ResponseEntity deleteNetworkModifications(@Parameter(description = "Study UUID") @PathVariable("studyUuid") UUID studyUuid, @Parameter(description = "Node UUID") @PathVariable("nodeUuid") UUID nodeUuid, - @Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List networkModificationUuids, + @Parameter(description = "Network modification UUIDs") @RequestParam(name = "uuids", required = false) List networkModificationUuids, + @Parameter(description = "Delete only stashed modifications") @RequestParam(name = "onlyStashed", required = false, defaultValue = "false") Boolean onlyStashed, @RequestHeader(HEADER_USER_ID) String userId) { studyService.assertCanModifyNode(studyUuid, nodeUuid); - studyService.deleteNetworkModifications(studyUuid, nodeUuid, networkModificationUuids, userId); + studyService.deleteNetworkModifications(studyUuid, nodeUuid, networkModificationUuids, onlyStashed, userId); + return ResponseEntity.ok().build(); } - @PostMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications/stash") + @PutMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications") @Operation(summary = "Stash network modifications for a node") - @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The network modifications were stashed"), @ApiResponse(responseCode = "404", description = "The study/node is not found")}) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The network modifications were stashed / restored "), @ApiResponse(responseCode = "404", description = "The study/node is not found")}) public ResponseEntity stashNetworkModifications(@Parameter(description = "Study UUID") @PathVariable("studyUuid") UUID studyUuid, @Parameter(description = "Node UUID") @PathVariable("nodeUuid") UUID nodeUuid, @Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List networkModificationUuids, + @Parameter(description = "Stashed Modification") @RequestParam(name = "stashed", required = true) Boolean stashed, @RequestHeader(HEADER_USER_ID) String userId) { studyService.assertCanModifyNode(studyUuid, nodeUuid); - studyService.stashNetworkModifications(studyUuid, nodeUuid, networkModificationUuids, userId); - return ResponseEntity.ok().build(); - } - - @PostMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications/restore") - @Operation(summary = "Restore network modifications for a node") - @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The network modifications were restored"), @ApiResponse(responseCode = "404", description = "The study/node is not found")}) - public ResponseEntity restoreNetworkModifications(@Parameter(description = "Study UUID") @PathVariable("studyUuid") UUID studyUuid, - @Parameter(description = "Node UUID") @PathVariable("nodeUuid") UUID nodeUuid, - @Parameter(description = "Network modification UUIDs") @RequestParam("uuids") List networkModificationUuids, - @RequestHeader(HEADER_USER_ID) String userId) { - studyService.assertCanModifyNode(studyUuid, nodeUuid); - studyService.restoreNetworkModifications(studyUuid, nodeUuid, networkModificationUuids, userId); + if (stashed.booleanValue()) { + studyService.stashNetworkModifications(studyUuid, nodeUuid, networkModificationUuids, userId); + } else { + studyService.restoreNetworkModifications(studyUuid, nodeUuid, networkModificationUuids, userId); + } return ResponseEntity.ok().build(); } diff --git a/src/main/java/org/gridsuite/study/server/service/NetworkModificationService.java b/src/main/java/org/gridsuite/study/server/service/NetworkModificationService.java index e7f9a50ea..c9f7e9472 100644 --- a/src/main/java/org/gridsuite/study/server/service/NetworkModificationService.java +++ b/src/main/java/org/gridsuite/study/server/service/NetworkModificationService.java @@ -46,10 +46,7 @@ public class NetworkModificationService { private static final String DELIMITER = "/"; private static final String GROUP_PATH = "groups" + DELIMITER + "{groupUuid}"; - private static final String MODIFICATIONS_PATH = "modifications"; private static final String NETWORK_MODIFICATIONS_PATH = "network-modifications"; - private static final String MODIFICATIONS_RESTORE_PATH = NETWORK_MODIFICATIONS_PATH + "/restore"; - private static final String MODIFICATIONS_STASH_PATH = NETWORK_MODIFICATIONS_PATH + "/stash"; private static final String NETWORK_UUID = "networkUuid"; private static final String REPORT_UUID = "reportUuid"; private static final String REPORTER_ID = "reporterId"; @@ -85,10 +82,10 @@ private String buildPathFrom(UUID networkUuid) { // Return json string because modification dtos are not available here public String getModifications(UUID groupUUid, boolean stashedModifications, boolean onlyMetadata) { Objects.requireNonNull(groupUUid); - var path = UriComponentsBuilder.fromPath(GROUP_PATH + DELIMITER + MODIFICATIONS_PATH) + var path = UriComponentsBuilder.fromPath(GROUP_PATH + DELIMITER + NETWORK_MODIFICATIONS_PATH) .queryParam(QUERY_PARAM_ERROR_ON_GROUP_NOT_FOUND, false) - .queryParam("stashed", stashedModifications) - .queryParam("onlyMetadata", onlyMetadata) + .queryParam(QUERY_PARAM_ONLY_STASHED, stashedModifications) + .queryParam("onlyMetadata", onlyMetadata) .buildAndExpand(groupUUid) .toUriString(); @@ -113,13 +110,13 @@ public void deleteModifications(UUID groupUUid) { } } - public void deleteModifications(UUID groupUuid, List modificationsUuids) { + public void deleteModifications(UUID groupUuid, List modificationsUuids, boolean onlyStashed) { Objects.requireNonNull(groupUuid); - Objects.requireNonNull(modificationsUuids); var path = UriComponentsBuilder .fromUriString(getNetworkModificationServerURI(false) + NETWORK_MODIFICATIONS_PATH) .queryParam(UUIDS, modificationsUuids) .queryParam(GROUP_UUID, groupUuid) + .queryParam(QUERY_PARAM_ONLY_STASHED, onlyStashed) .buildAndExpand() .toUriString(); try { @@ -194,9 +191,10 @@ public void stashModifications(UUID groupUUid, List modificationsUuids) { Objects.requireNonNull(groupUUid); Objects.requireNonNull(modificationsUuids); var path = UriComponentsBuilder - .fromUriString(getNetworkModificationServerURI(false) + MODIFICATIONS_STASH_PATH) + .fromUriString(getNetworkModificationServerURI(false) + NETWORK_MODIFICATIONS_PATH) .queryParam(UUIDS, modificationsUuids) .queryParam(GROUP_UUID, groupUUid) + .queryParam(QUERY_PARAM_STASHED, true) .buildAndExpand() .toUriString(); @@ -205,7 +203,7 @@ public void stashModifications(UUID groupUUid, List modificationsUuids) { HttpEntity httpEntity = new HttpEntity<>(headers); try { - restTemplate.exchange(path, HttpMethod.POST, httpEntity, Void.class); + restTemplate.exchange(path, HttpMethod.PUT, httpEntity, Void.class); } catch (HttpStatusCodeException e) { throw handleHttpError(e, UPDATE_NETWORK_MODIFICATION_FAILED); } @@ -215,9 +213,10 @@ public void restoreModifications(UUID groupUUid, List modificationsUuids) Objects.requireNonNull(groupUUid); Objects.requireNonNull(modificationsUuids); var path = UriComponentsBuilder - .fromUriString(getNetworkModificationServerURI(false) + MODIFICATIONS_RESTORE_PATH) + .fromUriString(getNetworkModificationServerURI(false) + NETWORK_MODIFICATIONS_PATH) .queryParam(UUIDS, modificationsUuids) .queryParam(GROUP_UUID, groupUUid) + .queryParam(QUERY_PARAM_STASHED, false) .buildAndExpand() .toUriString(); @@ -227,7 +226,7 @@ public void restoreModifications(UUID groupUUid, List modificationsUuids) HttpEntity httpEntity = new HttpEntity<>(headers); try { - restTemplate.exchange(path, HttpMethod.POST, httpEntity, Void.class); + restTemplate.exchange(path, HttpMethod.PUT, httpEntity, Void.class); } catch (HttpStatusCodeException e) { throw handleHttpError(e, UPDATE_NETWORK_MODIFICATION_FAILED); } diff --git a/src/main/java/org/gridsuite/study/server/service/NetworkModificationTreeService.java b/src/main/java/org/gridsuite/study/server/service/NetworkModificationTreeService.java index a37980b2b..8eee44401 100644 --- a/src/main/java/org/gridsuite/study/server/service/NetworkModificationTreeService.java +++ b/src/main/java/org/gridsuite/study/server/service/NetworkModificationTreeService.java @@ -552,8 +552,8 @@ public UUID getModificationGroupUuid(UUID nodeUuid) { // Return json string because modification dtos are not available here @Transactional(readOnly = true) - public String getNetworkModifications(@NonNull UUID nodeUuid, boolean stashedModifications, boolean onlyMetadata) { - return networkModificationService.getModifications(getModificationGroupUuid(nodeUuid), stashedModifications, onlyMetadata); + public String getNetworkModifications(@NonNull UUID nodeUuid, boolean onlyStashed, boolean onlyMetadata) { + return networkModificationService.getModifications(getModificationGroupUuid(nodeUuid), onlyStashed, onlyMetadata); } // Return json string because modification dtos are not available here 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 03a07a7cd..f5fc0d7de 100644 --- a/src/main/java/org/gridsuite/study/server/service/StudyService.java +++ b/src/main/java/org/gridsuite/study/server/service/StudyService.java @@ -1522,7 +1522,7 @@ public void changeModificationActiveState(@NonNull UUID studyUuid, @NonNull UUID } @Transactional - public void deleteNetworkModifications(UUID studyUuid, UUID nodeUuid, List modificationsUuids, String userId) { + public void deleteNetworkModifications(UUID studyUuid, UUID nodeUuid, List modificationsUuids, boolean onlyStashed, String userId) { List childrenUuids = networkModificationTreeService.getChildren(nodeUuid); notificationService.emitStartModificationEquipmentNotification(studyUuid, nodeUuid, childrenUuids, NotificationService.MODIFICATIONS_DELETING_IN_PROGRESS); try { @@ -1530,8 +1530,10 @@ public void deleteNetworkModifications(UUID studyUuid, UUID nodeUuid, List throw new StudyException(NOT_ALLOWED); } UUID groupId = networkModificationTreeService.getModificationGroupUuid(nodeUuid); - networkModificationService.deleteModifications(groupId, modificationsUuids); - networkModificationTreeService.removeModificationsToExclude(nodeUuid, modificationsUuids); + networkModificationService.deleteModifications(groupId, modificationsUuids, onlyStashed); + if (modificationsUuids != null) { + networkModificationTreeService.removeModificationsToExclude(nodeUuid, modificationsUuids); + } updateStatuses(studyUuid, nodeUuid, false); } finally { notificationService.emitEndModificationEquipmentNotification(studyUuid, nodeUuid, childrenUuids); diff --git a/src/test/java/org/gridsuite/study/server/NetworkModificationTreeTest.java b/src/test/java/org/gridsuite/study/server/NetworkModificationTreeTest.java index 7b670ae77..3411ee5ff 100644 --- a/src/test/java/org/gridsuite/study/server/NetworkModificationTreeTest.java +++ b/src/test/java/org/gridsuite/study/server/NetworkModificationTreeTest.java @@ -1136,7 +1136,7 @@ public void testGetNetworkModificationsToRestoreByNode() throws Exception { node = buildNetworkModification("modification node 3", "", UUID.fromString(MODIFICATION_GROUP_UUID_STRING), VARIANT_ID, null, null, null, null, null, BuildStatus.BUILT); createNode(root.getStudyId(), root, node, userId); - mockMvc.perform(post("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications/restore?uuids={modificationID1}", root.getStudyId(), node.getId(), MODIFICATION1_UUID_STRING) + mockMvc.perform(put("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications?uuids={modificationID1}&stashed=false", root.getStudyId(), node.getId(), MODIFICATION1_UUID_STRING) .header(USER_ID_HEADER, userId)) .andExpect(status().isOk()); @@ -1172,7 +1172,7 @@ public void testGetNetworkModificationsToStashByNode() throws Exception { node = buildNetworkModification("modification node 3", "", UUID.fromString(MODIFICATION_GROUP_UUID_STRING), VARIANT_ID, null, null, null, null, null, BuildStatus.BUILT); createNode(root.getStudyId(), root, node, userId); - mockMvc.perform(post("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications/stash?uuids={modificationID1}", root.getStudyId(), node.getId(), MODIFICATION1_UUID_STRING) + mockMvc.perform(put("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications?uuids={modificationID1}&stashed=true", root.getStudyId(), node.getId(), MODIFICATION1_UUID_STRING) .header(USER_ID_HEADER, userId)) .andExpect(status().isOk()); diff --git a/src/test/java/org/gridsuite/study/server/VoltageInitTest.java b/src/test/java/org/gridsuite/study/server/VoltageInitTest.java index bf9b744d1..e1adbc058 100644 --- a/src/test/java/org/gridsuite/study/server/VoltageInitTest.java +++ b/src/test/java/org/gridsuite/study/server/VoltageInitTest.java @@ -244,7 +244,7 @@ public MockResponse dispatch(RecordedRequest request) { IdentifiableType.GENERATOR, "genId", Set.of("s1")); return new MockResponse().setResponseCode(200).setBody(objectMapper.writeValueAsString(networkModificationResult)) .addHeader("Content-Type", "application/json; charset=utf-8"); - } else if (path.matches("/v1/groups/" + MODIFICATIONS_GROUP_UUID + "/modifications\\?errorOnGroupNotFound=false&stashed=false&onlyMetadata=.*")) { + } else if (path.matches("/v1/groups/" + MODIFICATIONS_GROUP_UUID + "/network-modifications\\?errorOnGroupNotFound=false&onlyStashed=false&onlyMetadata=.*")) { return new MockResponse().setResponseCode(200).setBody(objectMapper.writeValueAsString(VOLTAGE_INIT_PREVIEW_MODIFICATION_LIST)) .addHeader("Content-Type", "application/json; charset=utf-8"); } else if (path.matches("/v1/results/" + VOLTAGE_INIT_RESULT_UUID + "/stop.*") @@ -470,7 +470,7 @@ public void testCopyVoltageInitModifications() throws Exception { mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/voltage-init/modifications", studyNameUserIdUuid, modificationNode3Uuid) .header("userId", "userId")).andExpect(status().isOk()); assertTrue(TestUtils.getRequestsDone(2, server).stream().allMatch(r -> - r.matches("/v1/groups/" + MODIFICATIONS_GROUP_UUID + "/modifications\\?errorOnGroupNotFound=false&stashed=false&onlyMetadata=false") || + r.matches("/v1/groups/" + MODIFICATIONS_GROUP_UUID + "/network-modifications\\?errorOnGroupNotFound=false&onlyStashed=false&onlyMetadata=false") || r.matches("/v1/results/" + VOLTAGE_INIT_RESULT_UUID + "/modifications-group-uuid") )); diff --git a/src/test/java/org/gridsuite/study/server/utils/WireMockUtils.java b/src/test/java/org/gridsuite/study/server/utils/WireMockUtils.java index 05b4aa079..011f02f20 100644 --- a/src/test/java/org/gridsuite/study/server/utils/WireMockUtils.java +++ b/src/test/java/org/gridsuite/study/server/utils/WireMockUtils.java @@ -130,15 +130,17 @@ public void verifyNetworkEquipmentInfosGet(UUID stubUuid, String networkUuid, St } public UUID stubNetworkModificationGet() { - return wireMock.stubFor(WireMock.get(WireMock.urlPathMatching(URI_NETWORK_MODIFICATION_GROUPS + "/.*/modifications")) + return wireMock.stubFor(WireMock.get(WireMock.urlPathMatching(URI_NETWORK_MODIFICATION_GROUPS + "/.*/network-modifications")) .withQueryParam("errorOnGroupNotFound", WireMock.equalTo("false")) + .withQueryParam("onlyStashed", WireMock.equalTo("false")) .willReturn(WireMock.ok()) ).getId(); } public UUID stubNetworkModificationGet(String groupUuid, String result) { - return wireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo(URI_NETWORK_MODIFICATION_GROUPS + DELIMITER + groupUuid + "/modifications")) + return wireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo(URI_NETWORK_MODIFICATION_GROUPS + DELIMITER + groupUuid + "/network-modifications")) .withQueryParam("errorOnGroupNotFound", WireMock.equalTo("false")) + .withQueryParam(QUERY_PARAM_ONLY_STASHED, WireMock.equalTo("false")) .willReturn(WireMock.ok().withBody(result)) ).getId(); } @@ -204,7 +206,7 @@ public UUID stubNetworkModificationDeleteGroup() { } public void verifyNetworkModificationsGet(UUID stubId, String groupUuid) { - verifyGetRequest(stubId, URI_NETWORK_MODIFICATION_GROUPS + DELIMITER + groupUuid + "/modifications", Map.of("errorOnGroupNotFound", WireMock.equalTo("false"))); + verifyGetRequest(stubId, URI_NETWORK_MODIFICATION_GROUPS + DELIMITER + groupUuid + "/network-modifications", Map.of("errorOnGroupNotFound", WireMock.equalTo("false"))); } public void verifyNetworkModificationPost(UUID stubId, String requestBody, String networkUuid) {