Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
Signed-off-by: Slimane AMAR <[email protected]>
  • Loading branch information
Slimane AMAR committed Jan 22, 2025
1 parent 126de23 commit a5386c1
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.tuple.Pair;
import org.gridsuite.modification.NetworkModificationException;
import org.gridsuite.modification.dto.ModificationInfos;
import org.gridsuite.modification.server.dto.*;
import org.gridsuite.modification.server.dto.catalog.LineTypeInfos;
import org.gridsuite.modification.server.service.LineTypesCatalogService;
import org.gridsuite.modification.server.service.NetworkModificationService;
import org.springframework.data.util.Pair;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -78,7 +78,7 @@ public ResponseEntity<Void> duplicateGroup(@RequestParam("groupUuid") UUID group
return ResponseEntity.ok().build();
}

@PutMapping(value = "/groups/{groupUuid}", produces = MediaType.APPLICATION_JSON_VALUE)
@PutMapping(value = "/groups/{groupUuid}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "For a list of network modifications passed in body, Move them before another one or at the end of the list, or Duplicate them at the end of the list")
@ApiResponse(responseCode = "200", description = "The modification list of the group has been updated.")
public ResponseEntity<List<Optional<NetworkModificationResult>>> handleNetworkModifications(@Parameter(description = "updated group UUID, where modifications are pasted") @PathVariable("groupUuid") UUID targetGroupUuid,
Expand All @@ -89,16 +89,16 @@ public ResponseEntity<List<Optional<NetworkModificationResult>>> handleNetworkMo
@RequestBody Pair<List<UUID>, List<ModificationApplicationContext>> modificationContextInfos) {
return switch (action) {
case COPY ->
ResponseEntity.ok().body(networkModificationService.duplicateModifications(targetGroupUuid, modificationContextInfos.getLeft(), modificationContextInfos.getRight()));
ResponseEntity.ok().body(networkModificationService.duplicateModifications(targetGroupUuid, modificationContextInfos.getFirst(), modificationContextInfos.getSecond()));
case INSERT ->
ResponseEntity.ok().body(networkModificationService.insertCompositeModifications(targetGroupUuid, modificationContextInfos.getLeft(), modificationContextInfos.getRight()));
ResponseEntity.ok().body(networkModificationService.insertCompositeModifications(targetGroupUuid, modificationContextInfos.getFirst(), modificationContextInfos.getSecond()));
case MOVE -> {
UUID sourceGroupUuid = originGroupUuid == null ? targetGroupUuid : originGroupUuid;
boolean canBuildNode = build;
if (sourceGroupUuid.equals(targetGroupUuid)) {
canBuildNode = false;
}
yield ResponseEntity.ok().body(networkModificationService.moveModifications(targetGroupUuid, sourceGroupUuid, beforeModificationUuid, modificationContextInfos.getLeft(), modificationContextInfos.getRight(), canBuildNode));
yield ResponseEntity.ok().body(networkModificationService.moveModifications(targetGroupUuid, sourceGroupUuid, beforeModificationUuid, modificationContextInfos.getFirst(), modificationContextInfos.getSecond(), canBuildNode));
}
};
}
Expand Down Expand Up @@ -161,8 +161,8 @@ public ResponseEntity<List<UUID>> getModificationGroups() {
public ResponseEntity<List<Optional<NetworkModificationResult>>> createNetworkModification(
@Parameter(description = "Group UUID") @RequestParam(name = "groupUuid") UUID groupUuid,
@RequestBody Pair<ModificationInfos, List<ModificationApplicationContext>> modificationContextInfos) {
modificationContextInfos.getLeft().check();
return ResponseEntity.ok().body(networkModificationService.createNetworkModification(groupUuid, modificationContextInfos.getLeft(), modificationContextInfos.getRight()));
modificationContextInfos.getFirst().check();
return ResponseEntity.ok().body(networkModificationService.createNetworkModification(groupUuid, modificationContextInfos.getFirst(), modificationContextInfos.getSecond()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

import java.util.UUID;

public record ModificationApplicationContext(UUID networkUuid, String variantId, UUID reportUuid, UUID nodeUuid) { }
public record ModificationApplicationContext(UUID networkUuid, String variantId, UUID reportUuid, UUID reporterId) { }
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public void deleteAll() {
// This method should be package-private and not used as API of the service as it uses ModificationEntity and
// we want to encapsulate the use of Entity related objects to this service.
// Nevertheless We have to keep it public for transactional annotation.
public List<? extends ModificationEntity> saveModifications(UUID groupUuid, List<? extends ModificationEntity> modifications) {
public List<ModificationEntity> saveModifications(UUID groupUuid, List<ModificationEntity> modifications) {
return saveModificationsNonTransactional(groupUuid, modifications);
}

@Transactional // To have all create in the same transaction (atomic)
// TODO Remove transaction when errors will no longer be sent to the front
public List<? extends ModificationEntity> saveModificationInfos(UUID groupUuid, List<? extends ModificationInfos> modifications) {
public List<ModificationEntity> saveModificationInfos(UUID groupUuid, List<ModificationInfos> modifications) {
List<ModificationEntity> entities = modifications.stream().map(ModificationEntity::fromDTO).toList();

return saveModificationsNonTransactional(groupUuid, entities);
Expand All @@ -85,7 +85,7 @@ public UUID createNetworkCompositeModification(@NonNull List<UUID> modificationU
return modificationRepository.save(compositeEntity).getId();
}

private List<? extends ModificationEntity> saveModificationsNonTransactional(UUID groupUuid, List<? extends ModificationEntity> modifications) {
private List<ModificationEntity> saveModificationsNonTransactional(UUID groupUuid, List<ModificationEntity> modifications) {
int order = modificationRepository.countByGroupIdAndStashed(groupUuid, false);
var modificationGroupEntity = this.modificationGroupRepository
.findById(groupUuid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public List<Optional<NetworkModificationResult>> createNetworkModification(@NonN
}

/**
* TODO : Remove this endpoint after the final integration of root networks
* Need to use tne new endpoint with ModificationApplicationContext DTO (see above)
* TODO : Remove this method after the final integration of root networks
* Need to use tne new method with ModificationApplicationContext DTO (see above)
*/
public Optional<NetworkModificationResult> createNetworkModification(@NonNull UUID networkUuid, String variantId, @NonNull UUID groupUuid,
@NonNull ReportInfos reportInfos,
Expand All @@ -175,7 +175,7 @@ private List<Optional<NetworkModificationResult>> applyModifications(List<Modifi
return applicationContexts.stream().map(modificationApplicationContext ->
applyModifications(modificationApplicationContext.networkUuid(),
modificationApplicationContext.variantId(),
new ReportInfos(modificationApplicationContext.reportUuid(), modificationApplicationContext.nodeUuid()),
new ReportInfos(modificationApplicationContext.reportUuid(), modificationApplicationContext.reporterId()),
modifications)
).toList();
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public void deleteNetworkModifications(UUID groupUuid, List<UUID> modificationsU
}

@Transactional
public List<Optional<NetworkModificationResult>> moveModifications(@NonNull UUID destinationGroupUuid, @NonNull UUID originGroupUuid, @NonNull UUID beforeModificationUuid,
public List<Optional<NetworkModificationResult>> moveModifications(@NonNull UUID destinationGroupUuid, @NonNull UUID originGroupUuid, UUID beforeModificationUuid,
@NonNull List<UUID> modificationsToMoveUuids, @NonNull List<ModificationApplicationContext> applicationContexts,
boolean canBuildNode) {
// update origin/destinations groups to cut and paste all modificationsToMove
Expand All @@ -265,8 +265,8 @@ public List<Optional<NetworkModificationResult>> moveModifications(@NonNull UUID
}

/**
* TODO : Remove this endpoint after the final integration of root networks
* Need to use the new endpoint with ModificationApplicationContext DTO (see above)
* TODO : Remove this method after the final integration of root networks
* Need to use the new method with ModificationApplicationContext DTO (see above)
*/
@Transactional
public Optional<NetworkModificationResult> moveModifications(UUID destinationGroupUuid, UUID originGroupUuid,
Expand Down Expand Up @@ -332,8 +332,8 @@ public List<Optional<NetworkModificationResult>> duplicateModifications(@NonNull
}

/**
* TODO : Remove this endpoint after the final integration of root networks
* Need to use the new endpoint with ModificationApplicationContext DTO (see above)
* TODO : Remove this method after the final integration of root networks
* Need to use the new method with ModificationApplicationContext DTO (see above)
*/
@Transactional
public Optional<NetworkModificationResult> duplicateModifications(UUID targetGroupUuid,
Expand All @@ -352,8 +352,8 @@ public List<Optional<NetworkModificationResult>> insertCompositeModifications(@N
}

/**
* TODO : Remove this endpoint after the final integration of root networks (used only for move)
* Need to use the new endpoint with ModificationApplicationContext DTO (see above)
* TODO : Remove this method after the final integration of root networks (used only for move)
* Need to use the new method with ModificationApplicationContext DTO (see above)
*/
@Transactional
public Optional<NetworkModificationResult> insertCompositeModifications(UUID targetGroupUuid,
Expand Down
Loading

0 comments on commit a5386c1

Please sign in to comment.