Skip to content

Commit 2a96b11

Browse files
authored
Add new endpoint to create a spreadsheet collection from N model Ids (#27)
Signed-off-by: David BRAQUART <[email protected]>
1 parent c932b0b commit 2a96b11

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.http.ResponseEntity;
2222
import org.springframework.web.bind.annotation.*;
2323

24+
import java.util.List;
2425
import java.util.UUID;
2526

2627
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@@ -46,6 +47,16 @@ public ResponseEntity<UUID> createSpreadsheetConfigCollection(@Parameter(descrip
4647
return ResponseEntity.status(HttpStatus.CREATED).body(id);
4748
}
4849

50+
@PostMapping(value = "/merge", consumes = APPLICATION_JSON_VALUE)
51+
@Operation(summary = "Create a new spreadsheet configuration collection duplicating and merging a list of existing configurations",
52+
description = "Creates a new spreadsheet configuration collection and returns its ID")
53+
@ApiResponse(responseCode = "201", description = "Configuration collection created",
54+
content = @Content(schema = @Schema(implementation = UUID.class)))
55+
public ResponseEntity<UUID> createSpreadsheetConfigCollectionFromConfigs(@Parameter(description = "Configurations to duplicate and merge") @Valid @RequestBody List<UUID> configUuids) {
56+
UUID id = spreadsheetConfigService.createSpreadsheetConfigCollectionFromConfigs(configUuids);
57+
return ResponseEntity.status(HttpStatus.CREATED).body(id);
58+
}
59+
4960
@GetMapping("/{id}")
5061
@Operation(summary = "Get a spreadsheet configuration collection",
5162
description = "Retrieves a spreadsheet configuration collection by its ID")

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ public UUID createSpreadsheetConfig(SpreadsheetConfigInfos dto) {
4444

4545
@Transactional
4646
public UUID duplicateSpreadsheetConfig(UUID id) {
47-
SpreadsheetConfigEntity entity = findEntityById(id);
47+
SpreadsheetConfigEntity duplicate = duplicateSpreadsheetConfigEntity(id);
48+
return spreadsheetConfigRepository.save(duplicate).getId();
49+
}
4850

51+
private SpreadsheetConfigEntity duplicateSpreadsheetConfigEntity(UUID id) {
52+
SpreadsheetConfigEntity entity = findEntityById(id);
4953
SpreadsheetConfigEntity duplicate = SpreadsheetConfigEntity.builder()
5054
.sheetType(entity.getSheetType())
5155
.build();
52-
5356
List<CustomColumnEmbeddable> customColumns = entity.getCustomColumns().stream()
5457
.map(column -> CustomColumnEmbeddable.builder()
5558
.name(column.getName())
@@ -60,10 +63,8 @@ public UUID duplicateSpreadsheetConfig(UUID id) {
6063
.id(column.getId())
6164
.build())
6265
.toList();
63-
6466
duplicate.setCustomColumns(customColumns);
65-
66-
return spreadsheetConfigRepository.save(duplicate).getId();
67+
return duplicate;
6768
}
6869

6970
@Transactional(readOnly = true)
@@ -127,6 +128,15 @@ public UUID createSpreadsheetConfigCollection(SpreadsheetConfigCollectionInfos d
127128
return spreadsheetConfigCollectionRepository.save(entity).getId();
128129
}
129130

131+
@Transactional
132+
public UUID createSpreadsheetConfigCollectionFromConfigs(List<UUID> configUuids) {
133+
SpreadsheetConfigCollectionEntity entity = new SpreadsheetConfigCollectionEntity();
134+
entity.setSpreadsheetConfigs(configUuids.stream()
135+
.map(this::duplicateSpreadsheetConfigEntity)
136+
.toList());
137+
return spreadsheetConfigCollectionRepository.save(entity).getId();
138+
}
139+
130140
@Transactional(readOnly = true)
131141
public SpreadsheetConfigCollectionInfos getSpreadsheetConfigCollection(UUID id) {
132142
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class SpreadsheetConfigCollectionIntegrationTest {
5151
private SpreadsheetConfigCollectionRepository spreadsheetConfigCollectionRepository;
5252

5353
@AfterEach
54-
public void tearDown() {
54+
void tearDown() {
5555
spreadsheetConfigCollectionRepository.deleteAll();
5656
}
5757

@@ -135,6 +135,22 @@ void testDuplicateCollection() throws Exception {
135135
assertThat(duplicatedCollection.id()).isNotEqualTo(collectionUuid);
136136
}
137137

138+
@Test
139+
void testMergeModelsIntoNewCollection() throws Exception {
140+
// create a first collection with 2 configs
141+
SpreadsheetConfigCollectionInfos collectionToCreate = new SpreadsheetConfigCollectionInfos(null, createSpreadsheetConfigs());
142+
UUID collectionUuid = postSpreadsheetConfigCollection(collectionToCreate);
143+
List<UUID> configIds = getSpreadsheetConfigCollection(collectionUuid).spreadsheetConfigs().stream().map(SpreadsheetConfigInfos::id).toList();
144+
assertThat(configIds).hasSize(2);
145+
// create a second collection duplicating + merging these existing Configs
146+
UUID mergedCollectionUuid = postMergeSpreadsheetConfigsIntoCollection(configIds);
147+
List<UUID> duplicatedConfigIds = getSpreadsheetConfigCollection(mergedCollectionUuid).spreadsheetConfigs().stream().map(SpreadsheetConfigInfos::id).toList();
148+
149+
assertThat(mergedCollectionUuid).isNotEqualTo(collectionUuid);
150+
assertThat(duplicatedConfigIds).hasSameSizeAs(configIds);
151+
assertThat(duplicatedConfigIds.stream().sorted().toList()).isNotEqualTo(configIds.stream().sorted().toList());
152+
}
153+
138154
private List<SpreadsheetConfigInfos> createSpreadsheetConfigs() {
139155
List<CustomColumnInfos> customColumnInfos = Arrays.asList(
140156
new CustomColumnInfos("cust_a", ColumnType.NUMBER, 1, "cust_b + cust_c", "[\"cust_b\", \"cust_c\"]", "idA"),
@@ -184,6 +200,18 @@ private UUID postSpreadsheetConfigCollection(SpreadsheetConfigCollectionInfos co
184200
return mapper.readValue(mvcPostResult.getResponse().getContentAsString(), UUID.class);
185201
}
186202

203+
private UUID postMergeSpreadsheetConfigsIntoCollection(List<UUID> configIds) throws Exception {
204+
String configIdsJson = mapper.writeValueAsString(configIds);
205+
206+
MvcResult mvcPostResult = mockMvc.perform(post(URI_SPREADSHEET_CONFIG_COLLECTION_BASE + "/merge")
207+
.content(configIdsJson)
208+
.contentType(MediaType.APPLICATION_JSON))
209+
.andExpect(status().isCreated())
210+
.andReturn();
211+
212+
return mapper.readValue(mvcPostResult.getResponse().getContentAsString(), UUID.class);
213+
}
214+
187215
private UUID duplicateSpreadsheetConfigCollection(UUID collectionUuid) throws Exception {
188216
MvcResult mvcPostResult = mockMvc.perform(post(URI_SPREADSHEET_CONFIG_COLLECTION_BASE + "/duplicate")
189217
.queryParam("duplicateFrom", collectionUuid.toString()))

0 commit comments

Comments
 (0)