Skip to content

Commit c4b9bbb

Browse files
authored
Replace all spreadsheetConfigs in a collection (#42)
Signed-off-by: Ayoub LABIDI <[email protected]>
1 parent c73e020 commit c4b9bbb

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/main/java/org/gridsuite/studyconfig/server/controller/SpreadsheetConfigCollectionController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ public ResponseEntity<Void> updateSpreadsheetConfigCollection(
9292
return ResponseEntity.noContent().build();
9393
}
9494

95+
@PutMapping("/{id}/spreadsheet-configs/replace-all")
96+
@Operation(summary = "Replace all spreadsheet configs in a collection",
97+
description = "Updates a collection with a list of existing spreadsheet configs, replacing any previous configs")
98+
@ApiResponse(responseCode = "204", description = "Collection updated with configs")
99+
@ApiResponse(responseCode = "404", description = "Collection or one of the configs not found")
100+
public ResponseEntity<Void> updateSpreadsheetConfigCollectionWithConfigs(
101+
@Parameter(description = "ID of the configuration collection") @PathVariable UUID id,
102+
@Parameter(description = "List of spreadsheet config UUIDs to replace the collection's configs")
103+
@Valid @RequestBody List<UUID> configIds) {
104+
spreadsheetConfigService.updateSpreadsheetConfigCollectionWithConfigs(id, configIds);
105+
return ResponseEntity.noContent().build();
106+
}
107+
95108
@PostMapping(params = { "duplicateFrom" })
96109
@Operation(summary = "Duplicate a spreadsheet configuration collection",
97110
description = "Creates a copy of an existing spreadsheet configuration collection")

src/main/java/org/gridsuite/studyconfig/server/service/SpreadsheetConfigService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ public void updateSpreadsheetConfigCollection(UUID id, SpreadsheetConfigCollecti
179179
entity.setNodeAliases(dto.nodeAliases());
180180
}
181181

182+
@Transactional
183+
public void updateSpreadsheetConfigCollectionWithConfigs(UUID id, List<UUID> configUuids) {
184+
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)
185+
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id));
186+
187+
entity.getSpreadsheetConfigs().clear();
188+
entity.getSpreadsheetConfigs().addAll(configUuids.stream()
189+
.map(this::duplicateSpreadsheetConfigEntity)
190+
.toList());
191+
}
192+
182193
@Transactional
183194
public UUID duplicateSpreadsheetConfigCollection(UUID id) {
184195
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)

src/test/java/org/gridsuite/studyconfig/server/SpreadsheetConfigCollectionIntegrationTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,50 @@ void testReorderSpreadsheetConfigs() throws Exception {
277277
assertThat(updatedConfigIds).isEqualTo(newOrder);
278278
}
279279

280+
@Test
281+
void testReplaceAllSpreadsheetConfigs() throws Exception {
282+
// Create a first collection with initial configs
283+
SpreadsheetConfigCollectionInfos initialCollection = new SpreadsheetConfigCollectionInfos(null, createSpreadsheetConfigs(), null);
284+
UUID collectionUuid = postSpreadsheetConfigCollection(initialCollection);
285+
286+
// Get the initial config IDs
287+
List<UUID> initialConfigIds = getSpreadsheetConfigCollection(collectionUuid)
288+
.spreadsheetConfigs()
289+
.stream()
290+
.map(SpreadsheetConfigInfos::id)
291+
.toList();
292+
293+
// Create a second collection with different configs
294+
SpreadsheetConfigCollectionInfos sourceCollection = new SpreadsheetConfigCollectionInfos(null, createUpdatedSpreadsheetConfigs(), null);
295+
UUID sourceCollectionUuid = postSpreadsheetConfigCollection(sourceCollection);
296+
297+
// Get the config IDs from the source collection
298+
List<UUID> sourceConfigIds = getSpreadsheetConfigCollection(sourceCollectionUuid)
299+
.spreadsheetConfigs()
300+
.stream()
301+
.map(SpreadsheetConfigInfos::id)
302+
.toList();
303+
304+
// Call the replace-all endpoint
305+
String configIdsJson = mapper.writeValueAsString(sourceConfigIds);
306+
mockMvc.perform(put(URI_SPREADSHEET_CONFIG_COLLECTION_BASE + "/" + collectionUuid + "/spreadsheet-configs/replace-all")
307+
.content(configIdsJson)
308+
.contentType(MediaType.APPLICATION_JSON))
309+
.andExpect(status().isNoContent());
310+
311+
// Verify the collection now has the new configs
312+
SpreadsheetConfigCollectionInfos updatedCollection = getSpreadsheetConfigCollection(collectionUuid);
313+
List<UUID> updatedConfigIds = updatedCollection.spreadsheetConfigs()
314+
.stream()
315+
.map(SpreadsheetConfigInfos::id)
316+
.toList();
317+
318+
// Check that the initial and updated collections have different configs
319+
assertThat(updatedConfigIds)
320+
.isNotEqualTo(initialConfigIds)
321+
.hasSize(sourceConfigIds.size());
322+
}
323+
280324
private List<SpreadsheetConfigInfos> createSpreadsheetConfigs() {
281325
List<ColumnInfos> columnInfos = Arrays.asList(
282326
new ColumnInfos(null, "cust_a", ColumnType.NUMBER, 1, "cust_b + cust_c", "[\"cust_b\", \"cust_c\"]", "idA"),

0 commit comments

Comments
 (0)