diff --git a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java index c84d8019c..47074e4e5 100644 --- a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java +++ b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java @@ -89,7 +89,7 @@ public ResponseEntity>> handleNetworkMo @RequestBody Pair, List> modificationContextInfos) { return switch (action) { case COPY -> - ResponseEntity.ok().body(networkModificationService.duplicateModifications(targetGroupUuid, modificationContextInfos.getFirst(), modificationContextInfos.getSecond())); + ResponseEntity.ok().body(networkModificationService.duplicateModifications(targetGroupUuid, originGroupUuid, modificationContextInfos.getFirst(), modificationContextInfos.getSecond())); case INSERT -> ResponseEntity.ok().body(networkModificationService.insertCompositeModifications(targetGroupUuid, modificationContextInfos.getFirst(), modificationContextInfos.getSecond())); case MOVE -> { @@ -254,13 +254,6 @@ public ResponseEntity deleteLineTypesCatalog() { return ResponseEntity.ok().build(); } - @PostMapping(value = "/groups/modification", consumes = MediaType.APPLICATION_JSON_VALUE) - @Operation(summary = "Create a group containing a modification") - @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The group with the modification has been created")}) - public ResponseEntity createModificationInGroup(@RequestBody ModificationInfos modificationsInfos) { - return ResponseEntity.ok().body(networkModificationService.createModificationInGroup(modificationsInfos)); - } - @PostMapping(value = "/network-composite-modifications", consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Create a network composite modification") @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The composite modification has been created")}) @@ -313,18 +306,6 @@ public ResponseEntity updateNetworkModificationsActivationStatus( return ResponseEntity.ok().build(); } - @PutMapping(value = "/groups/{groupUuid}/duplications", produces = MediaType.APPLICATION_JSON_VALUE) - @Operation(summary = "Duplicate all modifications in a group and append them at the end of another modifications group") - @ApiResponse(responseCode = "200", description = "The modifications have been duplicated") - public ResponseEntity> duplicateModificationsInGroup(@Parameter(description = "updated group UUID, where modifications are pasted") @PathVariable("groupUuid") UUID targetGroupUuid, - @Parameter(description = "the network uuid", required = true) @RequestParam(value = "networkUuid") UUID networkUuid, - @Parameter(description = "the report uuid", required = true) @RequestParam(value = "reportUuid") UUID reportUuid, - @Parameter(description = "the reporter id", required = true) @RequestParam(value = "reporterId") UUID reporterId, - @Parameter(description = "the variant id", required = true) @RequestParam(value = "variantId") String variantId, - @Parameter(description = "origin group UUID, from where modifications are copied") @RequestParam(value = "duplicateFrom") UUID originGroupUuid) { - return ResponseEntity.ok().body(networkModificationService.duplicateModificationsInGroup(targetGroupUuid, networkUuid, variantId, new ReportInfos(reportUuid, reporterId), originGroupUuid)); - } - @DeleteMapping(value = "/groups/{groupUuid}/stashed-modifications") @Operation(summary = "Delete the stashed modifications in a group") @ApiResponse(responseCode = "200", description = "Stashed modifications in the group deleted") 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 2918041ef..979877c27 100644 --- a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java +++ b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java @@ -325,8 +325,11 @@ private Optional applyModifications(UUID networkUuid, } @Transactional - public List> duplicateModifications(@NonNull UUID targetGroupUuid, @NonNull List modificationsUuids, @NonNull List applicationContexts) { - List modificationInfos = networkModificationRepository.getModificationsInfos(modificationsUuids); + public List> duplicateModifications(@NonNull UUID targetGroupUuid, UUID originGroupUuid, @NonNull List modificationsUuids, @NonNull List applicationContexts) { + if (originGroupUuid != null && !modificationsUuids.isEmpty()) { // Duplicate modifications from a group or from a list + throw new NetworkModificationException(TYPE_MISMATCH); // TODO Create a DUPLICATION_ARGUMENT_INVALID exception type + } + List modificationInfos = originGroupUuid != null ? networkModificationRepository.getActiveModificationsInfos(originGroupUuid) : networkModificationRepository.getModificationsInfos(modificationsUuids); networkModificationRepository.saveModificationInfos(targetGroupUuid, modificationInfos); return applyModifications(modificationInfos, applicationContexts); } @@ -364,12 +367,6 @@ public Optional insertCompositeModifications(UUID tar return applyModifications(networkUuid, variantId, reportInfos, modificationInfos); } - public UUID createModificationInGroup(@NonNull ModificationInfos modificationsInfos) { - UUID groupUuid = UUID.randomUUID(); - networkModificationRepository.saveModificationInfos(groupUuid, List.of(modificationsInfos)); - return groupUuid; - } - @Transactional public UUID createNetworkCompositeModification(@NonNull List modificationUuids) { return networkModificationRepository.createNetworkCompositeModification(modificationUuids); @@ -379,16 +376,6 @@ public Map duplicateModifications(List sourceModificationUuids return networkModificationRepository.duplicateModifications(sourceModificationUuids); } - @Transactional - public Optional duplicateModificationsInGroup(UUID targetGroupUuid, - UUID networkUuid, String variantId, - ReportInfos reportInfos, - UUID originGroupUuid) { - List modificationsInfos = networkModificationRepository.getActiveModificationsInfos(originGroupUuid); - networkModificationRepository.saveModificationInfos(targetGroupUuid, modificationsInfos); - return applyModifications(networkUuid, variantId, reportInfos, modificationsInfos); - } - 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 ce2883410..3d4e742ba 100644 --- a/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java +++ b/src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java @@ -577,16 +577,14 @@ void testCopyModification() throws Exception { assertThat(newModificationListOtherGroup.get(2)).recursivelyEquals(modificationList.get(1)); // Duplicate all modifications in TEST_GROUP_ID, and append them at the end of otherGroupId + applicationContext = new ModificationApplicationContext(TEST_NETWORK_ID, NetworkCreation.VARIANT_ID, TEST_REPORT_ID, UUID.randomUUID()); + bodyJson = objectWriter.writeValueAsString(org.springframework.data.util.Pair.of(List.of(), List.of(applicationContext))); mvcResult = mockMvc.perform( - put("/v1/groups/" + otherGroupId + "/duplications" - + "?networkUuid=" + TEST_NETWORK_ID - + "&reportUuid=" + TEST_REPORT_ID - + "&reporterId=" + UUID.randomUUID() - + "&variantId=" + NetworkCreation.VARIANT_ID - + "&duplicateFrom=" + TEST_GROUP_ID) + put("/v1/groups/" + otherGroupId + "?action=COPY" + "&originGroupUuid=" + TEST_GROUP_ID) + .content(bodyJson) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); - assertApplicationStatusOK(mvcResult); + assertApplicationStatusOKNew(mvcResult); newModificationListOtherGroup = modificationRepository.getModifications(otherGroupId, true, true); // now 8 modifications in new group: first 3 are still the same, 5 last are new duplicates from first group @@ -671,16 +669,14 @@ void testCopyModificationOld() throws Exception { assertThat(newModificationListOtherGroup.get(2)).recursivelyEquals(modificationList.get(1)); // Duplicate all modifications in TEST_GROUP_ID, and append them at the end of otherGroupId + ModificationApplicationContext applicationContext = new ModificationApplicationContext(TEST_NETWORK_ID, NetworkCreation.VARIANT_ID, TEST_REPORT_ID, UUID.randomUUID()); + String bodyJson = objectWriter.writeValueAsString(org.springframework.data.util.Pair.of(List.of(), List.of(applicationContext))); mvcResult = mockMvc.perform( - put("/v1/groups/" + otherGroupId + "/duplications" - + "?networkUuid=" + TEST_NETWORK_ID - + "&reportUuid=" + TEST_REPORT_ID - + "&reporterId=" + UUID.randomUUID() - + "&variantId=" + NetworkCreation.VARIANT_ID - + "&duplicateFrom=" + TEST_GROUP_ID) + put("/v1/groups/" + otherGroupId + "?action=COPY" + "&originGroupUuid=" + TEST_GROUP_ID) + .content(bodyJson) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); - assertApplicationStatusOK(mvcResult); + assertApplicationStatusOKNew(mvcResult); newModificationListOtherGroup = modificationRepository.getModifications(otherGroupId, true, true); // now 8 modifications in new group: first 3 are still the same, 5 last are new duplicates from first group @@ -1633,15 +1629,15 @@ void testCreateVoltageInitModification() throws Exception { .build())) .build(); - MvcResult mvcResult = mockMvc.perform(post("/v1/groups/modification") - .content(objectWriter.writeValueAsString(modificationsInfos1)) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andReturn(); - UUID groupUuid = UUID.fromString(mapper.readValue(mvcResult.getResponse().getContentAsString(), String.class)); + UUID groupUuid = UUID.randomUUID(); + mockMvc.perform(post(URI_NETWORK_MODIF_BASE) + .queryParam("groupUuid", groupUuid.toString()) + .content(objectWriter.writeValueAsString(org.springframework.data.util.Pair.of(modificationsInfos1, List.of()))) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); // Get the modifications - mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=false", groupUuid)).andExpectAll( + MvcResult mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=false", groupUuid)).andExpectAll( status().isOk(), content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); @@ -1786,14 +1782,15 @@ void testApplyModificationsFromUuids() throws Exception { CompositeModificationInfos compositeModificationInfos = CompositeModificationInfos.builder() .modifications(List.of(switchStatusModificationInfos)) .build(); - MvcResult mvcResult = mockMvc.perform(post("/v1/groups/modification") - .content(objectWriter.writeValueAsString(compositeModificationInfos)) + + UUID groupUuid = UUID.randomUUID(); + mockMvc.perform(post(URI_NETWORK_MODIF_BASE) + .queryParam("groupUuid", groupUuid.toString()) + .content(objectWriter.writeValueAsString(org.springframework.data.util.Pair.of(compositeModificationInfos, List.of()))) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andReturn(); - UUID groupUuid = UUID.fromString(mapper.readValue(mvcResult.getResponse().getContentAsString(), String.class)); + .andExpect(status().isOk()); - mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=false", groupUuid)).andExpectAll( + MvcResult mvcResult = mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications?onlyMetadata=false", groupUuid)).andExpectAll( status().isOk(), content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); List modificationsInfos = mapper.readValue(mvcResult.getResponse().getContentAsString(), new TypeReference<>() { }); diff --git a/src/test/java/org/gridsuite/modification/server/VoltageInitReportTest.java b/src/test/java/org/gridsuite/modification/server/VoltageInitReportTest.java index dbbec9625..e5336086d 100644 --- a/src/test/java/org/gridsuite/modification/server/VoltageInitReportTest.java +++ b/src/test/java/org/gridsuite/modification/server/VoltageInitReportTest.java @@ -81,7 +81,7 @@ void testVoltageInitDuplicationLogs(final ApplicationStatus resultStatus, final final UUID networkUuuid = UUID.fromString("11111111-1111-1111-1111-111111111111"); final UUID reportUuid = UUID.fromString("88888888-8888-8888-8888-888888888888"); - //simulate PUT /v1/groups/abc/duplications?networkUuid=0000&reportUuid=0000&reporterId=0000&variantId=0000&duplicateFrom=0000 + //simulate PUT /v1/groups/abc?action=COPY with body ModificationApplicationContext(networkUuid=0000, reportUuid=0000, reporterId=0000, variantId=0000, duplicateFrom=0000) assertThat(networkModificationApplicator.applyModifications( List.of(modificationInfos), new NetworkInfos(network, networkUuuid, true), diff --git a/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java b/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java index fa5bfca49..63e2558ee 100644 --- a/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java +++ b/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java @@ -169,7 +169,7 @@ void testSqlRequestsCountOnPostGroups2() throws Exception { } /* - PUT /v1/groups/{groupUuid}/duplications SQL requests analysis + PUT /v1/groups/{groupUuid}?action=COPY SQL requests analysis Given an example with 2 tabular modifications having 1000 modifications each diff --git a/src/test/java/org/gridsuite/modification/server/utils/ApiUtils.java b/src/test/java/org/gridsuite/modification/server/utils/ApiUtils.java index b780670a3..5fb96225e 100644 --- a/src/test/java/org/gridsuite/modification/server/utils/ApiUtils.java +++ b/src/test/java/org/gridsuite/modification/server/utils/ApiUtils.java @@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.gridsuite.modification.dto.ModificationInfos; +import org.gridsuite.modification.server.dto.ModificationApplicationContext; import org.gridsuite.modification.server.dto.NetworkModificationResult; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; @@ -53,17 +54,19 @@ public static void postGroups(MockMvc mockMvc, UUID originGroupUuid, UUID target } public static Optional putGroupsDuplications(MockMvc mockMvc, UUID originGroupUuid, UUID targetGroupUuid, UUID networkUuid) throws Exception { + ModificationApplicationContext applicationContext = new ModificationApplicationContext(networkUuid, UUID.randomUUID().toString(), UUID.randomUUID(), UUID.randomUUID()); + String bodyJson = getObjectMapper().writeValueAsString(org.springframework.data.util.Pair.of(List.of(), List.of(applicationContext))); MvcResult mvcResult = mockMvc.perform( - put("/v1/groups/{groupUuid}/duplications", targetGroupUuid) - .param("networkUuid", networkUuid.toString()) - .param("reporterId", UUID.randomUUID().toString()) - .param("duplicateFrom", originGroupUuid.toString()) - .param("reportUuid", UUID.randomUUID().toString()) - .param("variantId", UUID.randomUUID().toString()) + put("/v1/groups/{groupUuid}", targetGroupUuid) + .param("action", "COPY") + .param("originGroupUuid", originGroupUuid.toString()) + .content(bodyJson) + .contentType(MediaType.APPLICATION_JSON) ) .andExpectAll(status().isOk()) .andReturn(); - return getObjectMapper().readValue(mvcResult.getResponse().getContentAsString(), new TypeReference<>() { }); + List> result = getObjectMapper().readValue(mvcResult.getResponse().getContentAsString(), new TypeReference<>() { }); + return result.isEmpty() ? Optional.empty() : result.get(0); } public static Optional putGroupsWithCopy(MockMvc mockMvc, UUID targetGroupUuid, List modificationUuids, UUID networkUuid) throws Exception {