Skip to content

Commit

Permalink
refactore stashed / restore / delete modifications api (#444)
Browse files Browse the repository at this point in the history
* merge POST /stash and POST /restore into PUT network-modifications 

* DELETE /network-modifications (onlyStashed=false) -> delete all modifications (also stashed modification)
* DELETE /network-modifications?onlyStashed=true -> delete all stashed modifications
* GET /network-modifications (onlyStashed=false) -> get all modifications (also stashed modifications)
* GET /network-modifications?onlyStashed=true -> get all stashed modifications
* PUT /network-modifications?stashed=true -> stash modification
* PUT /network-modifications?stashed=false -> restore modification

Signed-off-by: jamal-khey <[email protected]>
  • Loading branch information
jamal-khey authored Nov 8, 2023
1 parent 4915678 commit 2807ead
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 43 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/gridsuite/study/server/StudyConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 14 additions & 19 deletions src/main/java/org/gridsuite/study/server/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1053,10 +1053,10 @@ public ResponseEntity<List<String>> getAvailableSvgComponentLibraries() {
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The network modifications was returned"), @ApiResponse(responseCode = "404", description = "The study/node is not found")})
public ResponseEntity<String> 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")
Expand Down Expand Up @@ -1089,34 +1089,29 @@ public ResponseEntity<Void> 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<Void> 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<UUID> networkModificationUuids,
@Parameter(description = "Network modification UUIDs") @RequestParam(name = "uuids", required = false) List<UUID> 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<Void> 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<UUID> 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<Void> 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<UUID> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();

Expand All @@ -113,13 +110,13 @@ public void deleteModifications(UUID groupUUid) {
}
}

public void deleteModifications(UUID groupUuid, List<UUID> modificationsUuids) {
public void deleteModifications(UUID groupUuid, List<UUID> 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 {
Expand Down Expand Up @@ -194,9 +191,10 @@ public void stashModifications(UUID groupUUid, List<UUID> 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();

Expand All @@ -205,7 +203,7 @@ public void stashModifications(UUID groupUUid, List<UUID> modificationsUuids) {

HttpEntity<BuildInfos> 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);
}
Expand All @@ -215,9 +213,10 @@ public void restoreModifications(UUID groupUUid, List<UUID> 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();

Expand All @@ -227,7 +226,7 @@ public void restoreModifications(UUID groupUUid, List<UUID> modificationsUuids)
HttpEntity<BuildInfos> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1522,16 +1522,18 @@ public void changeModificationActiveState(@NonNull UUID studyUuid, @NonNull UUID
}

@Transactional
public void deleteNetworkModifications(UUID studyUuid, UUID nodeUuid, List<UUID> modificationsUuids, String userId) {
public void deleteNetworkModifications(UUID studyUuid, UUID nodeUuid, List<UUID> modificationsUuids, boolean onlyStashed, String userId) {
List<UUID> childrenUuids = networkModificationTreeService.getChildren(nodeUuid);
notificationService.emitStartModificationEquipmentNotification(studyUuid, nodeUuid, childrenUuids, NotificationService.MODIFICATIONS_DELETING_IN_PROGRESS);
try {
if (!networkModificationTreeService.getStudyUuidForNodeId(nodeUuid).equals(studyUuid)) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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());

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/gridsuite/study/server/VoltageInitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*")
Expand Down Expand Up @@ -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")
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2807ead

Please sign in to comment.